<?php $PLUGINS=array(); $PLUGIN_REGISTER_CURRENT=null; function load_plugins_config($mb_id) { global $Config; $cfgscript=compute_user_path($mb_id,$Config->Paths['user.base'].'/',"/config.plugins.inc.php"); if (!file_exists($cfgscript)) return array(); $PluginsConfig=array(); include $cfgscript; return $PluginsConfig; } function save_plugins_config($mb_id,$PluginsConfig) { global $Config; $cfgscript=compute_user_path($mb_id,$Config->Paths['user.base'].'/',"/config.plugins.inc.php"); @mkdir(dirname($cfgscript),750,true); $text="<?php\n"; foreach ($PluginsConfig as $pname=>$conf) { $text.="\$PluginsConfig['".addcslashes($pname,"'")."']=array("; foreach ($conf as $key=>$value) if ($key) switch ($key) { case 'enabled': $text.="'enabled'=>".($value?'true':'false').','; break; default:; } $text.="null);\n"; } $text.="?>"; file_put_contents($cfgscript,$text); } function list_local_plugins() { global $Config; $plugins=array(); $pluginpaths=glob($Config->PluginBasePath."/*",GLOB_ONLYDIR); foreach ($pluginpaths as $path) { $pluginname=basename($path); $xml=load_plugin_meta($pluginname); if ($xml) $plugins[$pluginname]=$xml; } return $plugins; } function load_plugin_meta($pluginname) { global $Config; $metafile=$Config->PluginBasePath.'/'.$pluginname.'/meta.xml'; $xml=@simplexml_load_file($metafile); return $xml; } function default_plugin_addscript($pluginEnv) { global $Config; $serverurl=is_https()?$Config->ScriptServerURL['https']:$Config->ScriptServerURL['http']; return array( 'src'=>$serverurl.'serviceadjust.js?DID='.urlencode($Config->DISTRIBUTION_ID).'&ver='.urlencode($Config->VERSION), 'defer'=>true ); } function populate_default_plugin($plugintype) { global $PLUGIN_REGISTER_CURRENT; switch ($plugintype) { case 'addscript': $PLUGIN_REGISTER_CURRENT='default_plugin_addscript'; register_plugin($plugintype,'default_plugin_addscript',1,100); break; } } function populate_plugins() { global $Config,$PLUGIN_REGISTER_CURRENT; $cp_id=compere_id_requested(); $PluginsConfig=load_plugins_config($cp_id); $Config->PluginsConfig=$PluginsConfig; foreach ($PluginsConfig as $PluginName=>$pConf) { if (!$pConf['enabled']) continue; $scriptpath=$Config->PluginBasePath.'/'.$PluginName.'/'.$PluginName.'.php'; if (file_exists($scriptpath) && is_readable($scriptpath)) { $pluginEnv=compute_plugin_env($cp_id,$PluginName,$scriptpath); $PLUGIN_REGISTER_CURRENT=$PluginName; load_plugin($pluginEnv); } } $PLUGIN_REGISTER_CURRENT=null; } function load_plugin($pluginEnv) { include $pluginEnv->self; } function register_plugin($plugintype,$handler,$paramcount,$priority=50) { global $PLUGINS,$PLUGIN_REGISTER_CURRENT; if (!$PLUGIN_REGISTER_CURRENT) return; $PLUGINS[$plugintype][$priority][]=array( 'pluginname'=>$PLUGIN_REGISTER_CURRENT, 'handler'=>$handler, 'paramcount'=>$paramcount ); } function compute_plugin_env($mb_id,$pluginname,$pluginscript=null) { global $Config,$CMD; $pluginEnv=null; $pluginEnv->cmd=$CMD; $pluginEnv->lang=language_requested(); $pluginEnv->country=get_user_countrycode(); $pluginEnv->timezone=get_user_time_zone(); $pluginEnv->root=$Config->PluginBasePath.'/'.$pluginname.'/'; $pluginEnv->self=$pluginscript; $pluginEnv->datapath=compute_plugin_datapath($mb_id,$pluginname); return $pluginEnv; } function call_plugins_addscript() { populate_default_plugin('addscript'); global $PLUGINS,$dataset; $mb_id=compere_id_requested(); $priorities=array_keys((array)$PLUGINS['addscript']); sort($priorities); foreach ($priorities as $pri) { foreach ((array)$PLUGINS['addscript'][$pri] as $plugin) { $pluginEnv=compute_plugin_env($mb_id,$plugin['pluginname']); $script=call_user_func_array($plugin['handler'],array($pluginEnv)); if (is_array($script)) { $srcs=(array)$script['src']; foreach ($srcs as $src) $dataset['BEFORE-BODY-END'].='<script type="text/javascript" '. ($src?'src="'.htmlspecialchars($src).'" ':''). ($script['defer']?'deter="defer"':''). '></script>'; } else if (is_string($script)) { $dataset['BEFORE-BODY-END'].='<script type="text/javascript">'."\n". '<!--'."\n". $script. '/'.'/ -->'."\n". '</script>'; } } } } function compute_plugin_datapath($mb_id,$pluginname) { global $Config; return compute_user_path($mb_id,$Config->Paths['user.base'].'/','/plugins/'.$pluginname.'/'); } ?>