From 18b7d22368c5772191044e8f5bc73f94cb27ce61 Mon Sep 17 00:00:00 2001 From: pecesama Date: Fri, 31 Oct 2008 08:56:50 +0000 Subject: [PATCH] Plugins engine :) --- classes/configuration.class.php | 31 +++++++++----- classes/functions.php | 30 ++++++++++++- classes/plugin.class.php | 74 +++++++++++++++++++++++++++++++++ classes/plugins.class.php | 25 +++++++++++ entry.php | 29 +++++++++++-- plugins/cinco.php | 15 +++++++ plugins/dos.php | 0 plugins/hola.php | 15 +++++++ 8 files changed, 203 insertions(+), 16 deletions(-) create mode 100644 classes/plugin.class.php create mode 100644 classes/plugins.class.php create mode 100644 plugins/cinco.php create mode 100644 plugins/dos.php create mode 100644 plugins/hola.php diff --git a/classes/configuration.class.php b/classes/configuration.class.php index 6f1c256..ea78b98 100644 --- a/classes/configuration.class.php +++ b/classes/configuration.class.php @@ -12,9 +12,7 @@ if(!defined('entry') || !entry) die('Not a valid page'); =========================== */ ?> ejecutarConsulta("SELECT * FROM ".Table_prefix."config")) { - $row=$this->obtenerRegistro(); + if ($db->ejecutarConsulta("SELECT * FROM ".Table_prefix."config")) { + $row=$db->obtenerRegistro(); $this->tablePrefix = Table_prefix; $this->urlGelato = $row['url_installation']; $this->postLimit = $row['posts_limit']; @@ -58,6 +56,16 @@ class configuration extends Conexion_Mysql { $this->rssImportFrec = $this->get_option("rss_import_frec"); $this->check_version = $this->get_option("check_version"); + //TODO: Soporte de los plugins desde BD activar/desactivar + if ($handle = opendir("plugins")) { + while (false !== ($file = readdir($handle))) { + if (substr($file, strlen($file)-3, 3) == "php") { + require_once("plugins/{$file}"); + $this->plugins[] = substr($file, 0, strlen($file)-4); + } + } + closedir($handle); + } } else { if($isFeed) { header("HTTP/1.0 503 Service Unavailable"); @@ -73,10 +81,11 @@ class configuration extends Conexion_Mysql { } function get_option($name){ + global $db; $sql = "SELECT * FROM ".Table_prefix."options WHERE name='".$name."' LIMIT 1"; - $this->ejecutarConsulta($sql); - if ($this->contarRegistros()>0) { - $row=$this->obtenerRegistro(); + $db->ejecutarConsulta($sql); + if ($db->contarRegistros()>0) { + $row=$db->obtenerRegistro(); return $row['val']; } else { return "0"; diff --git a/classes/functions.php b/classes/functions.php index 80d8148..ddf835b 100644 --- a/classes/functions.php +++ b/classes/functions.php @@ -646,4 +646,32 @@ if(!defined('entry') || !entry) die('Not a valid page'); } return $out; } -?> \ No newline at end of file + + function init_plugins() { + global $conf; + + foreach ($conf->plugins as $index => $plugin) { + //echo "[".$index."] => ".$plugin."
"; + if (!file_exists("plugins/".$plugin.".php")) { + //echo "\tNo existe el archivo
"; + unset($config->plugins[$index]); + continue; + } + //echo "\tSi existe el archivo
"; + + if (!class_exists($plugin)) { + //echo "\tNo existe la clase
"; + continue; + } + //echo "\tSi existe la clase
"; + + plugins::$instances[$plugin] = new $plugin; + /*print_r(plugins::$instances[$plugin]); + echo "
";*/ + } + /*echo "

"; + print_r(plugins::$instances); + echo "
"; + die();*/ + } + ?> \ No newline at end of file diff --git a/classes/plugin.class.php b/classes/plugin.class.php new file mode 100644 index 0000000..9910005 --- /dev/null +++ b/classes/plugin.class.php @@ -0,0 +1,74 @@ + + + =========================== */ +?> +exists($name)) { + return false; + } + + /*echo "
==========
"; + echo $name; + echo "
";*/ + + $index = 0; + foreach (plugins::$instances as $plugin) { + $action = $this->actions[$name][$index][1]; + if (is_callable(array($plugin, $action))) { + $plugin->$action(); + } + $index++; + } + } + + function exists($name) { + if (isset($this->exists[$name])) { + return $this->exists[$name]; + } + + foreach (plugins::$instances as $plugin) { + /*print_r(plugins::$instances); + echo "
"; + print_r($plugin); + echo "
"; + print_r($this->actions[$name]); + echo "
"; + echo $this->actions[$name][0][1]; + echo "
";*/ + if (is_callable(array($plugin, $this->actions[$name][0][1]))) { + return $this->exists[$name] = true; + } + } + + return $this->exists[$name] = false; + } + + function & instance() + { + static $instance; + + if( !isset($instance) ) { + $instance = new self(); + } + + return $instance; + } + + } +?> diff --git a/classes/plugins.class.php b/classes/plugins.class.php new file mode 100644 index 0000000..2c550cf --- /dev/null +++ b/classes/plugins.class.php @@ -0,0 +1,25 @@ + + + =========================== */ +?> +actions[$name][] = array($this, $function); + } + + } +?> diff --git a/entry.php b/entry.php index 6940095..8951cc2 100644 --- a/entry.php +++ b/entry.php @@ -1,12 +1,13 @@ plugins); + //die(); + init_plugins(); + $trigger = plugin::instance(); + + //echo "plugins.instances: "; + //print_r(plugins::$instances); + //echo "
"; + //die(); + + //echo "plugin.actions: "; + //$plugEngine = plugin::instance(); + //print_r($plugEngine->actions); + //die(); + + $trigger->call('add_post'); $feeds = new feeds(); $feeds->updateFeeds(); diff --git a/plugins/cinco.php b/plugins/cinco.php new file mode 100644 index 0000000..ebd0193 --- /dev/null +++ b/plugins/cinco.php @@ -0,0 +1,15 @@ +addAction('add_post', 'dameCinco'); + } + + function dameCinco() { + echo "Este es el plugin 5
"; + } + +} + +?> \ No newline at end of file diff --git a/plugins/dos.php b/plugins/dos.php new file mode 100644 index 0000000..e69de29 diff --git a/plugins/hola.php b/plugins/hola.php new file mode 100644 index 0000000..1c9ca03 --- /dev/null +++ b/plugins/hola.php @@ -0,0 +1,15 @@ +addAction('add_post', 'saluda'); + } + + function saluda() { + echo "Hola mundo desde plugin en gelato
"; + } + +} + +?>