-
Notifications
You must be signed in to change notification settings - Fork 9
/
pi.switchee.php
405 lines (343 loc) · 10.7 KB
/
pi.switchee.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
$plugin_info = array(
'pi_name' => 'Switchee',
'pi_version' =>'3.0.1',
'pi_author' =>'Mark Croxton',
'pi_author_url' => 'http://www.hallmark-design.co.uk/',
'pi_description' => 'Switch/case control structure for templates',
'pi_usage' => Switchee::usage()
);
class Switchee {
public $return_data = '';
private $_ph = array();
/**
* Constructor
*
* Evaluates case values and extracts the content of the
* first case that matches the variable parameter
*
* @access public
* @return void
*/
public function __construct()
{
// reduce the PCRE default recursion limit to a safe level to prevent a server crash
// (segmentation fault) when the available stack is exhausted before recursion limit reached
// Apache *nix executable stack size is 8Mb, so safe size is 16777
// Apache Win32 executable stack size is 256Kb, so safe size is 524
ini_set('pcre.recursion_limit', '16777');
// PCRE default backtrack limit is low on PHP <5.3.6
// Increase it to the default value in newer versions of PHP
ini_set('pcre.backtrack_limit', '1000000');
// fetch the tagdata
$tagdata = ee()->TMPL->tagdata;
// the variable we want to find
$var = ee()->TMPL->fetch_param('variable', '');
$match_all = ( ee()->TMPL->fetch_param('match') == 'all' );
// debug?
$debug = (bool) preg_match('/1|on|yes|y/i', ee()->TMPL->fetch_param('debug'));
// register POST and GET values
if (strncmp($var, 'get:', 4) == 0)
{
$var = filter_var(ee()->input->get(substr($var, 4)), FILTER_SANITIZE_STRING);
}
if (strncmp($var, 'post:', 5) == 0)
{
$var = filter_var(ee()->input->post(substr($var, 5)), FILTER_SANITIZE_STRING);
}
// register variables created by Stash
if (strncmp($var, 'stash:', 6) == 0)
{
$var = substr($var, 6);
if ( isset(ee()->session->cache['stash']) && isset(ee()->session->cache['stash'][$var]))
{
// first look in the native stash variables array, for speed's sake
$var = ee()->session->cache['stash'][$var];
}
else
{
// we'll need to invoke Stash itself
if ( ! class_exists('Stash'))
{
include_once PATH_THIRD . 'stash/mod.stash.php';
}
$stash = new Stash(TRUE);
$var = (string) $stash->get($var);
}
}
// register global vars
if (strncmp($var, 'global:', 7) == 0)
{
$var = substr($var, 7);
if (array_key_exists($var, ee()->config->_global_vars))
{
$var = ee()->config->_global_vars[$var];
}
else
{
// global has not been parsed yet, so we'll do it the hard way (this adds some overhead)
$var = ee()->TMPL->parse_globals(LD.$var.RD);
}
}
// log
if ($debug)
{
ee()->TMPL->log_item("Switchee: evaluating variable {$var}");
}
// replace content inside nested tags with indexed placeholders, storing it in an array for later
// here's the tricky bit - we only match outer tags
/*
$pattern = '/{switchee(?>(?!{\/?switchee).|(?R))*{\/switchee/si';
*/
// more memory efficient version of the above...
$pattern = '#{switchee(?>(?:[^{]++|{(?!\/?switchee[^}]*}))+|(?R))*{\/switchee#si';
$tagdata = preg_replace_callback($pattern, array(get_class($this), '_placeholders'), $tagdata);
// returns NULL on PCRE error
if ($tagdata === NULL && $debug)
{
$this->_pcre_error();
}
// loop through case parameters and find a case pair value that matches our variable
$index = 0;
// now we need to generate a new array of tag pairs for our tagdata
if (version_compare(APP_VER, '4.0', '>='))
{
$tag_vars = ee('Variables/Parser')->extractVariables($tagdata);
}
else
{
$tag_vars = ee()->functions->assign_variables($tagdata);
}
$has_match = false;
$temp_return_data = '';
$default = '';
foreach ($tag_vars['var_pair'] as $key => $val)
{
// is this tag pair a case?
if (preg_match('/^case/', $key))
{
// index of the case tag pair we're looking at
$index++;
// replace any regex in the case values with a marker
$tagdata = str_replace($key, 'case_'.$index, $tagdata);
// get the position of the content inside the case being evaluated
$starts_at = strpos($tagdata, "{case_".$index."}") + strlen("{case_".$index."}");
$ends_at = strpos($tagdata, "{/case}", $starts_at);
if(isset($val['value']))
{
$val_array = array();
if (stristr($val['value'], '|'))
{
$val_array = explode('|', $val['value']);
}
else
{
$val_array[] = $val['value'];
}
// loop through each value and look for a match
foreach ($val_array as $case_index => $case_value)
{
// convert '' and "" to an actual empty string
if ($case_value == "''" || $case_value == '""')
{
$case_value = '';
}
// decode any encoded characters
if (version_compare(APP_VER, '3.0', '>='))
{
$case_value = ee('Security/XSS')->entity_decode($case_value);
$var = ee('Security/XSS')->entity_decode($var);
}
else
{
$case_value = ee()->security->entity_decode($case_value);
$var = ee()->security->entity_decode($var);
}
// is the case value a regular expression?
// check for a string contained within hashes #regex#
if (preg_match('/^#(.*)#$/', $case_value))
{
if (preg_match($case_value, $var))
{
// we've found a match, grab case content and exit loop
$temp_return_data .= substr($tagdata, $starts_at, $ends_at - $starts_at);
// log
if ($debug)
{
ee()->TMPL->log_item("Switchee: regex match: case '{$case_value}' matched variable '{$var}'");
}
$has_match = true;
if ( $match_all )
{
break;
}
else
{
break 2;
}
}
}
if ($case_value == $var)
{
// we've found a match, grab case content and exit loop
$temp_return_data .= substr($tagdata, $starts_at, $ends_at - $starts_at);
// log
if ($debug)
{
ee()->TMPL->log_item("Switchee: string match: case '{$case_value}' matched variable '{$var}'");
}
$has_match = true;
if ( $match_all )
{
break;
}
else
{
break 2;
}
}
}
}
// default value
if(!$has_match && isset($val['default']))
{
$default_param = strtolower($val['default']);
if($default_param == 'yes' || $default_param == 'y' || $default_param == 'true' || $default_param == '1')
{
// found a default, save matched content but keep search for a match (continue loop)
$default = substr($tagdata, $starts_at, $ends_at - $starts_at);
// log
if ($debug)
{
ee()->TMPL->log_item("Switchee: default case found for variable '{$var}'. This will be returned if no match is found.");
}
}
}
}
}
// fallback to default value if no matches
if ( ! $has_match)
{
$temp_return_data = $default;
}
// replace namespaced no_results with the real deal
$temp_return_data = str_replace(strtolower(__CLASS__).'_no_results', 'no_results', $temp_return_data);
// restore original content inside nested tags
foreach ($this->_ph as $index => $val)
{
// convert the outer shell of {switchee} tag pairs to plugin tags {exp:switchee}
// now we can do this all over again...
$val = preg_replace( array('/^{switchee/i', '/{\/switchee$/i'), array('{exp:switchee', '{/exp:switchee'), $val);
$temp_return_data = str_replace('{[_'.__CLASS__.'_'.($index+1).']', $val, $temp_return_data);
}
$this->return_data = $temp_return_data;
}
/**
* _placeholders
*
* Replaces nested tag content with placeholders
*
* @access private
* @param array
* @return string
*/
private function _placeholders($matches)
{
$this->_ph[] = $matches[0];
return '{[_'.__CLASS__.'_'.count($this->_ph).']';
}
/**
* _pcre error
*
* Log PCRE error for debugging
*
* @access private
* @return void
*/
private function _pcre_error()
{
// either an unsuccessful match, or a PCRE error occurred
$pcre_err = preg_last_error(); // PHP 5.2 and above
if ($pcre_err === PREG_NO_ERROR)
{
ee()->TMPL->log_item("Switchee: Successful non-match");
}
else
{
// preg_match error :(
switch ($pcre_err)
{
case PREG_INTERNAL_ERROR:
ee()->TMPL->log_item("Switchee: PREG_INTERNAL_ERROR");
break;
case PREG_BACKTRACK_LIMIT_ERROR:
ee()->TMPL->log_item("Switchee: PREG_BACKTRACK_LIMIT_ERROR");
break;
case PREG_RECURSION_LIMIT_ERROR:
ee()->TMPL->log_item("Switchee: PREG_RECURSION_LIMIT_ERROR");
break;
case PREG_BAD_UTF8_ERROR:
ee()->TMPL->log_item("Switchee: PREG_BAD_UTF8_ERROR");
break;
case PREG_BAD_UTF8_OFFSET_ERROR:
ee()->TMPL->log_item("Switchee: PREG_BAD_UTF8_OFFSET_ERROR");
break;
default:
ee()->TMPL->log_item("Switchee: Unrecognized PREG error");
break;
}
}
}
// usage instructions
public static function usage()
{
ob_start();
?>
-------------------
HOW TO USE
-------------------
{exp:switchee variable = "{variable_to_test}" parse="inward"}
{case value="value1|value2"}
Content to show
{/case}
{case value="value3" default="Yes"}
Content to show
{/case}
{case value="#^P(\d+)$#|''"}
Use regular expressions enclosed by hashes #regex#
Be careful to encode the following reserved characters as follows:
{ = {
| = |
} = }
Use '' to represent an empty string
{/case}
{case value="value4" default="Yes"}
{!-- you can also nest Switchee by leaving off the 'exp:' in nested tags : --}
{switchee variable="{another_variable_to_test}" parse="inward"}
{case value="value1" default="yes"}
nested content to show
{/case}
{/switchee}
{/case}
{/exp:switchee}
How to support no_result blocks inside wrapped tags:
{if switchee_no_results}
{redirect="channel/noresult"}
{/if}
GET and POST globals can be evaluated by prefixing with get: or post:, e.g.:
{exp:switchee variable="post:my_var" parse="inward"}
Any global variable can be evaluated by prefixing with global:, e.g.:
{exp:switchee variable="global:my_var" parse="inward"}
Any Stash variable can be evaluated by prefixing with stash:, e.g.:
{exp:switchee variable="stash:my_var" parse="inward"}
You can use the match="all" parameter to return the results of all matching cases, instead of just the first one.
{exp:switchee variable="my_var" match="all" parse="inward"}
Debugging:
To enable logging add the parameter debug="yes", e.g.:
{exp:switchee variable="my_var" parse="inward" debug="yes"}
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
}