00001 <?php 00002 /* 00003 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) 00004 * Copyright (C) 2002-2007 The Nucleus Group 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * (see nucleus/documentation/index.html#license for more info) 00011 */ 00019 if ( !function_exists('requestVar') ) exit; 00020 require_once dirname(__FILE__) . '/BaseActions.php'; 00021 00026 class PARSER { 00027 00028 // array with the names of all allowed actions 00029 var $actions; 00030 00031 // reference to actions handler 00032 var $handler; 00033 00034 // delimiters that can be used for skin/templatevars 00035 var $delim; 00036 00037 // parameter delimiter (to separate skinvar params) 00038 var $pdelim; 00039 00040 // usually set to 0. When set to 1, all skinvars are allowed regardless of $actions 00041 var $norestrictions; 00042 00052 function PARSER($allowedActions, &$handler, $delim = '(<%|%>)', $pdelim = ',') { 00053 $this->actions = $allowedActions; 00054 $this->handler =& $handler; 00055 $this->delim = $delim; 00056 $this->pdelim = $pdelim; 00057 $this->norestrictions = 0; // set this to 1 to disable checking for allowedActions 00058 } 00059 00063 function parse(&$contents) { 00064 00065 $pieces = preg_split('/'.$this->delim.'/',$contents); 00066 00067 $maxidx = sizeof($pieces); 00068 for ($idx = 0; $idx < $maxidx; $idx++) { 00069 echo $pieces[$idx]; 00070 $idx++; 00071 if ($idx < $maxidx) { 00072 $this->doAction($pieces[$idx]); 00073 } 00074 } 00075 } 00076 00077 00081 function doAction($action) { 00082 global $manager; 00083 00084 if (!$action) return; 00085 00086 // split into action name + arguments 00087 if (strstr($action,'(')) { 00088 $paramStartPos = strpos($action, '('); 00089 $params = substr($action, $paramStartPos + 1, strlen($action) - $paramStartPos - 2); 00090 $action = substr($action, 0, $paramStartPos); 00091 $params = explode ($this->pdelim, $params); 00092 00093 // trim parameters 00094 // for PHP versions lower than 4.0.6: 00095 // - add // before '$params = ...' 00096 // - remove // before 'foreach' 00097 $params = array_map('trim',$params); 00098 // foreach ($params as $key => $value) { $params[$key] = trim($value); } 00099 } else { 00100 // no parameters 00101 $params = array(); 00102 } 00103 00104 $actionlc = strtolower($action); 00105 00106 // skip execution of skinvars while inside an if condition which hides this part of the page 00107 if (!$this->handler->if_currentlevel && ($actionlc != 'else') && ($actionlc != 'elseif') && ($actionlc != 'endif') && ($actionlc != 'ifnot') && ($actionlc != 'elseifnot') && (substr($actionlc,0,2) != 'if')) 00108 return; 00109 00110 if (in_array($actionlc, $this->actions) || $this->norestrictions ) { 00111 // when using PHP versions lower than 4.0.5, uncomment the line before 00112 // and comment the call_user_func_array call 00113 //$this->call_using_array($action, $this->handler, $params); 00114 call_user_func_array(array(&$this->handler,'parse_' . $actionlc), $params); 00115 } else { 00116 // redirect to plugin action if possible 00117 if (in_array('plugin', $this->actions) && $manager->pluginInstalled('NP_'.$action)) 00118 $this->doAction('plugin('.$action.$this->pdelim.implode($this->pdelim,$params).')'); 00119 else 00120 echo '<%' , $action , '(', implode($this->pdelim, $params), ')%>'; 00121 } 00122 00123 } 00124 00129 function call_using_array($methodname, &$handler, $paramarray) { 00130 00131 $methodname = 'parse_' . $methodname; 00132 00133 if (!method_exists($handler, $methodname)) { 00134 return; 00135 } 00136 00137 $command = 'call_user_func(array(&$handler,$methodname)'; 00138 for ($i = 0; $i<count($paramarray); $i++) 00139 $command .= ',$paramarray[' . $i . ']'; 00140 $command .= ');'; 00141 eval($command); // execute the correct method 00142 } 00143 00144 function setProperty($property, $value) { 00145 global $manager; 00146 $manager->setParserProperty($property, $value); 00147 } 00148 00149 function getProperty($name) { 00150 global $manager; 00151 return $manager->getParserProperty($name); 00152 } 00153 00154 00155 } 00156 00157 ?>