@ 2014-04-07T16:44:50Z <?php
interface IPlugin {
public function set_hooks();
public function plugin_info();
public function deactivate();
}
function __autoload($class) {
include(getcwd() . "/plugins/" . ucfirst($class) . ".php");
}
/*
Static class responsible for every plugin
*/
class Plugins {
private static $plugins = array();
private static $hooks = array();
private static $plugin_count = 0;
private static $hook_count = 0;
public function __construct() {
$dir = opendir(getcwd() . '/plugins');
while (false !== ($filename = readdir($dir))) {
if (stripos($filename, '.php') === false || strcmp($filename, 'IPlugin.php') == 0)
continue;
$name = str_replace('.php', '', $filename);
array_push(self::$plugins, array(
'name' => $name,
'filename' => $filename,
'filepath' => getcwd().'/plugins/'.$filename
)
);
self::$plugin_count++;
self::add_hook($name, true);
}
}
public static function add_hook($name, $activate) {
$hooks = self::$hooks;
$length = count($hooks);
for ($i = 0; $i < $length; $i++)
if (in_array($name, $hooks[$i]) == true) {
if ($hooks[$i]['name'] !== $name)
continue;
self::$hooks[$i]['activate'] = 1;
self::$hook_count++;
return;
}
array_push(self::$hooks, array(
'name' => $name,
'activate' => $activate
)
);
self::$hook_count++;
}
public static function remove_hook($name) {
$hooks = self::$hooks;
$length = count($hooks);
$name = str_replace('.php', '', $name);
for ($i = 0; $i < $length; $i++) {
if ($hooks[$i]['name'] !== $name)
continue;
self::$hook_count--;
self::$hooks[$i]['activate'] = 0;
}
}
public static function run_hooks($hook) {
$plugins = self::$plugins;
$hooks = self::$hooks;
for ($i = 0; $i < count($hooks); $i++) {
$plugin = self::getPluginObject($hooks[$i]['name']);
if ($hooks[$i]['activate'] == 0) {
$hook_defunc = 'deactivate_' . $hook;
$func = is_callable(array($plugin, $hook_defunc));
if (!$func)
$plugin->deactivate();
else
$plugin->$hook_defunc();
continue;
}
$plugin_hooks = count($plugin->set_hooks());
for ($i2 = 0; $i2 < $plugin_hooks; $i2++)
if (strcasecmp($plugin_hooks[$i2], $hook) != 0)
continue;
$hook_func = 'activate_' . $hook;
$func = is_callable(array($plugin, $hook_func));
if (!$func)
$plugin->deactivate();
else
$plugin->$hook_func();
}
}
public static function getPluginObject($name) {
$plugins = self::$plugins;
$plugin_count = count($plugins);
for ($i = 0; $i < $plugin_count; $i++) {
if ($plugins[$i]['name'] !== $name)
continue;
return new $plugins[$i]['name']();
}
return null;
}
public static function getPluginCount() {
return self::$plugin_count;
}
public static function getHookCount() {
return self::$hook_count;
}
public static function get_plugins() {
return self::$plugins;
}
public static function get_hooks() {
return self::$hooks;
}
}
class TestPlugin implements IPlugin {
public function set_hooks() {
return array(
'index_bottom', 'index_content'
);
}
public function plugin_info() {
return array(
'name' => 'Test Plugin',
'version' => 1.1
);
}
public function activate_index_bottom() {
echo '<p>This will activate if the index_bottom hook is used</p>';
}
public function deactivate_index_bottom() {
echo 'DEACTIVATE INDEX BOTTOM';
}
public function activate_index_content() {
echo '<a href="lol.php" alt="">lolfags</a>';
}
public function deactivate_index_content() {
echo 'DEACTIVATE';
}
// invoked if other hook functions fail for any reason
public function deactivate() {
echo '<h1>Plugin system broken!!?!?!?</h1>';
}
}
require('Plugins.php');
echo '<html><head></head><body><p>blabla</p>';
$plugins = new Plugins();
$plugins->run_hooks('index_content');
echo '<hr />';
$plugins->run_hooks('index_bottom');
echo '<p>woot</p></body></html>';
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Fatal error: __autoload() is no longer supported, use spl_autoload_register() instead in /in/oYqmI on line 11
Process exited with code 255 . Output for 7.4.33 Deprecated: __autoload() is deprecated, use spl_autoload_register() instead in /in/oYqmI on line 11
Warning: require(Plugins.php): failed to open stream: No such file or directory in /in/oYqmI on line 169
Fatal error: require(): Failed opening required 'Plugins.php' (include_path='.:') in /in/oYqmI on line 169
Process exited with code 255 . Output for 7.4.26 - 7.4.32 Deprecated: __autoload() is deprecated, use spl_autoload_register() instead in /in/oYqmI on line 11
Warning: require(): open_basedir restriction in effect. File(Plugins.php) is not within the allowed path(s): (/tmp:/in:/etc) in /in/oYqmI on line 169
Warning: require(Plugins.php): failed to open stream: Operation not permitted in /in/oYqmI on line 169
Fatal error: require(): Failed opening required 'Plugins.php' (include_path='.:') in /in/oYqmI on line 169
Process exited with code 255 . Output for 5.3.0 - 5.3.29 , 5.4.0 - 5.4.45 , 5.5.0 - 5.5.38 , 5.6.0 - 5.6.40 , 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.34 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.25 Warning: require(Plugins.php): failed to open stream: No such file or directory in /in/oYqmI on line 169
Fatal error: require(): Failed opening required 'Plugins.php' (include_path='.:') in /in/oYqmI on line 169
Process exited with code 255 . preferences:dark mode live preview
216.18 ms | 404 KiB | 415 Q