3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2007 The Nucleus Group
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 * (see nucleus/documentation/index.html#license for more info)
13 * This class to parse item templates
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 $
20 class ITEMACTIONS extends BaseActions {
22 // contains an assoc array with parameters that need to be included when
23 // generating links to items/archives/... (e.g. catid)
26 // true when the current user is a blog admin (and thus allowed to edit all items)
29 // timestamp of last visit
32 // item currently being handled (mysql result object, see BLOG::showUsingQuery)
35 // reference to the blog currently being displayed
38 // associative array with template info (part name => contents)
41 // true when comments need to be displayed
44 function ITEMACTIONS(&$blog) {
45 // call constructor of superclass first
48 // extra parameters for created links
51 $this->linkparams = array('catid' => $catid);
53 // check if member is blog admin (and thus allowed to edit all items)
55 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));
56 $this->setBlog($blog);
59 function getDefinedActions() {
82 'syndicate_description',
111 function setLastVisit($lastVisit) {
112 $this->lastVisit = $lastVisit;
115 function setParser(&$parser) {
116 $this->parser =& $parser;
119 function setCurrentItem(&$item) {
120 $this->currentItem =& $item;
123 function setBlog(&$blog) {
124 $this->blog =& $blog;
127 function setTemplate($template) {
128 $this->template =& $template;
131 function setShowComments($val) {
132 $this->showComments = $val;
135 // methods used by parser to insert content
137 function parse_blogid() {
138 echo $this->blog->getID();
141 function parse_body() {
142 $this->highlightAndParse($this->currentItem->body);
145 function parse_more() {
146 $this->highlightAndParse($this->currentItem->more);
149 function parse_itemid() {
150 echo $this->currentItem->itemid;
153 function parse_category() {
154 echo $this->currentItem->category;
157 function parse_categorylink() {
158 echo createLink('category', array('catid' => $this->currentItem->catid, 'name' => $this->currentItem->category));
161 function parse_catid() {
162 echo $this->currentItem->catid;
165 function parse_authorid() {
166 echo $this->currentItem->authorid;
169 function parse_authorlink() {
173 'memberid' => $this->currentItem->authorid,
174 'name' => $this->currentItem->author,
175 'extra' => $this->linkparams
180 function parse_query() {
181 echo $this->strHighlight;
184 function parse_itemlink() {
188 'itemid' => $this->currentItem->itemid,
189 'title' => $this->currentItem->title,
190 'timestamp' => $this->currentItem->timestamp,
191 'extra' => $this->linkparams
196 function parse_blogurl() {
197 echo $this->blog->getURL();
200 function parse_closed() {
201 echo $this->currentItem->closed;
204 function parse_relevance() {
205 echo round($this->currentItem->score,2);
208 function parse_title($format = '') {
211 echo stringToXML ($this->currentItem->title);
214 echo stringToAttribute ($this->currentItem->title);
217 echo $this->currentItem->title;
220 $this->highlightAndParse($this->currentItem->title);
225 function parse_karma($type = 'totalscore') {
229 $karma =& $manager->getKarma($this->currentItem->itemid);
233 echo $karma->getNbPosVotes();
236 echo $karma->getNbNegVotes();
239 echo $karma->getNbOfVotes();
242 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
243 echo number_format($percentage,2), '%';
246 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
247 echo number_format($percentage,2), '%';
251 echo $karma->getTotalScore();
257 function parse_author($which = '') {
261 echo $this->currentItem->authorname;
264 echo $this->currentItem->authorid;
267 echo $this->currentItem->authormail;
270 echo $this->currentItem->authorurl;
274 echo $this->currentItem->author;
278 function parse_smartbody() {
279 if (!$this->currentItem->more) {
280 $this->highlightAndParse($this->currentItem->body);
282 $this->highlightAndParse($this->currentItem->more);
286 function parse_morelink() {
287 if ($this->currentItem->more)
288 $this->parser->parse($this->template['MORELINK']);
291 function parse_date($format = '') {
292 echo formatDate($format, $this->currentItem->timestamp, $this->template['FORMAT_DATE'], $this->blog);
296 * @param format optional strftime format
298 function parse_time($format = '') {
299 echo strftime($format ? $format : $this->template['FORMAT_TIME'],$this->currentItem->timestamp);
303 * @param maxLength optional maximum length
305 function parse_syndicate_title($maxLength = 100) {
306 $syndicated = strip_tags($this->currentItem->title);
307 echo htmlspecialchars(shorten($syndicated,$maxLength,'...'));
311 * @param maxLength optional maximum length
313 function parse_syndicate_description($maxLength = 250, $addHighlight = 0) {
314 $syndicated = strip_tags($this->currentItem->body);
316 $tmp_highlight = htmlspecialchars(shorten($syndicated,$maxLength,'...'));
317 echo $this->highlightAndParse($tmp_highlight);
319 echo htmlspecialchars(shorten($syndicated,$maxLength,'...'));
323 function parse_karmaposlink($text = '') {
325 $link = $CONF['ActionURL'] . '?action=votepositive&itemid='.$this->currentItem->itemid;
326 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
329 function parse_karmaneglink($text = '') {
331 $link = $CONF['ActionURL'] . '?action=votenegative&itemid='.$this->currentItem->itemid;
332 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
335 function parse_new() {
336 if (($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit))
337 echo $this->template['NEW'];
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);
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);
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);
361 function parse_daylink() {
362 echo createArchiveLink($this->blog->getID(), strftime('%Y-%m-%d',$this->currentItem->timestamp), $this->linkparams);
365 function parse_comments($maxToShow = 0) {
367 $maxToShow = $this->blog->getMaxComments();
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);
378 * Executes a plugin templatevar
380 * @param pluginName name of plugin (without the NP_)
382 * extra parameters can be added
384 function parse_plugin($pluginName) {
387 // only continue when the plugin is really installed
388 if (!$manager->pluginInstalled('NP_' . $pluginName))
391 $plugin =& $manager->getPlugin('NP_' . $pluginName);
392 if (!$plugin) return;
395 $params = func_get_args();
397 // remove plugin name
398 array_shift($params);
400 // add item reference (array_unshift didn't work)
401 $params = array_merge(array(&$this->currentItem),$params);
403 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
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']);
413 function parse_editlink() {
415 echo $CONF['AdminURL'],'bookmarklet.php?action=edit&itemid=',$this->currentItem->itemid;
418 function parse_editpopupcode() {
419 echo "if (event && 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;";
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
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));
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();
450 function createPopupCode($filename, $width, $height, $text = '') {
453 // select private collection when no collection given
454 if (!strstr($filename,'/')) {
455 $filename = $this->currentItem->authorid . '/' . $filename;
458 $windowwidth = $width;
459 $windowheight = $height;
461 $vars['rawpopuplink'] = $CONF['Self'] . "?imagepopup=" . htmlspecialchars($filename) . "&width=$width&height=$height&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>';
471 echo TEMPLATE::fill($this->template['POPUP_CODE'],$vars);
474 function createImageCode($filename, $width, $height, $text = '') {
477 // select private collection when no collection given
478 if (!strstr($filename,'/')) {
479 $filename = $this->currentItem->authorid . '/' . $filename;
482 $windowwidth = $width;
483 $windowheight = $height;
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>';
493 echo TEMPLATE::fill($this->template['IMAGE_CODE'],$vars);;
497 function createMediaCode($filename, $text = '') {
500 // select private collection when no collection given
501 if (!strstr($filename,'/')) {
502 $filename = $this->currentItem->authorid . '/' . $filename;
505 $vars['link'] = htmlspecialchars($CONF['MediaURL'] . $filename);
506 $vars['text'] = htmlspecialchars($text);
507 $vars['media'] = '<a href="' . $vars['link'] . '">' . $vars['text'] . '</a>';
509 echo TEMPLATE::fill($this->template['MEDIA_CODE'],$vars);;