3 * SHOW Comment Form/List PLUG-IN FOR NucleusCMS
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 * (see nucleus/documentation/index.html#license for more info)
13 * @copyright 2008 shizuki
14 * @license http://www.gnu.org/licenses/gpl.txt GNU GENERAL PUBLIC LICENSE Version 2, June 1991
15 * @version $Date: 2008-07-07 10:24:00 $ $Revision: 1.7 $
16 * @link http://japan.nucleuscms.org/wiki/plugins:showblogs
17 * @since File available since Release 1.0
23 * $Log: not supported by cvs2svn $
26 class NP_EzComment2 extends NucleusPlugin
31 * The calling number of times by the index page.
38 * OpenID authentication module.
54 return 'Ez Comment II';
74 * I get a plug-in, the address of the possible site or author's mail address.
80 return 'http://japan.nucleuscms.org/wiki/plugins:ezcomment2';
91 function getPluginDep()
95 'NP_znSpecialTemplateParts',
107 function getVersion()
109 return '$Date: 2008-07-07 10:24:00 $ $Revision: 1.7 $';
113 // {{{ getDescription()
120 function getDescription()
122 return _NP_EZCOMMENT2_DESC;
126 // {{{ supportsFeature($what)
129 * Supports Nucleus Feature
134 function supportsFeature($what)
137 case 'SqlTablePrefix':
145 // {{{ getEventList()
148 * List of feature event
152 function getEventList()
162 // {{{ getTableList()
165 * Database tables for plugin used
169 function getTableList()
172 sql_table('plug_ezcomment2'),
186 if (!TEMPLATE::exists('EzCommentTemplate')) {
188 include_once($DIR_LIBS . 'skinie.php');
189 $importer = new SKINIMPORT();
191 $template = $this->getDirectory() . 'skinbackup.xml';
192 if (!@file_exists($template)) {
193 $aErrors[] = 'Unable to import ' . $template . ' : file does not exist';
196 $error = $importer->readFile($template);
198 $aErrors[] = 'Unable to import ' . $template . ' : ' . $error;
201 $error = $importer->writeToDatabase(1);
203 $aErrors[] = 'Unable to import ' . $template . ' : ' . $error;
207 $message = implode("<br />\n", $aErrors);
210 $this->createBlogOption('secret', _NP_EZCOMMENT2_OP_SECRETMODE, 'yesno', 'no');
211 $this->createBlogOption('secComment', _NP_EZCOMMENT2_OP_SUBSTIUTION, 'text', _NP_EZCOMMENT2_OP_SUBSTIUTION_VAL);
212 $this->createBlogOption('secLabel', _NP_EZCOMMENT2_OP_CHECKLABEL, 'text', _NP_EZCOMMENT2_OP_CHECKLABEL_VAL);
213 $this->createOption('tabledel', _NP_EZCOMMENT2_OP_DROPTABLE, 'yesno', 'no');
214 $sql = 'CREATE TABLE IF NOT EXISTS %s ('
215 . '`comid` int(11) NOT NULL, '
216 . '`secflg` tinyint(1) NULL, '
217 . '`module` varchar(15) NULL, '
218 . '`userID` varchar(255) NULL, '
219 . 'PRIMARY KEY(`comid`) );';
220 sql_query(sprintf($sql, sql_table('plug_ezcomment2')));
221 $this->updateTable();
229 * Un Install function
235 if ($this->getOption('tabledel') == 'yes')
236 sql_query('DROP TABLE '.sql_table('plug_ezcomment2'));
249 $this->languageInclude();
250 $this->numcalled = 0;
252 if ($manager->pluginInstalled('NP_OpenId') && !$this->authOpenID) {
253 $this->authOpenID = $manager->getPlugin('NP_OpenId');
258 // {{{ event_PostAddComment($data)
261 * After adding a comment to the database.
269 function event_PostAddComment($data)
273 case $member->isLoggedin():
274 $userID = $member->getID();
277 case ($this->authOpenID && $this->authOpenID->isLoggedin()):
278 $userID = $this->authOpenID->loggedinUser['identity'];
284 if (postVar('EzComment2_Secret')) {
289 $sql = 'INSERT INTO ' . sql_table('plug_ezcomment2')
290 . ' (`comid`, `secflg`, `module`, `userID`) VALUES (%d, %d, %s, %s)';
291 sql_query(sprintf($sql, $data['commentid'], $secCheck, $module, $userID));
295 // {{{ event_FormExtra(&$data)
298 * Inside one of the comment, membermail or account activation forms.
304 function event_FormExtra(&$data)
308 if ($blogid && $this->getBlogOption($blogid, 'secret') == 'yes') {
309 echo '<br /><input type="checkbox" value="1" name="EzComment2_Secret" id="EzComment2_Secret_' . $this->numcalled . '" />';
310 echo '<label for="EzComment2_Secret_' . $this->numcalled . '">'.$this->getBlogOption($bid, 'secLabel').'</label><br />';
315 // {{{ doTemplateVar()
318 * Basically the same as doSkinVar,
319 * but this time for calls of the <%plugin(...)%>-var in templates (item header/body/footer and dateheader/footer).
321 * @param object item object(refarence)
329 function doTemplateVar(&$item,
332 $destinationurl = '',
333 $formTemplate = 'EzCommentTemplate',
334 $listTemplate = 'EzCommentTemplate')
336 $this->doSkinVar('template', $showType, $showMode, $destinationurl, $formTemplate, $listTemplate, $item);
343 * When plugins are called using the <%plugin(...)%>-skinvar, this method will be called.
351 * @param object item object(refarence)
354 function doSkinVar($skinType,
357 $destinationurl = '',
358 $formTemplate = 'EzCommentTemplate',
359 $listTemplate = 'EzCommentTemplate',
362 if ($skinType != 'item' && $skinType != 'template') return;
363 global $manager, $member, $itemid;
364 if (!$commentItem && $itemid) {
365 $commentItem = $manager->getItem($itemid, 0, 0);
366 if (is_array($commentItem)) {
367 $commentItem = (object)$commentItem;
370 if (!$commentItem || $commentItem->closed) {
371 echo _ERROR_ITEMCLOSED;
375 if (is_numeric($showType) || strpos($showType, '/') !== false) $showMode = $showType;
376 if ($showType != 'list' && $showType != 'form') {
382 list($maxToShow, $sortOrder, $commentOrder) = explode('/', $showMode);
383 if (!$maxToShow) $maxToShow = 5;
384 if (!$sortOrder) $sortOrder = 1;
385 if ($commentOrder > 0) {
386 $commentOrder = true;
388 $commentOrder = false;
390 if (!$formTemplate) $formTemplate = 'EzCommentTemplate';
391 if (!$listTemplate) $listTemplate = 'EzCommentTemplate';
395 $listTemplate = TEMPLATE::read($listTemplate);
396 $this->showComment($commentItem, $listTemplate, $maxToShow, $commentOrder, $skinType);
399 $formTemplate = TEMPLATE::read($formTemplate);
400 $this->showForm($commentItem, $formTemplate, $destinationurl, $skinType);
403 $listTemplate = TEMPLATE::read($listTemplate);
404 $formTemplate = TEMPLATE::read($formTemplate);
406 $this->showComment($commentItem, $listTemplate, $maxToShow, $commentOrder, $skinType);
407 $this->showForm($commentItem, $formTemplate, $destinationurl, $skinType);
409 $this->showForm($commentItem, $formTemplate, $destinationurl, $skinType);
410 $this->showComment($commentItem, $listTemplate, $maxToShow, $commentOrder, $skinType);
417 // {{{ languageInclude()
420 * Include language file
424 function languageInclude()
426 $language = ereg_replace( '[\\|/]', '', getLanguageName());
427 if (file_exists($this->getDirectory() . 'language/' . $language . '.php')) {
428 include_once($this->getDirectory() . 'language/' . $language . '.php');
430 include_once($this->getDirectory() . 'language/english.php');
438 * Update database table
442 function updateTable()
444 $sql = 'SELECT cnumber FROM ' . sql_table('comment') . ' ORDER BY cnumber';
445 $res = sql_query($sql);
446 $sql = 'REPLACE INTO ' . sql_table('plug_ezcomment2') . '(`comid`) VALUES (%d)';
447 while ($cid = mysql_fetch_assoc($res)) {
448 sql_query(sprintf($sql, $cid['cnumber']));
453 // {{{ plugOpenIDdoSkinVar()
456 * Overwride NP_OpenId's doSkinVar()
462 function plugOpenIDdoSkinVar($skinType, $iid = 0)
464 global $CONF, $manager, $member;
465 if ($member->isLoggedIn()) return;
466 $authOpenID = $this->authOpenID;
467 if (!$authOpenID) return;
468 $externalauth = array ( 'source' => $authOpenID->getName() );
469 $manager->notify('ExternalAuth', array ('externalauth' => &$externalauth));
470 if (isset($externalauth['result']) && $externalauth['result'] == true) return;
471 $templateEngine = $authOpenID->_getTemplateEngine();
473 $aVars['PluginURL'] = $CONF['PluginURL'];
474 if ($authOpenID->isLoggedin()) {
476 if ($skinType == 'template') {
477 require_once 'cles/Template.php';
478 $templateDirectory = rtrim($this->getDirectory(), '/');
479 $templateEngine =& new cles_Template($templateDirectory);
480 $templateEngine->defaultLang = 'english';
481 $aVars['itemid'] = intval($iid);
483 $nowURL = 'http://' . serverVar("HTTP_HOST")
484 . serverVar("REQUEST_URI");
485 $aVars['url'] = $authOpenID->getAdminURL() . 'rd.php?action=rd'
486 . '&url=' . urlencode($nowURL);
487 $aVars['nick'] = $authOpenID->loggedinUser['nick'];
488 $aVars['email'] = $authOpenID->loggedinUser['email'];
489 $aVars['ts'] = $authOpenID->loggedinUser['ts'];
490 $aVars['identity'] = $authOpenID->loggedinUser['identity'];
491 $aVars['visible'] = $aVars['nick'] ? 'false' : 'true' ;
492 $actionUrl = parse_url($CONF['ActionURL']);
493 $aVars['updateUrl'] = $actionUrl['path'];
494 echo $templateEngine->fetchAndFill('yui', $aVars, 'np_openid');
495 echo $templateEngine->fetchAndFill('loggedin', $aVars, 'np_openid');
496 echo $templateEngine->fetchAndFill('form', $aVars, 'np_openid');
497 } elseif (!$authOpenID->isLoggedin()) {
499 $aVars['url'] = $authOpenID->getAdminURL() . 'rd.php?action=doauth'
500 . '&return_url=' . urlencode(createItemLink(intval($iid)));
501 echo $templateEngine->fetchAndFill('notloggedin', $aVars, 'np_openid');
506 // {{{ checkDestinationurl($destinationurl)
509 * Destinationurl check
514 function checkDestinationurl($destinationurl)
516 if (stristr($destinationurl, 'action.php') || empty($destinationurl)) {
517 if (stristr($destinationurl, 'action.php')) {
518 $logMessage = 'actionurl is not longer a parameter on commentform skinvars.'
519 . ' Moved to be a global setting instead.';
520 ACTIONLOG::add(WARNING, $logMessage);
523 $linkparams['catid'] = intval($catid);
525 if ($manager->pluginInstalled('NP_MultipleCategories') && $subcatid) {
526 $linkparams['subcatid'] = intval($subcatid);
528 $destinationurl = createItemLink($commentItem->itemid, $linkparams);
530 $destinationurl = preg_replace('|[^a-z0-9-~+_.?#=&;,/:@%]|i', '', $destinationurl);
532 return $destinationurl;
536 // {{{ getCommentatorInfo()
539 * Get commentator info.
543 function getCommentatorInfo()
546 $user = cookieVar($CONF['CookiePrefix'] .'comment_user');
548 $user = postVar('user');
550 $userid = cookieVar($CONF['CookiePrefix'] .'comment_userid');
552 $userid = postVar('userid');
554 $email = cookieVar($CONF['CookiePrefix'] .'comment_email');
556 $email = postVar('email');
558 $body = postVar('body');
577 function showForm($commentItem, $template, $destinationurl, $skinType)
579 global $CONF, $manager, $member, $catid, $subcatid;
580 $bid = getBlogIDFromItemID($commentItem->itemid);
581 $b =& $manager->getBlog($bid);
583 if (!$member->isLoggedIn() && !$b->commentsEnabled()) {
586 $destinationurl = $this->checkDestinationurl($destinationurl);
587 list($user, $userid, $email, $body) = $this->getCommentatorInfo();
589 $checked = cookieVar($CONF['CookiePrefix'] .'comment_user') ? 'checked="checked" ' : '';
592 'self' => $this->_hsc(serverVar('REQUEST_URI')),
593 'destinationurl' => $this->_hsc($destinationurl),
594 'actionurl' => $this->_hsc($CONF['ActionURL']),
595 'itemid' => intval($commentItem->itemid),
596 'user' => $this->_hsc($user),
597 'userid' => $this->_hsc($userid),
598 'email' => $this->_hsc($email),
599 'body' => $this->_hsc($body),
600 // 'membername' => $this->_hsc($membername),
601 'rememberchecked' => $checked
603 if ($skinType == 'item') {
608 if ($member && $member->isLoggedIn()) {
609 $formType = 'FORM_LOGGEDIN' . $formFlg;
610 $loginMember = $member->createFromID($member->getID());
611 $formdata['membername'] = $this->_hsc($loginMember->getDisplayName());
613 $formType = 'FORM_NOTLOGGEDIN' . $formFlg;
615 if ($this->authOpenID && ($skinType == 'item' || $this->numcalled == 0)) {
616 $this->plugOpenIDdoSkinVar($skinType, intval($commentItem->itemid));
618 $contents = $template[$formType];
619 include_once($this->getDirectory() . 'EzCommentActions.php');
620 $formAction =& new EzCommentFormActions($commentItem, $formdata, $loginMember);
621 $parser =& new PARSER($formAction->getAllowedActions(), $formAction);
622 $parser->parse(&$contents);
638 function showComment($commentItem, $template, $maxToShow, $commentOrder, $skinType)
640 global $manager, $member;
641 $bid = getBlogIDFromItemID($commentItem->itemid);
642 $b =& $manager->getBlog($bid);
643 if (!$b->commentsEnabled()) return;
645 $maxToShow = $b->getMaxComments();
647 $itemActions =& new ITEMACTIONS($b);
648 $itemActions->setCurrentItem($commentItem);
649 $commentObj =& new COMMENTS($commentItem->itemid);
650 $commentObj->setItemActions($itemActions);
651 $commentObj->commentcount = $commentObj->amountComments();
652 // create parser object & action handler
653 include_once($this->getDirectory() . 'EzCommentActions.php');
654 $actions =& new EzCommentActions($commentObj);
655 $parser =& new PARSER($actions->getAllowedActions(), $actions);
656 $actions->setTemplate($template);
657 $actions->setParser($parser);
658 if ($commentObj->commentcount == 0) {
659 $parser->parse($template['COMMENTS_NONE']);
662 $actions->setPostnum($commentObj->commentcount);
663 if ($maxToShow && $maxToShow < $commentObj->commentcount && $commentOrder) {
664 $startnum = $commentObj->commentcount - $maxToShow;
668 $comments = $this->getComments($commentOrder, intval($commentItem->itemid), $maxToShow, $startnum);
669 $viewnum = mysql_num_rows($comments);
670 $actions->setViewnum($viewnum);
671 if ($this->getBlogOption($bid, 'secret') == 'yes') {
672 $secret = $this->setSecretJudge($bid, $member, $b);
676 if ($skinType == 'index') $templateType = '_IDX';
677 $blogURL = $b->getURL();
678 $substitution = $this->getBlogOption($bid, 'secComment');
680 $parser->parse($template['COMMENTS_HEADER' . $templateType]);
682 while ($comment = mysql_fetch_assoc($comments)) {
683 $comment['timestamp'] = strtotime($comment['ctime']);
685 $comment = $this->JudgementCommentSecrets($comment, $secret, $blogURL, $substitution);
687 $actions->setCurrentComment($comment);
688 $manager->notify('PreComment', array('comment' => &$comment));
689 $parser->parse($template['COMMENTS_BODY' . $templateType]);
690 $manager->notify('PostComment', array('comment' => &$comment));
693 $parser->parse($template['COMMENTS_FOOTER' . $templateType]);
695 mysql_free_result($comments);
700 // {{{ setSecretJudge($bid)
703 * Setting for judgment of whether it's a comment of a secret.
710 function setSecretJudge($bid, $member, $b)
712 $memberLoggedin = $member->isLoggedin();
713 $loginUser = $member->getID();
714 $blogAdmin = $member->blogAdminRights($bid);
715 $blogURL = $b->getURL();
716 $substitution = $this->getBlogOption($bid, 'secComment');
717 if ($this->authOpenID) {
718 $openIDLoggedin = $this->authOpenID->isLoggedin();
719 $openIDUser = $this->authOpenID->loggedinUser['identity'];
722 'memberLoggedin' => $memberLoggedin,
723 'loginUser' => $loginUser,
724 'blogAdmin' => $blogAdmin,
725 'blogURL' => $blogURL,
726 'substitution' => $substitution,
727 'openIDLoggedin' => $openIDLoggedin,
728 'openIDUser' => $openIDUser,
733 // {{{ JudgementCommentSecrets($comment, $judge)
736 * Comment is secret ?
744 function JudgementCommentSecrets($comment, $judge, $blogURL, $substitution)
746 if (!(($judge['memberLoggedin'] && ($judge['loginUser'] == intval($comment['identity']) || $blogAdmin)) ||
747 ($judge['openIDLoggedin'] && $judge['openIDUser'] == $comment['identity'])) && $comment['secret']) {
748 $this->changeCommentSet($comment, $blogURL, $substitution);
754 // {{{ changeCommentSet($comment, $blogURL, $substitution)
757 * Change secret comment contents
764 function changeCommentSet($comment, $blogURL, $substitution)
766 $comment['body'] = $substitution;
767 $comment['userid'] = $blogURL;
768 $comment['memberid'] = 0;
769 $comment['user'] = '#';
770 $comment['email'] = '#';
771 $comment['host'] = '127.0.0.1';
772 $comment['ip'] = '127.0.0.1';
775 // {{{ getComments($comment, $judge)
778 * Change in the comment contents.
786 function getComments($commentOrder, $iid, $maxToShow, $startnum)
788 $order = ($commentOrder) ? "DESC" : "ASC";
790 . 'c.citem as itemid, '
791 . 'c.cnumber as commentid, '
792 . 'c.cbody as body, '
793 . 'c.cuser as user, '
794 . 'c.cmail as userid, '
795 . 'c.cemail as email, '
796 . 'c.cmember as memberid, '
798 . 'c.chost as host, '
800 . 'c.cblog as blogid, '
802 . 's.secflg as secret, '
803 . 's.module as modname, '
804 . 's.userID as identity '
805 . ' FROM ' . sql_table('comment') . ' as c '
806 . ' LEFT OUTER JOIN ' . sql_table('plug_ezcomment2') . ' as s '
807 . ' p ON c.cnumber = s.comid '
808 . ' WHERE c.citem = ' . intval($iid)
809 . ' ORDER BY c.ctime '
812 if ($order == "DESC") {
813 $query .=' LIMIT ' . intval($maxToShow);
815 $query .=' LIMIT ' . intval($startnum) . ',' . intval($maxToShow);
818 return sql_query($query);
823 // {{{ getTemplateParts()
826 * Comment form/list template via NP_znSpecialTemplateParts
830 function getTemplateParts()
832 $this->languageInclude();
834 'FORM_LOGGEDIN_IDX' => _NP_EZCOMMENT2_FORM_LOGGEDIN_IDX,
835 'FORM_NOTLOGGEDIN_IDX' => _NP_EZCOMMENT2_FORM_NOTLOGGEDIN_IDX,
836 'FORM_LOGGEDIN_ITM' => _NP_EZCOMMENT2_FORM_LOGGEDIN_ITM,
837 'FORM_NOTLOGGEDIN_ITM' => _NP_EZCOMMENT2_FORM_NOTLOGGEDIN_ITM,
838 'COMMENTS_BODY_IDX' => _NP_EZCOMMENT2_COMMENTS_BODY_IDX,
839 'COMMENTS_FOOTER_IDX' => _NP_EZCOMMENT2_COMMENTS_FOOTER_IDX,
840 'COMMENTS_HEADER_IDX' => _NP_EZCOMMENT2_COMMENTS_HEADER_IDX,
855 return htmlspecialchars($str, ENT_QUOTES, _CHARSET);