浏览代码

Plugins engine :)

pecesama 16 年前
父节点
当前提交
18b7d22368
共有 8 个文件被更改,包括 203 次插入16 次删除
  1. 20 11
      classes/configuration.class.php
  2. 29 1
      classes/functions.php
  3. 74 0
      classes/plugin.class.php
  4. 25 0
      classes/plugins.class.php
  5. 25 4
      entry.php
  6. 15 0
      plugins/cinco.php
  7. 0 0
      plugins/dos.php
  8. 15 0
      plugins/hola.php

+ 20 - 11
classes/configuration.class.php 查看文件

@@ -12,9 +12,7 @@ if(!defined('entry') || !entry) die('Not a valid page');
12 12
   =========================== */
13 13
 ?>
14 14
 <?php
15
-require_once("lang.functions.php");
16
-require_once('mysql_connection.class.php');
17
-class configuration extends Conexion_Mysql {
15
+class configuration {
18 16
 	
19 17
 	var $urlGelato;
20 18
 	var $tablePrefix;
@@ -31,14 +29,14 @@ class configuration extends Conexion_Mysql {
31 29
 	var $shorten_links;
32 30
 	var $rssImportFrec;
33 31
 	var $check_version;
32
+	var $plugins = array();	
34 33
 		
35
-	function configuration() {
36
-		parent::Conexion_Mysql(DB_name, DB_Server, DB_User, DB_Password);
37
-		
34
+	function configuration() {		
38 35
 		global $isFeed;
36
+		global $db;
39 37
 		
40
-		if ($this->ejecutarConsulta("SELECT * FROM ".Table_prefix."config")) {
41
-			$row=$this->obtenerRegistro();
38
+		if ($db->ejecutarConsulta("SELECT * FROM ".Table_prefix."config")) {
39
+			$row=$db->obtenerRegistro();
42 40
 			$this->tablePrefix = Table_prefix;
43 41
 			$this->urlGelato = $row['url_installation'];				
44 42
 			$this->postLimit = $row['posts_limit'];
@@ -58,6 +56,16 @@ class configuration extends Conexion_Mysql {
58 56
 			$this->rssImportFrec = $this->get_option("rss_import_frec");
59 57
 			$this->check_version = $this->get_option("check_version");
60 58
 			
59
+			//TODO: Soporte de los plugins desde BD activar/desactivar
60
+			if ($handle = opendir("plugins")) {				
61
+				while (false !== ($file = readdir($handle))) { 
62
+					if (substr($file, strlen($file)-3, 3) == "php") {
63
+						require_once("plugins/{$file}");
64
+						$this->plugins[] = substr($file, 0, strlen($file)-4);						
65
+					} 
66
+				} 
67
+				closedir($handle); 
68
+			}			
61 69
 		} else {
62 70
 			if($isFeed) {
63 71
 				header("HTTP/1.0 503 Service Unavailable"); 
@@ -73,10 +81,11 @@ class configuration extends Conexion_Mysql {
73 81
 	}
74 82
 	
75 83
 	function get_option($name){
84
+		global $db;
76 85
 		$sql = "SELECT * FROM ".Table_prefix."options WHERE name='".$name."' LIMIT 1";
77
-		$this->ejecutarConsulta($sql);
78
-		if ($this->contarRegistros()>0) {
79
-			$row=$this->obtenerRegistro();
86
+		$db->ejecutarConsulta($sql);
87
+		if ($db->contarRegistros()>0) {
88
+			$row=$db->obtenerRegistro();
80 89
 			return $row['val'];
81 90
 		} else {
82 91
 			return "0";

+ 29 - 1
classes/functions.php 查看文件

@@ -646,4 +646,32 @@ if(!defined('entry') || !entry) die('Not a valid page');
646 646
 		}
647 647
 		return $out;
648 648
 	}
649
-?>
649
+
650
+	function init_plugins() {        
651
+		global $conf;
652
+        
653
+        foreach ($conf->plugins as $index => $plugin) {
654
+			//echo "[".$index."] => ".$plugin."<br />";
655
+            if (!file_exists("plugins/".$plugin.".php")) {
656
+				//echo "\tNo existe el archivo<br />";
657
+                unset($config->plugins[$index]);
658
+                continue;
659
+            }
660
+			//echo "\tSi existe el archivo<br />";
661
+            
662
+            if (!class_exists($plugin)) {
663
+				//echo "\tNo existe la clase<br />";
664
+                continue;
665
+			}
666
+			//echo "\tSi existe la clase<br />";
667
+
668
+            plugins::$instances[$plugin] = new $plugin;
669
+			/*print_r(plugins::$instances[$plugin]);
670
+			echo "<br />";*/
671
+        }
672
+		/*echo "<br /><br />";
673
+		print_r(plugins::$instances);
674
+		echo "<br />";
675
+		die();*/
676
+	}
677
+	?>

+ 74 - 0
classes/plugin.class.php 查看文件

@@ -0,0 +1,74 @@
1
+<?php
2
+if(!defined('entry') || !entry) die('Not a valid page');
3
+/* ===========================
4
+
5
+  gelato CMS - A PHP based tumblelog CMS
6
+  development version
7
+  http://www.gelatocms.com/
8
+
9
+  gelato CMS is a free software licensed under the GPL 2.0
10
+  Copyright (C) 2007 by Pedro Santana <pecesama at gmail dot com>
11
+
12
+  =========================== */
13
+?>
14
+<?php
15
+	class plugin {	
16
+		
17
+		var $actions = array();
18
+		var $exists = array();
19
+	
20
+		function call($name) {            
21
+
22
+            if (!$this->exists($name)) {
23
+                return false;
24
+			}            
25
+            
26
+            /*echo "<br />==========<br />";
27
+			echo $name;
28
+			echo "<br />";*/
29
+			
30
+			$index = 0;
31
+            foreach (plugins::$instances as $plugin) {				
32
+				$action = $this->actions[$name][$index][1];                
33
+				if (is_callable(array($plugin, $action))) {                    
34
+					$plugin->$action();
35
+				}
36
+				$index++;
37
+			}
38
+        }
39
+        
40
+		function exists($name) {
41
+            if (isset($this->exists[$name])) {
42
+                return $this->exists[$name];
43
+			}
44
+
45
+            foreach (plugins::$instances as $plugin) {
46
+				/*print_r(plugins::$instances);
47
+				echo "<br />";
48
+				print_r($plugin);
49
+				echo "<br />";
50
+				print_r($this->actions[$name]);
51
+				echo "<br />";
52
+				echo $this->actions[$name][0][1];
53
+				echo "<br />";*/                
54
+				if (is_callable(array($plugin, $this->actions[$name][0][1]))) {
55
+                    return $this->exists[$name] = true;
56
+				}
57
+			}
58
+
59
+            return $this->exists[$name] = false;
60
+        }		
61
+        
62
+		function & instance()
63
+        {
64
+            static $instance;
65
+
66
+            if( !isset($instance) ) {
67
+                $instance = new self();
68
+            }
69
+
70
+            return $instance;
71
+        }
72
+		
73
+	}
74
+?>

+ 25 - 0
classes/plugins.class.php 查看文件

@@ -0,0 +1,25 @@
1
+<?php
2
+if(!defined('entry') || !entry) die('Not a valid page');
3
+/* ===========================
4
+
5
+  gelato CMS - A PHP based tumblelog CMS
6
+  development version
7
+  http://www.gelatocms.com/
8
+
9
+  gelato CMS is a free software licensed under the GPL 2.0
10
+  Copyright (C) 2007 by Pedro Santana <pecesama at gmail dot com>
11
+
12
+  =========================== */
13
+?>
14
+<?php	
15
+	class plugins {
16
+		
17
+		static $instances = array();
18
+		
19
+		function addAction($name, $function) {
20
+	    	$plugEngine = plugin::instance();
21
+	    	$plugEngine->actions[$name][] = array($this, $function);	    	
22
+	    }
23
+		
24
+	}
25
+?>

+ 25 - 4
entry.php 查看文件

@@ -1,12 +1,13 @@
1 1
 <?php
2 2
 ob_start();
3
- if(!defined('entry') || !entry) die('Not a valid page');
3
+if(!defined('entry') || !entry) die('Not a valid page');
4 4
 /*
5 5
  * Created on Sep 15, 2007
6
- * Modified on Sep 22, 2007
6
+ * Modified on Oct 30, 2008
7 7
  *
8 8
  * Known Entry Points
9 9
  * api.php
10
+ * archive.php
10 11
  * install.php
11 12
  * index.php
12 13
  * login.php
@@ -56,6 +57,7 @@ if($installed) {
56 57
 }
57 58
 
58 59
 require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'configuration.class.php');
60
+require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'functions.php');
59 61
 require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'gelato.class.php');
60 62
 require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'templates.class.php');
61 63
 require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'themes.class.php');
@@ -67,16 +69,35 @@ require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'mysql_connection.class
67 69
 require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'streams.class.php');
68 70
 require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'gettext.class.php');
69 71
 require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'lang.functions.php');
72
+require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'plugin.class.php');
73
+require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'plugins.class.php');
70 74
 
71 75
 if($installed){
72 76
 
73 77
 	// Globals to be used throughout the application
78
+	$db = new Conexion_Mysql(DB_name, DB_Server, DB_User, DB_Password);
74 79
 	$user = new user();
75 80
 	$tumble = new gelato();
76
-	$conf = new configuration();
77
-	$db = new Conexion_Mysql(DB_name, DB_Server, DB_User, DB_Password);
81
+	$conf = new configuration();	
78 82
 
79 83
 	session_start();
84
+	
85
+	//print_r($conf->plugins);
86
+	//die();
87
+	init_plugins();
88
+	$trigger = plugin::instance();
89
+	
90
+	//echo "plugins.instances: ";
91
+	//print_r(plugins::$instances);
92
+	//echo "<br />";
93
+	//die();
94
+	
95
+	//echo "plugin.actions: ";
96
+	//$plugEngine = plugin::instance();
97
+	//print_r($plugEngine->actions);
98
+	//die();
99
+	
100
+	$trigger->call('add_post');	
80 101
 
81 102
 	$feeds = new feeds();
82 103
 	$feeds->updateFeeds();

+ 15 - 0
plugins/cinco.php 查看文件

@@ -0,0 +1,15 @@
1
+<?php
2
+
3
+class cinco extends plugins {
4
+	
5
+	function cinco() {
6
+		$this->addAction('add_post', 'dameCinco');
7
+	}
8
+	
9
+	function dameCinco() {
10
+		echo "Este es el plugin 5<br />";
11
+	}
12
+	
13
+}
14
+
15
+?>

+ 0 - 0
plugins/dos.php 查看文件


+ 15 - 0
plugins/hola.php 查看文件

@@ -0,0 +1,15 @@
1
+<?php
2
+
3
+class hola extends plugins {
4
+	
5
+	function hola() {
6
+		$this->addAction('add_post', 'saluda');		
7
+	}
8
+	
9
+	function saluda() {
10
+		echo "Hola mundo desde plugin en gelato<br />";
11
+	}
12
+	
13
+}
14
+
15
+?>