4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2004 The Nucleus Group
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * (see nucleus/documentation/index.html#license for more info)
13 * A class representing the comments (all of them) for a certain post on a ceratin blog
15 * $Id: COMMENTS.php,v 1.1.1.1 2005-02-28 07:14:49 kimitake Exp $
19 // item for which comment are being displayed
22 // reference to the itemActions object that is calling the showComments function
25 // total amount of comments displayed
29 * Creates a new COMMENTS object for the given blog and item
34 function COMMENTS($itemid) {
35 $this->itemid = intval($itemid);
38 * Used when parsing comments
41 * itemActions object, that will take care of the parsing
43 function setItemActions(&$itemActions) {
44 $this->itemActions =& $itemActions;
48 * Shows maximum $max comments to the given item using the given template
49 * returns the amount of shown comments (if maxToShow = -1, then there is no limit)
54 * max. comments to show
56 * indicates if the 'no comments' thingie should be outputted when there are no comments
57 * (useful for closed items)
59 * Highlight to use (if any)
61 function showComments($template, $maxToShow = -1, $showNone = 1, $highlight = '') {
62 global $CONF, $manager;
64 // create parser object & action handler
65 $actions =& new COMMENTACTIONS($this);
66 $parser =& new PARSER($actions->getDefinedActions(),$actions);
67 $actions->setTemplate($template);
68 $actions->setParser($parser);
70 if ($maxToShow == 0) {
71 $this->commentcount = $this->amountComments();
73 $query = 'SELECT c.cnumber as commentid, c.cbody as body, c.cuser as user, c.cmail as userid, c.cmember as memberid, c.ctime, c.chost as host, c.cip as ip, c.cblog as blogid'
74 . ' FROM '.sql_table('comment').' as c'
75 . ' WHERE c.citem=' . $this->itemid
76 . ' ORDER BY c.ctime';
78 $comments = sql_query($query);
79 $this->commentcount = mysql_num_rows($comments);
82 // if no result was found
83 if ($this->commentcount == 0) {
84 // note: when no reactions, COMMENTS_HEADER and COMMENTS_FOOTER are _NOT_ used
85 if ($showNone) $parser->parse($template['COMMENTS_NONE']);
89 // if too many comments to show
90 if (($maxToShow != -1) && ($this->commentcount > $maxToShow)) {
91 $parser->parse($template['COMMENTS_TOOMUCH']);
95 $parser->parse($template['COMMENTS_HEADER']);
97 while ( $comment = mysql_fetch_assoc($comments) ) {
98 $comment['timestamp'] = strtotime($comment['ctime']);
99 $actions->setCurrentComment($comment);
100 $actions->setHighlight($highlight);
101 $manager->notify('PreComment', array('comment' => &$comment));
102 $parser->parse($template['COMMENTS_BODY']);
103 $manager->notify('PostComment', array('comment' => &$comment));
106 $parser->parse($template['COMMENTS_FOOTER']);
108 mysql_free_result($comments);
110 return $this->commentcount;
114 * Returns the amount of comments for this itemid
116 function amountComments() {
117 $query = 'SELECT COUNT(*)'
118 . ' FROM '.sql_table('comment').' as c'
119 . ' WHERE c.citem='. $this->itemid;
120 $res = sql_query($query);
121 $arr = mysql_fetch_row($res);
127 function addComment($timestamp, $comment) {
128 global $CONF, $member, $manager;
130 $blogid = getBlogIDFromItemID($this->itemid);
132 $settings =& $manager->getBlog($blogid);
133 $settings->readSettings();
135 if (!$settings->commentsEnabled())
136 return _ERROR_COMMENTS_DISABLED;
138 if (!$settings->isPublic() && !$member->isLoggedIn())
139 return _ERROR_COMMENTS_NONPUBLIC;
141 // member name protection
142 if ($CONF['ProtectMemNames'] && !$member->isLoggedIn() && MEMBER::isNameProtected($comment['user']))
143 return _ERROR_COMMENTS_MEMBERNICK;
145 // isValidComment returns either "1" or an error message
146 $isvalid = $this->isValidComment($comment);
150 $comment['timestamp'] = $timestamp;
151 $comment['host'] = gethostbyaddr(serverVar('REMOTE_ADDR'));
152 $comment['ip'] = serverVar('REMOTE_ADDR');
154 // if member is logged in, use that data
155 if ($member->isLoggedIn()) {
156 $comment['memberid'] = $member->getID();
157 $comment['user'] = '';
158 $comment['userid'] = '';
160 $comment['memberid'] = 0;
164 // send email to notification address, if any
165 if ($settings->getNotifyAddress() && $settings->notifyOnComment()) {
167 $mailto_msg = _NOTIFY_NC_MSG . ' ' . $this->itemid . "\n";
168 $mailto_msg .= $CONF['IndexURL'] . 'index.php?itemid=' . $this->itemid . "\n\n";
169 if ($comment['memberid'] == 0) {
170 $mailto_msg .= _NOTIFY_USER . ' ' . $comment['user'] . "\n";
171 $mailto_msg .= _NOTIFY_USERID . ' ' . $comment['userid'] . "\n";
173 $mailto_msg .= _NOTIFY_MEMBER .' ' . $member->getDisplayName() . ' (ID=' . $member->getID() . ")\n";
175 $mailto_msg .= _NOTIFY_HOST . ' ' . $comment['host'] . "\n";
176 $mailto_msg .= _NOTIFY_COMMENT . "\n " . $comment['body'] . "\n";
177 $mailto_msg .= getMailFooter();
179 $item =& $manager->getItem($this->itemid, 0, 0);
180 $mailto_title = _NOTIFY_NC_TITLE . ' ' . strip_tags($item['title']) . ' (' . $this->itemid . ')';
182 $frommail = $member->getNotifyFromMailAddress($comment['userid']);
184 $notify =& new NOTIFICATION($settings->getNotifyAddress());
185 $notify->notify($mailto_title, $mailto_msg , $frommail);
188 $comment = COMMENT::prepare($comment);
190 $manager->notify('PreAddComment',array('comment' => &$comment));
192 $name = addslashes($comment['user']);
193 $url = addslashes($comment['userid']);
194 $body = addslashes($comment['body']);
195 $host = addslashes($comment['host']);
196 $ip = addslashes($comment['ip']);
197 $memberid = intval($comment['memberid']);
198 $timestamp = date('Y-m-d H:i:s', $comment['timestamp']);
199 $itemid = $this->itemid;
201 $query = 'INSERT INTO '.sql_table('comment').' (CUSER, CMAIL, CMEMBER, CBODY, CITEM, CTIME, CHOST, CIP, CBLOG) '
202 . "VALUES ('$name', '$url', $memberid, '$body', $itemid, '$timestamp', '$host', '$ip', '$blogid')";
207 $commentid = mysql_insert_id();
208 $manager->notify('PostAddComment',array('comment' => &$comment, 'commentid' => &$commentid));
215 function isValidComment($comment) {
216 global $member, $manager;
218 // check if there exists a item for this date
219 $item =& $manager->getItem($this->itemid,0,0);
222 return _ERROR_NOSUCHITEM;
225 return _ERROR_ITEMCLOSED;
227 // don't allow words that are too long
228 if (eregi('[a-zA-Z0-9|\.,;:!\?=\/\\]{90,90}',$comment['body']) != false)
229 return _ERROR_COMMENT_LONGWORD;
231 // check lengths of comment
232 if (strlen($comment['body'])<3)
233 return _ERROR_COMMENT_NOCOMMENT;
235 if (strlen($comment['body'])>5000)
236 return _ERROR_COMMENT_TOOLONG;
238 // only check username if no member logged in
239 if (!$member->isLoggedIn())
240 if (strlen($comment['user'])<2)
241 return _ERROR_COMMENT_NOUSERNAME;
243 // let plugins do verification (any plugin which thinks the comment is invalid
244 // can change 'error' to something other than '1')
246 $manager->notify('ValidateForm', array('type' => 'comment', 'comment' => &$comment, 'error' => &$result));
255 * This class is used when parsing comment templates
257 class COMMENTACTIONS extends BaseActions {
259 // ref to COMMENTS object which is using this object to handle
263 // template to use to parse the comments
266 // comment currenlty being handled (mysql result assoc array; see COMMENTS::showComments())
269 function COMMENTACTIONS(&$comments) {
270 // call constructor of superclass first
271 $this->BaseActions();
273 // reference to the comments object
274 $this->setCommentsObj($comments);
277 function getDefinedActions() {
308 function setParser(&$parser) { $this->parser =& $parser; }
309 function setCommentsObj(&$commentsObj) {$this->commentsObj =& $commentsObj; }
310 function setTemplate($template) { $this->template =& $template; }
311 function setCurrentComment(&$comment) {
312 if ($comment['memberid'] != 0) {
313 $comment['authtext'] = $template['COMMENTS_AUTH'];
315 $mem = MEMBER::createFromID($comment['memberid']);
316 $comment['user'] = $mem->getDisplayName();
318 $comment['userid'] = $mem->getURL();
320 $comment['userid'] = $mem->getEmail();
322 $comment['userlinkraw'] = createMemberLink(
323 $comment['memberid'],
324 $this->commentsObj->itemActions->linkparams
329 // create smart links
330 if (isValidMailAddress($comment['userid']))
331 $comment['userlinkraw'] = 'mailto:'.$comment['userid'];
332 elseif (strstr($comment['userid'],'http://') != false)
333 $comment['userlinkraw'] = $comment['userid'];
334 elseif (strstr($comment['userid'],'www') != false)
335 $comment['userlinkraw'] = 'http://'.$comment['userid'];
338 $this->currentComment =& $comment;
341 function parse_commentcount() { echo $this->commentsObj->commentcount; }
342 function parse_commentword() {
343 if ($this->commentsObj->commentcount == 1)
344 echo $this->template['COMMENTS_ONE'];
346 echo $this->template['COMMENTS_MANY'];
349 function parse_itemlink() { echo createItemLink($this->commentsObj->itemid, $this->commentsObj->itemActions->linkparams); }
350 function parse_itemid() { echo $this->commentsObj->itemid; }
351 function parse_itemtitle($maxLength = 0) {
353 $this->commentsObj->itemActions->parse_title();
355 $this->commentsObj->itemActions->parse_syndicate_title($maxLength);
358 function parse_date($format = '') {
359 echo formatDate($format, $this->currentComment['timestamp'], $this->template['FORMAT_DATE']);
361 function parse_time($format = '') {
363 ($format == '') ? $this->template['FORMAT_TIME'] : $format,
364 $this->currentComment['timestamp']
368 function parse_commentid() { echo $this->currentComment['commentid']; }
369 function parse_body() { echo $this->highlight($this->currentComment['body']); }
370 function parse_memberid() { echo $this->currentComment['memberid']; }
371 function parse_timestamp() { echo $this->currentComment['timestamp']; }
372 function parse_host() { echo $this->currentComment['host']; }
373 function parse_ip() { echo $this->currentComment['ip']; }
374 function parse_blogid() { echo $this->currentComment['blogid']; }
376 function parse_user() { echo $this->currentComment['user']; }
377 function parse_userid() { echo $this->currentComment['userid']; }
378 function parse_userlinkraw() { echo $this->currentComment['userlinkraw']; }
379 function parse_userlink() {
380 if ($this->currentComment['userlinkraw'])
381 echo '<a href="'.$this->currentComment['userlinkraw'].'">'.$this->currentComment['user'].'</a>';
383 echo $this->currentComment['user'];
385 function parse_short() {
386 // $tmp = strtok($this->currentComment['body'],"\n");
387 // $tmp = str_replace('<br />','',$tmp);
388 $tmp = strip_tags($this->currentComment['body']);
389 $tmp = mb_strimwidth($tmp, 0, 60, "...", "UTF-8");
390 $tmp = str_replace("\r\n", "\r", $tmp);
391 $tmp = str_replace("\r", "\n", $tmp);
392 $tmp = str_replace("\n",'¶',$tmp);
394 if ($tmp != $this->currentComment['body'])
395 $this->parser->parse($this->template['COMMENTS_CONTINUED']);
397 function parse_authtext() {
398 if ($this->currentComment['memberid'] != 0)
399 $this->parser->parse($this->template['COMMENTS_AUTH']);
403 * Executes a plugin templatevar
405 * @param pluginName name of plugin (without the NP_)
407 * extra parameters can be added
409 function parse_plugin($pluginName) {
412 // only continue when the plugin is really installed
413 if (!$manager->pluginInstalled('NP_' . $pluginName))
416 $plugin =& $manager->getPlugin('NP_' . $pluginName);
417 if (!$plugin) return;
420 $params = func_get_args();
422 // remove plugin name
423 array_shift($params);
425 // pass info on current item and current comment as well
426 $params = array_merge(array(&$this->currentComment),$params);
427 $params = array_merge(array(&$this->commentsObj->itemActions->currentItem),$params);
429 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);