OSDN Git Service

f069a5d91d622eaf013322e32cdd928338658004
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / 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$
18  * @version $NucleusJP: ITEMACTIONS.php,v 1.5.2.3 2007/10/30 19:05:20 kmorimatsu Exp $
19  */
20 class ITEMACTIONS extends BaseActions {\r
21 \r
22         // contains an assoc array with parameters that need to be included when\r
23         // generating links to items/archives/... (e.g. catid)\r
24         var $linkparams;\r
25 \r
26         // true when the current user is a blog admin (and thus allowed to edit all items)\r
27         var $allowEditAll;\r
28 \r
29         // timestamp of last visit\r
30         var $lastVisit;\r
31 \r
32         // item currently being handled (mysql result object, see BLOG::showUsingQuery)\r
33         var $currentItem;\r
34 \r
35         // reference to the blog currently being displayed\r
36         var $blog;\r
37 \r
38         // associative array with template info (part name => contents)\r
39         var $template;\r
40 \r
41         // true when comments need to be displayed\r
42         var $showComments;\r
43 \r
44         function ITEMACTIONS(&$blog) {\r
45                 // call constructor of superclass first\r
46                 $this->BaseActions();\r
47 \r
48                 // extra parameters for created links\r
49                 global $catid;\r
50                 if ($catid)\r
51                         $this->linkparams = array('catid' => $catid);\r
52 \r
53                 // check if member is blog admin (and thus allowed to edit all items)\r
54                 global $member;\r
55                 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));\r
56                 $this->setBlog($blog);\r
57         }\r
58 \r
59         /**\r
60           * Returns an array with the actions that are defined\r
61           * in the ITEMACTIONS class \r
62           */\r
63         function getDefinedActions() {\r
64                 return array(\r
65                         'blogid',\r
66                         'title',\r
67                         'body',\r
68                         'more',\r
69                         'smartbody',\r
70                         'itemid',\r
71                         'morelink',\r
72                         'category',\r
73                         'categorylink',\r
74                         'author',\r
75                         'authorid',\r
76                         'authorlink',\r
77                         'catid',\r
78                         'karma',\r
79                         'date',\r
80                         'time',\r
81                         'query',\r
82                         'itemlink',\r
83                         'blogurl',\r
84                         'closed',\r
85                         'syndicate_title',\r
86                         'syndicate_description',\r
87                         'karmaposlink',\r
88                         'karmaneglink',\r
89                         'new',\r
90                         'image',\r
91                         'popup',\r
92                         'media',\r
93                         'daylink',\r
94                         'query',\r
95                         'include',\r
96                         'phpinclude',\r
97                         'parsedinclude',\r
98                         'skinfile',\r
99                         'set',\r
100                         'plugin',\r
101                         'edit',\r
102                         'editlink',\r
103                         'editpopupcode',\r
104                         'comments',\r
105                         'relevance'/*,\r
106                         'if',\r
107                         'else',\r
108                         'endif',\r
109                         'elseif',\r
110                         'ifnot',\r
111                         'elseifnot'*/\r
112                 );\r
113         }\r
114 \r
115         function setLastVisit($lastVisit) {\r
116                 $this->lastVisit = $lastVisit;\r
117         }\r
118         \r
119         function setParser(&$parser) {\r
120                 $this->parser =& $parser;\r
121         }\r
122         \r
123         function setCurrentItem(&$item) {\r
124                 $this->currentItem =& $item;\r
125         }\r
126         \r
127         function setBlog(&$blog) {\r
128                 $this->blog =& $blog;\r
129         }\r
130         \r
131         function setTemplate($template) {\r
132                 $this->template =& $template;\r
133         }\r
134         \r
135         function setShowComments($val) {\r
136                 $this->showComments = $val;\r
137         }\r
138 \r
139         // methods used by parser to insert content\r
140 \r
141 \r
142         /**\r
143          * Parse templatevar blogid\r
144          */\r
145         function parse_blogid() {\r
146                 echo $this->blog->getID();\r
147         }\r
148         \r
149         /**\r
150          * Parse templatevar body\r
151          */\r
152         function parse_body() {\r
153                 $this->highlightAndParse($this->currentItem->body);\r
154         }\r
155         \r
156         /**\r
157          * Parse templatevar more\r
158          */\r
159         function parse_more() {\r
160                 $this->highlightAndParse($this->currentItem->more);\r
161         }\r
162         \r
163         /**\r
164          * Parse templatevar itemid\r
165          */\r
166         function parse_itemid() {\r
167                 echo $this->currentItem->itemid;\r
168         }\r
169 \r
170         /**\r
171          * Parse templatevar category\r
172          */     \r
173         function parse_category() {\r
174                 echo $this->currentItem->category;\r
175         }\r
176         \r
177         /**\r
178          * Parse templatevar categorylink\r
179          */\r
180         function parse_categorylink() {\r
181                 echo createLink('category', array('catid' => $this->currentItem->catid, 'name' => $this->currentItem->category));\r
182         }\r
183         \r
184         /**\r
185          * Parse templatevar catid\r
186          */\r
187         function parse_catid() {\r
188                 echo $this->currentItem->catid;\r
189         }\r
190         \r
191         /**\r
192          * Parse templatevar authorid\r
193          */\r
194         function parse_authorid() {\r
195                 echo $this->currentItem->authorid;\r
196         }\r
197         \r
198         /**\r
199          * Parse templatevar authorlink\r
200          */\r
201         function parse_authorlink() {\r
202                 echo createLink(\r
203                         'member',\r
204                         array(\r
205                                 'memberid' => $this->currentItem->authorid,\r
206                                 'name' => $this->currentItem->author,\r
207                                 'extra' => $this->linkparams\r
208                         )\r
209                 );\r
210         }\r
211         \r
212         /**\r
213          * Parse templatevar query\r
214          */\r
215         function parse_query() {\r
216                 echo $this->strHighlight;\r
217         }\r
218         \r
219         /**\r
220          * Parse templatevar itemlink\r
221          */\r
222         function parse_itemlink() {\r
223                 echo createLink(\r
224                         'item',\r
225                         array(\r
226                                 'itemid' => $this->currentItem->itemid,\r
227                                 'title' => $this->currentItem->title,\r
228                                 'timestamp' => $this->currentItem->timestamp,\r
229                                 'extra' => $this->linkparams\r
230                         )\r
231                 );\r
232         }\r
233         \r
234         /**\r
235          * Parse templatevar blogurl\r
236          */\r
237         function parse_blogurl() {\r
238                 echo $this->blog->getURL();\r
239         }\r
240         \r
241         /**\r
242          * Parse templatevar closed\r
243          */\r
244         function parse_closed() {\r
245                 echo $this->currentItem->closed;\r
246         }\r
247         \r
248         /**\r
249          * Parse templatevar relevance\r
250          */\r
251         function parse_relevance() {\r
252                 echo round($this->currentItem->score,2);\r
253         }\r
254 \r
255         /**\r
256          * Parse templatevar title\r
257          * \r
258          * @param string $format defines in which format the title is shown              \r
259          */\r
260         function parse_title($format = '') {\r
261                 if (is_array($this->currentItem)) {\r
262                         $itemtitle = $this->currentItem['title'];\r
263                 } elseif (is_object($this->currentItem)) {\r
264                         $itemtitle = $this->currentItem->title;\r
265                 }\r
266                 switch ($format) {\r
267                         case 'xml':\r
268 //                              echo stringToXML ($this->currentItem->title);\r
269                                 echo stringToXML ($itemtitle);\r
270                                 break;\r
271                         case 'attribute':\r
272 //                              echo stringToAttribute ($this->currentItem->title);\r
273                                 echo stringToAttribute ($itemtitle);\r
274                                 break;\r
275                         case 'raw':\r
276 //                              echo $this->currentItem->title;\r
277                                 echo $itemtitle;\r
278                                 break;\r
279                         default:\r
280 //                              $this->highlightAndParse($this->currentItem->title);\r
281                                 $this->highlightAndParse($itemtitle);\r
282                                 break;\r
283                 }\r
284         }\r
285 \r
286         /**\r
287          * Parse templatevar karma\r
288          */\r
289         function parse_karma($type = 'totalscore') {\r
290                 global $manager;\r
291 \r
292                 // get karma object\r
293                 $karma =& $manager->getKarma($this->currentItem->itemid);\r
294 \r
295                 switch($type) {\r
296                         case 'pos':\r
297                                 echo $karma->getNbPosVotes();\r
298                                 break;\r
299                         case 'neg':\r
300                                 echo $karma->getNbNegVotes();\r
301                                 break;\r
302                         case 'votes':\r
303                                 echo $karma->getNbOfVotes();\r
304                                 break;\r
305                         case 'posp':\r
306                                 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;\r
307                                 echo number_format($percentage,2), '%';\r
308                                 break;\r
309                         case 'negp':\r
310                                 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;\r
311                                 echo number_format($percentage,2), '%';\r
312                                 break;\r
313                         case 'totalscore':\r
314                         default:\r
315                                 echo $karma->getTotalScore();\r
316                                 break;\r
317                 }\r
318 \r
319         }\r
320 \r
321         /**\r
322          * Parse templatevar author\r
323          */\r
324         function parse_author($which = '') {\r
325                 switch($which)\r
326                 {\r
327                         case 'realname':\r
328                                 echo $this->currentItem->authorname;\r
329                                 break;\r
330                         case 'id':\r
331                                 echo $this->currentItem->authorid;\r
332                                 break;\r
333                         case 'email':\r
334                                 echo $this->currentItem->authormail;\r
335                                 break;\r
336                         case 'url':\r
337                                 echo $this->currentItem->authorurl;\r
338                                 break;\r
339                         case 'name':\r
340                         default:\r
341                                 echo $this->currentItem->author;\r
342                 }\r
343         }\r
344 \r
345         /**\r
346          * Parse templatevar smartbody\r
347          */\r
348         function parse_smartbody() {\r
349                 if (!$this->currentItem->more) {\r
350                         $this->highlightAndParse($this->currentItem->body);\r
351                 } else {\r
352                         $this->highlightAndParse($this->currentItem->more);\r
353                 }\r
354         }\r
355 \r
356         /**\r
357          * Parse templatevar morelink\r
358          */\r
359         function parse_morelink() {\r
360                 if ($this->currentItem->more)\r
361                         $this->parser->parse($this->template['MORELINK']);\r
362         }\r
363 \r
364         /**\r
365          * Parse templatevar date\r
366          * \r
367          * @param format optional strftime format                \r
368          */\r
369         function parse_date($format = '') {\r
370                 echo formatDate($format, $this->currentItem->timestamp, $this->template['FORMAT_DATE'], $this->blog);\r
371         }\r
372 \r
373         /**\r
374           * Parse templatevar time\r
375           *       \r
376           * @param format optional strftime format\r
377           */\r
378         function parse_time($format = '') {\r
379                 echo strftime($format ? $format : $this->template['FORMAT_TIME'],$this->currentItem->timestamp);\r
380         }\r
381 \r
382         /**\r
383           * Parse templatevar syndicate_title\r
384           *\r
385           * @param maxLength optional maximum length\r
386           */\r
387         function parse_syndicate_title($maxLength = 100) {\r
388                 $syndicated = strip_tags($this->currentItem->title);\r
389                 echo htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);\r
390         }\r
391 \r
392         /**\r
393           * Parse templatevar syndicate_description\r
394           *\r
395           * @param maxLength optional maximum length\r
396           */\r
397         function parse_syndicate_description($maxLength = 250, $addHighlight = 0) {\r
398                 $syndicated = strip_tags($this->currentItem->body);\r
399                 if ($addHighlight) {\r
400                         $tmp_highlight = htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);\r
401                         echo $this->highlightAndParse($tmp_highlight);\r
402                 } else {\r
403                         echo htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);\r
404                 }\r
405         }\r
406 \r
407         /**\r
408           * Parse templatevar karmaposlink\r
409           *\r
410           * @param string text\r
411           */\r
412         function parse_karmaposlink($text = '') {\r
413                 global $CONF;\r
414                 $link = $CONF['ActionURL'] . '?action=votepositive&amp;itemid='.$this->currentItem->itemid;\r
415                 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;\r
416         }\r
417 \r
418         /**\r
419           * Parse templatevar karmaneglink\r
420           *\r
421           * @param string text\r
422           */\r
423         function parse_karmaneglink($text = '') {\r
424                 global $CONF;\r
425                 $link = $CONF['ActionURL'] . '?action=votenegative&amp;itemid='.$this->currentItem->itemid;\r
426                 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;\r
427         }\r
428 \r
429         /**\r
430           * Parse templatevar new\r
431           */\r
432         function parse_new() {\r
433                 if (($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit))\r
434                         echo $this->template['NEW'];\r
435         }\r
436 \r
437         /**\r
438           * Parse templatevar daylink\r
439           */\r
440         function parse_daylink() {\r
441                 echo createArchiveLink($this->blog->getID(), strftime('%Y-%m-%d',$this->currentItem->timestamp), $this->linkparams);\r
442         }\r
443 \r
444         /**\r
445           * Parse templatevar comments\r
446           */\r
447         function parse_comments($maxToShow = 0) {\r
448                 if ($maxToShow == 0)\r
449                         $maxToShow = $this->blog->getMaxComments();\r
450 \r
451                 // add comments\r
452                 if ($this->showComments && $this->blog->commentsEnabled()) {\r
453                         $comments =& new COMMENTS($this->currentItem->itemid);\r
454                         $comments->setItemActions($this);\r
455                         $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);\r
456                 }\r
457         }\r
458 \r
459         /**\r
460           * Executes a plugin templatevar\r
461           *\r
462           * @param pluginName name of plugin (without the NP_)\r
463           *\r
464           * extra parameters can be added\r
465           */\r
466         function parse_plugin($pluginName) {\r
467                 global $manager;\r
468 \r
469                 // should be already tested from the parser (PARSER.php)\r
470                 // only continue when the plugin is really installed\r
471                 /*if (!$manager->pluginInstalled('NP_' . $pluginName))\r
472                         return;*/\r
473 \r
474                 $plugin =& $manager->getPlugin('NP_' . $pluginName);\r
475                 if (!$plugin) return;\r
476 \r
477                 // get arguments\r
478                 $params = func_get_args();\r
479 \r
480                 // remove plugin name\r
481                 array_shift($params);\r
482 \r
483                 // add item reference (array_unshift didn't work)\r
484                 $params = array_merge(array(&$this->currentItem),$params);\r
485 \r
486                 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);\r
487         }\r
488 \r
489         /**\r
490           * Parse templatevar edit\r
491           */\r
492         function parse_edit() {\r
493                 global $member, $CONF;\r
494                 if ($this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) ) {\r
495                         $this->parser->parse($this->template['EDITLINK']);\r
496                 }\r
497         }\r
498 \r
499         /**\r
500           * Parse templatevar editlink\r
501           */\r
502         function parse_editlink() {\r
503                 global $CONF;\r
504                 echo $CONF['AdminURL'],'bookmarklet.php?action=edit&amp;itemid=',$this->currentItem->itemid;\r
505         }\r
506 \r
507         /**\r
508           * Parse templatevar editpopupcode\r
509           */\r
510         function parse_editpopupcode() {\r
511                 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;";\r
512         }\r
513 \r
514         // helper functions\r
515 \r
516         /**\r
517          * Parses highlighted text, with limited actions only (to prevent not fully trusted team members\r
518          * from hacking your weblog.\r
519          * 'plugin variables in items' implementation by Andy\r
520          */\r
521         function highlightAndParse(&$data) {\r
522                 $actions =& new BODYACTIONS($this->blog);\r
523                 $parser =& new PARSER($actions->getDefinedActions(), $actions);\r
524                 $actions->setTemplate($this->template);\r
525                 $actions->setHighlight($this->strHighlight);\r
526                 $actions->setCurrentItem($this->currentItem);\r
527                 //$actions->setParser($parser);\r
528                 $parser->parse($actions->highlight($data));\r
529         }\r
530 \r
531         /*\r
532         // this is the function previous to the 'plugin variables in items' implementation by Andy\r
533         function highlightAndParse(&$data) {\r
534                 // allow only a limited subset of actions (do not allow includes etc, they might be evil)\r
535                 $this->parser->actions = array('image','media','popup');\r
536                 $tmp_highlight = $this->highlight($data);\r
537                 $this->parser->parse($tmp_highlight);\r
538                 $this->parser->actions = $this->getDefinedActions();\r
539         }\r
540         */\r
541 \r
542 }\r
543 \r
544 ?>