OSDN Git Service

コピーライト表示部分の年度変更
[nucleus-jp/nucleus-next.git] / nucleus / libs / BaseActions.php
1 <?php\r
2 /*\r
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
4  * Copyright (C) 2002-2012 The Nucleus Group\r
5  *\r
6  * This program is free software; you can redistribute it and/or\r
7  * modify it under the terms of the GNU General Public License\r
8  * as published by the Free Software Foundation; either version 2\r
9  * of the License, or (at your option) any later version.\r
10  * (see nucleus/documentation/index.html#license for more info)\r
11  */\r
12 /**\r
13  * This class contains parse actions that are available in all ACTION classes\r
14  * e.g. include, phpinclude, parsedinclude, skinfile, ...\r
15  *\r
16  * It should never be used on it's own\r
17  *\r
18  * @license http://nucleuscms.org/license.txt GNU General Public License\r
19  * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
20  * @version $Id: BaseActions.php 1527 2011-06-21 10:43:44Z sakamocchi $\r
21  */\r
22 \r
23 class BaseActions\r
24 {\r
25         // depth level for includes (max. level is 3)\r
26         var $level;\r
27 \r
28         // array of evaluated conditions (true/false). The element at the end is the one for the most nested\r
29         // if block.\r
30         var $if_conditions;\r
31 \r
32         // in the "elseif" / "elseifnot" sequences, if one of the conditions become "true" remained conditions should not\r
33         // be tested. this variable (actually a stack) holds this information.\r
34         var $if_execute;\r
35 \r
36         // at all times, can be evaluated to either true if the current block needs to be displayed. This\r
37         // variable is used to decide to skip skinvars in parts that will never be outputted.\r
38         var $if_currentlevel;\r
39 \r
40         // contains a search string with keywords that need to be highlighted. These get parsed into $aHighlight\r
41         var $strHighlight;\r
42 \r
43         // array of keywords that need to be highlighted in search results (see the highlight()\r
44         // and parseHighlight() methods)\r
45         var $aHighlight;\r
46 \r
47         // reference to the parser object that is using this object as actions-handler\r
48         var $parser;\r
49 \r
50         /**\r
51          *  Constructor for a new BaseAction object\r
52          */\r
53         function BaseActions() {\r
54                 $this->level = 0;\r
55 \r
56                 // if nesting level\r
57                 $this->if_conditions = array(); // array on which condition values are pushed/popped\r
58                 $this->if_execute = array();    // array on which condition values are pushed/popped\r
59                 $this->if_currentlevel = 1;             // 1 = current level is displayed; 0 = current level not displayed\r
60 \r
61                 // highlights\r
62                 $this->strHighlight = '';                       // full highlight\r
63                 $this->aHighlight = array();            // parsed highlight\r
64 \r
65         }\r
66 \r
67         /**\r
68          * include file (no parsing of php)\r
69          * \r
70          * ToDo: function returns nothing and refering to the cross reference it\r
71          *       isn't called from anywhere   \r
72          * \r
73          * @param $filename\r
74          */\r
75         function parse_include($filename) {\r
76                 @readfile($this->getIncludeFileName($filename));\r
77         }\r
78 \r
79         /**\r
80          * php-include file\r
81          * \r
82          * @param $filename\r
83          */\r
84         function parse_phpinclude($filename) {\r
85                 includephp($this->getIncludeFileName($filename));\r
86         }\r
87 \r
88         \r
89         /**\r
90          * parsed include\r
91          * \r
92          * @param $filename\r
93          */\r
94         function parse_parsedinclude($filename) {\r
95                 // check current level\r
96                 if ($this->level > 3) return;   // max. depth reached (avoid endless loop)\r
97                 $file = $this->getIncludeFileName($filename);\r
98                 if (!file_exists($file)) return;\r
99         $contents = file_get_contents($file);\r
100         if (empty($contents)) return;\r
101                 \r
102                 $this->level = $this->level + 1;\r
103                 // parse file contents\r
104                 $this->parser->parse($contents);\r
105 \r
106                 $this->level = $this->level - 1;\r
107         }\r
108 \r
109         /**\r
110          * Returns the correct location of the file to be included, according to\r
111          * parser properties\r
112          *\r
113          * IF IncludeMode = 'skindir' => use skindir\r
114          * \r
115          * @param $filename\r
116          */\r
117         function getIncludeFileName($filename) {\r
118                 // leave absolute filenames and http urls as they are\r
119                 if (\r
120                                 (i18n::substr($filename,0,1) == '/')\r
121                         ||      (i18n::substr($filename,0,7) == 'http://')\r
122                         ||      (i18n::substr($filename,0,6) == 'ftp://')\r
123                         )\r
124                         return $filename;\r
125 \r
126                 $filename = Parser::getProperty('IncludePrefix') . $filename;\r
127                 if (Parser::getProperty('IncludeMode') == 'skindir') {\r
128                         global $DIR_SKINS;\r
129                         return $DIR_SKINS . $filename;\r
130                 } else {\r
131                         return $filename;\r
132                 }\r
133         }\r
134 \r
135         /**\r
136          * Inserts an url relative to the skindir (useful when doing import/export)\r
137          *\r
138          * e.g. <skinfile(default/myfile.sth)>\r
139          */\r
140         function parse_skinfile($filename) {\r
141                 global $CONF;\r
142 \r
143                 echo $CONF['SkinsURL'] . Parser::getProperty('IncludePrefix') . $filename;\r
144         }\r
145 \r
146         /**\r
147          * Sets a property for the parser\r
148          */\r
149         function parse_set($property, $value) {\r
150                 Parser::setProperty($property, $value);\r
151         }\r
152 \r
153         /**\r
154          * Helper function: add if condition\r
155          */\r
156         function _addIfCondition($condition) {\r
157 \r
158                 array_push($this->if_conditions,$condition);\r
159 \r
160                 $this->_updateTopIfCondition();\r
161 \r
162                 ob_start();\r
163         }\r
164 \r
165         /**\r
166          * Helper function: update the Top of the If Conditions Array\r
167          */\r
168         function _updateTopIfCondition() {\r
169                 if (sizeof($this->if_conditions) == 0) {\r
170                         $this->if_currentlevel = 1;\r
171                 }\r
172                 else {\r
173                         $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];\r
174                 }\r
175         }\r
176 \r
177         /**\r
178          * Helper function for elseif / elseifnot\r
179          */\r
180         function _addIfExecute() {\r
181                 array_push($this->if_execute, 0);\r
182         }\r
183 \r
184         /**\r
185          * Helper function for elseif / elseifnot\r
186          * @param string condition to be fullfilled\r
187          */\r
188         function _updateIfExecute($condition) {\r
189                 $index = sizeof($this->if_execute) - 1;\r
190                 $this->if_execute[$index] = $this->if_execute[$index] || $condition;\r
191         }\r
192 \r
193         /**\r
194          * returns the currently top if condition\r
195          */\r
196         function _getTopIfCondition() {\r
197                 return $this->if_currentlevel;\r
198         }\r
199 \r
200         /**\r
201          * Sets the search terms to be highlighted\r
202          *\r
203          * @param $highlight\r
204          *              A series of search terms\r
205          */\r
206         function setHighlight($highlight) {\r
207                 $this->strHighlight = $highlight;\r
208                 if ($highlight) {\r
209                         $this->aHighlight = parseHighlight($highlight);\r
210                 }\r
211         }\r
212         \r
213         /**\r
214          * Applies the highlight to the given piece of text\r
215          *\r
216          * @see setHighlight\r
217          * @param String        $data   Data that needs to be highlighted\r
218          * @return      String  hilighted data\r
219          */\r
220         function highlight($data)\r
221         {\r
222                 if ( $this->aHighlight )\r
223                 {\r
224                         $data = Entity::highlight($data, $this->aHighlight, $this->template['SEARCH_HIGHLIGHT']);\r
225                 }\r
226                 return $data;\r
227         }\r
228         \r
229         /**\r
230          * Parses <%if%> statements\r
231          */\r
232         function parse_if() {\r
233                 $this->_addIfExecute();\r
234 \r
235                 $args = func_get_args();\r
236                 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);\r
237                 $this->_addIfCondition($condition);\r
238         }\r
239 \r
240         /**\r
241          * Parses <%else%> statements\r
242          */\r
243         function parse_else() {\r
244                 if (sizeof($this->if_conditions) == 0) return;\r
245                 array_pop($this->if_conditions);\r
246                 if ($this->if_currentlevel) {\r
247                         ob_end_flush();\r
248                         $this->_updateIfExecute(1);\r
249                         $this->_addIfCondition(0);\r
250                 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {\r
251                         ob_end_clean();\r
252                         $this->_addIfCondition(0);\r
253                 } else {\r
254                         ob_end_clean();\r
255                         $this->_addIfCondition(1);\r
256                 }\r
257         }\r
258 \r
259         /**\r
260          * Parses <%elseif%> statements\r
261          */\r
262         function parse_elseif() {\r
263                 if (sizeof($this->if_conditions) == 0) return;\r
264                 array_pop($this->if_conditions);\r
265                 if ($this->if_currentlevel) {\r
266                         ob_end_flush();\r
267                         $this->_updateIfExecute(1);\r
268                         $this->_addIfCondition(0);\r
269                 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {\r
270                         ob_end_clean();\r
271                         $this->_addIfCondition(0);\r
272                 } else {\r
273                         ob_end_clean();\r
274                         $args = func_get_args();\r
275                         $condition = call_user_func_array(array(&$this,'checkCondition'), $args);\r
276                         $this->_addIfCondition($condition);\r
277                 }\r
278         }\r
279 \r
280         /**\r
281          * Parses <%ifnot%> statements\r
282          */\r
283         function parse_ifnot() {\r
284                 $this->_addIfExecute();\r
285 \r
286                 $args = func_get_args();\r
287                 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);\r
288                 $this->_addIfCondition(!$condition);\r
289         }\r
290 \r
291         /**\r
292          * Parses <%elseifnot%> statements\r
293          */\r
294         function parse_elseifnot() {\r
295                 if (sizeof($this->if_conditions) == 0) return;\r
296                 array_pop($this->if_conditions);\r
297                 if ($this->if_currentlevel) {\r
298                         ob_end_flush();\r
299                         $this->_updateIfExecute(1);\r
300                         $this->_addIfCondition(0);\r
301                 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {\r
302                         ob_end_clean();\r
303                         $this->_addIfCondition(0);\r
304                 } else {\r
305                         ob_end_clean();\r
306                         $args = func_get_args();\r
307                         $condition = call_user_func_array(array(&$this,'checkCondition'), $args);\r
308                         $this->_addIfCondition(!$condition);\r
309                 }\r
310         }\r
311 \r
312         /**\r
313          * Ends a conditional if-block\r
314          * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)\r
315          */\r
316         function parse_endif() {\r
317                 // we can only close what has been opened\r
318                 if (sizeof($this->if_conditions) == 0) return;\r
319 \r
320                 if ($this->if_currentlevel) {\r
321                         ob_end_flush();\r
322                 } else {\r
323                         ob_end_clean();\r
324                 }\r
325                 array_pop($this->if_conditions);\r
326                 array_pop($this->if_execute);\r
327 \r
328                 $this->_updateTopIfCondition();\r
329         }\r
330 }\r
331 ?>\r