OSDN Git Service

use the original code for parse_short
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / COMMENTS.php
1 <?php\r
2 \r
3 /**\r
4   * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) \r
5   * Copyright (C) 2002-2004 The Nucleus Group\r
6   *\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
12   *\r
13   * A class representing the comments (all of them) for a certain post on a ceratin blog\r
14   */\r
15 class COMMENTS {\r
16 \r
17         // item for which comment are being displayed\r
18         var $itemid;\r
19         \r
20         // reference to the itemActions object that is calling the showComments function\r
21         var $itemActions;\r
22         \r
23         // total amount of comments displayed\r
24         var $commentcount;\r
25         \r
26         /**\r
27          * Creates a new COMMENTS object for the given blog and item\r
28          *\r
29          * @param $itemid \r
30          *              id of the item\r
31          */\r
32         function COMMENTS($itemid) {\r
33                 $this->itemid = intval($itemid);        \r
34         }\r
35         /**\r
36          * Used when parsing comments\r
37          *\r
38          * @param $itemActions\r
39          *              itemActions object, that will take care of the parsing\r
40          */\r
41         function setItemActions(&$itemActions) {\r
42                 $this->itemActions =& $itemActions;\r
43         }\r
44         \r
45         /**\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
48          *\r
49          * @param template\r
50          *              template to use\r
51          * @param maxToShow\r
52          *              max. comments to show\r
53          * @param showNone\r
54          *              indicates if the 'no comments' thingie should be outputted when there are no comments\r
55          *              (useful for closed items)\r
56          * @param highlight\r
57          *              Highlight to use (if any)\r
58          */\r
59         function showComments($template, $maxToShow = -1, $showNone = 1, $highlight = '') {\r
60                 global $CONF, $manager;\r
61 \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
67                 \r
68                 if ($maxToShow == 0) {\r
69                         $this->commentcount = $this->amountComments();\r
70                 } else {\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
75                                \r
76                         $comments = sql_query($query);          \r
77                         $this->commentcount = mysql_num_rows($comments);\r
78                 }\r
79 \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
84                         return 0;\r
85                 }\r
86                 \r
87                 // if too many comments to show\r
88                 if (($maxToShow != -1) && ($this->commentcount > $maxToShow)) {\r
89                         $parser->parse($template['COMMENTS_TOOMUCH']);\r
90                         return 0;\r
91                 }\r
92                 \r
93                 $parser->parse($template['COMMENTS_HEADER']);\r
94                 \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
102                 }\r
103 \r
104                 $parser->parse($template['COMMENTS_FOOTER']);\r
105                 \r
106                 mysql_free_result($comments);\r
107                                 \r
108                 return $this->commentcount;\r
109         }\r
110          \r
111         /**\r
112          * Returns the amount of comments for this itemid\r
113          */\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
120                 \r
121                 return $arr[0];\r
122         }\r
123         \r
124 \r
125         function addComment($timestamp, $comment) {\r
126                 global $CONF, $member, $manager;\r
127                 \r
128                 $blogid = getBlogIDFromItemID($this->itemid);\r
129 \r
130                 $settings =& $manager->getBlog($blogid);\r
131                 $settings->readSettings();\r
132                 \r
133                 if (!$settings->commentsEnabled())\r
134                         return _ERROR_COMMENTS_DISABLED;\r
135                         \r
136                 if (!$settings->isPublic() && !$member->isLoggedIn())\r
137                         return _ERROR_COMMENTS_NONPUBLIC;\r
138 \r
139                 // member name protection\r
140                 if ($CONF['ProtectMemNames'] && !$member->isLoggedIn() && MEMBER::isNameProtected($comment['user']))\r
141                         return _ERROR_COMMENTS_MEMBERNICK;\r
142 \r
143                 // isValidComment returns either "1" or an error message\r
144                 $isvalid = $this->isValidComment($comment);\r
145                 if ($isvalid != 1)\r
146                         return $isvalid;\r
147 \r
148                 $comment['timestamp'] = $timestamp;\r
149                 $comment['host'] = gethostbyaddr(serverVar('REMOTE_ADDR'));     \r
150                 $comment['ip'] = serverVar('REMOTE_ADDR');\r
151 \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
157                 } else {\r
158                         $comment['memberid'] = 0;\r
159                 }\r
160                 \r
161                                 \r
162                 // send email to notification address, if any\r
163                 if ($settings->getNotifyAddress() && $settings->notifyOnComment()) {\r
164                 \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
170                         } else {\r
171                                 $mailto_msg .= _NOTIFY_MEMBER .' ' . $member->getDisplayName() . ' (ID=' . $member->getID() . ")\n";\r
172                         }\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
176                         \r
177                         $item =& $manager->getItem($this->itemid, 0, 0);\r
178                         $mailto_title = _NOTIFY_NC_TITLE . ' ' . strip_tags($item['title']) . ' (' . $this->itemid . ')';\r
179                         \r
180                         $frommail = $member->getNotifyFromMailAddress($comment['userid']);\r
181 \r
182                         $notify = new NOTIFICATION($settings->getNotifyAddress());\r
183                         $notify->notify($mailto_title, $mailto_msg , $frommail);\r
184                 }\r
185 \r
186                 $comment = COMMENT::prepare($comment);\r
187                 \r
188                 $manager->notify('PreAddComment',array('comment' => &$comment));                \r
189 \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
198                                 \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
201 \r
202                 sql_query($query);\r
203         \r
204                 // post add comment\r
205                 $commentid = mysql_insert_id();\r
206                 $manager->notify('PostAddComment',array('comment' => &$comment, 'commentid' => &$commentid));\r
207 \r
208                 // succeeded !\r
209                 return true;\r
210         }\r
211         \r
212 \r
213         function isValidComment($comment) {\r
214                 global $member, $manager;\r
215 \r
216                 // check if there exists a item for this date\r
217                 $item =& $manager->getItem($this->itemid,0,0);\r
218 \r
219                 if (!$item)\r
220                         return _ERROR_NOSUCHITEM;\r
221 \r
222                 if ($item['closed'])\r
223                         return _ERROR_ITEMCLOSED;\r
224 \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
228 \r
229                 // check lengths of comment\r
230                 if (strlen($comment['body'])<3)\r
231                         return _ERROR_COMMENT_NOCOMMENT;\r
232 \r
233                 if (strlen($comment['body'])>5000)\r
234                         return _ERROR_COMMENT_TOOLONG;\r
235                         \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
240 \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
243                 $result = 1;\r
244                 $manager->notify('ValidateForm', array('type' => 'comment', 'comment' => &$comment, 'error' => &$result));\r
245 \r
246                 return $result; \r
247         }       \r
248 \r
249         \r
250 }\r
251 \r
252 /**\r
253   * This class is used when parsing comment templates\r
254   */\r
255 class COMMENTACTIONS extends BaseActions {\r
256 \r
257         // ref to COMMENTS object which is using this object to handle\r
258         // its templatevars\r
259         var $commentsObj;\r
260         \r
261         // template to use to parse the comments\r
262         var $template;\r
263         \r
264         // comment currenlty being handled (mysql result assoc array; see COMMENTS::showComments())\r
265         var $currentComment;\r
266 \r
267         function COMMENTACTIONS(&$comments) {\r
268                 // call constructor of superclass first\r
269                 $this->BaseActions();   \r
270                 \r
271                 // reference to the comments object\r
272                 $this->setCommentsObj($comments);\r
273         }\r
274 \r
275         function getDefinedActions() {\r
276                 return array(\r
277                         'commentcount',\r
278                         'commentword',\r
279                         'itemlink',\r
280                         'itemid',\r
281                         'itemtitle',    \r
282                         'date',\r
283                         'time',\r
284                         'commentid',\r
285                         'body',\r
286                         'memberid',\r
287                         'timestamp',\r
288                         'host',\r
289                         'ip',\r
290                         'blogid',\r
291                         'authtext',\r
292                         'user',\r
293                         'userid',\r
294                         'userlinkraw',\r
295                         'userlink',\r
296                         'short',\r
297                         'skinfile',\r
298                         'set',\r
299                         'plugin',\r
300                         'include',\r
301                         'phpinclude',\r
302                         'parsedinclude'\r
303                 );\r
304         }\r
305         \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
312 \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
317                         else\r
318                                 $comment['userid'] = $mem->getEmail();\r
319 \r
320                         $comment['userlinkraw'] = createMemberLink(\r
321                                                                                 $comment['memberid'], \r
322                                                                                 $this->commentsObj->itemActions->linkparams\r
323                                                                           );\r
324 \r
325                 } else {\r
326 \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
334                 }\r
335         \r
336                 $this->currentComment =& $comment; \r
337         }\r
338 \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
343                 else\r
344                         echo $this->template['COMMENTS_MANY']; \r
345         }       \r
346         \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
352                 else\r
353                         $this->commentsObj->itemActions->parse_syndicate_title($maxLength);             \r
354         }\r
355 \r
356         function parse_date($format = '') {                                     \r
357                 echo formatDate($format, $this->currentComment['timestamp'], $this->template['FORMAT_DATE']);\r
358         }\r
359         function parse_time($format = '') {                                     \r
360                 echo strftime(\r
361                                 ($format == '') ? $this->template['FORMAT_TIME'] : $format,\r
362                                 $this->currentComment['timestamp']\r
363                         );\r
364         }\r
365 \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
373 \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
380                 else \r
381                         echo $this->currentComment['user'];\r
382         }\r
383         function parse_short() {\r
384                 $tmp = strtok($this->currentComment['body'],"\n");\r
385                 $tmp = str_replace('<br />','',$tmp);\r
386                 echo $tmp;\r
387                 if ($tmp != $this->currentComment['body'])\r
388                         $this->parser->parse($this->template['COMMENTS_CONTINUED']); \r
389         }\r
390         function parse_authtext() {\r
391                 if ($this->currentComment['memberid'] != 0) \r
392                         $this->parser->parse($this->template['COMMENTS_AUTH']);\r
393         }\r
394         \r
395         /**\r
396           * Executes a plugin templatevar\r
397           *\r
398           * @param pluginName name of plugin (without the NP_)\r
399           * \r
400           * extra parameters can be added\r
401           */\r
402         function parse_plugin($pluginName) {\r
403                 global $manager;\r
404                 \r
405                 // only continue when the plugin is really installed\r
406                 if (!$manager->pluginInstalled('NP_' . $pluginName))\r
407                         return;\r
408                 \r
409                 $plugin =& $manager->getPlugin('NP_' . $pluginName);\r
410                 if (!$plugin) return;\r
411 \r
412                 // get arguments\r
413                 $params = func_get_args();\r
414                 \r
415                 // remove plugin name \r
416                 array_shift($params);\r
417                 \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
421 \r
422                 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);\r
423         }       \r
424 }\r
425 \r
426 ?>\r