OSDN Git Service

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