4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2012 The Nucleus Group
\r
7 * This program is free software; you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation; either version 2
\r
10 * of the License, or (at your option) any later version.
\r
11 * (see nucleus/documentation/index.html#license for more info)
\r
14 * A class representing the comments (all of them) for a certain post on a ceratin blog
\r
16 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
17 * @copyright Copyright (C) 2002-2012 The Nucleus Group
\r
18 * @version $Id: COMMENTS.php 1527 2011-06-21 10:43:44Z sakamocchi $
\r
21 if ( !function_exists('requestVar') ) exit;
\r
22 require_once dirname(__FILE__) . '/COMMENTACTIONS.php';
\r
27 // item for which comment are being displayed
\r
30 // reference to the itemActions object that is calling the showComments function
\r
33 // total amount of comments displayed
\r
37 * Creates a new Comments object for the given blog and item
\r
42 function COMMENTS($itemid) {
\r
43 $this->itemid = intval($itemid);
\r
47 * Used when parsing comments
\r
49 * @param $itemActions
\r
50 * itemActions object, that will take care of the parsing
\r
52 function setItemActions(&$itemActions) {
\r
53 $this->itemActions =& $itemActions;
\r
57 * Shows maximum $max comments to the given item using the given template
\r
58 * returns the amount of shown comments (if maxToShow = -1, then there is no limit)
\r
63 * max. comments to show
\r
65 * indicates if the 'no comments' thingie should be outputted when there are no comments
\r
66 * (useful for closed items)
\r
68 * Highlight to use (if any)
\r
70 function showComments($template, $maxToShow = -1, $showNone = 1, $highlight = '') {
\r
71 global $CONF, $manager;
\r
73 // create parser object & action handler
\r
74 $handler = new CommentActions($this);
\r
75 $handler->setTemplate($template);
\r
77 $parser = new Parser($handler);
\r
79 if ($maxToShow == 0) {
\r
80 $this->commentcount = $this->amountComments();
\r
82 $query = 'SELECT c.citem as itemid, c.cnumber as commentid, c.cbody as body, c.cuser as user, c.cmail as userid, c.cemail as email, c.cmember as memberid, c.ctime, c.chost as host, c.cip as ip, c.cblog as blogid'
\r
83 . ' FROM '.sql_table('comment').' as c'
\r
84 . ' WHERE c.citem=' . $this->itemid
\r
85 . ' ORDER BY c.ctime';
\r
87 $comments = DB::getResult($query);
\r
88 $this->commentcount = $comments->rowCount();
\r
91 // if no result was found
\r
92 if ($this->commentcount == 0) {
\r
93 // note: when no reactions, COMMENTS_HEADER and COMMENTS_FOOTER are _NOT_ used
\r
94 if ($showNone) $parser->parse($template['COMMENTS_NONE']);
\r
98 // if too many comments to show
\r
99 if (($maxToShow != -1) && ($this->commentcount > $maxToShow)) {
\r
100 $parser->parse($template['COMMENTS_TOOMUCH']);
\r
104 $parser->parse($template['COMMENTS_HEADER']);
\r
106 foreach ( $comments as $comment ) {
\r
107 $comment['timestamp'] = strtotime($comment['ctime']);
\r
108 $handler->setCurrentComment($comment);
\r
109 $handler->setHighlight($highlight);
\r
110 $manager->notify('PreComment', array('comment' => &$comment));
\r
111 $parser->parse($template['COMMENTS_BODY']);
\r
112 $manager->notify('PostComment', array('comment' => &$comment));
\r
115 $parser->parse($template['COMMENTS_FOOTER']);
\r
117 $comments->closeCursor();
\r
119 return $this->commentcount;
\r
123 * Returns the amount of comments for this itemid
\r
125 function amountComments() {
\r
126 $query = 'SELECT COUNT(*)'
\r
127 . ' FROM '.sql_table('comment').' as c'
\r
128 . ' WHERE c.citem='. $this->itemid;
\r
129 $res = DB::getValue($query);
\r
135 * Comments::addComment()
\r
136 * Adds a new comment to the database
\r
138 * @param string $timestamp
\r
139 * @param array $comment
\r
142 function addComment($timestamp, $comment)
\r
144 global $CONF, $member, $manager;
\r
146 $blogid = getBlogIDFromItemID($this->itemid);
\r
148 $settings =& $manager->getBlog($blogid);
\r
149 $settings->readSettings();
\r
151 // begin if: comments disabled
\r
152 if ( !$settings->commentsEnabled() )
\r
154 return _ERROR_COMMENTS_DISABLED;
\r
157 // begin if: public cannot comment
\r
158 if ( !$settings->isPublic() && !$member->isLoggedIn() )
\r
160 return _ERROR_COMMENTS_NONPUBLIC;
\r
163 // begin if: comment uses a protected member name
\r
164 if ( $CONF['ProtectMemNames'] && !$member->isLoggedIn() && Member::isNameProtected($comment['user']) )
\r
166 return _ERROR_COMMENTS_MEMBERNICK;
\r
169 // begin if: email required, but missing (doesn't apply to members)
\r
170 if ( $settings->emailRequired() && i18n::strlen($comment['email']) == 0 && !$member->isLoggedIn() )
\r
172 return _ERROR_EMAIL_REQUIRED;
\r
175 // begin if: commenter's name is too long
\r
176 if ( i18n::strlen($comment['user']) > 40 )
\r
178 return _ERROR_USER_TOO_LONG;
\r
181 // begin if: commenter's email is too long
\r
182 if ( i18n::strlen($comment['email']) > 100 )
\r
184 return _ERROR_EMAIL_TOO_LONG;
\r
187 // begin if: commenter's url is too long
\r
188 if ( i18n::strlen($comment['userid']) > 100 )
\r
190 return _ERROR_URL_TOO_LONG;
\r
193 $comment['timestamp'] = $timestamp;
\r
194 $comment['host'] = gethostbyaddr(serverVar('REMOTE_ADDR') );
\r
195 $comment['ip'] = serverVar('REMOTE_ADDR');
\r
197 // begin if: member is logged in, use that data
\r
198 if ( $member->isLoggedIn() )
\r
200 $comment['memberid'] = $member->getID();
\r
201 $comment['user'] = '';
\r
202 $comment['userid'] = '';
\r
203 $comment['email'] = '';
\r
207 $comment['memberid'] = 0;
\r
212 $plugins = array();
\r
214 if ( isset($manager->subscriptions['ValidateForm']) )
\r
216 $plugins = array_merge($plugins, $manager->subscriptions['ValidateForm']);
\r
219 if ( isset($manager->subscriptions['PreAddComment']) )
\r
221 $plugins = array_merge($plugins, $manager->subscriptions['PreAddComment']);
\r
224 if ( isset($manager->subscriptions['PostAddComment']) )
\r
226 $plugins = array_merge($plugins, $manager->subscriptions['PostAddComment']);
\r
229 $plugins = array_unique($plugins);
\r
231 while ( list(, $plugin) = each($plugins) )
\r
233 $p = $manager->getPlugin($plugin);
\r
234 $continue = $continue || $p->supportsFeature('handleSpam');
\r
237 $spamcheck = array(
\r
238 'type' => 'comment',
\r
239 'body' => $comment['body'],
\r
240 'id' => $comment['itemid'],
\r
242 'return' => $continue
\r
245 // begin if: member logged in
\r
246 if ( $member->isLoggedIn() )
\r
248 $spamcheck['author'] = $member->displayname;
\r
249 $spamcheck['email'] = $member->email;
\r
254 $spamcheck['author'] = $comment['user'];
\r
255 $spamcheck['email'] = $comment['email'];
\r
256 $spamcheck['url'] = $comment['userid'];
\r
259 $manager->notify('SpamCheck', array('spamcheck' => &$spamcheck) );
\r
261 if ( !$continue && isset($spamcheck['result']) && $spamcheck['result'] == TRUE )
\r
263 return _ERROR_COMMENTS_SPAM;
\r
266 // isValidComment returns either "1" or an error message
\r
267 $isvalid = $this->isValidComment($comment, $spamcheck);
\r
269 if ( $isvalid != 1 )
\r
274 // begin if: send email to notification address
\r
275 if ( $settings->getNotifyAddress() && $settings->notifyOnComment() )
\r
278 $message = _NOTIFY_NC_MSG . ' ' . $this->itemid . "\n";
\r
279 $temp = parse_url($CONF['Self']);
\r
281 if ( $temp['scheme'] )
\r
283 $message .= Link::create_item_link($this->itemid) . "\n\n";
\r
287 $tempurl = $settings->getURL();
\r
289 if ( i18n::substr($tempurl, -1) == '/' || i18n::substr($tempurl, -4) == '.php' )
\r
291 $message .= $tempurl . '?itemid=' . $this->itemid . "\n\n";
\r
295 $message .= $tempurl . '/?itemid=' . $this->itemid . "\n\n";
\r
299 if ( $comment['memberid'] == 0 )
\r
301 $message .= _NOTIFY_USER . ' ' . $comment['user'] . "\n";
\r
302 $message .= _NOTIFY_USERID . ' ' . $comment['userid'] . "\n";
\r
306 $message .= _NOTIFY_MEMBER .' ' . $member->getDisplayName() . ' (ID=' . $member->getID() . ")\n";
\r
309 $message .= _NOTIFY_HOST . ' ' . $comment['host'] . "\n";
\r
310 $message .= _NOTIFY_COMMENT . "\n " . $comment['body'] . "\n";
\r
311 $message .= NOTIFICATION::get_mail_footer();
\r
313 $item =& $manager->getItem($this->itemid, 0, 0);
\r
314 $subject = _NOTIFY_NC_TITLE . ' ' . strip_tags($item['title']) . ' (' . $this->itemid . ')';
\r
316 $from = $member->getNotifyFromMailAddress($comment['email']);
\r
318 NOTIFICATION::mail($settings->getNotifyAddress(), $subject, $message, $from, i18n::get_current_charset());
\r
321 $comment = Comment::prepare($comment);
\r
323 $manager->notify('PreAddComment', array('comment' => &$comment, 'spamcheck' => &$spamcheck) );
\r
325 $name = DB::quoteValue($comment['user']);
\r
326 $url = DB::quoteValue($comment['userid']);
\r
327 $email = DB::quoteValue($comment['email']);
\r
328 $body = DB::quoteValue($comment['body']);
\r
329 $host = DB::quoteValue($comment['host']);
\r
330 $ip = DB::quoteValue($comment['ip']);
\r
331 $memberid = intval($comment['memberid']);
\r
332 $timestamp = DB::formatDateTime($comment['timestamp']);
\r
333 $itemid = $this->itemid;
\r
335 $qSql = 'SELECT COUNT(*) AS result '
\r
336 . 'FROM ' . sql_table('comment')
\r
338 . 'cmail = ' . $url
\r
339 . ' AND cmember = ' . $memberid
\r
340 . ' AND cbody = ' . $body
\r
341 . ' AND citem = ' . $itemid
\r
342 . ' AND cblog = ' . $blogid;
\r
343 $result = (integer) DB::getValue($qSql);
\r
347 return _ERROR_BADACTION;
\r
350 $query = sprintf('INSERT INTO %s (cuser, cmail, cemail, cmember, cbody, citem, ctime, chost, cip, cblog) '
\r
351 . 'VALUES (%s, %s, %s, %d, %s, %d, %s, %s, %s, %d)'
\r
352 , sql_table('comment'), $name, $url, $email, $memberid, $body, $itemid, $timestamp, $host, $ip, $blogid);
\r
354 DB::execute($query);
\r
356 // post add comment
\r
357 $commentid = DB::getInsertId();
\r
358 $manager->notify('PostAddComment', array('comment' => &$comment, 'commentid' => &$commentid, 'spamcheck' => &$spamcheck) );
\r
366 * Comments::isValidComment()
\r
367 * Checks if a comment is valid and call plugins
\r
368 * that can check if the comment is a spam comment
\r
370 * @param Array $comment array with comment elements
\r
371 * @param Array $spamcheck array with spamcheck elements
\r
373 function isValidComment(&$comment, &$spamcheck)
\r
375 global $member, $manager;
\r
377 // check if there exists a item for this date
\r
378 $item =& $manager->getItem($this->itemid, 0, 0);
\r
382 return _ERROR_NOSUCHITEM;
\r
385 if ( $item['closed'] )
\r
387 return _ERROR_ITEMCLOSED;
\r
390 // don't allow words that are too long
\r
391 if ( preg_match('/[a-zA-Z0-9|\.,;:!\?=\/\\\\]{90,90}/', $comment['body']) != 0 )
\r
393 return _ERROR_COMMENT_LONGWORD;
\r
396 // check lengths of comment
\r
397 if ( i18n::strlen($comment['body']) < 3 )
\r
399 return _ERROR_COMMENT_NOCOMMENT;
\r
402 if ( i18n::strlen($comment['body']) > 5000 )
\r
404 return _ERROR_COMMENT_TOOLONG;
\r
407 // only check username if no member logged in
\r
408 if ( !$member->isLoggedIn() && (i18n::strlen($comment['user']) < 2) )
\r
410 return _ERROR_COMMENT_NOUSERNAME;
\r
413 if ( (i18n::strlen($comment['email']) != 0) && !NOTIFICATION::address_validation(trim($comment['email'])) )
\r
415 return _ERROR_BADMAILADDRESS;
\r
418 // let plugins do verification (any plugin which thinks the comment is invalid
\r
419 // can change 'error' to something other than '1')
\r
421 $manager->notify('ValidateForm', array('type' => 'comment', 'comment' => &$comment, 'error' => &$result, 'spamcheck' => &$spamcheck) );
\r