C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\nucleus\nucleus\libs\PARSER.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
/*
  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) 
  * Copyright (C) 2002-2005 The Nucleus Group
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
  * (see nucleus/documentation/index.html#license for more info)
 */
/**
 * @license http://nucleuscms.org/license.txt GNU General Public License
 * @copyright Copyright (C) 2002-2005 The Nucleus Group
 * @version $Id: PARSER.php,v 1.14.2.1 2005/08/15 10:50:12 dekarma Exp $
  */
 
/**
 * This is the parser class of Nucleus. It is used for various things (skin parsing,
 * form generation, ...)
 */
class PARSER {

    
// array with the names of all allowed actions
    
var $actions;
    
    
// reference to actions handler
    
var $handler;
    
    
// delimiters that can be used for skin/templatevars
    
var $delim;
    
    
// parameter delimiter (to separate skinvar params)
    
var $pdelim;
    
    
// usually set to 0. When set to 1, all skinvars are allowed regardless of $actions
    
var $norestrictions;
    
    
/**
     * Creates a new parser object with the given allowed actions 
     * and the given handler
     *
     * @param $allowedActions array
     * @param $handler class object with functions for each action (reference)
     * @param $delim optional delimiter
     * @param $paramdelim optional parameterdelimiter     
     */
    
function PARSER($allowedActions, &$handler$delim '(<%|%>)'$pdelim ',') {
        
$this->actions $allowedActions;
        
$this->handler =& $handler;
        
$this->delim $delim;
        
$this->pdelim $pdelim;
        
$this->norestrictions 0;    // set this to 1 to disable checking for allowedActions
    
}
    
    
/**
     * Parses the given contents and outputs it
     */
    
function parse(&$contents) {
    
        
$pieces preg_split('/'.$this->delim.'/',$contents);
        
        
$maxidx sizeof($pieces);
        for (
$idx 0;$idx<$maxidx;$idx++) {
            echo 
$pieces[$idx];        
            
$idx++;
            
$this->doAction($pieces[$idx]);
        }
    }
    

    
/**
      * handle an action 
      */
    
function doAction($action) {
        global 
$manager;

        if (!
$action) return;
        
        
// split into action name + arguments
        
if (strstr($action,'(')) {
            
$paramStartPos strpos($action'(');
            
$params substr($action$paramStartPos 1strlen($action) - $paramStartPos 2);
            
$action substr($action0$paramStartPos);
            
$params explode ($this->pdelim$params);
            
            
// trim parameters 
            // for PHP versions lower than 4.0.6:
            //   - add // before '$params = ...' 
            //   - remove // before 'foreach'
            
$params array_map('trim',$params);
            
// foreach ($params as $key => $value) { $params[$key] = trim($value); }            
        
} else {
            
// no parameters
            
$params = array();
        }
    
        
$actionlc strtolower($action);
        
        
// skip execution of skinvars while inside an if condition which hides this part of the page
        
if (!$this->handler->if_currentlevel && ($actionlc != 'else') && ($actionlc != 'endif') && (substr($actionlc,0,2) != 'if'))
            return;
    
        if (
in_array($actionlc$this->actions) || $this->norestrictions ) {
            
// when using PHP versions lower than 4.0.5, uncomment the line before
            // and comment the call_user_func_array call
            //$this->call_using_array($action, $this->handler, $params);
            
call_user_func_array(array(&$this->handler,'parse_' $actionlc), $params);
        } else {
            
// redirect to plugin action if possible
            
if (in_array('plugin'$this->actions) && $manager->pluginInstalled('NP_'.$action))
                
$this->doAction('plugin('.$action.$this->pdelim.implode($this->pdelim,$params).')');
            else
                echo 
'<b>DISALLOWED (' $action ')</b>';
        }
        
    }
    
    
/**
      * Calls a method using an array of parameters (for use with PHP versions lower than 4.0.5)
      * ( = call_user_func_array() function )
      */
    
function call_using_array($methodname, &$handler$paramarray) {

        
$methodname 'parse_' $methodname;
        
        if (!
method_exists($handler$methodname)) {
            return;
        }

        
$command 'call_user_func(array(&$handler,$methodname)';
        for (
$i 0$i<count($paramarray); $i++)
            
$command .= ',$paramarray[' $i ']';
        
$command .= ');';
        eval(
$command);    // execute the correct method
    
}
    
    function 
setProperty($property$value) {
        global 
$manager;
        
$manager->setParserProperty($property$value);
    }
    
    function 
getProperty($name) {
        global 
$manager;
        return 
$manager->getParserProperty($name);
    }
    
    
}

/**
 * This class contains parse actions that are available in all ACTION classes
 * e.g. include, phpinclude, parsedinclude, skinfile, ...
 *
 * It should never be used on it's own
 */
class BaseActions {
    
    
// depth level for includes (max. level is 3)
    
var $level;
    
    
// array of evaluated conditions (true/false). The element at the end is the one for the most nested
    // if block.
    
var $if_conditions;
    
    
// at all times, can be evaluated to either true if the current block needs to be displayed. This 
    // variable is used to decide to skip skinvars in parts that will never be outputted.
    
var $if_currentlevel;
    
    
// contains a search string with keywords that need to be highlighted. These get parsed into $aHighlight
    
var $strHighlight;
    
    
// array of keywords that need to be highlighted in search results (see the highlight() 
    // and parseHighlight() methods)
    
var $aHighlight;
    

    
// reference to the parser object that is using this object as actions-handler

    
var $parser;
    

    function 
BaseActions() {
        
$this->level 0
        
        
// if nesting level
        
$this->if_conditions = array(); // array on which condition values are pushed/popped
        
$this->if_currentlevel 1;        // 1 = current level is displayed; 0 = current level not displayed

        // highlights        
        
$this->strHighlight '';            // full highlight
        
$this->aHighlight = array();        // parsed highlight
        
    
}

    
// include file (no parsing of php)
    
function parse_include($filename) {
        @
readfile($this->getIncludeFileName($filename));
    }
    
    
// php-include file 
    
function parse_phpinclude($filename) {
        
includephp($this->getIncludeFileName($filename));
    }    
    
// parsed include
    
function parse_parsedinclude($filename) {
        
// check current level
        
if ($this->level 3) return;    // max. depth reached (avoid endless loop)
        
$filename $this->getIncludeFileName($filename);
        if (!
file_exists($filename)) return '';
        
        
$fsize filesize($filename);
        
        
// nothing to include
        
if ($fsize <= 0)
            return;
        
        
$this->level $this->level 1;
        
        
// read file 
        
$fd fopen ($filename'r');
        
$contents fread ($fd$fsize);
        
fclose ($fd);        
        
        
// parse file contents
        
$this->parser->parse($contents);
        
        
$this->level $this->level 1;        
    }
    
    
/**
     * Returns the correct location of the file to be included, according to
     * parser properties
     *
     * IF IncludeMode = 'skindir' => use skindir
     */
    
function getIncludeFileName($filename) {
        
// leave absolute filenames and http urls as they are
        
if (
                (
substr($filename,0,1) == '/')
            ||    (
substr($filename,0,7) == 'http://')
            ||    (
substr($filename,0,6) == 'ftp://')            
            )
            return 
$filename;
    
        
$filename PARSER::getProperty('IncludePrefix') . $filename;
        if (
PARSER::getProperty('IncludeMode') == 'skindir') {
            global 
$DIR_SKINS;
            return 
$DIR_SKINS $filename;
        } else {
            return 
$filename;
        }
    }
    
    
/**
     * Inserts an url relative to the skindir (useful when doing import/export)
     *
     * e.g. <skinfile(default/myfile.sth)>
     */
    
function parse_skinfile($filename) {
        global 
$CONF;
        
        echo 
$CONF['SkinsURL'] . PARSER::getProperty('IncludePrefix') . $filename;
    }
    
    
/**
     * Sets a property for the parser
     */
    
function parse_set($property$value) {
        
PARSER::setProperty($property$value);
    }
    
    
/**
     * Helper function: add if condition
     */
    
function _addIfCondition($condition) {
    
        
array_push($this->if_conditions,$condition);
    
        
$this->_updateTopIfCondition();
            
        
ob_start();        
    }
    
    function 
_updateTopIfCondition() {
        if (
sizeof($this->if_conditions) == 0
            
$this->if_currentlevel 1;
        else
            
$this->if_currentlevel $this->if_conditions[sizeof($this->if_conditions) - 1];
    }
    
    
/**
     * returns the currently top if condition
     */
    
function _getTopIfCondition() {
        return 
$this->if_currentlevel;
    }
    
    
/**
     * else
     */
    
function parse_else() {
        if (
sizeof($this->if_conditions) == 0) return;
        
$old $this->if_currentlevel;
        if (
array_pop($this->if_conditions)) {
            
ob_end_flush();
            
$this->_addIfCondition(0);
        } else {
            
ob_end_clean();
            
$this->_addIfCondition(1);
        }
    }
    
    
/**
     * Ends a conditional if-block 
     * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
     */
    
function parse_endif() {
        
// we can only close what has been opened
        
if (sizeof($this->if_conditions) == 0) return;
        
        if (
array_pop($this->if_conditions)) {
            
ob_end_flush();
        } else {
            
ob_end_clean();
        }
        
        
$this->_updateTopIfCondition();
    }
    
    
    
/** 
     * Sets the search terms to be highlighted
     *
     * @param $highlight
     *        A series of search terms
     */
    
function setHighlight($highlight) {    
        
$this->strHighlight $highlight;
        if (
$highlight) {
            
$this->aHighlight parseHighlight($highlight); 
        }
    }
    
    
/**
     * Applies the highlight to the given piece of text
     *
     * @param &$data
     *        Data that needs to be highlighted
     * @see setHighlight
     */
    
function highlight(&$data) {
        if (
$this->aHighlight)
            return 
highlight($data,$this->aHighlight,$this->template['SEARCH_HIGHLIGHT']);
        else
            return 
$data;
    }
    
    

}
 
?>