OSDN Git Service

merged 3.2 original code
[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-2005 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   * $Id: COMMENTS.php,v 1.5 2005-03-12 06:19:05 kimitake Exp $\r
16   * $NucleusJP$\r
17   */\r
18 class COMMENTS {\r
19 \r
20         // item for which comment are being displayed\r
21         var $itemid;\r
22 \r
23         // reference to the itemActions object that is calling the showComments function\r
24         var $itemActions;\r
25 \r
26         // total amount of comments displayed\r
27         var $commentcount;\r
28 \r
29         /**\r
30          * Creates a new COMMENTS object for the given blog and item\r
31          *\r
32          * @param $itemid\r
33          *              id of the item\r
34          */\r
35         function COMMENTS($itemid) {\r
36                 $this->itemid = intval($itemid);\r
37         }\r
38         /**\r
39          * Used when parsing comments\r
40          *\r
41          * @param $itemActions\r
42          *              itemActions object, that will take care of the parsing\r
43          */\r
44         function setItemActions(&$itemActions) {\r
45                 $this->itemActions =& $itemActions;\r
46         }\r
47 \r
48         /**\r
49          * Shows maximum $max comments to the given item using the given template\r
50          * returns the amount of shown comments (if maxToShow = -1, then there is no limit)\r
51          *\r
52          * @param template\r
53          *              template to use\r
54          * @param maxToShow\r
55          *              max. comments to show\r
56          * @param showNone\r
57          *              indicates if the 'no comments' thingie should be outputted when there are no comments\r
58          *              (useful for closed items)\r
59          * @param highlight\r
60          *              Highlight to use (if any)\r
61          */\r
62         function showComments($template, $maxToShow = -1, $showNone = 1, $highlight = '') {\r
63                 global $CONF, $manager;\r
64 \r
65                 // create parser object & action handler\r
66                 $actions =& new COMMENTACTIONS($this);\r
67                 $parser =& new PARSER($actions->getDefinedActions(),$actions);\r
68                 $actions->setTemplate($template);\r
69                 $actions->setParser($parser);\r
70 \r
71                 if ($maxToShow == 0) {\r
72                         $this->commentcount = $this->amountComments();\r
73                 } else {\r
74                         $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
75                                    . ' FROM '.sql_table('comment').' as c'\r
76                                    . ' WHERE c.citem=' . $this->itemid\r
77                                    . ' ORDER BY c.ctime';\r
78 \r
79                         $comments = sql_query($query);\r
80                         $this->commentcount = mysql_num_rows($comments);\r
81                 }\r
82 \r
83                 // if no result was found\r
84                 if ($this->commentcount == 0) {\r
85                         // note: when no reactions, COMMENTS_HEADER and COMMENTS_FOOTER are _NOT_ used\r
86                         if ($showNone) $parser->parse($template['COMMENTS_NONE']);\r
87                         return 0;\r
88                 }\r
89 \r
90                 // if too many comments to show\r
91                 if (($maxToShow != -1) && ($this->commentcount > $maxToShow)) {\r
92                         $parser->parse($template['COMMENTS_TOOMUCH']);\r
93                         return 0;\r
94                 }\r
95 \r
96                 $parser->parse($template['COMMENTS_HEADER']);\r
97 \r
98                 while ( $comment = mysql_fetch_assoc($comments) ) {\r
99                         $comment['timestamp'] = strtotime($comment['ctime']);\r
100                         $actions->setCurrentComment($comment);\r
101                         $actions->setHighlight($highlight);\r
102                         $manager->notify('PreComment', array('comment' => &$comment));\r
103                         $parser->parse($template['COMMENTS_BODY']);\r
104                         $manager->notify('PostComment', array('comment' => &$comment));\r
105                 }\r
106 \r
107                 $parser->parse($template['COMMENTS_FOOTER']);\r
108 \r
109                 mysql_free_result($comments);\r
110 \r
111                 return $this->commentcount;\r
112         }\r
113 \r
114         /**\r
115          * Returns the amount of comments for this itemid\r
116          */\r
117         function amountComments() {\r
118                 $query =  'SELECT COUNT(*)'\r
119                            . ' FROM '.sql_table('comment').' as c'\r
120                            . ' WHERE c.citem='. $this->itemid;\r
121                 $res = sql_query($query);\r
122                 $arr = mysql_fetch_row($res);\r
123 \r
124                 return $arr[0];\r
125         }\r
126 \r
127 \r
128         function addComment($timestamp, $comment) {\r
129                 global $CONF, $member, $manager;\r
130 \r
131                 $blogid = getBlogIDFromItemID($this->itemid);\r
132 \r
133                 $settings =& $manager->getBlog($blogid);\r
134                 $settings->readSettings();\r
135 \r
136                 if (!$settings->commentsEnabled())\r
137                         return _ERROR_COMMENTS_DISABLED;\r
138 \r
139                 if (!$settings->isPublic() && !$member->isLoggedIn())\r
140                         return _ERROR_COMMENTS_NONPUBLIC;\r
141 \r
142                 // member name protection\r
143                 if ($CONF['ProtectMemNames'] && !$member->isLoggedIn() && MEMBER::isNameProtected($comment['user']))\r
144                         return _ERROR_COMMENTS_MEMBERNICK;\r
145 \r
146                 // isValidComment returns either "1" or an error message\r
147                 $isvalid = $this->isValidComment($comment);\r
148                 if ($isvalid != 1)\r
149                         return $isvalid;\r
150 \r
151                 $comment['timestamp'] = $timestamp;\r
152                 $comment['host'] = gethostbyaddr(serverVar('REMOTE_ADDR'));\r
153                 $comment['ip'] = serverVar('REMOTE_ADDR');\r
154 \r
155                 // if member is logged in, use that data\r
156                 if ($member->isLoggedIn()) {\r
157                         $comment['memberid'] = $member->getID();\r
158                         $comment['user'] = '';\r
159                         $comment['userid'] = '';\r
160                 } else {\r
161                         $comment['memberid'] = 0;\r
162                 }\r
163 \r
164 \r
165                 // send email to notification address, if any\r
166                 if ($settings->getNotifyAddress() && $settings->notifyOnComment()) {\r
167 \r
168                         $mailto_msg = _NOTIFY_NC_MSG . ' ' . $this->itemid . "\n";\r
169                         $mailto_msg .= $CONF['IndexURL'] . 'index.php?itemid=' . $this->itemid . "\n\n";\r
170                         if ($comment['memberid'] == 0) {\r
171                                 $mailto_msg .= _NOTIFY_USER . ' ' . $comment['user'] . "\n";\r
172                                 $mailto_msg .= _NOTIFY_USERID . ' ' . $comment['userid'] . "\n";\r
173                         } else {\r
174                                 $mailto_msg .= _NOTIFY_MEMBER .' ' . $member->getDisplayName() . ' (ID=' . $member->getID() . ")\n";\r
175                         }\r
176                         $mailto_msg .= _NOTIFY_HOST . ' ' . $comment['host'] . "\n";\r
177                         $mailto_msg .= _NOTIFY_COMMENT . "\n " . $comment['body'] . "\n";\r
178                         $mailto_msg .= getMailFooter();\r
179 \r
180                         $item =& $manager->getItem($this->itemid, 0, 0);\r
181                         $mailto_title = _NOTIFY_NC_TITLE . ' ' . strip_tags($item['title']) . ' (' . $this->itemid . ')';\r
182 \r
183                         $frommail = $member->getNotifyFromMailAddress($comment['userid']);\r
184 \r
185                         $notify =& new NOTIFICATION($settings->getNotifyAddress());\r
186                         $notify->notify($mailto_title, $mailto_msg , $frommail);\r
187                 }\r
188 \r
189                 $comment = COMMENT::prepare($comment);\r
190 \r
191                 $manager->notify('PreAddComment',array('comment' => &$comment));\r
192 \r
193                 $name           = addslashes($comment['user']);\r
194                 $url            = addslashes($comment['userid']);\r
195                 $body           = addslashes($comment['body']);\r
196                 $host           = addslashes($comment['host']);\r
197                 $ip                     = addslashes($comment['ip']);\r
198                 $memberid       = intval($comment['memberid']);\r
199                 $timestamp      = date('Y-m-d H:i:s', $comment['timestamp']);\r
200                 $itemid         = $this->itemid;\r
201 \r
202                 $query = 'INSERT INTO '.sql_table('comment').' (CUSER, CMAIL, CMEMBER, CBODY, CITEM, CTIME, CHOST, CIP, CBLOG) '\r
203                            . "VALUES ('$name', '$url', $memberid, '$body', $itemid, '$timestamp', '$host', '$ip', '$blogid')";\r
204 \r
205                 sql_query($query);\r
206 \r
207                 // post add comment\r
208                 $commentid = mysql_insert_id();\r
209                 $manager->notify('PostAddComment',array('comment' => &$comment, 'commentid' => &$commentid));\r
210 \r
211                 // succeeded !\r
212                 return true;\r
213         }\r
214 \r
215 \r
216         function isValidComment($comment) {\r
217                 global $member, $manager;\r
218 \r
219                 // check if there exists a item for this date\r
220                 $item =& $manager->getItem($this->itemid,0,0);\r
221 \r
222                 if (!$item)\r
223                         return _ERROR_NOSUCHITEM;\r
224 \r
225                 if ($item['closed'])\r
226                         return _ERROR_ITEMCLOSED;\r
227 \r
228                 // don't allow words that are too long\r
229                 if (eregi('[a-zA-Z0-9|\.,;:!\?=\/\\]{90,90}',$comment['body']) != false)\r
230                         return _ERROR_COMMENT_LONGWORD;\r
231 \r
232                 // check lengths of comment\r
233                 if (strlen($comment['body'])<3)\r
234                         return _ERROR_COMMENT_NOCOMMENT;\r
235 \r
236                 if (strlen($comment['body'])>5000)\r
237                         return _ERROR_COMMENT_TOOLONG;\r
238 \r
239                 // only check username if no member logged in\r
240                 if (!$member->isLoggedIn())\r
241                         if (strlen($comment['user'])<2)\r
242                                 return _ERROR_COMMENT_NOUSERNAME;\r
243 \r
244                 // let plugins do verification (any plugin which thinks the comment is invalid\r
245                 // can change 'error' to something other than '1')\r
246                 $result = 1;\r
247                 $manager->notify('ValidateForm', array('type' => 'comment', 'comment' => &$comment, 'error' => &$result));\r
248 \r
249                 return $result;\r
250         }\r
251 \r
252 \r
253 }\r
254 \r
255 /**\r
256   * This class is used when parsing comment templates\r
257   */\r
258 class COMMENTACTIONS extends BaseActions {\r
259 \r
260         // ref to COMMENTS object which is using this object to handle\r
261         // its templatevars\r
262         var $commentsObj;\r
263 \r
264         // template to use to parse the comments\r
265         var $template;\r
266 \r
267         // comment currenlty being handled (mysql result assoc array; see COMMENTS::showComments())\r
268         var $currentComment;\r
269 \r
270         function COMMENTACTIONS(&$comments) {\r
271                 // call constructor of superclass first\r
272                 $this->BaseActions();\r
273 \r
274                 // reference to the comments object\r
275                 $this->setCommentsObj($comments);\r
276         }\r
277 \r
278         function getDefinedActions() {\r
279                 return array(\r
280                         'commentcount',\r
281                         'commentword',\r
282                         'itemlink',\r
283                         'itemid',\r
284                         'itemtitle',\r
285                         'date',\r
286                         'time',\r
287                         'commentid',\r
288                         'body',\r
289                         'memberid',\r
290                         'timestamp',\r
291                         'host',\r
292                         'ip',\r
293                         'blogid',\r
294                         'authtext',\r
295                         'user',\r
296                         'userid',\r
297                         'userlinkraw',\r
298                         'userlink',\r
299                         'short',\r
300                         'skinfile',\r
301                         'set',\r
302                         'plugin',\r
303                         'include',\r
304                         'phpinclude',\r
305                         'parsedinclude'\r
306                 );\r
307         }\r
308 \r
309         function setParser(&$parser) {                  $this->parser =& $parser; }\r
310         function setCommentsObj(&$commentsObj) {$this->commentsObj =& $commentsObj; }\r
311         function setTemplate($template) {               $this->template =& $template; }\r
312         function setCurrentComment(&$comment) {\r
313                 if ($comment['memberid'] != 0) {\r
314                         $comment['authtext'] = $template['COMMENTS_AUTH'];\r
315 \r
316                         $mem = MEMBER::createFromID($comment['memberid']);\r
317                         $comment['user'] = $mem->getDisplayName();\r
318                         if ($mem->getURL())\r
319                                 $comment['userid'] = $mem->getURL();\r
320                         else\r
321                                 $comment['userid'] = $mem->getEmail();\r
322 \r
323                         $comment['userlinkraw'] = createMemberLink(\r
324                                                                                 $comment['memberid'],\r
325                                                                                 $this->commentsObj->itemActions->linkparams\r
326                                                                           );\r
327 \r
328                 } else {\r
329 \r
330                         // create smart links\r
331                         if (isValidMailAddress($comment['userid']))\r
332                                 $comment['userlinkraw'] = 'mailto:'.$comment['userid'];\r
333                         elseif (strstr($comment['userid'],'http://') != false)\r
334                                 $comment['userlinkraw'] = $comment['userid'];\r
335                         elseif (strstr($comment['userid'],'www') != false)\r
336                                 $comment['userlinkraw'] = 'http://'.$comment['userid'];\r
337                 }\r
338 \r
339                 $this->currentComment =& $comment;\r
340         }\r
341 \r
342         function parse_commentcount() {                 echo $this->commentsObj->commentcount; }\r
343         function parse_commentword() {\r
344                 if ($this->commentsObj->commentcount == 1)\r
345                         echo $this->template['COMMENTS_ONE'];\r
346                 else\r
347                         echo $this->template['COMMENTS_MANY'];\r
348         }\r
349 \r
350         function parse_itemlink() {                             echo createItemLink($this->commentsObj->itemid, $this->commentsObj->itemActions->linkparams); }\r
351         function parse_itemid() {                               echo $this->commentsObj->itemid; }\r
352         function parse_itemtitle($maxLength = 0) {\r
353                 if ($maxLength == 0)\r
354                         $this->commentsObj->itemActions->parse_title();\r
355                 else\r
356                         $this->commentsObj->itemActions->parse_syndicate_title($maxLength);\r
357         }\r
358 \r
359         function parse_date($format = '') {\r
360                 echo formatDate($format, $this->currentComment['timestamp'], $this->template['FORMAT_DATE']);\r
361         }\r
362         function parse_time($format = '') {\r
363                 echo strftime(\r
364                                 ($format == '') ? $this->template['FORMAT_TIME'] : $format,\r
365                                 $this->currentComment['timestamp']\r
366                         );\r
367         }\r
368 \r
369         function parse_commentid() {                    echo $this->currentComment['commentid']; }\r
370         function parse_body() {                                 echo $this->highlight($this->currentComment['body']); }\r
371         function parse_memberid() {                             echo $this->currentComment['memberid']; }\r
372         function parse_timestamp() {                    echo $this->currentComment['timestamp']; }\r
373         function parse_host() {                                 echo $this->currentComment['host']; }\r
374         function parse_ip() {                                   echo $this->currentComment['ip']; }\r
375         function parse_blogid() {                               echo $this->currentComment['blogid']; }\r
376 \r
377         function parse_user() {                                 echo $this->currentComment['user']; }\r
378         function parse_userid() {                               echo $this->currentComment['userid']; }\r
379         function parse_userlinkraw() {                  echo $this->currentComment['userlinkraw']; }\r
380         function parse_userlink() {\r
381                 if ($this->currentComment['userlinkraw']) {\r
382                         echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';\r
383                 } else {\r
384                         echo $this->currentComment['user'];\r
385                 }\r
386         }\r
387         function parse_short() {\r
388                 $tmp = strtok($this->currentComment['body'],"\n");\r
389                 $tmp = str_replace('<br />','',$tmp);\r
390                 echo $tmp;\r
391                 if ($tmp != $this->currentComment['body'])\r
392                         $this->parser->parse($this->template['COMMENTS_CONTINUED']);\r
393         }\r
394         function parse_authtext() {\r
395                 if ($this->currentComment['memberid'] != 0)\r
396                         $this->parser->parse($this->template['COMMENTS_AUTH']);\r
397         }\r
398 \r
399         /**\r
400           * Executes a plugin templatevar\r
401           *\r
402           * @param pluginName name of plugin (without the NP_)\r
403           *\r
404           * extra parameters can be added\r
405           */\r
406         function parse_plugin($pluginName) {\r
407                 global $manager;\r
408 \r
409                 // only continue when the plugin is really installed\r
410                 if (!$manager->pluginInstalled('NP_' . $pluginName))\r
411                         return;\r
412 \r
413                 $plugin =& $manager->getPlugin('NP_' . $pluginName);\r
414                 if (!$plugin) return;\r
415 \r
416                 // get arguments\r
417                 $params = func_get_args();\r
418 \r
419                 // remove plugin name\r
420                 array_shift($params);\r
421 \r
422                 // pass info on current item and current comment as well\r
423                 $params = array_merge(array(&$this->currentComment),$params);\r
424                 $params = array_merge(array(&$this->commentsObj->itemActions->currentItem),$params);\r
425 \r
426                 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);\r
427         }\r
428 }\r
429 \r
430 ?>\r