summaryrefslogtreecommitdiff
path: root/NexusSystem.php
blob: 68489cd93fac153438b08cc1b2d03453d6a383cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* Nexus system class for handling the menu plugins
*
* @abstract
* @author   xing <xing@synapse.plus.com>
* @copied   copied from LibertySystem.php
* @version  $Revision: 1.1 $
* @package  Nexus
*/

// for menus that use regular HTML as output
define( 'NEXUS_HTML_PLUGIN', 'nexushtml' );
define( 'NEXUS_DEFAULT_GUID', 'suckerfish' );

require_once( KERNEL_PKG_PATH.'BitBase.php' );

class NexusSystem extends BitBase {

	var $mPlugins;

	function NexusSystem() {
		BitBase::BitBase();
		$this->loadPlugins();
	}

	function loadPlugins( $pCacheTime=BIT_QUERY_CACHE_TIME ) {
		$rs = $this->query( "SELECT * FROM `".BIT_DB_PREFIX."tiki_nexus_plugins`", NULL, BIT_QUERY_DEFAULT, BIT_QUERY_DEFAULT );
		while( $rs && !$rs->EOF ) {
			$this->mPlugins[$rs->fields['plugin_guid']] = $rs->fields;
			$rs->MoveNext();
		}
	}

	function scanPlugins() {
		$pluginsPath = NEXUS_PKG_PATH.'plugins/';
		if( $pluginDir = opendir( $pluginsPath ) ) {
			// Scan the plugins directory for plugins
			while( FALSE !== ( $plugin = readdir( $pluginDir ) ) ) {
				if( preg_match( '/\.php$/', $plugin ) ) {
					include_once( $pluginsPath.$plugin );
				}
			}
		}

		// match up storage_type_id to plugin_guids. this _id varies from install to install, but guids are the same
		foreach( array_keys( $this->mPlugins ) as $guid ) {
			$handler = &$this->mPlugins[$guid]; //shorthand var alias
			if( !isset( $handler['verified'] ) && $handler['is_active'] =='y' ) {
				// We are missing a plugin!
				$sql = "UPDATE `".BIT_DB_PREFIX."tiki_nexus_plugins` SET `is_active`='x' WHERE `plugin_guid`=?";
				$this->query( $sql, array( $guid ) );
				$handler['is_active'] = 'n';
			} elseif( !empty( $handler['verified'] ) && $handler['is_active'] =='x' ) {
				//We found a formally missing plugin - re-enable it
				$sql = "UPDATE `".BIT_DB_PREFIX."tiki_nexus_plugins` SET `is_active`='y' WHERE `plugin_guid`=?";
				$this->query( $sql, array( $guid ) );
				$handler['is_active'] = 'y';
			} elseif( empty( $handler['verified'] ) && !isset( $handler['is_active'] ) ) {
				//We found a missing plugin - insert it
				$sql = "INSERT INTO `".BIT_DB_PREFIX."tiki_nexus_plugins` ( `plugin_guid`, `plugin_type`, `plugin_description`, `is_active` ) VALUES ( ?, ?, ?, 'y' )";
				$this->query( $sql, array( $guid, $handler['plugin_type'], $handler['description'] ) );
				$handler['is_active'] = 'y';
			}
		}
		if( !empty( $sql ) ) {
			// we just ran some SQL - let's flush the loadPlugins query cache
			$this->loadPlugins( 0 );
		}
		asort( $this->mPlugins );
	}

	function registerPlugin( $pGuid, $pPluginParams ) {
		if( isset( $this->mPlugins[$pGuid] ) ) {
			$this->mPlugins[$pGuid]['verified'] = TRUE;
		} else {
			$this->mPlugins[$pGuid]['verified'] = FALSE;
		}
		$this->mPlugins[$pGuid] = array_merge( $this->mPlugins[$pGuid], $pPluginParams );
	}

	// @parameter pPluginGuids an array of all the plugin guids that are active. Any left out are *inactive*!
	function setActivePlugins( $pPluginGuids ) {
		if( is_array( $pPluginGuids ) ) {
			$sql = "UPDATE `".BIT_DB_PREFIX."tiki_nexus_plugins` SET `is_active`='n' WHERE `is_active`!='x'";
			$this->query( $sql );
			foreach( array_keys( $this->mPlugins ) as $guid ) {
				$this->mPlugins[$guid]['is_active'] = 'n';
			}

			foreach( array_keys( $pPluginGuids ) as $guid ) {
				$sql = "UPDATE `".BIT_DB_PREFIX."tiki_nexus_plugins` SET `is_active`='y' WHERE `plugin_guid`=?";
				$this->query( $sql, array( $guid ) );
				$this->mPlugins[$guid]['is_active'] = 'y';
			}
			// we just ran some SQL - let's flush the loadPlugins query cache
			$this->loadPlugins( 0 );
		}
	}

	function getPluginFunction( $pGuid, $pFunctionName ) {
		$ret = NULL;
		if( !empty( $pGuid ) 
			&& !empty( $this->mPlugins[$pGuid] ) 
			&& !empty( $this->mPlugins[$pGuid][$pFunctionName] ) 
			&& function_exists( $this->mPlugins[$pGuid][$pFunctionName] ) 
		) {
			$ret = $this->mPlugins[$pGuid][$pFunctionName];
		}
		return $ret;
	}

	/**
	 * fucntion to store pluging settings
	 * $param $pParamHash contains settings for any guid that require updating
	 * return TRUE
	 */
	function storePluginSettings( $pParamHash ) {
		// first get all values from tiki_nexus_plugin_settings to see which ones need updating and which ones are added for the first time
		$rs = $this->query( "SELECT * FROM `".BIT_DB_PREFIX."tiki_nexus_plugin_settings`", NULL );
		while( $rs && !$rs->EOF ) {
			$settings[] = $rs->fields;
			$rs->MoveNext();
		}
		vd($settings);
	}
}

global $gNexusSystem;
$gNexusSystem = new NexusSystem();
$gNexusSystem->scanPlugins();
$smarty->assign_by_ref( 'gNexusSystem', $gNexusSystem );
?>