OSDN Git Service

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