OSDN Git Service

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