forked from Kandepet/HackerNews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vote.php
131 lines (109 loc) · 4.41 KB
/
vote.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
<?php
require 'vendor/autoload.php';
// Connect to database
$config = new HackerNews\Config('config.ini');
$base_url = $config['site.base_url'] . $config['site.base_path'] . '/';
$db = HackerNews\Common::database($config);
// Start the session
session_start();
// If no type then it's for a story
if(!isset($_GET['type'])) {
$_GET['type'] = 'story';
}
// Check user is logged in
if(isset($_SESSION['hn_login']['id'])) {
if($_GET['type'] == 'story') {
if(!isset($_SESSION['voted'][$_GET['i']])) {
// Lookup votes table to see if they have already voted this
$db->where("story_id", hackerNews\Common::validate_input($_GET['i']));
$db->where("user_id", $_SESSION['hn_login']['id']);
$voted = $db->getValue("votes", "count(*)");
// Check that they havn't voted it
if(!$voted) {
// Update voted count on story
$update = Array(
"story_votes" => $db->inc(1),
"story_last5" => $db->inc(1)
);
$db->where("story_id", hackerNews\Common::validate_input($_GET['i']));
$db->update("stories", $update);
// Insert vote into votes table
$insert = Array(
"story_id" => hackerNews\Common::validate_input($_GET['i']),
"user_id" => $_SESSION['hn_login']['id'],
"time" => time()
);
$db->insert("votes", $insert);
// Check number of votes on story
$db->where("story_id", hackerNews\Common::validate_input($_GET['i']));
$votes = $db->getOne("stories", "story_votes");
// Register that they have voted this story in session
$_SESSION['voted'][$_GET['i']] = true;
// Return success and number of votes
echo '1|'.$_GET['i'].'|'.$votes['story_votes'];
} else {
// User has already voted this story
echo '2|'.$_GET['i'].'|'.$votes['story_votes'];
}
} else {
// User has already voted this story
echo '2|'.$_GET['i'].'|'.$votes['story_votes'];
}
} elseif($_GET['type'] == 'comm') {
// Lookup votes table to see if they have already voted this
$db->where("comment_id", hackerNews\Common::validate_input($_GET['i']));
$db->where("user_id", $_SESSION['hn_login']['id']);
$voted = $db->getValue("comment_votes", "count(*)");
// Check that they havn't voted it
if(!$voted) {
// Update voted count on story
if($_GET['dir']) {
$update = Array(
"comment_votes" => $db->inc(1),
);
$db->where("comment_id", hackerNews\Common::validate_input($_GET['i']));
$db->update("comments", $update);
// Insert vote into votes table
$insert = Array(
"story_id" => hackerNews\Common::validate_input($_GET['story']),
"comment_id" => hackerNews\Common::validate_input($_GET['i']),
"user_id" => $_SESSION['hn_login']['id'],
"vote" => 1,
"time" => time()
);
$db->insert("comment_votes", $insert);
} else {
$update = Array(
"comment_votes" => $db->inc(1),
);
$db->where("comment_id", hackerNews\Common::validate_input($_GET['i']));
$db->update("comments", $update);
// Insert vote into votes table
$insert = Array(
"story_id" => hackerNews\Common::validate_input($_GET['story']),
"comment_id" => hackerNews\Common::validate_input($_GET['i']),
"user_id" => $_SESSION['hn_login']['id'],
"vote" => 1,
"time" => time()
);
$db->insert("comment_votes", $insert);
}
// Check number of votes on story
$db->where("comment_id", hackerNews\Common::validate_input($_GET['i']));
$votes = $db->getOne("comments", "comment_votes");
//print_r($votes);
if($votes['comment_votes'] > -1) {
$votes['comment_votes'] = '+'.$votes['comment_votes'];
}
// Return success and number of votes
echo '1|'.$_GET['i'].'|'.$votes['comment_votes'];
} else {
// User has already voted this story
echo '2|'.$_GET['i'].'|'.$votes['comment_votes'];
}
}
} else {
// User isn't logged in
echo '0|'.$_GET['i'].'|0';
}
?>