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 $actions = new CommentActions($this);
\r
75 $parser = new Parser($actions->getDefinedActions(),$actions);
\r
76 $actions->setTemplate($template);
\r
77 $actions->setParser($parser);
\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 = sql_query($query);
\r
88 $this->commentcount = sql_num_rows($comments);
\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 while ( $comment = sql_fetch_assoc($comments) ) {
\r
107 $comment['timestamp'] = strtotime($comment['ctime']);
\r
108 $actions->setCurrentComment($comment);
\r
109 $actions->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 sql_free_result($comments);
\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 = sql_query($query);
\r
130 $arr = sql_fetch_row($res);
\r
136 * Comments::addComment()
\r
137 * Adds a new comment to the database
\r
139 * @param string $timestamp
\r
140 * @param array $comment
\r
143 function addComment($timestamp, $comment)
\r
145 global $CONF, $member, $manager;
\r
147 $blogid = getBlogIDFromItemID($this->itemid);
\r
149 $settings =& $manager->getBlog($blogid);
\r
150 $settings->readSettings();
\r
152 // begin if: comments disabled
\r
153 if ( !$settings->commentsEnabled() )
\r
155 return _ERROR_COMMENTS_DISABLED;
\r
158 // begin if: public cannot comment
\r
159 if ( !$settings->isPublic() && !$member->isLoggedIn() )
\r
161 return _ERROR_COMMENTS_NONPUBLIC;
\r
164 // begin if: comment uses a protected member name
\r
165 if ( $CONF['ProtectMemNames'] && !$member->isLoggedIn() && Member::isNameProtected($comment['user']) )
\r
167 return _ERROR_COMMENTS_MEMBERNICK;
\r
170 // begin if: email required, but missing (doesn't apply to members)
\r
171 if ( $settings->emailRequired() && i18n::strlen($comment['email']) == 0 && !$member->isLoggedIn() )
\r
173 return _ERROR_EMAIL_REQUIRED;
\r
176 // begin if: commenter's name is too long
\r
177 if ( i18n::strlen($comment['user']) > 40 )
\r
179 return _ERROR_USER_TOO_LONG;
\r
182 // begin if: commenter's email is too long
\r
183 if ( i18n::strlen($comment['email']) > 100 )
\r
185 return _ERROR_EMAIL_TOO_LONG;
\r
188 // begin if: commenter's url is too long
\r
189 if ( i18n::strlen($comment['userid']) > 100 )
\r
191 return _ERROR_URL_TOO_LONG;
\r
194 $comment['timestamp'] = $timestamp;
\r
195 $comment['host'] = gethostbyaddr(serverVar('REMOTE_ADDR') );
\r
196 $comment['ip'] = serverVar('REMOTE_ADDR');
\r
198 // begin if: member is logged in, use that data
\r
199 if ( $member->isLoggedIn() )
\r
201 $comment['memberid'] = $member->getID();
\r
202 $comment['user'] = '';
\r
203 $comment['userid'] = '';
\r
204 $comment['email'] = '';
\r
208 $comment['memberid'] = 0;
\r
213 $plugins = array();
\r
215 if ( isset($manager->subscriptions['ValidateForm']) )
\r
217 $plugins = array_merge($plugins, $manager->subscriptions['ValidateForm']);
\r
220 if ( isset($manager->subscriptions['PreAddComment']) )
\r
222 $plugins = array_merge($plugins, $manager->subscriptions['PreAddComment']);
\r
225 if ( isset($manager->subscriptions['PostAddComment']) )
\r
227 $plugins = array_merge($plugins, $manager->subscriptions['PostAddComment']);
\r
230 $plugins = array_unique($plugins);
\r
232 while ( list(, $plugin) = each($plugins) )
\r
234 $p = $manager->getPlugin($plugin);
\r
235 $continue = $continue || $p->supportsFeature('handleSpam');
\r
238 $spamcheck = array(
\r
239 'type' => 'comment',
\r
240 'body' => $comment['body'],
\r
241 'id' => $comment['itemid'],
\r
243 'return' => $continue
\r
246 // begin if: member logged in
\r
247 if ( $member->isLoggedIn() )
\r
249 $spamcheck['author'] = $member->displayname;
\r
250 $spamcheck['email'] = $member->email;
\r
255 $spamcheck['author'] = $comment['user'];
\r
256 $spamcheck['email'] = $comment['email'];
\r
257 $spamcheck['url'] = $comment['userid'];
\r
260 $manager->notify('SpamCheck', array('spamcheck' => &$spamcheck) );
\r
262 if ( !$continue && isset($spamcheck['result']) && $spamcheck['result'] == TRUE )
\r
264 return _ERROR_COMMENTS_SPAM;
\r
267 // isValidComment returns either "1" or an error message
\r
268 $isvalid = $this->isValidComment($comment, $spamcheck);
\r
270 if ( $isvalid != 1 )
\r
275 // begin if: send email to notification address
\r
276 if ( $settings->getNotifyAddress() && $settings->notifyOnComment() )
\r
279 $message = _NOTIFY_NC_MSG . ' ' . $this->itemid . "\n";
\r
280 $temp = parse_url($CONF['Self']);
\r
282 if ( $temp['scheme'] )
\r
284 $message .= Link::create_item_link($this->itemid) . "\n\n";
\r
288 $tempurl = $settings->getURL();
\r
290 if ( i18n::substr($tempurl, -1) == '/' || i18n::substr($tempurl, -4) == '.php' )
\r
292 $message .= $tempurl . '?itemid=' . $this->itemid . "\n\n";
\r
296 $message .= $tempurl . '/?itemid=' . $this->itemid . "\n\n";
\r
300 if ( $comment['memberid'] == 0 )
\r
302 $message .= _NOTIFY_USER . ' ' . $comment['user'] . "\n";
\r
303 $message .= _NOTIFY_USERID . ' ' . $comment['userid'] . "\n";
\r
307 $message .= _NOTIFY_MEMBER .' ' . $member->getDisplayName() . ' (ID=' . $member->getID() . ")\n";
\r
310 $message .= _NOTIFY_HOST . ' ' . $comment['host'] . "\n";
\r
311 $message .= _NOTIFY_COMMENT . "\n " . $comment['body'] . "\n";
\r
312 $message .= NOTIFICATION::get_mail_footer();
\r
314 $item =& $manager->getItem($this->itemid, 0, 0);
\r
315 $subject = _NOTIFY_NC_TITLE . ' ' . strip_tags($item['title']) . ' (' . $this->itemid . ')';
\r
317 $from = $member->getNotifyFromMailAddress($comment['email']);
\r
319 NOTIFICATION::mail($settings->getNotifyAddress(), $subject, $message, $from, i18n::get_current_charset());
\r
322 $comment = Comment::prepare($comment);
\r
324 $manager->notify('PreAddComment', array('comment' => &$comment, 'spamcheck' => &$spamcheck) );
\r
326 $name = sql_real_escape_string($comment['user']);
\r
327 $url = sql_real_escape_string($comment['userid']);
\r
328 $email = sql_real_escape_string($comment['email']);
\r
329 $body = sql_real_escape_string($comment['body']);
\r
330 $host = sql_real_escape_string($comment['host']);
\r
331 $ip = sql_real_escape_string($comment['ip']);
\r
332 $memberid = intval($comment['memberid']);
\r
333 $timestamp = date('Y-m-d H:i:s', $comment['timestamp']);
\r
334 $itemid = $this->itemid;
\r
336 $qSql = 'SELECT COUNT(*) AS result '
\r
337 . 'FROM ' . sql_table('comment')
\r
339 . 'cmail = "' . $url . '"'
\r
340 . ' AND cmember = "' . $memberid . '"'
\r
341 . ' AND cbody = "' . $body . '"'
\r
342 . ' AND citem = "' . $itemid . '"'
\r
343 . ' AND cblog = "' . $blogid . '"';
\r
344 $result = (integer) quickQuery($qSql);
\r
348 return _ERROR_BADACTION;
\r
351 $query = 'INSERT INTO '.sql_table('comment').' (CUSER, CMAIL, CEMAIL, CMEMBER, CBODY, CITEM, CTIME, CHOST, CIP, CBLOG) '
\r
352 . "VALUES ('$name', '$url', '$email', $memberid, '$body', $itemid, '$timestamp', '$host', '$ip', '$blogid')";
\r
356 // post add comment
\r
357 $commentid = sql_insert_id();
\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