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 $item =& $manager->getItem($this->itemid, 0, 0);
\r
147 $settings =& $manager->getBlog($item['blogid']);
\r
148 $settings->readSettings();
\r
150 // begin if: comments disabled
\r
151 if ( !$settings->commentsEnabled() )
\r
153 return _ERROR_COMMENTS_DISABLED;
\r
156 // begin if: public cannot comment
\r
157 if ( !$settings->isPublic() && !$member->isLoggedIn() )
\r
159 return _ERROR_COMMENTS_NONPUBLIC;
\r
162 // begin if: comment uses a protected member name
\r
163 if ( $CONF['ProtectMemNames'] && !$member->isLoggedIn() && Member::isNameProtected($comment['user']) )
\r
165 return _ERROR_COMMENTS_MEMBERNICK;
\r
168 // begin if: email required, but missing (doesn't apply to members)
\r
169 if ( $settings->emailRequired() && i18n::strlen($comment['email']) == 0 && !$member->isLoggedIn() )
\r
171 return _ERROR_EMAIL_REQUIRED;
\r
174 // begin if: commenter's name is too long
\r
175 if ( i18n::strlen($comment['user']) > 40 )
\r
177 return _ERROR_USER_TOO_LONG;
\r
180 // begin if: commenter's email is too long
\r
181 if ( i18n::strlen($comment['email']) > 100 )
\r
183 return _ERROR_EMAIL_TOO_LONG;
\r
186 // begin if: commenter's url is too long
\r
187 if ( i18n::strlen($comment['userid']) > 100 )
\r
189 return _ERROR_URL_TOO_LONG;
\r
192 $comment['timestamp'] = $timestamp;
\r
193 $comment['host'] = gethostbyaddr(serverVar('REMOTE_ADDR') );
\r
194 $comment['ip'] = serverVar('REMOTE_ADDR');
\r
196 // begin if: member is logged in, use that data
\r
197 if ( $member->isLoggedIn() )
\r
199 $comment['memberid'] = $member->getID();
\r
200 $comment['user'] = '';
\r
201 $comment['userid'] = '';
\r
202 $comment['email'] = '';
\r
206 $comment['memberid'] = 0;
\r
211 $plugins = array();
\r
213 if ( isset($manager->subscriptions['ValidateForm']) )
\r
215 $plugins = array_merge($plugins, $manager->subscriptions['ValidateForm']);
\r
218 if ( isset($manager->subscriptions['PreAddComment']) )
\r
220 $plugins = array_merge($plugins, $manager->subscriptions['PreAddComment']);
\r
223 if ( isset($manager->subscriptions['PostAddComment']) )
\r
225 $plugins = array_merge($plugins, $manager->subscriptions['PostAddComment']);
\r
228 $plugins = array_unique($plugins);
\r
230 while ( list(, $plugin) = each($plugins) )
\r
232 $p = $manager->getPlugin($plugin);
\r
233 $continue = $continue || $p->supportsFeature('handleSpam');
\r
236 $spamcheck = array(
\r
237 'type' => 'comment',
\r
238 'body' => $comment['body'],
\r
239 'id' => $comment['itemid'],
\r
241 'return' => $continue
\r
244 // begin if: member logged in
\r
245 if ( $member->isLoggedIn() )
\r
247 $spamcheck['author'] = $member->displayname;
\r
248 $spamcheck['email'] = $member->email;
\r
253 $spamcheck['author'] = $comment['user'];
\r
254 $spamcheck['email'] = $comment['email'];
\r
255 $spamcheck['url'] = $comment['userid'];
\r
258 $manager->notify('SpamCheck', array('spamcheck' => &$spamcheck) );
\r
260 if ( !$continue && isset($spamcheck['result']) && $spamcheck['result'] == TRUE )
\r
262 return _ERROR_COMMENTS_SPAM;
\r
265 // isValidComment returns either "1" or an error message
\r
266 $isvalid = $this->isValidComment($comment, $spamcheck);
\r
268 if ( $isvalid != 1 )
\r
273 // begin if: send email to notification address
\r
274 if ( $settings->getNotifyAddress() && $settings->notifyOnComment() )
\r
277 $message = _NOTIFY_NC_MSG . ' ' . $this->itemid . "\n";
\r
278 $temp = parse_url($CONF['Self']);
\r
280 if ( $temp['scheme'] )
\r
282 $message .= Link::create_item_link($this->itemid) . "\n\n";
\r
286 $tempurl = $settings->getURL();
\r
288 if ( i18n::substr($tempurl, -1) == '/' || i18n::substr($tempurl, -4) == '.php' )
\r
290 $message .= $tempurl . '?itemid=' . $this->itemid . "\n\n";
\r
294 $message .= $tempurl . '/?itemid=' . $this->itemid . "\n\n";
\r
298 if ( $comment['memberid'] == 0 )
\r
300 $message .= _NOTIFY_USER . ' ' . $comment['user'] . "\n";
\r
301 $message .= _NOTIFY_USERID . ' ' . $comment['userid'] . "\n";
\r
305 $message .= _NOTIFY_MEMBER .' ' . $member->getDisplayName() . ' (ID=' . $member->getID() . ")\n";
\r
308 $message .= _NOTIFY_HOST . ' ' . $comment['host'] . "\n";
\r
309 $message .= _NOTIFY_COMMENT . "\n " . $comment['body'] . "\n";
\r
310 $message .= NOTIFICATION::get_mail_footer();
\r
312 $subject = _NOTIFY_NC_TITLE . ' ' . strip_tags($item['title']) . ' (' . $this->itemid . ')';
\r
314 $from = $member->getNotifyFromMailAddress($comment['email']);
\r
316 NOTIFICATION::mail($settings->getNotifyAddress(), $subject, $message, $from, i18n::get_current_charset());
\r
319 $comment = Comment::prepare($comment);
\r
321 $manager->notify('PreAddComment', array('comment' => &$comment, 'spamcheck' => &$spamcheck) );
\r
323 $name = DB::quoteValue($comment['user']);
\r
324 $url = DB::quoteValue($comment['userid']);
\r
325 $email = DB::quoteValue($comment['email']);
\r
326 $body = DB::quoteValue($comment['body']);
\r
327 $host = DB::quoteValue($comment['host']);
\r
328 $ip = DB::quoteValue($comment['ip']);
\r
329 $memberid = (integer) $comment['memberid'];
\r
330 $timestamp = DB::formatDateTime($comment['timestamp']);
\r
331 $itemid = $this->itemid;
\r
333 $qSql = 'SELECT COUNT(*) AS result '
\r
334 . 'FROM ' . sql_table('comment')
\r
336 . 'cmail = ' . $url
\r
337 . ' AND cmember = ' . $memberid
\r
338 . ' AND cbody = ' . $body
\r
339 . ' AND citem = ' . $itemid
\r
340 . ' AND cblog = ' . $item['blogid'];
\r
341 $result = (integer) DB::getValue($qSql);
\r
345 return _ERROR_BADACTION;
\r
348 $query = sprintf('INSERT INTO %s (cuser, cmail, cemail, cmember, cbody, citem, ctime, chost, cip, cblog) '
\r
349 . 'VALUES (%s, %s, %s, %d, %s, %d, %s, %s, %s, %d)'
\r
350 , sql_table('comment'), $name, $url, $email, $memberid, $body, $itemid, $timestamp, $host, $ip, $item['blogid']);
\r
352 DB::execute($query);
\r
354 // post add comment
\r
355 $commentid = DB::getInsertId();
\r
356 $manager->notify('PostAddComment', array('comment' => &$comment, 'commentid' => &$commentid, 'spamcheck' => &$spamcheck) );
\r
364 * Comments::isValidComment()
\r
365 * Checks if a comment is valid and call plugins
\r
366 * that can check if the comment is a spam comment
\r
368 * @param Array $comment array with comment elements
\r
369 * @param Array $spamcheck array with spamcheck elements
\r
371 function isValidComment(&$comment, &$spamcheck)
\r
373 global $member, $manager;
\r
375 // check if there exists a item for this date
\r
376 $item =& $manager->getItem($this->itemid, 0, 0);
\r
380 return _ERROR_NOSUCHITEM;
\r
383 if ( $item['closed'] )
\r
385 return _ERROR_ITEMCLOSED;
\r
388 // don't allow words that are too long
\r
389 if ( preg_match('/[a-zA-Z0-9|\.,;:!\?=\/\\\\]{90,90}/', $comment['body']) != 0 )
\r
391 return _ERROR_COMMENT_LONGWORD;
\r
394 // check lengths of comment
\r
395 if ( i18n::strlen($comment['body']) < 3 )
\r
397 return _ERROR_COMMENT_NOCOMMENT;
\r
400 if ( i18n::strlen($comment['body']) > 5000 )
\r
402 return _ERROR_COMMENT_TOOLONG;
\r
405 // only check username if no member logged in
\r
406 if ( !$member->isLoggedIn() && (i18n::strlen($comment['user']) < 2) )
\r
408 return _ERROR_COMMENT_NOUSERNAME;
\r
411 if ( (i18n::strlen($comment['email']) != 0) && !NOTIFICATION::address_validation(trim($comment['email'])) )
\r
413 return _ERROR_BADMAILADDRESS;
\r
416 // let plugins do verification (any plugin which thinks the comment is invalid
\r
417 // can change 'error' to something other than '1')
\r
419 $manager->notify('ValidateForm', array('type' => 'comment', 'comment' => &$comment, 'error' => &$result, 'spamcheck' => &$spamcheck) );
\r