OSDN Git Service

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