1
+ − 1
<?php
+ − 2
+ − 3
/*
+ − 4
* Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
261
+ − 5
* Version 1.0.3 (Dyrad)
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
261
+ − 57
if ( $page_id == $paths->page_id && $namespace == $paths->namespace )
1
+ − 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
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
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
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
diff
changeset
+ − 90
return $ret;
c162ca39db8f
Finished pagination code (was incomplete in previous revision) and added a few hacks for an upcoming theme
Dan
diff
changeset
+ − 91
}
c162ca39db8f
Finished pagination code (was incomplete in previous revision) and added a few hacks for an upcoming theme
Dan
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
diff
changeset
+ − 93
{
c162ca39db8f
Finished pagination code (was incomplete in previous revision) and added a few hacks for an upcoming theme
Dan
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
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
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
}
108
1c7f59df9474
Implemented some extra functionality for friends/foes in comments; fixed lack of table_prefix in stats.php line 63
Dan
diff
changeset
+ − 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, b.buddy_id IS NOT NULL AS is_buddy, ( b.is_friend IS NOT NULL AND b.is_friend=1 ) AS is_friend FROM '.table_prefix.'comments AS c
1
+ − 110
LEFT JOIN '.table_prefix.'users AS u
+ − 111
ON (u.user_id=c.user_id)
108
1c7f59df9474
Implemented some extra functionality for friends/foes in comments; fixed lack of table_prefix in stats.php line 63
Dan
diff
changeset
+ − 112
LEFT JOIN '.table_prefix.'buddies AS b
1c7f59df9474
Implemented some extra functionality for friends/foes in comments; fixed lack of table_prefix in stats.php line 63
Dan
diff
changeset
+ − 113
ON ( ( b.user_id=' . $session->user_id.' AND b.buddy_user_id=c.user_id ) OR b.user_id IS NULL)
1
+ − 114
WHERE page_id=\'' . $this->page_id . '\'
108
1c7f59df9474
Implemented some extra functionality for friends/foes in comments; fixed lack of table_prefix in stats.php line 63
Dan
diff
changeset
+ − 115
AND namespace=\'' . $this->namespace . '\'
259
112debff64bd
SURPRISE! Preliminary PostgreSQL support added. The required schema file is not present in this commit and will be included at a later date. No installer support is implemented. Also in this commit: several fixes including <!-- SYSMSG ... --> was broken in template compiler; set fixed width on included images to prevent the thumbnail box from getting huge; added a much more friendly interface to AJAX responses that are invalid JSON
Dan
diff
changeset
+ − 116
GROUP BY c.comment_id,c.name,c.subject,c.comment_data,c.time,c.approved,u.user_level,u.user_id,u.signature,b.buddy_id,b.is_friend
108
1c7f59df9474
Implemented some extra functionality for friends/foes in comments; fixed lack of table_prefix in stats.php line 63
Dan
diff
changeset
+ − 117
ORDER BY c.time ASC;');
1
+ − 118
$count_appr = 0;
+ − 119
$count_total = 0;
+ − 120
$count_unappr = 0;
+ − 121
$ret['comments'] = Array();
+ − 122
if (!$q)
+ − 123
$db->die_json();
+ − 124
if ( $row = $db->fetchrow() )
+ − 125
{
+ − 126
do {
+ − 127
+ − 128
// Increment counters
+ − 129
$count_total++;
+ − 130
( $row['approved'] == 1 ) ? $count_appr++ : $count_unappr++;
+ − 131
+ − 132
if ( !$this->perms->get_permissions('mod_comments') && $row['approved'] == 0 )
+ − 133
continue;
+ − 134
+ − 135
// Send the source
+ − 136
$row['comment_source'] = $row['comment_data'];
+ − 137
+ − 138
// Format text
+ − 139
$row['comment_data'] = RenderMan::render($row['comment_data']);
+ − 140
108
1c7f59df9474
Implemented some extra functionality for friends/foes in comments; fixed lack of table_prefix in stats.php line 63
Dan
diff
changeset
+ − 141
if ( $row['is_buddy'] == 1 && $row['is_friend'] == 0 )
1c7f59df9474
Implemented some extra functionality for friends/foes in comments; fixed lack of table_prefix in stats.php line 63
Dan
diff
changeset
+ − 142
{
1c7f59df9474
Implemented some extra functionality for friends/foes in comments; fixed lack of table_prefix in stats.php line 63
Dan
diff
changeset
+ − 143
$seed = md5(sha1(mt_rand() . microtime()));
1c7f59df9474
Implemented some extra functionality for friends/foes in comments; fixed lack of table_prefix in stats.php line 63
Dan
diff
changeset
+ − 144
$wrapper = '
1c7f59df9474
Implemented some extra functionality for friends/foes in comments; fixed lack of table_prefix in stats.php line 63
Dan
diff
changeset
+ − 145
<div id="posthide_'.$seed.'" style="display: none;">
1c7f59df9474
Implemented some extra functionality for friends/foes in comments; fixed lack of table_prefix in stats.php line 63
Dan
diff
changeset
+ − 146
' . $row['comment_data'] . '
1c7f59df9474
Implemented some extra functionality for friends/foes in comments; fixed lack of table_prefix in stats.php line 63
Dan
diff
changeset
+ − 147
</div>
1c7f59df9474
Implemented some extra functionality for friends/foes in comments; fixed lack of table_prefix in stats.php line 63
Dan
diff
changeset
+ − 148
<p><span style="opacity: 0.4; filter: alpha(opacity=40);">Post from foe hidden.</span> <span style="text-align: right;"><a href="#showpost" onclick="document.getElementById(\'posthide_'.$seed.'\').style.display=\'block\'; this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); return false;">Display post</a></span></p>
1c7f59df9474
Implemented some extra functionality for friends/foes in comments; fixed lack of table_prefix in stats.php line 63
Dan
diff
changeset
+ − 149
';
1c7f59df9474
Implemented some extra functionality for friends/foes in comments; fixed lack of table_prefix in stats.php line 63
Dan
diff
changeset
+ − 150
$row['comment_data'] = $wrapper;
1c7f59df9474
Implemented some extra functionality for friends/foes in comments; fixed lack of table_prefix in stats.php line 63
Dan
diff
changeset
+ − 151
}
1c7f59df9474
Implemented some extra functionality for friends/foes in comments; fixed lack of table_prefix in stats.php line 63
Dan
diff
changeset
+ − 152
1
+ − 153
// Format date
+ − 154
$row['time'] = date('F d, Y h:i a', $row['time']);
+ − 155
+ − 156
// Format signature
+ − 157
$row['signature'] = ( !empty($row['signature']) ) ? RenderMan::render($row['signature']) : '';
+ − 158
+ − 159
// Add the comment to the list
+ − 160
$ret['comments'][] = $row;
+ − 161
+ − 162
} while ( $row = $db->fetchrow() );
+ − 163
}
+ − 164
$db->free_result();
+ − 165
$ret['count_appr'] = $count_appr;
+ − 166
$ret['count_total'] = $count_total;
+ − 167
$ret['count_unappr'] = $count_unappr;
+ − 168
$ret['auth_mod_comments'] = $this->perms->get_permissions('mod_comments');
+ − 169
$ret['auth_post_comments'] = $this->perms->get_permissions('post_comments');
+ − 170
$ret['auth_edit_comments'] = $this->perms->get_permissions('edit_comments');
+ − 171
$ret['user_id'] = $session->user_id;
+ − 172
$ret['username'] = $session->username;
+ − 173
$ret['logged_in'] = $session->user_logged_in;
+ − 174
+ − 175
$ret['user_level'] = Array();
+ − 176
$ret['user_level']['guest'] = USER_LEVEL_GUEST;
+ − 177
$ret['user_level']['member'] = USER_LEVEL_MEMBER;
+ − 178
$ret['user_level']['mod'] = USER_LEVEL_MOD;
+ − 179
$ret['user_level']['admin'] = USER_LEVEL_ADMIN;
+ − 180
+ − 181
$ret['approval_needed'] = ( getConfig('approve_comments') == '1' );
+ − 182
$ret['guest_posting'] = getConfig('comments_need_login');
+ − 183
+ − 184
if ( $ret['guest_posting'] == '1' && !$session->user_logged_in )
+ − 185
{
+ − 186
$session->kill_captcha();
+ − 187
$ret['captcha'] = $session->make_captcha();
+ − 188
}
+ − 189
break;
+ − 190
case 'edit':
+ − 191
$cid = (string)$data['id'];
+ − 192
if ( !preg_match('#^([0-9]+)$#i', $cid) || intval($cid) < 1 )
+ − 193
{
+ − 194
echo '{"mode":"error","error":"HACKING ATTEMPT"}';
+ − 195
return false;
+ − 196
}
+ − 197
$cid = intval($cid);
+ − 198
$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.';');
+ − 199
if(!$q)
+ − 200
$db->die_json();
+ − 201
$row = $db->fetchrow();
+ − 202
$uid = intval($row['user_id']);
+ − 203
$can_edit = ( ( $uid == $session->user_id && $uid != 1 && $this->perms->get_permissions('edit_comments') ) || ( $this->perms->get_permissions('mod_comments') ) );
+ − 204
if(!$can_edit)
+ − 205
{
+ − 206
echo '{"mode":"error","error":"HACKING ATTEMPT"}';
+ − 207
return false;
+ − 208
}
+ − 209
$data['data'] = str_replace("\r", '', $data['data']); // Windows compatibility
+ − 210
$text = RenderMan::preprocess_text($data['data'], true, false);
+ − 211
$text2 = $db->escape($text);
+ − 212
$subj = $db->escape(htmlspecialchars($data['subj']));
+ − 213
$q = $db->sql_query('UPDATE '.table_prefix.'comments SET subject=\'' . $subj . '\',comment_data=\'' . $text2 . '\' WHERE comment_id=' . $cid . ';');
+ − 214
if(!$q)
+ − 215
$db->die_json();
+ − 216
$ret = Array(
+ − 217
'mode' => 'redraw',
+ − 218
'id' => $data['local_id'],
+ − 219
'subj' => htmlspecialchars($data['subj']),
+ − 220
'text' => RenderMan::render($text),
+ − 221
'src' => $text,
+ − 222
'approved' => $row['approved']
+ − 223
);
+ − 224
break;
+ − 225
case 'delete':
+ − 226
$cid = (string)$data['id'];
+ − 227
if ( !preg_match('#^([0-9]+)$#i', $cid) || intval($cid) < 1 )
+ − 228
{
+ − 229
echo '{"mode":"error","error":"HACKING ATTEMPT"}';
+ − 230
return false;
+ − 231
}
+ − 232
$cid = intval($cid);
+ − 233
$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.';');
+ − 234
if(!$q)
+ − 235
$db->die_json();
+ − 236
$row = $db->fetchrow();
+ − 237
$uid = intval($row['user_id']);
+ − 238
$can_edit = ( ( $uid == $session->user_id && $uid != 1 && $this->perms->get_permissions('edit_comments') ) || ( $this->perms->get_permissions('mod_comments') ) );
+ − 239
if(!$can_edit)
+ − 240
{
+ − 241
echo '{"mode":"error","error":"HACKING ATTEMPT"}';
+ − 242
return false;
+ − 243
}
+ − 244
$q = $db->sql_query('DELETE FROM '.table_prefix.'comments WHERE comment_id='.$cid.';');
+ − 245
if(!$q)
+ − 246
$db->die_json();
+ − 247
$ret = Array(
+ − 248
'mode' => 'annihilate',
+ − 249
'id' => $data['local_id']
+ − 250
);
+ − 251
break;
+ − 252
case 'submit':
+ − 253
+ − 254
// Now for a huge round of security checks...
+ − 255
+ − 256
$errors = Array();
+ − 257
+ − 258
// Authorization
+ − 259
// Like the rest of the ACL system, this call is a one-stop check for ALL ACL entries.
+ − 260
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
diff
changeset
+ − 261
$errors[] = 'The site security policy prevents your user account from posting comments;';
1
+ − 262
+ − 263
// Guest authorization
+ − 264
if ( getConfig('comments_need_login') == '2' && !$session->user_logged_in )
+ − 265
$errors[] = 'You need to log in before posting comments.';
+ − 266
+ − 267
// CAPTCHA code
+ − 268
if ( getConfig('comments_need_login') == '1' && !$session->user_logged_in )
+ − 269
{
+ − 270
$real_code = $session->get_captcha($data['captcha_id']);
+ − 271
if ( $real_code != $data['captcha_code'] )
+ − 272
$errors[] = 'The confirmation code you entered was incorrect.';
212
d57af0b0302e
Major improvements in the security of the CAPTCHA system (no SQL injection or anything like that); fixed denied form submission due to _af_acting on form object wrongly switched to true
Dan
diff
changeset
+ − 273
$session->kill_captcha();
1
+ − 274
}
+ − 275
+ − 276
if ( count($errors) > 0 )
+ − 277
{
+ − 278
$ret = Array(
+ − 279
'mode' => 'error',
+ − 280
'error' => implode("\n", $errors)
+ − 281
);
+ − 282
}
+ − 283
else
+ − 284
{
+ − 285
// We're authorized!
+ − 286
+ − 287
// Preprocess
+ − 288
$name = ( $session->user_logged_in ) ? htmlspecialchars($session->username) : htmlspecialchars($data['name']);
+ − 289
$subj = htmlspecialchars($data['subj']);
+ − 290
$text = RenderMan::preprocess_text($data['text'], true, false);
+ − 291
$src = $text;
+ − 292
$sql_text = $db->escape($text);
+ − 293
$text = RenderMan::render($text);
+ − 294
$appr = ( getConfig('approve_comments') == '1' ) ? '0' : '1';
+ − 295
$time = time();
+ − 296
$date = date('F d, Y h:i a', $time);
+ − 297
+ − 298
// Send it to the database
+ − 299
$q = $db->sql_query('INSERT INTO '.table_prefix.'comments(page_id,namespace,name,subject,comment_data,approved, time, user_id) VALUES' .
+ − 300
"('$this->page_id', '$this->namespace', '$name', '$subj', '$sql_text', $appr, $time, $session->user_id);");
+ − 301
if(!$q)
+ − 302
$db->die_json();
+ − 303
+ − 304
// Re-fetch
+ − 305
$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
+ − 306
LEFT JOIN '.table_prefix.'users AS u
+ − 307
ON (u.user_id=c.user_id)
+ − 308
WHERE page_id=\'' . $this->page_id . '\'
+ − 309
AND namespace=\'' . $this->namespace . '\'
+ − 310
AND time='.$time.' ORDER BY comment_id DESC LIMIT 1;');
+ − 311
if(!$q)
+ − 312
$db->die_json();
+ − 313
+ − 314
$row = $db->fetchrow();
+ − 315
$db->free_result();
+ − 316
$row['time'] = $date;
+ − 317
$row['comment_data'] = $text;
+ − 318
$row['comment_source'] = $src;
+ − 319
$ret = Array(
+ − 320
'mode' => 'materialize'
+ − 321
);
+ − 322
$ret = enano_safe_array_merge($ret, $row);
+ − 323
+ − 324
$ret['auth_mod_comments'] = $this->perms->get_permissions('mod_comments');
+ − 325
$ret['auth_post_comments'] = $this->perms->get_permissions('post_comments');
+ − 326
$ret['auth_edit_comments'] = $this->perms->get_permissions('edit_comments');
+ − 327
$ret['user_id'] = $session->user_id;
+ − 328
$ret['username'] = $session->username;
+ − 329
$ret['logged_in'] = $session->user_logged_in;
+ − 330
$ret['signature'] = RenderMan::render($row['signature']);
+ − 331
+ − 332
$ret['user_level_list'] = Array();
+ − 333
$ret['user_level_list']['guest'] = USER_LEVEL_GUEST;
+ − 334
$ret['user_level_list']['member'] = USER_LEVEL_MEMBER;
+ − 335
$ret['user_level_list']['mod'] = USER_LEVEL_MOD;
+ − 336
$ret['user_level_list']['admin'] = USER_LEVEL_ADMIN;
+ − 337
+ − 338
}
+ − 339
+ − 340
break;
+ − 341
case 'approve':
+ − 342
if ( !$this->perms->get_permissions('mod_comments') )
+ − 343
{
+ − 344
$ret = Array(
+ − 345
'mode' => 'error',
+ − 346
'error' => 'You are not authorized to moderate comments.'
+ − 347
);
+ − 348
echo $parser->encode($ret);
+ − 349
return $ret;
+ − 350
}
+ − 351
+ − 352
$cid = (string)$data['id'];
+ − 353
if ( !preg_match('#^([0-9]+)$#i', $cid) || intval($cid) < 1 )
+ − 354
{
+ − 355
echo '{"mode":"error","error":"HACKING ATTEMPT"}';
+ − 356
return false;
+ − 357
}
+ − 358
$cid = intval($cid);
+ − 359
$q = $db->sql_query('SELECT subject,approved FROM '.table_prefix.'comments WHERE comment_id='.$cid.';');
+ − 360
if(!$q || $db->numrows() < 1)
+ − 361
$db->die_json();
+ − 362
$row = $db->fetchrow();
+ − 363
$db->free_result();
+ − 364
$appr = ( $row['approved'] == '1' ) ? '0' : '1';
+ − 365
$q = $db->sql_query('UPDATE '.table_prefix."comments SET approved=$appr WHERE comment_id=$cid;");
+ − 366
if (!$q)
+ − 367
$db->die_json();
+ − 368
+ − 369
$ret = Array(
+ − 370
'mode' => 'redraw',
+ − 371
'approved' => $appr,
+ − 372
'subj' => $row['subject'],
29
e5484a9e0818
Rewrote change theme dialog; a few minor stability fixes here and there; fixed IE + St Patty background image
Dan
diff
changeset
+ − 373
'id' => $data['local_id'],
e5484a9e0818
Rewrote change theme dialog; a few minor stability fixes here and there; fixed IE + St Patty background image
Dan
diff
changeset
+ − 374
'approve_updated' => 'yes'
1
+ − 375
);
+ − 376
+ − 377
break;
+ − 378
default:
+ − 379
$ret = Array(
+ − 380
'mode' => 'error',
+ − 381
'error' => $data['mode'] . ' is not a valid request mode'
+ − 382
);
+ − 383
break;
+ − 384
}
+ − 385
echo $parser->encode($ret);
+ − 386
return $ret;
+ − 387
}
+ − 388
+ − 389
} // class Comments
+ − 390