OSDN Git Service

CHANGE: フィードとゲストアカウント作成フォームのためのスクリプトを修正
[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 1527 2011-06-21 10:43:44Z sakamocchi $
21  */
22
23 class BaseActions {
24
25         // depth level for includes (max. level is 3)
26         var $level;
27
28         // array of evaluated conditions (true/false). The element at the end is the one for the most nested
29         // if block.
30         var $if_conditions;
31
32         // in the "elseif" / "elseifnot" sequences, if one of the conditions become "true" remained conditions should not
33         // be tested. this variable (actually a stack) holds this information.
34         var $if_execute;
35
36         // at all times, can be evaluated to either true if the current block needs to be displayed. This
37         // variable is used to decide to skip skinvars in parts that will never be outputted.
38         var $if_currentlevel;
39
40         // contains a search string with keywords that need to be highlighted. These get parsed into $aHighlight
41         var $strHighlight;
42
43         // array of keywords that need to be highlighted in search results (see the highlight()
44         // and parseHighlight() methods)
45         var $aHighlight;
46
47         // reference to the parser object that is using this object as actions-handler
48         var $parser;
49
50         /**
51          *  Constructor for a new BaseAction object
52          */
53         function BaseActions() {
54                 $this->level = 0;
55
56                 // if nesting level
57                 $this->if_conditions = array(); // array on which condition values are pushed/popped
58                 $this->if_execute = array();    // array on which condition values are pushed/popped
59                 $this->if_currentlevel = 1;             // 1 = current level is displayed; 0 = current level not displayed
60
61                 // highlights
62                 $this->strHighlight = '';                       // full highlight
63                 $this->aHighlight = array();            // parsed highlight
64
65         }
66
67         /**
68          * include file (no parsing of php)
69          * 
70          * ToDo: function returns nothing and refering to the cross reference it
71          *       isn't called from anywhere   
72          * 
73          * @param $filename
74          */
75         function parse_include($filename) {
76                 @readfile($this->getIncludeFileName($filename));
77         }
78
79         /**
80          * php-include file
81          * 
82          * @param $filename
83          */
84         function parse_phpinclude($filename) {
85                 includephp($this->getIncludeFileName($filename));
86         }
87
88         
89         /**
90          * parsed include
91          * 
92          * @param $filename
93          */
94         function parse_parsedinclude($filename) {
95                 // check current level
96                 if ($this->level > 3) return;   // max. depth reached (avoid endless loop)
97                 $file = $this->getIncludeFileName($filename);
98                 if (!file_exists($file)) return;
99         $contents = file_get_contents($file);
100         if (empty($contents)) return;
101                 
102                 $this->level = $this->level + 1;
103                 // parse file contents
104                 $this->parser->parse($contents);
105
106                 $this->level = $this->level - 1;
107         }
108
109         /**
110          * Returns the correct location of the file to be included, according to
111          * parser properties
112          *
113          * IF IncludeMode = 'skindir' => use skindir
114          * 
115          * @param $filename
116          */
117         function getIncludeFileName($filename) {
118                 // leave absolute filenames and http urls as they are
119                 if (
120                                 (i18n::substr($filename,0,1) == '/')
121                         ||      (i18n::substr($filename,0,7) == 'http://')
122                         ||      (i18n::substr($filename,0,6) == 'ftp://')
123                         )
124                         return $filename;
125
126                 $filename = PARSER::getProperty('IncludePrefix') . $filename;
127                 if (PARSER::getProperty('IncludeMode') == 'skindir') {
128                         global $DIR_SKINS;
129                         return $DIR_SKINS . $filename;
130                 } else {
131                         return $filename;
132                 }
133         }
134
135         /**
136          * Inserts an url relative to the skindir (useful when doing import/export)
137          *
138          * e.g. <skinfile(default/myfile.sth)>
139          */
140         function parse_skinfile($filename) {
141                 global $CONF;
142
143                 echo $CONF['SkinsURL'] . PARSER::getProperty('IncludePrefix') . $filename;
144         }
145
146         /**
147          * Sets a property for the parser
148          */
149         function parse_set($property, $value) {
150                 PARSER::setProperty($property, $value);
151         }
152
153         /**
154          * Helper function: add if condition
155          */
156         function _addIfCondition($condition) {
157
158                 array_push($this->if_conditions,$condition);
159
160                 $this->_updateTopIfCondition();
161
162                 ob_start();
163         }
164
165         /**
166          * Helper function: update the Top of the If Conditions Array
167          */
168         function _updateTopIfCondition() {
169                 if (sizeof($this->if_conditions) == 0) {
170                         $this->if_currentlevel = 1;
171                 }
172                 else {
173                         $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
174                 }
175         }
176
177         /**
178          * Helper function for elseif / elseifnot
179          */
180         function _addIfExecute() {
181                 array_push($this->if_execute, 0);
182         }
183
184         /**
185          * Helper function for elseif / elseifnot
186          * @param string condition to be fullfilled
187          */
188         function _updateIfExecute($condition) {
189                 $index = sizeof($this->if_execute) - 1;
190                 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
191         }
192
193         /**
194          * returns the currently top if condition
195          */
196         function _getTopIfCondition() {
197                 return $this->if_currentlevel;
198         }
199
200         /**
201          * Sets the search terms to be highlighted
202          *
203          * @param $highlight
204          *              A series of search terms
205          */
206         function setHighlight($highlight) {
207                 $this->strHighlight = $highlight;
208                 if ($highlight) {
209                         $this->aHighlight = parseHighlight($highlight);
210                 }
211         }
212         
213         /**
214          * Applies the highlight to the given piece of text
215          *
216          * @see setHighlight
217          * @param String        $data   Data that needs to be highlighted
218          * @return      String  hilighted data
219          */
220         function highlight($data)
221         {
222                 if ( $this->aHighlight )
223                 {
224                         $data = ENTITY::highlight($data, $this->aHighlight, $this->template['SEARCH_HIGHLIGHT']);
225                 }
226                 return $data;
227         }
228         
229         /**
230          * Parses <%if%> statements
231          */
232         function parse_if() {
233                 $this->_addIfExecute();
234
235                 $args = func_get_args();
236                 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
237                 $this->_addIfCondition($condition);
238         }
239
240         /**
241          * Parses <%else%> statements
242          */
243         function parse_else() {
244                 if (sizeof($this->if_conditions) == 0) return;
245                 array_pop($this->if_conditions);
246                 if ($this->if_currentlevel) {
247                         ob_end_flush();
248                         $this->_updateIfExecute(1);
249                         $this->_addIfCondition(0);
250                 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
251                         ob_end_clean();
252                         $this->_addIfCondition(0);
253                 } else {
254                         ob_end_clean();
255                         $this->_addIfCondition(1);
256                 }
257         }
258
259         /**
260          * Parses <%elseif%> statements
261          */
262         function parse_elseif() {
263                 if (sizeof($this->if_conditions) == 0) return;
264                 array_pop($this->if_conditions);
265                 if ($this->if_currentlevel) {
266                         ob_end_flush();
267                         $this->_updateIfExecute(1);
268                         $this->_addIfCondition(0);
269                 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
270                         ob_end_clean();
271                         $this->_addIfCondition(0);
272                 } else {
273                         ob_end_clean();
274                         $args = func_get_args();
275                         $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
276                         $this->_addIfCondition($condition);
277                 }
278         }
279
280         /**
281          * Parses <%ifnot%> statements
282          */
283         function parse_ifnot() {
284                 $this->_addIfExecute();
285
286                 $args = func_get_args();
287                 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
288                 $this->_addIfCondition(!$condition);
289         }
290
291         /**
292          * Parses <%elseifnot%> statements
293          */
294         function parse_elseifnot() {
295                 if (sizeof($this->if_conditions) == 0) return;
296                 array_pop($this->if_conditions);
297                 if ($this->if_currentlevel) {
298                         ob_end_flush();
299                         $this->_updateIfExecute(1);
300                         $this->_addIfCondition(0);
301                 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
302                         ob_end_clean();
303                         $this->_addIfCondition(0);
304                 } else {
305                         ob_end_clean();
306                         $args = func_get_args();
307                         $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
308                         $this->_addIfCondition(!$condition);
309                 }
310         }
311
312         /**
313          * Ends a conditional if-block
314          * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
315          */
316         function parse_endif() {
317                 // we can only close what has been opened
318                 if (sizeof($this->if_conditions) == 0) return;
319
320                 if ($this->if_currentlevel) {
321                         ob_end_flush();
322                 } else {
323                         ob_end_clean();
324                 }
325                 array_pop($this->if_conditions);
326                 array_pop($this->if_execute);
327
328                 $this->_updateTopIfCondition();
329         }
330 }
331 ?>