OSDN Git Service

本家Nucleus CMS 4.0のリビジョン1626をコミット
[nucleus-jp/nucleus-next.git] / nucleus / libs / BaseActions.php
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 1527 2011-06-21 10:43:44Z sakamocchi $
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         /**
51          *  Constructor for a new BaseAction object
52          */
53         function BaseActions() {
54                 $this->level = 0;
55
56                 // if nesting level
57                 $this->if_conditions = array(); // array on which condition values are pushed/popped
58                 $this->if_execute = array();    // array on which condition values are pushed/popped
59                 $this->if_currentlevel = 1;             // 1 = current level is displayed; 0 = current level not displayed
60
61                 // highlights
62                 $this->strHighlight = '';                       // full highlight
63                 $this->aHighlight = array();            // parsed highlight
64
65         }
66
67         /**
68          * include file (no parsing of php)
69          * 
70          * ToDo: function returns nothing and refering to the cross reference it
71          *       isn't called from anywhere   
72          * 
73          * @param $filename
74          */
75         function parse_include($filename) {
76                 @readfile($this->getIncludeFileName($filename));
77         }
78
79         /**
80          * php-include file
81          * 
82          * @param $filename
83          */
84         function parse_phpinclude($filename) {
85                 includephp($this->getIncludeFileName($filename));
86         }
87
88         
89         /**
90          * parsed include
91          * 
92          * @param $filename
93          */
94         function parse_parsedinclude($filename) {
95                 // check current level
96                 if ($this->level > 3) return;   // max. depth reached (avoid endless loop)
97                 $file = $this->getIncludeFileName($filename);
98                 if (!file_exists($file)) return;
99         $contents = file_get_contents($file);
100         if (empty($contents)) return;
101                 
102                 $this->level = $this->level + 1;
103                 // parse file contents
104                 $this->parser->parse($contents);
105
106                 $this->level = $this->level - 1;
107         }
108
109         /**
110          * Returns the correct location of the file to be included, according to
111          * parser properties
112          *
113          * IF IncludeMode = 'skindir' => use skindir
114          * 
115          * @param $filename
116          */
117         function getIncludeFileName($filename) {
118                 // leave absolute filenames and http urls as they are
119                 if (
120                                 (i18n::substr($filename,0,1) == '/')
121                         ||      (i18n::substr($filename,0,7) == 'http://')
122                         ||      (i18n::substr($filename,0,6) == 'ftp://')
123                         )
124                         return $filename;
125
126                 $filename = PARSER::getProperty('IncludePrefix') . $filename;
127                 if (PARSER::getProperty('IncludeMode') == 'skindir') {
128                         global $DIR_SKINS;
129                         return $DIR_SKINS . $filename;
130                 } else {
131                         return $filename;
132                 }
133         }
134
135         /**
136          * Inserts an url relative to the skindir (useful when doing import/export)
137          *
138          * e.g. <skinfile(default/myfile.sth)>
139          */
140         function parse_skinfile($filename) {
141                 global $CONF;
142
143                 echo $CONF['SkinsURL'] . PARSER::getProperty('IncludePrefix') . $filename;
144         }
145
146         /**
147          * Sets a property for the parser
148          */
149         function parse_set($property, $value) {
150                 PARSER::setProperty($property, $value);
151         }
152
153         /**
154          * Helper function: add if condition
155          */
156         function _addIfCondition($condition) {
157
158                 array_push($this->if_conditions,$condition);
159
160                 $this->_updateTopIfCondition();
161
162                 ob_start();
163         }
164
165         /**
166          * Helper function: update the Top of the If Conditions Array
167          */
168         function _updateTopIfCondition() {
169                 if (sizeof($this->if_conditions) == 0) {
170                         $this->if_currentlevel = 1;
171                 }
172                 else {
173                         $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
174                 }
175         }
176
177         /**
178          * Helper function for elseif / elseifnot
179          */
180         function _addIfExecute() {
181                 array_push($this->if_execute, 0);
182         }
183
184         /**
185          * Helper function for elseif / elseifnot
186          * @param string condition to be fullfilled
187          */
188         function _updateIfExecute($condition) {
189                 $index = sizeof($this->if_execute) - 1;
190                 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
191         }
192
193         /**
194          * returns the currently top if condition
195          */
196         function _getTopIfCondition() {
197                 return $this->if_currentlevel;
198         }
199
200         /**
201          * Sets the search terms to be highlighted
202          *
203          * @param $highlight
204          *              A series of search terms
205          */
206         function setHighlight($highlight) {
207                 $this->strHighlight = $highlight;
208                 if ($highlight) {
209                         $this->aHighlight = parseHighlight($highlight);
210                 }
211         }
212
213         /**
214          * Applies the highlight to the given piece of text
215          *
216          * @param &$data
217          *              Data that needs to be highlighted
218          * @see setHighlight
219          */
220         function highlight(&$data) {
221                 if ($this->aHighlight)
222                         return highlight($data,$this->aHighlight,$this->template['SEARCH_HIGHLIGHT']);
223                 else
224                         return $data;
225         }
226
227         /**
228          * Parses <%if%> statements
229          */
230         function parse_if() {
231                 $this->_addIfExecute();
232
233                 $args = func_get_args();
234                 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
235                 $this->_addIfCondition($condition);
236         }
237
238         /**
239          * Parses <%else%> statements
240          */
241         function parse_else() {
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                         $this->_addIfCondition(1);
254                 }
255         }
256
257         /**
258          * Parses <%elseif%> statements
259          */
260         function parse_elseif() {
261                 if (sizeof($this->if_conditions) == 0) return;
262                 array_pop($this->if_conditions);
263                 if ($this->if_currentlevel) {
264                         ob_end_flush();
265                         $this->_updateIfExecute(1);
266                         $this->_addIfCondition(0);
267                 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
268                         ob_end_clean();
269                         $this->_addIfCondition(0);
270                 } else {
271                         ob_end_clean();
272                         $args = func_get_args();
273                         $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
274                         $this->_addIfCondition($condition);
275                 }
276         }
277
278         /**
279          * Parses <%ifnot%> statements
280          */
281         function parse_ifnot() {
282                 $this->_addIfExecute();
283
284                 $args = func_get_args();
285                 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
286                 $this->_addIfCondition(!$condition);
287         }
288
289         /**
290          * Parses <%elseifnot%> statements
291          */
292         function parse_elseifnot() {
293                 if (sizeof($this->if_conditions) == 0) return;
294                 array_pop($this->if_conditions);
295                 if ($this->if_currentlevel) {
296                         ob_end_flush();
297                         $this->_updateIfExecute(1);
298                         $this->_addIfCondition(0);
299                 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
300                         ob_end_clean();
301                         $this->_addIfCondition(0);
302                 } else {
303                         ob_end_clean();
304                         $args = func_get_args();
305                         $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
306                         $this->_addIfCondition(!$condition);
307                 }
308         }
309
310         /**
311          * Ends a conditional if-block
312          * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
313          */
314         function parse_endif() {
315                 // we can only close what has been opened
316                 if (sizeof($this->if_conditions) == 0) return;
317
318                 if ($this->if_currentlevel) {
319                         ob_end_flush();
320                 } else {
321                         ob_end_clean();
322                 }
323                 array_pop($this->if_conditions);
324                 array_pop($this->if_execute);
325
326                 $this->_updateTopIfCondition();
327         }
328 }
329 ?>