forked from e107inc/e107
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortcode_handler.php
652 lines (583 loc) · 14.8 KB
/
shortcode_handler.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
<?php
/*
* e107 website system
*
* Copyright (C) 2008-2009 e107 Inc (e107.org)
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
* e107 Shortcode handler
*
* $Source: /cvs_backup/e107_0.8/e107_handlers/shortcode_handler.php,v $
* $Revision$
* $Date$
* $Author$
*/
if (!defined('e107_INIT')) { exit; }
/**
* Register shortcode
* $classFunc could be function name, class name or object
* $code could be 'true' when class name/object is passed to automate the
* registration of shortcode methods
* XXX - maybe we could move this to the class?
*
* @param mixed $classFunc
* @param mixed $codes
* @param string $path
* @param boolean $force
*/
function register_shortcode($classFunc, $codes, $path='', $force = false)
{
$sc = e107::getScParser();
//If codes is set to true, let's go get a list of shortcode methods
if(/*is_bool($codes) && */$codes === true) //double check fix
{
$codes = array();
$tmp = get_class_methods($classFunc);
foreach($tmp as $c)
{
if(strpos($c, 'sc_') === 0)
{
$codes[] = substr($c, 3);
}
}
unset($tmp);
}
//Register object feature
$classObj = null;
if(is_object($classFunc))
{
$classObj = $classFunc;
$classFunc = get_class($classObj);
}
//We only register these shortcodes if they have not already been registered in some manner
//ie theme or other plugin .sc files
if(is_array($codes))
{
foreach($codes as $code)
{
$code = strtoupper($code);
if((!$sc->isRegistered($code) || $force == true) && !$sc->isOverride($code))
{
$sc->registered_codes[$code] = array('type' => 'class', 'path' => $path, 'class' => $classFunc);
}
}
//register object if required
if(null !== $classObj && !isset($sc->scClasses[$classFunc]))
{
$sc->scClasses[$classFunc] = $classObj;
}
}
else
{
$codes = strtoupper($codes);
if((!$sc->isRegistered($code) || $force == true) && !$sc->isOverride($code))
{
$sc->registered_codes[$codes] = array('type' => 'func', 'path' => $path, 'function' => $classFunc);
}
}
}
/**
* Add value to already registered SC object
*
* @param string $className
* @param string $scVarName
* @param mixed $value
*/
function setScVar($className, $scVarName, $value)
{
$sc = e107::getScParser();
if(isset($sc->scClasses[$className]))
{
$sc->scClasses[$className]->$scVarName = $value;
}
}
/**
* Call function on an already registered SC object
*
* @param string $className
* @param string $scFuncName
* @param mixed $param - passed to function
*
* @return mixed|boolean - FALSE if class doesn't exist; otherwise whatever the function returns.
*/
function callScFunc($className, $scFuncName, $param= '')
{
$sc = e107::getScParser();
if(isset($sc->scClasses[$className]))
{
return call_user_func(array($sc->scClasses[$className],$scFuncName), $param);
}
else
{
return FALSE;
}
}
/**
* Create shortcode object - DEPRECATED. use e_shortcode.php or
*
* @param string $class
* @param boolean $force
*/
function initShortcodeClass($class, $force = false)
{
$sc = e107::getScParser();
if(class_exists($class) && ($force || !isset($sc->scClasses[$class])))
{
$sc->scClasses[$class] = new $class();
}
}
class e_shortcode
{
var $scList = array(); // The actual code - added by parsing files or when plugin codes encountered. Array key is the shortcode name.
var $parseSCFiles; // True if individual shortcode files are to be used
var $addedCodes; // Apparently not used
var $registered_codes = array(); // Shortcodes added by plugins
var $scClasses = array(); // Batch shortcode classes
var $scOverride = array(); // Array of codes found in override/ dir
private $eVars = '';
function e_shortcode($noload=false)
{
global $pref;
$this->parseSCFiles = true; // Default probably never used, but make sure its defined.
$this->loadOverrideShortcodes();
$this->loadThemeShortcodes();
$this->loadPluginShortcodes();
$this->loadPluginSCFiles();
}
/**
* Register any shortcode from the override/shortcodes/ directory
*
* @return void
*/
protected function loadOverrideShortcodes()
{
$pref = e107::getConfig('core')->getPref();
if(varset($pref['sc_override']))
{
$tmp = explode(',', $pref['sc_override']);
foreach($tmp as $code)
{
$code = strtoupper(trim($code));
$this->registered_codes[$code]['type'] = 'override';
$this->scOverride[] = $code;
}
}
}
/**
* Register any shortcodes that were registered by the theme
* $register_sc[] = 'MY_THEME_CODE'
*
* @return void
*/
protected function loadThemeShortcodes()
{
global $register_sc;
if(isset($register_sc) && is_array($register_sc))
{
foreach($register_sc as $code)
{
if(!$this->isRegistered($code))
{
$code = strtoupper($code);
$this->registered_codes[$code]['type'] = 'theme';
}
}
}
}
/**
* Register all .sc files found in plugin directories (via pref)
*
* @return void
*/
protected function loadPluginSCFiles()
{
$pref = e107::getConfig('core')->getPref();
if(varset($pref['shortcode_list'], '') != '')
{
foreach($pref['shortcode_list'] as $path => $namearray)
{
foreach($namearray as $code => $uclass)
{
if($code == 'shortcode_config')
{
include_once(e_PLUGIN.$path.'/shortcode_config.php');
}
else
{
$code = strtoupper($code);
if(!$this->isRegistered($code))
{
$this->registered_codes[$code]['type'] = 'plugin';
$this->registered_codes[$code]['path'] = $path;
$this->registered_codes[$code]['perms'] = $uclass; // Add this in
}
}
}
}
}
}
/**
* Register Plugin Shortcode Batch files (e_shortcode.php) for use site-wide.
* Equivalent to multiple .sc files in the plugin's folder.
*
* @return void
*/
protected function loadPluginShortcodes()
{
$pref = e107::getConfig('core')->getPref();
if(!vartrue($pref['e_shortcode_list']))
{
return;
}
foreach($pref['e_shortcode_list'] as $key=>$val)
{
if(!include_once(e_PLUGIN.$key.'/e_shortcode.php'))
{
continue;
}
$path = e_PLUGIN.$key.'/e_shortcode.php';
$classFunc = $key.'_shortcodes';
$this->registerClassMethods($classFunc,$path);
}
}
/**
* Common Auto-Register function for class methods.
*
*/
private function registerClassMethods($classFunc,$path)
{
$this->scClasses[$classFunc] = new $classFunc;
$tmp = get_class_methods($classFunc);
foreach($tmp as $c)
{
if(strpos($c, 'sc_') === 0)
{
$sc_func = substr($c, 3);
$code = strtoupper($sc_func);
if(!$this->isRegistered($code))
{
$this->registered_codes[$code] = array('type' => 'class', 'path' => $path, 'class' => $classFunc);
}
}
}
}
/**
* Register Core Shortcode Batches.
* FIXME - currently loaded all the time (even on front-end)
*
* @return void
*/
function loadCoreShortcodes()
{
$coreBatchList = array('admin_shortcodes');
foreach($coreBatchList as $cb)
{
$path = e_CORE.'shortcodes/batch/'.$cb.".php";
if(include_once($path))
{
$this->registerClassMethods($cb,$path);
}
}
}
function isRegistered($code)
{
return in_array($code, $this->registered_codes);
}
public function resetScClass($className, $object)
{
if($this->isScClass($className))
{
$this->scClasses[$className] = $object;
}
}
function isScClass($className)
{
return isset($this->scClasses[$className]);
}
function isOverride($code)
{
return in_array($code, $this->scOverride);
}
function parseCodes($text, $useSCFiles = true, $extraCodes = '', $eVars = null)
{
$saveParseSCFiles = $this->parseSCFiles; // In case of nested call
$this->parseSCFiles = $useSCFiles;
$this->eVars = null;
if(is_object($eVars)) {
$this->eVars = $eVars;
}
//object support
if(is_object($extraCodes))
{
$classname = get_class($extraCodes);
//register once
if(!$this->isScClass($classname))
{
register_shortcode($extraCodes, true);
}
//always overwrite object
$this->scClasses[$classname] = $extraCodes;
}
elseif(is_array($extraCodes))
{
foreach($extraCodes as $sc => $code)
{
$this->scList[$sc] = $code;
}
}
$ret = preg_replace_callback('#\{(\S[^\x02]*?\S)\}#', array(&$this, 'doCode'), $text);
$this->parseSCFiles = $saveParseSCFiles; // Restore previous value
return $ret;
}
function doCode($matches)
{
global $pref, $e107cache, $menu_pref, $sc_style, $parm, $sql;
if($this->eVars) {
if($this->eVars->$matches[1]) {
return $this->eVars->$matches[1];
}
}
if(strpos($matches[1], E_NL) !== false) { return $matches[0]; }
if (strpos($matches[1], '='))
{
list($code, $parm) = explode('=', $matches[1], 2);
}
else
{
$code = $matches[1];
$parm = '';
}
//look for the $sc_mode
if (strpos($code, '|'))
{
list($code, $sc_mode) = explode("|", $code, 2);
$code = trim($code);
$sc_mode = trim($sc_mode);
}
else
{
$sc_mode = '';
}
$parm = trim($parm);
$parm = str_replace(array('[[', ']]'), array('{', '}'), $parm);
if (E107_DBG_BBSC || E107_DBG_SC)
{
global $db_debug;
$sql->db_Mark_Time("SC $code");
$db_debug->logCode(2, $code, $parm, "");
}
if(E107_DBG_SC)
{
echo "<strong>";
echo '{';
echo $code;
echo ($parm) ? '='.htmlentities($parm) : "";
echo '}';
echo "</strong>";
// trigger_error('starting shortcode {'.$code.'}', E_USER_ERROR); // no longer useful - use ?[debug=bbsc]
}
$scCode = '';
$scFile = '';
// Check to see if we've already loaded the .sc file contents
if (array_key_exists($code, $this->scList))
{
$scCode = $this->scList[$code];
}
else
{
//.sc file not yet loaded, or shortcode is new function type
if ($this->parseSCFiles == true)
{
if (array_key_exists($code, $this->registered_codes))
{
//shortcode is registered, let's proceed.
if(isset($this->registered_codes[$code]['perms']))
{
if(!check_class($this->registered_codes[$code]['perms'])) { return ''; }
}
switch($this->registered_codes[$code]['type'])
{
case 'class':
//It is batch shortcode. Load the class and call the method
$_class = $this->registered_codes[$code]['class'];
$_method = 'sc_'.strtolower($code);
if(!isset($this->scClasses[$_class]))
{
if(!class_exists($_class) && $this->registered_codes[$code]['path'])
{
include_once($this->registered_codes[$code]['path']);
}
$this->scClasses[$_class] = new $_class;
}
if(method_exists($this->scClasses[$_class], $_method))
{
$ret = $this->scClasses[$_class]->$_method($parm, $sc_mode);
}
else
{
echo $_class.'::'.$_method.' NOT FOUND!<br />';
}
break;
case 'func':
//It is a function, so include the file and call the function
$_function = $this->registered_codes[$code]['function'];
if($this->registered_codes[$code]['path'])
{
include_once($this->registered_codes[$code]['path'].strtolower($code).'.php');
}
if(function_exists($_function))
{
$ret = call_user_func($_function, $parm, $sc_mode);
}
break;
case 'plugin':
$scFile = e_PLUGIN.strtolower($this->registered_codes[$code]['path']).'/'.strtolower($code).'.sc';
break;
case 'override':
$scFile = e_CORE.'override/shortcodes/'.strtolower($code).'.sc';
break;
case 'theme':
$scFile = THEME.strtolower($code).'.sc';
break;
}
}
else
{
// Code is not registered, let's look for .sc or .php file
// .php file takes precedence over .sc file
if(is_readable(e_CORE.'shortcodes/single/'.strtolower($code).'.php'))
{
$_function = strtolower($code).'_shortcode';
$_class = strtolower($code);
include_once(e_CORE.'shortcodes/single/'.strtolower($code).'.php');
if(class_exists($_class, false)) // prevent __autoload - performance
{
$ret = call_user_func(array($_class,$_function), $parm);
}
elseif(function_exists($_function))
{
$ret = call_user_func($_function, $parm);
}
}
else
{
$scFile = e_CORE.'shortcodes/single/'.strtolower($code).'.sc';
}
}
if ($scFile && file_exists($scFile))
{
$scCode = file_get_contents($scFile);
$this->scList[$code] = $scCode;
}
}
if (!isset($scCode))
{
if(E107_DBG_BBSC) { trigger_error('shortcode not found:{'.$code.'}', E_USER_ERROR); }
return $matches[0];
}
if(E107_DBG_SC && $scFile)
{
// echo (isset($scFile)) ? "<br />sc_file= ".str_replace(e_CORE.'shortcodes/single/', '', $scFile).'<br />' : '';
// echo "<br />sc= <b>$code</b>";
}
}
if($scCode)
{
$ret = eval($scCode);
}
if(isset($ret) && ($ret != '' || is_numeric($ret)))
{
//if $sc_mode exists, we need it to parse $sc_style
if($sc_mode)
{
$code = $code.'|'.$sc_mode;
}
if(isset($sc_style) && is_array($sc_style) && array_key_exists($code, $sc_style))
{
if(isset($sc_style[$code]['pre']))
{
$ret = $sc_style[$code]['pre'].$ret;
}
if(isset($sc_style[$code]['post']))
{
$ret = $ret.$sc_style[$code]['post'];
}
}
}
if (E107_DBG_SC)
{
$sql->db_Mark_Time("(SC {$code} Done)");
}
return isset($ret) ? $ret : '';
}
function parse_scbatch($fname, $type = 'file')
{
global $e107cache, $eArrayStorage;
$cur_shortcodes = array();
if($type == 'file')
{
$batch_cachefile = 'nomd5_scbatch_'.md5($fname);
// $cache_filename = $e107cache->cache_fname("nomd5_{$batchfile_md5}");
$sc_cache = $e107cache->retrieve_sys($batch_cachefile);
if(!$sc_cache)
{
$sc_batch = file($fname);
}
else
{
$cur_shortcodes = $eArrayStorage->ReadArray($sc_cache);
$sc_batch = "";
}
}
else
{
$sc_batch = $fname;
}
if($sc_batch)
{
$cur_sc = '';
foreach($sc_batch as $line)
{
if (trim($line) == 'SC_END')
{
$cur_sc = '';
}
if ($cur_sc)
{
$cur_shortcodes[$cur_sc] .= $line;
}
if (preg_match('#^SC_BEGIN (\w*).*#', $line, $matches))
{
$cur_sc = $matches[1];
$cur_shortcodes[$cur_sc] = varset($cur_shortcodes[$cur_sc],'');
}
}
if($type == 'file')
{
$sc_cache = $eArrayStorage->WriteArray($cur_shortcodes, false);
$e107cache->set_sys($batch_cachefile, $sc_cache);
}
}
foreach(array_keys($cur_shortcodes) as $cur_sc)
{
if (array_key_exists($cur_sc, $this -> registered_codes))
{
if ($this -> registered_codes[$cur_sc]['type'] == 'plugin')
{
$scFile = e_PLUGIN.strtolower($this -> registered_codes[$cur_sc]['path']).'/'.strtolower($cur_sc).'.sc';
}
else
{
$scFile = THEME.strtolower($cur_sc).'.sc';
}
if (is_readable($scFile))
{
$cur_shortcodes[$cur_sc] = file_get_contents($scFile);
}
}
}
return $cur_shortcodes;
}
}
?>