Plugins engine :)

master
pecesama 2008-10-31 08:56:50 +00:00
parent fd69f73e66
commit 18b7d22368
8 changed files with 203 additions and 16 deletions

View File

@ -12,9 +12,7 @@ if(!defined('entry') || !entry) die('Not a valid page');
=========================== */
?>
<?php
require_once("lang.functions.php");
require_once('mysql_connection.class.php');
class configuration extends Conexion_Mysql {
class configuration {
var $urlGelato;
var $tablePrefix;
@ -31,14 +29,14 @@ class configuration extends Conexion_Mysql {
var $shorten_links;
var $rssImportFrec;
var $check_version;
var $plugins = array();
function configuration() {
parent::Conexion_Mysql(DB_name, DB_Server, DB_User, DB_Password);
global $isFeed;
global $db;
if ($this->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";

View File

@ -646,4 +646,32 @@ if(!defined('entry') || !entry) die('Not a valid page');
}
return $out;
}
function init_plugins() {
global $conf;
foreach ($conf->plugins as $index => $plugin) {
//echo "[".$index."] => ".$plugin."<br />";
if (!file_exists("plugins/".$plugin.".php")) {
//echo "\tNo existe el archivo<br />";
unset($config->plugins[$index]);
continue;
}
//echo "\tSi existe el archivo<br />";
if (!class_exists($plugin)) {
//echo "\tNo existe la clase<br />";
continue;
}
//echo "\tSi existe la clase<br />";
plugins::$instances[$plugin] = new $plugin;
/*print_r(plugins::$instances[$plugin]);
echo "<br />";*/
}
/*echo "<br /><br />";
print_r(plugins::$instances);
echo "<br />";
die();*/
}
?>

74
classes/plugin.class.php Normal file
View File

@ -0,0 +1,74 @@
<?php
if(!defined('entry') || !entry) die('Not a valid page');
/* ===========================
gelato CMS - A PHP based tumblelog CMS
development version
http://www.gelatocms.com/
gelato CMS is a free software licensed under the GPL 2.0
Copyright (C) 2007 by Pedro Santana <pecesama at gmail dot com>
=========================== */
?>
<?php
class plugin {
var $actions = array();
var $exists = array();
function call($name) {
if (!$this->exists($name)) {
return false;
}
/*echo "<br />==========<br />";
echo $name;
echo "<br />";*/
$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 "<br />";
print_r($plugin);
echo "<br />";
print_r($this->actions[$name]);
echo "<br />";
echo $this->actions[$name][0][1];
echo "<br />";*/
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;
}
}
?>

25
classes/plugins.class.php Normal file
View File

@ -0,0 +1,25 @@
<?php
if(!defined('entry') || !entry) die('Not a valid page');
/* ===========================
gelato CMS - A PHP based tumblelog CMS
development version
http://www.gelatocms.com/
gelato CMS is a free software licensed under the GPL 2.0
Copyright (C) 2007 by Pedro Santana <pecesama at gmail dot com>
=========================== */
?>
<?php
class plugins {
static $instances = array();
function addAction($name, $function) {
$plugEngine = plugin::instance();
$plugEngine->actions[$name][] = array($this, $function);
}
}
?>

View File

@ -3,10 +3,11 @@ ob_start();
if(!defined('entry') || !entry) die('Not a valid page');
/*
* Created on Sep 15, 2007
* Modified on Sep 22, 2007
* Modified on Oct 30, 2008
*
* Known Entry Points
* api.php
* archive.php
* install.php
* index.php
* login.php
@ -56,6 +57,7 @@ if($installed) {
}
require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'configuration.class.php');
require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'functions.php');
require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'gelato.class.php');
require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'templates.class.php');
require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'themes.class.php');
@ -67,17 +69,36 @@ require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'mysql_connection.class
require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'streams.class.php');
require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'gettext.class.php');
require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'lang.functions.php');
require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'plugin.class.php');
require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'plugins.class.php');
if($installed){
// Globals to be used throughout the application
$db = new Conexion_Mysql(DB_name, DB_Server, DB_User, DB_Password);
$user = new user();
$tumble = new gelato();
$conf = new configuration();
$db = new Conexion_Mysql(DB_name, DB_Server, DB_User, DB_Password);
session_start();
//print_r($conf->plugins);
//die();
init_plugins();
$trigger = plugin::instance();
//echo "plugins.instances: ";
//print_r(plugins::$instances);
//echo "<br />";
//die();
//echo "plugin.actions: ";
//$plugEngine = plugin::instance();
//print_r($plugEngine->actions);
//die();
$trigger->call('add_post');
$feeds = new feeds();
$feeds->updateFeeds();
unset($feeds);

15
plugins/cinco.php Normal file
View File

@ -0,0 +1,15 @@
<?php
class cinco extends plugins {
function cinco() {
$this->addAction('add_post', 'dameCinco');
}
function dameCinco() {
echo "Este es el plugin 5<br />";
}
}
?>

0
plugins/dos.php Normal file
View File

15
plugins/hola.php Normal file
View File

@ -0,0 +1,15 @@
<?php
class hola extends plugins {
function hola() {
$this->addAction('add_post', 'saluda');
}
function saluda() {
echo "Hola mundo desde plugin en gelato<br />";
}
}
?>