OSDN Git Service

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