OSDN Git Service

c0dbf7d7052e51cda16dd3a4793293cb5df8830a
[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-2011 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-2011 The Nucleus Group
20  * @version $Id$
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                 global $skinid;
80                 $skin = new SKIN($skinid);
81                 $file = $this->getIncludeFileName($filename);
82                 if (!$skin->isValid && !file_exists($file)) {
83                         return;
84                 }
85                 $contents = $skin->getContent($filename);
86                 if (!$contents) {
87             if (!file_exists($file)) return;
88             $contents = file_get_contents($file);
89             if (empty($contents)) return;
90                 }
91                 $this->level = $this->level + 1;
92                 // parse file contents
93                 $this->parser->parse($contents);
94
95                 $this->level = $this->level - 1;
96         }
97
98         /**
99          * Returns the correct location of the file to be included, according to
100          * parser properties
101          *
102          * IF IncludeMode = 'skindir' => use skindir
103          */
104         function getIncludeFileName($filename) {
105                 // leave absolute filenames and http urls as they are
106                 if (
107                                 (substr($filename,0,1) == '/')
108                         ||      (substr($filename,0,7) == 'http://')
109                         ||      (substr($filename,0,6) == 'ftp://')
110                         )
111                         return $filename;
112
113                 $filename = PARSER::getProperty('IncludePrefix') . $filename;
114                 if (PARSER::getProperty('IncludeMode') == 'skindir') {
115                         global $DIR_SKINS;
116                         return $DIR_SKINS . $filename;
117                 } else {
118                         return $filename;
119                 }
120         }
121
122         /**
123          * Inserts an url relative to the skindir (useful when doing import/export)
124          *
125          * e.g. <skinfile(default/myfile.sth)>
126          */
127         function parse_skinfile($filename) {
128                 global $CONF;
129
130                 echo $CONF['SkinsURL'] . PARSER::getProperty('IncludePrefix') . $filename;
131         }
132
133         /**
134          * Sets a property for the parser
135          */
136         function parse_set($property, $value) {
137                 PARSER::setProperty($property, $value);
138         }
139
140         /**
141          * Helper function: add if condition
142          */
143         function _addIfCondition($condition) {
144
145                 array_push($this->if_conditions,$condition);
146
147                 $this->_updateTopIfCondition();
148
149                 ob_start();
150         }
151
152         function _updateTopIfCondition() {
153                 if (sizeof($this->if_conditions) == 0)
154                         $this->if_currentlevel = 1;
155                 else
156                         $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
157         }
158
159         /**
160          * Helper function for elseif / elseifnot
161          */
162         function _addIfExecute() {
163                 array_push($this->if_execute, 0);
164         }
165
166         /**
167          * Helper function for elseif / elseifnot
168          * @param string condition to be fullfilled
169          */
170         function _updateIfExecute($condition) {
171                 $index = sizeof($this->if_execute) - 1;
172                 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
173         }
174
175         /**
176          * returns the currently top if condition
177          */
178         function _getTopIfCondition() {
179                 return $this->if_currentlevel;
180         }
181
182         /**
183          * Sets the search terms to be highlighted
184          *
185          * @param $highlight
186          *              A series of search terms
187          */
188         function setHighlight($highlight) {
189                 $this->strHighlight = $highlight;
190                 if ($highlight) {
191                         $this->aHighlight = parseHighlight($highlight);
192                 }
193         }
194
195         /**
196          * Applies the highlight to the given piece of text
197          *
198          * @param &$data
199          *              Data that needs to be highlighted
200          * @see setHighlight
201          */
202         function highlight(&$data) {
203                 if ($this->aHighlight)
204                         return highlight($data,$this->aHighlight,$this->template['SEARCH_HIGHLIGHT']);
205                 else
206                         return $data;
207         }
208
209         /**
210          * Parses <%if%> statements
211          */
212         function parse_if() {
213                 $this->_addIfExecute();
214
215                 $args = func_get_args();
216                 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
217                 $this->_addIfCondition($condition);
218         }
219
220         /**
221          * Parses <%else%> statements
222          */
223         function parse_else() {
224                 if (sizeof($this->if_conditions) == 0) return;
225                 array_pop($this->if_conditions);
226                 if ($this->if_currentlevel) {
227                         ob_end_flush();
228                         $this->_updateIfExecute(1);
229                         $this->_addIfCondition(0);
230                 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
231                         ob_end_clean();
232                         $this->_addIfCondition(0);
233                 } else {
234                         ob_end_clean();
235                         $this->_addIfCondition(1);
236                 }
237         }
238
239         /**
240          * Parses <%elseif%> statements
241          */
242         function parse_elseif() {
243                 if (sizeof($this->if_conditions) == 0) return;
244                 array_pop($this->if_conditions);
245                 if ($this->if_currentlevel) {
246                         ob_end_flush();
247                         $this->_updateIfExecute(1);
248                         $this->_addIfCondition(0);
249                 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
250                         ob_end_clean();
251                         $this->_addIfCondition(0);
252                 } else {
253                         ob_end_clean();
254                         $args = func_get_args();
255                         $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
256                         $this->_addIfCondition($condition);
257                 }
258         }
259
260         /**
261          * Parses <%ifnot%> statements
262          */
263         function parse_ifnot() {
264                 $this->_addIfExecute();
265
266                 $args = func_get_args();
267                 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
268                 $this->_addIfCondition(!$condition);
269         }
270
271         /**
272          * Parses <%elseifnot%> statements
273          */
274         function parse_elseifnot() {
275                 if (sizeof($this->if_conditions) == 0) return;
276                 array_pop($this->if_conditions);
277                 if ($this->if_currentlevel) {
278                         ob_end_flush();
279                         $this->_updateIfExecute(1);
280                         $this->_addIfCondition(0);
281                 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
282                         ob_end_clean();
283                         $this->_addIfCondition(0);
284                 } else {
285                         ob_end_clean();
286                         $args = func_get_args();
287                         $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
288                         $this->_addIfCondition(!$condition);
289                 }
290         }
291
292         /**
293          * Ends a conditional if-block
294          * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
295          */
296         function parse_endif() {
297                 // we can only close what has been opened
298                 if (sizeof($this->if_conditions) == 0) return;
299
300                 if ($this->if_currentlevel) {
301                         ob_end_flush();
302                 } else {
303                         ob_end_clean();
304                 }
305                 array_pop($this->if_conditions);
306                 array_pop($this->if_execute);
307
308                 $this->_updateTopIfCondition();
309         }
310 }
311 ?>