OSDN Git Service

ADD: <%parsedinclude%>でスキン内でスペシャルスキンパーツを展開する機能を追加
[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  $name   filename to be included
187          * @return      void
188          */
189         public function parse_parsedinclude($name)
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($name);
199                 if ( !file_exists($file) && $this->parser->skin != NULL)
200                 {
201                         $contents = $this->parser->skin->getContentFromDB($name);
202                 }
203                 else
204                 {
205                         $contents = file_get_contents($file);
206                 }
207                 
208                 if ( empty($contents) )
209                 {
210                         return;
211                 }
212                 
213                 $this->level = $this->level + 1;
214                 // parse file contents
215                 $this->parser->parse($contents);
216                 
217                 $this->level = $this->level - 1;
218                 return;
219         }
220         
221         /**
222          * BaseActions::getIncludeFileName()
223          * Returns the correct location of the file to be included, according to
224          * parser properties
225          *
226          * IF IncludeMode = 'skindir' => use skindir
227          * 
228          * @param       string  $filename       name of file to be inclluded
229          * @return      string  name of file with relative path
230          */
231         private function getIncludeFileName($filename)
232         {
233                 // leave absolute filenames and http urls as they are
234                 if (
235                                 (i18n::substr($filename,0,1) == '/')
236                         ||      (i18n::substr($filename,0,7) == 'http://')
237                         ||      (i18n::substr($filename,0,6) == 'ftp://')
238                         )
239                 {
240                         return $filename;
241                 }
242                 
243                 $filename = Parser::getProperty('IncludePrefix') . $filename;
244                 
245                 if ( Parser::getProperty('IncludeMode') == 'skindir' )
246                 {
247                         global $DIR_SKINS;
248                         return $DIR_SKINS . $filename;
249                 }
250                 return $filename;
251         }
252         
253         /**
254          * BaseActions::parse_skinfile()
255          * Inserts an url relative to the skindir (useful when doing import/export)
256          *
257          * e.g. <skinfile(default/myfile.sth)>
258          * 
259          * @param       string  $filename       name of file to be inclluded
260          * @return      void
261          */
262         public function parse_skinfile($filename)
263         {
264                 global $CONF;
265                 echo $CONF['SkinsURL'] . Parser::getProperty('IncludePrefix') . $filename;
266                 return;
267         }
268
269         /**
270          * BaseActions::parse_set()
271          * Sets a property for the parser
272          * 
273          * @param       string  $property       name of property
274          * @param       string  $value          value of property
275          * @return      void
276          */
277         public function parse_set($property, $value)
278         {
279                 Parser::setProperty($property, $value);
280                 return;
281         }
282         
283         /**
284          * BaseActions::parse_text()
285          * Parse text
286          * 
287          * @param       string  $constant       named constant
288          * @return      void
289          */
290         public function parse_text($constant)
291         {
292                 if ( !defined($constant) )
293                 {
294                         echo Entity::hsc($constant);
295                 }
296                 else
297                 {
298                         echo Entity::hsc(constant($constant));
299                 }
300                 return;
301         }
302         
303         /**
304          * BaseActions::addIfCondition()
305          * Helper function: add if condition
306          * 
307          * @param       string  $condition      condition for if context
308          * @return      void
309          */
310         private function addIfCondition($condition)
311         {
312                 array_push($this->if_conditions,$condition);
313                 $this->updateTopIfCondition();
314                 ob_start();
315                 return;
316         }
317         
318         /**
319          * BaseActions::updateTopIfCondition()
320          * Helper function: update the Top of the If Conditions Array
321          * 
322          * @param       void
323          * @return      void
324          */
325         private function updateTopIfCondition()
326         {
327                 if ( sizeof($this->if_conditions) == 0 )
328                 {
329                         $this->if_currentlevel = 1;
330                 }
331                 else
332                 {
333                         $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
334                 }
335                 return;
336         }
337         
338         /**
339          * BaseActions::addIfExecute()
340          * Helper function for elseif / elseifnot
341          * 
342          * @param       void
343          * @return      void
344          */
345         private function addIfExecute()
346         {
347                 array_push($this->if_execute, 0);
348                 return;
349         }
350         
351         /**
352          * BaseActions::updateIfExecute()
353          * Helper function for elseif / elseifnot
354          * 
355          * @param       string  $condition      condition to be fullfilled
356          * @return      void
357          */
358         private function updateIfExecute($condition)
359         {
360                 $index = sizeof($this->if_execute) - 1;
361                 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
362                 return;
363         }
364
365         /**
366          * BaseActions::getTopIfCondition()()
367          * returns the currently top if condition
368          * 
369          * @param       void
370          * @return      string  level
371          */
372         public function getTopIfCondition()
373         {
374                 return $this->if_currentlevel;
375         }
376         
377         /**
378          * BaseActions::setHighlight(()
379          * Sets the search terms to be highlighted
380          *
381          * @param       string  $highlight      A series of search terms
382          * @return      void
383          */
384         public function setHighlight($highlight)
385         {
386                 $this->strHighlight = $highlight;
387                 if ( $highlight )
388                 {
389                         $this->aHighlight = parseHighlight($highlight);
390                 }
391                 return;
392         }
393         
394         /**
395          * BaseActions::highlight()
396          * Applies the highlight to the given piece of text
397          *
398          * @see setHighlight
399          * @param       string  $data           Data that needs to be highlighted
400          * @return      string  hilighted data
401          */
402         public function highlight($data)
403         {
404                 if ( $this->aHighlight )
405                 {
406                         $data = Entity::highlight($data, $this->aHighlight, $this->template['SEARCH_HIGHLIGHT']);
407                 }
408                 return $data;
409         }
410         
411         /**
412          * BaseActions::parse_if()
413          * Parses <%if%> statements
414          * 
415          * @param       void
416          * @return      void
417          */
418         public function parse_if()
419         {
420                 $this->addIfExecute();
421                 $args = func_get_args();
422                 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
423                 $this->addIfCondition($condition);
424                 return;
425         }
426
427         /**
428          * BaseActions::parse_else()
429          * Parses <%else%> statements
430          * 
431          * @param       void
432          * @return      void
433          */
434         public function parse_else()
435         {
436                 if ( sizeof($this->if_conditions) == 0 )
437                 {
438                         return;
439                 }
440                 
441                 array_pop($this->if_conditions);
442                 
443                 if ( $this->if_currentlevel )
444                 {
445                         ob_end_flush();
446                         $this->updateIfExecute(1);
447                         $this->addIfCondition(0);
448                 }
449                 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
450                 {
451                         ob_end_clean();
452                         $this->addIfCondition(0);
453                 }
454                 else
455                 {
456                         ob_end_clean();
457                         $this->addIfCondition(1);
458                 }
459                 return;
460         }
461         
462         /**
463          * BaseActions::parse_elseif()
464          * Parses <%elseif%> statements
465          * 
466          * @param       void
467          * @return      void
468          */
469         public function parse_elseif()
470         {
471                 if ( sizeof($this->if_conditions) == 0 )
472                 {
473                         return;
474                 }
475                 
476                 array_pop($this->if_conditions);
477                 
478                 if ( $this->if_currentlevel )
479                 {
480                         ob_end_flush();
481                         $this->updateIfExecute(1);
482                         $this->addIfCondition(0);
483                 }
484                 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
485                 {
486                         ob_end_clean();
487                         $this->addIfCondition(0);
488                 }
489                 else
490                 {
491                         ob_end_clean();
492                         $args = func_get_args();
493                         $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
494                         $this->addIfCondition($condition);
495                 }
496                 return;
497         }
498         
499         /**
500          * BaseActions::parse_ifnot()
501          * Parses <%ifnot%> statements
502          * 
503          * @param       void
504          * @return      void
505          */
506         public function parse_ifnot()
507         {
508                 $this->addIfExecute();
509                 
510                 $args = func_get_args();
511                 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
512                 $this->addIfCondition(!$condition);
513                 return;
514         }
515         
516         /**
517          * BaseActions::parse_elseifnot()
518          * Parses <%elseifnot%> statements
519          * 
520          * @param       void
521          * @return      void
522          */
523         public function parse_elseifnot()
524         {
525                 if ( sizeof($this->if_conditions) == 0 )
526                 {
527                         return;
528                 }
529                 
530                 array_pop($this->if_conditions);
531                 
532                 if ( $this->if_currentlevel )
533                 {
534                         ob_end_flush();
535                         $this->updateIfExecute(1);
536                         $this->addIfCondition(0);
537                 }
538                 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
539                 {
540                         ob_end_clean();
541                         $this->addIfCondition(0);
542                 }
543                 else
544                 {
545                         ob_end_clean();
546                         $args = func_get_args();
547                         $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
548                         $this->addIfCondition(!$condition);
549                 }
550                 return;
551         }
552
553         /**
554          * BaseActions::parse_endif()
555          * Ends a conditional if-block
556          * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
557          * 
558          * @param       void
559          * @return      void
560          */
561         public function parse_endif()
562         {
563                 // we can only close what has been opened
564                 if ( sizeof($this->if_conditions) == 0 )
565                 {
566                         return;
567                 }
568                 
569                 if ( $this->if_currentlevel )
570                 {
571                         ob_end_flush();
572                 }
573                 else
574                 {
575                         ob_end_clean();
576                 }
577                 
578                 array_pop($this->if_conditions);
579                 array_pop($this->if_execute);
580                 
581                 $this->updateTopIfCondition();
582                 return;
583         }
584 }