-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvote.php
More file actions
executable file
·60 lines (45 loc) · 2.32 KB
/
vote.php
File metadata and controls
executable file
·60 lines (45 loc) · 2.32 KB
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
<?php /*====================================================================================
SamNews [http://samjlevy.com/samnews], open-source PHP social news application
sam j levy [http://samjlevy.com]
This program is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this
program. If not, see <http://www.gnu.org/licenses/>.
====================================================================================*/
include('config.php');
// check to make sure user is logged in
if(isset($_SESSION['user'],$_POST['id'])) {
$post_id = esc($_POST['id']);
// check to make sure user isn't author of post
$author_check = samq("post","author",NULL,"author = " . esc($_SESSION['user_id']) . " AND id = " . $post_id);
// check to make sure user hasn't already voted
$vote_check = samq("vote_post","userid",NULL,"userid = " . esc($_SESSION['user_id']) . " AND post = " . $post_id);
if(count($author_check) == 0 && count($vote_check) == 0) {
// retrieve number of votes for this post
$current_count = samq("post","score",NULL,"id = " . $post_id);
$current_count = $current_count[0]['score'];
// update authors voted count
samq_c("UPDATE users INNER JOIN post ON users.id = author SET voted_count = voted_count + 1 WHERE post.id = " . $post_id);
// record users vote
samq_i("vote_post",array("post","userid","created"),array($post_id,$_SESSION['user_id'],DATETIME_NOW));
// update user's vote count
samq_c("UPDATE users SET vote_count = vote_count + 1 WHERE id = " . esc($_SESSION['user_id']));
// update the vote
if(samq_c("UPDATE post SET score = score + 1 WHERE id = " . $post_id)) {
// voting done
echo $current_count + 1;
}
else {
echo "Voting failed";
}
}
} else {
header("Location: " . SITE_URL);
die();
}
?>