author | Dan |
Sun, 12 Aug 2007 13:33:04 -0400 | |
changeset 89 | fea81844b9a5 |
parent 86 | c162ca39db8f |
child 108 | 1c7f59df9474 |
permissions | -rw-r--r-- |
1 | 1 |
<?php |
2 |
||
3 |
/* |
|
4 |
* Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between |
|
73
0a74676a2f2f
Made the move to Loch Ness, and got some basic page grouping functionality working. TODO: fix some UI issues in Javascript ACL editor and change non-JS ACL editor to work with page groups too
Dan
parents:
29
diff
changeset
|
5 |
* Version 1.0.1 (Loch Ness) |
1 | 6 |
* Copyright (C) 2006-2007 Dan Fuhry |
7 |
* |
|
8 |
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License |
|
9 |
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
|
12 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
|
13 |
*/ |
|
14 |
||
15 |
/** |
|
16 |
* Class that handles comments. Has HTML/Javascript frontend support. |
|
17 |
* @package Enano CMS |
|
18 |
* @subpackage Comment manager |
|
19 |
* @license GNU General Public License <http://www.gnu.org/licenses/gpl.html> |
|
20 |
*/ |
|
21 |
||
22 |
class Comments |
|
23 |
{ |
|
24 |
# |
|
25 |
# VARIABLES |
|
26 |
# |
|
27 |
||
28 |
/** |
|
29 |
* Current list of comments. |
|
30 |
* @var array |
|
31 |
*/ |
|
32 |
||
33 |
var $comments = Array(); |
|
34 |
||
35 |
/** |
|
36 |
* Object to track permissions. |
|
37 |
* @var object |
|
38 |
*/ |
|
39 |
||
40 |
var $perms; |
|
41 |
||
42 |
# |
|
43 |
# METHODS |
|
44 |
# |
|
45 |
||
46 |
/** |
|
47 |
* Constructor. |
|
48 |
* @param string Page ID of the page to load comments for |
|
49 |
* @param string Namespace of the page to load comments for |
|
50 |
*/ |
|
51 |
||
52 |
function __construct($page_id, $namespace) |
|
53 |
{ |
|
54 |
global $db, $session, $paths, $template, $plugins; // Common objects |
|
55 |
||
56 |
// Initialize permissions |
|
57 |
if ( $page_id == $paths->cpage['urlname_nons'] && $namespace == $paths->namespace ) |
|
58 |
$this->perms =& $GLOBALS['session']; |
|
59 |
else |
|
60 |
$this->perms = $session->fetch_page_acl($page_id, $namespace); |
|
61 |
||
62 |
$this->page_id = $db->escape($page_id); |
|
63 |
$this->namespace = $db->escape($namespace); |
|
64 |
} |
|
65 |
||
66 |
/** |
|
67 |
* PHP 4 constructor. |
|
68 |
* @see Comments::__construct |
|
69 |
*/ |
|
70 |
function Comments($page_id, $namespace) |
|
71 |
{ |
|
72 |
$this->__construct($page_id, $namespace); |
|
73 |
} |
|
74 |
||
75 |
/** |
|
76 |
* Processes a command in JSON format. |
|
77 |
* @param string The JSON-encoded input, probably something sent from the Javascript/AJAX frontend |
|
78 |
*/ |
|
79 |
||
80 |
function process_json($json) |
|
81 |
{ |
|
82 |
global $db, $session, $paths, $template, $plugins; // Common objects |
|
83 |
$parser = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); |
|
84 |
$data = $parser->decode($json); |
|
78
4df25dfdde63
Modified Text_Wiki parser to fully support UTF-8 strings; several other UTF-8 fixes, international characters seem to work reasonably well now
Dan
parents:
74
diff
changeset
|
85 |
$data = decode_unicode_array($data); |
1 | 86 |
if ( !isset($data['mode']) ) |
87 |
{ |
|
86
c162ca39db8f
Finished pagination code (was incomplete in previous revision) and added a few hacks for an upcoming theme
Dan
parents:
78
diff
changeset
|
88 |
$ret = Array('mode'=>'error','error'=>'No mode defined!'); |
c162ca39db8f
Finished pagination code (was incomplete in previous revision) and added a few hacks for an upcoming theme
Dan
parents:
78
diff
changeset
|
89 |
echo $parser->encode($ret); |
c162ca39db8f
Finished pagination code (was incomplete in previous revision) and added a few hacks for an upcoming theme
Dan
parents:
78
diff
changeset
|
90 |
return $ret; |
c162ca39db8f
Finished pagination code (was incomplete in previous revision) and added a few hacks for an upcoming theme
Dan
parents:
78
diff
changeset
|
91 |
} |
c162ca39db8f
Finished pagination code (was incomplete in previous revision) and added a few hacks for an upcoming theme
Dan
parents:
78
diff
changeset
|
92 |
if ( getConfig('enable_comments') == '0' ) |
c162ca39db8f
Finished pagination code (was incomplete in previous revision) and added a few hacks for an upcoming theme
Dan
parents:
78
diff
changeset
|
93 |
{ |
c162ca39db8f
Finished pagination code (was incomplete in previous revision) and added a few hacks for an upcoming theme
Dan
parents:
78
diff
changeset
|
94 |
$ret = Array('mode'=>'error','error'=>'Comments are not enabled on this site.'); |
c162ca39db8f
Finished pagination code (was incomplete in previous revision) and added a few hacks for an upcoming theme
Dan
parents:
78
diff
changeset
|
95 |
echo $parser->encode($ret); |
c162ca39db8f
Finished pagination code (was incomplete in previous revision) and added a few hacks for an upcoming theme
Dan
parents:
78
diff
changeset
|
96 |
return $ret; |
1 | 97 |
} |
98 |
$ret = Array(); |
|
99 |
$ret['mode'] = $data['mode']; |
|
100 |
switch ( $data['mode'] ) |
|
101 |
{ |
|
102 |
case 'fetch': |
|
103 |
if ( !$template->theme_loaded ) |
|
104 |
$template->load_theme(); |
|
105 |
if ( !isset($data['have_template']) ) |
|
106 |
{ |
|
107 |
$ret['template'] = file_get_contents(ENANO_ROOT . '/themes/' . $template->theme . '/comment.tpl'); |
|
108 |
} |
|
109 |
$q = $db->sql_query('SELECT c.comment_id,c.name,c.subject,c.comment_data,c.time,c.approved,u.user_level,u.user_id,u.signature FROM '.table_prefix.'comments AS c |
|
110 |
LEFT JOIN '.table_prefix.'users AS u |
|
111 |
ON (u.user_id=c.user_id) |
|
112 |
WHERE page_id=\'' . $this->page_id . '\' |
|
113 |
AND namespace=\'' . $this->namespace . '\';'); |
|
114 |
$count_appr = 0; |
|
115 |
$count_total = 0; |
|
116 |
$count_unappr = 0; |
|
117 |
$ret['comments'] = Array(); |
|
118 |
if (!$q) |
|
119 |
$db->die_json(); |
|
120 |
if ( $row = $db->fetchrow() ) |
|
121 |
{ |
|
122 |
do { |
|
123 |
||
124 |
// Increment counters |
|
125 |
$count_total++; |
|
126 |
( $row['approved'] == 1 ) ? $count_appr++ : $count_unappr++; |
|
127 |
||
128 |
if ( !$this->perms->get_permissions('mod_comments') && $row['approved'] == 0 ) |
|
129 |
continue; |
|
130 |
||
131 |
// Send the source |
|
132 |
$row['comment_source'] = $row['comment_data']; |
|
133 |
||
134 |
// Format text |
|
135 |
$row['comment_data'] = RenderMan::render($row['comment_data']); |
|
136 |
||
137 |
// Format date |
|
138 |
$row['time'] = date('F d, Y h:i a', $row['time']); |
|
139 |
||
140 |
// Format signature |
|
141 |
$row['signature'] = ( !empty($row['signature']) ) ? RenderMan::render($row['signature']) : ''; |
|
142 |
||
143 |
// Add the comment to the list |
|
144 |
$ret['comments'][] = $row; |
|
145 |
||
146 |
} while ( $row = $db->fetchrow() ); |
|
147 |
} |
|
148 |
$db->free_result(); |
|
149 |
$ret['count_appr'] = $count_appr; |
|
150 |
$ret['count_total'] = $count_total; |
|
151 |
$ret['count_unappr'] = $count_unappr; |
|
152 |
$ret['auth_mod_comments'] = $this->perms->get_permissions('mod_comments'); |
|
153 |
$ret['auth_post_comments'] = $this->perms->get_permissions('post_comments'); |
|
154 |
$ret['auth_edit_comments'] = $this->perms->get_permissions('edit_comments'); |
|
155 |
$ret['user_id'] = $session->user_id; |
|
156 |
$ret['username'] = $session->username; |
|
157 |
$ret['logged_in'] = $session->user_logged_in; |
|
158 |
||
159 |
$ret['user_level'] = Array(); |
|
160 |
$ret['user_level']['guest'] = USER_LEVEL_GUEST; |
|
161 |
$ret['user_level']['member'] = USER_LEVEL_MEMBER; |
|
162 |
$ret['user_level']['mod'] = USER_LEVEL_MOD; |
|
163 |
$ret['user_level']['admin'] = USER_LEVEL_ADMIN; |
|
164 |
||
165 |
$ret['approval_needed'] = ( getConfig('approve_comments') == '1' ); |
|
166 |
$ret['guest_posting'] = getConfig('comments_need_login'); |
|
167 |
||
168 |
if ( $ret['guest_posting'] == '1' && !$session->user_logged_in ) |
|
169 |
{ |
|
170 |
$session->kill_captcha(); |
|
171 |
$ret['captcha'] = $session->make_captcha(); |
|
172 |
} |
|
173 |
break; |
|
174 |
case 'edit': |
|
175 |
$cid = (string)$data['id']; |
|
176 |
if ( !preg_match('#^([0-9]+)$#i', $cid) || intval($cid) < 1 ) |
|
177 |
{ |
|
178 |
echo '{"mode":"error","error":"HACKING ATTEMPT"}'; |
|
179 |
return false; |
|
180 |
} |
|
181 |
$cid = intval($cid); |
|
182 |
$q = $db->sql_query('SELECT c.user_id,c.approved FROM '.table_prefix.'comments c LEFT JOIN '.table_prefix.'users u ON (u.user_id=c.user_id) WHERE comment_id='.$cid.';'); |
|
183 |
if(!$q) |
|
184 |
$db->die_json(); |
|
185 |
$row = $db->fetchrow(); |
|
186 |
$uid = intval($row['user_id']); |
|
187 |
$can_edit = ( ( $uid == $session->user_id && $uid != 1 && $this->perms->get_permissions('edit_comments') ) || ( $this->perms->get_permissions('mod_comments') ) ); |
|
188 |
if(!$can_edit) |
|
189 |
{ |
|
190 |
echo '{"mode":"error","error":"HACKING ATTEMPT"}'; |
|
191 |
return false; |
|
192 |
} |
|
193 |
$data['data'] = str_replace("\r", '', $data['data']); // Windows compatibility |
|
194 |
$text = RenderMan::preprocess_text($data['data'], true, false); |
|
195 |
$text2 = $db->escape($text); |
|
196 |
$subj = $db->escape(htmlspecialchars($data['subj'])); |
|
197 |
$q = $db->sql_query('UPDATE '.table_prefix.'comments SET subject=\'' . $subj . '\',comment_data=\'' . $text2 . '\' WHERE comment_id=' . $cid . ';'); |
|
198 |
if(!$q) |
|
199 |
$db->die_json(); |
|
200 |
$ret = Array( |
|
201 |
'mode' => 'redraw', |
|
202 |
'id' => $data['local_id'], |
|
203 |
'subj' => htmlspecialchars($data['subj']), |
|
204 |
'text' => RenderMan::render($text), |
|
205 |
'src' => $text, |
|
206 |
'approved' => $row['approved'] |
|
207 |
); |
|
208 |
break; |
|
209 |
case 'delete': |
|
210 |
$cid = (string)$data['id']; |
|
211 |
if ( !preg_match('#^([0-9]+)$#i', $cid) || intval($cid) < 1 ) |
|
212 |
{ |
|
213 |
echo '{"mode":"error","error":"HACKING ATTEMPT"}'; |
|
214 |
return false; |
|
215 |
} |
|
216 |
$cid = intval($cid); |
|
217 |
$q = $db->sql_query('SELECT c.user_id FROM '.table_prefix.'comments c LEFT JOIN '.table_prefix.'users u ON (u.user_id=c.user_id) WHERE comment_id='.$cid.';'); |
|
218 |
if(!$q) |
|
219 |
$db->die_json(); |
|
220 |
$row = $db->fetchrow(); |
|
221 |
$uid = intval($row['user_id']); |
|
222 |
$can_edit = ( ( $uid == $session->user_id && $uid != 1 && $this->perms->get_permissions('edit_comments') ) || ( $this->perms->get_permissions('mod_comments') ) ); |
|
223 |
if(!$can_edit) |
|
224 |
{ |
|
225 |
echo '{"mode":"error","error":"HACKING ATTEMPT"}'; |
|
226 |
return false; |
|
227 |
} |
|
228 |
$q = $db->sql_query('DELETE FROM '.table_prefix.'comments WHERE comment_id='.$cid.';'); |
|
229 |
if(!$q) |
|
230 |
$db->die_json(); |
|
231 |
$ret = Array( |
|
232 |
'mode' => 'annihilate', |
|
233 |
'id' => $data['local_id'] |
|
234 |
); |
|
235 |
break; |
|
236 |
case 'submit': |
|
237 |
||
238 |
// Now for a huge round of security checks... |
|
239 |
||
240 |
$errors = Array(); |
|
241 |
||
242 |
// Authorization |
|
243 |
// Like the rest of the ACL system, this call is a one-stop check for ALL ACL entries. |
|
244 |
if ( !$this->perms->get_permissions('post_comments') ) |
|
74
68469a95658d
Various bugfixes and cleanups, too much to remember... see the diffs for what got changed :-)
Dan
parents:
73
diff
changeset
|
245 |
$errors[] = 'The site security policy prevents your user account from posting comments;'; |
1 | 246 |
|
247 |
// Guest authorization |
|
248 |
if ( getConfig('comments_need_login') == '2' && !$session->user_logged_in ) |
|
249 |
$errors[] = 'You need to log in before posting comments.'; |
|
250 |
||
251 |
// CAPTCHA code |
|
252 |
if ( getConfig('comments_need_login') == '1' && !$session->user_logged_in ) |
|
253 |
{ |
|
254 |
$real_code = $session->get_captcha($data['captcha_id']); |
|
255 |
if ( $real_code != $data['captcha_code'] ) |
|
256 |
$errors[] = 'The confirmation code you entered was incorrect.'; |
|
257 |
} |
|
258 |
||
259 |
if ( count($errors) > 0 ) |
|
260 |
{ |
|
261 |
$ret = Array( |
|
262 |
'mode' => 'error', |
|
263 |
'error' => implode("\n", $errors) |
|
264 |
); |
|
265 |
} |
|
266 |
else |
|
267 |
{ |
|
268 |
// We're authorized! |
|
269 |
||
270 |
// Preprocess |
|
271 |
$name = ( $session->user_logged_in ) ? htmlspecialchars($session->username) : htmlspecialchars($data['name']); |
|
272 |
$subj = htmlspecialchars($data['subj']); |
|
273 |
$text = RenderMan::preprocess_text($data['text'], true, false); |
|
274 |
$src = $text; |
|
275 |
$sql_text = $db->escape($text); |
|
276 |
$text = RenderMan::render($text); |
|
277 |
$appr = ( getConfig('approve_comments') == '1' ) ? '0' : '1'; |
|
278 |
$time = time(); |
|
279 |
$date = date('F d, Y h:i a', $time); |
|
280 |
||
281 |
// Send it to the database |
|
282 |
$q = $db->sql_query('INSERT INTO '.table_prefix.'comments(page_id,namespace,name,subject,comment_data,approved, time, user_id) VALUES' . |
|
283 |
"('$this->page_id', '$this->namespace', '$name', '$subj', '$sql_text', $appr, $time, $session->user_id);"); |
|
284 |
if(!$q) |
|
285 |
$db->die_json(); |
|
286 |
||
287 |
// Re-fetch |
|
288 |
$q = $db->sql_query('SELECT c.comment_id,c.name,c.subject,c.comment_data,c.time,c.approved,u.user_level,u.user_id,u.signature FROM '.table_prefix.'comments AS c |
|
289 |
LEFT JOIN '.table_prefix.'users AS u |
|
290 |
ON (u.user_id=c.user_id) |
|
291 |
WHERE page_id=\'' . $this->page_id . '\' |
|
292 |
AND namespace=\'' . $this->namespace . '\' |
|
293 |
AND time='.$time.' ORDER BY comment_id DESC LIMIT 1;'); |
|
294 |
if(!$q) |
|
295 |
$db->die_json(); |
|
296 |
||
297 |
$row = $db->fetchrow(); |
|
298 |
$db->free_result(); |
|
299 |
$row['time'] = $date; |
|
300 |
$row['comment_data'] = $text; |
|
301 |
$row['comment_source'] = $src; |
|
302 |
$ret = Array( |
|
303 |
'mode' => 'materialize' |
|
304 |
); |
|
305 |
$ret = enano_safe_array_merge($ret, $row); |
|
306 |
||
307 |
$ret['auth_mod_comments'] = $this->perms->get_permissions('mod_comments'); |
|
308 |
$ret['auth_post_comments'] = $this->perms->get_permissions('post_comments'); |
|
309 |
$ret['auth_edit_comments'] = $this->perms->get_permissions('edit_comments'); |
|
310 |
$ret['user_id'] = $session->user_id; |
|
311 |
$ret['username'] = $session->username; |
|
312 |
$ret['logged_in'] = $session->user_logged_in; |
|
313 |
$ret['signature'] = RenderMan::render($row['signature']); |
|
314 |
||
315 |
$ret['user_level_list'] = Array(); |
|
316 |
$ret['user_level_list']['guest'] = USER_LEVEL_GUEST; |
|
317 |
$ret['user_level_list']['member'] = USER_LEVEL_MEMBER; |
|
318 |
$ret['user_level_list']['mod'] = USER_LEVEL_MOD; |
|
319 |
$ret['user_level_list']['admin'] = USER_LEVEL_ADMIN; |
|
320 |
||
321 |
} |
|
322 |
||
323 |
break; |
|
324 |
case 'approve': |
|
325 |
if ( !$this->perms->get_permissions('mod_comments') ) |
|
326 |
{ |
|
327 |
$ret = Array( |
|
328 |
'mode' => 'error', |
|
329 |
'error' => 'You are not authorized to moderate comments.' |
|
330 |
); |
|
331 |
echo $parser->encode($ret); |
|
332 |
return $ret; |
|
333 |
} |
|
334 |
||
335 |
$cid = (string)$data['id']; |
|
336 |
if ( !preg_match('#^([0-9]+)$#i', $cid) || intval($cid) < 1 ) |
|
337 |
{ |
|
338 |
echo '{"mode":"error","error":"HACKING ATTEMPT"}'; |
|
339 |
return false; |
|
340 |
} |
|
341 |
$cid = intval($cid); |
|
342 |
$q = $db->sql_query('SELECT subject,approved FROM '.table_prefix.'comments WHERE comment_id='.$cid.';'); |
|
343 |
if(!$q || $db->numrows() < 1) |
|
344 |
$db->die_json(); |
|
345 |
$row = $db->fetchrow(); |
|
346 |
$db->free_result(); |
|
347 |
$appr = ( $row['approved'] == '1' ) ? '0' : '1'; |
|
348 |
$q = $db->sql_query('UPDATE '.table_prefix."comments SET approved=$appr WHERE comment_id=$cid;"); |
|
349 |
if (!$q) |
|
350 |
$db->die_json(); |
|
351 |
||
352 |
$ret = Array( |
|
353 |
'mode' => 'redraw', |
|
354 |
'approved' => $appr, |
|
355 |
'subj' => $row['subject'], |
|
29
e5484a9e0818
Rewrote change theme dialog; a few minor stability fixes here and there; fixed IE + St Patty background image
Dan
parents:
21
diff
changeset
|
356 |
'id' => $data['local_id'], |
e5484a9e0818
Rewrote change theme dialog; a few minor stability fixes here and there; fixed IE + St Patty background image
Dan
parents:
21
diff
changeset
|
357 |
'approve_updated' => 'yes' |
1 | 358 |
); |
359 |
||
360 |
break; |
|
361 |
default: |
|
362 |
$ret = Array( |
|
363 |
'mode' => 'error', |
|
364 |
'error' => $data['mode'] . ' is not a valid request mode' |
|
365 |
); |
|
366 |
break; |
|
367 |
} |
|
368 |
echo $parser->encode($ret); |
|
369 |
return $ret; |
|
370 |
} |
|
371 |
||
372 |
} // class Comments |
|
373 |