OSDN Git Service

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