OSDN Git Service

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