1
0
mirror of https://github.com/e107inc/e107.git synced 2025-03-14 17:39:46 +01:00

Shortcode parser: fixed number of errors, more major changes, getScBatch will now auto-register batch (no need of registerShortcode() call), added new example of sc handler usage (news), work in progress

This commit is contained in:
secretr 2010-04-25 13:52:14 +00:00
parent 6b7a4f7242
commit e0c8e76f70
6 changed files with 299 additions and 165 deletions

View File

@ -2124,7 +2124,7 @@ function __autoload($className)
// read the todo for e_shortcode.php related problems // read the todo for e_shortcode.php related problems
if('shortcodes' == $end) if('shortcodes' == $end)
{ {
$filename = e_PLUGIN.$tmp[0].'/shortcodes/'; // plugname/shortcodes/ $filename = e_PLUGIN.$tmp[0].'/core/shortcodes/batch/'; // plugname/core/shortcodes/batch/
unset($tmp[0]); unset($tmp[0]);
$filename .= implode('_', $tmp).'_shortcodes.php'; // my_shortcodes.php $filename .= implode('_', $tmp).'_shortcodes.php'; // my_shortcodes.php
break; break;
@ -2163,7 +2163,7 @@ function __autoload($className)
// register __autoload if possible to prevent its override by // register __autoload if possible to prevent its override by
// 3rd party spl_autoload_register calls // 3rd party spl_autoload_register calls
if(function_exists('spl_autoload_register')) if(function_exists('spl_autoload_register') && !defset('E107_DISABLE_AUTOLOAD', false))
{ {
spl_autoload_register('__autoload'); spl_autoload_register('__autoload');
} }

View File

@ -20,8 +20,9 @@ register_shortcode('news_shortcodes', TRUE);
initShortcodeClass('news_shortcodes'); initShortcodeClass('news_shortcodes');
*/ */
e107::getScParser()->registerShortcode('news_shortcodes', true) // Done via e107::getScBatch('news'); call - see news_class.php
->initShortcodeClass('news_shortcodes'); /*e107::getScParser()->registerShortcode('news_shortcodes', true)
->initShortcodeClass('news_shortcodes');*/
class news_shortcodes extends e_shortcode class news_shortcodes extends e_shortcode
{ {

View File

@ -861,14 +861,29 @@ class e107
/** /**
* Retrieve registered sc object (batch) by class name * Retrieve registered sc object (batch) by class name
* Note - '_shortcodes' part of the class is added by the method * Note - '_shortcodes' part of the class/override is added by the method
* <code><?php e107::getScObject('news');</code> * Override is possible only if class is not already instantiated by shortcode parser
* *
* <code><?php
* // core news shortcodes
* e107::getScObject('news');
* // object of plugin_myplugin_my_shortcodes class -> myplugin/core/shortcodes/batch/my_shortcodes.php
* e107::getScObject('my', 'myplugin');
* // news override - plugin_myplugin_news_shortcodes extends news_shortcodes -> myplugin/core/shortcodes/batch/news_shortcodes.php
* e107::getScObject('news', 'myplugin', true);
* // news override - plugin_myplugin_mynews_shortcodes extends news_shortcodes -> myplugin/core/shortcodes/batch/mynews_shortcodes.php
* e107::getScObject('news', 'myplugin', 'mynews');
* </code>
*
* @param string $className
* @param string $pluginName
* @param string|true $overrideClass
* @return e_shortcode * @return e_shortcode
*/ */
public static function getScBatch($className) public static function getScBatch($className, $pluginName = null, $overrideClass = null)
{ {
return self::getScParser()->getScObject($className.'_shortcodes'); if(is_string($overrideClass)) $overrideClass .= '_shortcodes';
return self::getScParser()->getScObject($className.'_shortcodes', $pluginName, $overrideClass);
} }
/** /**

View File

@ -2131,6 +2131,16 @@ class e_vars
{ {
return empty($this->vars); return empty($this->vars);
} }
/**
* Check if given data key is set
* @param string $key
* @return boolean
*/
public function isVar($key)
{
return isset($this->vars[$key]);
}
/** /**
* Magic setter * Magic setter
@ -2171,7 +2181,7 @@ class e_vars
*/ */
public function __isset($key) public function __isset($key)
{ {
return isset($this->vars[$key]); return $this->isVar($key);
} }
/** /**

View File

@ -601,7 +601,7 @@ class news {
} }
//$loop_uid = $news['news_author']; - no references found //$loop_uid = $news['news_author']; - no references found
require_once(e_CORE.'shortcodes/batch/news_shortcodes.php'); //require_once(e_CORE.'shortcodes/batch/news_shortcodes.php');
/* DEPRECATED /* DEPRECATED
setScVar('news_shortcodes', 'news_item', $news); setScVar('news_shortcodes', 'news_item', $news);
setScVar('news_shortcodes', 'param', $param); setScVar('news_shortcodes', 'param', $param);

View File

@ -10,9 +10,12 @@
* *
* $URL$ * $URL$
* $Id$ * $Id$
*/ */
if (!defined('e107_INIT')) { exit; } if (!defined('e107_INIT'))
{
exit;
}
/** /**
* *
@ -28,7 +31,7 @@ if (!defined('e107_INIT')) { exit; }
/** /**
* FIXME: to be removed * FIXME: to be removed
*/ */
function register_shortcode($classFunc, $codes, $path='', $force = false) function register_shortcode($classFunc, $codes, $path = '', $force = false)
{ {
return e107::getScParser()->registerShortcode($classFunc, $codes, $path, $force); return e107::getScParser()->registerShortcode($classFunc, $codes, $path, $force);
} }
@ -41,7 +44,6 @@ function setScVar($className, $scVarName, $value)
return e107::getScParser()->setScVar($className, $scVarName, $value); return e107::getScParser()->setScVar($className, $scVarName, $value);
} }
/** /**
* FIXME: to be removed * FIXME: to be removed
*/ */
@ -58,28 +60,30 @@ function initShortcodeClass($class, $force = false, $eVars = null)
return e107::getScParser()->initShortcodeClass($class, $eVars, $force); return e107::getScParser()->initShortcodeClass($class, $eVars, $force);
} }
class e_parse_shortcode class e_parse_shortcode
{ {
protected $scList = array(); // The actual code - added by parsing files or when plugin codes encountered. Array key is the shortcode name. protected $scList = array(); // The actual code - added by parsing files or when plugin codes encountered. Array key is the shortcode name.
protected $parseSCFiles; // True if individual shortcode files are to be used protected $parseSCFiles; // True if individual shortcode files are to be used
protected $addedCodes; // Apparently not used protected $addedCodes; // Apparently not used
protected $registered_codes = array(); // Shortcodes added by plugins TODO make it private protected $registered_codes = array(); // Shortcodes added by plugins TODO make it private
protected $scClasses = array(); // Batch shortcode classes - TODO make it private protected $scClasses = array(); // Batch shortcode classes - TODO make it private
protected $scOverride = array(); // Array of codes found in override/ dir protected $scOverride = array(); // Array of codes found in override/ dir
protected $eVars = ''; /**
* @var e_vars
*/
protected $eVars = null;
function __construct() function __construct()
{ {
$this->parseSCFiles = true; // Default probably never used, but make sure its defined. $this->parseSCFiles = true; // Default probably never used, but make sure its defined.
$this->loadOverrideShortcodes(); $this->loadOverrideShortcodes();
$this->loadThemeShortcodes(); $this->loadThemeShortcodes();
$this->loadPluginShortcodes(); $this->loadPluginShortcodes();
$this->loadPluginSCFiles(); $this->loadPluginSCFiles();
$this->loadCoreShortcodes(); $this->loadCoreShortcodes();
} }
/** /**
* Register shortcode * Register shortcode
* $classFunc could be function name, class name or object * $classFunc could be function name, class name or object
@ -95,44 +99,44 @@ class e_parse_shortcode
function registerShortcode($classFunc, $codes, $path = '', $force = false) function registerShortcode($classFunc, $codes, $path = '', $force = false)
{ {
//If codes is set to true, let's go get a list of shortcode methods //If codes is set to true, let's go get a list of shortcode methods
if($codes === true) if ($codes === true)
{ {
$codes = array(); $codes = array();
$tmp = get_class_methods($classFunc); $tmp = get_class_methods($classFunc);
foreach($tmp as $c) foreach ($tmp as $c)
{ {
if(strpos($c, 'sc_') === 0) if (strpos($c, 'sc_') === 0)
{ {
$codes[] = substr($c, 3); $codes[] = substr($c, 3);
} }
} }
unset($tmp); unset($tmp);
} }
//Register object feature //Register object feature
$classObj = null; $classObj = null;
if(is_object($classFunc)) if (is_object($classFunc))
{ {
$classObj = $classFunc; $classObj = $classFunc;
$classFunc = get_class($classObj); $classFunc = get_class($classObj);
} }
//We only register these shortcodes if they have not already been registered in some manner //We only register these shortcodes if they have not already been registered in some manner
//ie theme or other plugin .sc files //ie theme or other plugin .sc files
if(is_array($codes)) if (is_array($codes))
{ {
foreach($codes as $code) foreach ($codes as $code)
{ {
$code = strtoupper($code); $code = strtoupper($code);
if((!$this->isRegistered($code) || $force == true) && !$this->isOverride($code)) if ((!$this->isRegistered($code) || $force == true) && !$this->isOverride($code))
{ {
$this->registered_codes[$code] = array('type' => 'class', 'path' => $path, 'class' => $classFunc); $this->registered_codes[$code] = array('type' => 'class', 'path' => $path, 'class' => $classFunc);
} }
} }
//register object if required //register object if required
if(null !== $classObj && (!isset($this->scClasses[$classFunc]) || $force == true)) if (null !== $classObj && (!$this->isScClass($classFunc) || $force == true))
{ {
$this->scClasses[$classFunc] = $classObj; $this->scClasses[$classFunc] = $classObj;
} }
@ -140,14 +144,14 @@ class e_parse_shortcode
else else
{ {
$codes = strtoupper($codes); $codes = strtoupper($codes);
if((!$this->isRegistered($code) || $force == true) && !$this->isOverride($code)) if ((!$this->isRegistered($code) || $force == true) && !$this->isOverride($code))
{ {
$this->registered_codes[$codes] = array('type' => 'func', 'path' => $path, 'function' => $classFunc); $this->registered_codes[$codes] = array('type' => 'func', 'path' => $path, 'function' => $classFunc);
} }
} }
return $this; return $this;
} }
/** /**
* Add value to already registered SC object * Add value to already registered SC object
* *
@ -158,21 +162,22 @@ class e_parse_shortcode
*/ */
public function setScVar($className, $scVarName, $value) public function setScVar($className, $scVarName, $value)
{ {
if(isset($this->scClasses[$className])) if ($this->isScClass($className))
{ {
// new way - batch should extend e_shortcode class // new way - batch should extend e_shortcode class
if(method_exists($this->scClasses[$className], 'setScVar')) if (method_exists($this->scClasses[$className], 'setScVar'))
{ {
$this->scClasses[$className]->setScVar($scVarName, $value); $this->scClasses[$className]->setScVar($scVarName, $value);
} }
else // Old - DEPRECATED else // Old - DEPRECATED
{ {
$this->scClasses[$className]->$scVarName = $value; $this->scClasses[$className]->$scVarName = $value;
} }
} }
return $this; return $this;
} }
/** /**
* Call function on an already registered SC object * Call function on an already registered SC object
* *
@ -180,17 +185,37 @@ class e_parse_shortcode
* @param string $scFuncName * @param string $scFuncName
* @param mixed $param - passed to function * @param mixed $param - passed to function
* *
* @return mixed|boolean - NULL if class doesn't exist; otherwise whatever the function returns. * @return mixed|boolean - NULL if class/method doesn't exist; otherwise whatever the function returns.
*/ */
public function callScFunc($className, $scFuncName, $param = '') public function callScFunc($className, $scFuncName, $param = '')
{ {
if(isset($this->scClasses[$className])) if ($this->isScClass($className))
{ {
return call_user_func(array($this->scClasses[$className], $scFuncName), $param); return method_exists($this->scClasses[$className], $scFuncName) ? call_user_func(array($this->scClasses[$className], $scFuncName), $param) : null;
} }
return null; return null;
} }
/**
* same as e_parse_shortcode::callScFunc(), but passes the last argument (array)
* to the called method as multiple arguments
*
* @param string $className
* @param string $scFuncName
* @param array $param - arguments passed to function
*
* @return mixed|boolean - NULL if class/method doesn't exist; otherwise whatever the function returns.
*/
protected function callScFuncA($className, $scFuncName, $args = array())
{
if ($this->isScClass($className))
{
// avoid warnings
return method_exists($this->scClasses[$className], $scFuncName) ? call_user_func_array(array($this->scClasses[$className], $scFuncName), $args) : null;
}
return null;
}
/** /**
* Create shortcode object - don't forget you still can use e_shortcode.php * Create shortcode object - don't forget you still can use e_shortcode.php
* *
@ -198,28 +223,83 @@ class e_parse_shortcode
* @param boolean $force * @param boolean $force
* @return e_shortcode * @return e_shortcode
*/ */
public function initShortcodeClass($class, $eVars = null, $force = false) public function initShortcodeClass($class, $force = false)
{ {
if(class_exists($class, false) && ($force || !isset($this->scClasses[$class]))) if (class_exists($class, false) && ($force || !$this->isScClass($className)))
{ {
$this->scClasses[$class] = new $class($eVars); $this->scClasses[$class] = new $class();
return $this->scClasses[$class]; return $this->scClasses[$class];
} }
return null; return null;
} }
/** /*public function getScObject($className)
* Get registered SC object
*
* @param string $className
* @return e_shortcode
*/
public function getScObject($className)
{ {
if(isset($this->scClasses[$className])) if (isset($this->scClasses[$className]))
{ {
return $this->scClasses[$className]; return $this->scClasses[$className];
} }
// TODO - throw exception?
return null;
}*/
/**
* Get registered SC object
* Normally you would use the proxy of this method - e107::getScBatch()
* DRAFT!
*
* <code><?php
* // simple use
* e107::getScParser()->getScObject('news_shortcodes');
*
* // plugin override - e107_plugins/myplug/core/shortcodes/batch/news_shortcodes.php -> class plugin_myplug_news_shortcodes
* e107::getScParser()->getScObject('news_shortcodes', 'myplug', true);
*
* // more complex plugin override
* // e107_plugins/myplug/core/shortcodes/batch/news2_shortcodes.php -> class plugin_myplug_news2_shortcodes
* e107::getScParser()->getScObject('news_shortcodes', 'myplug', 'news2_shortcodes');
* </code>
* @param string $className
* @param string $plugName
* @param string $overrideClass if true, $className is used
* @return e_shortcode
*/
public function getScObject($className, $pluginName = null, $overrideClass = null)
{
if ($this->isScClass($className))
{
return $this->scClasses[$className];
}
// defaults
$_class = $_class_fname = $className;
// plugin override
if($overrideClass)
{
if(true === $overrideClass)
{
$overrideClass = $className;
}
// e.g. class plugin_myplug_news_shortcodes
$_class_fname = $overrideClass;
$_class = 'plugin_'.$pluginName.'_'.$overrideClass;
}
$path = ($pluginName ? e_PLUGIN.$pluginName.'/shortcodes/batch/' : e_CORE.'shortcodes/batch/').$_class_fname.'.php';
if (is_readable($path))
{
require_once($path);
if (class_exists($_class, false)) // don't allow __autload()
{
// register instance directly to allow override
$this->scClasses[$className] = new $_class();
$this->registerClassMethods($className);
return $this->scClasses[$className];
}
}
// TODO - throw exception? // TODO - throw exception?
return null; return null;
} }
@ -231,10 +311,10 @@ class e_parse_shortcode
*/ */
protected function loadOverrideShortcodes() protected function loadOverrideShortcodes()
{ {
if(e107::getPref('sc_override')) if (e107::getPref('sc_override'))
{ {
$tmp = explode(',', e107::getPref('sc_override')); $tmp = explode(',', e107::getPref('sc_override'));
foreach($tmp as $code) foreach ($tmp as $code)
{ {
$code = strtoupper(trim($code)); $code = strtoupper(trim($code));
$this->registered_codes[$code]['type'] = 'override'; $this->registered_codes[$code]['type'] = 'override';
@ -254,11 +334,11 @@ class e_parse_shortcode
{ {
global $register_sc; global $register_sc;
if(isset($register_sc) && is_array($register_sc)) if (isset($register_sc) && is_array($register_sc))
{ {
foreach($register_sc as $code) foreach ($register_sc as $code)
{ {
if(!$this->isRegistered($code)) if (!$this->isRegistered($code))
{ {
$code = strtoupper($code); $code = strtoupper($code);
$this->registered_codes[$code]['type'] = 'theme'; $this->registered_codes[$code]['type'] = 'theme';
@ -268,7 +348,6 @@ class e_parse_shortcode
return $this; return $this;
} }
/** /**
* Register all .sc files found in plugin directories (via pref) * Register all .sc files found in plugin directories (via pref)
* *
@ -278,20 +357,20 @@ class e_parse_shortcode
{ {
$pref = e107::getPref('shortcode_list'); $pref = e107::getPref('shortcode_list');
if($pref) if ($pref)
{ {
foreach($pref as $path => $namearray) foreach ($pref as $path => $namearray)
{ {
foreach($namearray as $code => $uclass) foreach ($namearray as $code => $uclass)
{ {
if($code == 'shortcode_config') if ($code == 'shortcode_config')
{ {
include_once(e_PLUGIN.$path.'/shortcode_config.php'); include_once(e_PLUGIN.$path.'/shortcode_config.php');
} }
else else
{ {
$code = strtoupper($code); $code = strtoupper($code);
if(!$this->isRegistered($code)) if (!$this->isRegistered($code))
{ {
$this->registered_codes[$code]['type'] = 'plugin'; $this->registered_codes[$code]['type'] = 'plugin';
$this->registered_codes[$code]['path'] = $path; $this->registered_codes[$code]['path'] = $path;
@ -314,51 +393,52 @@ class e_parse_shortcode
{ {
$pref = e107::getPref('e_shortcode_list'); $pref = e107::getPref('e_shortcode_list');
if(!$pref) if (!$pref)
{ {
return $this; return $this;
} }
foreach($pref as $key=>$val) foreach ($pref as $key => $val)
{ {
$path = e_PLUGIN.$key.'/e_shortcode.php'; $path = e_PLUGIN.$key.'/e_shortcode.php';
$classFunc = $key.'_shortcodes'; $classFunc = $key.'_shortcodes';
if(!include_once($path)) if (!include_once($path))
{ {
continue; continue;
} }
$this->registerClassMethods($classFunc, $path); $this->registerClassMethods($classFunc, $path, false);
} }
return $this; return $this;
} }
/** /**
* Common Auto-Register function for class methods. * Common Auto-Register function for class methods.
* * @return e_parse_shortcode
*/ */
protected function registerClassMethods($classFunc, $path) protected function registerClassMethods($class, $path, $force = false)
{ {
//$this->scClasses[$classFunc] = new $classFunc; $tmp = get_class_methods($class);
$className = is_object($class) ? get_class($class) : $class;
$tmp = get_class_methods($classFunc);
foreach($tmp as $c) foreach ($tmp as $c)
{ {
if(strpos($c, 'sc_') === 0) if (strpos($c, 'sc_') === 0)
{ {
$sc_func = substr($c, 3); $sc_func = substr($c, 3);
$code = strtoupper($sc_func); $code = strtoupper($sc_func);
if(!$this->isRegistered($code)) if ($force || !$this->isRegistered($code))
{ {
$this->registered_codes[$code] = array('type' => 'class', 'path' => $path, 'class' => $classFunc); $this->registered_codes[$code] = array('type' => 'class', 'path' => $path, 'class' => $className);
} }
} }
} }
return $this;
} }
/** /**
* Register Core Shortcode Batches. * Register Core Shortcode Batches.
* FIXME - currently loaded all the time (even on front-end) * FIXME - make it smarter - currently loaded all the time (even on front-end)
* *
* @return e_parse_shortcode * @return e_parse_shortcode
*/ */
@ -366,10 +446,10 @@ class e_parse_shortcode
{ {
$coreBatchList = array('admin_shortcodes'); $coreBatchList = array('admin_shortcodes');
foreach($coreBatchList as $cb) foreach ($coreBatchList as $cb)
{ {
$path = e_CORE.'shortcodes/batch/'.$cb.".php"; $path = e_CORE.'shortcodes/batch/'.$cb.".php";
if(include_once($path)) if (include_once($path))
{ {
$this->registerClassMethods($cb, $path); $this->registerClassMethods($cb, $path);
} }
@ -384,10 +464,15 @@ class e_parse_shortcode
public function resetScClass($className, $object) public function resetScClass($className, $object)
{ {
if($this->isScClass($className)) if(null === $object)
{
unset($this->scClasses[$className]);
}
elseif ($this->isScClass($className))
{ {
$this->scClasses[$className] = $object; $this->scClasses[$className] = $object;
} }
return $this;
} }
function isScClass($className) function isScClass($className)
@ -402,38 +487,38 @@ class e_parse_shortcode
function parseCodes($text, $useSCFiles = true, $extraCodes = '', $eVars = null) function parseCodes($text, $useSCFiles = true, $extraCodes = '', $eVars = null)
{ {
$saveParseSCFiles = $this->parseSCFiles; // In case of nested call $saveParseSCFiles = $this->parseSCFiles; // In case of nested call
$this->parseSCFiles = $useSCFiles; $this->parseSCFiles = $useSCFiles;
$saveVars = $this->eVars; // In case of nested call
$this->eVars = null; $this->eVars = $eVars;
if(is_object($eVars)) {
$this->eVars = $eVars;
}
//object support //object support
if(is_object($extraCodes)) if (is_object($extraCodes))
{ {
$classname = get_class($extraCodes); $classname = get_class($extraCodes);
//register once //register once
if(!$this->isScClass($classname)) if (!$this->isScClass($classname))
{ {
$this->registerShortcode($extraCodes, true); $this->registerShortcode($extraCodes, true);
} }
//always overwrite object //always overwrite object
$this->scClasses[$classname] = $extraCodes; $this->scClasses[$classname] = $extraCodes;
// auto-register eVars if possible - call it manually?
// $this->callScFunc($classname, 'setVars', $this->eVars);
} }
elseif(is_array($extraCodes)) elseif (is_array($extraCodes))
{ {
foreach($extraCodes as $sc => $code) foreach ($extraCodes as $sc => $code)
{ {
$this->scList[$sc] = $code; $this->scList[$sc] = $code;
} }
} }
$ret = preg_replace_callback('#\{(\S[^\x02]*?\S)\}#', array(&$this, 'doCode'), $text); $ret = preg_replace_callback('#\{(\S[^\x02]*?\S)\}#', array(&$this, 'doCode'), $text);
$this->parseSCFiles = $saveParseSCFiles; // Restore previous value $this->parseSCFiles = $saveParseSCFiles; // Restore previous value
$this->eVars = null; // reset eVars $this->eVars = $saveVars; // restore eVars
return $ret; return $ret;
} }
@ -441,12 +526,17 @@ class e_parse_shortcode
{ {
global $pref, $e107cache, $menu_pref, $sc_style, $parm, $sql; global $pref, $e107cache, $menu_pref, $sc_style, $parm, $sql;
if($this->eVars) { if ($this->eVars)
if($this->eVars->$matches[1]) { {
if ($this->eVars->isVar($matches[1]))
{
return $this->eVars->$matches[1]; return $this->eVars->$matches[1];
} }
} }
if(strpos($matches[1], E_NL) !== false) { return $matches[0]; } if (strpos($matches[1], E_NL) !== false)
{
return $matches[0];
}
if (strpos($matches[1], '=')) if (strpos($matches[1], '='))
{ {
@ -478,16 +568,16 @@ class e_parse_shortcode
$db_debug->logCode(2, $code, $parm, ""); $db_debug->logCode(2, $code, $parm, "");
} }
if(E107_DBG_SC) if (E107_DBG_SC)
{ {
echo "<strong>"; echo "<strong>";
echo '{'; echo '{';
echo $code; echo $code;
echo ($parm) ? '='.htmlentities($parm) : ""; echo($parm) ? '='.htmlentities($parm) : "";
echo '}'; echo '}';
echo "</strong>"; echo "</strong>";
// trigger_error('starting shortcode {'.$code.'}', E_USER_ERROR); // no longer useful - use ?[debug=bbsc] // trigger_error('starting shortcode {'.$code.'}', E_USER_ERROR); // no longer useful - use ?[debug=bbsc]
} }
$scCode = ''; $scCode = '';
$scFile = ''; $scFile = '';
@ -504,48 +594,62 @@ class e_parse_shortcode
if (array_key_exists($code, $this->registered_codes)) if (array_key_exists($code, $this->registered_codes))
{ {
//shortcode is registered, let's proceed. //shortcode is registered, let's proceed.
if(isset($this->registered_codes[$code]['perms'])) if (isset($this->registered_codes[$code]['perms']))
{ {
if(!check_class($this->registered_codes[$code]['perms'])) { return ''; } if (!check_class($this->registered_codes[$code]['perms']))
{
return '';
}
} }
switch($this->registered_codes[$code]['type']) switch ($this->registered_codes[$code]['type'])
{ {
case 'class': case 'class':
//It is batch shortcode. Load the class and call the method //It is batch shortcode. Load the class and call the method
$_class = $this->registered_codes[$code]['class']; $_class = $this->registered_codes[$code]['class'];
$_method = 'sc_'.strtolower($code); $_method = 'sc_'.strtolower($code);
if(!isset($this->scClasses[$_class])) if (!$this->isScClass($_class))
{ {
if(!class_exists($_class) && $this->registered_codes[$code]['path']) if (!class_exists($_class) && $this->registered_codes[$code]['path'])
{ {
include_once($this->registered_codes[$code]['path']); include_once($this->registered_codes[$code]['path']);
} }
$this->initShortcodeClass($_class, $this->eVars, false); $this->initShortcodeClass($_class, false);
//$this->scClasses[$_class] = new $_class; if(!$this->isScClass($_class))
// make eVars available to the SC object members {
//$this->scClasses[$_class]->eVars = $this->eVars; return '';
}
// egister passed eVars object on init - call it manually?
// $this->callScFunc($_class, 'setVars', $this->eVars);
} }
if(method_exists($this->scClasses[$_class], $_method))
// FIXME - register passed eVars object - BAD solution - called on EVERY sc method call
// XXX - removal candidate - I really think it should be done manually (outside the parser)
// via e107::getScBatch(name)->setVars($eVars);
// $this->callScFunc($_class, 'setVars', $this->eVars);
$ret = $this->callScFuncA($_class, $_method, array($parm, $sc_mode));
/*if (method_exists($this->scClasses[$_class], $_method))
{ {
$ret = $this->scClasses[$_class]->$_method($parm, $sc_mode); $ret = $this->scClasses[$_class]->$_method($parm, $sc_mode);
} }
else else
{ {
echo $_class.'::'.$_method.' NOT FOUND!<br />'; echo $_class.'::'.$_method.' NOT FOUND!<br />';
} }*/
break; break;
case 'func': case 'func':
//It is a function, so include the file and call the function //It is a function, so include the file and call the function
$_function = $this->registered_codes[$code]['function']; $_function = $this->registered_codes[$code]['function'];
if($this->registered_codes[$code]['path']) if ($this->registered_codes[$code]['path'])
{ {
include_once($this->registered_codes[$code]['path'].strtolower($code).'.php'); include_once($this->registered_codes[$code]['path'].strtolower($code).'.php');
} }
if(function_exists($_function)) if (function_exists($_function))
{ {
$ret = call_user_func($_function, $parm, $sc_mode); $ret = call_user_func($_function, $parm, $sc_mode);
} }
@ -569,18 +673,19 @@ class e_parse_shortcode
{ {
// Code is not registered, let's look for .sc or .php file // Code is not registered, let's look for .sc or .php file
// .php file takes precedence over .sc file // .php file takes precedence over .sc file
if(is_readable(e_CORE.'shortcodes/single/'.strtolower($code).'.php')) if (is_readable(e_CORE.'shortcodes/single/'.strtolower($code).'.php'))
{ {
$_function = strtolower($code).'_shortcode'; $_function = strtolower($code).'_shortcode';
$_class = strtolower($code); $_class = strtolower($code);
include_once(e_CORE.'shortcodes/single/'.strtolower($code).'.php'); include_once(e_CORE.'shortcodes/single/'.strtolower($code).'.php');
if(class_exists($_class, false)) // prevent __autoload - performance if (class_exists($_class, false)) // prevent __autoload - performance
{ {
$ret = call_user_func(array($_class,$_function), $parm); $ret = call_user_func(array($_class, $_function), $parm);
} }
elseif(function_exists($_function)) elseif (function_exists($_function))
{ {
$ret = call_user_func($_function, $parm); $ret = call_user_func($_function, $parm);
} }
@ -599,36 +704,39 @@ class e_parse_shortcode
if (!isset($scCode)) if (!isset($scCode))
{ {
if(E107_DBG_BBSC) { trigger_error('shortcode not found:{'.$code.'}', E_USER_ERROR); } if (E107_DBG_BBSC)
{
trigger_error('shortcode not found:{'.$code.'}', E_USER_ERROR);
}
return $matches[0]; return $matches[0];
} }
if(E107_DBG_SC && $scFile) if (E107_DBG_SC && $scFile)
{ {
// echo (isset($scFile)) ? "<br />sc_file= ".str_replace(e_CORE.'shortcodes/single/', '', $scFile).'<br />' : ''; // echo (isset($scFile)) ? "<br />sc_file= ".str_replace(e_CORE.'shortcodes/single/', '', $scFile).'<br />' : '';
// echo "<br />sc= <b>$code</b>"; // echo "<br />sc= <b>$code</b>";
} }
} }
if($scCode) if ($scCode)
{ {
$ret = eval($scCode); $ret = eval($scCode);
} }
if(isset($ret) && ($ret != '' || is_numeric($ret))) if (isset($ret) && ($ret != '' || is_numeric($ret)))
{ {
//if $sc_mode exists, we need it to parse $sc_style //if $sc_mode exists, we need it to parse $sc_style
if($sc_mode) if ($sc_mode)
{ {
$code = $code.'|'.$sc_mode; $code = $code.'|'.$sc_mode;
} }
if(isset($sc_style) && is_array($sc_style) && array_key_exists($code, $sc_style)) if (isset($sc_style) && is_array($sc_style) && array_key_exists($code, $sc_style))
{ {
if(isset($sc_style[$code]['pre'])) if (isset($sc_style[$code]['pre']))
{ {
$ret = $sc_style[$code]['pre'].$ret; $ret = $sc_style[$code]['pre'].$ret;
} }
if(isset($sc_style[$code]['post'])) if (isset($sc_style[$code]['post']))
{ {
$ret = $ret.$sc_style[$code]['post']; $ret = $ret.$sc_style[$code]['post'];
} }
@ -645,12 +753,12 @@ class e_parse_shortcode
{ {
global $e107cache, $eArrayStorage; global $e107cache, $eArrayStorage;
$cur_shortcodes = array(); $cur_shortcodes = array();
if($type == 'file') if ($type == 'file')
{ {
$batch_cachefile = 'nomd5_scbatch_'.md5($fname); $batch_cachefile = 'nomd5_scbatch_'.md5($fname);
// $cache_filename = $e107cache->cache_fname("nomd5_{$batchfile_md5}"); // $cache_filename = $e107cache->cache_fname("nomd5_{$batchfile_md5}");
$sc_cache = $e107cache->retrieve_sys($batch_cachefile); $sc_cache = $e107cache->retrieve_sys($batch_cachefile);
if(!$sc_cache) if (!$sc_cache)
{ {
$sc_batch = file($fname); $sc_batch = file($fname);
} }
@ -665,10 +773,10 @@ class e_parse_shortcode
$sc_batch = $fname; $sc_batch = $fname;
} }
if($sc_batch) if ($sc_batch)
{ {
$cur_sc = ''; $cur_sc = '';
foreach($sc_batch as $line) foreach ($sc_batch as $line)
{ {
if (trim($line) == 'SC_END') if (trim($line) == 'SC_END')
{ {
@ -681,23 +789,23 @@ class e_parse_shortcode
if (preg_match('#^SC_BEGIN (\w*).*#', $line, $matches)) if (preg_match('#^SC_BEGIN (\w*).*#', $line, $matches))
{ {
$cur_sc = $matches[1]; $cur_sc = $matches[1];
$cur_shortcodes[$cur_sc] = varset($cur_shortcodes[$cur_sc],''); $cur_shortcodes[$cur_sc] = varset($cur_shortcodes[$cur_sc], '');
} }
} }
if($type == 'file') if ($type == 'file')
{ {
$sc_cache = $eArrayStorage->WriteArray($cur_shortcodes, false); $sc_cache = $eArrayStorage->WriteArray($cur_shortcodes, false);
$e107cache->set_sys($batch_cachefile, $sc_cache); $e107cache->set_sys($batch_cachefile, $sc_cache);
} }
} }
foreach(array_keys($cur_shortcodes) as $cur_sc) foreach (array_keys($cur_shortcodes) as $cur_sc)
{ {
if (array_key_exists($cur_sc, $this -> registered_codes)) if (array_key_exists($cur_sc, $this->registered_codes))
{ {
if ($this -> registered_codes[$cur_sc]['type'] == 'plugin') if ($this->registered_codes[$cur_sc]['type'] == 'plugin')
{ {
$scFile = e_PLUGIN.strtolower($this -> registered_codes[$cur_sc]['path']).'/'.strtolower($cur_sc).'.sc'; $scFile = e_PLUGIN.strtolower($this->registered_codes[$cur_sc]['path']).'/'.strtolower($cur_sc).'.sc';
} }
else else
{ {
@ -719,43 +827,43 @@ class e_shortcode
* Stores passed to shortcode handler simple parser object * Stores passed to shortcode handler simple parser object
* @var e_vars * @var e_vars
*/ */
protected $eVars = null; protected $eVars = null;
/** /**
* Storage for shortcode values * Storage for shortcode values
* @var e_vars * @var e_vars
*/ */
protected $scVars = null; protected $scVars = null;
public function __construct($eVars = null) public function __construct()
{ {
$this->setVars($eVars);
$this->scVars = new e_vars(); $this->scVars = new e_vars();
} }
/** /**
* Set external simple parser object * Set external simple parser object
* *
* @param e_vars $eVars * @param e_vars $eVars
* @return e_shortcode * @return e_shortcode
*/ */
public function setVars($eVars) public function setVars($eVars)
{ {
if(null === $eVars) $eVars = new e_vars(); if (null === $eVars)
$eVars = new e_vars();
$this->eVars = $eVars; $this->eVars = $eVars;
return $this; return $this;
} }
/** /**
* Get external simple parser object * Get external simple parser object
* *
* @return e_vars * @return e_vars
*/ */
public function getVars() public function getVars()
{ {
return $this->eVars; return $this->eVars;
} }
/** /**
* Add shortcode value * Add shortcode value
* <code>e107::getScObject('class_name')->setScVar('some_property', $some_value);</code> * <code>e107::getScObject('class_name')->setScVar('some_property', $some_value);</code>
@ -769,7 +877,7 @@ class e_shortcode
$this->scVars->$name = $value; $this->scVars->$name = $value;
return $this; return $this;
} }
/** /**
* Retrieve shortcode value * Retrieve shortcode value
* <code>$some_value = e107::getScObject('class_name')->getScVar('some_property');</code> * <code>$some_value = e107::getScObject('class_name')->getScVar('some_property');</code>
@ -781,11 +889,11 @@ class e_shortcode
{ {
return $this->scVars->$name; return $this->scVars->$name;
} }
/** /**
* Check if shortcode variable is set * Check if shortcode variable is set
* <code>if(e107::getScObject('class_name')->issetScVar('some_property')) * <code>if(e107::getScObject('class_name')->issetScVar('some_property'))
* { * {
* //do something * //do something
* }</code> * }</code>
* *
@ -796,7 +904,7 @@ class e_shortcode
{ {
return isset($this->scVars->$name); return isset($this->scVars->$name);
} }
/** /**
* Unset shortcode value * Unset shortcode value
* <code>e107::getScObject('class_name')->unsetScVar('some_property');</code> * <code>e107::getScObject('class_name')->unsetScVar('some_property');</code>
@ -807,9 +915,9 @@ class e_shortcode
public function unsetScVar($name) public function unsetScVar($name)
{ {
$this->scVars->$name = null; $this->scVars->$name = null;
unset($this->scVars->$name); unset($this->scVars->$name);
} }
/** /**
* Magic setter - bind to eVars object * Magic setter - bind to eVars object
* *
@ -831,7 +939,7 @@ class e_shortcode
{ {
return $this->getScVar($name); return $this->getScVar($name);
} }
/** /**
* Magic method - bind to eVars object * Magic method - bind to eVars object
* NOTE: works on PHP 5.1.0+ * NOTE: works on PHP 5.1.0+
@ -843,7 +951,7 @@ class e_shortcode
{ {
return $this->issetScVar($name); return $this->issetScVar($name);
} }
/** /**
* Magic method - bind to eVars object * Magic method - bind to eVars object
* NOTE: works on PHP 5.1.0+ * NOTE: works on PHP 5.1.0+
@ -854,4 +962,4 @@ class e_shortcode
{ {
$this->unsetScVar($name); $this->unsetScVar($name);
} }
} }