OSDN Git Service

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