@@ -24,288 +24,288 @@ discard block |
||
24 | 24 | */ |
25 | 25 | class XTemplate { |
26 | 26 | |
27 | - /** |
|
28 | - * Properties |
|
29 | - */ |
|
30 | - |
|
31 | - /** |
|
32 | - * Raw contents of the template file |
|
33 | - * |
|
34 | - * @access public |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - public $filecontents = ''; |
|
38 | - |
|
39 | - /** |
|
40 | - * Unparsed blocks |
|
41 | - * |
|
42 | - * @access public |
|
43 | - * @var array |
|
44 | - */ |
|
45 | - public $blocks = array(); |
|
46 | - |
|
47 | - /** |
|
48 | - * Parsed blocks |
|
49 | - * |
|
50 | - * @var unknown_type |
|
51 | - */ |
|
52 | - public $parsed_blocks = array(); |
|
53 | - |
|
54 | - /** |
|
55 | - * Preparsed blocks (for file includes) |
|
56 | - * |
|
57 | - * @access public |
|
58 | - * @var array |
|
59 | - */ |
|
60 | - public $preparsed_blocks = array(); |
|
61 | - |
|
62 | - /** |
|
63 | - * Block parsing order for recursive parsing |
|
64 | - * (Sometimes reverse :) |
|
65 | - * |
|
66 | - * @access public |
|
67 | - * @var array |
|
68 | - */ |
|
69 | - public $block_parse_order = array(); |
|
70 | - |
|
71 | - /** |
|
72 | - * Store sub-block names |
|
73 | - * (For fast resetting) |
|
74 | - * |
|
75 | - * @access public |
|
76 | - * @var array |
|
77 | - */ |
|
78 | - public $sub_blocks = array(); |
|
79 | - |
|
80 | - /** |
|
81 | - * Variables array |
|
82 | - * |
|
83 | - * @access public |
|
84 | - * @var array |
|
85 | - */ |
|
86 | - public $vars = array(); |
|
87 | - |
|
88 | - /** |
|
89 | - * File variables array |
|
90 | - * |
|
91 | - * @access public |
|
92 | - * @var array |
|
93 | - */ |
|
94 | - public $filevars = array(); |
|
95 | - |
|
96 | - /** |
|
97 | - * Filevars' parent block |
|
98 | - * |
|
99 | - * @access public |
|
100 | - * @var array |
|
101 | - */ |
|
102 | - public $filevar_parent = array(); |
|
103 | - |
|
104 | - /** |
|
105 | - * File caching during duration of script |
|
106 | - * e.g. files only cached to speed {FILE "filename"} repeats |
|
107 | - * |
|
108 | - * @access public |
|
109 | - * @var array |
|
110 | - */ |
|
111 | - public $filecache = array(); |
|
112 | - |
|
113 | - /** |
|
114 | - * Location of template files |
|
115 | - * |
|
116 | - * @access public |
|
117 | - * @var string |
|
118 | - */ |
|
119 | - public $tpldir = ''; |
|
120 | - |
|
121 | - /** |
|
122 | - * Filenames lookup table |
|
123 | - * |
|
124 | - * @access public |
|
125 | - * @var null |
|
126 | - */ |
|
127 | - public $files = null; |
|
128 | - |
|
129 | - /** |
|
130 | - * Template filename |
|
131 | - * |
|
132 | - * @access public |
|
133 | - * @var string |
|
134 | - */ |
|
135 | - public $filename = ''; |
|
136 | - |
|
137 | - // moved to setup method so uses the tag_start & end_delims |
|
138 | - /** |
|
139 | - * RegEx for file includes |
|
140 | - * |
|
141 | - * "/\{FILE\s*\"([^\"]+)\"\s*\}/m"; |
|
142 | - * |
|
143 | - * @access public |
|
144 | - * @var string |
|
145 | - */ |
|
146 | - public $file_delim = ''; |
|
147 | - |
|
148 | - /** |
|
149 | - * RegEx for file include variable |
|
150 | - * |
|
151 | - * "/\{FILE\s*\{([A-Za-z0-9\._]+?)\}\s*\}/m"; |
|
152 | - * |
|
153 | - * @access public |
|
154 | - * @var string |
|
155 | - */ |
|
156 | - public $filevar_delim = ''; |
|
157 | - |
|
158 | - /** |
|
159 | - * RegEx for file includes with newlines |
|
160 | - * |
|
161 | - * "/^\s*\{FILE\s*\{([A-Za-z0-9\._]+?)\}\s*\}\s*\n/m"; |
|
162 | - * |
|
163 | - * @access public |
|
164 | - * @var string |
|
165 | - */ |
|
166 | - public $filevar_delim_nl = ''; |
|
167 | - |
|
168 | - /** |
|
169 | - * Template block start delimiter |
|
170 | - * |
|
171 | - * @access public |
|
172 | - * @var string |
|
173 | - */ |
|
174 | - public $block_start_delim = '<!-- '; |
|
175 | - |
|
176 | - /** |
|
177 | - * Template block end delimiter |
|
178 | - * |
|
179 | - * @access public |
|
180 | - * @var string |
|
181 | - */ |
|
182 | - public $block_end_delim = '-->'; |
|
183 | - |
|
184 | - /** |
|
185 | - * Template block start word |
|
186 | - * |
|
187 | - * @access public |
|
188 | - * @var string |
|
189 | - */ |
|
190 | - public $block_start_word = 'BEGIN:'; |
|
191 | - |
|
192 | - /** |
|
193 | - * Template block end word |
|
194 | - * |
|
195 | - * The last 3 properties and this make the delimiters look like: |
|
196 | - * @example <!-- BEGIN: block_name --> |
|
197 | - * if you use the default syntax. |
|
198 | - * |
|
199 | - * @access public |
|
200 | - * @var string |
|
201 | - */ |
|
202 | - public $block_end_word = 'END:'; |
|
203 | - |
|
204 | - /** |
|
205 | - * Template tag start delimiter |
|
206 | - * |
|
207 | - * This makes the delimiters look like: |
|
208 | - * @example {tagname} |
|
209 | - * if you use the default syntax. |
|
210 | - * |
|
211 | - * @access public |
|
212 | - * @var string |
|
213 | - */ |
|
214 | - public $tag_start_delim = '{'; |
|
215 | - |
|
216 | - /** |
|
217 | - * Template tag end delimiter |
|
218 | - * |
|
219 | - * This makes the delimiters look like: |
|
220 | - * @example {tagname} |
|
221 | - * if you use the default syntax. |
|
222 | - * |
|
223 | - * @access public |
|
224 | - * @var string |
|
225 | - */ |
|
226 | - public $tag_end_delim = '}'; |
|
227 | - /* this makes the delimiters look like: {tagname} if you use my syntax. */ |
|
228 | - |
|
229 | - /** |
|
230 | - * Regular expression element for comments within tags and blocks |
|
231 | - * |
|
232 | - * @example {tagname#My Comment} |
|
233 | - * @example {tagname #My Comment} |
|
234 | - * @example <!-- BEGIN: blockname#My Comment --> |
|
235 | - * @example <!-- BEGIN: blockname #My Comment --> |
|
236 | - * |
|
237 | - * @access public |
|
238 | - * @var string |
|
239 | - */ |
|
240 | - public $comment_preg = '( ?#.*?)?'; |
|
241 | - |
|
242 | - /** |
|
243 | - * Default main template block name |
|
244 | - * |
|
245 | - * @access public |
|
246 | - * @var string |
|
247 | - */ |
|
248 | - public $mainblock = 'main'; |
|
249 | - |
|
250 | - /** |
|
251 | - * Script output type |
|
252 | - * |
|
253 | - * @access public |
|
254 | - * @var string |
|
255 | - */ |
|
256 | - public $output_type = 'HTML'; |
|
257 | - |
|
258 | - /** |
|
259 | - * Debug mode |
|
260 | - * |
|
261 | - * @access public |
|
262 | - * @var boolean |
|
263 | - */ |
|
264 | - public $debug = false; |
|
265 | - |
|
266 | - /** |
|
267 | - * Null string for unassigned vars |
|
268 | - * |
|
269 | - * @access protected |
|
270 | - * @var array |
|
271 | - */ |
|
272 | - protected $_null_string = array('' => ''); |
|
273 | - |
|
274 | - /** |
|
275 | - * Null string for unassigned blocks |
|
276 | - * |
|
277 | - * @access protected |
|
278 | - * @var array |
|
279 | - */ |
|
280 | - protected $_null_block = array('' => ''); |
|
281 | - |
|
282 | - /** |
|
283 | - * Errors |
|
284 | - * |
|
285 | - * @access protected |
|
286 | - * @var string |
|
287 | - */ |
|
288 | - protected $_error = ''; |
|
289 | - |
|
290 | - /** |
|
291 | - * Auto-reset sub blocks |
|
292 | - * |
|
293 | - * @access protected |
|
294 | - * @var boolean |
|
295 | - */ |
|
296 | - protected $_autoreset = true; |
|
297 | - |
|
298 | - /** |
|
299 | - * Set to FALSE to generate errors if a non-existant blocks is referenced |
|
300 | - * |
|
301 | - * @author NW |
|
302 | - * @since 2002/10/17 |
|
303 | - * @access protected |
|
304 | - * @var boolean |
|
305 | - */ |
|
306 | - protected $_ignore_missing_blocks = true; |
|
307 | - |
|
308 | - /** |
|
27 | + /** |
|
28 | + * Properties |
|
29 | + */ |
|
30 | + |
|
31 | + /** |
|
32 | + * Raw contents of the template file |
|
33 | + * |
|
34 | + * @access public |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + public $filecontents = ''; |
|
38 | + |
|
39 | + /** |
|
40 | + * Unparsed blocks |
|
41 | + * |
|
42 | + * @access public |
|
43 | + * @var array |
|
44 | + */ |
|
45 | + public $blocks = array(); |
|
46 | + |
|
47 | + /** |
|
48 | + * Parsed blocks |
|
49 | + * |
|
50 | + * @var unknown_type |
|
51 | + */ |
|
52 | + public $parsed_blocks = array(); |
|
53 | + |
|
54 | + /** |
|
55 | + * Preparsed blocks (for file includes) |
|
56 | + * |
|
57 | + * @access public |
|
58 | + * @var array |
|
59 | + */ |
|
60 | + public $preparsed_blocks = array(); |
|
61 | + |
|
62 | + /** |
|
63 | + * Block parsing order for recursive parsing |
|
64 | + * (Sometimes reverse :) |
|
65 | + * |
|
66 | + * @access public |
|
67 | + * @var array |
|
68 | + */ |
|
69 | + public $block_parse_order = array(); |
|
70 | + |
|
71 | + /** |
|
72 | + * Store sub-block names |
|
73 | + * (For fast resetting) |
|
74 | + * |
|
75 | + * @access public |
|
76 | + * @var array |
|
77 | + */ |
|
78 | + public $sub_blocks = array(); |
|
79 | + |
|
80 | + /** |
|
81 | + * Variables array |
|
82 | + * |
|
83 | + * @access public |
|
84 | + * @var array |
|
85 | + */ |
|
86 | + public $vars = array(); |
|
87 | + |
|
88 | + /** |
|
89 | + * File variables array |
|
90 | + * |
|
91 | + * @access public |
|
92 | + * @var array |
|
93 | + */ |
|
94 | + public $filevars = array(); |
|
95 | + |
|
96 | + /** |
|
97 | + * Filevars' parent block |
|
98 | + * |
|
99 | + * @access public |
|
100 | + * @var array |
|
101 | + */ |
|
102 | + public $filevar_parent = array(); |
|
103 | + |
|
104 | + /** |
|
105 | + * File caching during duration of script |
|
106 | + * e.g. files only cached to speed {FILE "filename"} repeats |
|
107 | + * |
|
108 | + * @access public |
|
109 | + * @var array |
|
110 | + */ |
|
111 | + public $filecache = array(); |
|
112 | + |
|
113 | + /** |
|
114 | + * Location of template files |
|
115 | + * |
|
116 | + * @access public |
|
117 | + * @var string |
|
118 | + */ |
|
119 | + public $tpldir = ''; |
|
120 | + |
|
121 | + /** |
|
122 | + * Filenames lookup table |
|
123 | + * |
|
124 | + * @access public |
|
125 | + * @var null |
|
126 | + */ |
|
127 | + public $files = null; |
|
128 | + |
|
129 | + /** |
|
130 | + * Template filename |
|
131 | + * |
|
132 | + * @access public |
|
133 | + * @var string |
|
134 | + */ |
|
135 | + public $filename = ''; |
|
136 | + |
|
137 | + // moved to setup method so uses the tag_start & end_delims |
|
138 | + /** |
|
139 | + * RegEx for file includes |
|
140 | + * |
|
141 | + * "/\{FILE\s*\"([^\"]+)\"\s*\}/m"; |
|
142 | + * |
|
143 | + * @access public |
|
144 | + * @var string |
|
145 | + */ |
|
146 | + public $file_delim = ''; |
|
147 | + |
|
148 | + /** |
|
149 | + * RegEx for file include variable |
|
150 | + * |
|
151 | + * "/\{FILE\s*\{([A-Za-z0-9\._]+?)\}\s*\}/m"; |
|
152 | + * |
|
153 | + * @access public |
|
154 | + * @var string |
|
155 | + */ |
|
156 | + public $filevar_delim = ''; |
|
157 | + |
|
158 | + /** |
|
159 | + * RegEx for file includes with newlines |
|
160 | + * |
|
161 | + * "/^\s*\{FILE\s*\{([A-Za-z0-9\._]+?)\}\s*\}\s*\n/m"; |
|
162 | + * |
|
163 | + * @access public |
|
164 | + * @var string |
|
165 | + */ |
|
166 | + public $filevar_delim_nl = ''; |
|
167 | + |
|
168 | + /** |
|
169 | + * Template block start delimiter |
|
170 | + * |
|
171 | + * @access public |
|
172 | + * @var string |
|
173 | + */ |
|
174 | + public $block_start_delim = '<!-- '; |
|
175 | + |
|
176 | + /** |
|
177 | + * Template block end delimiter |
|
178 | + * |
|
179 | + * @access public |
|
180 | + * @var string |
|
181 | + */ |
|
182 | + public $block_end_delim = '-->'; |
|
183 | + |
|
184 | + /** |
|
185 | + * Template block start word |
|
186 | + * |
|
187 | + * @access public |
|
188 | + * @var string |
|
189 | + */ |
|
190 | + public $block_start_word = 'BEGIN:'; |
|
191 | + |
|
192 | + /** |
|
193 | + * Template block end word |
|
194 | + * |
|
195 | + * The last 3 properties and this make the delimiters look like: |
|
196 | + * @example <!-- BEGIN: block_name --> |
|
197 | + * if you use the default syntax. |
|
198 | + * |
|
199 | + * @access public |
|
200 | + * @var string |
|
201 | + */ |
|
202 | + public $block_end_word = 'END:'; |
|
203 | + |
|
204 | + /** |
|
205 | + * Template tag start delimiter |
|
206 | + * |
|
207 | + * This makes the delimiters look like: |
|
208 | + * @example {tagname} |
|
209 | + * if you use the default syntax. |
|
210 | + * |
|
211 | + * @access public |
|
212 | + * @var string |
|
213 | + */ |
|
214 | + public $tag_start_delim = '{'; |
|
215 | + |
|
216 | + /** |
|
217 | + * Template tag end delimiter |
|
218 | + * |
|
219 | + * This makes the delimiters look like: |
|
220 | + * @example {tagname} |
|
221 | + * if you use the default syntax. |
|
222 | + * |
|
223 | + * @access public |
|
224 | + * @var string |
|
225 | + */ |
|
226 | + public $tag_end_delim = '}'; |
|
227 | + /* this makes the delimiters look like: {tagname} if you use my syntax. */ |
|
228 | + |
|
229 | + /** |
|
230 | + * Regular expression element for comments within tags and blocks |
|
231 | + * |
|
232 | + * @example {tagname#My Comment} |
|
233 | + * @example {tagname #My Comment} |
|
234 | + * @example <!-- BEGIN: blockname#My Comment --> |
|
235 | + * @example <!-- BEGIN: blockname #My Comment --> |
|
236 | + * |
|
237 | + * @access public |
|
238 | + * @var string |
|
239 | + */ |
|
240 | + public $comment_preg = '( ?#.*?)?'; |
|
241 | + |
|
242 | + /** |
|
243 | + * Default main template block name |
|
244 | + * |
|
245 | + * @access public |
|
246 | + * @var string |
|
247 | + */ |
|
248 | + public $mainblock = 'main'; |
|
249 | + |
|
250 | + /** |
|
251 | + * Script output type |
|
252 | + * |
|
253 | + * @access public |
|
254 | + * @var string |
|
255 | + */ |
|
256 | + public $output_type = 'HTML'; |
|
257 | + |
|
258 | + /** |
|
259 | + * Debug mode |
|
260 | + * |
|
261 | + * @access public |
|
262 | + * @var boolean |
|
263 | + */ |
|
264 | + public $debug = false; |
|
265 | + |
|
266 | + /** |
|
267 | + * Null string for unassigned vars |
|
268 | + * |
|
269 | + * @access protected |
|
270 | + * @var array |
|
271 | + */ |
|
272 | + protected $_null_string = array('' => ''); |
|
273 | + |
|
274 | + /** |
|
275 | + * Null string for unassigned blocks |
|
276 | + * |
|
277 | + * @access protected |
|
278 | + * @var array |
|
279 | + */ |
|
280 | + protected $_null_block = array('' => ''); |
|
281 | + |
|
282 | + /** |
|
283 | + * Errors |
|
284 | + * |
|
285 | + * @access protected |
|
286 | + * @var string |
|
287 | + */ |
|
288 | + protected $_error = ''; |
|
289 | + |
|
290 | + /** |
|
291 | + * Auto-reset sub blocks |
|
292 | + * |
|
293 | + * @access protected |
|
294 | + * @var boolean |
|
295 | + */ |
|
296 | + protected $_autoreset = true; |
|
297 | + |
|
298 | + /** |
|
299 | + * Set to FALSE to generate errors if a non-existant blocks is referenced |
|
300 | + * |
|
301 | + * @author NW |
|
302 | + * @since 2002/10/17 |
|
303 | + * @access protected |
|
304 | + * @var boolean |
|
305 | + */ |
|
306 | + protected $_ignore_missing_blocks = true; |
|
307 | + |
|
308 | + /** |
|
309 | 309 | * PHP 5 Constructor - Instantiate the object |
310 | 310 | * |
311 | 311 | * @param string $file Template file to work on |
@@ -315,12 +315,12 @@ discard block |
||
315 | 315 | * @param boolean $autosetup If true, run setup() as part of constuctor |
316 | 316 | * @return XTemplate |
317 | 317 | */ |
318 | - public function __construct($file, $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true) { |
|
318 | + public function __construct($file, $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true) { |
|
319 | 319 | |
320 | - $this->restart($file, $tpldir, $files, $mainblock, $autosetup, $this->tag_start_delim, $this->tag_end_delim); |
|
321 | - } |
|
320 | + $this->restart($file, $tpldir, $files, $mainblock, $autosetup, $this->tag_start_delim, $this->tag_end_delim); |
|
321 | + } |
|
322 | 322 | |
323 | - /** |
|
323 | + /** |
|
324 | 324 | * PHP 4 Constructor - Instantiate the object |
325 | 325 | * |
326 | 326 | * @deprecated Use PHP 5 constructor instead |
@@ -331,111 +331,111 @@ discard block |
||
331 | 331 | * @param boolean $autosetup If true, run setup() as part of constuctor |
332 | 332 | * @return XTemplate |
333 | 333 | */ |
334 | - public function XTemplate ($file, $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true) { |
|
335 | - |
|
336 | - assert('Deprecated - use PHP 5 constructor'); |
|
337 | - } |
|
338 | - |
|
339 | - |
|
340 | - /***************************************************************************/ |
|
341 | - /***[ public stuff ]********************************************************/ |
|
342 | - /***************************************************************************/ |
|
343 | - |
|
344 | - /** |
|
345 | - * Restart the class - allows one instantiation with several files processed by restarting |
|
346 | - * e.g. $xtpl = new XTemplate('file1.xtpl'); |
|
347 | - * $xtpl->parse('main'); |
|
348 | - * $xtpl->out('main'); |
|
349 | - * $xtpl->restart('file2.xtpl'); |
|
350 | - * $xtpl->parse('main'); |
|
351 | - * $xtpl->out('main'); |
|
352 | - * (Added in response to sf:641407 feature request) |
|
353 | - * |
|
354 | - * @param string $file Template file to work on |
|
355 | - * @param string/array $tpldir Location of template files |
|
356 | - * @param array $files Filenames lookup |
|
357 | - * @param string $mainblock Name of main block in the template |
|
358 | - * @param boolean $autosetup If true, run setup() as part of restarting |
|
359 | - * @param string $tag_start { |
|
360 | - * @param string $tag_end } |
|
361 | - */ |
|
362 | - public function restart ($file, $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true, $tag_start = '{', $tag_end = '}') { |
|
363 | - |
|
364 | - $this->filename = $file; |
|
365 | - |
|
366 | - // From SF Feature request 1202027 |
|
367 | - // Kenneth Kalmer |
|
368 | - $this->tpldir = $tpldir; |
|
369 | - if (defined('XTPL_DIR') && empty($this->tpldir)) { |
|
370 | - $this->tpldir = XTPL_DIR; |
|
371 | - } |
|
372 | - |
|
373 | - if (is_array($files)) { |
|
374 | - $this->files = $files; |
|
375 | - } |
|
376 | - |
|
377 | - $this->mainblock = $mainblock; |
|
378 | - |
|
379 | - $this->tag_start_delim = $tag_start; |
|
380 | - $this->tag_end_delim = $tag_end; |
|
381 | - |
|
382 | - // Start with fresh file contents |
|
383 | - $this->filecontents = ''; |
|
384 | - |
|
385 | - // Reset the template arrays |
|
386 | - $this->blocks = array(); |
|
387 | - $this->parsed_blocks = array(); |
|
388 | - $this->preparsed_blocks = array(); |
|
389 | - $this->block_parse_order = array(); |
|
390 | - $this->sub_blocks = array(); |
|
391 | - $this->vars = array(); |
|
392 | - $this->filevars = array(); |
|
393 | - $this->filevar_parent = array(); |
|
394 | - $this->filecache = array(); |
|
395 | - |
|
396 | - if ($autosetup) { |
|
397 | - $this->setup(); |
|
398 | - } |
|
399 | - } |
|
400 | - |
|
401 | - /** |
|
334 | + public function XTemplate ($file, $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true) { |
|
335 | + |
|
336 | + assert('Deprecated - use PHP 5 constructor'); |
|
337 | + } |
|
338 | + |
|
339 | + |
|
340 | + /***************************************************************************/ |
|
341 | + /***[ public stuff ]********************************************************/ |
|
342 | + /***************************************************************************/ |
|
343 | + |
|
344 | + /** |
|
345 | + * Restart the class - allows one instantiation with several files processed by restarting |
|
346 | + * e.g. $xtpl = new XTemplate('file1.xtpl'); |
|
347 | + * $xtpl->parse('main'); |
|
348 | + * $xtpl->out('main'); |
|
349 | + * $xtpl->restart('file2.xtpl'); |
|
350 | + * $xtpl->parse('main'); |
|
351 | + * $xtpl->out('main'); |
|
352 | + * (Added in response to sf:641407 feature request) |
|
353 | + * |
|
354 | + * @param string $file Template file to work on |
|
355 | + * @param string/array $tpldir Location of template files |
|
356 | + * @param array $files Filenames lookup |
|
357 | + * @param string $mainblock Name of main block in the template |
|
358 | + * @param boolean $autosetup If true, run setup() as part of restarting |
|
359 | + * @param string $tag_start { |
|
360 | + * @param string $tag_end } |
|
361 | + */ |
|
362 | + public function restart ($file, $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true, $tag_start = '{', $tag_end = '}') { |
|
363 | + |
|
364 | + $this->filename = $file; |
|
365 | + |
|
366 | + // From SF Feature request 1202027 |
|
367 | + // Kenneth Kalmer |
|
368 | + $this->tpldir = $tpldir; |
|
369 | + if (defined('XTPL_DIR') && empty($this->tpldir)) { |
|
370 | + $this->tpldir = XTPL_DIR; |
|
371 | + } |
|
372 | + |
|
373 | + if (is_array($files)) { |
|
374 | + $this->files = $files; |
|
375 | + } |
|
376 | + |
|
377 | + $this->mainblock = $mainblock; |
|
378 | + |
|
379 | + $this->tag_start_delim = $tag_start; |
|
380 | + $this->tag_end_delim = $tag_end; |
|
381 | + |
|
382 | + // Start with fresh file contents |
|
383 | + $this->filecontents = ''; |
|
384 | + |
|
385 | + // Reset the template arrays |
|
386 | + $this->blocks = array(); |
|
387 | + $this->parsed_blocks = array(); |
|
388 | + $this->preparsed_blocks = array(); |
|
389 | + $this->block_parse_order = array(); |
|
390 | + $this->sub_blocks = array(); |
|
391 | + $this->vars = array(); |
|
392 | + $this->filevars = array(); |
|
393 | + $this->filevar_parent = array(); |
|
394 | + $this->filecache = array(); |
|
395 | + |
|
396 | + if ($autosetup) { |
|
397 | + $this->setup(); |
|
398 | + } |
|
399 | + } |
|
400 | + |
|
401 | + /** |
|
402 | 402 | * setup - the elements that were previously in the constructor |
403 | 403 | * |
404 | 404 | * @access public |
405 | 405 | * @param boolean $add_outer If true is passed when called, it adds an outer main block to the file |
406 | 406 | */ |
407 | - public function setup ($add_outer = false) { |
|
407 | + public function setup ($add_outer = false) { |
|
408 | 408 | |
409 | - $this->tag_start_delim = preg_quote($this->tag_start_delim); |
|
410 | - $this->tag_end_delim = preg_quote($this->tag_end_delim); |
|
409 | + $this->tag_start_delim = preg_quote($this->tag_start_delim); |
|
410 | + $this->tag_end_delim = preg_quote($this->tag_end_delim); |
|
411 | 411 | |
412 | - // Setup the file delimiters |
|
412 | + // Setup the file delimiters |
|
413 | 413 | |
414 | - // regexp for file includes |
|
415 | - $this->file_delim = "/" . $this->tag_start_delim . "FILE\s*\"([^\"]+)\"" . $this->comment_preg . $this->tag_end_delim . "/m"; |
|
414 | + // regexp for file includes |
|
415 | + $this->file_delim = "/" . $this->tag_start_delim . "FILE\s*\"([^\"]+)\"" . $this->comment_preg . $this->tag_end_delim . "/m"; |
|
416 | 416 | |
417 | - // regexp for file includes |
|
418 | - $this->filevar_delim = "/" . $this->tag_start_delim . "FILE\s*" . $this->tag_start_delim . "([A-Za-z0-9\._]+?)" . $this->comment_preg . $this->tag_end_delim . $this->comment_preg . $this->tag_end_delim . "/m"; |
|
417 | + // regexp for file includes |
|
418 | + $this->filevar_delim = "/" . $this->tag_start_delim . "FILE\s*" . $this->tag_start_delim . "([A-Za-z0-9\._]+?)" . $this->comment_preg . $this->tag_end_delim . $this->comment_preg . $this->tag_end_delim . "/m"; |
|
419 | 419 | |
420 | - // regexp for file includes w/ newlines |
|
421 | - $this->filevar_delim_nl = "/^\s*" . $this->tag_start_delim . "FILE\s*" . $this->tag_start_delim . "([A-Za-z0-9\._]+?)" . $this->comment_preg . $this->tag_end_delim . $this->comment_preg . $this->tag_end_delim . "\s*\n/m"; |
|
420 | + // regexp for file includes w/ newlines |
|
421 | + $this->filevar_delim_nl = "/^\s*" . $this->tag_start_delim . "FILE\s*" . $this->tag_start_delim . "([A-Za-z0-9\._]+?)" . $this->comment_preg . $this->tag_end_delim . $this->comment_preg . $this->tag_end_delim . "\s*\n/m"; |
|
422 | 422 | |
423 | - if (empty($this->filecontents)) { |
|
424 | - // read in template file |
|
425 | - $this->filecontents = $this->_r_getfile($this->filename); |
|
426 | - } |
|
423 | + if (empty($this->filecontents)) { |
|
424 | + // read in template file |
|
425 | + $this->filecontents = $this->_r_getfile($this->filename); |
|
426 | + } |
|
427 | 427 | |
428 | - if ($add_outer) { |
|
429 | - $this->_add_outer_block(); |
|
430 | - } |
|
428 | + if ($add_outer) { |
|
429 | + $this->_add_outer_block(); |
|
430 | + } |
|
431 | 431 | |
432 | - // preprocess some stuff |
|
433 | - $this->blocks = $this->_maketree($this->filecontents, ''); |
|
434 | - $this->filevar_parent = $this->_store_filevar_parents($this->blocks); |
|
435 | - $this->scan_globals(); |
|
436 | - } |
|
432 | + // preprocess some stuff |
|
433 | + $this->blocks = $this->_maketree($this->filecontents, ''); |
|
434 | + $this->filevar_parent = $this->_store_filevar_parents($this->blocks); |
|
435 | + $this->scan_globals(); |
|
436 | + } |
|
437 | 437 | |
438 | - /** |
|
438 | + /** |
|
439 | 439 | * assign a variable |
440 | 440 | * |
441 | 441 | * @example Simplest case: |
@@ -459,500 +459,500 @@ discard block |
||
459 | 459 | * @access public |
460 | 460 | * @param string $name Variable to assign $val to |
461 | 461 | * @param string / array $val Value to assign to $name |
462 | - * @param boolean $reset_array Reset the variable array if $val is an array |
|
462 | + * @param boolean $reset_array Reset the variable array if $val is an array |
|
463 | 463 | */ |
464 | - public function assign ($name, $val = '', $reset_array = true) { |
|
464 | + public function assign ($name, $val = '', $reset_array = true) { |
|
465 | 465 | |
466 | - if (is_array($name)) { |
|
466 | + if (is_array($name)) { |
|
467 | 467 | |
468 | - foreach ($name as $k => $v) { |
|
468 | + foreach ($name as $k => $v) { |
|
469 | 469 | |
470 | - $this->vars[$k] = $v; |
|
471 | - } |
|
472 | - } elseif (is_array($val)) { |
|
470 | + $this->vars[$k] = $v; |
|
471 | + } |
|
472 | + } elseif (is_array($val)) { |
|
473 | 473 | |
474 | - // Clear the existing values |
|
475 | - if ($reset_array) { |
|
476 | - $this->vars[$name] = array(); |
|
477 | - } |
|
474 | + // Clear the existing values |
|
475 | + if ($reset_array) { |
|
476 | + $this->vars[$name] = array(); |
|
477 | + } |
|
478 | 478 | |
479 | - foreach ($val as $k => $v) { |
|
479 | + foreach ($val as $k => $v) { |
|
480 | 480 | |
481 | - $this->vars[$name][$k] = $v; |
|
482 | - } |
|
481 | + $this->vars[$name][$k] = $v; |
|
482 | + } |
|
483 | 483 | |
484 | - } else { |
|
484 | + } else { |
|
485 | 485 | |
486 | - $this->vars[$name] = $val; |
|
487 | - } |
|
488 | - } |
|
486 | + $this->vars[$name] = $val; |
|
487 | + } |
|
488 | + } |
|
489 | 489 | |
490 | - /** |
|
490 | + /** |
|
491 | 491 | * assign a file variable |
492 | 492 | * |
493 | 493 | * @access public |
494 | 494 | * @param string $name Variable to assign $val to |
495 | 495 | * @param string / array $val Values to assign to $name |
496 | 496 | */ |
497 | - public function assign_file ($name, $val = '') { |
|
497 | + public function assign_file ($name, $val = '') { |
|
498 | 498 | |
499 | - if (is_array($name)) { |
|
499 | + if (is_array($name)) { |
|
500 | 500 | |
501 | - foreach ($name as $k => $v) { |
|
501 | + foreach ($name as $k => $v) { |
|
502 | 502 | |
503 | - $this->_assign_file_sub($k, $v); |
|
504 | - } |
|
505 | - } else { |
|
503 | + $this->_assign_file_sub($k, $v); |
|
504 | + } |
|
505 | + } else { |
|
506 | 506 | |
507 | - $this->_assign_file_sub($name, $val); |
|
508 | - } |
|
509 | - } |
|
507 | + $this->_assign_file_sub($name, $val); |
|
508 | + } |
|
509 | + } |
|
510 | 510 | |
511 | - /** |
|
511 | + /** |
|
512 | 512 | * parse a block |
513 | 513 | * |
514 | 514 | * @access public |
515 | 515 | * @param string $bname Block name to parse |
516 | 516 | */ |
517 | - public function parse ($bname) { |
|
517 | + public function parse ($bname) { |
|
518 | 518 | |
519 | - if (isset($this->preparsed_blocks[$bname])) { |
|
519 | + if (isset($this->preparsed_blocks[$bname])) { |
|
520 | 520 | |
521 | - $copy = $this->preparsed_blocks[$bname]; |
|
521 | + $copy = $this->preparsed_blocks[$bname]; |
|
522 | 522 | |
523 | - } elseif (isset($this->blocks[$bname])) { |
|
523 | + } elseif (isset($this->blocks[$bname])) { |
|
524 | 524 | |
525 | - $copy = $this->blocks[$bname]; |
|
525 | + $copy = $this->blocks[$bname]; |
|
526 | 526 | |
527 | - } elseif ($this->_ignore_missing_blocks) { |
|
528 | - // ------------------------------------------------------ |
|
529 | - // NW : 17 Oct 2002. Added default of ignore_missing_blocks |
|
530 | - // to allow for generalised processing where some |
|
531 | - // blocks may be removed from the HTML without the |
|
532 | - // processing code needing to be altered. |
|
533 | - // ------------------------------------------------------ |
|
534 | - // JRC: 3/1/2003 added set error to ignore missing functionality |
|
535 | - $this->_set_error("parse: blockname [$bname] does not exist"); |
|
536 | - return; |
|
527 | + } elseif ($this->_ignore_missing_blocks) { |
|
528 | + // ------------------------------------------------------ |
|
529 | + // NW : 17 Oct 2002. Added default of ignore_missing_blocks |
|
530 | + // to allow for generalised processing where some |
|
531 | + // blocks may be removed from the HTML without the |
|
532 | + // processing code needing to be altered. |
|
533 | + // ------------------------------------------------------ |
|
534 | + // JRC: 3/1/2003 added set error to ignore missing functionality |
|
535 | + $this->_set_error("parse: blockname [$bname] does not exist"); |
|
536 | + return; |
|
537 | 537 | |
538 | - } else { |
|
538 | + } else { |
|
539 | 539 | |
540 | - $this->_set_error("parse: blockname [$bname] does not exist"); |
|
541 | - } |
|
540 | + $this->_set_error("parse: blockname [$bname] does not exist"); |
|
541 | + } |
|
542 | 542 | |
543 | - /* from there we should have no more {FILE } directives */ |
|
544 | - if (!isset($copy)) { |
|
545 | - die('Block: ' . $bname); |
|
546 | - } |
|
543 | + /* from there we should have no more {FILE } directives */ |
|
544 | + if (!isset($copy)) { |
|
545 | + die('Block: ' . $bname); |
|
546 | + } |
|
547 | 547 | |
548 | - $copy = preg_replace($this->filevar_delim_nl, '', $copy); |
|
548 | + $copy = preg_replace($this->filevar_delim_nl, '', $copy); |
|
549 | 549 | |
550 | - $var_array = array(); |
|
550 | + $var_array = array(); |
|
551 | 551 | |
552 | - /* find & replace variables+blocks */ |
|
553 | - preg_match_all("|" . $this->tag_start_delim . "([A-Za-z0-9\._]+?" . $this->comment_preg . ")" . $this->tag_end_delim. "|", $copy, $var_array); |
|
552 | + /* find & replace variables+blocks */ |
|
553 | + preg_match_all("|" . $this->tag_start_delim . "([A-Za-z0-9\._]+?" . $this->comment_preg . ")" . $this->tag_end_delim. "|", $copy, $var_array); |
|
554 | 554 | |
555 | - $var_array = $var_array[1]; |
|
555 | + $var_array = $var_array[1]; |
|
556 | 556 | |
557 | - foreach ($var_array as $k => $v) { |
|
557 | + foreach ($var_array as $k => $v) { |
|
558 | 558 | |
559 | - // Are there any comments in the tags {tag#a comment for documenting the template} |
|
560 | - $any_comments = explode('#', $v); |
|
561 | - $v = rtrim($any_comments[0]); |
|
559 | + // Are there any comments in the tags {tag#a comment for documenting the template} |
|
560 | + $any_comments = explode('#', $v); |
|
561 | + $v = rtrim($any_comments[0]); |
|
562 | 562 | |
563 | - if (sizeof($any_comments) > 1) { |
|
563 | + if (sizeof($any_comments) > 1) { |
|
564 | 564 | |
565 | - $comments = $any_comments[1]; |
|
566 | - } else { |
|
565 | + $comments = $any_comments[1]; |
|
566 | + } else { |
|
567 | 567 | |
568 | - $comments = ''; |
|
569 | - } |
|
568 | + $comments = ''; |
|
569 | + } |
|
570 | 570 | |
571 | - $sub = explode('.', $v); |
|
571 | + $sub = explode('.', $v); |
|
572 | 572 | |
573 | - if ($sub[0] == '_BLOCK_') { |
|
573 | + if ($sub[0] == '_BLOCK_') { |
|
574 | 574 | |
575 | - unset($sub[0]); |
|
575 | + unset($sub[0]); |
|
576 | 576 | |
577 | - $bname2 = implode('.', $sub); |
|
577 | + $bname2 = implode('.', $sub); |
|
578 | 578 | |
579 | - // trinary operator eliminates assign error in E_ALL reporting |
|
580 | - $var = isset($this->parsed_blocks[$bname2]) ? $this->parsed_blocks[$bname2] : null; |
|
581 | - $nul = (!isset($this->_null_block[$bname2])) ? $this->_null_block[''] : $this->_null_block[$bname2]; |
|
579 | + // trinary operator eliminates assign error in E_ALL reporting |
|
580 | + $var = isset($this->parsed_blocks[$bname2]) ? $this->parsed_blocks[$bname2] : null; |
|
581 | + $nul = (!isset($this->_null_block[$bname2])) ? $this->_null_block[''] : $this->_null_block[$bname2]; |
|
582 | 582 | |
583 | - if ($var === '') { |
|
583 | + if ($var === '') { |
|
584 | 584 | |
585 | - if ($nul == '') { |
|
586 | - // ----------------------------------------------------------- |
|
587 | - // Removed requirement for blocks to be at the start of string |
|
588 | - // ----------------------------------------------------------- |
|
589 | - // $copy=preg_replace("/^\s*\{".$v."\}\s*\n*/m","",$copy); |
|
590 | - // Now blocks don't need to be at the beginning of a line, |
|
591 | - //$copy=preg_replace("/\s*" . $this->tag_start_delim . $v . $this->tag_end_delim . "\s*\n*/m","",$copy); |
|
592 | - $copy = preg_replace("|" . $this->tag_start_delim . $v . $this->tag_end_delim . "|m", '', $copy); |
|
585 | + if ($nul == '') { |
|
586 | + // ----------------------------------------------------------- |
|
587 | + // Removed requirement for blocks to be at the start of string |
|
588 | + // ----------------------------------------------------------- |
|
589 | + // $copy=preg_replace("/^\s*\{".$v."\}\s*\n*/m","",$copy); |
|
590 | + // Now blocks don't need to be at the beginning of a line, |
|
591 | + //$copy=preg_replace("/\s*" . $this->tag_start_delim . $v . $this->tag_end_delim . "\s*\n*/m","",$copy); |
|
592 | + $copy = preg_replace("|" . $this->tag_start_delim . $v . $this->tag_end_delim . "|m", '', $copy); |
|
593 | 593 | |
594 | - } else { |
|
594 | + } else { |
|
595 | 595 | |
596 | - $copy = preg_replace("|" . $this->tag_start_delim . $v . $this->tag_end_delim . "|m", "$nul", $copy); |
|
597 | - } |
|
598 | - } else { |
|
596 | + $copy = preg_replace("|" . $this->tag_start_delim . $v . $this->tag_end_delim . "|m", "$nul", $copy); |
|
597 | + } |
|
598 | + } else { |
|
599 | 599 | |
600 | - //$var = trim($var); |
|
601 | - switch (true) { |
|
602 | - case preg_match('/^\n/', $var) && preg_match('/\n$/', $var): |
|
603 | - $var = substr($var, 1, -1); |
|
604 | - break; |
|
600 | + //$var = trim($var); |
|
601 | + switch (true) { |
|
602 | + case preg_match('/^\n/', $var) && preg_match('/\n$/', $var): |
|
603 | + $var = substr($var, 1, -1); |
|
604 | + break; |
|
605 | 605 | |
606 | - case preg_match('/^\n/', $var): |
|
607 | - $var = substr($var, 1); |
|
608 | - break; |
|
606 | + case preg_match('/^\n/', $var): |
|
607 | + $var = substr($var, 1); |
|
608 | + break; |
|
609 | 609 | |
610 | - case preg_match('/\n$/', $var): |
|
611 | - $var = substr($var, 0, -1); |
|
612 | - break; |
|
613 | - } |
|
610 | + case preg_match('/\n$/', $var): |
|
611 | + $var = substr($var, 0, -1); |
|
612 | + break; |
|
613 | + } |
|
614 | 614 | |
615 | - // SF Bug no. 810773 - thanks anonymous |
|
616 | - $var = str_replace('\\', '\\\\', $var); |
|
617 | - // Ensure dollars in strings are not evaluated reported by SadGeezer 31/3/04 |
|
618 | - $var = str_replace('$', '\\$', $var); |
|
619 | - // Replaced str_replaces with preg_quote |
|
620 | - //$var = preg_quote($var); |
|
621 | - $var = str_replace('\\|', '|', $var); |
|
622 | - $copy = preg_replace("|" . $this->tag_start_delim . $v . $this->tag_end_delim . "|m", "$var", $copy); |
|
615 | + // SF Bug no. 810773 - thanks anonymous |
|
616 | + $var = str_replace('\\', '\\\\', $var); |
|
617 | + // Ensure dollars in strings are not evaluated reported by SadGeezer 31/3/04 |
|
618 | + $var = str_replace('$', '\\$', $var); |
|
619 | + // Replaced str_replaces with preg_quote |
|
620 | + //$var = preg_quote($var); |
|
621 | + $var = str_replace('\\|', '|', $var); |
|
622 | + $copy = preg_replace("|" . $this->tag_start_delim . $v . $this->tag_end_delim . "|m", "$var", $copy); |
|
623 | 623 | |
624 | - if (preg_match('/^\n/', $copy) && preg_match('/\n$/', $copy)) { |
|
625 | - $copy = substr($copy, 1, -1); |
|
626 | - } |
|
627 | - } |
|
628 | - } else { |
|
624 | + if (preg_match('/^\n/', $copy) && preg_match('/\n$/', $copy)) { |
|
625 | + $copy = substr($copy, 1, -1); |
|
626 | + } |
|
627 | + } |
|
628 | + } else { |
|
629 | 629 | |
630 | - $var = $this->vars; |
|
630 | + $var = $this->vars; |
|
631 | 631 | |
632 | - foreach ($sub as $v1) { |
|
632 | + foreach ($sub as $v1) { |
|
633 | 633 | |
634 | - // NW 4 Oct 2002 - Added isset and is_array check to avoid NOTICE messages |
|
635 | - // JC 17 Oct 2002 - Changed EMPTY to stlen=0 |
|
636 | - // if (empty($var[$v1])) { // this line would think that zeros(0) were empty - which is not true |
|
637 | - if (!isset($var[$v1]) || (!is_array($var[$v1]) && strlen($var[$v1]) == 0)) { |
|
634 | + // NW 4 Oct 2002 - Added isset and is_array check to avoid NOTICE messages |
|
635 | + // JC 17 Oct 2002 - Changed EMPTY to stlen=0 |
|
636 | + // if (empty($var[$v1])) { // this line would think that zeros(0) were empty - which is not true |
|
637 | + if (!isset($var[$v1]) || (!is_array($var[$v1]) && strlen($var[$v1]) == 0)) { |
|
638 | 638 | |
639 | - // Check for constant, when variable not assigned |
|
640 | - if (defined($v1)) { |
|
639 | + // Check for constant, when variable not assigned |
|
640 | + if (defined($v1)) { |
|
641 | 641 | |
642 | - $var[$v1] = constant($v1); |
|
642 | + $var[$v1] = constant($v1); |
|
643 | 643 | |
644 | - } else { |
|
644 | + } else { |
|
645 | 645 | |
646 | - $var[$v1] = null; |
|
647 | - } |
|
648 | - } |
|
646 | + $var[$v1] = null; |
|
647 | + } |
|
648 | + } |
|
649 | 649 | |
650 | - $var = $var[$v1]; |
|
651 | - } |
|
650 | + $var = $var[$v1]; |
|
651 | + } |
|
652 | 652 | |
653 | - $nul = (!isset($this->_null_string[$v])) ? ($this->_null_string[""]) : ($this->_null_string[$v]); |
|
654 | - $var = (!isset($var)) ? $nul : $var; |
|
653 | + $nul = (!isset($this->_null_string[$v])) ? ($this->_null_string[""]) : ($this->_null_string[$v]); |
|
654 | + $var = (!isset($var)) ? $nul : $var; |
|
655 | 655 | |
656 | - if ($var === '') { |
|
657 | - // ----------------------------------------------------------- |
|
658 | - // Removed requriement for blocks to be at the start of string |
|
659 | - // ----------------------------------------------------------- |
|
660 | - // $copy=preg_replace("|^\s*\{".$v." ?#?".$comments."\}\s*\n|m","",$copy); |
|
661 | - $copy = preg_replace("|" . $this->tag_start_delim . $v . "( ?#" . $comments . ")?" . $this->tag_end_delim . "|m", '', $copy); |
|
662 | - } |
|
656 | + if ($var === '') { |
|
657 | + // ----------------------------------------------------------- |
|
658 | + // Removed requriement for blocks to be at the start of string |
|
659 | + // ----------------------------------------------------------- |
|
660 | + // $copy=preg_replace("|^\s*\{".$v." ?#?".$comments."\}\s*\n|m","",$copy); |
|
661 | + $copy = preg_replace("|" . $this->tag_start_delim . $v . "( ?#" . $comments . ")?" . $this->tag_end_delim . "|m", '', $copy); |
|
662 | + } |
|
663 | 663 | |
664 | - $var = trim($var); |
|
665 | - // SF Bug no. 810773 - thanks anonymous |
|
666 | - $var = str_replace('\\', '\\\\', $var); |
|
667 | - // Ensure dollars in strings are not evaluated reported by SadGeezer 31/3/04 |
|
668 | - $var = str_replace('$', '\\$', $var); |
|
669 | - // Replace str_replaces with preg_quote |
|
670 | - //$var = preg_quote($var); |
|
671 | - $var = str_replace('\\|', '|', $var); |
|
672 | - $copy = preg_replace("|" . $this->tag_start_delim . $v . "( ?#" . $comments . ")?" . $this->tag_end_delim . "|m", "$var", $copy); |
|
673 | - |
|
674 | - if (preg_match('/^\n/', $copy) && preg_match('/\n$/', $copy)) { |
|
675 | - $copy = substr($copy, 1); |
|
676 | - } |
|
677 | - } |
|
678 | - } |
|
679 | - |
|
680 | - if (isset($this->parsed_blocks[$bname])) { |
|
681 | - $this->parsed_blocks[$bname] .= $copy; |
|
682 | - } else { |
|
683 | - $this->parsed_blocks[$bname] = $copy; |
|
684 | - } |
|
685 | - |
|
686 | - /* reset sub-blocks */ |
|
687 | - if ($this->_autoreset && (!empty($this->sub_blocks[$bname]))) { |
|
688 | - |
|
689 | - reset($this->sub_blocks[$bname]); |
|
690 | - |
|
691 | - foreach ($this->sub_blocks[$bname] as $k => $v) { |
|
692 | - $this->reset($v); |
|
693 | - } |
|
694 | - } |
|
695 | - } |
|
696 | - |
|
697 | - /** |
|
664 | + $var = trim($var); |
|
665 | + // SF Bug no. 810773 - thanks anonymous |
|
666 | + $var = str_replace('\\', '\\\\', $var); |
|
667 | + // Ensure dollars in strings are not evaluated reported by SadGeezer 31/3/04 |
|
668 | + $var = str_replace('$', '\\$', $var); |
|
669 | + // Replace str_replaces with preg_quote |
|
670 | + //$var = preg_quote($var); |
|
671 | + $var = str_replace('\\|', '|', $var); |
|
672 | + $copy = preg_replace("|" . $this->tag_start_delim . $v . "( ?#" . $comments . ")?" . $this->tag_end_delim . "|m", "$var", $copy); |
|
673 | + |
|
674 | + if (preg_match('/^\n/', $copy) && preg_match('/\n$/', $copy)) { |
|
675 | + $copy = substr($copy, 1); |
|
676 | + } |
|
677 | + } |
|
678 | + } |
|
679 | + |
|
680 | + if (isset($this->parsed_blocks[$bname])) { |
|
681 | + $this->parsed_blocks[$bname] .= $copy; |
|
682 | + } else { |
|
683 | + $this->parsed_blocks[$bname] = $copy; |
|
684 | + } |
|
685 | + |
|
686 | + /* reset sub-blocks */ |
|
687 | + if ($this->_autoreset && (!empty($this->sub_blocks[$bname]))) { |
|
688 | + |
|
689 | + reset($this->sub_blocks[$bname]); |
|
690 | + |
|
691 | + foreach ($this->sub_blocks[$bname] as $k => $v) { |
|
692 | + $this->reset($v); |
|
693 | + } |
|
694 | + } |
|
695 | + } |
|
696 | + |
|
697 | + /** |
|
698 | 698 | * returns the parsed text for a block, including all sub-blocks. |
699 | 699 | * |
700 | 700 | * @access public |
701 | 701 | * @param string $bname Block name to parse |
702 | 702 | */ |
703 | - public function rparse ($bname) { |
|
703 | + public function rparse ($bname) { |
|
704 | 704 | |
705 | - if (!empty($this->sub_blocks[$bname])) { |
|
705 | + if (!empty($this->sub_blocks[$bname])) { |
|
706 | 706 | |
707 | - reset($this->sub_blocks[$bname]); |
|
707 | + reset($this->sub_blocks[$bname]); |
|
708 | 708 | |
709 | - foreach ($this->sub_blocks[$bname] as $k => $v) { |
|
709 | + foreach ($this->sub_blocks[$bname] as $k => $v) { |
|
710 | 710 | |
711 | - if (!empty($v)) { |
|
712 | - $this->rparse($v); |
|
713 | - } |
|
714 | - } |
|
715 | - } |
|
711 | + if (!empty($v)) { |
|
712 | + $this->rparse($v); |
|
713 | + } |
|
714 | + } |
|
715 | + } |
|
716 | 716 | |
717 | - $this->parse($bname); |
|
718 | - } |
|
717 | + $this->parse($bname); |
|
718 | + } |
|
719 | 719 | |
720 | - /** |
|
720 | + /** |
|
721 | 721 | * inserts a loop ( call assign & parse ) |
722 | 722 | * |
723 | 723 | * @access public |
724 | 724 | * @param string $bname Block name to assign |
725 | 725 | * @param string $var Variable to assign values to |
726 | 726 | * @param string / array $value Value to assign to $var |
727 | - */ |
|
728 | - public function insert_loop ($bname, $var, $value = '') { |
|
727 | + */ |
|
728 | + public function insert_loop ($bname, $var, $value = '') { |
|
729 | 729 | |
730 | - $this->assign($var, $value); |
|
731 | - $this->parse($bname); |
|
732 | - } |
|
730 | + $this->assign($var, $value); |
|
731 | + $this->parse($bname); |
|
732 | + } |
|
733 | 733 | |
734 | - /** |
|
734 | + /** |
|
735 | 735 | * parses a block for every set of data in the values array |
736 | 736 | * |
737 | 737 | * @access public |
738 | 738 | * @param string $bname Block name to loop |
739 | 739 | * @param string $var Variable to assign values to |
740 | 740 | * @param array $values Values to assign to $var |
741 | - */ |
|
742 | - public function array_loop ($bname, $var, &$values) { |
|
741 | + */ |
|
742 | + public function array_loop ($bname, $var, &$values) { |
|
743 | 743 | |
744 | - if (is_array($values)) { |
|
744 | + if (is_array($values)) { |
|
745 | 745 | |
746 | - foreach($values as $v) { |
|
746 | + foreach($values as $v) { |
|
747 | 747 | |
748 | - $this->insert_loop($bname, $var, $v); |
|
749 | - } |
|
750 | - } |
|
751 | - } |
|
748 | + $this->insert_loop($bname, $var, $v); |
|
749 | + } |
|
750 | + } |
|
751 | + } |
|
752 | 752 | |
753 | - /** |
|
753 | + /** |
|
754 | 754 | * returns the parsed text for a block |
755 | 755 | * |
756 | 756 | * @access public |
757 | 757 | * @param string $bname Block name to return |
758 | 758 | * @return string |
759 | 759 | */ |
760 | - public function text ($bname = '') { |
|
760 | + public function text ($bname = '') { |
|
761 | 761 | |
762 | - $text = ''; |
|
762 | + $text = ''; |
|
763 | 763 | |
764 | - if ($this->debug && $this->output_type == 'HTML') { |
|
765 | - // JC 20/11/02 echo the template filename if in development as |
|
766 | - // html comment |
|
767 | - $text .= '<!-- XTemplate: ' . realpath($this->filename) . " -->\n"; |
|
768 | - } |
|
764 | + if ($this->debug && $this->output_type == 'HTML') { |
|
765 | + // JC 20/11/02 echo the template filename if in development as |
|
766 | + // html comment |
|
767 | + $text .= '<!-- XTemplate: ' . realpath($this->filename) . " -->\n"; |
|
768 | + } |
|
769 | 769 | |
770 | - $bname = !empty($bname) ? $bname : $this->mainblock; |
|
770 | + $bname = !empty($bname) ? $bname : $this->mainblock; |
|
771 | 771 | |
772 | - $text .= isset($this->parsed_blocks[$bname]) ? $this->parsed_blocks[$bname] : $this->get_error(); |
|
772 | + $text .= isset($this->parsed_blocks[$bname]) ? $this->parsed_blocks[$bname] : $this->get_error(); |
|
773 | 773 | |
774 | - return $text; |
|
775 | - } |
|
774 | + return $text; |
|
775 | + } |
|
776 | 776 | |
777 | - /** |
|
777 | + /** |
|
778 | 778 | * prints the parsed text |
779 | 779 | * |
780 | 780 | * @access public |
781 | 781 | * @param string $bname Block name to echo out |
782 | 782 | */ |
783 | - public function out ($bname) { |
|
783 | + public function out ($bname) { |
|
784 | 784 | |
785 | - $out = $this->text($bname); |
|
786 | - // $length=strlen($out); |
|
787 | - //header("Content-Length: ".$length); // TODO: Comment this back in later |
|
785 | + $out = $this->text($bname); |
|
786 | + // $length=strlen($out); |
|
787 | + //header("Content-Length: ".$length); // TODO: Comment this back in later |
|
788 | 788 | |
789 | - echo $out; |
|
790 | - } |
|
789 | + echo $out; |
|
790 | + } |
|
791 | 791 | |
792 | - /** |
|
792 | + /** |
|
793 | 793 | * prints the parsed text to a specified file |
794 | 794 | * |
795 | 795 | * @access public |
796 | 796 | * @param string $bname Block name to write out |
797 | 797 | * @param string $fname File name to write to |
798 | 798 | */ |
799 | - public function out_file ($bname, $fname) { |
|
799 | + public function out_file ($bname, $fname) { |
|
800 | 800 | |
801 | - if (!empty($bname) && !empty($fname) && is_writeable($fname)) { |
|
801 | + if (!empty($bname) && !empty($fname) && is_writeable($fname)) { |
|
802 | 802 | |
803 | - $fp = fopen($fname, 'w'); |
|
804 | - fwrite($fp, $this->text($bname)); |
|
805 | - fclose($fp); |
|
806 | - } |
|
807 | - } |
|
803 | + $fp = fopen($fname, 'w'); |
|
804 | + fwrite($fp, $this->text($bname)); |
|
805 | + fclose($fp); |
|
806 | + } |
|
807 | + } |
|
808 | 808 | |
809 | - /** |
|
809 | + /** |
|
810 | 810 | * resets the parsed text |
811 | 811 | * |
812 | 812 | * @access public |
813 | 813 | * @param string $bname Block to reset |
814 | 814 | */ |
815 | - public function reset ($bname) { |
|
815 | + public function reset ($bname) { |
|
816 | 816 | |
817 | - $this->parsed_blocks[$bname] = ''; |
|
818 | - } |
|
817 | + $this->parsed_blocks[$bname] = ''; |
|
818 | + } |
|
819 | 819 | |
820 | - /** |
|
820 | + /** |
|
821 | 821 | * returns true if block was parsed, false if not |
822 | 822 | * |
823 | 823 | * @access public |
824 | 824 | * @param string $bname Block name to test |
825 | 825 | * @return boolean |
826 | 826 | */ |
827 | - public function parsed ($bname) { |
|
827 | + public function parsed ($bname) { |
|
828 | 828 | |
829 | - return (!empty($this->parsed_blocks[$bname])); |
|
830 | - } |
|
829 | + return (!empty($this->parsed_blocks[$bname])); |
|
830 | + } |
|
831 | 831 | |
832 | - /** |
|
832 | + /** |
|
833 | 833 | * sets the string to replace in case the var was not assigned |
834 | 834 | * |
835 | 835 | * @access public |
836 | 836 | * @param string $str Display string for null block |
837 | 837 | * @param string $varname Variable name to apply $str to |
838 | 838 | */ |
839 | - public function set_null_string($str, $varname = '') { |
|
840 | - |
|
841 | - $this->_null_string[$varname] = $str; |
|
842 | - } |
|
843 | - |
|
844 | - /** |
|
845 | - * Backwards compatibility only |
|
846 | - * |
|
847 | - * @param string $str |
|
848 | - * @param string $varname |
|
849 | - * @deprecated Change to set_null_string to keep in with rest of naming convention |
|
850 | - */ |
|
851 | - public function SetNullString ($str, $varname = '') { |
|
852 | - $this->set_null_string($str, $varname); |
|
853 | - } |
|
854 | - |
|
855 | - /** |
|
839 | + public function set_null_string($str, $varname = '') { |
|
840 | + |
|
841 | + $this->_null_string[$varname] = $str; |
|
842 | + } |
|
843 | + |
|
844 | + /** |
|
845 | + * Backwards compatibility only |
|
846 | + * |
|
847 | + * @param string $str |
|
848 | + * @param string $varname |
|
849 | + * @deprecated Change to set_null_string to keep in with rest of naming convention |
|
850 | + */ |
|
851 | + public function SetNullString ($str, $varname = '') { |
|
852 | + $this->set_null_string($str, $varname); |
|
853 | + } |
|
854 | + |
|
855 | + /** |
|
856 | 856 | * sets the string to replace in case the block was not parsed |
857 | 857 | * |
858 | 858 | * @access public |
859 | 859 | * @param string $str Display string for null block |
860 | 860 | * @param string $bname Block name to apply $str to |
861 | 861 | */ |
862 | - public function set_null_block ($str, $bname = '') { |
|
863 | - |
|
864 | - $this->_null_block[$bname] = $str; |
|
865 | - } |
|
866 | - |
|
867 | - /** |
|
868 | - * Backwards compatibility only |
|
869 | - * |
|
870 | - * @param string $str |
|
871 | - * @param string $bname |
|
872 | - * @deprecated Change to set_null_block to keep in with rest of naming convention |
|
873 | - */ |
|
874 | - public function SetNullBlock ($str, $bname = '') { |
|
875 | - $this->set_null_block($str, $bname); |
|
876 | - } |
|
877 | - |
|
878 | - /** |
|
862 | + public function set_null_block ($str, $bname = '') { |
|
863 | + |
|
864 | + $this->_null_block[$bname] = $str; |
|
865 | + } |
|
866 | + |
|
867 | + /** |
|
868 | + * Backwards compatibility only |
|
869 | + * |
|
870 | + * @param string $str |
|
871 | + * @param string $bname |
|
872 | + * @deprecated Change to set_null_block to keep in with rest of naming convention |
|
873 | + */ |
|
874 | + public function SetNullBlock ($str, $bname = '') { |
|
875 | + $this->set_null_block($str, $bname); |
|
876 | + } |
|
877 | + |
|
878 | + /** |
|
879 | 879 | * sets AUTORESET to 1. (default is 1) |
880 | 880 | * if set to 1, parse() automatically resets the parsed blocks' sub blocks |
881 | 881 | * (for multiple level blocks) |
882 | 882 | * |
883 | 883 | * @access public |
884 | 884 | */ |
885 | - public function set_autoreset () { |
|
885 | + public function set_autoreset () { |
|
886 | 886 | |
887 | - $this->_autoreset = true; |
|
888 | - } |
|
887 | + $this->_autoreset = true; |
|
888 | + } |
|
889 | 889 | |
890 | - /** |
|
890 | + /** |
|
891 | 891 | * sets AUTORESET to 0. (default is 1) |
892 | 892 | * if set to 1, parse() automatically resets the parsed blocks' sub blocks |
893 | 893 | * (for multiple level blocks) |
894 | 894 | * |
895 | 895 | * @access public |
896 | 896 | */ |
897 | - public function clear_autoreset () { |
|
897 | + public function clear_autoreset () { |
|
898 | 898 | |
899 | - $this->_autoreset = false; |
|
900 | - } |
|
899 | + $this->_autoreset = false; |
|
900 | + } |
|
901 | 901 | |
902 | - /** |
|
902 | + /** |
|
903 | 903 | * scans global variables and assigns to PHP array |
904 | 904 | * |
905 | 905 | * @access public |
906 | 906 | */ |
907 | - public function scan_globals () { |
|
907 | + public function scan_globals () { |
|
908 | 908 | |
909 | - reset($GLOBALS); |
|
909 | + reset($GLOBALS); |
|
910 | 910 | |
911 | - foreach ($GLOBALS as $k => $v) { |
|
912 | - $GLOB[$k] = $v; |
|
913 | - } |
|
911 | + foreach ($GLOBALS as $k => $v) { |
|
912 | + $GLOB[$k] = $v; |
|
913 | + } |
|
914 | 914 | |
915 | - /** |
|
916 | - * Access global variables as: |
|
917 | - * @example {PHP._SERVER.HTTP_HOST} |
|
918 | - * in your template! |
|
919 | - */ |
|
920 | - $this->assign('PHP', $GLOB); |
|
921 | - } |
|
915 | + /** |
|
916 | + * Access global variables as: |
|
917 | + * @example {PHP._SERVER.HTTP_HOST} |
|
918 | + * in your template! |
|
919 | + */ |
|
920 | + $this->assign('PHP', $GLOB); |
|
921 | + } |
|
922 | 922 | |
923 | - /** |
|
923 | + /** |
|
924 | 924 | * gets error condition / string |
925 | 925 | * |
926 | 926 | * @access public |
927 | 927 | * @return boolean / string |
928 | 928 | */ |
929 | - public function get_error () { |
|
929 | + public function get_error () { |
|
930 | 930 | |
931 | - // JRC: 3/1/2003 Added ouptut wrapper and detection of output type for error message output |
|
932 | - $retval = false; |
|
931 | + // JRC: 3/1/2003 Added ouptut wrapper and detection of output type for error message output |
|
932 | + $retval = false; |
|
933 | 933 | |
934 | - if ($this->_error != '') { |
|
934 | + if ($this->_error != '') { |
|
935 | 935 | |
936 | - switch ($this->output_type) { |
|
937 | - case 'HTML': |
|
938 | - case 'html': |
|
939 | - $retval = '<b>[XTemplate]</b><ul>' . nl2br(str_replace('* ', '<li>', str_replace(" *\n", "</li>\n", $this->_error))) . '</ul>'; |
|
940 | - break; |
|
936 | + switch ($this->output_type) { |
|
937 | + case 'HTML': |
|
938 | + case 'html': |
|
939 | + $retval = '<b>[XTemplate]</b><ul>' . nl2br(str_replace('* ', '<li>', str_replace(" *\n", "</li>\n", $this->_error))) . '</ul>'; |
|
940 | + break; |
|
941 | 941 | |
942 | - default: |
|
943 | - $retval = '[XTemplate] ' . str_replace(' *\n', "\n", $this->_error); |
|
944 | - break; |
|
945 | - } |
|
946 | - } |
|
942 | + default: |
|
943 | + $retval = '[XTemplate] ' . str_replace(' *\n', "\n", $this->_error); |
|
944 | + break; |
|
945 | + } |
|
946 | + } |
|
947 | 947 | |
948 | - return $retval; |
|
949 | - } |
|
948 | + return $retval; |
|
949 | + } |
|
950 | 950 | |
951 | - /***************************************************************************/ |
|
952 | - /***[ private stuff ]*******************************************************/ |
|
953 | - /***************************************************************************/ |
|
951 | + /***************************************************************************/ |
|
952 | + /***[ private stuff ]*******************************************************/ |
|
953 | + /***************************************************************************/ |
|
954 | 954 | |
955 | - /** |
|
955 | + /** |
|
956 | 956 | * generates the array containing to-be-parsed stuff: |
957 | 957 | * $blocks["main"],$blocks["main.table"],$blocks["main.table.row"], etc. |
958 | 958 | * also builds the reverse parse order. |
@@ -961,343 +961,343 @@ discard block |
||
961 | 961 | * @param string $con content to be processed |
962 | 962 | * @param string $parentblock name of the parent block in the block hierarchy |
963 | 963 | */ |
964 | - public function _maketree ($con, $parentblock='') { |
|
964 | + public function _maketree ($con, $parentblock='') { |
|
965 | 965 | |
966 | - $blocks = array(); |
|
966 | + $blocks = array(); |
|
967 | 967 | |
968 | - $con2 = explode($this->block_start_delim, $con); |
|
968 | + $con2 = explode($this->block_start_delim, $con); |
|
969 | 969 | |
970 | - if (!empty($parentblock)) { |
|
970 | + if (!empty($parentblock)) { |
|
971 | 971 | |
972 | - $block_names = explode('.', $parentblock); |
|
973 | - $level = sizeof($block_names); |
|
972 | + $block_names = explode('.', $parentblock); |
|
973 | + $level = sizeof($block_names); |
|
974 | 974 | |
975 | - } else { |
|
975 | + } else { |
|
976 | 976 | |
977 | - $block_names = array(); |
|
978 | - $level = 0; |
|
979 | - } |
|
977 | + $block_names = array(); |
|
978 | + $level = 0; |
|
979 | + } |
|
980 | 980 | |
981 | - // JRC 06/04/2005 Added block comments (on BEGIN or END) <!-- BEGIN: block_name#Comments placed here --> |
|
982 | - //$patt = "($this->block_start_word|$this->block_end_word)\s*(\w+)\s*$this->block_end_delim(.*)"; |
|
983 | - $patt = "(" . $this->block_start_word . "|" . $this->block_end_word . ")\s*(\w+)" . $this->comment_preg . "\s*" . $this->block_end_delim . "(.*)"; |
|
981 | + // JRC 06/04/2005 Added block comments (on BEGIN or END) <!-- BEGIN: block_name#Comments placed here --> |
|
982 | + //$patt = "($this->block_start_word|$this->block_end_word)\s*(\w+)\s*$this->block_end_delim(.*)"; |
|
983 | + $patt = "(" . $this->block_start_word . "|" . $this->block_end_word . ")\s*(\w+)" . $this->comment_preg . "\s*" . $this->block_end_delim . "(.*)"; |
|
984 | 984 | |
985 | - foreach($con2 as $k => $v) { |
|
985 | + foreach($con2 as $k => $v) { |
|
986 | 986 | |
987 | - $res = array(); |
|
987 | + $res = array(); |
|
988 | 988 | |
989 | - if (preg_match_all("/$patt/ims", $v, $res, PREG_SET_ORDER)) { |
|
990 | - // $res[0][1] = BEGIN or END |
|
991 | - // $res[0][2] = block name |
|
992 | - // $res[0][3] = comment |
|
993 | - // $res[0][4] = kinda content |
|
994 | - $block_word = $res[0][1]; |
|
995 | - $block_name = $res[0][2]; |
|
996 | - $comment = $res[0][3]; |
|
997 | - $content = $res[0][4]; |
|
989 | + if (preg_match_all("/$patt/ims", $v, $res, PREG_SET_ORDER)) { |
|
990 | + // $res[0][1] = BEGIN or END |
|
991 | + // $res[0][2] = block name |
|
992 | + // $res[0][3] = comment |
|
993 | + // $res[0][4] = kinda content |
|
994 | + $block_word = $res[0][1]; |
|
995 | + $block_name = $res[0][2]; |
|
996 | + $comment = $res[0][3]; |
|
997 | + $content = $res[0][4]; |
|
998 | 998 | |
999 | - if (strtoupper($block_word) == $this->block_start_word) { |
|
999 | + if (strtoupper($block_word) == $this->block_start_word) { |
|
1000 | 1000 | |
1001 | - $parent_name = implode('.', $block_names); |
|
1001 | + $parent_name = implode('.', $block_names); |
|
1002 | 1002 | |
1003 | - // add one level - array("main","table","row") |
|
1004 | - $block_names[++$level] = $block_name; |
|
1003 | + // add one level - array("main","table","row") |
|
1004 | + $block_names[++$level] = $block_name; |
|
1005 | 1005 | |
1006 | - // make block name (main.table.row) |
|
1007 | - $cur_block_name=implode('.', $block_names); |
|
1006 | + // make block name (main.table.row) |
|
1007 | + $cur_block_name=implode('.', $block_names); |
|
1008 | 1008 | |
1009 | - // build block parsing order (reverse) |
|
1010 | - $this->block_parse_order[] = $cur_block_name; |
|
1009 | + // build block parsing order (reverse) |
|
1010 | + $this->block_parse_order[] = $cur_block_name; |
|
1011 | 1011 | |
1012 | - //add contents. trinary operator eliminates assign error in E_ALL reporting |
|
1013 | - $blocks[$cur_block_name] = isset($blocks[$cur_block_name]) ? $blocks[$cur_block_name] . $content : $content; |
|
1012 | + //add contents. trinary operator eliminates assign error in E_ALL reporting |
|
1013 | + $blocks[$cur_block_name] = isset($blocks[$cur_block_name]) ? $blocks[$cur_block_name] . $content : $content; |
|
1014 | 1014 | |
1015 | - // add {_BLOCK_.blockname} string to parent block |
|
1016 | - $blocks[$parent_name] .= str_replace('\\', '', $this->tag_start_delim) . '_BLOCK_.' . $cur_block_name . str_replace('\\', '', $this->tag_end_delim); |
|
1015 | + // add {_BLOCK_.blockname} string to parent block |
|
1016 | + $blocks[$parent_name] .= str_replace('\\', '', $this->tag_start_delim) . '_BLOCK_.' . $cur_block_name . str_replace('\\', '', $this->tag_end_delim); |
|
1017 | 1017 | |
1018 | - // store sub block names for autoresetting and recursive parsing |
|
1019 | - $this->sub_blocks[$parent_name][] = $cur_block_name; |
|
1018 | + // store sub block names for autoresetting and recursive parsing |
|
1019 | + $this->sub_blocks[$parent_name][] = $cur_block_name; |
|
1020 | 1020 | |
1021 | - // store sub block names for autoresetting |
|
1022 | - $this->sub_blocks[$cur_block_name][] = ''; |
|
1021 | + // store sub block names for autoresetting |
|
1022 | + $this->sub_blocks[$cur_block_name][] = ''; |
|
1023 | 1023 | |
1024 | - } else if (strtoupper($block_word) == $this->block_end_word) { |
|
1024 | + } else if (strtoupper($block_word) == $this->block_end_word) { |
|
1025 | 1025 | |
1026 | - unset($block_names[$level--]); |
|
1026 | + unset($block_names[$level--]); |
|
1027 | 1027 | |
1028 | - $parent_name = implode('.', $block_names); |
|
1028 | + $parent_name = implode('.', $block_names); |
|
1029 | 1029 | |
1030 | - // add rest of block to parent block |
|
1031 | - $blocks[$parent_name] .= $content; |
|
1032 | - } |
|
1033 | - } else { |
|
1030 | + // add rest of block to parent block |
|
1031 | + $blocks[$parent_name] .= $content; |
|
1032 | + } |
|
1033 | + } else { |
|
1034 | 1034 | |
1035 | - // no block delimiters found |
|
1036 | - // Saves doing multiple implodes - less overhead |
|
1037 | - $tmp = implode('.', $block_names); |
|
1035 | + // no block delimiters found |
|
1036 | + // Saves doing multiple implodes - less overhead |
|
1037 | + $tmp = implode('.', $block_names); |
|
1038 | 1038 | |
1039 | - if ($k) { |
|
1040 | - $blocks[$tmp] .= $this->block_start_delim; |
|
1041 | - } |
|
1039 | + if ($k) { |
|
1040 | + $blocks[$tmp] .= $this->block_start_delim; |
|
1041 | + } |
|
1042 | 1042 | |
1043 | - // trinary operator eliminates assign error in E_ALL reporting |
|
1044 | - $blocks[$tmp] = isset($blocks[$tmp]) ? $blocks[$tmp] . $v : $v; |
|
1045 | - } |
|
1046 | - } |
|
1043 | + // trinary operator eliminates assign error in E_ALL reporting |
|
1044 | + $blocks[$tmp] = isset($blocks[$tmp]) ? $blocks[$tmp] . $v : $v; |
|
1045 | + } |
|
1046 | + } |
|
1047 | 1047 | |
1048 | - return $blocks; |
|
1049 | - } |
|
1048 | + return $blocks; |
|
1049 | + } |
|
1050 | 1050 | |
1051 | - /** |
|
1051 | + /** |
|
1052 | 1052 | * Sub processing for assign_file method |
1053 | 1053 | * |
1054 | 1054 | * @access private |
1055 | 1055 | * @param string $name |
1056 | 1056 | * @param string $val |
1057 | 1057 | */ |
1058 | - private function _assign_file_sub ($name, $val) { |
|
1058 | + private function _assign_file_sub ($name, $val) { |
|
1059 | 1059 | |
1060 | - if (isset($this->filevar_parent[$name])) { |
|
1060 | + if (isset($this->filevar_parent[$name])) { |
|
1061 | 1061 | |
1062 | - if ($val != '') { |
|
1062 | + if ($val != '') { |
|
1063 | 1063 | |
1064 | - $val = $this->_r_getfile($val); |
|
1064 | + $val = $this->_r_getfile($val); |
|
1065 | 1065 | |
1066 | - foreach($this->filevar_parent[$name] as $parent) { |
|
1066 | + foreach($this->filevar_parent[$name] as $parent) { |
|
1067 | 1067 | |
1068 | - if (isset($this->preparsed_blocks[$parent]) && !isset($this->filevars[$name])) { |
|
1068 | + if (isset($this->preparsed_blocks[$parent]) && !isset($this->filevars[$name])) { |
|
1069 | 1069 | |
1070 | - $copy = $this->preparsed_blocks[$parent]; |
|
1070 | + $copy = $this->preparsed_blocks[$parent]; |
|
1071 | 1071 | |
1072 | - } elseif (isset($this->blocks[$parent])) { |
|
1072 | + } elseif (isset($this->blocks[$parent])) { |
|
1073 | 1073 | |
1074 | - $copy = $this->blocks[$parent]; |
|
1075 | - } |
|
1074 | + $copy = $this->blocks[$parent]; |
|
1075 | + } |
|
1076 | 1076 | |
1077 | - $res = array(); |
|
1077 | + $res = array(); |
|
1078 | 1078 | |
1079 | - preg_match_all($this->filevar_delim, $copy, $res, PREG_SET_ORDER); |
|
1079 | + preg_match_all($this->filevar_delim, $copy, $res, PREG_SET_ORDER); |
|
1080 | 1080 | |
1081 | - if (is_array($res) && isset($res[0])) { |
|
1081 | + if (is_array($res) && isset($res[0])) { |
|
1082 | 1082 | |
1083 | - // Changed as per solution in SF bug ID #1261828 |
|
1084 | - foreach ($res as $v) { |
|
1083 | + // Changed as per solution in SF bug ID #1261828 |
|
1084 | + foreach ($res as $v) { |
|
1085 | 1085 | |
1086 | - // Changed as per solution in SF bug ID #1261828 |
|
1087 | - if ($v[1] == $name) { |
|
1086 | + // Changed as per solution in SF bug ID #1261828 |
|
1087 | + if ($v[1] == $name) { |
|
1088 | 1088 | |
1089 | - // Changed as per solution in SF bug ID #1261828 |
|
1090 | - $copy = preg_replace("/" . preg_quote($v[0]) . "/", "$val", $copy); |
|
1091 | - $this->preparsed_blocks = array_merge($this->preparsed_blocks, $this->_maketree($copy, $parent)); |
|
1092 | - $this->filevar_parent = array_merge($this->filevar_parent, $this->_store_filevar_parents($this->preparsed_blocks)); |
|
1093 | - } |
|
1094 | - } |
|
1095 | - } |
|
1096 | - } |
|
1097 | - } |
|
1098 | - } |
|
1089 | + // Changed as per solution in SF bug ID #1261828 |
|
1090 | + $copy = preg_replace("/" . preg_quote($v[0]) . "/", "$val", $copy); |
|
1091 | + $this->preparsed_blocks = array_merge($this->preparsed_blocks, $this->_maketree($copy, $parent)); |
|
1092 | + $this->filevar_parent = array_merge($this->filevar_parent, $this->_store_filevar_parents($this->preparsed_blocks)); |
|
1093 | + } |
|
1094 | + } |
|
1095 | + } |
|
1096 | + } |
|
1097 | + } |
|
1098 | + } |
|
1099 | 1099 | |
1100 | - $this->filevars[$name] = $val; |
|
1101 | - } |
|
1100 | + $this->filevars[$name] = $val; |
|
1101 | + } |
|
1102 | 1102 | |
1103 | - /** |
|
1103 | + /** |
|
1104 | 1104 | * store container block's name for file variables |
1105 | 1105 | * |
1106 | 1106 | * @access public - aiming for private |
1107 | 1107 | * @param array $blocks |
1108 | 1108 | * @return array |
1109 | 1109 | */ |
1110 | - public function _store_filevar_parents ($blocks){ |
|
1110 | + public function _store_filevar_parents ($blocks){ |
|
1111 | 1111 | |
1112 | - $parents = array(); |
|
1112 | + $parents = array(); |
|
1113 | 1113 | |
1114 | - foreach ($blocks as $bname => $con) { |
|
1114 | + foreach ($blocks as $bname => $con) { |
|
1115 | 1115 | |
1116 | - $res = array(); |
|
1116 | + $res = array(); |
|
1117 | 1117 | |
1118 | - preg_match_all($this->filevar_delim, $con, $res); |
|
1118 | + preg_match_all($this->filevar_delim, $con, $res); |
|
1119 | 1119 | |
1120 | - foreach ($res[1] as $k => $v) { |
|
1120 | + foreach ($res[1] as $k => $v) { |
|
1121 | 1121 | |
1122 | - $parents[$v][] = $bname; |
|
1123 | - } |
|
1124 | - } |
|
1125 | - return $parents; |
|
1126 | - } |
|
1122 | + $parents[$v][] = $bname; |
|
1123 | + } |
|
1124 | + } |
|
1125 | + return $parents; |
|
1126 | + } |
|
1127 | 1127 | |
1128 | - /** |
|
1128 | + /** |
|
1129 | 1129 | * Set the error string |
1130 | 1130 | * |
1131 | 1131 | * @access private |
1132 | 1132 | * @param string $str |
1133 | 1133 | */ |
1134 | - private function _set_error ($str) { |
|
1134 | + private function _set_error ($str) { |
|
1135 | 1135 | |
1136 | - // JRC: 3/1/2003 Made to append the error messages |
|
1137 | - $this->_error .= '* ' . $str . " *\n"; |
|
1138 | - // JRC: 3/1/2003 Removed trigger error, use this externally if you want it eg. trigger_error($xtpl->get_error()) |
|
1139 | - //trigger_error($this->get_error()); |
|
1140 | - } |
|
1136 | + // JRC: 3/1/2003 Made to append the error messages |
|
1137 | + $this->_error .= '* ' . $str . " *\n"; |
|
1138 | + // JRC: 3/1/2003 Removed trigger error, use this externally if you want it eg. trigger_error($xtpl->get_error()) |
|
1139 | + //trigger_error($this->get_error()); |
|
1140 | + } |
|
1141 | 1141 | |
1142 | - /** |
|
1142 | + /** |
|
1143 | 1143 | * returns the contents of a file |
1144 | 1144 | * |
1145 | 1145 | * @access protected |
1146 | 1146 | * @param string $file |
1147 | 1147 | * @return string |
1148 | 1148 | */ |
1149 | - protected function _getfile ($file) { |
|
1149 | + protected function _getfile ($file) { |
|
1150 | 1150 | |
1151 | - if (!isset($file)) { |
|
1152 | - // JC 19/12/02 added $file to error message |
|
1153 | - $this->_set_error('!isset file name!' . $file); |
|
1151 | + if (!isset($file)) { |
|
1152 | + // JC 19/12/02 added $file to error message |
|
1153 | + $this->_set_error('!isset file name!' . $file); |
|
1154 | 1154 | |
1155 | - return ''; |
|
1156 | - } |
|
1155 | + return ''; |
|
1156 | + } |
|
1157 | 1157 | |
1158 | - // check if filename is mapped to other filename |
|
1159 | - if (isset($this->files)) { |
|
1158 | + // check if filename is mapped to other filename |
|
1159 | + if (isset($this->files)) { |
|
1160 | 1160 | |
1161 | - if (isset($this->files[$file])) { |
|
1161 | + if (isset($this->files[$file])) { |
|
1162 | 1162 | |
1163 | - $file = $this->files[$file]; |
|
1164 | - } |
|
1165 | - } |
|
1163 | + $file = $this->files[$file]; |
|
1164 | + } |
|
1165 | + } |
|
1166 | 1166 | |
1167 | - // prepend template dir |
|
1168 | - if (!empty($this->tpldir)) { |
|
1167 | + // prepend template dir |
|
1168 | + if (!empty($this->tpldir)) { |
|
1169 | 1169 | |
1170 | - /** |
|
1171 | - * Support hierarchy of file locations to search |
|
1172 | - * |
|
1173 | - * @example Supply array of filepaths when instantiating |
|
1174 | - * First path supplied that has the named file is prioritised |
|
1175 | - * $xtpl = new XTemplate('myfile.xtpl', array('.','/mypath', '/mypath2')); |
|
1176 | - * @since 29/05/2007 |
|
1177 | - */ |
|
1178 | - if (is_array($this->tpldir)) { |
|
1170 | + /** |
|
1171 | + * Support hierarchy of file locations to search |
|
1172 | + * |
|
1173 | + * @example Supply array of filepaths when instantiating |
|
1174 | + * First path supplied that has the named file is prioritised |
|
1175 | + * $xtpl = new XTemplate('myfile.xtpl', array('.','/mypath', '/mypath2')); |
|
1176 | + * @since 29/05/2007 |
|
1177 | + */ |
|
1178 | + if (is_array($this->tpldir)) { |
|
1179 | 1179 | |
1180 | - foreach ($this->tpldir as $dir) { |
|
1180 | + foreach ($this->tpldir as $dir) { |
|
1181 | 1181 | |
1182 | - if (is_readable($dir . DIRECTORY_SEPARATOR . $file)) { |
|
1183 | - $file = $dir . DIRECTORY_SEPARATOR . $file; |
|
1184 | - break; |
|
1185 | - } |
|
1186 | - } |
|
1187 | - } else { |
|
1182 | + if (is_readable($dir . DIRECTORY_SEPARATOR . $file)) { |
|
1183 | + $file = $dir . DIRECTORY_SEPARATOR . $file; |
|
1184 | + break; |
|
1185 | + } |
|
1186 | + } |
|
1187 | + } else { |
|
1188 | 1188 | |
1189 | - $file = $this->tpldir. DIRECTORY_SEPARATOR . $file; |
|
1190 | - } |
|
1191 | - } |
|
1189 | + $file = $this->tpldir. DIRECTORY_SEPARATOR . $file; |
|
1190 | + } |
|
1191 | + } |
|
1192 | 1192 | |
1193 | - $file_text = ''; |
|
1193 | + $file_text = ''; |
|
1194 | 1194 | |
1195 | - if (isset($this->filecache[$file])) { |
|
1195 | + if (isset($this->filecache[$file])) { |
|
1196 | 1196 | |
1197 | - $file_text .= $this->filecache[$file]; |
|
1197 | + $file_text .= $this->filecache[$file]; |
|
1198 | 1198 | |
1199 | - if ($this->debug) { |
|
1200 | - $file_text = '<!-- XTemplate debug cached: ' . realpath($file) . ' -->' . "\n" . $file_text; |
|
1201 | - } |
|
1199 | + if ($this->debug) { |
|
1200 | + $file_text = '<!-- XTemplate debug cached: ' . realpath($file) . ' -->' . "\n" . $file_text; |
|
1201 | + } |
|
1202 | 1202 | |
1203 | - } else { |
|
1203 | + } else { |
|
1204 | 1204 | |
1205 | - if (is_file($file) && is_readable($file)) { |
|
1205 | + if (is_file($file) && is_readable($file)) { |
|
1206 | 1206 | |
1207 | - if (filesize($file)) { |
|
1207 | + if (filesize($file)) { |
|
1208 | 1208 | |
1209 | - if (!($fh = fopen($file, 'r'))) { |
|
1209 | + if (!($fh = fopen($file, 'r'))) { |
|
1210 | 1210 | |
1211 | - $this->_set_error('Cannot open file: ' . realpath($file)); |
|
1212 | - return ''; |
|
1213 | - } |
|
1211 | + $this->_set_error('Cannot open file: ' . realpath($file)); |
|
1212 | + return ''; |
|
1213 | + } |
|
1214 | 1214 | |
1215 | - $file_text .= fread($fh,filesize($file)); |
|
1216 | - fclose($fh); |
|
1215 | + $file_text .= fread($fh,filesize($file)); |
|
1216 | + fclose($fh); |
|
1217 | 1217 | |
1218 | - } |
|
1218 | + } |
|
1219 | 1219 | |
1220 | - if ($this->debug) { |
|
1221 | - $file_text = '<!-- XTemplate debug: ' . realpath($file) . ' -->' . "\n" . $file_text; |
|
1222 | - } |
|
1220 | + if ($this->debug) { |
|
1221 | + $file_text = '<!-- XTemplate debug: ' . realpath($file) . ' -->' . "\n" . $file_text; |
|
1222 | + } |
|
1223 | 1223 | |
1224 | - } elseif (str_replace('.', '', phpversion()) >= '430' && $file_text = @file_get_contents($file, true)) { |
|
1225 | - // Enable use of include path by using file_get_contents |
|
1226 | - // Implemented at suggestion of SF Feature Request ID #1529478 michaelgroh |
|
1227 | - if ($file_text === false) { |
|
1228 | - $this->_set_error("[" . realpath($file) . "] ($file) does not exist"); |
|
1229 | - $file_text = "<b>__XTemplate fatal error: file [$file] does not exist in the include path__</b>"; |
|
1230 | - } elseif ($this->debug) { |
|
1231 | - $file_text = '<!-- XTemplate debug: ' . realpath($file) . ' (via include path) -->' . "\n" . $file_text; |
|
1232 | - } |
|
1233 | - } elseif (!is_file($file)) { |
|
1224 | + } elseif (str_replace('.', '', phpversion()) >= '430' && $file_text = @file_get_contents($file, true)) { |
|
1225 | + // Enable use of include path by using file_get_contents |
|
1226 | + // Implemented at suggestion of SF Feature Request ID #1529478 michaelgroh |
|
1227 | + if ($file_text === false) { |
|
1228 | + $this->_set_error("[" . realpath($file) . "] ($file) does not exist"); |
|
1229 | + $file_text = "<b>__XTemplate fatal error: file [$file] does not exist in the include path__</b>"; |
|
1230 | + } elseif ($this->debug) { |
|
1231 | + $file_text = '<!-- XTemplate debug: ' . realpath($file) . ' (via include path) -->' . "\n" . $file_text; |
|
1232 | + } |
|
1233 | + } elseif (!is_file($file)) { |
|
1234 | 1234 | |
1235 | - // NW 17 Oct 2002 : Added realpath around the file name to identify where the code is searching. |
|
1236 | - $this->_set_error("[" . realpath($file) . "] ($file) does not exist"); |
|
1237 | - $file_text .= "<b>__XTemplate fatal error: file [$file] does not exist__</b>"; |
|
1235 | + // NW 17 Oct 2002 : Added realpath around the file name to identify where the code is searching. |
|
1236 | + $this->_set_error("[" . realpath($file) . "] ($file) does not exist"); |
|
1237 | + $file_text .= "<b>__XTemplate fatal error: file [$file] does not exist__</b>"; |
|
1238 | 1238 | |
1239 | - } elseif (!is_readable($file)) { |
|
1239 | + } elseif (!is_readable($file)) { |
|
1240 | 1240 | |
1241 | - $this->_set_error("[" . realpath($file) . "] ($file) is not readable"); |
|
1242 | - $file_text .= "<b>__XTemplate fatal error: file [$file] is not readable__</b>"; |
|
1243 | - } |
|
1241 | + $this->_set_error("[" . realpath($file) . "] ($file) is not readable"); |
|
1242 | + $file_text .= "<b>__XTemplate fatal error: file [$file] is not readable__</b>"; |
|
1243 | + } |
|
1244 | 1244 | |
1245 | - $this->filecache[$file] = $file_text; |
|
1246 | - } |
|
1245 | + $this->filecache[$file] = $file_text; |
|
1246 | + } |
|
1247 | 1247 | |
1248 | - return $file_text; |
|
1249 | - } |
|
1248 | + return $file_text; |
|
1249 | + } |
|
1250 | 1250 | |
1251 | - /** |
|
1251 | + /** |
|
1252 | 1252 | * recursively gets the content of a file with {FILE "filename.tpl"} directives |
1253 | 1253 | * |
1254 | 1254 | * @access public - aiming for private |
1255 | 1255 | * @param string $file |
1256 | 1256 | * @return string |
1257 | 1257 | */ |
1258 | - public function _r_getfile ($file) { |
|
1258 | + public function _r_getfile ($file) { |
|
1259 | 1259 | |
1260 | - $text = $this->_getfile($file); |
|
1260 | + $text = $this->_getfile($file); |
|
1261 | 1261 | |
1262 | - $res = array(); |
|
1262 | + $res = array(); |
|
1263 | 1263 | |
1264 | - while (preg_match($this->file_delim,$text,$res)) { |
|
1264 | + while (preg_match($this->file_delim,$text,$res)) { |
|
1265 | 1265 | |
1266 | - $text2 = $this->_getfile($res[1]); |
|
1267 | - $text = preg_replace("'".preg_quote($res[0])."'",$text2,$text); |
|
1268 | - } |
|
1266 | + $text2 = $this->_getfile($res[1]); |
|
1267 | + $text = preg_replace("'".preg_quote($res[0])."'",$text2,$text); |
|
1268 | + } |
|
1269 | 1269 | |
1270 | - return $text; |
|
1271 | - } |
|
1270 | + return $text; |
|
1271 | + } |
|
1272 | 1272 | |
1273 | 1273 | |
1274 | - /** |
|
1274 | + /** |
|
1275 | 1275 | * add an outer block delimiter set useful for rtfs etc - keeps them editable in word |
1276 | 1276 | * |
1277 | 1277 | * @access private |
1278 | 1278 | */ |
1279 | - private function _add_outer_block () { |
|
1279 | + private function _add_outer_block () { |
|
1280 | 1280 | |
1281 | - $before = $this->block_start_delim . $this->block_start_word . ' ' . $this->mainblock . ' ' . $this->block_end_delim; |
|
1282 | - $after = $this->block_start_delim . $this->block_end_word . ' ' . $this->mainblock . ' ' . $this->block_end_delim; |
|
1281 | + $before = $this->block_start_delim . $this->block_start_word . ' ' . $this->mainblock . ' ' . $this->block_end_delim; |
|
1282 | + $after = $this->block_start_delim . $this->block_end_word . ' ' . $this->mainblock . ' ' . $this->block_end_delim; |
|
1283 | 1283 | |
1284 | - $this->filecontents = $before . "\n" . $this->filecontents . "\n" . $after; |
|
1285 | - } |
|
1284 | + $this->filecontents = $before . "\n" . $this->filecontents . "\n" . $after; |
|
1285 | + } |
|
1286 | 1286 | |
1287 | - /** |
|
1287 | + /** |
|
1288 | 1288 | * Debug function - var_dump wrapped in '<pre></pre>' tags |
1289 | 1289 | * |
1290 | 1290 | * @access private |
1291 | 1291 | * @param multiple var_dumps all the supplied arguments |
1292 | 1292 | */ |
1293 | - private function _pre_var_dump ($args) { |
|
1294 | - |
|
1295 | - if ($this->debug) { |
|
1296 | - echo '<pre>'; |
|
1297 | - var_dump(func_get_args()); |
|
1298 | - echo '</pre>'; |
|
1299 | - } |
|
1300 | - } |
|
1293 | + private function _pre_var_dump ($args) { |
|
1294 | + |
|
1295 | + if ($this->debug) { |
|
1296 | + echo '<pre>'; |
|
1297 | + var_dump(func_get_args()); |
|
1298 | + echo '</pre>'; |
|
1299 | + } |
|
1300 | + } |
|
1301 | 1301 | } /* end of XTemplate class. */ |
1302 | 1302 | |
1303 | 1303 | ?> |
1304 | 1304 | \ No newline at end of file |
@@ -27,56 +27,56 @@ |
||
27 | 27 | |
28 | 28 | class Version { |
29 | 29 | |
30 | - protected static $instance = null; |
|
30 | + protected static $instance = null; |
|
31 | 31 | |
32 | - protected $version_data = array(); |
|
32 | + protected $version_data = array(); |
|
33 | 33 | |
34 | - public function __construct() { |
|
35 | - // |
|
36 | - } |
|
34 | + public function __construct() { |
|
35 | + // |
|
36 | + } |
|
37 | 37 | |
38 | - public function load ($version_path) { |
|
39 | - //because unserialize is 2 times faster than json_decode, we cache this value |
|
40 | - if (Cache::contains("version", "version_" . $version_path)) { |
|
41 | - $this->version_data = Cache::get("version", "version_" . $version_path); |
|
42 | - } else { |
|
43 | - if (!file_exists($version_path)) { |
|
44 | - echo "Version file doesnt exists: " . $version_path; |
|
45 | - exit; |
|
46 | - } |
|
38 | + public function load ($version_path) { |
|
39 | + //because unserialize is 2 times faster than json_decode, we cache this value |
|
40 | + if (Cache::contains("version", "version_" . $version_path)) { |
|
41 | + $this->version_data = Cache::get("version", "version_" . $version_path); |
|
42 | + } else { |
|
43 | + if (!file_exists($version_path)) { |
|
44 | + echo "Version file doesnt exists: " . $version_path; |
|
45 | + exit; |
|
46 | + } |
|
47 | 47 | |
48 | - $array = json_decode(file_get_contents($version_path), true); |
|
48 | + $array = json_decode(file_get_contents($version_path), true); |
|
49 | 49 | |
50 | - //cache |
|
51 | - Cache::put("version", "version_" . $version_path, $array); |
|
50 | + //cache |
|
51 | + Cache::put("version", "version_" . $version_path, $array); |
|
52 | 52 | |
53 | - $this->version_data = $array; |
|
54 | - } |
|
55 | - } |
|
53 | + $this->version_data = $array; |
|
54 | + } |
|
55 | + } |
|
56 | 56 | |
57 | - public function getVersion () : string { |
|
58 | - if (!isset($this->version_data['version'])) { |
|
59 | - var_dump($this->version_data); |
|
57 | + public function getVersion () : string { |
|
58 | + if (!isset($this->version_data['version'])) { |
|
59 | + var_dump($this->version_data); |
|
60 | 60 | |
61 | - echo "Version not found!"; |
|
62 | - exit; |
|
63 | - } |
|
61 | + echo "Version not found!"; |
|
62 | + exit; |
|
63 | + } |
|
64 | 64 | |
65 | - return $this->version_data['version']; |
|
66 | - } |
|
65 | + return $this->version_data['version']; |
|
66 | + } |
|
67 | 67 | |
68 | - public function getBuildNumber () : string { |
|
69 | - return $this->version_data['build']; |
|
70 | - } |
|
68 | + public function getBuildNumber () : string { |
|
69 | + return $this->version_data['build']; |
|
70 | + } |
|
71 | 71 | |
72 | - public static function ¤t () : Version { |
|
73 | - if (self::$instance == null) { |
|
74 | - self::$instance = new Version(); |
|
75 | - self::$instance->load(ROOT_PATH . "system/core/version.json"); |
|
76 | - } |
|
72 | + public static function ¤t () : Version { |
|
73 | + if (self::$instance == null) { |
|
74 | + self::$instance = new Version(); |
|
75 | + self::$instance->load(ROOT_PATH . "system/core/version.json"); |
|
76 | + } |
|
77 | 77 | |
78 | - return self::$instance; |
|
79 | - } |
|
78 | + return self::$instance; |
|
79 | + } |
|
80 | 80 | |
81 | 81 | } |
82 | 82 |
@@ -27,7 +27,7 @@ |
||
27 | 27 | |
28 | 28 | class Admin_Dashboard extends PageType { |
29 | 29 | |
30 | - // |
|
30 | + // |
|
31 | 31 | |
32 | 32 | } |
33 | 33 |
@@ -18,99 +18,99 @@ |
||
18 | 18 | |
19 | 19 | class Robots { |
20 | 20 | |
21 | - //https://developers.google.com/search/reference/robots_txt?hl=de |
|
21 | + //https://developers.google.com/search/reference/robots_txt?hl=de |
|
22 | 22 | |
23 | - protected $robots = array(); |
|
23 | + protected $robots = array(); |
|
24 | 24 | |
25 | - public function __construct () { |
|
26 | - // |
|
27 | - } |
|
25 | + public function __construct () { |
|
26 | + // |
|
27 | + } |
|
28 | 28 | |
29 | - public function loadFromDB () { |
|
30 | - $rows = DataBase::getInstance()->listRows("SELECT * FROM `{prefix}robots` WHERE `activated` = '1'; "); |
|
31 | - $this->robots = $rows; |
|
32 | - } |
|
29 | + public function loadFromDB () { |
|
30 | + $rows = DataBase::getInstance()->listRows("SELECT * FROM `{prefix}robots` WHERE `activated` = '1'; "); |
|
31 | + $this->robots = $rows; |
|
32 | + } |
|
33 | 33 | |
34 | - public function sortByUserAgent () { |
|
35 | - $array = array(); |
|
34 | + public function sortByUserAgent () { |
|
35 | + $array = array(); |
|
36 | 36 | |
37 | - foreach ($this->robots as $row) { |
|
38 | - if (!isset($row['useragent'])) { |
|
39 | - $array[$row['useragent']] = array(); |
|
40 | - } |
|
37 | + foreach ($this->robots as $row) { |
|
38 | + if (!isset($row['useragent'])) { |
|
39 | + $array[$row['useragent']] = array(); |
|
40 | + } |
|
41 | 41 | |
42 | - $array[$row['useragent']][] = $row; |
|
43 | - } |
|
42 | + $array[$row['useragent']][] = $row; |
|
43 | + } |
|
44 | 44 | |
45 | - return $array; |
|
46 | - } |
|
45 | + return $array; |
|
46 | + } |
|
47 | 47 | |
48 | - public function writeFile () { |
|
49 | - $array = $this->sortByUserAgent(); |
|
48 | + public function writeFile () { |
|
49 | + $array = $this->sortByUserAgent(); |
|
50 | 50 | |
51 | - if (!is_writable(ROOT_PATH . "robots.txt")) { |
|
52 | - throw new IllegalStateException(ROOT_PATH . "robots.txt is not writable, please correct file permissions!"); |
|
53 | - } |
|
51 | + if (!is_writable(ROOT_PATH . "robots.txt")) { |
|
52 | + throw new IllegalStateException(ROOT_PATH . "robots.txt is not writable, please correct file permissions!"); |
|
53 | + } |
|
54 | 54 | |
55 | - $handle = fopen(ROOT_PATH . "robots.txt", "w"); |
|
55 | + $handle = fopen(ROOT_PATH . "robots.txt", "w"); |
|
56 | 56 | |
57 | - foreach ($array as $useragent=>$value) { |
|
57 | + foreach ($array as $useragent=>$value) { |
|
58 | 58 | |
59 | - fwrite($handle, "User-agent: " . $useragent . "\r\n"); |
|
59 | + fwrite($handle, "User-agent: " . $useragent . "\r\n"); |
|
60 | 60 | |
61 | - foreach ($value as $line) { |
|
62 | - fwrite($handle, "" . $line['option'] . ": " . $line['value'] . "\r\n"); |
|
63 | - } |
|
61 | + foreach ($value as $line) { |
|
62 | + fwrite($handle, "" . $line['option'] . ": " . $line['value'] . "\r\n"); |
|
63 | + } |
|
64 | 64 | |
65 | - fwrite($handle, "\r\n"); |
|
65 | + fwrite($handle, "\r\n"); |
|
66 | 66 | |
67 | - } |
|
67 | + } |
|
68 | 68 | |
69 | - fclose($handle); |
|
70 | - } |
|
69 | + fclose($handle); |
|
70 | + } |
|
71 | 71 | |
72 | - public function getContent () { |
|
73 | - $array = $this->sortByUserAgent(); |
|
72 | + public function getContent () { |
|
73 | + $array = $this->sortByUserAgent(); |
|
74 | 74 | |
75 | - $buffer = ""; |
|
75 | + $buffer = ""; |
|
76 | 76 | |
77 | - foreach ($array as $useragent=>$value) { |
|
78 | - $buffer .= "User-agent: " . $useragent . "\r\n"; |
|
77 | + foreach ($array as $useragent=>$value) { |
|
78 | + $buffer .= "User-agent: " . $useragent . "\r\n"; |
|
79 | 79 | |
80 | - foreach ($value as $line) { |
|
81 | - $buffer .= "" . $line['option'] . ": " . $line['value'] . "\r\n"; |
|
82 | - } |
|
80 | + foreach ($value as $line) { |
|
81 | + $buffer .= "" . $line['option'] . ": " . $line['value'] . "\r\n"; |
|
82 | + } |
|
83 | 83 | |
84 | - $buffer .= "\r\n"; |
|
85 | - } |
|
84 | + $buffer .= "\r\n"; |
|
85 | + } |
|
86 | 86 | |
87 | - return $buffer; |
|
88 | - } |
|
87 | + return $buffer; |
|
88 | + } |
|
89 | 89 | |
90 | - public static function addRule (string $option, string $value, string $useragent = "*") { |
|
91 | - Database::getInstance()->execute("INSERT INTO `{praefix}robots` ( |
|
90 | + public static function addRule (string $option, string $value, string $useragent = "*") { |
|
91 | + Database::getInstance()->execute("INSERT INTO `{praefix}robots` ( |
|
92 | 92 | `useragent`, `option`, `value`, `activated` |
93 | 93 | ) VALUES ( |
94 | 94 | :useragent, :option, :value, '1' |
95 | 95 | ) ON DUPLICATE KEY UPDATE `option` = :option, `activated` = '1'; ", array( |
96 | - 'useragent' => $useragent, |
|
97 | - 'option' => $option, |
|
98 | - 'value' => $value |
|
99 | - )); |
|
100 | - } |
|
101 | - |
|
102 | - public static function removeRule (string $option, string $value, string $useragent = "*") { |
|
103 | - Database::getInstance()->execute("DELETE FROM `{praefix}robots` WHERE `useragent` = :useragent AND `option` = :option AND `value` = :value; ", array( |
|
104 | - 'useragent' => $useragent, |
|
105 | - 'option' => $option, |
|
106 | - 'value' => $value |
|
107 | - )); |
|
108 | - } |
|
109 | - |
|
110 | - public static function listRules () : array { |
|
111 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}robots`; "); |
|
112 | - return $rows; |
|
113 | - } |
|
96 | + 'useragent' => $useragent, |
|
97 | + 'option' => $option, |
|
98 | + 'value' => $value |
|
99 | + )); |
|
100 | + } |
|
101 | + |
|
102 | + public static function removeRule (string $option, string $value, string $useragent = "*") { |
|
103 | + Database::getInstance()->execute("DELETE FROM `{praefix}robots` WHERE `useragent` = :useragent AND `option` = :option AND `value` = :value; ", array( |
|
104 | + 'useragent' => $useragent, |
|
105 | + 'option' => $option, |
|
106 | + 'value' => $value |
|
107 | + )); |
|
108 | + } |
|
109 | + |
|
110 | + public static function listRules () : array { |
|
111 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}robots`; "); |
|
112 | + return $rows; |
|
113 | + } |
|
114 | 114 | |
115 | 115 | } |
116 | 116 |
@@ -27,27 +27,27 @@ |
||
27 | 27 | |
28 | 28 | class RobotsPage extends PageType { |
29 | 29 | |
30 | - public function getContentType(): string { |
|
31 | - return "text/plain"; |
|
32 | - } |
|
33 | - |
|
34 | - public function showDesign() { |
|
35 | - return false; |
|
36 | - } |
|
37 | - |
|
38 | - public function exitAfterOutput() { |
|
39 | - //dont add additional text (like HTML comments for benchmarks) at end of this content |
|
40 | - return true; |
|
41 | - } |
|
42 | - |
|
43 | - public function getContent(): string { |
|
44 | - //load robots.txt entries from database |
|
45 | - $robots = new Robots(); |
|
46 | - $robots->loadFromDB(); |
|
47 | - |
|
48 | - //get robots.txt content |
|
49 | - return $robots->getContent(); |
|
50 | - } |
|
30 | + public function getContentType(): string { |
|
31 | + return "text/plain"; |
|
32 | + } |
|
33 | + |
|
34 | + public function showDesign() { |
|
35 | + return false; |
|
36 | + } |
|
37 | + |
|
38 | + public function exitAfterOutput() { |
|
39 | + //dont add additional text (like HTML comments for benchmarks) at end of this content |
|
40 | + return true; |
|
41 | + } |
|
42 | + |
|
43 | + public function getContent(): string { |
|
44 | + //load robots.txt entries from database |
|
45 | + $robots = new Robots(); |
|
46 | + $robots->loadFromDB(); |
|
47 | + |
|
48 | + //get robots.txt content |
|
49 | + return $robots->getContent(); |
|
50 | + } |
|
51 | 51 | |
52 | 52 | } |
53 | 53 |
@@ -27,102 +27,102 @@ |
||
27 | 27 | |
28 | 28 | class UserRights { |
29 | 29 | |
30 | - protected $userID = -1; |
|
31 | - |
|
32 | - //array with permissions - value: 0 (no), 1 (yes), 2 (never) |
|
33 | - protected $permissions = array(); |
|
34 | - |
|
35 | - public function __construct(int $userID) { |
|
36 | - $this->userID = $userID; |
|
37 | - |
|
38 | - //load permissions |
|
39 | - $this->load($userID); |
|
40 | - } |
|
41 | - |
|
42 | - protected function load (int $userID) { |
|
43 | - if (Cache::contains("user_permissions", "user_" . $userID)) { |
|
44 | - $this->permissions = Cache::get("user_permissions", "user_" . $userID); |
|
45 | - } else { |
|
46 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}user_rights` WHERE `userID` = :userID; ", array( |
|
47 | - 'userID' => array( |
|
48 | - 'type' => PDO::PARAM_INT, |
|
49 | - 'value' => $userID |
|
50 | - ) |
|
51 | - )); |
|
52 | - |
|
53 | - $this->permissions = array(); |
|
54 | - |
|
55 | - foreach ($rows as $row) { |
|
56 | - $this->permissions[$row['token']] = $row['value']; |
|
57 | - } |
|
58 | - |
|
59 | - //cache result |
|
60 | - Cache::put("user_permissions", "user_user_" . $userID, $this->permissions); |
|
61 | - } |
|
62 | - } |
|
63 | - |
|
64 | - public function setRight (string $token, int $value = 1) { |
|
65 | - //validate token |
|
66 | - $token = Validator_Token::get($token); |
|
67 | - |
|
68 | - if ($value < 0 || $value > 2) { |
|
69 | - throw new IllegalArgumentException("token value '" . $value . "' is not allowed, value has to be >= 0 and <= 2."); |
|
70 | - } |
|
71 | - |
|
72 | - //update database |
|
73 | - Database::getInstance()->execute("INSERT INTO `{praefix}user_rights` ( |
|
30 | + protected $userID = -1; |
|
31 | + |
|
32 | + //array with permissions - value: 0 (no), 1 (yes), 2 (never) |
|
33 | + protected $permissions = array(); |
|
34 | + |
|
35 | + public function __construct(int $userID) { |
|
36 | + $this->userID = $userID; |
|
37 | + |
|
38 | + //load permissions |
|
39 | + $this->load($userID); |
|
40 | + } |
|
41 | + |
|
42 | + protected function load (int $userID) { |
|
43 | + if (Cache::contains("user_permissions", "user_" . $userID)) { |
|
44 | + $this->permissions = Cache::get("user_permissions", "user_" . $userID); |
|
45 | + } else { |
|
46 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}user_rights` WHERE `userID` = :userID; ", array( |
|
47 | + 'userID' => array( |
|
48 | + 'type' => PDO::PARAM_INT, |
|
49 | + 'value' => $userID |
|
50 | + ) |
|
51 | + )); |
|
52 | + |
|
53 | + $this->permissions = array(); |
|
54 | + |
|
55 | + foreach ($rows as $row) { |
|
56 | + $this->permissions[$row['token']] = $row['value']; |
|
57 | + } |
|
58 | + |
|
59 | + //cache result |
|
60 | + Cache::put("user_permissions", "user_user_" . $userID, $this->permissions); |
|
61 | + } |
|
62 | + } |
|
63 | + |
|
64 | + public function setRight (string $token, int $value = 1) { |
|
65 | + //validate token |
|
66 | + $token = Validator_Token::get($token); |
|
67 | + |
|
68 | + if ($value < 0 || $value > 2) { |
|
69 | + throw new IllegalArgumentException("token value '" . $value . "' is not allowed, value has to be >= 0 and <= 2."); |
|
70 | + } |
|
71 | + |
|
72 | + //update database |
|
73 | + Database::getInstance()->execute("INSERT INTO `{praefix}user_rights` ( |
|
74 | 74 | `userID`, `token`, `value` |
75 | 75 | ) VALUES ( |
76 | 76 | :userID, :token, :value |
77 | 77 | ) ON DUPLICATE KEY UPDATE `value` = :value; ", array( |
78 | - 'userID' => array( |
|
79 | - 'type' => PDO::PARAM_INT, |
|
80 | - 'value' => $this->userID |
|
81 | - ), |
|
82 | - 'token' => $token, |
|
83 | - 'value' => array( |
|
84 | - 'type' => PDO::PARAM_INT, |
|
85 | - 'value' => $value |
|
86 | - ) |
|
87 | - )); |
|
88 | - |
|
89 | - $this->permissions[$token] = $value; |
|
90 | - |
|
91 | - //clear cache |
|
92 | - Cache::clear("user_permissions", "user_" . $this->userID); |
|
93 | - Cache::clear("permissions", "permissions_user_" . $this->userID); |
|
94 | - } |
|
95 | - |
|
96 | - public function removeRight (string $token) { |
|
97 | - //validate token |
|
98 | - $token = Validator_Token::get($token); |
|
99 | - |
|
100 | - //delete from database |
|
101 | - Database::getInstance()->execute("DELETE FROM `{praefix}user_rights` WHERE `userID` = :userID AND `token` = :token; ", array( |
|
102 | - 'userID' => array( |
|
103 | - 'type' => PDO::PARAM_INT, |
|
104 | - 'value' => $this->userID |
|
105 | - ), |
|
106 | - 'token' => $token |
|
107 | - )); |
|
108 | - |
|
109 | - //delete from in-memory cache |
|
110 | - if (isset($this->permissions[$token])) { |
|
111 | - unset($this->permissions[$token]); |
|
112 | - } |
|
113 | - |
|
114 | - //clear cache |
|
115 | - Cache::clear("user_permissions", "user_" . $this->userID); |
|
116 | - Cache::clear("permissions", "permissions_user_" . $this->userID); |
|
117 | - } |
|
118 | - |
|
119 | - public function listRights () : array { |
|
120 | - return $this->permissions; |
|
121 | - } |
|
122 | - |
|
123 | - public function hasRight (string $token) { |
|
124 | - return isset($this->permissions[$token]) && $this->permissions[$token] == 1; |
|
125 | - } |
|
78 | + 'userID' => array( |
|
79 | + 'type' => PDO::PARAM_INT, |
|
80 | + 'value' => $this->userID |
|
81 | + ), |
|
82 | + 'token' => $token, |
|
83 | + 'value' => array( |
|
84 | + 'type' => PDO::PARAM_INT, |
|
85 | + 'value' => $value |
|
86 | + ) |
|
87 | + )); |
|
88 | + |
|
89 | + $this->permissions[$token] = $value; |
|
90 | + |
|
91 | + //clear cache |
|
92 | + Cache::clear("user_permissions", "user_" . $this->userID); |
|
93 | + Cache::clear("permissions", "permissions_user_" . $this->userID); |
|
94 | + } |
|
95 | + |
|
96 | + public function removeRight (string $token) { |
|
97 | + //validate token |
|
98 | + $token = Validator_Token::get($token); |
|
99 | + |
|
100 | + //delete from database |
|
101 | + Database::getInstance()->execute("DELETE FROM `{praefix}user_rights` WHERE `userID` = :userID AND `token` = :token; ", array( |
|
102 | + 'userID' => array( |
|
103 | + 'type' => PDO::PARAM_INT, |
|
104 | + 'value' => $this->userID |
|
105 | + ), |
|
106 | + 'token' => $token |
|
107 | + )); |
|
108 | + |
|
109 | + //delete from in-memory cache |
|
110 | + if (isset($this->permissions[$token])) { |
|
111 | + unset($this->permissions[$token]); |
|
112 | + } |
|
113 | + |
|
114 | + //clear cache |
|
115 | + Cache::clear("user_permissions", "user_" . $this->userID); |
|
116 | + Cache::clear("permissions", "permissions_user_" . $this->userID); |
|
117 | + } |
|
118 | + |
|
119 | + public function listRights () : array { |
|
120 | + return $this->permissions; |
|
121 | + } |
|
122 | + |
|
123 | + public function hasRight (string $token) { |
|
124 | + return isset($this->permissions[$token]) && $this->permissions[$token] == 1; |
|
125 | + } |
|
126 | 126 | |
127 | 127 | } |
128 | 128 |
@@ -27,102 +27,102 @@ |
||
27 | 27 | |
28 | 28 | class GroupRights { |
29 | 29 | |
30 | - protected $groupID = -1; |
|
31 | - |
|
32 | - //array with permissions - value: 0 (no), 1 (yes), 2 (never) |
|
33 | - protected $permissions = array(); |
|
34 | - |
|
35 | - public function __construct(int $groupID) { |
|
36 | - $this->groupID = $groupID; |
|
37 | - |
|
38 | - //load permissions |
|
39 | - $this->load($groupID); |
|
40 | - } |
|
41 | - |
|
42 | - protected function load (int $groupID) { |
|
43 | - if (Cache::contains("group_permissions", "group_" . $groupID)) { |
|
44 | - $this->permissions = Cache::get("group_permissions", "group_" . $groupID); |
|
45 | - } else { |
|
46 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}group_rights` WHERE `groupID` = :groupID; ", array( |
|
47 | - 'groupID' => array( |
|
48 | - 'type' => PDO::PARAM_INT, |
|
49 | - 'value' => $groupID |
|
50 | - ) |
|
51 | - )); |
|
52 | - |
|
53 | - $this->permissions = array(); |
|
54 | - |
|
55 | - foreach ($rows as $row) { |
|
56 | - $this->permissions[$row['token']] = $row['value']; |
|
57 | - } |
|
58 | - |
|
59 | - //cache result |
|
60 | - Cache::put("group_permissions", "group_" . $groupID, $this->permissions); |
|
61 | - } |
|
62 | - } |
|
63 | - |
|
64 | - public function setRight (string $token, int $value = 1) { |
|
65 | - //validate token |
|
66 | - $token = Validator_Token::get($token); |
|
67 | - |
|
68 | - if ($value < 0 || $value > 2) { |
|
69 | - throw new IllegalArgumentException("token value '" . $value . "' is not allowed, value has to be >= 0 and <= 2."); |
|
70 | - } |
|
71 | - |
|
72 | - //update database |
|
73 | - Database::getInstance()->execute("INSERT INTO `{praefix}group_rights` ( |
|
30 | + protected $groupID = -1; |
|
31 | + |
|
32 | + //array with permissions - value: 0 (no), 1 (yes), 2 (never) |
|
33 | + protected $permissions = array(); |
|
34 | + |
|
35 | + public function __construct(int $groupID) { |
|
36 | + $this->groupID = $groupID; |
|
37 | + |
|
38 | + //load permissions |
|
39 | + $this->load($groupID); |
|
40 | + } |
|
41 | + |
|
42 | + protected function load (int $groupID) { |
|
43 | + if (Cache::contains("group_permissions", "group_" . $groupID)) { |
|
44 | + $this->permissions = Cache::get("group_permissions", "group_" . $groupID); |
|
45 | + } else { |
|
46 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}group_rights` WHERE `groupID` = :groupID; ", array( |
|
47 | + 'groupID' => array( |
|
48 | + 'type' => PDO::PARAM_INT, |
|
49 | + 'value' => $groupID |
|
50 | + ) |
|
51 | + )); |
|
52 | + |
|
53 | + $this->permissions = array(); |
|
54 | + |
|
55 | + foreach ($rows as $row) { |
|
56 | + $this->permissions[$row['token']] = $row['value']; |
|
57 | + } |
|
58 | + |
|
59 | + //cache result |
|
60 | + Cache::put("group_permissions", "group_" . $groupID, $this->permissions); |
|
61 | + } |
|
62 | + } |
|
63 | + |
|
64 | + public function setRight (string $token, int $value = 1) { |
|
65 | + //validate token |
|
66 | + $token = Validator_Token::get($token); |
|
67 | + |
|
68 | + if ($value < 0 || $value > 2) { |
|
69 | + throw new IllegalArgumentException("token value '" . $value . "' is not allowed, value has to be >= 0 and <= 2."); |
|
70 | + } |
|
71 | + |
|
72 | + //update database |
|
73 | + Database::getInstance()->execute("INSERT INTO `{praefix}group_rights` ( |
|
74 | 74 | `groupID`, `token`, `value` |
75 | 75 | ) VALUES ( |
76 | 76 | :groupID, :token, :value |
77 | 77 | ) ON DUPLICATE KEY UPDATE `value` = :value; ", array( |
78 | - 'groupID' => array( |
|
79 | - 'type' => PDO::PARAM_INT, |
|
80 | - 'value' => $this->groupID |
|
81 | - ), |
|
82 | - 'token' => $token, |
|
83 | - 'value' => array( |
|
84 | - 'type' => PDO::PARAM_INT, |
|
85 | - 'value' => $value |
|
86 | - ) |
|
87 | - )); |
|
88 | - |
|
89 | - $this->permissions[$token] = $value; |
|
90 | - |
|
91 | - //clear cache |
|
92 | - Cache::clear("group_permissions", "group_" . $this->groupID); |
|
93 | - Cache::clear("permissions"); |
|
94 | - } |
|
95 | - |
|
96 | - public function removeRight (string $token) { |
|
97 | - //validate token |
|
98 | - $token = Validator_Token::get($token); |
|
99 | - |
|
100 | - //delete from database |
|
101 | - Database::getInstance()->execute("DELETE FROM `{praefix}group_rights` WHERE `groupID` = :groupID AND `token` = :token; ", array( |
|
102 | - 'groupID' => array( |
|
103 | - 'type' => PDO::PARAM_INT, |
|
104 | - 'value' => $this->groupID |
|
105 | - ), |
|
106 | - 'token' => $token |
|
107 | - )); |
|
108 | - |
|
109 | - //delete from in-memory cache |
|
110 | - if (isset($this->permissions[$token])) { |
|
111 | - unset($this->permissions[$token]); |
|
112 | - } |
|
113 | - |
|
114 | - //clear cache |
|
115 | - Cache::clear("group_permissions", "group_" . $this->groupID); |
|
116 | - Cache::clear("permissions"); |
|
117 | - } |
|
118 | - |
|
119 | - public function listRights () : array { |
|
120 | - return $this->permissions; |
|
121 | - } |
|
122 | - |
|
123 | - public function hasRight (string $token) { |
|
124 | - return isset($this->permissions[$token]) && $this->permissions[$token] == 1; |
|
125 | - } |
|
78 | + 'groupID' => array( |
|
79 | + 'type' => PDO::PARAM_INT, |
|
80 | + 'value' => $this->groupID |
|
81 | + ), |
|
82 | + 'token' => $token, |
|
83 | + 'value' => array( |
|
84 | + 'type' => PDO::PARAM_INT, |
|
85 | + 'value' => $value |
|
86 | + ) |
|
87 | + )); |
|
88 | + |
|
89 | + $this->permissions[$token] = $value; |
|
90 | + |
|
91 | + //clear cache |
|
92 | + Cache::clear("group_permissions", "group_" . $this->groupID); |
|
93 | + Cache::clear("permissions"); |
|
94 | + } |
|
95 | + |
|
96 | + public function removeRight (string $token) { |
|
97 | + //validate token |
|
98 | + $token = Validator_Token::get($token); |
|
99 | + |
|
100 | + //delete from database |
|
101 | + Database::getInstance()->execute("DELETE FROM `{praefix}group_rights` WHERE `groupID` = :groupID AND `token` = :token; ", array( |
|
102 | + 'groupID' => array( |
|
103 | + 'type' => PDO::PARAM_INT, |
|
104 | + 'value' => $this->groupID |
|
105 | + ), |
|
106 | + 'token' => $token |
|
107 | + )); |
|
108 | + |
|
109 | + //delete from in-memory cache |
|
110 | + if (isset($this->permissions[$token])) { |
|
111 | + unset($this->permissions[$token]); |
|
112 | + } |
|
113 | + |
|
114 | + //clear cache |
|
115 | + Cache::clear("group_permissions", "group_" . $this->groupID); |
|
116 | + Cache::clear("permissions"); |
|
117 | + } |
|
118 | + |
|
119 | + public function listRights () : array { |
|
120 | + return $this->permissions; |
|
121 | + } |
|
122 | + |
|
123 | + public function hasRight (string $token) { |
|
124 | + return isset($this->permissions[$token]) && $this->permissions[$token] == 1; |
|
125 | + } |
|
126 | 126 | |
127 | 127 | } |
128 | 128 |
@@ -57,1006 +57,1006 @@ |
||
57 | 57 | |
58 | 58 | class JSqueeze |
59 | 59 | { |
60 | - const |
|
61 | - |
|
62 | - SPECIAL_VAR_PACKER = '(\$+[a-zA-Z_]|_[a-zA-Z0-9$])[a-zA-Z0-9_$]*'; |
|
63 | - |
|
64 | - public |
|
65 | - |
|
66 | - $charFreq; |
|
67 | - |
|
68 | - protected |
|
69 | - |
|
70 | - $strings, |
|
71 | - $closures, |
|
72 | - $str0, |
|
73 | - $str1, |
|
74 | - $argFreq, |
|
75 | - $specialVarRx, |
|
76 | - $keepImportantComments, |
|
77 | - |
|
78 | - $varRx = '(?:[a-zA-Z_$])[a-zA-Z0-9_$]*', |
|
79 | - $reserved = array( |
|
80 | - // Literals |
|
81 | - 'true','false','null', |
|
82 | - // ES6 |
|
83 | - 'break','case','class','catch','const','continue','debugger','default','delete','do','else','export','extends','finally','for','function','if','import','in','instanceof','new','return','super','switch','this','throw','try','typeof','var','void','while','with','yield', |
|
84 | - // Future |
|
85 | - 'enum', |
|
86 | - // Strict mode |
|
87 | - 'implements','package','protected','static','let','interface','private','public', |
|
88 | - // Module |
|
89 | - 'await', |
|
90 | - // Older standards |
|
91 | - 'abstract','boolean','byte','char','double','final','float','goto','int','long','native','short','synchronized','throws','transient','volatile', |
|
92 | - ); |
|
93 | - |
|
94 | - public function __construct() |
|
95 | - { |
|
96 | - $this->reserved = array_flip($this->reserved); |
|
97 | - $this->charFreq = array_fill(0, 256, 0); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Squeezes a JavaScript source code. |
|
102 | - * |
|
103 | - * Set $singleLine to false if you want optional |
|
104 | - * semi-colons to be replaced by line feeds. |
|
105 | - * |
|
106 | - * Set $keepImportantComments to false if you want /*! comments to be removed. |
|
107 | - * |
|
108 | - * $specialVarRx defines the regular expression of special variables names |
|
109 | - * for global vars, methods, properties and in string substitution. |
|
110 | - * Set it to false if you don't want any. |
|
111 | - * |
|
112 | - * If the analysed javascript source contains a single line comment like |
|
113 | - * this one, then the directive will overwrite $specialVarRx: |
|
114 | - * |
|
115 | - * // jsqueeze.specialVarRx = your_special_var_regexp_here |
|
116 | - * |
|
117 | - * Only the first directive is parsed, others are ignored. It is not possible |
|
118 | - * to redefine $specialVarRx in the middle of the javascript source. |
|
119 | - * |
|
120 | - * Example: |
|
121 | - * $parser = new JSqueeze; |
|
122 | - * $squeezed_js = $parser->squeeze($fat_js); |
|
123 | - */ |
|
124 | - public function squeeze($code, $singleLine = true, $keepImportantComments = true, $specialVarRx = false) |
|
125 | - { |
|
126 | - $code = trim($code); |
|
127 | - if ('' === $code) { |
|
128 | - return ''; |
|
129 | - } |
|
130 | - |
|
131 | - $this->argFreq = array(-1 => 0); |
|
132 | - $this->specialVarRx = $specialVarRx; |
|
133 | - $this->keepImportantComments = !!$keepImportantComments; |
|
134 | - |
|
135 | - if (preg_match("#//[ \t]*jsqueeze\.specialVarRx[ \t]*=[ \t]*([\"']?)(.*)\\1#i", $code, $key)) { |
|
136 | - if (!$key[1]) { |
|
137 | - $key[2] = trim($key[2]); |
|
138 | - $key[1] = strtolower($key[2]); |
|
139 | - $key[1] = $key[1] && $key[1] != 'false' && $key[1] != 'none' && $key[1] != 'off'; |
|
140 | - } |
|
141 | - |
|
142 | - $this->specialVarRx = $key[1] ? $key[2] : false; |
|
143 | - } |
|
144 | - |
|
145 | - // Remove capturing parentheses |
|
146 | - $this->specialVarRx && $this->specialVarRx = preg_replace('/(?<!\\\\)((?:\\\\\\\\)*)\((?!\?)/', '(?:', $this->specialVarRx); |
|
147 | - |
|
148 | - false !== strpos($code, "\r") && $code = strtr(str_replace("\r\n", "\n", $code), "\r", "\n"); |
|
149 | - false !== strpos($code, "\xC2\x85") && $code = str_replace("\xC2\x85", "\n", $code); // Next Line |
|
150 | - false !== strpos($code, "\xE2\x80\xA8") && $code = str_replace("\xE2\x80\xA8", "\n", $code); // Line Separator |
|
151 | - false !== strpos($code, "\xE2\x80\xA9") && $code = str_replace("\xE2\x80\xA9", "\n", $code); // Paragraph Separator |
|
152 | - |
|
153 | - list($code, $this->strings) = $this->extractStrings($code); |
|
154 | - list($code, $this->closures) = $this->extractClosures($code); |
|
155 | - |
|
156 | - $key = "//''\"\"#0'"; // This crap has a wonderful property: it can not happen in any valid javascript, even in strings |
|
157 | - $this->closures[$key] = &$code; |
|
158 | - |
|
159 | - $tree = array($key => array('parent' => false)); |
|
160 | - $this->makeVars($code, $tree[$key], $key); |
|
161 | - $this->renameVars($tree[$key], true); |
|
162 | - |
|
163 | - $code = substr($tree[$key]['code'], 1); |
|
164 | - $code = preg_replace("'\breturn !'", 'return!', $code); |
|
165 | - $code = preg_replace("'\}(?=(else|while)[^\$.a-zA-Z0-9_])'", "}\r", $code); |
|
166 | - $code = str_replace(array_keys($this->strings), array_values($this->strings), $code); |
|
167 | - |
|
168 | - if ($singleLine) { |
|
169 | - $code = strtr($code, "\n", ';'); |
|
170 | - } else { |
|
171 | - $code = str_replace("\n", ";\n", $code); |
|
172 | - } |
|
173 | - false !== strpos($code, "\r") && $code = strtr(trim($code), "\r", "\n"); |
|
174 | - |
|
175 | - // Cleanup memory |
|
176 | - $this->charFreq = array_fill(0, 256, 0); |
|
177 | - $this->strings = $this->closures = $this->argFreq = array(); |
|
178 | - $this->str0 = $this->str1 = ''; |
|
179 | - |
|
180 | - return $code; |
|
181 | - } |
|
182 | - |
|
183 | - protected function extractStrings($f) |
|
184 | - { |
|
185 | - if ($cc_on = false !== strpos($f, '@cc_on')) { |
|
186 | - // Protect conditional comments from being removed |
|
187 | - $f = str_replace('#', '##', $f); |
|
188 | - $f = str_replace('/*@', '1#@', $f); |
|
189 | - $f = preg_replace("'//@([^\n]+)'", '2#@$1@#3', $f); |
|
190 | - $f = str_replace('@*/', '@#1', $f); |
|
191 | - } |
|
192 | - |
|
193 | - $len = strlen($f); |
|
194 | - $code = str_repeat(' ', $len); |
|
195 | - $j = 0; |
|
196 | - |
|
197 | - $strings = array(); |
|
198 | - $K = 0; |
|
199 | - |
|
200 | - $instr = false; |
|
201 | - |
|
202 | - $q = array( |
|
203 | - "'", '"', |
|
204 | - "'" => 0, |
|
205 | - '"' => 0, |
|
206 | - ); |
|
207 | - |
|
208 | - // Extract strings, removes comments |
|
209 | - for ($i = 0; $i < $len; ++$i) { |
|
210 | - if ($instr) { |
|
211 | - if ('//' == $instr) { |
|
212 | - if ("\n" == $f[$i]) { |
|
213 | - $f[$i--] = ' '; |
|
214 | - $instr = false; |
|
215 | - } |
|
216 | - } elseif ($f[$i] == $instr || ('/' == $f[$i] && "/'" == $instr)) { |
|
217 | - if ('!' == $instr) { |
|
218 | - } elseif ('*' == $instr) { |
|
219 | - if ('/' == $f[$i + 1]) { |
|
220 | - ++$i; |
|
221 | - $instr = false; |
|
222 | - } |
|
223 | - } else { |
|
224 | - if ("/'" == $instr) { |
|
225 | - while (isset($f[$i + 1]) && false !== strpos('gmi', $f[$i + 1])) { |
|
226 | - $s[] = $f[$i++]; |
|
227 | - } |
|
228 | - $s[] = $f[$i]; |
|
229 | - } |
|
230 | - |
|
231 | - $instr = false; |
|
232 | - } |
|
233 | - } elseif ('*' == $instr) { |
|
234 | - } elseif ('!' == $instr) { |
|
235 | - if ('*' == $f[$i] && '/' == $f[$i + 1]) { |
|
236 | - $s[] = "*/\r"; |
|
237 | - ++$i; |
|
238 | - $instr = false; |
|
239 | - } elseif ("\n" == $f[$i]) { |
|
240 | - $s[] = "\r"; |
|
241 | - } else { |
|
242 | - $s[] = $f[$i]; |
|
243 | - } |
|
244 | - } elseif ('\\' == $f[$i]) { |
|
245 | - ++$i; |
|
246 | - |
|
247 | - if ("\n" != $f[$i]) { |
|
248 | - isset($q[$f[$i]]) && ++$q[$f[$i]]; |
|
249 | - $s[] = '\\'.$f[$i]; |
|
250 | - } |
|
251 | - } elseif ('[' == $f[$i] && "/'" == $instr) { |
|
252 | - $instr = '/['; |
|
253 | - $s[] = '['; |
|
254 | - } elseif (']' == $f[$i] && '/[' == $instr) { |
|
255 | - $instr = "/'"; |
|
256 | - $s[] = ']'; |
|
257 | - } elseif ("'" == $f[$i] || '"' == $f[$i]) { |
|
258 | - ++$q[$f[$i]]; |
|
259 | - $s[] = '\\'.$f[$i]; |
|
260 | - } else { |
|
261 | - $s[] = $f[$i]; |
|
262 | - } |
|
263 | - } else { |
|
264 | - switch ($f[$i]) { |
|
265 | - case ';': |
|
266 | - // Remove triple semi-colon |
|
267 | - if ($i > 0 && ';' == $f[$i - 1] && $i + 1 < $len && ';' == $f[$i + 1]) { |
|
268 | - $f[$i] = $f[$i + 1] = '/'; |
|
269 | - } else { |
|
270 | - $code[++$j] = ';'; |
|
271 | - break; |
|
272 | - } |
|
273 | - |
|
274 | - case '/': |
|
275 | - if ('*' == $f[$i + 1]) { |
|
276 | - ++$i; |
|
277 | - $instr = '*'; |
|
278 | - |
|
279 | - if ($this->keepImportantComments && '!' == $f[$i + 1]) { |
|
280 | - ++$i; |
|
281 | - // no break here |
|
282 | - } else { |
|
283 | - break; |
|
284 | - } |
|
285 | - } elseif ('/' == $f[$i + 1]) { |
|
286 | - ++$i; |
|
287 | - $instr = '//'; |
|
288 | - break; |
|
289 | - } else { |
|
290 | - $a = $j && (' ' == $code[$j] || "\x7F" == $code[$j]) ? $code[$j - 1] : $code[$j]; |
|
291 | - if (false !== strpos('-!%&;<=>~:^+|,()*?[{} ', $a) |
|
292 | - || (false !== strpos('oenfd', $a) |
|
293 | - && preg_match( |
|
294 | - "'(?<![\$.a-zA-Z0-9_])(do|else|return|typeof|yield[ \x7F]?\*?)[ \x7F]?$'", |
|
295 | - substr($code, $j - 7, 8) |
|
296 | - )) |
|
297 | - ) { |
|
298 | - if (')' === $a && $j > 1) { |
|
299 | - $a = 1; |
|
300 | - $k = $j - (' ' == $code[$j] || "\x7F" == $code[$j]) - 1; |
|
301 | - while ($k >= 0 && $a) { |
|
302 | - if ('(' === $code[$k]) { |
|
303 | - --$a; |
|
304 | - } elseif (')' === $code[$k]) { |
|
305 | - ++$a; |
|
306 | - } |
|
307 | - --$k; |
|
308 | - } |
|
309 | - if (!preg_match("'(?<![\$.a-zA-Z0-9_])(if|for|while)[ \x7F]?$'", substr($code, 0, $k + 1))) { |
|
310 | - $code[++$j] = '/'; |
|
311 | - break; |
|
312 | - } |
|
313 | - } |
|
314 | - |
|
315 | - $key = "//''\"\"".$K++.$instr = "/'"; |
|
316 | - $a = $j; |
|
317 | - $code .= $key; |
|
318 | - while (isset($key[++$j - $a - 1])) { |
|
319 | - $code[$j] = $key[$j - $a - 1]; |
|
320 | - } |
|
321 | - --$j; |
|
322 | - isset($s) && ($s = implode('', $s)) && $cc_on && $this->restoreCc($s); |
|
323 | - $strings[$key] = array('/'); |
|
324 | - $s = &$strings[$key]; |
|
325 | - } else { |
|
326 | - $code[++$j] = '/'; |
|
327 | - } |
|
328 | - |
|
329 | - break; |
|
330 | - } |
|
331 | - |
|
332 | - case "'": |
|
333 | - case '"': |
|
334 | - $instr = $f[$i]; |
|
335 | - $key = "//''\"\"".$K++.('!' == $instr ? ']' : "'"); |
|
336 | - $a = $j; |
|
337 | - $code .= $key; |
|
338 | - while (isset($key[++$j - $a - 1])) { |
|
339 | - $code[$j] = $key[$j - $a - 1]; |
|
340 | - } |
|
341 | - --$j; |
|
342 | - isset($s) && ($s = implode('', $s)) && $cc_on && $this->restoreCc($s); |
|
343 | - $strings[$key] = array(); |
|
344 | - $s = &$strings[$key]; |
|
345 | - '!' == $instr && $s[] = "\r/*!"; |
|
346 | - |
|
347 | - break; |
|
348 | - |
|
349 | - case "\n": |
|
350 | - if ($j > 3) { |
|
351 | - if (' ' == $code[$j] || "\x7F" == $code[$j]) { |
|
352 | - --$j; |
|
353 | - } |
|
354 | - |
|
355 | - $code[++$j] = |
|
356 | - false !== strpos('kend+-', $code[$j - 1]) |
|
357 | - && preg_match( |
|
358 | - "'(?:\+\+|--|(?<![\$.a-zA-Z0-9_])(break|continue|return|yield[ \x7F]?\*?))[ \x7F]?$'", |
|
359 | - substr($code, $j - 8, 9) |
|
360 | - ) |
|
361 | - ? ';' : "\x7F"; |
|
362 | - |
|
363 | - break; |
|
364 | - } |
|
365 | - |
|
366 | - case "\t": $f[$i] = ' '; |
|
367 | - case ' ': |
|
368 | - if (!$j || ' ' == $code[$j] || "\x7F" == $code[$j]) { |
|
369 | - break; |
|
370 | - } |
|
371 | - |
|
372 | - default: |
|
373 | - $code[++$j] = $f[$i]; |
|
374 | - } |
|
375 | - } |
|
376 | - } |
|
377 | - |
|
378 | - isset($s) && ($s = implode('', $s)) && $cc_on && $this->restoreCc($s); |
|
379 | - unset($s); |
|
380 | - |
|
381 | - $code = substr($code, 0, $j + 1); |
|
382 | - $cc_on && $this->restoreCc($code, false); |
|
383 | - |
|
384 | - // Protect wanted spaces and remove unwanted ones |
|
385 | - $code = strtr($code, "\x7F", ' '); |
|
386 | - $code = str_replace('- -', "-\x7F-", $code); |
|
387 | - $code = str_replace('+ +', "+\x7F+", $code); |
|
388 | - $code = preg_replace("'(\d)\s+\.\s*([a-zA-Z\$_[(])'", "$1\x7F.$2", $code); |
|
389 | - $code = preg_replace("# ([-!%&;<=>~:.^+|,()*?[\]{}/']+)#", '$1', $code); |
|
390 | - $code = preg_replace("#([-!%&;<=>~:.^+|,()*?[\]{}/]+) #", '$1', $code); |
|
391 | - $cc_on && $code = preg_replace_callback("'//[^\'].*?@#3'", function ($m) { return strtr($m[0], ' ', "\x7F"); }, $code); |
|
392 | - |
|
393 | - // Replace new Array/Object by []/{} |
|
394 | - false !== strpos($code, 'new Array') && $code = preg_replace("'new Array(?:\(\)|([;\])},:]))'", '[]$1', $code); |
|
395 | - false !== strpos($code, 'new Object') && $code = preg_replace("'new Object(?:\(\)|([;\])},:]))'", '{}$1', $code); |
|
396 | - |
|
397 | - // Add missing semi-colons after curly braces |
|
398 | - // This adds more semi-colons than strictly needed, |
|
399 | - // but it seems that later gzipping is favorable to the repetition of "};" |
|
400 | - $code = preg_replace("'\}(?![:,;.()\[\]}\|&]|(else|catch|finally|while)[^\$.a-zA-Z0-9_])'", '};', $code); |
|
401 | - |
|
402 | - // Tag possible empty instruction for easy detection |
|
403 | - $code = preg_replace("'(?<![\$.a-zA-Z0-9_])if\('", '1#(', $code); |
|
404 | - $code = preg_replace("'(?<![\$.a-zA-Z0-9_])for\('", '2#(', $code); |
|
405 | - $code = preg_replace("'(?<![\$.a-zA-Z0-9_])while\('", '3#(', $code); |
|
406 | - |
|
407 | - $forPool = array(); |
|
408 | - $instrPool = array(); |
|
409 | - $s = 0; |
|
410 | - |
|
411 | - $f = array(); |
|
412 | - $j = -1; |
|
413 | - |
|
414 | - // Remove as much semi-colon as possible |
|
415 | - $len = strlen($code); |
|
416 | - for ($i = 0; $i < $len; ++$i) { |
|
417 | - switch ($code[$i]) { |
|
418 | - case '(': |
|
419 | - if ($j >= 0 && "\n" == $f[$j]) { |
|
420 | - $f[$j] = ';'; |
|
421 | - } |
|
422 | - |
|
423 | - ++$s; |
|
424 | - |
|
425 | - if ($i && '#' == $code[$i - 1]) { |
|
426 | - $instrPool[$s - 1] = 1; |
|
427 | - if ('2' == $code[$i - 2]) { |
|
428 | - $forPool[$s] = 1; |
|
429 | - } |
|
430 | - } |
|
431 | - |
|
432 | - $f[++$j] = '('; |
|
433 | - break; |
|
434 | - |
|
435 | - case ']': |
|
436 | - case ')': |
|
437 | - if ($i + 1 < $len && !isset($forPool[$s]) && !isset($instrPool[$s - 1]) && preg_match("'[a-zA-Z0-9_\$]'", $code[$i + 1])) { |
|
438 | - $f[$j] .= $code[$i]; |
|
439 | - $f[++$j] = "\n"; |
|
440 | - } else { |
|
441 | - $f[++$j] = $code[$i]; |
|
442 | - } |
|
443 | - |
|
444 | - if (')' == $code[$i]) { |
|
445 | - unset($forPool[$s]); |
|
446 | - --$s; |
|
447 | - } |
|
448 | - |
|
449 | - continue 2; |
|
450 | - |
|
451 | - case '}': |
|
452 | - if ("\n" == $f[$j]) { |
|
453 | - $f[$j] = '}'; |
|
454 | - } else { |
|
455 | - $f[++$j] = '}'; |
|
456 | - } |
|
457 | - break; |
|
458 | - |
|
459 | - case ';': |
|
460 | - if (isset($forPool[$s]) || isset($instrPool[$s])) { |
|
461 | - $f[++$j] = ';'; |
|
462 | - } elseif ($j >= 0 && "\n" != $f[$j] && ';' != $f[$j]) { |
|
463 | - $f[++$j] = "\n"; |
|
464 | - } |
|
465 | - |
|
466 | - break; |
|
467 | - |
|
468 | - case '#': |
|
469 | - switch ($f[$j]) { |
|
470 | - case '1': $f[$j] = 'if'; break 2; |
|
471 | - case '2': $f[$j] = 'for'; break 2; |
|
472 | - case '3': $f[$j] = 'while'; break 2; |
|
473 | - } |
|
474 | - |
|
475 | - case '['; |
|
476 | - if ($j >= 0 && "\n" == $f[$j]) { |
|
477 | - $f[$j] = ';'; |
|
478 | - } |
|
479 | - |
|
480 | - default: $f[++$j] = $code[$i]; |
|
481 | - } |
|
482 | - |
|
483 | - unset($instrPool[$s]); |
|
484 | - } |
|
485 | - |
|
486 | - $f = implode('', $f); |
|
487 | - $cc_on && $f = str_replace('@#3', "\r", $f); |
|
488 | - |
|
489 | - // Fix "else ;" empty instructions |
|
490 | - $f = preg_replace("'(?<![\$.a-zA-Z0-9_])else\n'", "\n", $f); |
|
491 | - |
|
492 | - $r1 = array( // keywords with a direct object |
|
493 | - 'case','delete','do','else','function','in','instanceof','of','break', |
|
494 | - 'new','return','throw','typeof','var','void','yield','let','if', |
|
495 | - 'const','get','set', |
|
496 | - ); |
|
497 | - |
|
498 | - $r2 = array( // keywords with a subject |
|
499 | - 'in','instanceof','of', |
|
500 | - ); |
|
501 | - |
|
502 | - // Fix missing semi-colons |
|
503 | - $f = preg_replace("'(?<!(?<![a-zA-Z0-9_\$])".implode(')(?<!(?<![a-zA-Z0-9_\$])', $r1).') (?!('.implode('|', $r2).")(?![a-zA-Z0-9_\$]))'", "\n", $f); |
|
504 | - $f = preg_replace("'(?<!(?<![a-zA-Z0-9_\$])do)(?<!(?<![a-zA-Z0-9_\$])else) if\('", "\nif(", $f); |
|
505 | - $f = preg_replace("'(?<=--|\+\+)(?<![a-zA-Z0-9_\$])(".implode('|', $r1).")(?![a-zA-Z0-9_\$])'", "\n$1", $f); |
|
506 | - $f = preg_replace("'(?<![a-zA-Z0-9_\$])for\neach\('", 'for each(', $f); |
|
507 | - $f = preg_replace("'(?<![a-zA-Z0-9_\$])\n(".implode('|', $r2).")(?![a-zA-Z0-9_\$])'", '$1', $f); |
|
508 | - |
|
509 | - // Merge strings |
|
510 | - if ($q["'"] > $q['"']) { |
|
511 | - $q = array($q[1], $q[0]); |
|
512 | - } |
|
513 | - $f = preg_replace("#//''\"\"[0-9]+'#", $q[0].'$0'.$q[0], $f); |
|
514 | - strpos($f, $q[0].'+'.$q[0]) && $f = str_replace($q[0].'+'.$q[0], '', $f); |
|
515 | - $len = count($strings); |
|
516 | - foreach ($strings as $r1 => &$r2) { |
|
517 | - $r2 = "/'" == substr($r1, -2) |
|
518 | - ? str_replace(array("\\'", '\\"'), array("'", '"'), $r2) |
|
519 | - : str_replace('\\'.$q[1], $q[1], $r2); |
|
520 | - } |
|
521 | - |
|
522 | - // Restore wanted spaces |
|
523 | - $f = strtr($f, "\x7F", ' '); |
|
524 | - |
|
525 | - return array($f, $strings); |
|
526 | - } |
|
527 | - |
|
528 | - protected function extractClosures($code) |
|
529 | - { |
|
530 | - $code = ';'.$code; |
|
531 | - |
|
532 | - $this->argFreq[-1] += substr_count($code, '}catch('); |
|
533 | - |
|
534 | - if ($this->argFreq[-1]) { |
|
535 | - // Special catch scope handling |
|
536 | - |
|
537 | - // FIXME: this implementation doesn't work with nested catch scopes who need |
|
538 | - // access to their parent's caught variable (but who needs that?). |
|
539 | - |
|
540 | - $f = preg_split("@}catch\(({$this->varRx})@", $code, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
541 | - |
|
542 | - $code = 'catch$scope$var'.mt_rand(); |
|
543 | - $this->specialVarRx = $this->specialVarRx ? '(?:'.$this->specialVarRx.'|'.preg_quote($code).')' : preg_quote($code); |
|
544 | - $i = count($f) - 1; |
|
545 | - |
|
546 | - while ($i) { |
|
547 | - $c = 1; |
|
548 | - $j = 0; |
|
549 | - $l = strlen($f[$i]); |
|
550 | - |
|
551 | - while ($c && $j < $l) { |
|
552 | - $s = $f[$i][$j++]; |
|
553 | - $c += '(' == $s ? 1 : (')' == $s ? -1 : 0); |
|
554 | - } |
|
555 | - |
|
556 | - if (!$c) { |
|
557 | - do { |
|
558 | - $s = $f[$i][$j++]; |
|
559 | - $c += '{' == $s ? 1 : ('}' == $s ? -1 : 0); |
|
560 | - } while ($c && $j < $l); |
|
561 | - } |
|
562 | - |
|
563 | - $c = preg_quote($f[$i - 1], '#'); |
|
564 | - $f[$i - 2] .= '}catch('.preg_replace("#([.,{]?)(?<![a-zA-Z0-9_\$@]){$c}\\b#", '$1'.$code, $f[$i - 1].substr($f[$i], 0, $j)).substr($f[$i], $j); |
|
565 | - |
|
566 | - unset($f[$i--], $f[$i--]); |
|
567 | - } |
|
568 | - |
|
569 | - $code = $f[0]; |
|
570 | - } |
|
571 | - |
|
572 | - $f = preg_split("'(?<![a-zA-Z0-9_\$])((?:function[ (]|get |set ).*?\{)'", $code, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
573 | - $i = count($f) - 1; |
|
574 | - $closures = array(); |
|
575 | - |
|
576 | - while ($i) { |
|
577 | - $c = 1; |
|
578 | - $j = 0; |
|
579 | - $l = strlen($f[$i]); |
|
580 | - |
|
581 | - while ($c && $j < $l) { |
|
582 | - $s = $f[$i][$j++]; |
|
583 | - $c += '{' == $s ? 1 : ('}' == $s ? -1 : 0); |
|
584 | - } |
|
585 | - |
|
586 | - switch (substr($f[$i - 2], -1)) { |
|
587 | - default: |
|
588 | - if (false !== $c = strpos($f[$i - 1], ' ', 8)) { |
|
589 | - break; |
|
590 | - } |
|
591 | - case false: case "\n": case ';': case '{': case '}': case ')': case ']': |
|
592 | - $c = strpos($f[$i - 1], '(', 4); |
|
593 | - } |
|
594 | - |
|
595 | - $l = "//''\"\"#$i'"; |
|
596 | - $code = substr($f[$i - 1], $c); |
|
597 | - $closures[$l] = $code.substr($f[$i], 0, $j); |
|
598 | - $f[$i - 2] .= substr($f[$i - 1], 0, $c).$l.substr($f[$i], $j); |
|
599 | - |
|
600 | - if ('(){' !== $code) { |
|
601 | - $j = substr_count($code, ','); |
|
602 | - do { |
|
603 | - isset($this->argFreq[$j]) ? ++$this->argFreq[$j] : $this->argFreq[$j] = 1; |
|
604 | - } while ($j--); |
|
605 | - } |
|
606 | - |
|
607 | - $i -= 2; |
|
608 | - } |
|
609 | - |
|
610 | - return array($f[0], $closures); |
|
611 | - } |
|
612 | - |
|
613 | - protected function makeVars($closure, &$tree, $key) |
|
614 | - { |
|
615 | - $tree['code'] = &$closure; |
|
616 | - $tree['nfe'] = false; |
|
617 | - $tree['used'] = array(); |
|
618 | - $tree['local'] = array(); |
|
619 | - |
|
620 | - // Replace multiple "var" declarations by a single one |
|
621 | - $closure = preg_replace_callback("'(?<=[\n\{\}])var [^\n\{\};]+(?:\nvar [^\n\{\};]+)+'", array($this, 'mergeVarDeclarations'), $closure); |
|
622 | - |
|
623 | - // Get all local vars (functions, arguments and "var" prefixed) |
|
624 | - |
|
625 | - $vars = &$tree['local']; |
|
626 | - |
|
627 | - if (preg_match("'^( [^(]*)?\((.*?)\)\{'", $closure, $v)) { |
|
628 | - if ($v[1]) { |
|
629 | - $vars[$tree['nfe'] = substr($v[1], 1)] = -1; |
|
630 | - $tree['parent']['local'][';'.$key] = &$vars[$tree['nfe']]; |
|
631 | - } |
|
632 | - |
|
633 | - if ($v[2]) { |
|
634 | - $i = 0; |
|
635 | - $v = explode(',', $v[2]); |
|
636 | - foreach ($v as $w) { |
|
637 | - $vars[$w] = $this->argFreq[$i++] - 1; // Give a bonus to argument variables |
|
638 | - } |
|
639 | - } |
|
640 | - } |
|
641 | - |
|
642 | - $v = preg_split("'(?<![\$.a-zA-Z0-9_])var '", $closure); |
|
643 | - if ($i = count($v) - 1) { |
|
644 | - $w = array(); |
|
645 | - |
|
646 | - while ($i) { |
|
647 | - $j = $c = 0; |
|
648 | - $l = strlen($v[$i]); |
|
649 | - |
|
650 | - while ($j < $l) { |
|
651 | - switch ($v[$i][$j]) { |
|
652 | - case '(': case '[': case '{': |
|
653 | - ++$c; |
|
654 | - break; |
|
655 | - |
|
656 | - case ')': case ']': case '}': |
|
657 | - if ($c-- <= 0) { |
|
658 | - break 2; |
|
659 | - } |
|
660 | - break; |
|
661 | - |
|
662 | - case ';': case "\n": |
|
663 | - if (!$c) { |
|
664 | - break 2; |
|
665 | - } |
|
666 | - |
|
667 | - default: |
|
668 | - $c || $w[] = $v[$i][$j]; |
|
669 | - } |
|
670 | - |
|
671 | - ++$j; |
|
672 | - } |
|
673 | - |
|
674 | - $w[] = ','; |
|
675 | - --$i; |
|
676 | - } |
|
677 | - |
|
678 | - $v = explode(',', implode('', $w)); |
|
679 | - foreach ($v as $w) { |
|
680 | - if (preg_match("'^{$this->varRx}'", $w, $v)) { |
|
681 | - isset($vars[$v[0]]) || $vars[$v[0]] = 0; |
|
682 | - } |
|
683 | - } |
|
684 | - } |
|
685 | - |
|
686 | - if (preg_match_all("@function ({$this->varRx})//''\"\"#@", $closure, $v)) { |
|
687 | - foreach ($v[1] as $w) { |
|
688 | - isset($vars[$w]) || $vars[$w] = 0; |
|
689 | - } |
|
690 | - } |
|
691 | - |
|
692 | - if ($this->argFreq[-1] && preg_match_all("@}catch\(({$this->varRx})@", $closure, $v)) { |
|
693 | - $v[0] = array(); |
|
694 | - foreach ($v[1] as $w) { |
|
695 | - isset($v[0][$w]) ? ++$v[0][$w] : $v[0][$w] = 1; |
|
696 | - } |
|
697 | - foreach ($v[0] as $w => $v) { |
|
698 | - $vars[$w] = $this->argFreq[-1] - $v; |
|
699 | - } |
|
700 | - } |
|
701 | - |
|
702 | - // Get all used vars, local and non-local |
|
703 | - |
|
704 | - $vars = &$tree['used']; |
|
705 | - |
|
706 | - if (preg_match_all("#([.,{]?(?:[gs]et )?)(?<![a-zA-Z0-9_\$])({$this->varRx})(:?)#", $closure, $w, PREG_SET_ORDER)) { |
|
707 | - foreach ($w as $k) { |
|
708 | - if (isset($k[1][0]) && (',' === $k[1][0] || '{' === $k[1][0])) { |
|
709 | - if (':' === $k[3]) { |
|
710 | - $k = '.'.$k[2]; |
|
711 | - } elseif ('get ' === substr($k[1], 1, 4) || 'set ' === substr($k[1], 1, 4)) { |
|
712 | - ++$this->charFreq[ord($k[1][1])]; // "g" or "s" |
|
713 | - ++$this->charFreq[101]; // "e" |
|
714 | - ++$this->charFreq[116]; // "t" |
|
715 | - $k = '.'.$k[2]; |
|
716 | - } else { |
|
717 | - $k = $k[2]; |
|
718 | - } |
|
719 | - } else { |
|
720 | - $k = $k[1].$k[2]; |
|
721 | - } |
|
722 | - |
|
723 | - isset($vars[$k]) ? ++$vars[$k] : $vars[$k] = 1; |
|
724 | - } |
|
725 | - } |
|
726 | - |
|
727 | - if (preg_match_all("#//''\"\"[0-9]+(?:['!]|/')#", $closure, $w)) { |
|
728 | - foreach ($w[0] as $a) { |
|
729 | - $v = "'" === substr($a, -1) && "/'" !== substr($a, -2) && $this->specialVarRx |
|
730 | - ? preg_split("#([.,{]?(?:[gs]et )?(?<![a-zA-Z0-9_\$@]){$this->specialVarRx}:?)#", $this->strings[$a], -1, PREG_SPLIT_DELIM_CAPTURE) |
|
731 | - : array($this->strings[$a]); |
|
732 | - $a = count($v); |
|
733 | - |
|
734 | - for ($i = 0; $i < $a; ++$i) { |
|
735 | - $k = $v[$i]; |
|
736 | - |
|
737 | - if (1 === $i % 2) { |
|
738 | - if (',' === $k[0] || '{' === $k[0]) { |
|
739 | - if (':' === substr($k, -1)) { |
|
740 | - $k = '.'.substr($k, 1, -1); |
|
741 | - } elseif ('get ' === substr($k, 1, 4) || 'set ' === substr($k, 1, 4)) { |
|
742 | - ++$this->charFreq[ord($k[1])]; // "g" or "s" |
|
743 | - ++$this->charFreq[101]; // "e" |
|
744 | - ++$this->charFreq[116]; // "t" |
|
745 | - $k = '.'.substr($k, 5); |
|
746 | - } else { |
|
747 | - $k = substr($k, 1); |
|
748 | - } |
|
749 | - } elseif (':' === substr($k, -1)) { |
|
750 | - $k = substr($k, 0, -1); |
|
751 | - } |
|
752 | - |
|
753 | - $w = &$tree; |
|
754 | - |
|
755 | - while (isset($w['parent']) && !(isset($w['used'][$k]) || isset($w['local'][$k]))) { |
|
756 | - $w = &$w['parent']; |
|
757 | - } |
|
758 | - |
|
759 | - (isset($w['used'][$k]) || isset($w['local'][$k])) && (isset($vars[$k]) ? ++$vars[$k] : $vars[$k] = 1); |
|
760 | - |
|
761 | - unset($w); |
|
762 | - } |
|
763 | - |
|
764 | - if (0 === $i % 2 || !isset($vars[$k])) { |
|
765 | - foreach (count_chars($v[$i], 1) as $k => $w) { |
|
766 | - $this->charFreq[$k] += $w; |
|
767 | - } |
|
768 | - } |
|
769 | - } |
|
770 | - } |
|
771 | - } |
|
772 | - |
|
773 | - // Propagate the usage number to parents |
|
774 | - |
|
775 | - foreach ($vars as $w => $a) { |
|
776 | - $k = &$tree; |
|
777 | - $chain = array(); |
|
778 | - do { |
|
779 | - $vars = &$k['local']; |
|
780 | - $chain[] = &$k; |
|
781 | - if (isset($vars[$w])) { |
|
782 | - unset($k['used'][$w]); |
|
783 | - if (isset($vars[$w])) { |
|
784 | - $vars[$w] += $a; |
|
785 | - } else { |
|
786 | - $vars[$w] = $a; |
|
787 | - } |
|
788 | - $a = false; |
|
789 | - break; |
|
790 | - } |
|
791 | - } while ($k['parent'] && $k = &$k['parent']); |
|
792 | - |
|
793 | - if ($a && !$k['parent']) { |
|
794 | - if (isset($vars[$w])) { |
|
795 | - $vars[$w] += $a; |
|
796 | - } else { |
|
797 | - $vars[$w] = $a; |
|
798 | - } |
|
799 | - } |
|
800 | - |
|
801 | - if (isset($tree['used'][$w]) && isset($vars[$w])) { |
|
802 | - foreach ($chain as &$b) { |
|
803 | - isset($b['local'][$w]) || $b['used'][$w] = &$vars[$w]; |
|
804 | - } |
|
805 | - } |
|
806 | - } |
|
807 | - |
|
808 | - // Analyse childs |
|
809 | - |
|
810 | - $tree['childs'] = array(); |
|
811 | - $vars = &$tree['childs']; |
|
812 | - |
|
813 | - if (preg_match_all("@//''\"\"#[0-9]+'@", $closure, $w)) { |
|
814 | - foreach ($w[0] as $a) { |
|
815 | - $vars[$a] = array('parent' => &$tree); |
|
816 | - $this->makeVars($this->closures[$a], $vars[$a], $a); |
|
817 | - } |
|
818 | - } |
|
819 | - } |
|
820 | - |
|
821 | - protected function mergeVarDeclarations($m) |
|
822 | - { |
|
823 | - return str_replace("\nvar ", ',', $m[0]); |
|
824 | - } |
|
825 | - |
|
826 | - protected function renameVars(&$tree, $root) |
|
827 | - { |
|
828 | - if ($root) { |
|
829 | - $tree['local'] += $tree['used']; |
|
830 | - $tree['used'] = array(); |
|
831 | - |
|
832 | - foreach ($tree['local'] as $k => $v) { |
|
833 | - if ('.' == $k[0]) { |
|
834 | - $k = substr($k, 1); |
|
835 | - } |
|
836 | - |
|
837 | - if ('true' === $k) { |
|
838 | - $this->charFreq[48] += $v; |
|
839 | - } elseif ('false' === $k) { |
|
840 | - $this->charFreq[49] += $v; |
|
841 | - } elseif (!$this->specialVarRx || !preg_match("#^{$this->specialVarRx}$#", $k)) { |
|
842 | - foreach (count_chars($k, 1) as $k => $w) { |
|
843 | - $this->charFreq[$k] += $w * $v; |
|
844 | - } |
|
845 | - } elseif (2 == strlen($k)) { |
|
846 | - $tree['used'][] = $k[1]; |
|
847 | - } |
|
848 | - } |
|
849 | - |
|
850 | - $this->charFreq = $this->rsort($this->charFreq); |
|
851 | - |
|
852 | - $this->str0 = ''; |
|
853 | - $this->str1 = ''; |
|
854 | - |
|
855 | - foreach ($this->charFreq as $k => $v) { |
|
856 | - if (!$v) { |
|
857 | - break; |
|
858 | - } |
|
859 | - |
|
860 | - $v = chr($k); |
|
861 | - |
|
862 | - if ((64 < $k && $k < 91) || (96 < $k && $k < 123)) { // A-Z a-z |
|
863 | - $this->str0 .= $v; |
|
864 | - $this->str1 .= $v; |
|
865 | - } elseif (47 < $k && $k < 58) { // 0-9 |
|
866 | - $this->str1 .= $v; |
|
867 | - } |
|
868 | - } |
|
869 | - |
|
870 | - if ('' === $this->str0) { |
|
871 | - $this->str0 = 'claspemitdbfrugnjvhowkxqyzCLASPEMITDBFRUGNJVHOWKXQYZ'; |
|
872 | - $this->str1 = $this->str0.'0123456789'; |
|
873 | - } |
|
874 | - |
|
875 | - foreach ($tree['local'] as $var => $root) { |
|
876 | - if ('.' != substr($var, 0, 1) && isset($tree['local'][".{$var}"])) { |
|
877 | - $tree['local'][$var] += $tree['local'][".{$var}"]; |
|
878 | - } |
|
879 | - } |
|
880 | - |
|
881 | - foreach ($tree['local'] as $var => $root) { |
|
882 | - if ('.' == substr($var, 0, 1) && isset($tree['local'][substr($var, 1)])) { |
|
883 | - $tree['local'][$var] = $tree['local'][substr($var, 1)]; |
|
884 | - } |
|
885 | - } |
|
886 | - |
|
887 | - $tree['local'] = $this->rsort($tree['local']); |
|
888 | - |
|
889 | - foreach ($tree['local'] as $var => $root) { |
|
890 | - switch (substr($var, 0, 1)) { |
|
891 | - case '.': |
|
892 | - if (!isset($tree['local'][substr($var, 1)])) { |
|
893 | - $tree['local'][$var] = '#'.($this->specialVarRx && 3 < strlen($var) && preg_match("'^\.{$this->specialVarRx}$'", $var) ? $this->getNextName($tree).'$' : substr($var, 1)); |
|
894 | - } |
|
895 | - break; |
|
896 | - |
|
897 | - case ';': $tree['local'][$var] = 0 === $root ? '' : $this->getNextName($tree); |
|
898 | - case '#': break; |
|
899 | - |
|
900 | - default: |
|
901 | - $root = $this->specialVarRx && 2 < strlen($var) && preg_match("'^{$this->specialVarRx}$'", $var) ? $this->getNextName($tree).'$' : $var; |
|
902 | - $tree['local'][$var] = $root; |
|
903 | - if (isset($tree['local'][".{$var}"])) { |
|
904 | - $tree['local'][".{$var}"] = '#'.$root; |
|
905 | - } |
|
906 | - } |
|
907 | - } |
|
908 | - |
|
909 | - foreach ($tree['local'] as $var => $root) { |
|
910 | - $tree['local'][$var] = preg_replace("'^#'", '.', $tree['local'][$var]); |
|
911 | - } |
|
912 | - } else { |
|
913 | - $tree['local'] = $this->rsort($tree['local']); |
|
914 | - if (false !== $tree['nfe']) { |
|
915 | - $tree['used'][] = $tree['local'][$tree['nfe']]; |
|
916 | - } |
|
917 | - |
|
918 | - foreach ($tree['local'] as $var => $root) { |
|
919 | - if ($tree['nfe'] !== $var) { |
|
920 | - $tree['local'][$var] = 0 === $root ? '' : $this->getNextName($tree); |
|
921 | - } |
|
922 | - } |
|
923 | - } |
|
924 | - |
|
925 | - $this->local_tree = &$tree['local']; |
|
926 | - $this->used_tree = &$tree['used']; |
|
927 | - |
|
928 | - $tree['code'] = preg_replace_callback("#[.,{ ]?(?:[gs]et )?(?<![a-zA-Z0-9_\$@]){$this->varRx}:?#", array($this, 'getNewName'), $tree['code']); |
|
929 | - |
|
930 | - if ($this->specialVarRx && preg_match_all("#//''\"\"[0-9]+'#", $tree['code'], $b)) { |
|
931 | - foreach ($b[0] as $a) { |
|
932 | - $this->strings[$a] = preg_replace_callback( |
|
933 | - "#[.,{]?(?:[gs]et )?(?<![a-zA-Z0-9_\$@]){$this->specialVarRx}:?#", |
|
934 | - array($this, 'getNewName'), |
|
935 | - $this->strings[$a] |
|
936 | - ); |
|
937 | - } |
|
938 | - } |
|
939 | - |
|
940 | - foreach ($tree['childs'] as $a => &$b) { |
|
941 | - $this->renameVars($b, false); |
|
942 | - $tree['code'] = str_replace($a, $b['code'], $tree['code']); |
|
943 | - unset($tree['childs'][$a]); |
|
944 | - } |
|
945 | - } |
|
946 | - |
|
947 | - protected function getNewName($m) |
|
948 | - { |
|
949 | - $m = $m[0]; |
|
950 | - |
|
951 | - $pre = '.' === $m[0] ? '.' : ''; |
|
952 | - $post = ''; |
|
953 | - |
|
954 | - if (',' === $m[0] || '{' === $m[0] || ' ' === $m[0]) { |
|
955 | - $pre = $m[0]; |
|
956 | - |
|
957 | - if (':' === substr($m, -1)) { |
|
958 | - $post = ':'; |
|
959 | - $m = (' ' !== $m[0] ? '.' : '').substr($m, 1, -1); |
|
960 | - } elseif ('get ' === substr($m, 1, 4) || 'set ' === substr($m, 1, 4)) { |
|
961 | - $pre .= substr($m, 1, 4); |
|
962 | - $m = '.'.substr($m, 5); |
|
963 | - } else { |
|
964 | - $m = substr($m, 1); |
|
965 | - } |
|
966 | - } elseif (':' === substr($m, -1)) { |
|
967 | - $post = ':'; |
|
968 | - $m = substr($m, 0, -1); |
|
969 | - } |
|
970 | - |
|
971 | - $post = (isset($this->reserved[$m]) |
|
972 | - ? ('true' === $m ? '!0' : ('false' === $m ? '!1' : $m)) |
|
973 | - : ( |
|
974 | - isset($this->local_tree[$m]) |
|
975 | - ? $this->local_tree[$m] |
|
976 | - : ( |
|
977 | - isset($this->used_tree[$m]) |
|
978 | - ? $this->used_tree[$m] |
|
979 | - : $m |
|
980 | - ) |
|
981 | - ) |
|
982 | - ).$post; |
|
983 | - |
|
984 | - return '' === $post ? '' : ($pre.('.' === $post[0] ? substr($post, 1) : $post)); |
|
985 | - } |
|
986 | - |
|
987 | - protected function getNextName(&$tree = array(), &$counter = false) |
|
988 | - { |
|
989 | - if (false === $counter) { |
|
990 | - $counter = &$tree['counter']; |
|
991 | - isset($counter) || $counter = -1; |
|
992 | - $exclude = array_flip($tree['used']); |
|
993 | - } else { |
|
994 | - $exclude = $tree; |
|
995 | - } |
|
996 | - |
|
997 | - ++$counter; |
|
998 | - |
|
999 | - $len0 = strlen($this->str0); |
|
1000 | - $len1 = strlen($this->str0); |
|
1001 | - |
|
1002 | - $name = $this->str0[$counter % $len0]; |
|
1003 | - |
|
1004 | - $i = intval($counter / $len0) - 1; |
|
1005 | - while ($i >= 0) { |
|
1006 | - $name .= $this->str1[ $i % $len1 ]; |
|
1007 | - $i = intval($i / $len1) - 1; |
|
1008 | - } |
|
1009 | - |
|
1010 | - return !(isset($this->reserved[$name]) || isset($exclude[$name])) ? $name : $this->getNextName($exclude, $counter); |
|
1011 | - } |
|
1012 | - |
|
1013 | - protected function restoreCc(&$s, $lf = true) |
|
1014 | - { |
|
1015 | - $lf && $s = str_replace('@#3', '', $s); |
|
1016 | - |
|
1017 | - $s = str_replace('@#1', '@*/', $s); |
|
1018 | - $s = str_replace('2#@', '//@', $s); |
|
1019 | - $s = str_replace('1#@', '/*@', $s); |
|
1020 | - $s = str_replace('##', '#', $s); |
|
1021 | - } |
|
1022 | - |
|
1023 | - private function rsort($array) |
|
1024 | - { |
|
1025 | - if (!$array) { |
|
1026 | - return $array; |
|
1027 | - } |
|
1028 | - |
|
1029 | - $i = 0; |
|
1030 | - $tuples = array(); |
|
1031 | - foreach ($array as $k => &$v) { |
|
1032 | - $tuples[] = array(++$i, $k, &$v); |
|
1033 | - } |
|
1034 | - |
|
1035 | - usort($tuples, function ($a, $b) { |
|
1036 | - if ($b[2] > $a[2]) { |
|
1037 | - return 1; |
|
1038 | - } |
|
1039 | - if ($b[2] < $a[2]) { |
|
1040 | - return -1; |
|
1041 | - } |
|
1042 | - if ($b[0] > $a[0]) { |
|
1043 | - return -1; |
|
1044 | - } |
|
1045 | - if ($b[0] < $a[0]) { |
|
1046 | - return 1; |
|
1047 | - } |
|
1048 | - |
|
1049 | - return 0; |
|
1050 | - }); |
|
1051 | - |
|
1052 | - $array = array(); |
|
1053 | - |
|
1054 | - foreach ($tuples as $t) { |
|
1055 | - $array[$t[1]] = &$t[2]; |
|
1056 | - } |
|
1057 | - |
|
1058 | - return $array; |
|
1059 | - } |
|
60 | + const |
|
61 | + |
|
62 | + SPECIAL_VAR_PACKER = '(\$+[a-zA-Z_]|_[a-zA-Z0-9$])[a-zA-Z0-9_$]*'; |
|
63 | + |
|
64 | + public |
|
65 | + |
|
66 | + $charFreq; |
|
67 | + |
|
68 | + protected |
|
69 | + |
|
70 | + $strings, |
|
71 | + $closures, |
|
72 | + $str0, |
|
73 | + $str1, |
|
74 | + $argFreq, |
|
75 | + $specialVarRx, |
|
76 | + $keepImportantComments, |
|
77 | + |
|
78 | + $varRx = '(?:[a-zA-Z_$])[a-zA-Z0-9_$]*', |
|
79 | + $reserved = array( |
|
80 | + // Literals |
|
81 | + 'true','false','null', |
|
82 | + // ES6 |
|
83 | + 'break','case','class','catch','const','continue','debugger','default','delete','do','else','export','extends','finally','for','function','if','import','in','instanceof','new','return','super','switch','this','throw','try','typeof','var','void','while','with','yield', |
|
84 | + // Future |
|
85 | + 'enum', |
|
86 | + // Strict mode |
|
87 | + 'implements','package','protected','static','let','interface','private','public', |
|
88 | + // Module |
|
89 | + 'await', |
|
90 | + // Older standards |
|
91 | + 'abstract','boolean','byte','char','double','final','float','goto','int','long','native','short','synchronized','throws','transient','volatile', |
|
92 | + ); |
|
93 | + |
|
94 | + public function __construct() |
|
95 | + { |
|
96 | + $this->reserved = array_flip($this->reserved); |
|
97 | + $this->charFreq = array_fill(0, 256, 0); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Squeezes a JavaScript source code. |
|
102 | + * |
|
103 | + * Set $singleLine to false if you want optional |
|
104 | + * semi-colons to be replaced by line feeds. |
|
105 | + * |
|
106 | + * Set $keepImportantComments to false if you want /*! comments to be removed. |
|
107 | + * |
|
108 | + * $specialVarRx defines the regular expression of special variables names |
|
109 | + * for global vars, methods, properties and in string substitution. |
|
110 | + * Set it to false if you don't want any. |
|
111 | + * |
|
112 | + * If the analysed javascript source contains a single line comment like |
|
113 | + * this one, then the directive will overwrite $specialVarRx: |
|
114 | + * |
|
115 | + * // jsqueeze.specialVarRx = your_special_var_regexp_here |
|
116 | + * |
|
117 | + * Only the first directive is parsed, others are ignored. It is not possible |
|
118 | + * to redefine $specialVarRx in the middle of the javascript source. |
|
119 | + * |
|
120 | + * Example: |
|
121 | + * $parser = new JSqueeze; |
|
122 | + * $squeezed_js = $parser->squeeze($fat_js); |
|
123 | + */ |
|
124 | + public function squeeze($code, $singleLine = true, $keepImportantComments = true, $specialVarRx = false) |
|
125 | + { |
|
126 | + $code = trim($code); |
|
127 | + if ('' === $code) { |
|
128 | + return ''; |
|
129 | + } |
|
130 | + |
|
131 | + $this->argFreq = array(-1 => 0); |
|
132 | + $this->specialVarRx = $specialVarRx; |
|
133 | + $this->keepImportantComments = !!$keepImportantComments; |
|
134 | + |
|
135 | + if (preg_match("#//[ \t]*jsqueeze\.specialVarRx[ \t]*=[ \t]*([\"']?)(.*)\\1#i", $code, $key)) { |
|
136 | + if (!$key[1]) { |
|
137 | + $key[2] = trim($key[2]); |
|
138 | + $key[1] = strtolower($key[2]); |
|
139 | + $key[1] = $key[1] && $key[1] != 'false' && $key[1] != 'none' && $key[1] != 'off'; |
|
140 | + } |
|
141 | + |
|
142 | + $this->specialVarRx = $key[1] ? $key[2] : false; |
|
143 | + } |
|
144 | + |
|
145 | + // Remove capturing parentheses |
|
146 | + $this->specialVarRx && $this->specialVarRx = preg_replace('/(?<!\\\\)((?:\\\\\\\\)*)\((?!\?)/', '(?:', $this->specialVarRx); |
|
147 | + |
|
148 | + false !== strpos($code, "\r") && $code = strtr(str_replace("\r\n", "\n", $code), "\r", "\n"); |
|
149 | + false !== strpos($code, "\xC2\x85") && $code = str_replace("\xC2\x85", "\n", $code); // Next Line |
|
150 | + false !== strpos($code, "\xE2\x80\xA8") && $code = str_replace("\xE2\x80\xA8", "\n", $code); // Line Separator |
|
151 | + false !== strpos($code, "\xE2\x80\xA9") && $code = str_replace("\xE2\x80\xA9", "\n", $code); // Paragraph Separator |
|
152 | + |
|
153 | + list($code, $this->strings) = $this->extractStrings($code); |
|
154 | + list($code, $this->closures) = $this->extractClosures($code); |
|
155 | + |
|
156 | + $key = "//''\"\"#0'"; // This crap has a wonderful property: it can not happen in any valid javascript, even in strings |
|
157 | + $this->closures[$key] = &$code; |
|
158 | + |
|
159 | + $tree = array($key => array('parent' => false)); |
|
160 | + $this->makeVars($code, $tree[$key], $key); |
|
161 | + $this->renameVars($tree[$key], true); |
|
162 | + |
|
163 | + $code = substr($tree[$key]['code'], 1); |
|
164 | + $code = preg_replace("'\breturn !'", 'return!', $code); |
|
165 | + $code = preg_replace("'\}(?=(else|while)[^\$.a-zA-Z0-9_])'", "}\r", $code); |
|
166 | + $code = str_replace(array_keys($this->strings), array_values($this->strings), $code); |
|
167 | + |
|
168 | + if ($singleLine) { |
|
169 | + $code = strtr($code, "\n", ';'); |
|
170 | + } else { |
|
171 | + $code = str_replace("\n", ";\n", $code); |
|
172 | + } |
|
173 | + false !== strpos($code, "\r") && $code = strtr(trim($code), "\r", "\n"); |
|
174 | + |
|
175 | + // Cleanup memory |
|
176 | + $this->charFreq = array_fill(0, 256, 0); |
|
177 | + $this->strings = $this->closures = $this->argFreq = array(); |
|
178 | + $this->str0 = $this->str1 = ''; |
|
179 | + |
|
180 | + return $code; |
|
181 | + } |
|
182 | + |
|
183 | + protected function extractStrings($f) |
|
184 | + { |
|
185 | + if ($cc_on = false !== strpos($f, '@cc_on')) { |
|
186 | + // Protect conditional comments from being removed |
|
187 | + $f = str_replace('#', '##', $f); |
|
188 | + $f = str_replace('/*@', '1#@', $f); |
|
189 | + $f = preg_replace("'//@([^\n]+)'", '2#@$1@#3', $f); |
|
190 | + $f = str_replace('@*/', '@#1', $f); |
|
191 | + } |
|
192 | + |
|
193 | + $len = strlen($f); |
|
194 | + $code = str_repeat(' ', $len); |
|
195 | + $j = 0; |
|
196 | + |
|
197 | + $strings = array(); |
|
198 | + $K = 0; |
|
199 | + |
|
200 | + $instr = false; |
|
201 | + |
|
202 | + $q = array( |
|
203 | + "'", '"', |
|
204 | + "'" => 0, |
|
205 | + '"' => 0, |
|
206 | + ); |
|
207 | + |
|
208 | + // Extract strings, removes comments |
|
209 | + for ($i = 0; $i < $len; ++$i) { |
|
210 | + if ($instr) { |
|
211 | + if ('//' == $instr) { |
|
212 | + if ("\n" == $f[$i]) { |
|
213 | + $f[$i--] = ' '; |
|
214 | + $instr = false; |
|
215 | + } |
|
216 | + } elseif ($f[$i] == $instr || ('/' == $f[$i] && "/'" == $instr)) { |
|
217 | + if ('!' == $instr) { |
|
218 | + } elseif ('*' == $instr) { |
|
219 | + if ('/' == $f[$i + 1]) { |
|
220 | + ++$i; |
|
221 | + $instr = false; |
|
222 | + } |
|
223 | + } else { |
|
224 | + if ("/'" == $instr) { |
|
225 | + while (isset($f[$i + 1]) && false !== strpos('gmi', $f[$i + 1])) { |
|
226 | + $s[] = $f[$i++]; |
|
227 | + } |
|
228 | + $s[] = $f[$i]; |
|
229 | + } |
|
230 | + |
|
231 | + $instr = false; |
|
232 | + } |
|
233 | + } elseif ('*' == $instr) { |
|
234 | + } elseif ('!' == $instr) { |
|
235 | + if ('*' == $f[$i] && '/' == $f[$i + 1]) { |
|
236 | + $s[] = "*/\r"; |
|
237 | + ++$i; |
|
238 | + $instr = false; |
|
239 | + } elseif ("\n" == $f[$i]) { |
|
240 | + $s[] = "\r"; |
|
241 | + } else { |
|
242 | + $s[] = $f[$i]; |
|
243 | + } |
|
244 | + } elseif ('\\' == $f[$i]) { |
|
245 | + ++$i; |
|
246 | + |
|
247 | + if ("\n" != $f[$i]) { |
|
248 | + isset($q[$f[$i]]) && ++$q[$f[$i]]; |
|
249 | + $s[] = '\\'.$f[$i]; |
|
250 | + } |
|
251 | + } elseif ('[' == $f[$i] && "/'" == $instr) { |
|
252 | + $instr = '/['; |
|
253 | + $s[] = '['; |
|
254 | + } elseif (']' == $f[$i] && '/[' == $instr) { |
|
255 | + $instr = "/'"; |
|
256 | + $s[] = ']'; |
|
257 | + } elseif ("'" == $f[$i] || '"' == $f[$i]) { |
|
258 | + ++$q[$f[$i]]; |
|
259 | + $s[] = '\\'.$f[$i]; |
|
260 | + } else { |
|
261 | + $s[] = $f[$i]; |
|
262 | + } |
|
263 | + } else { |
|
264 | + switch ($f[$i]) { |
|
265 | + case ';': |
|
266 | + // Remove triple semi-colon |
|
267 | + if ($i > 0 && ';' == $f[$i - 1] && $i + 1 < $len && ';' == $f[$i + 1]) { |
|
268 | + $f[$i] = $f[$i + 1] = '/'; |
|
269 | + } else { |
|
270 | + $code[++$j] = ';'; |
|
271 | + break; |
|
272 | + } |
|
273 | + |
|
274 | + case '/': |
|
275 | + if ('*' == $f[$i + 1]) { |
|
276 | + ++$i; |
|
277 | + $instr = '*'; |
|
278 | + |
|
279 | + if ($this->keepImportantComments && '!' == $f[$i + 1]) { |
|
280 | + ++$i; |
|
281 | + // no break here |
|
282 | + } else { |
|
283 | + break; |
|
284 | + } |
|
285 | + } elseif ('/' == $f[$i + 1]) { |
|
286 | + ++$i; |
|
287 | + $instr = '//'; |
|
288 | + break; |
|
289 | + } else { |
|
290 | + $a = $j && (' ' == $code[$j] || "\x7F" == $code[$j]) ? $code[$j - 1] : $code[$j]; |
|
291 | + if (false !== strpos('-!%&;<=>~:^+|,()*?[{} ', $a) |
|
292 | + || (false !== strpos('oenfd', $a) |
|
293 | + && preg_match( |
|
294 | + "'(?<![\$.a-zA-Z0-9_])(do|else|return|typeof|yield[ \x7F]?\*?)[ \x7F]?$'", |
|
295 | + substr($code, $j - 7, 8) |
|
296 | + )) |
|
297 | + ) { |
|
298 | + if (')' === $a && $j > 1) { |
|
299 | + $a = 1; |
|
300 | + $k = $j - (' ' == $code[$j] || "\x7F" == $code[$j]) - 1; |
|
301 | + while ($k >= 0 && $a) { |
|
302 | + if ('(' === $code[$k]) { |
|
303 | + --$a; |
|
304 | + } elseif (')' === $code[$k]) { |
|
305 | + ++$a; |
|
306 | + } |
|
307 | + --$k; |
|
308 | + } |
|
309 | + if (!preg_match("'(?<![\$.a-zA-Z0-9_])(if|for|while)[ \x7F]?$'", substr($code, 0, $k + 1))) { |
|
310 | + $code[++$j] = '/'; |
|
311 | + break; |
|
312 | + } |
|
313 | + } |
|
314 | + |
|
315 | + $key = "//''\"\"".$K++.$instr = "/'"; |
|
316 | + $a = $j; |
|
317 | + $code .= $key; |
|
318 | + while (isset($key[++$j - $a - 1])) { |
|
319 | + $code[$j] = $key[$j - $a - 1]; |
|
320 | + } |
|
321 | + --$j; |
|
322 | + isset($s) && ($s = implode('', $s)) && $cc_on && $this->restoreCc($s); |
|
323 | + $strings[$key] = array('/'); |
|
324 | + $s = &$strings[$key]; |
|
325 | + } else { |
|
326 | + $code[++$j] = '/'; |
|
327 | + } |
|
328 | + |
|
329 | + break; |
|
330 | + } |
|
331 | + |
|
332 | + case "'": |
|
333 | + case '"': |
|
334 | + $instr = $f[$i]; |
|
335 | + $key = "//''\"\"".$K++.('!' == $instr ? ']' : "'"); |
|
336 | + $a = $j; |
|
337 | + $code .= $key; |
|
338 | + while (isset($key[++$j - $a - 1])) { |
|
339 | + $code[$j] = $key[$j - $a - 1]; |
|
340 | + } |
|
341 | + --$j; |
|
342 | + isset($s) && ($s = implode('', $s)) && $cc_on && $this->restoreCc($s); |
|
343 | + $strings[$key] = array(); |
|
344 | + $s = &$strings[$key]; |
|
345 | + '!' == $instr && $s[] = "\r/*!"; |
|
346 | + |
|
347 | + break; |
|
348 | + |
|
349 | + case "\n": |
|
350 | + if ($j > 3) { |
|
351 | + if (' ' == $code[$j] || "\x7F" == $code[$j]) { |
|
352 | + --$j; |
|
353 | + } |
|
354 | + |
|
355 | + $code[++$j] = |
|
356 | + false !== strpos('kend+-', $code[$j - 1]) |
|
357 | + && preg_match( |
|
358 | + "'(?:\+\+|--|(?<![\$.a-zA-Z0-9_])(break|continue|return|yield[ \x7F]?\*?))[ \x7F]?$'", |
|
359 | + substr($code, $j - 8, 9) |
|
360 | + ) |
|
361 | + ? ';' : "\x7F"; |
|
362 | + |
|
363 | + break; |
|
364 | + } |
|
365 | + |
|
366 | + case "\t": $f[$i] = ' '; |
|
367 | + case ' ': |
|
368 | + if (!$j || ' ' == $code[$j] || "\x7F" == $code[$j]) { |
|
369 | + break; |
|
370 | + } |
|
371 | + |
|
372 | + default: |
|
373 | + $code[++$j] = $f[$i]; |
|
374 | + } |
|
375 | + } |
|
376 | + } |
|
377 | + |
|
378 | + isset($s) && ($s = implode('', $s)) && $cc_on && $this->restoreCc($s); |
|
379 | + unset($s); |
|
380 | + |
|
381 | + $code = substr($code, 0, $j + 1); |
|
382 | + $cc_on && $this->restoreCc($code, false); |
|
383 | + |
|
384 | + // Protect wanted spaces and remove unwanted ones |
|
385 | + $code = strtr($code, "\x7F", ' '); |
|
386 | + $code = str_replace('- -', "-\x7F-", $code); |
|
387 | + $code = str_replace('+ +', "+\x7F+", $code); |
|
388 | + $code = preg_replace("'(\d)\s+\.\s*([a-zA-Z\$_[(])'", "$1\x7F.$2", $code); |
|
389 | + $code = preg_replace("# ([-!%&;<=>~:.^+|,()*?[\]{}/']+)#", '$1', $code); |
|
390 | + $code = preg_replace("#([-!%&;<=>~:.^+|,()*?[\]{}/]+) #", '$1', $code); |
|
391 | + $cc_on && $code = preg_replace_callback("'//[^\'].*?@#3'", function ($m) { return strtr($m[0], ' ', "\x7F"); }, $code); |
|
392 | + |
|
393 | + // Replace new Array/Object by []/{} |
|
394 | + false !== strpos($code, 'new Array') && $code = preg_replace("'new Array(?:\(\)|([;\])},:]))'", '[]$1', $code); |
|
395 | + false !== strpos($code, 'new Object') && $code = preg_replace("'new Object(?:\(\)|([;\])},:]))'", '{}$1', $code); |
|
396 | + |
|
397 | + // Add missing semi-colons after curly braces |
|
398 | + // This adds more semi-colons than strictly needed, |
|
399 | + // but it seems that later gzipping is favorable to the repetition of "};" |
|
400 | + $code = preg_replace("'\}(?![:,;.()\[\]}\|&]|(else|catch|finally|while)[^\$.a-zA-Z0-9_])'", '};', $code); |
|
401 | + |
|
402 | + // Tag possible empty instruction for easy detection |
|
403 | + $code = preg_replace("'(?<![\$.a-zA-Z0-9_])if\('", '1#(', $code); |
|
404 | + $code = preg_replace("'(?<![\$.a-zA-Z0-9_])for\('", '2#(', $code); |
|
405 | + $code = preg_replace("'(?<![\$.a-zA-Z0-9_])while\('", '3#(', $code); |
|
406 | + |
|
407 | + $forPool = array(); |
|
408 | + $instrPool = array(); |
|
409 | + $s = 0; |
|
410 | + |
|
411 | + $f = array(); |
|
412 | + $j = -1; |
|
413 | + |
|
414 | + // Remove as much semi-colon as possible |
|
415 | + $len = strlen($code); |
|
416 | + for ($i = 0; $i < $len; ++$i) { |
|
417 | + switch ($code[$i]) { |
|
418 | + case '(': |
|
419 | + if ($j >= 0 && "\n" == $f[$j]) { |
|
420 | + $f[$j] = ';'; |
|
421 | + } |
|
422 | + |
|
423 | + ++$s; |
|
424 | + |
|
425 | + if ($i && '#' == $code[$i - 1]) { |
|
426 | + $instrPool[$s - 1] = 1; |
|
427 | + if ('2' == $code[$i - 2]) { |
|
428 | + $forPool[$s] = 1; |
|
429 | + } |
|
430 | + } |
|
431 | + |
|
432 | + $f[++$j] = '('; |
|
433 | + break; |
|
434 | + |
|
435 | + case ']': |
|
436 | + case ')': |
|
437 | + if ($i + 1 < $len && !isset($forPool[$s]) && !isset($instrPool[$s - 1]) && preg_match("'[a-zA-Z0-9_\$]'", $code[$i + 1])) { |
|
438 | + $f[$j] .= $code[$i]; |
|
439 | + $f[++$j] = "\n"; |
|
440 | + } else { |
|
441 | + $f[++$j] = $code[$i]; |
|
442 | + } |
|
443 | + |
|
444 | + if (')' == $code[$i]) { |
|
445 | + unset($forPool[$s]); |
|
446 | + --$s; |
|
447 | + } |
|
448 | + |
|
449 | + continue 2; |
|
450 | + |
|
451 | + case '}': |
|
452 | + if ("\n" == $f[$j]) { |
|
453 | + $f[$j] = '}'; |
|
454 | + } else { |
|
455 | + $f[++$j] = '}'; |
|
456 | + } |
|
457 | + break; |
|
458 | + |
|
459 | + case ';': |
|
460 | + if (isset($forPool[$s]) || isset($instrPool[$s])) { |
|
461 | + $f[++$j] = ';'; |
|
462 | + } elseif ($j >= 0 && "\n" != $f[$j] && ';' != $f[$j]) { |
|
463 | + $f[++$j] = "\n"; |
|
464 | + } |
|
465 | + |
|
466 | + break; |
|
467 | + |
|
468 | + case '#': |
|
469 | + switch ($f[$j]) { |
|
470 | + case '1': $f[$j] = 'if'; break 2; |
|
471 | + case '2': $f[$j] = 'for'; break 2; |
|
472 | + case '3': $f[$j] = 'while'; break 2; |
|
473 | + } |
|
474 | + |
|
475 | + case '['; |
|
476 | + if ($j >= 0 && "\n" == $f[$j]) { |
|
477 | + $f[$j] = ';'; |
|
478 | + } |
|
479 | + |
|
480 | + default: $f[++$j] = $code[$i]; |
|
481 | + } |
|
482 | + |
|
483 | + unset($instrPool[$s]); |
|
484 | + } |
|
485 | + |
|
486 | + $f = implode('', $f); |
|
487 | + $cc_on && $f = str_replace('@#3', "\r", $f); |
|
488 | + |
|
489 | + // Fix "else ;" empty instructions |
|
490 | + $f = preg_replace("'(?<![\$.a-zA-Z0-9_])else\n'", "\n", $f); |
|
491 | + |
|
492 | + $r1 = array( // keywords with a direct object |
|
493 | + 'case','delete','do','else','function','in','instanceof','of','break', |
|
494 | + 'new','return','throw','typeof','var','void','yield','let','if', |
|
495 | + 'const','get','set', |
|
496 | + ); |
|
497 | + |
|
498 | + $r2 = array( // keywords with a subject |
|
499 | + 'in','instanceof','of', |
|
500 | + ); |
|
501 | + |
|
502 | + // Fix missing semi-colons |
|
503 | + $f = preg_replace("'(?<!(?<![a-zA-Z0-9_\$])".implode(')(?<!(?<![a-zA-Z0-9_\$])', $r1).') (?!('.implode('|', $r2).")(?![a-zA-Z0-9_\$]))'", "\n", $f); |
|
504 | + $f = preg_replace("'(?<!(?<![a-zA-Z0-9_\$])do)(?<!(?<![a-zA-Z0-9_\$])else) if\('", "\nif(", $f); |
|
505 | + $f = preg_replace("'(?<=--|\+\+)(?<![a-zA-Z0-9_\$])(".implode('|', $r1).")(?![a-zA-Z0-9_\$])'", "\n$1", $f); |
|
506 | + $f = preg_replace("'(?<![a-zA-Z0-9_\$])for\neach\('", 'for each(', $f); |
|
507 | + $f = preg_replace("'(?<![a-zA-Z0-9_\$])\n(".implode('|', $r2).")(?![a-zA-Z0-9_\$])'", '$1', $f); |
|
508 | + |
|
509 | + // Merge strings |
|
510 | + if ($q["'"] > $q['"']) { |
|
511 | + $q = array($q[1], $q[0]); |
|
512 | + } |
|
513 | + $f = preg_replace("#//''\"\"[0-9]+'#", $q[0].'$0'.$q[0], $f); |
|
514 | + strpos($f, $q[0].'+'.$q[0]) && $f = str_replace($q[0].'+'.$q[0], '', $f); |
|
515 | + $len = count($strings); |
|
516 | + foreach ($strings as $r1 => &$r2) { |
|
517 | + $r2 = "/'" == substr($r1, -2) |
|
518 | + ? str_replace(array("\\'", '\\"'), array("'", '"'), $r2) |
|
519 | + : str_replace('\\'.$q[1], $q[1], $r2); |
|
520 | + } |
|
521 | + |
|
522 | + // Restore wanted spaces |
|
523 | + $f = strtr($f, "\x7F", ' '); |
|
524 | + |
|
525 | + return array($f, $strings); |
|
526 | + } |
|
527 | + |
|
528 | + protected function extractClosures($code) |
|
529 | + { |
|
530 | + $code = ';'.$code; |
|
531 | + |
|
532 | + $this->argFreq[-1] += substr_count($code, '}catch('); |
|
533 | + |
|
534 | + if ($this->argFreq[-1]) { |
|
535 | + // Special catch scope handling |
|
536 | + |
|
537 | + // FIXME: this implementation doesn't work with nested catch scopes who need |
|
538 | + // access to their parent's caught variable (but who needs that?). |
|
539 | + |
|
540 | + $f = preg_split("@}catch\(({$this->varRx})@", $code, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
541 | + |
|
542 | + $code = 'catch$scope$var'.mt_rand(); |
|
543 | + $this->specialVarRx = $this->specialVarRx ? '(?:'.$this->specialVarRx.'|'.preg_quote($code).')' : preg_quote($code); |
|
544 | + $i = count($f) - 1; |
|
545 | + |
|
546 | + while ($i) { |
|
547 | + $c = 1; |
|
548 | + $j = 0; |
|
549 | + $l = strlen($f[$i]); |
|
550 | + |
|
551 | + while ($c && $j < $l) { |
|
552 | + $s = $f[$i][$j++]; |
|
553 | + $c += '(' == $s ? 1 : (')' == $s ? -1 : 0); |
|
554 | + } |
|
555 | + |
|
556 | + if (!$c) { |
|
557 | + do { |
|
558 | + $s = $f[$i][$j++]; |
|
559 | + $c += '{' == $s ? 1 : ('}' == $s ? -1 : 0); |
|
560 | + } while ($c && $j < $l); |
|
561 | + } |
|
562 | + |
|
563 | + $c = preg_quote($f[$i - 1], '#'); |
|
564 | + $f[$i - 2] .= '}catch('.preg_replace("#([.,{]?)(?<![a-zA-Z0-9_\$@]){$c}\\b#", '$1'.$code, $f[$i - 1].substr($f[$i], 0, $j)).substr($f[$i], $j); |
|
565 | + |
|
566 | + unset($f[$i--], $f[$i--]); |
|
567 | + } |
|
568 | + |
|
569 | + $code = $f[0]; |
|
570 | + } |
|
571 | + |
|
572 | + $f = preg_split("'(?<![a-zA-Z0-9_\$])((?:function[ (]|get |set ).*?\{)'", $code, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
573 | + $i = count($f) - 1; |
|
574 | + $closures = array(); |
|
575 | + |
|
576 | + while ($i) { |
|
577 | + $c = 1; |
|
578 | + $j = 0; |
|
579 | + $l = strlen($f[$i]); |
|
580 | + |
|
581 | + while ($c && $j < $l) { |
|
582 | + $s = $f[$i][$j++]; |
|
583 | + $c += '{' == $s ? 1 : ('}' == $s ? -1 : 0); |
|
584 | + } |
|
585 | + |
|
586 | + switch (substr($f[$i - 2], -1)) { |
|
587 | + default: |
|
588 | + if (false !== $c = strpos($f[$i - 1], ' ', 8)) { |
|
589 | + break; |
|
590 | + } |
|
591 | + case false: case "\n": case ';': case '{': case '}': case ')': case ']': |
|
592 | + $c = strpos($f[$i - 1], '(', 4); |
|
593 | + } |
|
594 | + |
|
595 | + $l = "//''\"\"#$i'"; |
|
596 | + $code = substr($f[$i - 1], $c); |
|
597 | + $closures[$l] = $code.substr($f[$i], 0, $j); |
|
598 | + $f[$i - 2] .= substr($f[$i - 1], 0, $c).$l.substr($f[$i], $j); |
|
599 | + |
|
600 | + if ('(){' !== $code) { |
|
601 | + $j = substr_count($code, ','); |
|
602 | + do { |
|
603 | + isset($this->argFreq[$j]) ? ++$this->argFreq[$j] : $this->argFreq[$j] = 1; |
|
604 | + } while ($j--); |
|
605 | + } |
|
606 | + |
|
607 | + $i -= 2; |
|
608 | + } |
|
609 | + |
|
610 | + return array($f[0], $closures); |
|
611 | + } |
|
612 | + |
|
613 | + protected function makeVars($closure, &$tree, $key) |
|
614 | + { |
|
615 | + $tree['code'] = &$closure; |
|
616 | + $tree['nfe'] = false; |
|
617 | + $tree['used'] = array(); |
|
618 | + $tree['local'] = array(); |
|
619 | + |
|
620 | + // Replace multiple "var" declarations by a single one |
|
621 | + $closure = preg_replace_callback("'(?<=[\n\{\}])var [^\n\{\};]+(?:\nvar [^\n\{\};]+)+'", array($this, 'mergeVarDeclarations'), $closure); |
|
622 | + |
|
623 | + // Get all local vars (functions, arguments and "var" prefixed) |
|
624 | + |
|
625 | + $vars = &$tree['local']; |
|
626 | + |
|
627 | + if (preg_match("'^( [^(]*)?\((.*?)\)\{'", $closure, $v)) { |
|
628 | + if ($v[1]) { |
|
629 | + $vars[$tree['nfe'] = substr($v[1], 1)] = -1; |
|
630 | + $tree['parent']['local'][';'.$key] = &$vars[$tree['nfe']]; |
|
631 | + } |
|
632 | + |
|
633 | + if ($v[2]) { |
|
634 | + $i = 0; |
|
635 | + $v = explode(',', $v[2]); |
|
636 | + foreach ($v as $w) { |
|
637 | + $vars[$w] = $this->argFreq[$i++] - 1; // Give a bonus to argument variables |
|
638 | + } |
|
639 | + } |
|
640 | + } |
|
641 | + |
|
642 | + $v = preg_split("'(?<![\$.a-zA-Z0-9_])var '", $closure); |
|
643 | + if ($i = count($v) - 1) { |
|
644 | + $w = array(); |
|
645 | + |
|
646 | + while ($i) { |
|
647 | + $j = $c = 0; |
|
648 | + $l = strlen($v[$i]); |
|
649 | + |
|
650 | + while ($j < $l) { |
|
651 | + switch ($v[$i][$j]) { |
|
652 | + case '(': case '[': case '{': |
|
653 | + ++$c; |
|
654 | + break; |
|
655 | + |
|
656 | + case ')': case ']': case '}': |
|
657 | + if ($c-- <= 0) { |
|
658 | + break 2; |
|
659 | + } |
|
660 | + break; |
|
661 | + |
|
662 | + case ';': case "\n": |
|
663 | + if (!$c) { |
|
664 | + break 2; |
|
665 | + } |
|
666 | + |
|
667 | + default: |
|
668 | + $c || $w[] = $v[$i][$j]; |
|
669 | + } |
|
670 | + |
|
671 | + ++$j; |
|
672 | + } |
|
673 | + |
|
674 | + $w[] = ','; |
|
675 | + --$i; |
|
676 | + } |
|
677 | + |
|
678 | + $v = explode(',', implode('', $w)); |
|
679 | + foreach ($v as $w) { |
|
680 | + if (preg_match("'^{$this->varRx}'", $w, $v)) { |
|
681 | + isset($vars[$v[0]]) || $vars[$v[0]] = 0; |
|
682 | + } |
|
683 | + } |
|
684 | + } |
|
685 | + |
|
686 | + if (preg_match_all("@function ({$this->varRx})//''\"\"#@", $closure, $v)) { |
|
687 | + foreach ($v[1] as $w) { |
|
688 | + isset($vars[$w]) || $vars[$w] = 0; |
|
689 | + } |
|
690 | + } |
|
691 | + |
|
692 | + if ($this->argFreq[-1] && preg_match_all("@}catch\(({$this->varRx})@", $closure, $v)) { |
|
693 | + $v[0] = array(); |
|
694 | + foreach ($v[1] as $w) { |
|
695 | + isset($v[0][$w]) ? ++$v[0][$w] : $v[0][$w] = 1; |
|
696 | + } |
|
697 | + foreach ($v[0] as $w => $v) { |
|
698 | + $vars[$w] = $this->argFreq[-1] - $v; |
|
699 | + } |
|
700 | + } |
|
701 | + |
|
702 | + // Get all used vars, local and non-local |
|
703 | + |
|
704 | + $vars = &$tree['used']; |
|
705 | + |
|
706 | + if (preg_match_all("#([.,{]?(?:[gs]et )?)(?<![a-zA-Z0-9_\$])({$this->varRx})(:?)#", $closure, $w, PREG_SET_ORDER)) { |
|
707 | + foreach ($w as $k) { |
|
708 | + if (isset($k[1][0]) && (',' === $k[1][0] || '{' === $k[1][0])) { |
|
709 | + if (':' === $k[3]) { |
|
710 | + $k = '.'.$k[2]; |
|
711 | + } elseif ('get ' === substr($k[1], 1, 4) || 'set ' === substr($k[1], 1, 4)) { |
|
712 | + ++$this->charFreq[ord($k[1][1])]; // "g" or "s" |
|
713 | + ++$this->charFreq[101]; // "e" |
|
714 | + ++$this->charFreq[116]; // "t" |
|
715 | + $k = '.'.$k[2]; |
|
716 | + } else { |
|
717 | + $k = $k[2]; |
|
718 | + } |
|
719 | + } else { |
|
720 | + $k = $k[1].$k[2]; |
|
721 | + } |
|
722 | + |
|
723 | + isset($vars[$k]) ? ++$vars[$k] : $vars[$k] = 1; |
|
724 | + } |
|
725 | + } |
|
726 | + |
|
727 | + if (preg_match_all("#//''\"\"[0-9]+(?:['!]|/')#", $closure, $w)) { |
|
728 | + foreach ($w[0] as $a) { |
|
729 | + $v = "'" === substr($a, -1) && "/'" !== substr($a, -2) && $this->specialVarRx |
|
730 | + ? preg_split("#([.,{]?(?:[gs]et )?(?<![a-zA-Z0-9_\$@]){$this->specialVarRx}:?)#", $this->strings[$a], -1, PREG_SPLIT_DELIM_CAPTURE) |
|
731 | + : array($this->strings[$a]); |
|
732 | + $a = count($v); |
|
733 | + |
|
734 | + for ($i = 0; $i < $a; ++$i) { |
|
735 | + $k = $v[$i]; |
|
736 | + |
|
737 | + if (1 === $i % 2) { |
|
738 | + if (',' === $k[0] || '{' === $k[0]) { |
|
739 | + if (':' === substr($k, -1)) { |
|
740 | + $k = '.'.substr($k, 1, -1); |
|
741 | + } elseif ('get ' === substr($k, 1, 4) || 'set ' === substr($k, 1, 4)) { |
|
742 | + ++$this->charFreq[ord($k[1])]; // "g" or "s" |
|
743 | + ++$this->charFreq[101]; // "e" |
|
744 | + ++$this->charFreq[116]; // "t" |
|
745 | + $k = '.'.substr($k, 5); |
|
746 | + } else { |
|
747 | + $k = substr($k, 1); |
|
748 | + } |
|
749 | + } elseif (':' === substr($k, -1)) { |
|
750 | + $k = substr($k, 0, -1); |
|
751 | + } |
|
752 | + |
|
753 | + $w = &$tree; |
|
754 | + |
|
755 | + while (isset($w['parent']) && !(isset($w['used'][$k]) || isset($w['local'][$k]))) { |
|
756 | + $w = &$w['parent']; |
|
757 | + } |
|
758 | + |
|
759 | + (isset($w['used'][$k]) || isset($w['local'][$k])) && (isset($vars[$k]) ? ++$vars[$k] : $vars[$k] = 1); |
|
760 | + |
|
761 | + unset($w); |
|
762 | + } |
|
763 | + |
|
764 | + if (0 === $i % 2 || !isset($vars[$k])) { |
|
765 | + foreach (count_chars($v[$i], 1) as $k => $w) { |
|
766 | + $this->charFreq[$k] += $w; |
|
767 | + } |
|
768 | + } |
|
769 | + } |
|
770 | + } |
|
771 | + } |
|
772 | + |
|
773 | + // Propagate the usage number to parents |
|
774 | + |
|
775 | + foreach ($vars as $w => $a) { |
|
776 | + $k = &$tree; |
|
777 | + $chain = array(); |
|
778 | + do { |
|
779 | + $vars = &$k['local']; |
|
780 | + $chain[] = &$k; |
|
781 | + if (isset($vars[$w])) { |
|
782 | + unset($k['used'][$w]); |
|
783 | + if (isset($vars[$w])) { |
|
784 | + $vars[$w] += $a; |
|
785 | + } else { |
|
786 | + $vars[$w] = $a; |
|
787 | + } |
|
788 | + $a = false; |
|
789 | + break; |
|
790 | + } |
|
791 | + } while ($k['parent'] && $k = &$k['parent']); |
|
792 | + |
|
793 | + if ($a && !$k['parent']) { |
|
794 | + if (isset($vars[$w])) { |
|
795 | + $vars[$w] += $a; |
|
796 | + } else { |
|
797 | + $vars[$w] = $a; |
|
798 | + } |
|
799 | + } |
|
800 | + |
|
801 | + if (isset($tree['used'][$w]) && isset($vars[$w])) { |
|
802 | + foreach ($chain as &$b) { |
|
803 | + isset($b['local'][$w]) || $b['used'][$w] = &$vars[$w]; |
|
804 | + } |
|
805 | + } |
|
806 | + } |
|
807 | + |
|
808 | + // Analyse childs |
|
809 | + |
|
810 | + $tree['childs'] = array(); |
|
811 | + $vars = &$tree['childs']; |
|
812 | + |
|
813 | + if (preg_match_all("@//''\"\"#[0-9]+'@", $closure, $w)) { |
|
814 | + foreach ($w[0] as $a) { |
|
815 | + $vars[$a] = array('parent' => &$tree); |
|
816 | + $this->makeVars($this->closures[$a], $vars[$a], $a); |
|
817 | + } |
|
818 | + } |
|
819 | + } |
|
820 | + |
|
821 | + protected function mergeVarDeclarations($m) |
|
822 | + { |
|
823 | + return str_replace("\nvar ", ',', $m[0]); |
|
824 | + } |
|
825 | + |
|
826 | + protected function renameVars(&$tree, $root) |
|
827 | + { |
|
828 | + if ($root) { |
|
829 | + $tree['local'] += $tree['used']; |
|
830 | + $tree['used'] = array(); |
|
831 | + |
|
832 | + foreach ($tree['local'] as $k => $v) { |
|
833 | + if ('.' == $k[0]) { |
|
834 | + $k = substr($k, 1); |
|
835 | + } |
|
836 | + |
|
837 | + if ('true' === $k) { |
|
838 | + $this->charFreq[48] += $v; |
|
839 | + } elseif ('false' === $k) { |
|
840 | + $this->charFreq[49] += $v; |
|
841 | + } elseif (!$this->specialVarRx || !preg_match("#^{$this->specialVarRx}$#", $k)) { |
|
842 | + foreach (count_chars($k, 1) as $k => $w) { |
|
843 | + $this->charFreq[$k] += $w * $v; |
|
844 | + } |
|
845 | + } elseif (2 == strlen($k)) { |
|
846 | + $tree['used'][] = $k[1]; |
|
847 | + } |
|
848 | + } |
|
849 | + |
|
850 | + $this->charFreq = $this->rsort($this->charFreq); |
|
851 | + |
|
852 | + $this->str0 = ''; |
|
853 | + $this->str1 = ''; |
|
854 | + |
|
855 | + foreach ($this->charFreq as $k => $v) { |
|
856 | + if (!$v) { |
|
857 | + break; |
|
858 | + } |
|
859 | + |
|
860 | + $v = chr($k); |
|
861 | + |
|
862 | + if ((64 < $k && $k < 91) || (96 < $k && $k < 123)) { // A-Z a-z |
|
863 | + $this->str0 .= $v; |
|
864 | + $this->str1 .= $v; |
|
865 | + } elseif (47 < $k && $k < 58) { // 0-9 |
|
866 | + $this->str1 .= $v; |
|
867 | + } |
|
868 | + } |
|
869 | + |
|
870 | + if ('' === $this->str0) { |
|
871 | + $this->str0 = 'claspemitdbfrugnjvhowkxqyzCLASPEMITDBFRUGNJVHOWKXQYZ'; |
|
872 | + $this->str1 = $this->str0.'0123456789'; |
|
873 | + } |
|
874 | + |
|
875 | + foreach ($tree['local'] as $var => $root) { |
|
876 | + if ('.' != substr($var, 0, 1) && isset($tree['local'][".{$var}"])) { |
|
877 | + $tree['local'][$var] += $tree['local'][".{$var}"]; |
|
878 | + } |
|
879 | + } |
|
880 | + |
|
881 | + foreach ($tree['local'] as $var => $root) { |
|
882 | + if ('.' == substr($var, 0, 1) && isset($tree['local'][substr($var, 1)])) { |
|
883 | + $tree['local'][$var] = $tree['local'][substr($var, 1)]; |
|
884 | + } |
|
885 | + } |
|
886 | + |
|
887 | + $tree['local'] = $this->rsort($tree['local']); |
|
888 | + |
|
889 | + foreach ($tree['local'] as $var => $root) { |
|
890 | + switch (substr($var, 0, 1)) { |
|
891 | + case '.': |
|
892 | + if (!isset($tree['local'][substr($var, 1)])) { |
|
893 | + $tree['local'][$var] = '#'.($this->specialVarRx && 3 < strlen($var) && preg_match("'^\.{$this->specialVarRx}$'", $var) ? $this->getNextName($tree).'$' : substr($var, 1)); |
|
894 | + } |
|
895 | + break; |
|
896 | + |
|
897 | + case ';': $tree['local'][$var] = 0 === $root ? '' : $this->getNextName($tree); |
|
898 | + case '#': break; |
|
899 | + |
|
900 | + default: |
|
901 | + $root = $this->specialVarRx && 2 < strlen($var) && preg_match("'^{$this->specialVarRx}$'", $var) ? $this->getNextName($tree).'$' : $var; |
|
902 | + $tree['local'][$var] = $root; |
|
903 | + if (isset($tree['local'][".{$var}"])) { |
|
904 | + $tree['local'][".{$var}"] = '#'.$root; |
|
905 | + } |
|
906 | + } |
|
907 | + } |
|
908 | + |
|
909 | + foreach ($tree['local'] as $var => $root) { |
|
910 | + $tree['local'][$var] = preg_replace("'^#'", '.', $tree['local'][$var]); |
|
911 | + } |
|
912 | + } else { |
|
913 | + $tree['local'] = $this->rsort($tree['local']); |
|
914 | + if (false !== $tree['nfe']) { |
|
915 | + $tree['used'][] = $tree['local'][$tree['nfe']]; |
|
916 | + } |
|
917 | + |
|
918 | + foreach ($tree['local'] as $var => $root) { |
|
919 | + if ($tree['nfe'] !== $var) { |
|
920 | + $tree['local'][$var] = 0 === $root ? '' : $this->getNextName($tree); |
|
921 | + } |
|
922 | + } |
|
923 | + } |
|
924 | + |
|
925 | + $this->local_tree = &$tree['local']; |
|
926 | + $this->used_tree = &$tree['used']; |
|
927 | + |
|
928 | + $tree['code'] = preg_replace_callback("#[.,{ ]?(?:[gs]et )?(?<![a-zA-Z0-9_\$@]){$this->varRx}:?#", array($this, 'getNewName'), $tree['code']); |
|
929 | + |
|
930 | + if ($this->specialVarRx && preg_match_all("#//''\"\"[0-9]+'#", $tree['code'], $b)) { |
|
931 | + foreach ($b[0] as $a) { |
|
932 | + $this->strings[$a] = preg_replace_callback( |
|
933 | + "#[.,{]?(?:[gs]et )?(?<![a-zA-Z0-9_\$@]){$this->specialVarRx}:?#", |
|
934 | + array($this, 'getNewName'), |
|
935 | + $this->strings[$a] |
|
936 | + ); |
|
937 | + } |
|
938 | + } |
|
939 | + |
|
940 | + foreach ($tree['childs'] as $a => &$b) { |
|
941 | + $this->renameVars($b, false); |
|
942 | + $tree['code'] = str_replace($a, $b['code'], $tree['code']); |
|
943 | + unset($tree['childs'][$a]); |
|
944 | + } |
|
945 | + } |
|
946 | + |
|
947 | + protected function getNewName($m) |
|
948 | + { |
|
949 | + $m = $m[0]; |
|
950 | + |
|
951 | + $pre = '.' === $m[0] ? '.' : ''; |
|
952 | + $post = ''; |
|
953 | + |
|
954 | + if (',' === $m[0] || '{' === $m[0] || ' ' === $m[0]) { |
|
955 | + $pre = $m[0]; |
|
956 | + |
|
957 | + if (':' === substr($m, -1)) { |
|
958 | + $post = ':'; |
|
959 | + $m = (' ' !== $m[0] ? '.' : '').substr($m, 1, -1); |
|
960 | + } elseif ('get ' === substr($m, 1, 4) || 'set ' === substr($m, 1, 4)) { |
|
961 | + $pre .= substr($m, 1, 4); |
|
962 | + $m = '.'.substr($m, 5); |
|
963 | + } else { |
|
964 | + $m = substr($m, 1); |
|
965 | + } |
|
966 | + } elseif (':' === substr($m, -1)) { |
|
967 | + $post = ':'; |
|
968 | + $m = substr($m, 0, -1); |
|
969 | + } |
|
970 | + |
|
971 | + $post = (isset($this->reserved[$m]) |
|
972 | + ? ('true' === $m ? '!0' : ('false' === $m ? '!1' : $m)) |
|
973 | + : ( |
|
974 | + isset($this->local_tree[$m]) |
|
975 | + ? $this->local_tree[$m] |
|
976 | + : ( |
|
977 | + isset($this->used_tree[$m]) |
|
978 | + ? $this->used_tree[$m] |
|
979 | + : $m |
|
980 | + ) |
|
981 | + ) |
|
982 | + ).$post; |
|
983 | + |
|
984 | + return '' === $post ? '' : ($pre.('.' === $post[0] ? substr($post, 1) : $post)); |
|
985 | + } |
|
986 | + |
|
987 | + protected function getNextName(&$tree = array(), &$counter = false) |
|
988 | + { |
|
989 | + if (false === $counter) { |
|
990 | + $counter = &$tree['counter']; |
|
991 | + isset($counter) || $counter = -1; |
|
992 | + $exclude = array_flip($tree['used']); |
|
993 | + } else { |
|
994 | + $exclude = $tree; |
|
995 | + } |
|
996 | + |
|
997 | + ++$counter; |
|
998 | + |
|
999 | + $len0 = strlen($this->str0); |
|
1000 | + $len1 = strlen($this->str0); |
|
1001 | + |
|
1002 | + $name = $this->str0[$counter % $len0]; |
|
1003 | + |
|
1004 | + $i = intval($counter / $len0) - 1; |
|
1005 | + while ($i >= 0) { |
|
1006 | + $name .= $this->str1[ $i % $len1 ]; |
|
1007 | + $i = intval($i / $len1) - 1; |
|
1008 | + } |
|
1009 | + |
|
1010 | + return !(isset($this->reserved[$name]) || isset($exclude[$name])) ? $name : $this->getNextName($exclude, $counter); |
|
1011 | + } |
|
1012 | + |
|
1013 | + protected function restoreCc(&$s, $lf = true) |
|
1014 | + { |
|
1015 | + $lf && $s = str_replace('@#3', '', $s); |
|
1016 | + |
|
1017 | + $s = str_replace('@#1', '@*/', $s); |
|
1018 | + $s = str_replace('2#@', '//@', $s); |
|
1019 | + $s = str_replace('1#@', '/*@', $s); |
|
1020 | + $s = str_replace('##', '#', $s); |
|
1021 | + } |
|
1022 | + |
|
1023 | + private function rsort($array) |
|
1024 | + { |
|
1025 | + if (!$array) { |
|
1026 | + return $array; |
|
1027 | + } |
|
1028 | + |
|
1029 | + $i = 0; |
|
1030 | + $tuples = array(); |
|
1031 | + foreach ($array as $k => &$v) { |
|
1032 | + $tuples[] = array(++$i, $k, &$v); |
|
1033 | + } |
|
1034 | + |
|
1035 | + usort($tuples, function ($a, $b) { |
|
1036 | + if ($b[2] > $a[2]) { |
|
1037 | + return 1; |
|
1038 | + } |
|
1039 | + if ($b[2] < $a[2]) { |
|
1040 | + return -1; |
|
1041 | + } |
|
1042 | + if ($b[0] > $a[0]) { |
|
1043 | + return -1; |
|
1044 | + } |
|
1045 | + if ($b[0] < $a[0]) { |
|
1046 | + return 1; |
|
1047 | + } |
|
1048 | + |
|
1049 | + return 0; |
|
1050 | + }); |
|
1051 | + |
|
1052 | + $array = array(); |
|
1053 | + |
|
1054 | + foreach ($tuples as $t) { |
|
1055 | + $array[$t[1]] = &$t[2]; |
|
1056 | + } |
|
1057 | + |
|
1058 | + return $array; |
|
1059 | + } |
|
1060 | 1060 | } |
1061 | 1061 | |
1062 | 1062 | ?> |
@@ -23,179 +23,179 @@ |
||
23 | 23 | |
24 | 24 | class Lang { |
25 | 25 | |
26 | - protected static $supported_languages = array(); |
|
27 | - protected static $initialized = false; |
|
26 | + protected static $supported_languages = array(); |
|
27 | + protected static $initialized = false; |
|
28 | 28 | |
29 | - //https://paulund.co.uk/auto-detect-browser-language-in-php |
|
29 | + //https://paulund.co.uk/auto-detect-browser-language-in-php |
|
30 | 30 | |
31 | - public static function getPrefLangToken () : string { |
|
32 | - if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
|
33 | - $_SERVER['HTTP_ACCEPT_LANGUAGE'] = Settings::get("default_lang"); |
|
34 | - } |
|
31 | + public static function getPrefLangToken () : string { |
|
32 | + if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
|
33 | + $_SERVER['HTTP_ACCEPT_LANGUAGE'] = Settings::get("default_lang"); |
|
34 | + } |
|
35 | 35 | |
36 | - return substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); |
|
37 | - } |
|
36 | + return substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); |
|
37 | + } |
|
38 | 38 | |
39 | - public static function getLangToken (array $supported_lang_tokens) : string { |
|
40 | - //http://php.net/manual/fa/function.http-negotiate-language.php |
|
39 | + public static function getLangToken (array $supported_lang_tokens) : string { |
|
40 | + //http://php.net/manual/fa/function.http-negotiate-language.php |
|
41 | 41 | |
42 | - //https://stackoverflow.com/questions/6038236/using-the-php-http-accept-language-server-variable |
|
42 | + //https://stackoverflow.com/questions/6038236/using-the-php-http-accept-language-server-variable |
|
43 | 43 | |
44 | - //https://stackoverflow.com/questions/3770513/detect-browser-language-in-php |
|
44 | + //https://stackoverflow.com/questions/3770513/detect-browser-language-in-php |
|
45 | 45 | |
46 | - return self::prefered_language($supported_lang_tokens);//http_negotiate_language($supported_lang_tokens); |
|
47 | - } |
|
46 | + return self::prefered_language($supported_lang_tokens);//http_negotiate_language($supported_lang_tokens); |
|
47 | + } |
|
48 | 48 | |
49 | - public static function loadSupportedLangs () { |
|
50 | - if (Cache::contains("supported-languages", "list")) { |
|
51 | - self::$supported_languages = Cache::get("supported-languages", "list"); |
|
52 | - } else { |
|
53 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}supported_languages`; "); |
|
49 | + public static function loadSupportedLangs () { |
|
50 | + if (Cache::contains("supported-languages", "list")) { |
|
51 | + self::$supported_languages = Cache::get("supported-languages", "list"); |
|
52 | + } else { |
|
53 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}supported_languages`; "); |
|
54 | 54 | |
55 | - $array = array(); |
|
55 | + $array = array(); |
|
56 | 56 | |
57 | - foreach ($rows as $row) { |
|
58 | - $array[$row['lang_token']] = $row; |
|
59 | - } |
|
57 | + foreach ($rows as $row) { |
|
58 | + $array[$row['lang_token']] = $row; |
|
59 | + } |
|
60 | 60 | |
61 | - //cache values |
|
62 | - Cache::put("supported-languages", "list", $array); |
|
61 | + //cache values |
|
62 | + Cache::put("supported-languages", "list", $array); |
|
63 | 63 | |
64 | - self::$supported_languages = $array; |
|
65 | - } |
|
64 | + self::$supported_languages = $array; |
|
65 | + } |
|
66 | 66 | |
67 | - self::$initialized = true; |
|
68 | - } |
|
67 | + self::$initialized = true; |
|
68 | + } |
|
69 | 69 | |
70 | - protected static function loadIfAbsent () { |
|
71 | - if (!self::$initialized) { |
|
72 | - self::loadSupportedLangs(); |
|
73 | - } |
|
74 | - } |
|
70 | + protected static function loadIfAbsent () { |
|
71 | + if (!self::$initialized) { |
|
72 | + self::loadSupportedLangs(); |
|
73 | + } |
|
74 | + } |
|
75 | 75 | |
76 | - public static function listSupportedLangTokens () { |
|
77 | - //load tokens, if not initialized |
|
78 | - self::loadIfAbsent(); |
|
76 | + public static function listSupportedLangTokens () { |
|
77 | + //load tokens, if not initialized |
|
78 | + self::loadIfAbsent(); |
|
79 | 79 | |
80 | - $keys = array_keys(self::$supported_languages); |
|
80 | + $keys = array_keys(self::$supported_languages); |
|
81 | 81 | |
82 | - //get default language |
|
83 | - $default_lang = Settings::get("default_lang"); |
|
82 | + //get default language |
|
83 | + $default_lang = Settings::get("default_lang"); |
|
84 | 84 | |
85 | - if (!in_array($default_lang, $keys)) { |
|
86 | - throw new IllegalStateException("default language (in global settings) isnt a supported language"); |
|
87 | - } |
|
85 | + if (!in_array($default_lang, $keys)) { |
|
86 | + throw new IllegalStateException("default language (in global settings) isnt a supported language"); |
|
87 | + } |
|
88 | 88 | |
89 | - //remove element from array |
|
90 | - if (($key = array_search($default_lang, $keys)) !== false) { |
|
91 | - unset($keys[$key]); |
|
92 | - } |
|
89 | + //remove element from array |
|
90 | + if (($key = array_search($default_lang, $keys)) !== false) { |
|
91 | + unset($keys[$key]); |
|
92 | + } |
|
93 | 93 | |
94 | - //add as first element |
|
95 | - array_unshift($keys, $default_lang); |
|
94 | + //add as first element |
|
95 | + array_unshift($keys, $default_lang); |
|
96 | 96 | |
97 | - return $keys; |
|
98 | - } |
|
97 | + return $keys; |
|
98 | + } |
|
99 | 99 | |
100 | - public static function addLang (string $token, string $title) { |
|
101 | - Database::getInstance()->execute("INSERT INTO `{praefix}supported_languages` ( |
|
100 | + public static function addLang (string $token, string $title) { |
|
101 | + Database::getInstance()->execute("INSERT INTO `{praefix}supported_languages` ( |
|
102 | 102 | `lang_token`, `title` |
103 | 103 | ) VALUES ( |
104 | 104 | :token, :title |
105 | 105 | ); ", array( |
106 | - 'token' => $token, |
|
107 | - 'title' => $title |
|
108 | - )); |
|
106 | + 'token' => $token, |
|
107 | + 'title' => $title |
|
108 | + )); |
|
109 | 109 | |
110 | - //clear local in-memory cache |
|
111 | - self::$initialized = false; |
|
110 | + //clear local in-memory cache |
|
111 | + self::$initialized = false; |
|
112 | 112 | |
113 | - //clear cache |
|
114 | - Cache::clear("supported-languages"); |
|
115 | - } |
|
113 | + //clear cache |
|
114 | + Cache::clear("supported-languages"); |
|
115 | + } |
|
116 | 116 | |
117 | - public static function addLangOrUpdate (string $token, string $title) { |
|
118 | - Database::getInstance()->execute("INSERT INTO `{praefix}supported_languages` ( |
|
117 | + public static function addLangOrUpdate (string $token, string $title) { |
|
118 | + Database::getInstance()->execute("INSERT INTO `{praefix}supported_languages` ( |
|
119 | 119 | `lang_token`, `title` |
120 | 120 | ) VALUES ( |
121 | 121 | :token, :title |
122 | 122 | ) ON DUPLICATE KEY UPDATE `title` = :title; ", array( |
123 | - 'token' => $token, |
|
124 | - 'title' => $title |
|
125 | - )); |
|
126 | - |
|
127 | - //clear local in-memory cache |
|
128 | - self::$initialized = false; |
|
129 | - |
|
130 | - //clear cache |
|
131 | - Cache::clear("supported-languages"); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * determine which language out of an available set the user prefers most |
|
136 | - * |
|
137 | - * @param $available_languages array with language-tag-strings (must be lowercase) that are available |
|
138 | - * @param $http_accept_language a HTTP_ACCEPT_LANGUAGE string (read from $_SERVER['HTTP_ACCEPT_LANGUAGE'] if left out) |
|
139 | - */ |
|
140 | - |
|
141 | - /** |
|
142 | - * determine which language out of an available set the user prefers most |
|
143 | - * |
|
144 | - * @param $available_languages array with language-tag-strings (must be lowercase) that are available |
|
145 | - * @param $http_accept_language string HTTP_ACCEPT_LANGUAGE string (read from $_SERVER['HTTP_ACCEPT_LANGUAGE'] if left out) |
|
146 | - * |
|
147 | - * @link http://www.theserverpages.com/php/manual/en/function.http-negotiate-language.php |
|
148 | - * |
|
149 | - * @return prefered language |
|
150 | - */ |
|
151 | - protected static function prefered_language (array $available_languages, string $http_accept_language = "auto") : string { |
|
152 | - if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
|
153 | - $_SERVER['HTTP_ACCEPT_LANGUAGE'] = Settings::get("default_lang"); |
|
154 | - } |
|
155 | - |
|
156 | - // if $http_accept_language was left out, read it from the HTTP-Header |
|
157 | - if ($http_accept_language == "auto") $http_accept_language = $_SERVER['HTTP_ACCEPT_LANGUAGE']; |
|
158 | - |
|
159 | - // standard for HTTP_ACCEPT_LANGUAGE is defined under |
|
160 | - // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4 |
|
161 | - // pattern to find is therefore something like this: |
|
162 | - // 1#( language-range [ ";" "q" "=" qvalue ] ) |
|
163 | - // where: |
|
164 | - // language-range = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" ) |
|
165 | - // qvalue = ( "0" [ "." 0*3DIGIT ] ) |
|
166 | - // | ( "1" [ "." 0*3("0") ] ) |
|
167 | - preg_match_all("/([[:alpha:]]{1,8})(-([[:alpha:]|-]{1,8}))?" . |
|
168 | - "(\s*;\s*q\s*=\s*(1\.0{0,3}|0\.\d{0,3}))?\s*(,|$)/i", |
|
169 | - $http_accept_language, $hits, PREG_SET_ORDER); |
|
170 | - |
|
171 | - // default language (in case of no hits) is the first in the array |
|
172 | - $bestlang = $available_languages[0]; |
|
173 | - $bestqval = 0; |
|
174 | - |
|
175 | - foreach ($hits as $arr) { |
|
176 | - // read data from the array of this hit |
|
177 | - $langprefix = strtolower ($arr[1]); |
|
178 | - if (!empty($arr[3])) { |
|
179 | - $langrange = strtolower ($arr[3]); |
|
180 | - $language = $langprefix . "-" . $langrange; |
|
181 | - } |
|
182 | - else $language = $langprefix; |
|
183 | - $qvalue = 1.0; |
|
184 | - if (!empty($arr[5])) $qvalue = floatval($arr[5]); |
|
185 | - |
|
186 | - // find q-maximal language |
|
187 | - if (in_array($language,$available_languages) && ($qvalue > $bestqval)) { |
|
188 | - $bestlang = $language; |
|
189 | - $bestqval = $qvalue; |
|
190 | - } |
|
191 | - // if no direct hit, try the prefix only but decrease q-value by 10% (as http_negotiate_language does) |
|
192 | - else if (in_array($langprefix,$available_languages) && (($qvalue*0.9) > $bestqval)) { |
|
193 | - $bestlang = $langprefix; |
|
194 | - $bestqval = $qvalue*0.9; |
|
195 | - } |
|
196 | - } |
|
197 | - return $bestlang; |
|
198 | - } |
|
123 | + 'token' => $token, |
|
124 | + 'title' => $title |
|
125 | + )); |
|
126 | + |
|
127 | + //clear local in-memory cache |
|
128 | + self::$initialized = false; |
|
129 | + |
|
130 | + //clear cache |
|
131 | + Cache::clear("supported-languages"); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * determine which language out of an available set the user prefers most |
|
136 | + * |
|
137 | + * @param $available_languages array with language-tag-strings (must be lowercase) that are available |
|
138 | + * @param $http_accept_language a HTTP_ACCEPT_LANGUAGE string (read from $_SERVER['HTTP_ACCEPT_LANGUAGE'] if left out) |
|
139 | + */ |
|
140 | + |
|
141 | + /** |
|
142 | + * determine which language out of an available set the user prefers most |
|
143 | + * |
|
144 | + * @param $available_languages array with language-tag-strings (must be lowercase) that are available |
|
145 | + * @param $http_accept_language string HTTP_ACCEPT_LANGUAGE string (read from $_SERVER['HTTP_ACCEPT_LANGUAGE'] if left out) |
|
146 | + * |
|
147 | + * @link http://www.theserverpages.com/php/manual/en/function.http-negotiate-language.php |
|
148 | + * |
|
149 | + * @return prefered language |
|
150 | + */ |
|
151 | + protected static function prefered_language (array $available_languages, string $http_accept_language = "auto") : string { |
|
152 | + if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
|
153 | + $_SERVER['HTTP_ACCEPT_LANGUAGE'] = Settings::get("default_lang"); |
|
154 | + } |
|
155 | + |
|
156 | + // if $http_accept_language was left out, read it from the HTTP-Header |
|
157 | + if ($http_accept_language == "auto") $http_accept_language = $_SERVER['HTTP_ACCEPT_LANGUAGE']; |
|
158 | + |
|
159 | + // standard for HTTP_ACCEPT_LANGUAGE is defined under |
|
160 | + // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4 |
|
161 | + // pattern to find is therefore something like this: |
|
162 | + // 1#( language-range [ ";" "q" "=" qvalue ] ) |
|
163 | + // where: |
|
164 | + // language-range = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" ) |
|
165 | + // qvalue = ( "0" [ "." 0*3DIGIT ] ) |
|
166 | + // | ( "1" [ "." 0*3("0") ] ) |
|
167 | + preg_match_all("/([[:alpha:]]{1,8})(-([[:alpha:]|-]{1,8}))?" . |
|
168 | + "(\s*;\s*q\s*=\s*(1\.0{0,3}|0\.\d{0,3}))?\s*(,|$)/i", |
|
169 | + $http_accept_language, $hits, PREG_SET_ORDER); |
|
170 | + |
|
171 | + // default language (in case of no hits) is the first in the array |
|
172 | + $bestlang = $available_languages[0]; |
|
173 | + $bestqval = 0; |
|
174 | + |
|
175 | + foreach ($hits as $arr) { |
|
176 | + // read data from the array of this hit |
|
177 | + $langprefix = strtolower ($arr[1]); |
|
178 | + if (!empty($arr[3])) { |
|
179 | + $langrange = strtolower ($arr[3]); |
|
180 | + $language = $langprefix . "-" . $langrange; |
|
181 | + } |
|
182 | + else $language = $langprefix; |
|
183 | + $qvalue = 1.0; |
|
184 | + if (!empty($arr[5])) $qvalue = floatval($arr[5]); |
|
185 | + |
|
186 | + // find q-maximal language |
|
187 | + if (in_array($language,$available_languages) && ($qvalue > $bestqval)) { |
|
188 | + $bestlang = $language; |
|
189 | + $bestqval = $qvalue; |
|
190 | + } |
|
191 | + // if no direct hit, try the prefix only but decrease q-value by 10% (as http_negotiate_language does) |
|
192 | + else if (in_array($langprefix,$available_languages) && (($qvalue*0.9) > $bestqval)) { |
|
193 | + $bestlang = $langprefix; |
|
194 | + $bestqval = $qvalue*0.9; |
|
195 | + } |
|
196 | + } |
|
197 | + return $bestlang; |
|
198 | + } |
|
199 | 199 | |
200 | 200 | } |
201 | 201 |