[ Index ]

PHP Cross Reference of Nucleus CMS v3.51 code documentation

title

Body

[close]

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

   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 contains parse actions that are available in all ACTION classes

  14   * e.g. include, phpinclude, parsedinclude, skinfile, ...

  15   *

  16   * It should never be used on it's own

  17   *

  18   * @license http://nucleuscms.org/license.txt GNU General Public License

  19   * @copyright Copyright (C) 2002-2009 The Nucleus Group

  20   * @version $Id: BaseActions.php 1388 2009-07-18 06:31:28Z shizuki $

  21   */
  22  
  23  class BaseActions {
  24  
  25      // depth level for includes (max. level is 3)

  26      var $level;
  27  
  28      // array of evaluated conditions (true/false). The element at the end is the one for the most nested

  29      // if block.

  30      var $if_conditions;
  31  
  32      // in the "elseif" / "elseifnot" sequences, if one of the conditions become "true" remained conditions should not

  33      // be tested. this variable (actually a stack) holds this information.

  34      var $if_execute;
  35  
  36      // at all times, can be evaluated to either true if the current block needs to be displayed. This

  37      // variable is used to decide to skip skinvars in parts that will never be outputted.

  38      var $if_currentlevel;
  39  
  40      // contains a search string with keywords that need to be highlighted. These get parsed into $aHighlight

  41      var $strHighlight;
  42  
  43      // array of keywords that need to be highlighted in search results (see the highlight()

  44      // and parseHighlight() methods)

  45      var $aHighlight;
  46  
  47      // reference to the parser object that is using this object as actions-handler

  48      var $parser;
  49  
  50  	function BaseActions() {
  51          $this->level = 0;
  52  
  53          // if nesting level

  54          $this->if_conditions = array(); // array on which condition values are pushed/popped

  55          $this->if_execute = array();     // array on which condition values are pushed/popped

  56          $this->if_currentlevel = 1;        // 1 = current level is displayed; 0 = current level not displayed

  57  
  58          // highlights

  59          $this->strHighlight = '';            // full highlight

  60          $this->aHighlight = array();        // parsed highlight

  61  
  62      }
  63  
  64      // include file (no parsing of php)

  65  	function parse_include($filename) {
  66          @readfile($this->getIncludeFileName($filename));
  67      }
  68  
  69      // php-include file

  70  	function parse_phpinclude($filename) {
  71          includephp($this->getIncludeFileName($filename));
  72      }
  73  
  74      // parsed include

  75  	function parse_parsedinclude($filename) {
  76          // check current level

  77          if ($this->level > 3) return;    // max. depth reached (avoid endless loop)

  78          global $skinid;
  79          $skin = new SKIN($skinid);
  80          $file = $this->getIncludeFileName($filename);
  81          if (!$skin->isValid && !file_exists($file)) {
  82              return;
  83          }
  84          $contents = $skin->getContent($filename);
  85          if (!$contents) {
  86              if (!file_exists($file)) return;
  87              $contents = file_get_contents($file);
  88              if (empty($contents)) return;
  89          }
  90          $this->level = $this->level + 1;
  91          // parse file contents

  92          $this->parser->parse($contents);
  93  
  94          $this->level = $this->level - 1;
  95      }
  96  
  97      /**

  98       * Returns the correct location of the file to be included, according to

  99       * parser properties

 100       *

 101       * IF IncludeMode = 'skindir' => use skindir

 102       */
 103  	function getIncludeFileName($filename) {
 104          // leave absolute filenames and http urls as they are

 105          if (
 106                  (substr($filename,0,1) == '/')
 107              ||    (substr($filename,0,7) == 'http://')
 108              ||    (substr($filename,0,6) == 'ftp://')
 109              )
 110              return $filename;
 111  
 112          $filename = PARSER::getProperty('IncludePrefix') . $filename;
 113          if (PARSER::getProperty('IncludeMode') == 'skindir') {
 114              global $DIR_SKINS;
 115              return $DIR_SKINS . $filename;
 116          } else {
 117              return $filename;
 118          }
 119      }
 120  
 121      /**

 122       * Inserts an url relative to the skindir (useful when doing import/export)

 123       *

 124       * e.g. <skinfile(default/myfile.sth)>

 125       */
 126  	function parse_skinfile($filename) {
 127          global $CONF;
 128  
 129          echo $CONF['SkinsURL'] . PARSER::getProperty('IncludePrefix') . $filename;
 130      }
 131  
 132      /**

 133       * Sets a property for the parser

 134       */
 135  	function parse_set($property, $value) {
 136          PARSER::setProperty($property, $value);
 137      }
 138  
 139      /**

 140       * Helper function: add if condition

 141       */
 142  	function _addIfCondition($condition) {
 143  
 144          array_push($this->if_conditions,$condition);
 145  
 146          $this->_updateTopIfCondition();
 147  
 148          ob_start();
 149      }
 150  
 151  	function _updateTopIfCondition() {
 152          if (sizeof($this->if_conditions) == 0)
 153              $this->if_currentlevel = 1;
 154          else
 155              $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
 156      }
 157  
 158      /**

 159       * Helper function for elseif / elseifnot

 160       */
 161  	function _addIfExecute() {
 162          array_push($this->if_execute, 0);
 163      }
 164  
 165      /**

 166       * Helper function for elseif / elseifnot

 167       * @param string condition to be fullfilled

 168       */
 169  	function _updateIfExecute($condition) {
 170          $index = sizeof($this->if_execute) - 1;
 171          $this->if_execute[$index] = $this->if_execute[$index] || $condition;
 172      }
 173  
 174      /**

 175       * returns the currently top if condition

 176       */
 177  	function _getTopIfCondition() {
 178          return $this->if_currentlevel;
 179      }
 180  
 181      /**

 182       * Sets the search terms to be highlighted

 183       *

 184       * @param $highlight

 185       *        A series of search terms

 186       */
 187  	function setHighlight($highlight) {
 188          $this->strHighlight = $highlight;
 189          if ($highlight) {
 190              $this->aHighlight = parseHighlight($highlight);
 191          }
 192      }
 193  
 194      /**

 195       * Applies the highlight to the given piece of text

 196       *

 197       * @param &$data

 198       *        Data that needs to be highlighted

 199       * @see setHighlight

 200       */
 201  	function highlight(&$data) {
 202          if ($this->aHighlight)
 203              return highlight($data,$this->aHighlight,$this->template['SEARCH_HIGHLIGHT']);
 204          else
 205              return $data;
 206      }
 207  
 208      /**

 209       * Parses <%if%> statements

 210       */
 211  	function parse_if() {
 212          $this->_addIfExecute();
 213  
 214          $args = func_get_args();
 215          $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
 216          $this->_addIfCondition($condition);
 217      }
 218  
 219      /**

 220       * Parses <%else%> statements

 221       */
 222  	function parse_else() {
 223          if (sizeof($this->if_conditions) == 0) return;
 224          array_pop($this->if_conditions);
 225          if ($this->if_currentlevel) {
 226              ob_end_flush();
 227              $this->_updateIfExecute(1);
 228              $this->_addIfCondition(0);
 229          } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
 230              ob_end_clean();
 231              $this->_addIfCondition(0);
 232          } else {
 233              ob_end_clean();
 234              $this->_addIfCondition(1);
 235          }
 236      }
 237  
 238      /**

 239       * Parses <%elseif%> statements

 240       */
 241  	function parse_elseif() {
 242          if (sizeof($this->if_conditions) == 0) return;
 243          array_pop($this->if_conditions);
 244          if ($this->if_currentlevel) {
 245              ob_end_flush();
 246              $this->_updateIfExecute(1);
 247              $this->_addIfCondition(0);
 248          } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
 249              ob_end_clean();
 250              $this->_addIfCondition(0);
 251          } else {
 252              ob_end_clean();
 253              $args = func_get_args();
 254              $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
 255              $this->_addIfCondition($condition);
 256          }
 257      }
 258  
 259      /**

 260       * Parses <%ifnot%> statements

 261       */
 262  	function parse_ifnot() {
 263          $this->_addIfExecute();
 264  
 265          $args = func_get_args();
 266          $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
 267          $this->_addIfCondition(!$condition);
 268      }
 269  
 270      /**

 271       * Parses <%elseifnot%> statements

 272       */
 273  	function parse_elseifnot() {
 274          if (sizeof($this->if_conditions) == 0) return;
 275          array_pop($this->if_conditions);
 276          if ($this->if_currentlevel) {
 277              ob_end_flush();
 278              $this->_updateIfExecute(1);
 279              $this->_addIfCondition(0);
 280          } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
 281              ob_end_clean();
 282              $this->_addIfCondition(0);
 283          } else {
 284              ob_end_clean();
 285              $args = func_get_args();
 286              $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
 287              $this->_addIfCondition(!$condition);
 288          }
 289      }
 290  
 291      /**

 292       * Ends a conditional if-block

 293       * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)

 294       */
 295  	function parse_endif() {
 296          // we can only close what has been opened

 297          if (sizeof($this->if_conditions) == 0) return;
 298  
 299          if ($this->if_currentlevel) {
 300              ob_end_flush();
 301          } else {
 302              ob_end_clean();
 303          }
 304          array_pop($this->if_conditions);
 305          array_pop($this->if_execute);
 306  
 307          $this->_updateTopIfCondition();
 308      }
 309  }
 310  ?>


Generated: Sun Aug 1 03:56:06 2010
Open Source related documentation for developers.