OSDN Git Service

CHANGE: getBlogIDFromItemID()を使わずにManager::getItem()を使うよう変更
[nucleus-jp/nucleus-next.git] / 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-2012 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 /**\r
14  * A class representing the comments (all of them) for a certain post on a ceratin blog\r
15  *\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
19  */\r
20 \r
21 if ( !function_exists('requestVar') ) exit;\r
22 require_once dirname(__FILE__) . '/COMMENTACTIONS.php';\r
23 \r
24 class Comments\r
25 {\r
26 \r
27         // item for which comment are being displayed\r
28         var $itemid;\r
29 \r
30         // reference to the itemActions object that is calling the showComments function\r
31         var $itemActions;\r
32 \r
33         // total amount of comments displayed\r
34         var $commentcount;\r
35 \r
36         /**\r
37          * Creates a new Comments object for the given blog and item\r
38          *\r
39          * @param $itemid\r
40          *              id of the item\r
41          */\r
42         function COMMENTS($itemid) {\r
43                 $this->itemid = intval($itemid);\r
44         }\r
45         \r
46         /**\r
47          * Used when parsing comments\r
48          *\r
49          * @param $itemActions\r
50          *              itemActions object, that will take care of the parsing\r
51          */\r
52         function setItemActions(&$itemActions) {\r
53                 $this->itemActions =& $itemActions;\r
54         }\r
55 \r
56         /**\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
59          *\r
60          * @param template\r
61          *              template to use\r
62          * @param maxToShow\r
63          *              max. comments to show\r
64          * @param showNone\r
65          *              indicates if the 'no comments' thingie should be outputted when there are no comments\r
66          *              (useful for closed items)\r
67          * @param highlight\r
68          *              Highlight to use (if any)\r
69          */\r
70         function showComments($template, $maxToShow = -1, $showNone = 1, $highlight = '') {\r
71                 global $CONF, $manager;\r
72 \r
73                 // create parser object & action handler\r
74                 $handler = new CommentActions($this);\r
75                 $handler->setTemplate($template);\r
76                 \r
77                 $parser = new Parser($handler);\r
78                 \r
79                 if ($maxToShow == 0) {\r
80                         $this->commentcount = $this->amountComments();\r
81                 } else {\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
86 \r
87                         $comments = DB::getResult($query);\r
88                         $this->commentcount = $comments->rowCount();\r
89                 }\r
90 \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
95                         return 0;\r
96                 }\r
97 \r
98                 // if too many comments to show\r
99                 if (($maxToShow != -1) && ($this->commentcount > $maxToShow)) {\r
100                         $parser->parse($template['COMMENTS_TOOMUCH']);\r
101                         return 0;\r
102                 }\r
103 \r
104                 $parser->parse($template['COMMENTS_HEADER']);\r
105 \r
106                 foreach ( $comments as $comment ) {\r
107                         $comment['timestamp'] = strtotime($comment['ctime']);\r
108                         $handler->setCurrentComment($comment);\r
109                         $handler->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
113                 }\r
114 \r
115                 $parser->parse($template['COMMENTS_FOOTER']);\r
116 \r
117                 $comments->closeCursor();\r
118 \r
119                 return $this->commentcount;\r
120         }\r
121 \r
122         /**\r
123          * Returns the amount of comments for this itemid\r
124          */\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 = DB::getValue($query);\r
130 \r
131                 return $res;\r
132         }\r
133 \r
134         /**\r
135          * Comments::addComment()\r
136          * Adds a new comment to the database\r
137          * \r
138          * @param string $timestamp\r
139          * @param array $comment\r
140          * @return mixed\r
141          */\r
142         function addComment($timestamp, $comment)\r
143         {\r
144                 global $CONF, $member, $manager;\r
145                 \r
146                 $item =& $manager->getItem($this->itemid, 0, 0);\r
147                 $settings =& $manager->getBlog($item['blogid']);\r
148                 $settings->readSettings();\r
149                 \r
150                 // begin if: comments disabled\r
151                 if ( !$settings->commentsEnabled() )\r
152                 {\r
153                         return _ERROR_COMMENTS_DISABLED;\r
154                 }\r
155                 \r
156                 // begin if: public cannot comment\r
157                 if ( !$settings->isPublic() && !$member->isLoggedIn() )\r
158                 {\r
159                         return _ERROR_COMMENTS_NONPUBLIC;\r
160                 }\r
161                 \r
162                 // begin if: comment uses a protected member name\r
163                 if ( $CONF['ProtectMemNames'] && !$member->isLoggedIn() && Member::isNameProtected($comment['user']) )\r
164                 {\r
165                         return _ERROR_COMMENTS_MEMBERNICK;\r
166                 }\r
167                 \r
168                 // begin if: email required, but missing (doesn't apply to members)\r
169                 if ( $settings->emailRequired() && i18n::strlen($comment['email']) == 0 && !$member->isLoggedIn() )\r
170                 {\r
171                         return _ERROR_EMAIL_REQUIRED;\r
172                 }\r
173                 \r
174                 // begin if: commenter's name is too long\r
175                 if ( i18n::strlen($comment['user']) > 40 )\r
176                 {\r
177                         return _ERROR_USER_TOO_LONG;\r
178                 }\r
179                 \r
180                 // begin if: commenter's email is too long\r
181                 if ( i18n::strlen($comment['email']) > 100 )\r
182                 {\r
183                         return _ERROR_EMAIL_TOO_LONG;\r
184                 }\r
185                 \r
186                 // begin if: commenter's url is too long\r
187                 if ( i18n::strlen($comment['userid']) > 100 )\r
188                 {\r
189                         return _ERROR_URL_TOO_LONG;\r
190                 }\r
191                 \r
192                 $comment['timestamp'] = $timestamp;\r
193                 $comment['host'] = gethostbyaddr(serverVar('REMOTE_ADDR') );\r
194                 $comment['ip'] = serverVar('REMOTE_ADDR');\r
195                 \r
196                 // begin if: member is logged in, use that data\r
197                 if ( $member->isLoggedIn() )\r
198                 {\r
199                         $comment['memberid'] = $member->getID();\r
200                         $comment['user'] = '';\r
201                         $comment['userid'] = '';\r
202                         $comment['email'] = '';\r
203                 }\r
204                 else\r
205                 {\r
206                         $comment['memberid'] = 0;\r
207                 }\r
208                 \r
209                 // spam check\r
210                 $continue = FALSE;\r
211                 $plugins = array();\r
212                 \r
213                 if ( isset($manager->subscriptions['ValidateForm']) )\r
214                 {\r
215                         $plugins = array_merge($plugins, $manager->subscriptions['ValidateForm']);\r
216                 }\r
217                 \r
218                 if ( isset($manager->subscriptions['PreAddComment']) )\r
219                 {\r
220                         $plugins = array_merge($plugins, $manager->subscriptions['PreAddComment']);\r
221                 }\r
222                 \r
223                 if ( isset($manager->subscriptions['PostAddComment']) )\r
224                 {\r
225                         $plugins = array_merge($plugins, $manager->subscriptions['PostAddComment']);\r
226                 }\r
227                 \r
228                 $plugins = array_unique($plugins);\r
229                 \r
230                 while ( list(, $plugin) = each($plugins) )\r
231                 {\r
232                         $p = $manager->getPlugin($plugin);\r
233                         $continue = $continue || $p->supportsFeature('handleSpam');\r
234                 }\r
235                 \r
236                 $spamcheck = array(\r
237                         'type'          => 'comment',\r
238                         'body'          => $comment['body'],\r
239                         'id'        => $comment['itemid'],\r
240                         'live'          => TRUE,\r
241                         'return'        => $continue\r
242                 );\r
243                 \r
244                 // begin if: member logged in\r
245                 if ( $member->isLoggedIn() )\r
246                 {\r
247                         $spamcheck['author'] = $member->displayname;\r
248                         $spamcheck['email'] = $member->email;\r
249                 }\r
250                 // else: public\r
251                 else\r
252                 {\r
253                         $spamcheck['author'] = $comment['user'];\r
254                         $spamcheck['email'] = $comment['email'];\r
255                         $spamcheck['url'] = $comment['userid'];\r
256                 }\r
257                 \r
258                 $manager->notify('SpamCheck', array('spamcheck' => &$spamcheck) );\r
259                 \r
260                 if ( !$continue && isset($spamcheck['result']) && $spamcheck['result'] == TRUE )\r
261                 {\r
262                         return _ERROR_COMMENTS_SPAM;\r
263                 }\r
264                 \r
265                 // isValidComment returns either "1" or an error message\r
266                 $isvalid = $this->isValidComment($comment, $spamcheck);\r
267                 \r
268                 if ( $isvalid != 1 )\r
269                 {\r
270                         return $isvalid;\r
271                 }\r
272                 \r
273                 // begin if: send email to notification address\r
274                 if ( $settings->getNotifyAddress() && $settings->notifyOnComment() )\r
275                 {\r
276                 \r
277                         $message = _NOTIFY_NC_MSG . ' ' . $this->itemid . "\n";\r
278                         $temp = parse_url($CONF['Self']);\r
279                         \r
280                         if ( $temp['scheme'] )\r
281                         {\r
282                                 $message .= Link::create_item_link($this->itemid) . "\n\n";\r
283                         }\r
284                         else\r
285                         {\r
286                                 $tempurl = $settings->getURL();\r
287                                 \r
288                                 if ( i18n::substr($tempurl, -1) == '/' || i18n::substr($tempurl, -4) == '.php' )\r
289                                 {\r
290                                         $message .= $tempurl . '?itemid=' . $this->itemid . "\n\n";\r
291                                 }\r
292                                 else\r
293                                 {\r
294                                         $message .= $tempurl . '/?itemid=' . $this->itemid . "\n\n";\r
295                                 }\r
296                         }\r
297                         \r
298                         if ( $comment['memberid'] == 0 )\r
299                         {\r
300                                 $message .= _NOTIFY_USER . ' ' . $comment['user'] . "\n";\r
301                                 $message .= _NOTIFY_USERID . ' ' . $comment['userid'] . "\n";\r
302                         }\r
303                         else\r
304                         {\r
305                                 $message .= _NOTIFY_MEMBER .' ' . $member->getDisplayName() . ' (ID=' . $member->getID() . ")\n";\r
306                         }\r
307                         \r
308                         $message .= _NOTIFY_HOST . ' ' . $comment['host'] . "\n";\r
309                         $message .= _NOTIFY_COMMENT . "\n " . $comment['body'] . "\n";\r
310                         $message .= NOTIFICATION::get_mail_footer();\r
311                         \r
312                         $subject = _NOTIFY_NC_TITLE . ' ' . strip_tags($item['title']) . ' (' . $this->itemid . ')';\r
313                         \r
314                         $from = $member->getNotifyFromMailAddress($comment['email']);\r
315                         \r
316                         NOTIFICATION::mail($settings->getNotifyAddress(), $subject, $message, $from, i18n::get_current_charset());\r
317                 }\r
318                 \r
319                 $comment = Comment::prepare($comment);\r
320                 \r
321                 $manager->notify('PreAddComment', array('comment' => &$comment, 'spamcheck' => &$spamcheck) );\r
322                 \r
323                 $name           = DB::quoteValue($comment['user']);\r
324                 $url            = DB::quoteValue($comment['userid']);\r
325                 $email          = DB::quoteValue($comment['email']);\r
326                 $body           = DB::quoteValue($comment['body']);\r
327                 $host           = DB::quoteValue($comment['host']);\r
328                 $ip                     = DB::quoteValue($comment['ip']);\r
329                 $memberid       = (integer) $comment['memberid'];\r
330                 $timestamp      = DB::formatDateTime($comment['timestamp']);\r
331                 $itemid         = $this->itemid;\r
332                 \r
333                 $qSql       = 'SELECT COUNT(*) AS result '\r
334                                         . 'FROM ' . sql_table('comment')\r
335                                         . ' WHERE '\r
336                                         .      'cmail   = ' . $url\r
337                                         . ' AND cmember = ' . $memberid\r
338                                         . ' AND cbody   = ' . $body\r
339                                         . ' AND citem   = ' . $itemid\r
340                                         . ' AND cblog   = ' . $item['blogid'];\r
341                 $result     = (integer) DB::getValue($qSql);\r
342                 \r
343                 if ( $result > 0 )\r
344                 {\r
345                         return _ERROR_BADACTION;\r
346                 }\r
347                 \r
348                 $query = sprintf('INSERT INTO %s (cuser, cmail, cemail, cmember, cbody, citem, ctime, chost, cip, cblog) '\r
349                         . 'VALUES (%s, %s, %s, %d, %s, %d, %s, %s, %s, %d)'\r
350                         , sql_table('comment'), $name, $url, $email, $memberid, $body, $itemid, $timestamp, $host, $ip, $item['blogid']);\r
351                 \r
352                 DB::execute($query);\r
353                 \r
354                 // post add comment\r
355                 $commentid = DB::getInsertId();\r
356                 $manager->notify('PostAddComment', array('comment' => &$comment, 'commentid' => &$commentid, 'spamcheck' => &$spamcheck) );\r
357                 \r
358                 // succeeded !\r
359                 return TRUE;\r
360         }\r
361 \r
362 \r
363         /**\r
364          * Comments::isValidComment()\r
365          * Checks if a comment is valid and call plugins\r
366          * that can check if the comment is a spam comment        \r
367          * \r
368          * @param       Array   $comment        array with comment elements\r
369          * @param       Array   $spamcheck      array with spamcheck elements\r
370          */\r
371         function isValidComment(&$comment, &$spamcheck)\r
372         {\r
373                 global $member, $manager;\r
374                 \r
375                 // check if there exists a item for this date\r
376                 $item =& $manager->getItem($this->itemid, 0, 0);\r
377                 \r
378                 if ( !$item )\r
379                 {\r
380                         return _ERROR_NOSUCHITEM;\r
381                 }\r
382                 \r
383                 if ( $item['closed'] )\r
384                 {\r
385                         return _ERROR_ITEMCLOSED;\r
386                 }\r
387                 \r
388                 // don't allow words that are too long\r
389                 if ( preg_match('/[a-zA-Z0-9|\.,;:!\?=\/\\\\]{90,90}/', $comment['body']) != 0 )\r
390                 {\r
391                         return _ERROR_COMMENT_LONGWORD;\r
392                 }\r
393                 \r
394                 // check lengths of comment\r
395                 if ( i18n::strlen($comment['body']) < 3 )\r
396                 {\r
397                         return _ERROR_COMMENT_NOCOMMENT;\r
398                 }\r
399                 \r
400                 if ( i18n::strlen($comment['body']) > 5000 )\r
401                 {\r
402                         return _ERROR_COMMENT_TOOLONG;\r
403                 }\r
404                 \r
405                 // only check username if no member logged in\r
406                 if ( !$member->isLoggedIn() && (i18n::strlen($comment['user']) < 2) )\r
407                 {\r
408                         return _ERROR_COMMENT_NOUSERNAME;\r
409                 }\r
410                 \r
411                 if ( (i18n::strlen($comment['email']) != 0) && !NOTIFICATION::address_validation(trim($comment['email'])) )\r
412                 {\r
413                         return _ERROR_BADMAILADDRESS;\r
414                 }\r
415                 \r
416                 // let plugins do verification (any plugin which thinks the comment is invalid\r
417                 // can change 'error' to something other than '1')\r
418                 $result = 1;\r
419                 $manager->notify('ValidateForm', array('type' => 'comment', 'comment' => &$comment, 'error' => &$result, 'spamcheck' => &$spamcheck) );\r
420                 \r
421                 return $result;\r
422         }\r
423 }\r