OSDN Git Service

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