OSDN Git Service

dcfc183eb2ad9376140276e5fd0ccd15b3fc0469
[nucleus-jp/nucleus-plugins.git] / trunk / NP_EzComment2 / NP_EzComment2.php
1 <?php
2
3 class NP_EzComment2 extends NucleusPlugin
4 {
5         var $loggedin = false;
6
7         function getName()
8         {
9                 return 'Ez Comment II';
10         }
11
12         function getAuthor()
13         {
14                 return 'shizuki';
15         }
16
17         function getURL()
18         {
19                 return 'http://japan.nucleuscms.org/wiki/plugins:ezcomment2';
20         }
21
22         function getVersion()
23         {
24                 return '1.0';
25         }
26
27         function getDescription()
28         {
29                 return  _NP_EZCOMMENT2_DESC;
30         }
31
32         function supportsFeature($what)
33         {
34                 switch ($what) {
35                         case 'SqlTablePrefix':
36                                 return 1;
37                         default:
38                                 return 0;
39                 }
40         }
41
42         function getEventList()
43         {
44                 global $manager;
45                 return array(
46                         'ExternalAuth'
47                 );
48         }
49
50         function event_ExternalAuth(&$data)
51         {
52                 global $manager;
53                 $authPlug =& $manager->getPlugin('NP_' . $data['externalauth']['source']);
54                 if( $authPlug->isLoggedin() ){
55                         $data['externalauth']['result'] = true;
56                         $data['externalauth']['plugin'] = $this->getName();
57                 }
58         }
59
60         function install()
61         {
62                 if (!TEMPLATE::exists('EzCommentTemplateDefault')) {
63                         global $DIR_LIBS;
64                         include_once($DIR_LIBS . 'skinie.php');
65                         $importer = new SKINIMPORT();
66                         $importer->reset();
67                         $template = $this->getDirectory() . 'skinbackup.xml';
68                         if (!@file_exists($template)) {
69                                 $aErrors[] = 'Unable to import ' . $template . ' : file does not exist';
70                                 continue;
71                         }
72                         $error = $importer->readFile($template);
73                         if ($error) {
74                                 $aErrors[] = 'Unable to import ' . $template . ' : ' . $error;
75                                 continue;
76                         }
77                         $error = $importer->writeToDatabase(1);
78                         if ($error) {
79                                 $aErrors[] = 'Unable to import ' . $template . ' : ' . $error;
80                                 continue;
81                         }
82                         if ($aErrors) {
83                                 $message = implode("<br />\n", $aErrors);
84                                 doError($message);
85                         }
86                         createBlogOption('secret', 'Enable seacret comment ?', 'yesno', 'no');
87                         createOption('tabledel', 'Database table drop when uninstall ?', 'yesno', 'no');
88                         $sql = 'CREATE TABLE IF NOT EXISTS %s ('
89                                  . 'comid  int(11) NOT NULL,'
90                                  . 'secflg int(11) NOT NULL,'
91                                  . 'PRIMARY KEY(comid) );';
92                         sql_query(sprintf($sql, sql_table('plug_ezcomment2')));
93                 }
94         }
95
96         function uninstall()
97         {
98                 if ($this->getOption('tabledel') == 'yes')
99                         sql_query('DROP TABLE '.sql_table('plug_ezcomment2'));
100         }
101
102         function init()
103         {
104                 $language = ereg_replace( '[\\|/]', '', getLanguageName());
105                 if (file_exists($this->getDirectory() . 'language/' . $language . '.php')) {
106                         include_once($this->getDirectory() . 'language/' . $language . '.php');
107                 } else {
108                         include_once($this->getDirectory() . 'language/english.php');
109                 }
110         }
111
112
113         function doTemplateVar(&$item,
114                                                         $showType       = '',
115                                                         $showMode       = '5/1/1',
116                                                         $destinationurl = '',
117                                                         $formTemplate   = 'EzCommentTemplateDefault',
118                                                         $listTemplate   = 'EzCommentTemplateDefault')
119         {
120                 $this->doSkinVar('template', $showType, $showMode, $destinationurl, $formTemplate, $listTemplate, $item);
121         }
122
123         function doSkinVar($skinType,
124                                            $showType       = '',
125                                            $showMode       = '5/1/1',
126                                            $destinationurl = '',
127                                            $formTemplate   = 'EzCommentTemplateDefault',
128                                            $listTemplate   = 'EzCommentTemplateDefault',
129                                           &$commentItem    = '')
130         {
131                 if ($skinType != 'item' && $skinType != 'template') return;
132                 global $manager, $member, $itemid;
133                 if (!$commentItem && $itemid) {
134                         $commentItem = $manager->getItem($itemid, 0, 0);
135                         if (is_array($commentItem)) {
136                                 $commentItem = (object)$commentItem;
137                         }
138                 }
139                 if (!$commentItem || $commentItem->closed) {
140                         echo _ERROR_ITEMCLOSED;
141                         return 0;
142                 }
143
144                 if (is_numeric($showType) || strpos($showType, '/') !== false) $showMode = $showType;
145                 if ($showType != 'list' && $showType != 'form') {
146                         $showType = '';
147                 }
148                 if (!$showMode) {
149                         $showMode = '5/1/1';
150                 }
151                 list($maxToShow, $sortOrder, $commentOrder) = explode('/', $showMode);
152                 if (!$maxToShow) $maxToShow = 5;
153                 if (!$sortOrder) $sortOrder = 1;
154                 if (!$commentOrder) $commentOrder = 1;
155                 if (!$formTemplate) $formTemplate = 'EzCommentTemplateDefault';
156                 if (!$listTemplate) $listTemplate = 'EzCommentTemplateDefault';
157
158                 switch ($showType) {
159                         case 'list':
160                                 $listTemplate = TEMPLATE::read($listTemplate);
161                                 $this->showComment($commentItem, $listTemplate, $maxToShow, $commentOrder);
162                                 break;
163                         case 'form':
164                                 $formTemplate = TEMPLATE::read($formTemplate);
165                                 $this->showForm($commentItem, $formTemplate, $destinationurl);
166                                 break;
167                         default:
168                                 $listTemplate = TEMPLATE::read($listTemplate);
169                                 $formTemplate = TEMPLATE::read($formTemplate);
170                                 if ($sortOrder) {
171                                         $this->showComment($commentItem, $listTemplate, $maxToShow, $commentOrder);
172                                         $this->showForm($commentItem, $formTemplate, $destinationurl);
173                                 } else {
174                                         $this->showForm($commentItem, $formTemplate, $destinationurl);
175                                         $this->showComment($commentItem, $listTemplate, $maxToShow, $commentOrder);
176                                 }
177                                 break;
178                 }
179         }
180
181 // FORM START ---------------------------------------
182         function showForm($commentItem, $template, $destinationurl)
183         {
184                 global $CONF, $manager, $member, $catid, $subcatid;
185                 $bid =  getBlogIDFromItemID($commentItem->itemid);
186                 $b   =& $manager->getBlog($bid);
187                 $b->readSettings();
188                 if (!$member->isLoggedIn() && !$b->commentsEnabled()) {
189                         return;
190                 }
191                 if (stristr($destinationurl, 'action.php') || empty($destinationurl)) {
192                         if (stristr($destinationurl, 'action.php')) {
193                                 $logMessage = 'actionurl is not longer a parameter on commentform skinvars.'
194                                                         . ' Moved to be a global setting instead.';
195                                 ACTIONLOG::add(WARNING, $logMessage);
196                         }
197                         if ($catid) {
198                                 $linkparams['catid'] = intval($catid);
199                         }
200                         if ($manager->pluginInstalled('NP_MultipleCategories') && $subcatid) {
201                                 $linkparams['subcatid'] = intval($subcatid);
202                         }
203                         $destinationurl = createItemLink($commentItem->itemid, $linkparams);
204                 } else {
205                         $destinationurl = preg_replace('|[^a-z0-9-~+_.?#=&;,/:@%]|i', '', $destinationurl);
206                 }
207
208                 $user = cookieVar($CONF['CookiePrefix'] .'comment_user');
209                 if (!$user) {
210                         $user = postVar('user');
211                 }
212                 $userid = cookieVar($CONF['CookiePrefix'] .'comment_userid');
213                 if (!$userid) {
214                         $userid = postVar('userid');
215                 }
216                 $email = cookieVar($CONF['CookiePrefix'] .'comment_email');
217                 if (!$email) {
218                         $email = postVar('email');
219                 }
220                 $body    = postVar('body');
221                 $checked = cookieVar($CONF['CookiePrefix'] .'comment_user') ? 'checked="checked" ' : '';
222
223                 $formdata = array(
224                         'self'            => $this->_hsc(serverVar('REQUEST_URI')),
225                         'destinationurl'  => $this->_hsc($destinationurl),
226                         'actionurl'       => $this->_hsc($CONF['ActionURL']),
227                         'itemid'          => intval($commentItem->itemid),
228                         'user'            => $this->_hsc($user),
229                         'userid'          => $this->_hsc($userid),
230                         'email'           => $this->_hsc($email),
231                         'body'            => $this->_hsc($body),
232 //                      'membername'      => $this->_hsc($membername),
233                         'rememberchecked' => $checked
234                 );
235                 if ($member && $member->isLoggedIn()) {
236                         $formType = 'COMMENT_FORM_LOGGEDIN';
237                         $loginMember = $member->createFromID($member->getID());
238                         $formdata['membername'] = $this->_hsc($loginMember->getDisplayName());
239                 } else {
240                         $formType = 'COMMENT_FORM_NOTLOGGEDIN';
241                 }
242                 $contents   = $template[$formType];
243                 $formAction =& new EzCommentFormActions($commentItem, $formdata, $loginMember);
244                 $parser     =& new PARSER($formAction->getAllowedActions(), $formAction);
245                 $parser->parse(&$contents);
246         }
247
248 // FORM END -----------------------------------------*/
249
250 // LIST START ---------------------------------------
251         function showComment($commentItem, $template, $maxToShow, $commentOrder)
252         {
253                 global $manager;
254                 $bid =  getBlogIDFromItemID($commentItem->itemid);
255                 $b   =& $manager->getBlog($bid);
256                 if (!$b->commentsEnabled()) return;
257                 if (!$maxToShow) {
258                         $maxToShow = $b->getMaxComments();
259                 }
260                 $itemActions =& new ITEMACTIONS($b);
261                 $itemActions->setCurrentItem($commentItem);
262                 $commentObj =& new COMMENTS($commentItem->itemid);
263                 $commentObj->setItemActions($itemActions);
264                 $commentObj->commentcount = $commentObj->amountComments();
265                 // create parser object & action handler
266                 $actions =& new EzCommentActions($commentObj);
267                 $parser  =& new PARSER($actions->getAllowedActions(), $actions);
268                 $actions->setTemplate($template);
269                 $actions->setParser($parser);
270                 if ($commentObj->commentcount == 0) {
271                         $parser->parse($template['COMMENTS_NONE']);
272                         return 0;
273                 }
274                 $actions->setPostnum($commentObj->commentcount);
275                 if ($maxToShow && $maxToShow < $commentObj->commentcount && $commentOrder) {
276                         $startnum = $commentObj->commentcount - $maxToShow;
277                 } else {
278                         $startnum = 0;
279                 }
280                 $order = ($commentOrder) ? "DESC" : "ASC";
281                 $query = 'SELECT '
282                            . 'c.citem   as itemid, '
283                            . 'c.cnumber as commentid, '
284                            . 'c.cbody   as body, '
285                            . 'c.cuser   as user, '
286                            . 'c.cmail   as userid, '
287                            . 'c.cemail  as email, '
288                            . 'c.cmember as memberid, '
289                            . 'c.ctime, '
290                            . 'c.chost   as host, '
291                            . 'c.cip     as ip, '
292                            . 'c.cblog   as blogid'
293                            . ' FROM ' . sql_table('comment') . ' as c'
294                            . ' WHERE c.citem=' . intval($commentItem->itemid)
295                            . ' ORDER BY c.ctime '
296                            . $order;
297                 if ($maxToShow) {
298                         if ($order == "DESC") {
299                                 $query .=' LIMIT ' . intval($maxToShow);
300                         } else {
301                                 $query .=' LIMIT ' . intval($startnum) . ',' . intval($maxToShow);
302                         }
303                 }
304                 $comments = sql_query($query);
305                 $viewnum  = mysql_num_rows($comments);
306                 $actions->setViewnum($viewnum);
307
308                 $parser->parse($template['COMMENTS_HEADER']);
309
310                 while ( $comment = mysql_fetch_assoc($comments) ) {
311                         $comment['timestamp'] = strtotime($comment['ctime']);
312                         $actions->setCurrentComment($comment);
313                         $manager->notify('PreComment', array('comment' => &$comment));
314                         $parser->parse($template['COMMENTS_BODY']);
315                         $manager->notify('PostComment', array('comment' => &$comment));
316                 }
317
318                 $parser->parse($template['COMMENTS_FOOTER']);
319
320                 mysql_free_result($comments);
321
322         }
323 // LIST END -----------------------------------------
324
325         function getTemplateParts()
326         {
327                 $this->languageInclude();
328                 return array(
329                         'COMMENT_FORM_LOGGEDIN'    => _NP_EZCOMMENT2_FORM_LOGGEDIN, 
330                         'COMMENT_FORM_NOTLOGGEDIN' => _NP_EZCOMMENT2_FORM_NOTLOGGEDIN, 
331                 );
332         }
333
334         function _hsc($str)
335         {
336                 return htmlspecialchars($str, ENT_QUOTES, _CHARSET);
337         }
338 }
339
340 class EzCommentFormActions extends ACTIONS
341 {
342
343 var $commentItem;
344
345 var $loginMember;
346
347         function EzCommentFormActions(&$item, $formdata, $member)
348         {
349                 $this->ACTIONS('item');
350                 $this->commentItem =& $item;
351                 $this->formdata    =  $formdata;
352                 $this->loginMember =  $member;
353         }
354
355         function getAllowedActions()
356         {
357                 return array(
358                                          'text',
359                                          'self',
360                                          'formdata',
361                                          'callback',
362                                          'errordiv',
363                                          'ticket',
364                                          'itemid',
365                                          'itemlink',
366                                          'itemtitle',
367                                          'membername',
368                                          'memberurl',
369                                         );
370         }
371
372         function parse_itemid() {
373                 echo $this->commentItem->itemid;
374         }
375         
376         function parse_itemlink($linktext = '') {
377                 global $itemid;
378                 $this->_itemlink($this->commentItem->itemid, $linktext);
379         }
380
381         function parse_itemtitle($format = '') {
382                 switch ($format) {
383                         case 'xml':
384                                 echo stringToXML ($this->commentItem->itemtitle);
385                                 break;
386                         case 'attribute':
387                                 echo stringToAttribute ($this->commentItem->itemtitle);
388                                 break;
389                         case 'raw':
390                                 echo $this->commentItem->itemtitle;
391                                 break;
392                         default:
393                                 echo $this->_hsc(strip_tags($this->commentItem->itemtitle));
394                                 break;
395                 }
396         }
397
398         function parse_membername($mode='')
399         {
400                 if ($mode == 'realname') {
401                         echo $this->_hsc($this->loginMember->getRealName());
402                 } else {
403                         echo $this->_hsc($this->loginMember->getDisplayName());
404                 }
405         }
406
407         function parse_memberurl()
408         {
409                 echo $this->_hsc($this->loginMember->getURL());
410         }
411
412         function _hsc($str)
413         {
414                 return htmlspecialchars($str, ENT_QUOTES, _CHARSET);
415         }
416
417 }
418
419 class EzCommentActions extends COMMENTACTIONS
420 {
421
422 var $viewnum;
423
424 var $postnum;
425
426         function EzCommentActions(&$comments)
427         {
428                 $this->COMMENTACTIONS($comments);
429         }
430
431         function getAllowedActions()
432         {
433                 $allowedActions   = $this->getDefinedActions();
434                 $allowedActions[] = 'viewnum';
435                 $allowedActions[] = 'postnum';
436                 $allowedActions[] = 'viewparpost';
437                 return $allowedActions;
438         }
439
440         function setPostnum($postnum)
441         {
442                 $this->postnum = $postnum;
443         }
444
445         function setViewnum($viewnum)
446         {
447                 $this->viewnum = $viewnum;
448         }
449
450         function parse_viewnum()
451         {
452                 echo intval($this->viewnum);
453         }
454
455         function parse_postnum()
456         {
457                 echo intval($this->postnum);
458         }
459
460         function parse_viewparpost()
461         {
462                 echo intval($this->viewnum) . ' ';
463                 if ($this->postnum > $this->viewnum) {
464                         echo '/ ' . $this->postnum . ' ';
465                 }
466                 $this->parse_commentword();
467         }
468
469 }
470
471
472