Location: PHPKode > projects > Obsessive Website Statistics > ows/include/plugin.inc.php
	$Id: plugin.inc.php 76 2007-07-31 06:01:31Z randomperson83 $

	Obsessive Web Statistics
    Copyright (C) 2007 Dustin Spicuzza <hide@address.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

	This provides functions for loading and managing plugins

// initialize global plugin data
global $ows_plugins;
if (!is_array($ows_plugins)){
	$ows_plugins = array();			// except for special parameters (starting with plugin_), each
									// element contains an array with information about the plugin, including
									// its object and the file it was from

	$ows_plugins['plugin_file'] = '';				// filename of plugin currently being processed
	$ows_plugins['plugin_path'] = realpath(dirname(__FILE__) . '/../plugins');
	$ows_plugins['plugin_interfaces'] = array();			// valid plugin interfaces, except iPluginInterface
	$ows_plugins['plugins_loaded'] = false;					// don't load plugins multiple times!
	$ows_plugins['all'] = array();							// holds references to all plugins loaded

// required files


	this loads all plugins (no arg) or a particular plugin
function load_plugins($plugin = ''){

	global $ows_plugins;

	// don't load plugins twice
	if ($ows_plugins['plugins_loaded'])
		return true;
	if (!is_dir($ows_plugins['plugin_path']))
		return show_error("Invalid filter directory \"$ows_plugins[plugin_path]\" specified!",true);
	$plugins = array();
	// if a plugin is specified, load that
	if ($plugin != ""){
		$plugins[] = "$ows_plugins[plugin_path]/$plugin.php";
	}else{		// load all plugins
		// don't load plugins twice
		$ows_plugins['plugins_loaded'] = true;
		if (!$dh = opendir($ows_plugins['plugin_path']))
			return show_error("Could not list contents of plugin directory \"$ows_plugins[plugin_path]\"!",true);
		// load all plugins from the directory
		while(($file = readdir($dh)) !== false){
			if ($file != '.' && $file != '..'){
				if (pathinfo($file,PATHINFO_EXTENSION) == 'php')
					$plugins[] = "$ows_plugins[plugin_path]/$file";
	// load plugin files alphabetically
	// next, actually load the plugins
	foreach ($plugins as $plugin){
		$ows_plugins['plugin_file'] = $plugin;
		if ($ows_plugins['plugin_path'] != realpath(dirname($plugin)) || !file_exists($plugin) || (include_once $plugin) != 1)
			show_error("Error loading plugin file \"$plugin\"",true);
	return true;

	Returns an array of plugin objects that are of type $plugin_type, or an empty
function get_plugins($plugin_type){
	global $ows_plugins;
	if (!isset($ows_plugins[$plugin_type]))
		return array();
	return $ows_plugins[$plugin_type];

	Returns a particular plugin array of information by its ID, or false
function get_plugin($plugin_type, $plugin_id){

	foreach (get_plugins($plugin_type) as $p)
		if ($p['plugin']->getPluginId() == $plugin_id)
			return $p['plugin'];
	return false;

	Plugins should call this function to register themselves with OWS. It should pass 
	a string with the plugin type, and the plugin object itself.
function register_plugin($plugin_type,&$object){

	global $ows_plugins, $cfg;	
	if (!is_object($object))
		return show_error("plugin_register: tried to register something that wasn't an object!");
	$interfaces = class_implements($object);
	// verify that it is a plugin
	if (!in_array('iPlugin',$interfaces))
		return show_error("Plugin in $ows_plugins[plugin_file] (" . get_class($object) . ") does not implement iPlugin",true);
	// see if the plugin type is defined
	if (!array_key_exists($plugin_type,$ows_plugins['plugin_interfaces']))
		return show_error("Plugin type " . htmlentities($plugin_type) . " in $ows_plugins[plugin_file] is not a recognized interface!");
	// see if the object has the interface associated with the plugin type
	if (!in_array($ows_plugins['plugin_interfaces'][$plugin_type],$interfaces))
		return show_error("Plugin \"" . get_class($object) . "\" in $ows_plugins[plugin_file] does not implement $ows_plugins[plugin_interfaces][$plugin_type]",true);
	// store the plugin type
	if (!array_key_exists($plugin_type,$ows_plugins))
		$ows_plugins[$plugin_type] = array();
	// create the plugin information object
	$p = array(
		'plugin' => &$object, 
		'filename' => $ows_plugins['plugin_file'], 
		'interfaces' => $interfaces, 
	$ows_plugins[$plugin_type][] = $p;
	$ows_plugins['all'][] = $p;
	return true;

// pass the plugin array to this function
function show_plugin_error($plugin,$message,$escape = false, $retval = false){
	return show_error("Plugin " . get_plugin_name($plugin) . " in file $plugin[filename] $message",$escape,$retval);

// where $plugin is the plugin array, not the class. Returns true if successful. 
function show_plugin_information_table($plugin){
	$info = $plugin['plugin']->getPluginInformation();
	echo "<table>";
	if (array_key_exists('pluginName',$info)){
		echo '<tr><td>Plugin Name:</td><td>' . htmlentities($info['pluginName']);
		if (array_key_exists('version',$info))
			echo ' ' . htmlentities($info['version']) . ' ';
		if (array_key_exists('aboutUrl',$info))
			echo ' ( <a href="' . htmlentities($info['aboutUrl']) . '">' . htmlentities($info['aboutUrl']) . '</a> )';
	if (array_key_exists('author',$info)){
		if (array_key_exists('url',$info))
			echo '<tr><td>Author:</td><td><a href="' . htmlentities($info['url']) . '">' . htmlentities($info['author']) . '</a></td></tr>';
			echo '<tr><td>Author:</td><td>' . htmlentities($info['author']) . '</td></tr>';
	if (array_key_exists('description',$info))
		echo '<tr><td>Description:</td><td>' . nl2br(htmlentities($info['description'])) . '</td></tr>';
	echo "</table>";
	return true;

// returns a unique checkbox input for a plugin, where $plugin is the $this pointer
function plugin_checkbox($plugin, $name, $checked){
	return '<input type="checkbox" name="' . htmlentities($plugin->getPluginId() . "_$name") . '" value="yes" ' . ($checked ? 'checked' : '') . '/>';

// returns a string describing the plugin, pass it the plugin array. 
function get_plugin_name($plugin){
	$info = $plugin['plugin']->getPluginInformation();
	if (array_key_exists('pluginName',$info))
		return $info['pluginName'];
	return get_class($plugin['plugin']);

Return current item: Obsessive Website Statistics