TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-11-2008, 01:06 PM   #1 (permalink)
The Wanderer
 
Join Date: Dec 2007
Posts: 8
Thanks: 2
Jonnee is on a distinguished road
Default Interactive plugin system

Hello all users of TalkPHP

I have been reading and looking and writing user system. Now I had this idea to make this plug&play style plugin system so you only need to upload the plugin to ftp folder and active it from controlpanel.

So what I have now is this :

Code:
<?php

if( !defined( "INPROCESS" ) ){
	header("HTTP/1.0 403 Forbidden");
	die();
}
 include_once 'mysql'. EXT; 
class plugin extends mysql{  
 
 	var $mysql;
 	var $fullpath;
 	var $plugins = array();
 	
	function __construct() {
		
		$this->mysql = new mysql();
		
		if( phpversion( ) < 5 ){
            mysql::__construct( );
        }
        
        // Lets check all avaible plugins
       	$list = array();     

		if ( ($directoryHandle = opendir( $this->fullpath . '/plugins/' )) == true ) {  
			while (($file = readdir( $directoryHandle )) !== false) {  
			// Make sure we're not dealing with a file or a link to the parent directory  
				if( is_dir( $this->fullpath . '/plugins/' . $file ) && ($file == '.' || $file == '..') !== true )  
					array_push( $list, $file );  
			}  
		}  
		
		$results = $this->Fetch_Object('SELECT * FROM plugins');
		
		$newResult = array();
		// Create an array: 'plugin name' = 'active' (1 or 0)  
		foreach ( $results as $result ){  
			$newResult[$result->name] = $result->active;  
		}    
		
		// Register the active plugins 
		foreach( $list as $plugin ){  
			if($newResult[$plugin] == "1"){  
				$this->registerplug($plugin);
			}
		}  
	}
	
	function registerplug($plugin){
		require_once( $this->fullpath . "/plugins/$plugin/$plugin.plugin.php" );
 	}  	
 	
 	function hook($checkpoint){
 		// Cycle through all the plugins that are active  
  		foreach($this->plugins as $plugin){ 
    		if(!call_user_func($checkpoint))  
     			throw new Exception( "Cannot hook plugin ($plugin) at checkpoint ($checkpoint)" );  
   		}  
 	}
  
}
?>
And I use it like this ->

Code:
$plugin = new plugin;
$plugin->fullpath =  $system_folder;
if( phpversion() < '5' ) 
    $plugin->__construct( );
Now when I try to hook functions I use it like this :

Code:
$plugin->hook('Menu'); 
$plugin->hook('onLoad');
So the problem is that it wont print out "hello world" text which I asked it to do in both of those plugins.

Code:
class menu extends plugin {
    function Menu(){
        print 'Hello World!';
        return true;
    }
}
So what is wrong with this? Any suggestions? MySQL connections work 100% for sure (tested)
Jonnee is offline  
Reply With Quote
Old 02-22-2009, 06:38 AM   #2 (permalink)
The Wanderer
 
Join Date: Feb 2009
Posts: 7
Thanks: 0
jlodell is on a distinguished road
Default Quick and dirty

First, the actual registration of the plugin is missing.
It just includes the plugin class file.
Try this as your registerplug method.

function registerplug($plugin){
// if the plugin isn't already registered
if(!array_key_exists($plugin,$this->plugin)){
// include library file
require_once($this->fullpath.
"/plugins/$plugin/$plugin.plugin.php");
// create instance of plugin
$this->plugin[$plugin] = new $plugin;
}
return $this;
}

And this for the hook method...

function hook($checkpoint){
// Cycle through all the plugins that are active
foreach($this->plugins as $key=>$plugin){
if(!method_exists($plugin,$checkpoint)){
throw new Exception("Cannot hook plugin($key) at checkpoint ($checkpoint)");
}
$this->results[$key] = $plugin->$checkpoint();
}
}

This is just real dirty implementation on what you already have. There are better ways to go about this. some shortcomings include the fact that method exists will show private and protected members which cannot be called like this.
This should make this work and get you moving in the right direction though.

I would pick a common PHP framework and read thru some of the library files that implement similar behavior to get a little inspiration.

cheers,
justin
jlodell is offline  
Reply With Quote
The Following User Says Thank You to jlodell For This Useful Post:
Jonnee (02-22-2009)
Old 02-22-2009, 11:56 AM   #3 (permalink)
The Wanderer
 
Join Date: Dec 2007
Posts: 8
Thanks: 2
Jonnee is on a distinguished road
Default

Thanks for 'quick' answer This project has been thrown to recycle bin some time ago already
Jonnee is offline  
Reply With Quote
Old 02-22-2009, 05:26 PM   #4 (permalink)
The Wanderer
 
Join Date: Feb 2009
Posts: 7
Thanks: 0
jlodell is on a distinguished road
Default

yeah, i noticed the date after i replied. sorry.
jlodell is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:59 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design