OSDN Git Service

CHANGE: Linkクラスを参照せずに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                 if (is_array($this->currentItem)) {
263                         $itemtitle = $this->currentItem['title'];
264                 } elseif (is_object($this->currentItem)) {
265                         $itemtitle = $this->currentItem->title;
266                 }
267                 switch ($format) {
268                         case 'xml':
269 //                              echo stringToXML ($this->currentItem->title);
270                                 echo stringToXML ($itemtitle);
271                                 break;
272                         case 'attribute':
273 //                              echo stringToAttribute ($this->currentItem->title);
274                                 echo stringToAttribute ($itemtitle);
275                                 break;
276                         case 'raw':
277 //                              echo $this->currentItem->title;
278                                 echo $itemtitle;
279                                 break;
280                         default:
281 //                              $this->highlightAndParse($this->currentItem->title);
282                                 $this->highlightAndParse($itemtitle);
283                                 break;
284                 }
285         }
286
287         /**
288          * Parse templatevar karma
289          */
290         function parse_karma($type = 'totalscore') {
291                 global $manager;
292
293                 // get karma object
294                 $karma =& $manager->getKarma($this->currentItem->itemid);
295
296                 switch($type) {
297                         case 'pos':
298                                 echo $karma->getNbPosVotes();
299                                 break;
300                         case 'neg':
301                                 echo $karma->getNbNegVotes();
302                                 break;
303                         case 'votes':
304                                 echo $karma->getNbOfVotes();
305                                 break;
306                         case 'posp':
307                                 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
308                                 echo number_format($percentage,2), '%';
309                                 break;
310                         case 'negp':
311                                 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
312                                 echo number_format($percentage,2), '%';
313                                 break;
314                         case 'totalscore':
315                         default:
316                                 echo $karma->getTotalScore();
317                                 break;
318                 }
319
320         }
321
322         /**
323          * Parse templatevar author
324          */
325         function parse_author($which = '') {
326                 switch($which)
327                 {
328                         case 'realname':
329                                 echo $this->currentItem->authorname;
330                                 break;
331                         case 'id':
332                                 echo $this->currentItem->authorid;
333                                 break;
334                         case 'email':
335                                 echo $this->currentItem->authormail;
336                                 break;
337                         case 'url':
338                                 echo $this->currentItem->authorurl;
339                                 break;
340                         case 'name':
341                         default:
342                                 echo $this->currentItem->author;
343                 }
344         }
345
346         /**
347          * Parse templatevar smartbody
348          */
349         function parse_smartbody() {
350                 if (!$this->currentItem->more) {
351                         $this->highlightAndParse($this->currentItem->body);
352                 } else {
353                         $this->highlightAndParse($this->currentItem->more);
354                 }
355         }
356
357         /**
358          * Parse templatevar morelink
359          */
360         function parse_morelink() {
361                 if ($this->currentItem->more)
362                         $this->parser->parse($this->template['MORELINK']);
363         }
364
365         /**
366          * Parse templatevar date
367          *
368          * @param format optional strftime format
369          */
370         function parse_date($format = '') {
371                 if (!isset($this->template['FORMAT_DATE'])) $this->template['FORMAT_DATE'] = '';
372                 echo formatDate($format, $this->currentItem->timestamp, $this->template['FORMAT_DATE'], $this->blog);
373         }
374
375         /**
376           * Parse templatevar time
377           *
378           * @param format optional strftime format
379           */
380         function parse_time($format = '') {
381                 if (!isset($this->template['FORMAT_TIME'])) $this->template['FORMAT_TIME'] = '';
382                 echo i18n::strftime($format ? $format : $this->template['FORMAT_TIME'],$this->currentItem->timestamp);
383         }
384
385         /**
386           * Parse templatevar syndicate_title
387           *
388           * @param maxLength optional maximum length
389           */
390         function parse_syndicate_title($maxLength = 100) {
391                 $syndicated = strip_tags($this->currentItem->title);
392                 echo ENTITY::hsc(shorten($syndicated,$maxLength,'...'));
393         }
394
395         /**
396           * Parse templatevar syndicate_description
397           *
398           * @param maxLength optional maximum length
399           */
400         function parse_syndicate_description($maxLength = 250, $addHighlight = 0) {
401                 $syndicated = strip_tags($this->currentItem->body);
402                 if ($addHighlight) {
403                         $tmp_highlight = ENTITY::hsc(shorten($syndicated,$maxLength,'...'));
404                         echo $this->highlightAndParse($tmp_highlight);
405                 } else {
406                         echo ENTITY::hsc(shorten($syndicated,$maxLength,'...'));
407                 }
408         }
409
410         /**
411           * Parse templatevar karmaposlink
412           *
413           * @param string text
414           */
415         function parse_karmaposlink($text = '') {
416                 global $CONF;
417                 $link = $CONF['ActionURL'] . '?action=votepositive&amp;itemid='.$this->currentItem->itemid;
418                 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
419         }
420
421         /**
422           * Parse templatevar karmaneglink
423           *
424           * @param string text
425           */
426         function parse_karmaneglink($text = '') {
427                 global $CONF;
428                 $link = $CONF['ActionURL'] . '?action=votenegative&amp;itemid='.$this->currentItem->itemid;
429                 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
430         }
431
432         /**
433           * Parse templatevar new
434           */
435         function parse_new() {
436                 if (($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit))
437                         echo $this->template['NEW'];
438         }
439
440         /**
441           * Parse templatevar daylink
442           */
443         function parse_daylink() {
444                 echo Link::create_archive_link($this->blog->getID(), i18n::strftime('%Y-%m-%d',$this->currentItem->timestamp), $this->linkparams);
445         }
446
447         /**
448           * Parse templatevar comments
449           */
450         function parse_comments($maxToShow = 0) {
451                 if ($maxToShow == 0)
452                         $maxToShow = $this->blog->getMaxComments();
453
454                 // add comments
455                 if ($this->showComments && $this->blog->commentsEnabled()) {
456                         $comments = new COMMENTS($this->currentItem->itemid);
457                         $comments->setItemActions($this);
458                         $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);
459                 }
460         }
461
462         /**
463           * Executes a plugin templatevar
464           *
465           * @param pluginName name of plugin (without the NP_)
466           *
467           * extra parameters can be added
468           */
469         function parse_plugin($pluginName) {
470                 global $manager;
471
472                 // should be already tested from the parser (PARSER.php)
473                 // only continue when the plugin is really installed
474                 /*if (!$manager->pluginInstalled('NP_' . $pluginName))
475                         return;*/
476
477                 $plugin =& $manager->getPlugin('NP_' . $pluginName);
478                 if (!$plugin) return;
479
480                 // get arguments
481                 $params = func_get_args();
482
483                 // remove plugin name
484                 array_shift($params);
485
486                 // add item reference (array_unshift didn't work)
487                 $params = array_merge(array(&$this->currentItem),$params);
488
489                 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
490         }
491
492         /**
493           * Parse templatevar edit
494           */
495         function parse_edit() {
496                 global $member, $CONF;
497                 if ($this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) ) {
498                         $this->parser->parse($this->template['EDITLINK']);
499                 }
500         }
501
502         /**
503           * Parse templatevar editlink
504           */
505         function parse_editlink() {
506                 global $CONF;
507                 echo $CONF['AdminURL'],'bookmarklet.php?action=edit&amp;itemid=',$this->currentItem->itemid;
508         }
509
510         /**
511           * Parse templatevar editpopupcode
512           */
513         function parse_editpopupcode() {
514                 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;";
515         }
516
517         // helper functions
518
519         /**
520          * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
521          * from hacking your weblog.
522          * 'plugin variables in items' implementation by Andy
523          */
524         function highlightAndParse(&$data) {
525                 $actions = new BODYACTIONS($this->blog);
526                 $parser = new PARSER($actions->getDefinedActions(), $actions);
527                 $actions->setTemplate($this->template);
528                 $actions->setHighlight($this->strHighlight);
529                 $actions->setCurrentItem($this->currentItem);
530                 //$actions->setParser($parser);
531                 $parser->parse($actions->highlight($data));
532         }
533
534         /*
535         // this is the function previous to the 'plugin variables in items' implementation by Andy
536         function highlightAndParse(&$data) {
537                 // allow only a limited subset of actions (do not allow includes etc, they might be evil)
538                 $this->parser->actions = array('image','media','popup');
539                 $tmp_highlight = $this->highlight($data);
540                 $this->parser->parse($tmp_highlight);
541                 $this->parser->actions = $this->getDefinedActions();
542         }
543         */
544         
545         // function to enable if-else-elseif-elseifnot-ifnot-endif to item template fields
546         
547                 /**
548          * Checks conditions for if statements
549          *
550          * @param string $field type of <%if%>
551          * @param string $name property of field
552          * @param string $value value of property
553          */
554         function checkCondition($field, $name='', $value = '') {
555                 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
556
557                 $condition = 0;
558                 switch($field) {
559                         case 'category':
560                                 $condition = ($blog && $this->_ifCategory($name,$value));
561                                 break;
562                         case 'itemcategory':
563                                 $condition = ($this->_ifItemCategory($name,$value));
564                                 break;
565                         case 'blogsetting':
566                                 $condition = ($blog && ($blog->getSetting($name) == $value));
567                                 break;
568                         case 'itemblogsetting':
569                                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
570                                 $condition = ($b && ($b->getSetting($name) == $value));
571                                 break;
572                         case 'loggedin':
573                                 $condition = $member->isLoggedIn();
574                                 break;
575                         case 'onteam':
576                                 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
577                                 break;
578                         case 'admin':
579                                 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
580                                 break;
581                         case 'author':
582                                 $condition = ($this->_ifAuthor($name,$value));
583                                 break;
584 /*                      case 'nextitem':
585                                 $condition = ($itemidnext != '');
586                                 break;
587                         case 'previtem':
588                                 $condition = ($itemidprev != '');
589                                 break;
590                         case 'archiveprevexists':
591                                 $condition = ($archiveprevexists == true);
592                                 break;
593                         case 'archivenextexists':
594                                 $condition = ($archivenextexists == true);
595                                 break; 
596                         case 'skintype':
597                                 $condition = ($name == $this->skintype);
598                                 break; */
599                         case 'hasplugin':
600                                 $condition = $this->_ifHasPlugin($name, $value);
601                                 break;
602                         default:
603                                 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
604                                 break;
605                 }
606                 return $condition;
607         }       
608         
609         /**
610          *  Different checks for a category
611          */
612         function _ifCategory($name = '', $value='') {
613                 global $blog, $catid;
614
615                 // when no parameter is defined, just check if a category is selected
616                 if (($name != 'catname' && $name != 'catid') || ($value == ''))
617                         return $blog->isValidCategory($catid);
618
619                 // check category name
620                 if ($name == 'catname') {
621                         $value = $blog->getCategoryIdFromName($value);
622                         if ($value == $catid)
623                                 return $blog->isValidCategory($catid);
624                 }
625
626                 // check category id
627                 if (($name == 'catid') && ($value == $catid))
628                         return $blog->isValidCategory($catid);
629
630                 return false;
631         }
632         
633                 
634         /**
635          *  Different checks for an author
636          */
637         function _ifAuthor($name = '', $value='') {
638                 global $member, $manager;
639                 
640                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
641
642                 // when no parameter is defined, just check if author is current visitor
643                 if (($name != 'isadmin' && $name != 'name') || ($name == 'name' && $value == '')) {
644                         return (intval($member->getID()) > 0 && intval($member->getID()) == intval($this->currentItem->authorid));
645                 }
646
647                 // check author name
648                 if ($name == 'name') {
649                         $value = strtolower($value);
650                         if ($value == strtolower($this->currentItem->author))
651                                 return true;
652                 }
653
654                 // check if author is admin
655                 if (($name == 'isadmin')) {                     
656                         $aid = intval($this->currentItem->authorid);
657                         $blogid = intval($b->getID());                  
658                         $amember =& $manager->getMember($aid);
659                         if ($amember->isAdmin())
660                                 return true;
661                                 
662                         return $amember->isBlogAdmin($blogid);
663                 }
664
665                 return false;
666         }
667         
668         /**
669          *  Different checks for a category
670          */
671         function _ifItemCategory($name = '', $value='') {
672                 global $catid, $manager;
673                 
674                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
675
676                 // when no parameter is defined, just check if a category is selected
677                 if (($name != 'catname' && $name != 'catid') || ($value == ''))
678                         return $b->isValidCategory($catid);
679                         
680                 $icatid = $this->currentItem->catid;
681                 //$icategory = $this->currentItem->category;
682
683                 // check category name
684                 if ($name == 'catname') {
685                         $value = $b->getCategoryIdFromName($value);
686                         if ($value == $icatid)
687                                 return $b->isValidCategory($icatid);
688                 }
689
690                 // check category id
691                 if (($name == 'catid') && ($value == $icatid))
692                         return $b->isValidCategory($icatid);
693
694                 return false;
695         }
696
697         
698         /**
699          *  Checks if a member is on the team of a blog and return his rights
700          */
701         function _ifOnTeam($blogName = '') {
702                 global $blog, $member, $manager;
703
704                 // when no blog found
705                 if (($blogName == '') && (!is_object($blog)))
706                         return 0;
707
708                 // explicit blog selection
709                 if ($blogName != '')
710                         $blogid = getBlogIDFromName($blogName);
711
712                 if (($blogName == '') || !$manager->existsBlogID($blogid))
713                         // use current blog
714                         $blogid = $blog->getID();
715
716                 return $member->teamRights($blogid);
717         }
718
719         /**
720          *  Checks if a member is admin of a blog
721          */
722         function _ifAdmin($blogName = '') {
723                 global $blog, $member, $manager;
724
725                 // when no blog found
726                 if (($blogName == '') && (!is_object($blog)))
727                         return 0;
728
729                 // explicit blog selection
730                 if ($blogName != '')
731                         $blogid = getBlogIDFromName($blogName);
732
733                 if (($blogName == '') || !$manager->existsBlogID($blogid))
734                         // use current blog
735                         $blogid = $blog->getID();
736
737                 return $member->isBlogAdmin($blogid);
738         }
739
740         
741         /**
742          *      hasplugin,PlugName
743          *         -> checks if plugin exists
744          *      hasplugin,PlugName,OptionName
745          *         -> checks if the option OptionName from plugin PlugName is not set to 'no'
746          *      hasplugin,PlugName,OptionName=value
747          *         -> checks if the option OptionName from plugin PlugName is set to value
748          */
749         function _ifHasPlugin($name, $value) {
750                 global $manager;
751                 $condition = false;
752                 // (pluginInstalled method won't write a message in the actionlog on failure)
753                 if ($manager->pluginInstalled('NP_'.$name)) {
754                         $plugin =& $manager->getPlugin('NP_' . $name);
755                         if ($plugin != NULL) {
756                                 if ($value == "") {
757                                         $condition = true;
758                                 } else {
759                                         list($name2, $value2) = i18n::explode('=', $value, 2);
760                                         if ($value2 == "" && $plugin->getOption($name2) != 'no') {
761                                                 $condition = true;
762                                         } else if ($plugin->getOption($name2) == $value2) {
763                                                 $condition = true;
764                                         }
765                                 }
766                         }
767                 }
768                 return $condition;
769         }
770
771         /**
772          * Checks if a plugin exists and call its doIf function
773          */
774         function _ifPlugin($name, $key = '', $value = '') {
775                 global $manager;
776
777                 $plugin =& $manager->getPlugin('NP_' . $name);
778                 if (!$plugin) return;
779
780                 $params = func_get_args();
781                 array_shift($params);
782
783                 return call_user_func_array(array(&$plugin, 'doIf'), $params);
784         }
785
786 }
787
788 ?>