Location: PHPKode > projects > Write2Left > Base/Installer.php
<?php
/*
+------------------------------------------------------
| Write2Left
| (c) timdorr
| http://www.write2left.com
| hide@address.com
| See License.txt for license info
|------------------------------------------------------
| Script: Plugins.php
| Description:
|   Mangages plugins and their installation
| Created Aug-8-2002
+------------------------------------------------------
*/

/* Class: Installer
 * Description:
 *   Driver for plugin installer
 */
class Installer
{
	var $skin    = '';
	var $menu    = true;
	var $message = '';
	var $error   = '';
	var $plugins = array();
		
	function run()
	{
		global $W2L, $userinfo, $output, $db;
		
		// Do various skin thingys
		require( "./Skin/Installer.php" );
		$this->skin = new Skin_Installer();
		$output->page_title = "Installer";
		$output->loc_add( "Installer" );
		$output->add( $this->skin->body_top() );


		// Open up the plugin directory
		$plug_dir = opendir( './Plugins' );

		// Now load up control panel plugins
		$cp_plugins = array();
        while( ( $file = readdir( $plug_dir ) ) !== false ) 
        { 
			if( is_dir( './Plugins/' . $file ) )
				continue;
            if( substr( $file, 0, 3 ) != 'cp_' )
                continue;
            if( substr( $file, -4 ) != '.php' )
                continue;

			include( './Plugins/' . $file );
        }

		$this->plugins =& $cp_plugins;

		// Play nice with the filesystem
		closedir( $plug_dir );  



		// Get the currently installed plugins
		$db->query( "SELECT * FROM w2l_plugins" );
		$installed = array( '' );
		while( $plugin = $db->fetch_array() )
		{
			if( array_key_exists( $plugin['name'], $cp_plugins ) )
				$installed[] = $plugin['name'];
		}



		// Check for a method
		if( array_key_exists( 'M', $W2L->input ) )
		{
			if( $W2L->input['M'] == 'Config' )
				$this->config_plugin( $W2L->input['name'] );
			if( $W2L->input['M'] == 'Install' )
				$this->install_plugin( $W2L->input['name'], $installed );
			if( $W2L->input['M'] == 'Remove' )
			{
				$this->remove_plugin( $W2L->input['name'], $installed );
				unset( $installed[array_search( $W2L->input['name'], $installed )] );
			}
		}


		// Show whatever result we get, if any
		if( $this->error != "" )
			$output->add( "<div class=\"error\">\n" . $this->error . "\n</div><br />\n" );
		if( $this->message != "" )
			$output->add( "<div class=\"message\">\n" . $this->message . "\n</div><br />\n" );



		// Display the table of plugins
		$output->add( $this->skin->plugin_head() );
		$plugins = array();

		// First get the installed ones
		foreach( $installed as $plugin )
		{
			if( array_key_exists( $plugin, $cp_plugins ) )
			{
				$p = array();
				$p['name'] = $plugin;
				$p['status'] = 'Installed';
				$p['action'] = 'Remove';
				$plugins[] = $p;
			}
		}

		// Now the non-installed ones
		foreach( array_diff( array_keys( $this->plugins ), $installed ) as $plugin )
		{
				$p = array();
				$p['name'] = $plugin;
				$p['status'] = 'Not Installed';
				$p['action'] = 'Install';
				$plugins[] = $p;
		}

		// Sort the list so things don't go in wacky order, confusing the user
		sort( $plugins );

		// Print the list of plugins
		foreach( $plugins as $plugin )
		{
			$output->add( $this->skin->plugin_row( $plugin['name'],
												   $plugin['status'],
												   $plugin['action'] ) );
		}

		$output->add( $this->skin->plugin_foot() );

		$output->add( $this->skin->body_bottom() );
	}

	//================
	// Runs a plugin's install method and adds it to the database
	//================
	function install_plugin( $name, &$installed )
	{
		global $db;

		$result = '';

		if( $this->plugins[$name]->install( $result ) )
		{
			$this->message = $result;
			$db->query( "INSERT INTO w2l_plugins VALUES( NULL,
														 '$name',
														 '{$this->plugins[$name]->filename}' )" );
			$installed[] = $name;
		}
		else
			$this->error = $result;
	}

	//================
	// Runs a plugin's remove method and removes it from the database
	//================
	function remove_plugin( $name )
	{
		global $db;

		$result = '';

		if( $this->plugins[$name]->remove( $result ) )
		{
			$this->message = $result;
			$db->query( "DELETE FROM w2l_plugins WHERE name = '$name'" );
		}
		else
			$this->error = $result;
	}

	//================
	// Runs a plugin's general configuration method
	//================
	function config_plugin( $name )
	{
		global $db;

		$id = $db->query( "SELECT * FROM w2l_plugins WHERE name = '$name'" );
		if( $db->num_rows( $id ) != 0 )
			$this->plugins[$name]->config();
		else
			$this->error = 'This plugin is not installed.';
	}
}

$driver = new Installer();
?>
Return current item: Write2Left