OSDN Git Service

merged 3.3 beta1
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / PARSER.php
index 29a5864..d4b4415 100755 (executable)
-<?php\r
-/*\r
- * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
- * Copyright (C) 2002-2005 The Nucleus Group\r
- *\r
- * This program is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU General Public License\r
- * as published by the Free Software Foundation; either version 2\r
- * of the License, or (at your option) any later version.\r
- * (see nucleus/documentation/index.html#license for more info)\r
- */\r
-/**\r
- * @license http://nucleuscms.org/license.txt GNU General Public License\r
- * @copyright Copyright (C) 2002-2005 The Nucleus Group\r
- * @version $Id: PARSER.php,v 1.4 2005-08-13 07:33:02 kimitake Exp $\r
- * $NucleusJP: PARSER.php,v 1.3 2005/03/12 06:19:05 kimitake Exp $\r
- */\r
\r
-/**\r
- * This is the parser class of Nucleus. It is used for various things (skin parsing,\r
- * form generation, ...)\r
- */\r
-class PARSER {\r
-\r
-       // array with the names of all allowed actions\r
-       var $actions;\r
-       \r
-       // reference to actions handler\r
-       var $handler;\r
-       \r
-       // delimiters that can be used for skin/templatevars\r
-       var $delim;\r
-       \r
-       // parameter delimiter (to separate skinvar params)\r
-       var $pdelim;\r
-       \r
-       // usually set to 0. When set to 1, all skinvars are allowed regardless of $actions\r
-       var $norestrictions;\r
-       \r
-       /**\r
-        * Creates a new parser object with the given allowed actions \r
-        * and the given handler\r
-        *\r
-        * @param $allowedActions array\r
-        * @param $handler class object with functions for each action (reference)\r
-        * @param $delim optional delimiter\r
-        * @param $paramdelim optional parameterdelimiter        \r
-        */\r
-       function PARSER($allowedActions, &$handler, $delim = '(<%|%>)', $pdelim = ',') {\r
-               $this->actions = $allowedActions;\r
-               $this->handler =& $handler;\r
-               $this->delim = $delim;\r
-               $this->pdelim = $pdelim;\r
-               $this->norestrictions = 0;      // set this to 1 to disable checking for allowedActions\r
-       }\r
-       \r
-       /**\r
-        * Parses the given contents and outputs it\r
-        */\r
-       function parse(&$contents) {\r
-       \r
-               $pieces = preg_split('/'.$this->delim.'/',$contents);\r
-               \r
-               $maxidx = sizeof($pieces);\r
-               for ($idx = 0;$idx<$maxidx;$idx++) {\r
-                       echo $pieces[$idx];             \r
-                       $idx++;\r
-                       $this->doAction($pieces[$idx]);\r
-               }\r
-       }\r
-       \r
-\r
-       /**\r
-         * handle an action \r
-         */\r
-       function doAction($action) {\r
-               global $manager;\r
-\r
-               if (!$action) return;\r
-               \r
-               // split into action name + arguments\r
-               if (strstr($action,'(')) {\r
-                       $paramStartPos = strpos($action, '(');\r
-                       $params = substr($action, $paramStartPos + 1, strlen($action) - $paramStartPos - 2);\r
-                       $action = substr($action, 0, $paramStartPos);\r
-                       $params = explode ($this->pdelim, $params);\r
-                       \r
-                       // trim parameters \r
-                       // for PHP versions lower than 4.0.6:\r
-                       //   - add // before '$params = ...' \r
-                       //   - remove // before 'foreach'\r
-                       $params = array_map('trim',$params);\r
-                       // foreach ($params as $key => $value) { $params[$key] = trim($value); }                        \r
-               } else {\r
-                       // no parameters\r
-                       $params = array();\r
-               }\r
-       \r
-               $actionlc = strtolower($action);\r
-               \r
-               // skip execution of skinvars while inside an if condition which hides this part of the page\r
-               if (!$this->handler->if_currentlevel && ($actionlc != 'else') && ($actionlc != 'endif') && (substr($actionlc,0,2) != 'if'))\r
-                       return;\r
-       \r
-               if (in_array($actionlc, $this->actions) || $this->norestrictions ) {\r
-                       // when using PHP versions lower than 4.0.5, uncomment the line before\r
-                       // and comment the call_user_func_array call\r
-                       //$this->call_using_array($action, $this->handler, $params);\r
-                       call_user_func_array(array(&$this->handler,'parse_' . $actionlc), $params);\r
-               } else {\r
-                       // redirect to plugin action if possible\r
-                       if (in_array('plugin', $this->actions) && $manager->pluginInstalled('NP_'.$action))\r
-                               $this->doAction('plugin('.$action.$this->pdelim.implode($this->pdelim,$params).')');\r
-                       else\r
-                               echo '<b>DISALLOWED (' , $action , ')</b>';\r
-               }\r
-               \r
-       }\r
-       \r
-       /**\r
-         * Calls a method using an array of parameters (for use with PHP versions lower than 4.0.5)\r
-         * ( = call_user_func_array() function )\r
-         */\r
-       function call_using_array($methodname, &$handler, $paramarray) {\r
-\r
-               $methodname = 'parse_' . $methodname;\r
-               \r
-               if (!method_exists($handler, $methodname)) {\r
-                       return;\r
-               }\r
-\r
-               $command = 'call_user_func(array(&$handler,$methodname)';\r
-               for ($i = 0; $i<count($paramarray); $i++)\r
-                       $command .= ',$paramarray[' . $i . ']';\r
-               $command .= ');';\r
-               eval($command); // execute the correct method\r
-       }\r
-       \r
-       function setProperty($property, $value) {\r
-               global $manager;\r
-               $manager->setParserProperty($property, $value);\r
-       }\r
-       \r
-       function getProperty($name) {\r
-               global $manager;\r
-               return $manager->getParserProperty($name);\r
-       }\r
-       \r
-       \r
-}\r
-\r
-/**\r
- * This class contains parse actions that are available in all ACTION classes\r
- * e.g. include, phpinclude, parsedinclude, skinfile, ...\r
- *\r
- * It should never be used on it's own\r
- */\r
-class BaseActions {\r
-       \r
-       // depth level for includes (max. level is 3)\r
-       var $level;\r
-       \r
-       // array of evaluated conditions (true/false). The element at the end is the one for the most nested\r
-       // if block.\r
-       var $if_conditions;\r
-       \r
-       // at all times, can be evaluated to either true if the current block needs to be displayed. This \r
-       // variable is used to decide to skip skinvars in parts that will never be outputted.\r
-       var $if_currentlevel;\r
-       \r
-       // contains a search string with keywords that need to be highlighted. These get parsed into $aHighlight\r
-       var $strHighlight;\r
-       \r
-       // array of keywords that need to be highlighted in search results (see the highlight() \r
-       // and parseHighlight() methods)\r
-       var $aHighlight;\r
-       \r
-\r
-       // reference to the parser object that is using this object as actions-handler\r
-\r
-       var $parser;\r
-       \r
-\r
-       function BaseActions() {\r
-               $this->level = 0; \r
-               \r
-               // if nesting level\r
-               $this->if_conditions = array(); // array on which condition values are pushed/popped\r
-               $this->if_currentlevel = 1;             // 1 = current level is displayed; 0 = current level not displayed\r
-\r
-               // highlights           \r
-               $this->strHighlight = '';                       // full highlight\r
-               $this->aHighlight = array();            // parsed highlight\r
-               \r
-       }\r
-\r
-       // include file (no parsing of php)\r
-       function parse_include($filename) {\r
-               @readfile($this->getIncludeFileName($filename));\r
-       }\r
-       \r
-       // php-include file \r
-       function parse_phpinclude($filename) {\r
-               includephp($this->getIncludeFileName($filename));\r
-       }       \r
-       // parsed include\r
-       function parse_parsedinclude($filename) {\r
-               // check current level\r
-               if ($this->level > 3) return;   // max. depth reached (avoid endless loop)\r
-               $filename = $this->getIncludeFileName($filename);\r
-               if (!file_exists($filename)) return '';\r
-               \r
-               $fsize = filesize($filename);\r
-               \r
-               // nothing to include\r
-               if ($fsize <= 0)\r
-                       return;\r
-               \r
-               $this->level = $this->level + 1;\r
-               \r
-               // read file \r
-               $fd = fopen ($filename, 'r');\r
-               $contents = fread ($fd, $fsize);\r
-               fclose ($fd);           \r
-               \r
-               // parse file contents\r
-               $this->parser->parse($contents);\r
-               \r
-               $this->level = $this->level - 1;                \r
-       }\r
-       \r
-       /**\r
-        * Returns the correct location of the file to be included, according to\r
-        * parser properties\r
-        *\r
-        * IF IncludeMode = 'skindir' => use skindir\r
-        */\r
-       function getIncludeFileName($filename) {\r
-               // leave absolute filenames and http urls as they are\r
-               if (\r
-                               (substr($filename,0,1) == '/')\r
-                       ||      (substr($filename,0,7) == 'http://')\r
-                       ||      (substr($filename,0,6) == 'ftp://')                     \r
-                       )\r
-                       return $filename;\r
-       \r
-               $filename = PARSER::getProperty('IncludePrefix') . $filename;\r
-               if (PARSER::getProperty('IncludeMode') == 'skindir') {\r
-                       global $DIR_SKINS;\r
-                       return $DIR_SKINS . $filename;\r
-               } else {\r
-                       return $filename;\r
-               }\r
-       }\r
-       \r
-       /**\r
-        * Inserts an url relative to the skindir (useful when doing import/export)\r
-        *\r
-        * e.g. <skinfile(default/myfile.sth)>\r
-        */\r
-       function parse_skinfile($filename) {\r
-               global $CONF;\r
-               \r
-               echo $CONF['SkinsURL'] . PARSER::getProperty('IncludePrefix') . $filename;\r
-       }\r
-       \r
-       /**\r
-        * Sets a property for the parser\r
-        */\r
-       function parse_set($property, $value) {\r
-               PARSER::setProperty($property, $value);\r
-       }\r
-       \r
-       /**\r
-        * Helper function: add if condition\r
-        */\r
-       function _addIfCondition($condition) {\r
-       \r
-               array_push($this->if_conditions,$condition);\r
-       \r
-               $this->_updateTopIfCondition();\r
-                       \r
-               ob_start();             \r
-       }\r
-       \r
-       function _updateTopIfCondition() {\r
-               if (sizeof($this->if_conditions) == 0) \r
-                       $this->if_currentlevel = 1;\r
-               else\r
-                       $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];\r
-       }\r
-       \r
-       /**\r
-        * returns the currently top if condition\r
-        */\r
-       function _getTopIfCondition() {\r
-               return $this->if_currentlevel;\r
-       }\r
-       \r
-       /**\r
-        * else\r
-        */\r
-       function parse_else() {\r
-               if (sizeof($this->if_conditions) == 0) return;\r
-               $old = $this->if_currentlevel;\r
-               if (array_pop($this->if_conditions)) {\r
-                       ob_end_flush();\r
-                       $this->_addIfCondition(0);\r
-               } else {\r
-                       ob_end_clean();\r
-                       $this->_addIfCondition(1);\r
-               }\r
-       }\r
-       \r
-       /**\r
-        * Ends a conditional if-block \r
-        * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)\r
-        */\r
-       function parse_endif() {\r
-               // we can only close what has been opened\r
-               if (sizeof($this->if_conditions) == 0) return;\r
-               \r
-               if (array_pop($this->if_conditions)) {\r
-                       ob_end_flush();\r
-               } else {\r
-                       ob_end_clean();\r
-               }\r
-               \r
-               $this->_updateTopIfCondition();\r
-       }\r
-       \r
-       \r
-       /** \r
-        * Sets the search terms to be highlighted\r
-        *\r
-        * @param $highlight\r
-        *              A series of search terms\r
-        */\r
-       function setHighlight($highlight) {     \r
-               $this->strHighlight = $highlight;\r
-               if ($highlight) {\r
-                       $this->aHighlight = parseHighlight($highlight); \r
-               }\r
-       }\r
-       \r
-       /**\r
-        * Applies the highlight to the given piece of text\r
-        *\r
-        * @param &$data\r
-        *              Data that needs to be highlighted\r
-        * @see setHighlight\r
-        */\r
-       function highlight(&$data) {\r
-               if ($this->aHighlight)\r
-                       return highlight($data,$this->aHighlight,$this->template['SEARCH_HIGHLIGHT']);\r
-               else\r
-                       return $data;\r
-       }\r
-       \r
-       \r
-\r
-}\r
\r
-?>\r
+<?php
+/*
+ * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
+ * Copyright (C) 2002-2006 The Nucleus Group
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * (see nucleus/documentation/index.html#license for more info)
+ */
+/**
+ * @license http://nucleuscms.org/license.txt GNU General Public License
+ * @copyright Copyright (C) 2002-2006 The Nucleus Group
+ * @version $Id: PARSER.php,v 1.5 2006-07-12 07:11:47 kimitake Exp $
+ * $NucleusJP: PARSER.php,v 1.4 2005/08/13 07:33:02 kimitake Exp $
+ */
+
+// temporary: dirt way to separe class BaseActions from PARSER
+require_once $DIR_LIBS . 'BaseActions.php';
+/**
+ * This is the parser class of Nucleus. It is used for various things (skin parsing,
+ * form generation, ...)
+ */
+class PARSER {
+
+       // array with the names of all allowed actions
+       var $actions;
+
+       // reference to actions handler
+       var $handler;
+
+       // delimiters that can be used for skin/templatevars
+       var $delim;
+
+       // parameter delimiter (to separate skinvar params)
+       var $pdelim;
+
+       // usually set to 0. When set to 1, all skinvars are allowed regardless of $actions
+       var $norestrictions;
+
+       /**
+        * Creates a new parser object with the given allowed actions
+        * and the given handler
+        *
+        * @param $allowedActions array
+        * @param $handler class object with functions for each action (reference)
+        * @param $delim optional delimiter
+        * @param $paramdelim optional parameterdelimiter
+        */
+       function PARSER($allowedActions, &$handler, $delim = '(<%|%>)', $pdelim = ',') {
+               $this->actions = $allowedActions;
+               $this->handler =& $handler;
+               $this->delim = $delim;
+               $this->pdelim = $pdelim;
+               $this->norestrictions = 0;      // set this to 1 to disable checking for allowedActions
+       }
+
+       /**
+        * Parses the given contents and outputs it
+        */
+       function parse(&$contents) {
+
+               $pieces = preg_split('/'.$this->delim.'/',$contents);
+
+               $maxidx = sizeof($pieces);
+               for ($idx = 0; $idx < $maxidx; $idx++) {
+                       echo $pieces[$idx];
+                       $idx++;
+                       if ($idx < $maxidx) {
+                               $this->doAction($pieces[$idx]);
+                       }
+               }
+       }
+
+
+       /**
+         * handle an action
+         */
+       function doAction($action) {
+               global $manager;
+
+               if (!$action) return;
+
+               // split into action name + arguments
+               if (strstr($action,'(')) {
+                       $paramStartPos = strpos($action, '(');
+                       $params = substr($action, $paramStartPos + 1, strlen($action) - $paramStartPos - 2);
+                       $action = substr($action, 0, $paramStartPos);
+                       $params = explode ($this->pdelim, $params);
+
+                       // trim parameters
+                       // for PHP versions lower than 4.0.6:
+                       //   - add // before '$params = ...'
+                       //   - remove // before 'foreach'
+                       $params = array_map('trim',$params);
+                       // foreach ($params as $key => $value) { $params[$key] = trim($value); }
+               } else {
+                       // no parameters
+                       $params = array();
+               }
+
+               $actionlc = strtolower($action);
+
+               // skip execution of skinvars while inside an if condition which hides this part of the page
+               if (!$this->handler->if_currentlevel && ($actionlc != 'else') && ($actionlc != 'elseif') && ($actionlc != 'endif') && ($actionlc != 'ifnot') && ($actionlc != 'elseifnot') && (substr($actionlc,0,2) != 'if'))
+                       return;
+
+               if (in_array($actionlc, $this->actions) || $this->norestrictions ) {
+                       // when using PHP versions lower than 4.0.5, uncomment the line before
+                       // and comment the call_user_func_array call
+                       //$this->call_using_array($action, $this->handler, $params);
+                       call_user_func_array(array(&$this->handler,'parse_' . $actionlc), $params);
+               } else {
+                       // redirect to plugin action if possible
+                       if (in_array('plugin', $this->actions) && $manager->pluginInstalled('NP_'.$action))
+                               $this->doAction('plugin('.$action.$this->pdelim.implode($this->pdelim,$params).')');
+                       else
+                               echo '&lt;%' , $action , '(', implode($this->pdelim, $params), ')%&gt;';
+               }
+
+       }
+
+       /**
+         * Calls a method using an array of parameters (for use with PHP versions lower than 4.0.5)
+         * ( = call_user_func_array() function )
+         */
+       function call_using_array($methodname, &$handler, $paramarray) {
+
+               $methodname = 'parse_' . $methodname;
+
+               if (!method_exists($handler, $methodname)) {
+                       return;
+               }
+
+               $command = 'call_user_func(array(&$handler,$methodname)';
+               for ($i = 0; $i<count($paramarray); $i++)
+                       $command .= ',$paramarray[' . $i . ']';
+               $command .= ');';
+               eval($command); // execute the correct method
+       }
+
+       function setProperty($property, $value) {
+               global $manager;
+               $manager->setParserProperty($property, $value);
+       }
+
+       function getProperty($name) {
+               global $manager;
+               return $manager->getParserProperty($name);
+       }
+
+
+}
+
+?>
\ No newline at end of file