OSDN Git Service

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