OSDN Git Service

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