OSDN Git Service

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