[ Index ]

PHP Cross Reference of Nucleus CMS 3.32

title

Body

[close]

/nucleus/libs/ -> ACTIONS.php (source)

   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 contains the functions that get called by using
  14   * the special tags in the skins
  15   *
  16   * The allowed tags for a type of skinpart are defined by the
  17   * SKIN::getAllowedActionsForType($type) method
  18   *
  19   * @license http://nucleuscms.org/license.txt GNU General Public License
  20   * @copyright Copyright (C) 2002-2007 The Nucleus Group
  21   * @version $Id: ACTIONS.php 1141 2007-05-10 08:10:26Z kimitake $
  22   */
  23  
  24  class ACTIONS extends BaseActions {
  25  
  26      // part of the skin currently being parsed ('index', 'item', 'archive',
  27      // 'archivelist', 'member', 'search', 'error', 'imagepopup')
  28      var $skintype;
  29  
  30      // contains an assoc array with parameters that need to be included when
  31      // generating links to items/archives/... (e.g. catid)
  32      var $linkparams;
  33  
  34      // reference to the skin object for which a part is being parsed
  35      var $skin;
  36  
  37      // used when including templated forms from the include/ dir. The $formdata var
  38      // contains the values to fill out in there (assoc array name -> value)
  39      var $formdata;
  40  
  41      // filled out with the number of displayed items after calling one of the
  42      // (other)blog/(other)searchresults skinvars.
  43      var $amountfound;
  44  
  45      /**
  46       * Constructor for a new ACTIONS object
  47       */
  48  	function ACTIONS($type) {
  49          // call constructor of superclass first
  50          $this->BaseActions();
  51  
  52          $this->skintype = $type;
  53  
  54          global $catid;
  55          if ($catid)
  56              $this->linkparams = array('catid' => $catid);
  57      }
  58  
  59      /**
  60       *  Set the skin
  61       */
  62  	function setSkin(&$skin) {
  63          $this->skin =& $skin;
  64      }
  65  
  66      /**
  67       *  Set the parser
  68       */
  69  	function setParser(&$parser) {
  70          $this->parser =& $parser;
  71      }
  72  
  73      /**
  74       *    Forms get parsedincluded now, using an extra <formdata> skinvar
  75      */
  76  	function doForm($filename) {
  77          global $DIR_NUCLEUS;
  78          array_push($this->parser->actions,'formdata','text','callback','errordiv','ticket');
  79          $oldIncludeMode = PARSER::getProperty('IncludeMode');
  80          $oldIncludePrefix = PARSER::getProperty('IncludePrefix');
  81          PARSER::setProperty('IncludeMode','normal');
  82          PARSER::setProperty('IncludePrefix','');
  83          $this->parse_parsedinclude($DIR_NUCLEUS . 'forms/' . $filename . '.template');
  84          PARSER::setProperty('IncludeMode',$oldIncludeMode);
  85          PARSER::setProperty('IncludePrefix',$oldIncludePrefix);
  86          array_pop($this->parser->actions);        // errordiv
  87          array_pop($this->parser->actions);        // callback
  88          array_pop($this->parser->actions);        // text
  89          array_pop($this->parser->actions);        // formdata
  90          array_pop($this->parser->actions);        // ticket
  91      }
  92  
  93      /**
  94       * Checks conditions for if statements
  95       *
  96       * @param string $field type of <%if%>
  97       * @param string $name property of field
  98       * @param string $value value of property
  99       */
 100  	function checkCondition($field, $name='', $value = '') {
 101          global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
 102  
 103          $condition = 0;
 104          switch($field) {
 105              case 'category':
 106                  $condition = ($blog && $this->_ifCategory($name,$value));
 107                  break;
 108              case 'blogsetting':
 109                  $condition = ($blog && ($blog->getSetting($name) == $value));
 110                  break;
 111              case 'loggedin':
 112                  $condition = $member->isLoggedIn();
 113                  break;
 114              case 'onteam':
 115                  $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
 116                  break;
 117              case 'admin':
 118                  $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
 119                  break;
 120              case 'nextitem':
 121                  $condition = ($itemidnext != '');
 122                  break;
 123              case 'previtem':
 124                  $condition = ($itemidprev != '');
 125                  break;
 126              case 'archiveprevexists':
 127                  $condition = ($archiveprevexists == true);
 128                  break;
 129              case 'archivenextexists':
 130                  $condition = ($archivenextexists == true);
 131                  break;
 132              case 'skintype':
 133                  $condition = ($name == $this->skintype);
 134                  break;
 135              case 'hasplugin':
 136                  $condition = $this->_ifHasPlugin($name, $value);
 137                  break;
 138              default:
 139                  $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
 140                  break;
 141          }
 142          return $condition;
 143      }
 144  
 145      /**
 146       *    hasplugin,PlugName
 147       *       -> checks if plugin exists
 148       *    hasplugin,PlugName,OptionName
 149       *       -> checks if the option OptionName from plugin PlugName is not set to 'no'
 150       *    hasplugin,PlugName,OptionName=value
 151       *       -> checks if the option OptionName from plugin PlugName is set to value
 152       */
 153  	function _ifHasPlugin($name, $value) {
 154          global $manager;
 155          $condition = false;
 156          // (pluginInstalled method won't write a message in the actionlog on failure)
 157          if ($manager->pluginInstalled('NP_'.$name)) {
 158              $plugin =& $manager->getPlugin('NP_' . $name);
 159              if ($plugin != NULL) {
 160                  if ($value == "") {
 161                      $condition = true;
 162                  } else {
 163                      list($name2, $value2) = explode('=', $value, 2);
 164                      if ($value2 == "" && $plugin->getOption($name2) != 'no') {
 165                          $condition = true;
 166                      } else if ($plugin->getOption($name2) == $value2) {
 167                          $condition = true;
 168                      }
 169                  }
 170              }
 171          }
 172          return $condition;
 173      }
 174  
 175      /**
 176       * Checks if a plugin exists and call its doIf function
 177       */
 178  	function _ifPlugin($name, $key = '', $value = '') {
 179          global $manager;
 180  
 181          $plugin =& $manager->getPlugin('NP_' . $name);
 182          if (!$plugin) return;
 183  
 184          $params = func_get_args();
 185          array_shift($params);
 186  
 187          return call_user_func_array(array(&$plugin, 'doIf'), $params);
 188      }
 189  
 190      /**
 191       *  Different checks for a category
 192       */
 193  	function _ifCategory($name = '', $value='') {
 194          global $blog, $catid;
 195  
 196          // when no parameter is defined, just check if a category is selected
 197          if (($name != 'catname' && $name != 'catid') || ($value == ''))
 198              return $blog->isValidCategory($catid);
 199  
 200          // check category name
 201          if ($name == 'catname') {
 202              $value = $blog->getCategoryIdFromName($value);
 203              if ($value == $catid)
 204                  return $blog->isValidCategory($catid);
 205          }
 206  
 207          // check category id
 208          if (($name == 'catid') && ($value == $catid))
 209              return $blog->isValidCategory($catid);
 210  
 211          return false;
 212      }
 213  
 214      /**
 215       *  Checks if a member is on the team of a blog and return his rights
 216       */
 217  	function _ifOnTeam($blogName = '') {
 218          global $blog, $member, $manager;
 219  
 220          // when no blog found
 221          if (($blogName == '') && (!is_object($blog)))
 222              return 0;
 223  
 224          // explicit blog selection
 225          if ($blogName != '')
 226              $blogid = getBlogIDFromName($blogName);
 227  
 228          if (($blogName == '') || !$manager->existsBlogID($blogid))
 229              // use current blog
 230              $blogid = $blog->getID();
 231  
 232          return $member->teamRights($blogid);
 233      }
 234  
 235      /**
 236       *  Checks if a member is admin of a blog
 237       */
 238  	function _ifAdmin($blogName = '') {
 239          global $blog, $member, $manager;
 240  
 241          // when no blog found
 242          if (($blogName == '') && (!is_object($blog)))
 243              return 0;
 244  
 245          // explicit blog selection
 246          if ($blogName != '')
 247              $blogid = getBlogIDFromName($blogName);
 248  
 249          if (($blogName == '') || !$manager->existsBlogID($blogid))
 250              // use current blog
 251              $blogid = $blog->getID();
 252  
 253          return $member->isBlogAdmin($blogid);
 254      }
 255      
 256      /**
 257       * returns either
 258       *        - a raw link (html/xml encoded) when no linktext is provided
 259       *        - a (x)html <a href... link when a text is present (text htmlencoded)
 260       */
 261  	function _link($url, $linktext = '')
 262      {
 263          $u = htmlspecialchars($url);
 264          $u = preg_replace("/&amp;amp;/",'&amp;',$u); // fix URLs that already had encoded ampersands
 265          if ($linktext != '')
 266              $l = '<a href="' . $u .'">'.htmlspecialchars($linktext).'</a>';
 267          else
 268              $l = $u;
 269          return $l;
 270      }
 271      
 272      /**
 273       * Outputs a next/prev link
 274       *
 275       * @param $maxresults
 276       *        The maximum amount of items shown per page (e.g. 10)
 277       * @param $startpos
 278       *        Current start position (requestVar('startpos'))
 279       * @param $direction
 280       *        either 'prev' or 'next'
 281       * @param $linktext
 282       *        When present, the output will be a full <a href...> link. When empty,
 283       *        only a raw link will be outputted
 284       */
 285  	function _searchlink($maxresults, $startpos, $direction, $linktext = '') {
 286          global $CONF, $blog, $query, $amount;
 287          // TODO: Move request uri to linkparams. this is ugly. sorry for that.
 288          $startpos    = intval($startpos);        // will be 0 when empty.
 289          $parsed        = parse_url(serverVar('REQUEST_URI'));
 290          $parsed        = $parsed['query'];
 291          $url        = '';
 292  
 293          switch ($direction) {
 294              case 'prev':
 295                  if ( intval($startpos) - intval($maxresults) >= 0) {
 296                      $startpos     = intval($startpos) - intval($maxresults);
 297                      $url        = $CONF['SearchURL'].'?'.alterQueryStr($parsed,'startpos',$startpos);
 298                  }
 299                  break;
 300              case 'next':
 301                  $iAmountOnPage = $this->amountfound;
 302                  if ($iAmountOnPage == 0)
 303                  {
 304                      // [%nextlink%] or [%prevlink%] probably called before [%blog%] or [%searchresults%]
 305                      // try a count query
 306                      switch ($this->skintype)
 307                      {
 308                          case 'index':
 309                              $sqlquery = $blog->getSqlBlog('', 'count');
 310                              break;
 311                          case 'search':
 312                              $sqlquery = $blog->getSqlSearch($query, $amount, $unused_highlight, 'count');
 313                              break;
 314                      }
 315                      if ($sqlquery)
 316                          $iAmountOnPage = intval(quickQuery($sqlquery)) - intval($startpos);
 317                  }
 318                  if (intval($iAmountOnPage) >= intval($maxresults)) {
 319                      $startpos     = intval($startpos) + intval($maxresults);
 320                      $url        = $CONF['SearchURL'].'?'.alterQueryStr($parsed,'startpos',$startpos);
 321                  }
 322                  break;
 323              default:
 324                  break;
 325          } // switch($direction)
 326  
 327          if ($url != '')
 328              echo $this->_link($url, $linktext);
 329      }
 330  
 331      /**
 332       *  Creates an item link and if no id is given a todaylink 
 333       */
 334  	function _itemlink($id, $linktext = '') {
 335          global $CONF;
 336          if ($id)
 337              echo $this->_link(createItemLink($id, $this->linkparams), $linktext);
 338          else
 339              $this->parse_todaylink($linktext);
 340      }
 341      
 342      /**
 343       *  Creates an archive link and if no id is given a todaylink 
 344       */
 345  	function _archivelink($id, $linktext = '') {
 346          global $CONF, $blog;
 347          if ($id)
 348              echo $this->_link(createArchiveLink($blog->getID(), $id, $this->linkparams), $linktext);
 349          else
 350              $this->parse_todaylink($linktext);
 351      }
 352      
 353      /**
 354        * Helper function that sets the category that a blog will need to use
 355        *
 356        * @param $blog
 357        *        An object of the blog class, passed by reference (we want to make changes to it)
 358        * @param $catname
 359        *        The name of the category to use
 360        */
 361  	function _setBlogCategory(&$blog, $catname) {
 362          global $catid;
 363          if ($catname != '')
 364              $blog->setSelectedCategoryByName($catname);
 365          else
 366              $blog->setSelectedCategory($catid);
 367      }
 368  
 369      /**
 370       *  Notifies the Manager that a PreBlogContent event occurs
 371       */
 372  	function _preBlogContent($type, &$blog) {
 373          global $manager;
 374          $manager->notify('PreBlogContent',array('blog' => &$blog, 'type' => $type));
 375      }
 376  
 377      /**
 378       *  Notifies the Manager that a PostBlogContent event occurs
 379       */
 380  	function _postBlogContent($type, &$blog) {
 381          global $manager;
 382          $manager->notify('PostBlogContent',array('blog' => &$blog, 'type' => $type));
 383      }
 384      
 385      /**
 386       * Parse skinvar additemform
 387       */
 388  	function parse_additemform() {
 389          global $blog, $CONF;
 390          $this->formdata = array(
 391              'adminurl' => htmlspecialchars($CONF['AdminURL'],ENT_QUOTES),
 392              'catid' => $blog->getDefaultCategory()
 393          );
 394          $blog->InsertJavaScriptInfo();
 395          $this->doForm('additemform');
 396      }
 397      
 398      /**
 399       * Parse skinvar adminurl
 400       * (shortcut for admin url)     
 401       */
 402  	function parse_adminurl() {
 403          $this->parse_sitevar('adminurl');
 404      }
 405  
 406      /**
 407       * Parse skinvar archive
 408       */
 409  	function parse_archive($template, $category = '') {
 410          global $blog, $archive;
 411          // can be used with either yyyy-mm or yyyy-mm-dd
 412          sscanf($archive,'%d-%d-%d',$y,$m,$d);
 413          $this->_setBlogCategory($blog, $category);
 414          $this->_preBlogContent('achive',$blog);
 415          $blog->showArchive($template, $y, $m, $d);
 416          $this->_postBlogContent('achive',$blog);
 417  
 418      }
 419  
 420      /**
 421        * %archivedate(locale,date format)%
 422        */
 423  	function parse_archivedate($locale = '-def-') {
 424          global $archive;
 425  
 426          if ($locale == '-def-')
 427              setlocale(LC_TIME,$template['LOCALE']);
 428          else
 429              setlocale(LC_TIME,$locale);
 430  
 431          // get archive date
 432          sscanf($archive,'%d-%d-%d',$y,$m,$d);
 433  
 434          // get format
 435          $args = func_get_args();
 436          // format can be spread over multiple parameters
 437          if (sizeof($args) > 1) {
 438              // take away locale
 439              array_shift($args);
 440              // implode
 441              $format=implode(',',$args);
 442          } elseif ($d == 0) {
 443              $format = '%B %Y';
 444          } else {
 445              $format = '%d %B %Y';
 446          }
 447  
 448          echo strftime($format,mktime(0,0,0,$m,$d?$d:1,$y));
 449      }
 450  
 451      /**
 452       *  Parse skinvar archivedaylist
 453       */         
 454  	function parse_archivedaylist($template, $category = 'all', $limit = 0) {
 455          global $blog;
 456          if ($category == 'all') $category = '';
 457          $this->_preBlogContent('archivelist',$blog);
 458          $this->_setBlogCategory($blog, $category);
 459          $blog->showArchiveList($template, 'day', $limit);
 460          $this->_postBlogContent('archivelist',$blog);
 461      }
 462      
 463      /**
 464       *    A link to the archives for the current blog (or for default blog)
 465       */
 466  	function parse_archivelink($linktext = '') {
 467          global $blog, $CONF;
 468          if ($blog)
 469              echo $this->_link(createArchiveListLink($blog->getID(),$this->linkparams), $linktext);
 470          else
 471              echo $this->_link(createArchiveListLink(), $linktext);
 472      }
 473  
 474  	function parse_archivelist($template, $category = 'all', $limit = 0) {
 475          global $blog;
 476          if ($category == 'all') $category = '';
 477          $this->_preBlogContent('archivelist',$blog);
 478          $this->_setBlogCategory($blog, $category);
 479          $blog->showArchiveList($template, 'month', $limit);
 480          $this->_postBlogContent('archivelist',$blog);
 481      }
 482  
 483      /**
 484       * Parse skinvar archivetype
 485       */
 486  	function parse_archivetype() {
 487          global $archivetype;
 488          echo $archivetype;
 489      }
 490  
 491      /**
 492       * Parse skinvar blog
 493       */
 494  	function parse_blog($template, $amount = 10, $category = '') {
 495          global $blog, $startpos;
 496  
 497          list($limit,