4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2004 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
13 * A class representing the comments (all of them) for a certain post on a ceratin blog
\r
17 // item for which comment are being displayed
\r
20 // reference to the itemActions object that is calling the showComments function
\r
23 // total amount of comments displayed
\r
27 * Creates a new COMMENTS object for the given blog and item
\r
32 function COMMENTS($itemid) {
\r
33 $this->itemid = intval($itemid);
\r
36 * Used when parsing comments
\r
38 * @param $itemActions
\r
39 * itemActions object, that will take care of the parsing
\r
41 function setItemActions(&$itemActions) {
\r
42 $this->itemActions =& $itemActions;
\r
46 * Shows maximum $max comments to the given item using the given template
\r
47 * returns the amount of shown comments (if maxToShow = -1, then there is no limit)
\r
52 * max. comments to show
\r
54 * indicates if the 'no comments' thingie should be outputted when there are no comments
\r
55 * (useful for closed items)
\r
57 * Highlight to use (if any)
\r
59 function showComments($template, $maxToShow = -1, $showNone = 1, $highlight = '') {
\r
60 global $CONF, $manager;
\r
62 // create parser object & action handler
\r
63 $actions = new COMMENTACTIONS($this);
\r
64 $parser = new PARSER($actions->getDefinedActions(),$actions);
\r
65 $actions->setTemplate($template);
\r
66 $actions->setParser($parser);
\r
68 if ($maxToShow == 0) {
\r
69 $this->commentcount = $this->amountComments();
\r
71 $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'
\r
72 . ' FROM '.sql_table('comment').' as c'
\r
73 . ' WHERE c.citem=' . $this->itemid
\r
74 . ' ORDER BY c.ctime';
\r
76 $comments = sql_query($query);
\r
77 $this->commentcount = mysql_num_rows($comments);
\r
80 // if no result was found
\r
81 if ($this->commentcount == 0) {
\r
82 // note: when no reactions, COMMENTS_HEADER and COMMENTS_FOOTER are _NOT_ used
\r
83 if ($showNone) $parser->parse($template['COMMENTS_NONE']);
\r
87 // if too many comments to show
\r
88 if (($maxToShow != -1) && ($this->commentcount > $maxToShow)) {
\r
89 $parser->parse($template['COMMENTS_TOOMUCH']);
\r
93 $parser->parse($template['COMMENTS_HEADER']);
\r
95 while ( $comment = mysql_fetch_assoc($comments) ) {
\r
96 $comment['timestamp'] = strtotime($comment['ctime']);
\r
97 $actions->setCurrentComment($comment);
\r
98 $actions->setHighlight($highlight);
\r
99 $manager->notify('PreComment', array('comment' => &$comment));
\r
100 $parser->parse($template['COMMENTS_BODY']);
\r
101 $manager->notify('PostComment', array('comment' => &$comment));
\r
104 $parser->parse($template['COMMENTS_FOOTER']);
\r
106 mysql_free_result($comments);
\r
108 return $this->commentcount;
\r
112 * Returns the amount of comments for this itemid
\r
114 function amountComments() {
\r
115 $query = 'SELECT COUNT(*)'
\r
116 . ' FROM '.sql_table('comment').' as c'
\r
117 . ' WHERE c.citem='. $this->itemid;
\r
118 $res = sql_query($query);
\r
119 $arr = mysql_fetch_row($res);
\r
125 function addComment($timestamp, $comment) {
\r
126 global $CONF, $member, $manager;
\r
128 $blogid = getBlogIDFromItemID($this->itemid);
\r
130 $settings =& $manager->getBlog($blogid);
\r
131 $settings->readSettings();
\r
133 if (!$settings->commentsEnabled())
\r
134 return _ERROR_COMMENTS_DISABLED;
\r
136 if (!$settings->isPublic() && !$member->isLoggedIn())
\r
137 return _ERROR_COMMENTS_NONPUBLIC;
\r
139 // member name protection
\r
140 if ($CONF['ProtectMemNames'] && !$member->isLoggedIn() && MEMBER::isNameProtected($comment['user']))
\r
141 return _ERROR_COMMENTS_MEMBERNICK;
\r
143 // isValidComment returns either "1" or an error message
\r
144 $isvalid = $this->isValidComment($comment);
\r
148 $comment['timestamp'] = $timestamp;
\r
149 $comment['host'] = gethostbyaddr(serverVar('REMOTE_ADDR'));
\r
150 $comment['ip'] = serverVar('REMOTE_ADDR');
\r
152 // if member is logged in, use that data
\r
153 if ($member->isLoggedIn()) {
\r
154 $comment['memberid'] = $member->getID();
\r
155 $comment['user'] = '';
\r
156 $comment['userid'] = '';
\r
158 $comment['memberid'] = 0;
\r
162 // send email to notification address, if any
\r
163 if ($settings->getNotifyAddress() && $settings->notifyOnComment()) {
\r
165 $mailto_msg = _NOTIFY_NC_MSG . ' ' . $this->itemid . "\n";
\r
166 $mailto_msg .= $CONF['IndexURL'] . 'index.php?itemid=' . $this->itemid . "\n\n";
\r
167 if ($comment['memberid'] == 0) {
\r
168 $mailto_msg .= _NOTIFY_USER . ' ' . $comment['user'] . "\n";
\r
169 $mailto_msg .= _NOTIFY_USERID . ' ' . $comment['userid'] . "\n";
\r
171 $mailto_msg .= _NOTIFY_MEMBER .' ' . $member->getDisplayName() . ' (ID=' . $member->getID() . ")\n";
\r
173 $mailto_msg .= _NOTIFY_HOST . ' ' . $comment['host'] . "\n";
\r
174 $mailto_msg .= _NOTIFY_COMMENT . "\n " . $comment['body'] . "\n";
\r
175 $mailto_msg .= getMailFooter();
\r
177 $item =& $manager->getItem($this->itemid, 0, 0);
\r
178 $mailto_title = _NOTIFY_NC_TITLE . ' ' . strip_tags($item['title']) . ' (' . $this->itemid . ')';
\r
180 $frommail = $member->getNotifyFromMailAddress($comment['userid']);
\r
182 $notify = new NOTIFICATION($settings->getNotifyAddress());
\r
183 $notify->notify($mailto_title, $mailto_msg , $frommail);
\r
186 $comment = COMMENT::prepare($comment);
\r
188 $manager->notify('PreAddComment',array('comment' => &$comment));
\r
190 $name = addslashes($comment['user']);
\r
191 $url = addslashes($comment['userid']);
\r
192 $body = addslashes($comment['body']);
\r
193 $host = addslashes($comment['host']);
\r
194 $ip = addslashes($comment['ip']);
\r
195 $memberid = intval($comment['memberid']);
\r
196 $timestamp = date('Y-m-d H:i:s', $comment['timestamp']);
\r
197 $itemid = $this->itemid;
\r
199 $query = 'INSERT INTO '.sql_table('comment').' (CUSER, CMAIL, CMEMBER, CBODY, CITEM, CTIME, CHOST, CIP, CBLOG) '
\r
200 . "VALUES ('$name', '$url', $memberid, '$body', $itemid, '$timestamp', '$host', '$ip', '$blogid')";
\r
204 // post add comment
\r
205 $commentid = mysql_insert_id();
\r
206 $manager->notify('PostAddComment',array('comment' => &$comment, 'commentid' => &$commentid));
\r
213 function isValidComment($comment) {
\r
214 global $member, $manager;
\r
216 // check if there exists a item for this date
\r
217 $item =& $manager->getItem($this->itemid,0,0);
\r
220 return _ERROR_NOSUCHITEM;
\r
222 if ($item['closed'])
\r
223 return _ERROR_ITEMCLOSED;
\r
225 // don't allow words that are too long
\r
226 if (eregi('[a-zA-Z0-9|\.,;:!\?=\/\\]{90,90}',$comment['body']) != false)
\r
227 return _ERROR_COMMENT_LONGWORD;
\r
229 // check lengths of comment
\r
230 if (strlen($comment['body'])<3)
\r
231 return _ERROR_COMMENT_NOCOMMENT;
\r
233 if (strlen($comment['body'])>5000)
\r
234 return _ERROR_COMMENT_TOOLONG;
\r
236 // only check username if no member logged in
\r
237 if (!$member->isLoggedIn())
\r
238 if (strlen($comment['user'])<2)
\r
239 return _ERROR_COMMENT_NOUSERNAME;
\r
241 // let plugins do verification (any plugin which thinks the comment is invalid
\r
242 // can change 'error' to something other than '1')
\r
244 $manager->notify('ValidateForm', array('type' => 'comment', 'comment' => &$comment, 'error' => &$result));
\r
253 * This class is used when parsing comment templates
\r
255 class COMMENTACTIONS extends BaseActions {
\r
257 // ref to COMMENTS object which is using this object to handle
\r
258 // its templatevars
\r
261 // template to use to parse the comments
\r
264 // comment currenlty being handled (mysql result assoc array; see COMMENTS::showComments())
\r
265 var $currentComment;
\r
267 function COMMENTACTIONS(&$comments) {
\r
268 // call constructor of superclass first
\r
269 $this->BaseActions();
\r
271 // reference to the comments object
\r
272 $this->setCommentsObj($comments);
\r
275 function getDefinedActions() {
\r
306 function setParser(&$parser) { $this->parser =& $parser; }
\r
307 function setCommentsObj(&$commentsObj) {$this->commentsObj =& $commentsObj; }
\r
308 function setTemplate($template) { $this->template =& $template; }
\r
309 function setCurrentComment(&$comment) {
\r
310 if ($comment['memberid'] != 0) {
\r
311 $comment['authtext'] = $template['COMMENTS_AUTH'];
\r
313 $mem = MEMBER::createFromID($comment['memberid']);
\r
314 $comment['user'] = $mem->getDisplayName();
\r
315 if ($mem->getURL())
\r
316 $comment['userid'] = $mem->getURL();
\r
318 $comment['userid'] = $mem->getEmail();
\r
320 $comment['userlinkraw'] = createMemberLink(
\r
321 $comment['memberid'],
\r
322 $this->commentsObj->itemActions->linkparams
\r
327 // create smart links
\r
328 if (isValidMailAddress($comment['userid']))
\r
329 $comment['userlinkraw'] = 'mailto:'.$comment['userid'];
\r
330 elseif (strstr($comment['userid'],'http://') != false)
\r
331 $comment['userlinkraw'] = $comment['userid'];
\r
332 elseif (strstr($comment['userid'],'www') != false)
\r
333 $comment['userlinkraw'] = 'http://'.$comment['userid'];
\r
336 $this->currentComment =& $comment;
\r
339 function parse_commentcount() { echo $this->commentsObj->commentcount; }
\r
340 function parse_commentword() {
\r
341 if ($this->commentsObj->commentcount == 1)
\r
342 echo $this->template['COMMENTS_ONE'];
\r
344 echo $this->template['COMMENTS_MANY'];
\r
347 function parse_itemlink() { echo createItemLink($this->commentsObj->itemid, $this->commentsObj->itemActions->linkparams); }
\r
348 function parse_itemid() { echo $this->commentsObj->itemid; }
\r
349 function parse_itemtitle($maxLength = 0) {
\r
350 if ($maxLength == 0)
\r
351 $this->commentsObj->itemActions->parse_title();
\r
353 $this->commentsObj->itemActions->parse_syndicate_title($maxLength);
\r
356 function parse_date($format = '') {
\r
357 echo formatDate($format, $this->currentComment['timestamp'], $this->template['FORMAT_DATE']);
\r
359 function parse_time($format = '') {
\r
361 ($format == '') ? $this->template['FORMAT_TIME'] : $format,
\r
362 $this->currentComment['timestamp']
\r
366 function parse_commentid() { echo $this->currentComment['commentid']; }
\r
367 function parse_body() { echo $this->highlight($this->currentComment['body']); }
\r
368 function parse_memberid() { echo $this->currentComment['memberid']; }
\r
369 function parse_timestamp() { echo $this->currentComment['timestamp']; }
\r
370 function parse_host() { echo $this->currentComment['host']; }
\r
371 function parse_ip() { echo $this->currentComment['ip']; }
\r
372 function parse_blogid() { echo $this->currentComment['blogid']; }
\r
374 function parse_user() { echo $this->currentComment['user']; }
\r
375 function parse_userid() { echo $this->currentComment['userid']; }
\r
376 function parse_userlinkraw() { echo $this->currentComment['userlinkraw']; }
\r
377 function parse_userlink() {
\r
378 if ($this->currentComment['userlinkraw'])
\r
379 echo '<a href="'.$this->currentComment['userlinkraw'].'">'.$this->currentComment['user'].'</a>';
\r
381 echo $this->currentComment['user'];
\r
383 function parse_short() {
\r
384 $tmp = strtok($this->currentComment['body'],"\n");
\r
385 $tmp = str_replace('<br />','',$tmp);
\r
387 if ($tmp != $this->currentComment['body'])
\r
388 $this->parser->parse($this->template['COMMENTS_CONTINUED']);
\r
390 function parse_authtext() {
\r
391 if ($this->currentComment['memberid'] != 0)
\r
392 $this->parser->parse($this->template['COMMENTS_AUTH']);
\r
396 * Executes a plugin templatevar
\r
398 * @param pluginName name of plugin (without the NP_)
\r
400 * extra parameters can be added
\r
402 function parse_plugin($pluginName) {
\r
405 // only continue when the plugin is really installed
\r
406 if (!$manager->pluginInstalled('NP_' . $pluginName))
\r
409 $plugin =& $manager->getPlugin('NP_' . $pluginName);
\r
410 if (!$plugin) return;
\r
413 $params = func_get_args();
\r
415 // remove plugin name
\r
416 array_shift($params);
\r
418 // pass info on current item and current comment as well
\r
419 $params = array_merge(array(&$this->currentComment),$params);
\r
420 $params = array_merge(array(&$this->commentsObj->itemActions->currentItem),$params);
\r
422 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);
\r