OSDN Git Service

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