OSDN Git Service

CHANGE:NOTIFICATIONクラスとENTITYクラスのメソッドではなくglobalfunctions.phpの関数を参照している箇所を修正
[nucleus-jp/nucleus-next.git] / nucleus / libs / ITEMACTIONS.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2009 The Nucleus Group
5  *
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)
11  */
12 /**
13  * This class is used to parse item templates
14  *
15  * @license http://nucleuscms.org/license.txt GNU General Public License
16  * @copyright Copyright (C) 2002-2009 The Nucleus Group
17  * @version $Id: ITEMACTIONS.php 1626 2012-01-09 15:46:54Z sakamocchi $
18  */
19 class ITEMACTIONS extends BaseActions {
20
21         // contains an assoc array with parameters that need to be included when
22         // generating links to items/archives/... (e.g. catid)
23         var $linkparams;
24
25         // true when the current user is a blog admin (and thus allowed to edit all items)
26         var $allowEditAll;
27
28         // timestamp of last visit
29         var $lastVisit;
30
31         // item currently being handled (mysql result object, see BLOG::showUsingQuery)
32         var $currentItem;
33
34         // reference to the blog currently being displayed
35         var $blog;
36
37         // associative array with template info (part name => contents)
38         var $template;
39
40         // true when comments need to be displayed
41         var $showComments;
42
43         function ITEMACTIONS(&$blog) {
44                 // call constructor of superclass first
45                 $this->BaseActions();
46
47                 // extra parameters for created links
48                 global $catid;
49                 if ($catid)
50                         $this->linkparams = array('catid' => $catid);
51
52                 // check if member is blog admin (and thus allowed to edit all items)
53                 global $member;
54                 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));
55                 $this->setBlog($blog);
56         }
57
58         /**
59           * Returns an array with the actions that are defined
60           * in the ITEMACTIONS class
61           */
62         function getDefinedActions() {
63                 return array(
64                         'blogid',
65                         'title',
66                         'body',
67                         'more',
68                         'smartbody',
69                         'itemid',
70                         'morelink',
71                         'category',
72                         'categorylink',
73                         'author',
74                         'authorid',
75                         'authorlink',
76                         'catid',
77                         'karma',
78                         'date',
79                         'time',
80                         'query',
81                         'itemlink',
82                         'blogurl',
83                         'closed',
84                         'syndicate_title',
85                         'syndicate_description',
86                         'karmaposlink',
87                         'karmaneglink',
88                         'new',
89                         'image',
90                         'popup',
91                         'media',
92                         'daylink',
93                         'query',
94                         'include',
95                         'phpinclude',
96                         'parsedinclude',
97                         'skinfile',
98                         'set',
99                         'plugin',
100                         'edit',
101                         'editlink',
102                         'editpopupcode',
103                         'comments',
104                         'relevance',
105                         'if',
106                         'else',
107                         'endif',
108                         'elseif',
109                         'ifnot',
110                         'elseifnot'
111                 );
112         }
113
114         function setLastVisit($lastVisit) {
115                 $this->lastVisit = $lastVisit;
116         }
117
118         function setParser(&$parser) {
119                 $this->parser =& $parser;
120         }
121
122         function setCurrentItem(&$item) {
123                 $this->currentItem =& $item;
124                 global $currentitemid;
125                 $currentitemid = $this->currentItem->itemid;
126         }
127
128         function setBlog(&$blog) {
129                 $this->blog =& $blog;
130         }
131
132         function setTemplate($template) {
133                 $this->template =& $template;
134         }
135
136         function setShowComments($val) {
137                 $this->showComments = $val;
138         }
139
140         // methods used by parser to insert content
141
142
143         /**
144          * Parse templatevar blogid
145          */
146         function parse_blogid() {
147                 echo $this->blog->getID();
148         }
149
150         /**
151          * Parse templatevar body
152          */
153         function parse_body() {
154                 $this->highlightAndParse($this->currentItem->body);
155         }
156
157         /**
158          * Parse templatevar more
159          */
160         function parse_more() {
161                 $this->highlightAndParse($this->currentItem->more);
162         }
163
164         /**
165          * Parse templatevar itemid
166          */
167         function parse_itemid() {
168                 echo $this->currentItem->itemid;
169         }
170
171         /**
172          * Parse templatevar category
173          */
174         function parse_category() {
175                 echo $this->currentItem->category;
176         }
177
178         /**
179          * Parse templatevar categorylink
180          */
181         function parse_categorylink() {
182                 echo Link::create_link('category', array('catid' => $this->currentItem->catid, 'name' => $this->currentItem->category));
183         }
184
185         /**
186          * Parse templatevar catid
187          */
188         function parse_catid() {
189                 echo $this->currentItem->catid;
190         }
191
192         /**
193          * Parse templatevar authorid
194          */
195         function parse_authorid() {
196                 echo $this->currentItem->authorid;
197         }
198
199         /**
200          * Parse templatevar authorlink
201          */
202         function parse_authorlink() {
203                 echo Link::create_link(
204                         'member',
205                         array(
206                                 'memberid' => $this->currentItem->authorid,
207                                 'name' => $this->currentItem->author,
208                                 'extra' => $this->linkparams
209                         )
210                 );
211         }
212
213         /**
214          * Parse templatevar query
215          */
216         function parse_query() {
217                 echo $this->strHighlight;
218         }
219
220         /**
221          * Parse templatevar itemlink
222          */
223         function parse_itemlink() {
224                 echo Link::create_link(
225                         'item',
226                         array(
227                                 'itemid' => $this->currentItem->itemid,
228                                 'title' => $this->currentItem->title,
229                                 'timestamp' => $this->currentItem->timestamp,
230                                 'extra' => $this->linkparams
231                         )
232                 );
233         }
234
235         /**
236          * Parse templatevar blogurl
237          */
238         function parse_blogurl() {
239                 echo $this->blog->getURL();
240         }
241
242         /**
243          * Parse templatevar closed
244          */
245         function parse_closed() {
246                 echo $this->currentItem->closed;
247         }
248
249         /**
250          * Parse templatevar relevance
251          */
252         function parse_relevance() {
253                 echo round($this->currentItem->score,2);
254         }
255
256         /**
257          * Parse templatevar title
258          *
259          * @param string $format defines in which format the title is shown
260          */
261         function parse_title($format = '')
262         {
263                 if ( is_array($this->currentItem) )
264                 {
265                         $itemtitle = $this->currentItem['title'];
266                 }
267                 elseif ( is_object($this->currentItem) )
268                 {
269                         $itemtitle = $this->currentItem->title;
270                 }
271                 switch ( $format )
272                 {
273                         case 'xml':
274                                 echo ENTITY::hen($itemtitle);
275                                 break;
276                         case 'attribute':
277                                 echo ENTITY::hsc($itemtitle);
278                                 break;
279                         case 'raw':
280                                 echo $itemtitle;
281                                 break;
282                         default:
283                                 $this->highlightAndParse($itemtitle);
284                                 break;
285                 }
286                 return;
287         }
288
289         /**
290          * Parse templatevar karma
291          */
292         function parse_karma($type = 'totalscore') {
293                 global $manager;
294
295                 // get karma object
296                 $karma =& $manager->getKarma($this->currentItem->itemid);
297
298                 switch($type) {
299                         case 'pos':
300                                 echo $karma->getNbPosVotes();
301                                 break;
302                         case 'neg':
303                                 echo $karma->getNbNegVotes();
304                                 break;
305                         case 'votes':
306                                 echo $karma->getNbOfVotes();
307                                 break;
308                         case 'posp':
309                                 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
310                                 echo number_format($percentage,2), '%';
311                                 break;
312                         case 'negp':
313                                 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
314                                 echo number_format($percentage,2), '%';
315                                 break;
316                         case 'totalscore':
317                         default:
318                                 echo $karma->getTotalScore();
319                                 break;
320                 }
321
322         }
323
324         /**
325          * Parse templatevar author
326          */
327         function parse_author($which = '') {
328                 switch($which)
329                 {
330                         case 'realname':
331                                 echo $this->currentItem->authorname;
332                                 break;
333                         case 'id':
334                                 echo $this->currentItem->authorid;
335                                 break;
336                         case 'email':
337                                 echo $this->currentItem->authormail;
338                                 break;
339                         case 'url':
340                                 echo $this->currentItem->authorurl;
341                                 break;
342                         case 'name':
343                         default:
344                                 echo $this->currentItem->author;
345                 }
346         }
347
348         /**
349          * Parse templatevar smartbody
350          */
351         function parse_smartbody() {
352                 if (!$this->currentItem->more) {
353                         $this->highlightAndParse($this->currentItem->body);
354                 } else {
355                         $this->highlightAndParse($this->currentItem->more);
356                 }
357         }
358
359         /**
360          * Parse templatevar morelink
361          */
362         function parse_morelink() {
363                 if ($this->currentItem->more)
364                         $this->parser->parse($this->template['MORELINK']);
365         }
366         
367         /**
368          * ITEMACTIONS::parse_date()
369          * Parse templatevar date
370          *
371          * @param       String  $format format optional strftime format
372          * @return      String  formatted datetime
373          */
374         function parse_date($format = '')
375         {
376                 if ( !isset($this->template['FORMAT_DATE']) )
377                 {
378                         $this->template['FORMAT_DATE'] = '';
379                 }
380                 
381                 $offset = date('Z', $timestamp);
382                 if ( $this->blog )
383                 {
384                         $offset += $this->blog->getTimeOffset() * 3600;
385                 }
386                 
387                 echo formatDate($format, $this->currentItem->timestamp, $this->template['FORMAT_DATE'], $offset);
388         }
389         
390         /**
391           * Parse templatevar time
392           *
393           * @param format optional strftime format
394           */
395         function parse_time($format = '') {
396                 if (!isset($this->template['FORMAT_TIME'])) $this->template['FORMAT_TIME'] = '';
397                 echo i18n::strftime($format ? $format : $this->template['FORMAT_TIME'],$this->currentItem->timestamp);
398         }
399
400         /**
401          * ITEMACTIONS::parse_syndicate_title()
402          * Parse templatevar syndicate_title
403          *
404          * @param String        $maxLength      maxLength optional maximum length
405          * @return      String  syndicated title
406          */
407         function parse_syndicate_title($maxLength = 100) {
408                 $syndicated = strip_tags($this->currentItem->title);
409                 echo ENTITY::hsc(ENTITY::shorten($syndicated,$maxLength,'...'));
410         }
411         
412         /**
413          * ITEMACTIONS::parse_syndicate_description()
414          * Parse templatevar syndicate_description
415          *
416          * @param Stromg        $maxLength      maxlength optional maximum length
417          * @param       String  $addHighlight   highlighted string
418          * @return      Void
419          */
420         function parse_syndicate_description($maxLength = 250, $addHighlight = 0)
421         {
422                 $syndicated = strip_tags($this->currentItem->body);
423                 if ( $addHighlight )
424                 {
425                         $tmp_highlight = ENTITY::hsc(ENTITY::shorten($syndicated,$maxLength,'...'));
426                         echo $this->highlightAndParse($tmp_highlight);
427                 }
428                 else
429                 {
430                         echo ENTITY::hsc(ENTITY::shorten($syndicated,$maxLength,'...'));
431                 }
432                 return;
433         }
434         
435         /**
436           * Parse templatevar karmaposlink
437           *
438           * @param string text
439           */
440         function parse_karmaposlink($text = '') {
441                 global $CONF;
442                 $link = $CONF['ActionURL'] . '?action=votepositive&amp;itemid='.$this->currentItem->itemid;
443                 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
444         }
445
446         /**
447           * Parse templatevar karmaneglink
448           *
449           * @param string text
450           */
451         function parse_karmaneglink($text = '') {
452                 global $CONF;
453                 $link = $CONF['ActionURL'] . '?action=votenegative&amp;itemid='.$this->currentItem->itemid;
454                 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
455         }
456
457         /**
458           * Parse templatevar new
459           */
460         function parse_new() {
461                 if (($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit))
462                         echo $this->template['NEW'];
463         }
464
465         /**
466           * Parse templatevar daylink
467           */
468         function parse_daylink() {
469                 echo Link::create_archive_link($this->blog->getID(), i18n::strftime('%Y-%m-%d',$this->currentItem->timestamp), $this->linkparams);
470         }
471
472         /**
473           * Parse templatevar comments
474           */
475         function parse_comments($maxToShow = 0) {
476                 if ($maxToShow == 0)
477                         $maxToShow = $this->blog->getMaxComments();
478
479                 // add comments
480                 if ($this->showComments && $this->blog->commentsEnabled()) {
481                         $comments = new COMMENTS($this->currentItem->itemid);
482                         $comments->setItemActions($this);
483                         $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);
484                 }
485         }
486
487         /**
488           * Executes a plugin templatevar
489           *
490           * @param pluginName name of plugin (without the NP_)
491           *
492           * extra parameters can be added
493           */
494         function parse_plugin($pluginName) {
495                 global $manager;
496
497                 // should be already tested from the parser (PARSER.php)
498                 // only continue when the plugin is really installed
499                 /*if (!$manager->pluginInstalled('NP_' . $pluginName))
500                         return;*/
501
502                 $plugin =& $manager->getPlugin('NP_' . $pluginName);
503                 if (!$plugin) return;
504
505                 // get arguments
506                 $params = func_get_args();
507
508                 // remove plugin name
509                 array_shift($params);
510
511                 // add item reference (array_unshift didn't work)
512                 $params = array_merge(array(&$this->currentItem),$params);
513
514                 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
515         }
516
517         /**
518           * Parse templatevar edit
519           */
520         function parse_edit() {
521                 global $member, $CONF;
522                 if ($this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) ) {
523                         $this->parser->parse($this->template['EDITLINK']);
524                 }
525         }
526
527         /**
528           * Parse templatevar editlink
529           */
530         function parse_editlink() {
531                 global $CONF;
532                 echo $CONF['AdminURL'],'bookmarklet.php?action=edit&amp;itemid=',$this->currentItem->itemid;
533         }
534
535         /**
536           * Parse templatevar editpopupcode
537           */
538         function parse_editpopupcode() {
539                 echo "if (event &amp;&amp; event.preventDefault) event.preventDefault();winbm=window.open(this.href,'nucleusbm','scrollbars=yes,width=600,height=550,left=10,top=10,status=yes,resizable=yes');winbm.focus();return false;";
540         }
541
542         // helper functions
543
544         /**
545          * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
546          * from hacking your weblog.
547          * 'plugin variables in items' implementation by Andy
548          */
549         function highlightAndParse(&$data) {
550                 $actions = new BODYACTIONS($this->blog);
551                 $parser = new PARSER($actions->getDefinedActions(), $actions);
552                 $actions->setTemplate($this->template);
553                 $actions->setHighlight($this->strHighlight);
554                 $actions->setCurrentItem($this->currentItem);
555                 //$actions->setParser($parser);
556                 $parser->parse($actions->highlight($data));
557         }
558
559         /*
560         // this is the function previous to the 'plugin variables in items' implementation by Andy
561         function highlightAndParse(&$data) {
562                 // allow only a limited subset of actions (do not allow includes etc, they might be evil)
563                 $this->parser->actions = array('image','media','popup');
564                 $tmp_highlight = $this->highlight($data);
565                 $this->parser->parse($tmp_highlight);
566                 $this->parser->actions = $this->getDefinedActions();
567         }
568         */
569         
570         // function to enable if-else-elseif-elseifnot-ifnot-endif to item template fields
571         
572                 /**
573          * Checks conditions for if statements
574          *
575          * @param string $field type of <%if%>
576          * @param string $name property of field
577          * @param string $value value of property
578          */
579         function checkCondition($field, $name='', $value = '') {
580                 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
581
582                 $condition = 0;
583                 switch($field) {
584                         case 'category':
585                                 $condition = ($blog && $this->_ifCategory($name,$value));
586                                 break;
587                         case 'itemcategory':
588                                 $condition = ($this->_ifItemCategory($name,$value));
589                                 break;
590                         case 'blogsetting':
591                                 $condition = ($blog && ($blog->getSetting($name) == $value));
592                                 break;
593                         case 'itemblogsetting':
594                                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
595                                 $condition = ($b && ($b->getSetting($name) == $value));
596                                 break;
597                         case 'loggedin':
598                                 $condition = $member->isLoggedIn();
599                                 break;
600                         case 'onteam':
601                                 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
602                                 break;
603                         case 'admin':
604                                 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
605                                 break;
606                         case 'author':
607                                 $condition = ($this->_ifAuthor($name,$value));
608                                 break;
609 /*                      case 'nextitem':
610                                 $condition = ($itemidnext != '');
611                                 break;
612                         case 'previtem':
613                                 $condition = ($itemidprev != '');
614                                 break;
615                         case 'archiveprevexists':
616                                 $condition = ($archiveprevexists == true);
617                                 break;
618                         case 'archivenextexists':
619                                 $condition = ($archivenextexists == true);
620                                 break; 
621                         case 'skintype':
622                                 $condition = ($name == $this->skintype);
623                                 break; */
624                         case 'hasplugin':
625                                 $condition = $this->_ifHasPlugin($name, $value);
626                                 break;
627                         default:
628                                 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
629                                 break;
630                 }
631                 return $condition;
632         }       
633         
634         /**
635          *  Different checks for a category
636          */
637         function _ifCategory($name = '', $value='') {
638                 global $blog, $catid;
639
640                 // when no parameter is defined, just check if a category is selected
641                 if (($name != 'catname' && $name != 'catid') || ($value == ''))
642                         return $blog->isValidCategory($catid);
643
644                 // check category name
645                 if ($name == 'catname') {
646                         $value = $blog->getCategoryIdFromName($value);
647                         if ($value == $catid)
648                                 return $blog->isValidCategory($catid);
649                 }
650
651                 // check category id
652                 if (($name == 'catid') && ($value == $catid))
653                         return $blog->isValidCategory($catid);
654
655                 return false;
656         }
657         
658                 
659         /**
660          *  Different checks for an author
661          */
662         function _ifAuthor($name = '', $value='') {
663                 global $member, $manager;
664                 
665                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
666
667                 // when no parameter is defined, just check if author is current visitor
668                 if (($name != 'isadmin' && $name != 'name') || ($name == 'name' && $value == '')) {
669                         return (intval($member->getID()) > 0 && intval($member->getID()) == intval($this->currentItem->authorid));
670                 }
671
672                 // check author name
673                 if ($name == 'name') {
674                         $value = strtolower($value);
675                         if ($value == strtolower($this->currentItem->author))
676                                 return true;
677                 }
678
679                 // check if author is admin
680                 if (($name == 'isadmin')) {                     
681                         $aid = intval($this->currentItem->authorid);
682                         $blogid = intval($b->getID());                  
683                         $amember =& $manager->getMember($aid);
684                         if ($amember->isAdmin())
685                                 return true;
686                                 
687                         return $amember->isBlogAdmin($blogid);
688                 }
689
690                 return false;
691         }
692         
693         /**
694          *  Different checks for a category
695          */
696         function _ifItemCategory($name = '', $value='') {
697                 global $catid, $manager;
698                 
699                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
700
701                 // when no parameter is defined, just check if a category is selected
702                 if (($name != 'catname' && $name != 'catid') || ($value == ''))
703                         return $b->isValidCategory($catid);
704                         
705                 $icatid = $this->currentItem->catid;
706                 //$icategory = $this->currentItem->category;
707
708                 // check category name
709                 if ($name == 'catname') {
710                         $value = $b->getCategoryIdFromName($value);
711                         if ($value == $icatid)
712                                 return $b->isValidCategory($icatid);
713                 }
714
715                 // check category id
716                 if (($name == 'catid') && ($value == $icatid))
717                         return $b->isValidCategory($icatid);
718
719                 return false;
720         }
721
722         
723         /**
724          *  Checks if a member is on the team of a blog and return his rights
725          */
726         function _ifOnTeam($blogName = '') {
727                 global $blog, $member, $manager;
728
729                 // when no blog found
730                 if (($blogName == '') && (!is_object($blog)))
731                         return 0;
732
733                 // explicit blog selection
734                 if ($blogName != '')
735                         $blogid = getBlogIDFromName($blogName);
736
737                 if (($blogName == '') || !$manager->existsBlogID($blogid))
738                         // use current blog
739                         $blogid = $blog->getID();
740
741                 return $member->teamRights($blogid);
742         }
743
744         /**
745          *  Checks if a member is admin of a blog
746          */
747         function _ifAdmin($blogName = '') {
748                 global $blog, $member, $manager;
749
750                 // when no blog found
751                 if (($blogName == '') && (!is_object($blog)))
752                         return 0;
753
754                 // explicit blog selection
755                 if ($blogName != '')
756                         $blogid = getBlogIDFromName($blogName);
757
758                 if (($blogName == '') || !$manager->existsBlogID($blogid))
759                         // use current blog
760                         $blogid = $blog->getID();
761
762                 return $member->isBlogAdmin($blogid);
763         }
764
765         
766         /**
767          *      hasplugin,PlugName
768          *         -> checks if plugin exists
769          *      hasplugin,PlugName,OptionName
770          *         -> checks if the option OptionName from plugin PlugName is not set to 'no'
771          *      hasplugin,PlugName,OptionName=value
772          *         -> checks if the option OptionName from plugin PlugName is set to value
773          */
774         function _ifHasPlugin($name, $value) {
775                 global $manager;
776                 $condition = false;
777                 // (pluginInstalled method won't write a message in the actionlog on failure)
778                 if ($manager->pluginInstalled('NP_'.$name)) {
779                         $plugin =& $manager->getPlugin('NP_' . $name);
780                         if ($plugin != NULL) {
781                                 if ($value == "") {
782                                         $condition = true;
783                                 } else {
784                                         list($name2, $value2) = i18n::explode('=', $value, 2);
785                                         if ($value2 == "" && $plugin->getOption($name2) != 'no') {
786                                                 $condition = true;
787                                         } else if ($plugin->getOption($name2) == $value2) {
788                                                 $condition = true;
789                                         }
790                                 }
791                         }
792                 }
793                 return $condition;
794         }
795
796         /**
797          * Checks if a plugin exists and call its doIf function
798          */
799         function _ifPlugin($name, $key = '', $value = '') {
800                 global $manager;
801
802                 $plugin =& $manager->getPlugin('NP_' . $name);
803                 if (!$plugin) return;
804
805                 $params = func_get_args();
806                 array_shift($params);
807
808                 return call_user_func_array(array(&$plugin, 'doIf'), $params);
809         }
810
811 }
812
813 ?>