forked from coppermine-gallery/cpg1.6.x
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ratepic.php
173 lines (133 loc) · 5.23 KB
/
ratepic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/*************************
Coppermine Photo Gallery
************************
Copyright (c) 2003-2016 Coppermine Dev Team
v1.0 originally written by Gregory Demar
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3
as published by the Free Software Foundation.
********************************************
Coppermine version: 1.6.01
$HeadURL$
**********************************************/
define('IN_COPPERMINE', true);
define('RATEPIC_PHP', true);
require('include/init.inc.php');
header("Content-Type: text/plain");
// Check if required parameters are present
if (!$superCage->get->keyExists('pic') || !$superCage->get->keyExists('rate')) {
//send back voting failure to ajax request
$send_back = array(
'status' => 'error',
'msg' => $lang_errors['param_missing'],
);
echo json_encode($send_back);
exit;
}
$rating_stars_amount = ($CONFIG['old_style_rating']) ? 5 : $CONFIG['rating_stars_amount'];
$pic = $superCage->get->getInt('pic');
$rate = $superCage->get->getInt('rate');
$rate = min($rate, $rating_stars_amount);
$rate = max($rate, 0);
// Retrieve picture/album information & check if user can rate picture
$sql = "SELECT a.votes as votes_allowed, p.votes as votes, pic_rating, owner_id FROM {$CONFIG['TABLE_PICTURES']} AS p INNER JOIN {$CONFIG['TABLE_ALBUMS']} AS a ON p.aid = a.aid WHERE pid = $pic";
$result = cpg_db_query($sql);
if (!$result->numRows()) {
//send back voting failure to ajax request
$send_back = array(
'status' => 'error',
'msg' => $lang_errors['non_exist_ap'],
);
echo json_encode($send_back);
exit;
}
//Check if the form token is valid
if(!checkFormToken()){
//send back voting failure to ajax request
$send_back = array(
'status' => 'error',
'msg' => $lang_errors['invalid_form_token'],
);
echo json_encode($send_back);
exit;
}
$row = $result->fetchAssoc(true);
if (!USER_CAN_RATE_PICTURES || $row['votes_allowed'] == 'NO') {
//send back voting failure to ajax request
$send_back = array(
'status' => 'error',
'msg' => $lang_errors['perm_denied'],
);
echo json_encode($send_back);
exit;
}
// Clean votes older votes
$clean_before = time() - $CONFIG['keep_votes_time'] * 86400;
$sql = "DELETE FROM {$CONFIG['TABLE_VOTES']} WHERE vote_time < $clean_before";
$result = cpg_db_query($sql);
// Check if user already rated this picture
$user_md5_id = USER_ID ? md5(USER_ID) : $USER['ID'];
$sql = "SELECT null FROM {$CONFIG['TABLE_VOTES']} WHERE pic_id = $pic AND user_md5_id = '$user_md5_id'";
$result = cpg_db_query($sql);
if ($result->numRows(true)) {
// user has already rated this file
$send_back = array(
'status' => 'error',
'msg' => $lang_rate_pic_php['already_rated'],
'a' => $USER,
);
echo json_encode($send_back);
exit;
}
// Check if user already rated this picture - vote stats table
$sql = "SELECT null FROM {$CONFIG['TABLE_VOTE_STATS']} WHERE pid = $pic AND ip = '$raw_ip'";
$result = cpg_db_query($sql);
if ($result->numRows(true)) {
$send_back = array(
'status' => 'error',
'msg' => $lang_rate_pic_php['already_rated'],
'a' => $USER,
);
echo json_encode($send_back);
exit;
}
//Test for Self-Rating
if (!empty($user_id) && $user_id == $row['owner_id'] && ($CONFIG['rate_own_files'] == 0 || $CONFIG['rate_own_files'] == 2 && !USER_IS_ADMIN)) {
$send_back = array(
'status' => 'error',
'msg' => $lang_rate_pic_php['forbidden'],
);
echo json_encode($send_back);
exit;
}
// Update picture rating
$new_rating = round(($row['votes'] * $row['pic_rating'] + ($rate * (5 / $rating_stars_amount)) * 2000) / ($row['votes'] + 1));
$sql = "UPDATE {$CONFIG['TABLE_PICTURES']} SET pic_rating = $new_rating, votes = votes + 1 WHERE pid = $pic";
$result = cpg_db_query($sql);
// Update the votes table
$sql = "INSERT INTO {$CONFIG['TABLE_VOTES']} (pic_id, user_md5_id, vote_time) VALUES ($pic, '$user_md5_id', ".time().")";
$result = cpg_db_query($sql);
//
// Code to record the details of votes for the picture if the option is set in CONFIG
//
if ($CONFIG['vote_details']) {
// Get the details of user browser, IP, OS, etc
$client_details = cpg_determine_client();
// Code to write the user id if a user is logged in
$voteUserId = USER_ID;
$referer = urlencode($superCage->post->getEscaped('HTTP_REFERER'));
// Insert the record in database
$query = "INSERT INTO {$CONFIG['TABLE_VOTE_STATS']} (pid, rating, Ip, sdate, referer, browser, os, uid) VALUES ($pic, $rate, '$raw_ip', ".time().", '$referer', '{$client_details['browser']}', '{$client_details['os']}', $voteUserId)";
cpg_db_query($query);
}
$new_rating = round(($new_rating / 2000) / (5 / $rating_stars_amount), 1);
$new_rating_text = $lang_rate_pic_php['rate_ok'] . ' ' . sprintf($lang_rate_pic['rating'], $new_rating, $rating_stars_amount, $row['votes'] + 1);
$send_back = array(
'status' => 'success',
'msg' => $lang_rate_pic_php['rate_ok'],
'new_rating_text' => $new_rating_text,
'new_rating' => round($new_rating, 0),
);
echo json_encode($send_back);
//EOF