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
174
175
176
|
<?php namespace HashOver;
// Copyright (C) 2010-2018 Jacob Barkdull
// This file is part of HashOver.
//
// HashOver is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// HashOver 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with HashOver. If not, see <http://www.gnu.org/licenses/>.
//
//--------------------
//
// Script Description:
//
// This script reads a given comment file, retrieves the like count,
// increases the count by one, then writes the file. Assuming the
// visitor hasn't already liked the given comment before and the
// visitor isn't the comment's original poster.
// Check if request is for JSONP
if (isset ($_GET['jsonp'])) {
// If so, setup HashOver for JavaScript
require ('javascript-setup.php');
} else {
// If not, setup HashOver for JSON
require ('json-setup.php');
}
// Sets cookie indicating what comment was liked
function set_like (&$hashover, $like_hash, $set, &$likes)
{
$hashover->cookies->set ($like_hash, $set, mktime (0, 0, 0, 11, 26, 3468));
$likes = $likes + 1;
}
// Decreases a like/dislike count
function like_decrease (&$likes)
{
if ($likes > 0) {
$likes = $likes - 1;
}
}
// Likes or dislikes a comment
function liker ($action, $like_hash, &$hashover, &$comment)
{
// Get the comment array key based on given action
$key = ($action === 'like') ? 'likes' : 'dislikes';
$set = ($action === 'like') ? 'liked' : 'disliked';
// Get like cookie
$like_cookie = $hashover->cookies->getValue ($like_hash);
// Check that a like/dislike cookie is not already set
if ($like_cookie === null) {
// If so, set the cookie and increase the like/dislike count
set_like ($hashover, $like_hash, $set, $comment[$key]);
} else {
// If not, we're unliking/un-disliking the comment
$opposite_key = ($action === 'like') ? 'dislikes' : 'likes';
$opposite_set = ($action === 'like') ? 'disliked' : 'liked';
// Check if the user has liked the comment
if ($like_cookie === $set) {
// If so, expire the like cookie
$hashover->cookies->expireCookie ($like_hash);
// And decrease the like count
like_decrease ($comment[$key]);
}
// Check if the user has disliked the comment
if ($like_cookie === $opposite_set) {
// If so, expire the dislike cookie
set_like ($hashover, $like_hash, $set, $comment[$key]);
// And decrease the dislike count
like_decrease ($comment[$opposite_key]);
}
}
}
function get_json_response ($hashover, $key, $action)
{
// JSON data
$data = array ();
// Store references to some long variables
$storageMode =& $hashover->thread->data->storageMode;
$thread = $hashover->setup->threadName;
// Sanitize file path
$file = str_replace ('../', '', $key);
// Read comment
$comment = $hashover->thread->data->read ($file, $thread);
// Return error message if failed to read comment
if ($comment === false) {
return array ('error' => 'Failed to read file: "' . $file . '"');
}
// Check if liker isn't poster via login ID comparision
if ($hashover->login->userIsLoggedIn and !empty ($comment['login_id'])) {
if ($hashover->cookies->getValue ('login') === $comment['login_id']) {
// Return error message if liker posted the comment
return array ('message' => 'Practice altruism!');
}
}
// Name of the cookie used to indicate liked comments
$like_hash = md5 ($hashover->setup->domain . $thread . '/' . $key);
// Action: like or dislike
$action = ($action !== 'dislike') ? 'like' : 'dislike';
// Like or dislike the comment
liker ($action, $like_hash, $hashover, $comment);
// Attempt to save file with updated like count
if ($hashover->thread->data->save ($file, $comment, true, $thread)) {
// If successful, add number of likes to JSON
if (isset ($comment['likes'])) {
$data['likes'] = $comment['likes'];
}
// And add dislikes to JSON as well
if (isset ($comment['dislikes'])) {
$data['dislikes'] = $comment['dislikes'];
}
} else {
// If failed, add error message to JSON
$data['error'] = 'Failed to save comment file!';
}
return $data;
}
try {
// Instanciate HashOver class
$hashover = new \HashOver ('json');
// Get required POST/GET data
$url = $hashover->setup->getRequest ('url', null);
$key = $hashover->setup->getRequest ('comment', null);
$action = $hashover->setup->getRequest ('action', null);
// Return error if we're missing necessary post data
if (($url and $key and $action) === null) {
return array ('error' => 'No action.');
}
// Continue HashOver setup
$hashover->setup->setPageURL ($url);
$hashover->initiate ();
// Display JSON response
$data = get_json_response ($hashover, $key, $action);
// Return JSON or JSONP function call
echo $hashover->misc->jsonData ($data);
} catch (\Exception $error) {
$misc = new Misc ('json');
$message = $error->getMessage ();
$misc->displayError ($message);
}
|