OSDN Git Service

MERGE: リビジョン1729のマージ。BaseActionsクラスのコード整理。
[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-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 /**
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-2012 The Nucleus Group
20  * @version $Id: BaseActions.php 1729 2012-04-07 05:07:08Z sakamocchi $
21  */
22
23 class BaseActions
24 {
25         // depth level for includes (max. level is 3)
26         public $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         public $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         public $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         public $if_currentlevel;
39         
40         // contains a search string with keywords that need to be highlighted. These get parsed into $aHighlight
41         public $strHighlight;
42         
43         // array of keywords that need to be highlighted in search results (see the highlight()
44         // and parseHighlight() methods)
45         public $aHighlight;
46         
47         // reference to the parser object that is using this object as actions-handler
48         public $parser;
49         
50         /**
51          * BaseActions::BaseActions()
52          *  Constructor for a new BaseAction object
53          */
54         protected function BaseActions()
55         {
56                 $this->level = 0;
57                 
58                 // if nesting level
59                 $this->if_conditions = array(); // array on which condition values are pushed/popped
60                 $this->if_execute = array();    // array on which condition values are pushed/popped
61                 $this->if_currentlevel = 1;             // 1 = current level is displayed; 0 = current level not displayed
62                 
63                 // highlights
64                 $this->strHighlight = '';                       // full highlight
65                 $this->aHighlight = array();            // parsed highlight
66                 return;
67         }
68         
69         /**
70          * BaseActions::parse_include()
71          * include file (no parsing of php)
72          * 
73          * ToDo: function returns nothing and refering to the cross reference it
74          *       isn't called from anywhere   
75          * 
76          * @param       string  $filename       filename to be included
77          * @return      void
78          */
79         public function parse_include($filename)
80         {
81                 @readfile($this->getIncludeFileName($filename));
82                 return;
83         }
84         
85         /**
86          * BaseActions::parse_phpinclude()
87          * php-include file
88          * 
89          * @param       string  $filename       filename to be included
90          * @return      void
91          */
92         public function parse_phpinclude($filename)
93         {
94                 includephp($this->getIncludeFileName($filename));
95                 return;
96         }
97         
98         
99         /**
100          * BaseActions::parse_parsedinclude()
101          * parsed include
102          * 
103          * @param       string  $filename       filename to be included
104          * @return      void
105          */
106         public function parse_parsedinclude($filename)
107         {
108                 // check current level
109                 if ( $this->level > 3 )
110                 {
111                         // max. depth reached (avoid endless loop)
112                         return;
113                 }
114                 
115                 $file = $this->getIncludeFileName($filename);
116                 
117                 if ( !file_exists($file) )
118                 {
119                         return;
120                 }
121                 
122                 $contents = file_get_contents($file);
123                 
124                 if ( empty($contents) )
125                 {
126                         return;
127                 }
128                 
129                 $this->level = $this->level + 1;
130                 // parse file contents
131                 $this->parser->parse($contents);
132                 
133                 $this->level = $this->level - 1;
134                 return;
135         }
136         
137         /**
138          * BaseActions::getIncludeFileName()
139          * Returns the correct location of the file to be included, according to
140          * parser properties
141          *
142          * IF IncludeMode = 'skindir' => use skindir
143          * 
144          * @param       string  $filename       name of file to be inclluded
145          * @return      string  name of file with relative path
146          */
147         public function getIncludeFileName($filename)
148         {
149                 // leave absolute filenames and http urls as they are
150                 if (
151                                 (i18n::substr($filename,0,1) == '/')
152                         ||      (i18n::substr($filename,0,7) == 'http://')
153                         ||      (i18n::substr($filename,0,6) == 'ftp://')
154                         )
155                 {
156                         return $filename;
157                 }
158                 
159                 $filename = Parser::getProperty('IncludePrefix') . $filename;
160                 
161                 if ( Parser::getProperty('IncludeMode') == 'skindir' )
162                 {
163                         global $DIR_SKINS;
164                         return $DIR_SKINS . $filename;
165                 }
166                 return $filename;
167         }
168         
169         /**
170          * BaseActions::parse_skinfile()
171          * Inserts an url relative to the skindir (useful when doing import/export)
172          *
173          * e.g. <skinfile(default/myfile.sth)>
174          * 
175          * @param       string  $filename       name of file to be inclluded
176          * @return      void
177          */
178         public function parse_skinfile($filename)
179         {
180                 global $CONF;
181                 echo $CONF['SkinsURL'] . Parser::getProperty('IncludePrefix') . $filename;
182                 return;
183         }
184
185         /**
186          * BaseActions::parse_set()
187          * Sets a property for the parser
188          * 
189          * @param       string  $property       name of property
190          * @param       string  $value          value of property
191          * @return      void
192          */
193         public function parse_set($property, $value)
194         {
195                 Parser::setProperty($property, $value);
196                 return;
197         }
198         
199         /**
200          * BaseActions::_addIfCondition()
201          * Helper function: add if condition
202          * 
203          * @param       string  $condition      condition for if context
204          * @return      void
205          */
206         protected function _addIfCondition($condition)
207         {
208                 array_push($this->if_conditions,$condition);
209                 $this->_updateTopIfCondition();
210                 ob_start();
211                 return;
212         }
213         
214         /**
215          * BaseActions::_updateTopIfCondition()
216          * Helper function: update the Top of the If Conditions Array
217          * 
218          * @param       void
219          * @return      void
220          */
221         protected function _updateTopIfCondition()
222         {
223                 if ( sizeof($this->if_conditions) == 0 )
224                 {
225                         $this->if_currentlevel = 1;
226                 }
227                 else
228                 {
229                         $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
230                 }
231                 return;
232         }
233         
234         /**
235          * BaseActions::_addIfExecute()
236          * Helper function for elseif / elseifnot
237          * 
238          * @param       void
239          * @return      void
240          */
241         protected function _addIfExecute()
242         {
243                 array_push($this->if_execute, 0);
244                 return;
245         }
246         
247         /**
248          * BaseActions::_updateIfExecute()
249          * Helper function for elseif / elseifnot
250          * 
251          * @param       string  $condition      condition to be fullfilled
252          * @return      void
253          */
254         protected function _updateIfExecute($condition)
255         {
256                 $index = sizeof($this->if_execute) - 1;
257                 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
258                 return;
259         }
260
261         /**
262          * BaseActions::_getTopIfCondition()()
263          * returns the currently top if condition
264          * 
265          * @param       void
266          * @return      string  level
267          */
268         protected function _getTopIfCondition()
269         {
270                 return $this->if_currentlevel;
271         }
272         
273         /**
274          * BaseActions::setHighlight(()
275          * Sets the search terms to be highlighted
276          *
277          * @param       string  $highlight      A series of search terms
278          * @return      void
279          */
280         public function setHighlight($highlight)
281         {
282                 $this->strHighlight = $highlight;
283                 if ( $highlight )
284                 {
285                         $this->aHighlight = parseHighlight($highlight);
286                 }
287                 return;
288         }
289         
290         /**
291          * BaseActions::highlight()
292          * Applies the highlight to the given piece of text
293          *
294          * @see setHighlight
295          * @param       string  $data           Data that needs to be highlighted
296          * @return      string  hilighted data
297          */
298         public function highlight($data)
299         {
300                 if ( $this->aHighlight )
301                 {
302                         $data = Entity::highlight($data, $this->aHighlight, $this->template['SEARCH_HIGHLIGHT']);
303                 }
304                 return $data;
305         }
306         
307         /**
308          * BaseActions::parse_if()
309          * Parses <%if%> statements
310          * 
311          * @param       void
312          * @return      void
313          */
314         public function parse_if()
315         {
316                 $this->_addIfExecute();
317                 $args = func_get_args();
318                 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
319                 $this->_addIfCondition($condition);
320                 return;
321         }
322
323         /**
324          * BaseActions::parse_else()
325          * Parses <%else%> statements
326          * 
327          * @param       void
328          * @return      void
329          */
330         public function parse_else()
331         {
332                 if ( sizeof($this->if_conditions) == 0 )
333                 {
334                         return;
335                 }
336                 
337                 array_pop($this->if_conditions);
338                 
339                 if ( $this->if_currentlevel )
340                 {
341                         ob_end_flush();
342                         $this->_updateIfExecute(1);
343                         $this->_addIfCondition(0);
344                 }
345                 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
346                 {
347                         ob_end_clean();
348                         $this->_addIfCondition(0);
349                 }
350                 else
351                 {
352                         ob_end_clean();
353                         $this->_addIfCondition(1);
354                 }
355                 return;
356         }
357         
358         /**
359          * BaseActions::parse_elseif()
360          * Parses <%elseif%> statements
361          * 
362          * @param       void
363          * @return      void
364          */
365         public function parse_elseif()
366         {
367                 if ( sizeof($this->if_conditions) == 0 )
368                 {
369                         return;
370                 }
371                 
372                 array_pop($this->if_conditions);
373                 
374                 if ( $this->if_currentlevel )
375                 {
376                         ob_end_flush();
377                         $this->_updateIfExecute(1);
378                         $this->_addIfCondition(0);
379                 }
380                 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
381                 {
382                         ob_end_clean();
383                         $this->_addIfCondition(0);
384                 }
385                 else
386                 {
387                         ob_end_clean();
388                         $args = func_get_args();
389                         $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
390                         $this->_addIfCondition($condition);
391                 }
392                 return;
393         }
394         
395         /**
396          * BaseActions::parse_ifnot()
397          * Parses <%ifnot%> statements
398          * 
399          * @param       void
400          * @return      void
401          */
402         public function parse_ifnot()
403         {
404                 $this->_addIfExecute();
405                 
406                 $args = func_get_args();
407                 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
408                 $this->_addIfCondition(!$condition);
409                 return;
410         }
411         
412         /**
413          * BaseActions::parse_elseifnot()
414          * Parses <%elseifnot%> statements
415          * 
416          * @param       void
417          * @return      void
418          */
419         public function parse_elseifnot()
420         {
421                 if ( sizeof($this->if_conditions) == 0 )
422                 {
423                         return;
424                 }
425                 
426                 array_pop($this->if_conditions);
427                 
428                 if ( $this->if_currentlevel )
429                 {
430                         ob_end_flush();
431                         $this->_updateIfExecute(1);
432                         $this->_addIfCondition(0);
433                 }
434                 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
435                 {
436                         ob_end_clean();
437                         $this->_addIfCondition(0);
438                 }
439                 else
440                 {
441                         ob_end_clean();
442                         $args = func_get_args();
443                         $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
444                         $this->_addIfCondition(!$condition);
445                 }
446                 return;
447         }
448
449         /**
450          * BaseActions::parse_endif()
451          * Ends a conditional if-block
452          * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
453          * 
454          * @param       void
455          * @return      void
456          */
457         public function parse_endif()
458         {
459                 // we can only close what has been opened
460                 if ( sizeof($this->if_conditions) == 0 )
461                 {
462                         return;
463                 }
464                 
465                 if ( $this->if_currentlevel )
466                 {
467                         ob_end_flush();
468                 }
469                 else
470                 {
471                         ob_end_clean();
472                 }
473                 
474                 array_pop($this->if_conditions);
475                 array_pop($this->if_execute);
476                 
477                 $this->_updateTopIfCondition();
478                 return;
479         }
480 }