OSDN Git Service

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