File: 0.00.0a/core/PluginManager.php (View as HTML)

  1: <?php 
  2: /* -------------------------------------------------------------
  3: This file is part of FreeDESK
  4: 
  5: FreeDESK is (C) Copyright 2012 David Cutting
  6: 
  7: FreeDESK is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeDESK is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeDESK.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freedesk/
 21: -------------------------------------------------------------- */
 22: 
 23: /**
 24:  * Plugin class - holds all associated data with generic plugins
 25: **/
 26: class Plugin
 27: {
 28: 	/**
 29: 	 * Name
 30: 	**/
 31: 	var $name;
 32: 	/**
 33: 	 * Version (maj.min only)
 34: 	**/
 35: 	var $version;
 36: 	/**
 37: 	 * Type
 38: 	**/
 39: 	var $type="";
 40: 	/**
 41: 	 * Sub-Type
 42: 	**/
 43: 	var $subtype="";
 44: 	/**
 45: 	 * Class Name
 46: 	**/
 47: 	var $classname="";
 48: 	/**
 49: 	 * Constructor
 50: 	 * @param string $name Name of plugin (optional, default "")
 51: 	 * @param string $version Version of plugin (optional, default "")
 52: 	 * @param string $type Type of plugin (optional, default "")
 53: 	 * @param string $subtype Subtype of plugin (optional, default "")
 54: 	 * @param string $classname Class name of pluging (optional, default "")
 55: 	**/
 56: 	function Plugin($name="",$version="",$type="",$subtype="",$classname="")
 57: 	{
 58: 		$this->name=$name;
 59: 		$this->version=$version;
 60: 		$this->type=$type;
 61: 		$this->subtype=$subtype;
 62: 		$this->classname=$classname;
 63: 	}
 64: }
 65: 
 66: /**
 67:  * Plugin Manager - Handles all run-time-enabled plugins
 68: **/
 69: class PluginManager
 70: {
 71: 	/**
 72: 	 * FreeDESK instance
 73: 	**/
 74: 	private $DESK = null;
 75: 	
 76: 	/**
 77: 	 * Array of registered plugins and components
 78: 	**/
 79: 	private $plugins = array();
 80: 	
 81: 	/**
 82: 	 * Array of plugin modules
 83: 	**/
 84: 	private $pims = array();
 85: 	
 86: 	/**
 87: 	 * PIM Counter
 88: 	**/
 89: 	private $pim_counter = 0;
 90: 	
 91: 	/**
 92: 	 * Array of simple pages
 93: 	**/
 94: 	private $pages = array();
 95: 	
 96: 	/**
 97: 	 * Array of PIM pages
 98: 	**/
 99: 	private $pim_pages = array();
100: 	
101: 	/**
102: 	 * Array of scripts
103: 	**/
104: 	private $scripts = array();
105: 	
106: 	/**
107: 	 * Array of API modes for PIMS
108: 	**/
109: 	private $pim_api = array();
110: 	
111: 	/**
112: 	 * Installed PIM List
113: 	**/
114: 	private $installed_pims = null;
115: 	
116: 	/**
117: 	 * Constructor
118: 	 * @param mixed $freeDESK FreeDESK instance
119: 	**/
120: 	function PluginManager(&$freeDESK)
121: 	{
122: 		$this->DESK = &$freeDESK;
123: 		$this->DESK->PermissionManager->Register("sysadmin_plugins",false);
124: 	}
125: 	
126: 	/**
127: 	 * Register a plugin
128: 	 * @param mixed $plugin Plugin to register
129: 	**/
130: 	function Register(&$plugin)
131: 	{
132: 		$this->plugins[]=$plugin;
133: 	}
134: 	
135: 	/**
136: 	 * Register a page for display
137: 	 * @param string $id Page identifier
138: 	 * @param string $page Page path (fully-qualified)
139: 	 * @param bool $autoperm Automatically register permission for the page (optional, default true)
140: 	**/
141: 	function RegisterPage($id, $page, $autoperm=true)
142: 	{
143: 		$this->pages[$id]=$page;
144: 		if ($autoperm)
145: 			$this->DESK->PermissionManager->Register("page.".$id,false);
146: 	}
147: 	
148: 	/**
149: 	 * Register a PIM page for use in a PIM
150: 	 * @param string $page Page identifier
151: 	 * @param int $id PIM internal ID
152: 	**/
153: 	function RegisterPIMPage($page, $id)
154: 	{
155: 		$this->pim_pages[$page] = $id;
156: 	}
157: 	
158: 	/**
159: 	 * Register a script for inclusion
160: 	 * @param string $script Web path to script
161: 	**/
162: 	function RegisterScript($script)
163: 	{
164: 		$this->scripts[] = $script;
165: 	}
166: 	
167: 	/**
168: 	 * Register an API call for inclusion
169: 	 * @param string $mode API Mode
170: 	 * @param int $id Plugin ID
171: 	**/
172: 	function RegisterPIMAPI($mode, $id)
173: 	{
174: 		$this->pim_api[$mode] = $id;
175: 	}
176: 	
177: 	/**
178: 	 * Perform an API call
179: 	 * @param string $mode API Call
180: 	 * @return bool False if no API call made true if called
181: 	**/
182: 	function API($mode)
183: 	{
184: 		if (isset($this->pim_api[$mode]))
185: 			if (isset($this->pims[$this->pim_api[$mode]]))
186: 			{
187: 				$this->pims[$this->pim_api[$mode]]->API($mode);
188: 				return true;
189: 			}
190: 		return false;
191: 	}
192: 	
193: 	/**
194: 	 * Get list of scripts
195: 	 * @return array List of scripts
196: 	**/
197: 	function GetScripts()
198: 	{
199: 		return $this->scripts;
200: 	}
201: 	
202: 	/**
203: 	 * Get a page by ID
204: 	 * @param string $id Page identifier
205: 	 * @return mixed Page path (fully-qualified) or bool false on failure
206: 	**/
207: 	function GetPage($id)
208: 	{
209: 		if (isset($this->pages[$id]))
210: 			return $this->pages[$id];
211: 		return false;
212: 	}
213: 	
214: 	/**
215: 	 * Call a PIM Page
216: 	 * @param $id Page identifier
217: 	 * @return bool true if exists and called or false if not
218: 	**/
219: 	function PIMPage($id)
220: 	{
221: 		if (isset($this->pim_pages[$id]))
222: 		{
223: 			$pimid = $this->pim_pages[$id];
224: 			if (isset($this->pims[$pimid]))
225: 			{
226: 				$this->pims[$pimid]->Page($id);
227: 				return true;
228: 			}
229: 		}
230: 		return false;
231: 	}
232: 	
233: 	/**
234: 	 * Return all plugins
235: 	 * @return array Array of all plugins
236: 	**/
237: 	function GetAll()
238: 	{
239: 		return $this->plugins;
240: 	}
241: 	
242: 	/**
243: 	 * Return plugins by type
244: 	 * @param string $type Plugin Type
245: 	 * @return array List of plugins
246: 	**/
247: 	function GetType($type)
248: 	{
249: 		$output=array();
250: 		foreach($this->plugins as $plugin)
251: 		{
252: 			if ($plugin->type == $type)
253: 				$output[]=$plugin;
254: 		}
255: 		return $output;
256: 	}
257: 	
258: 	/**
259: 	 * Load a PIM
260: 	 * @param string $pim Plugin Module Directory
261: 	**/
262: 	function LoadPIM($pim)
263: 	{
264: 		$filepath = "plugins/".$pim."/";
265: 		$webpath = "plugins/".$pim."/";
266: 		$id = $this->pim_counter++;
267: 		
268: 		include($filepath.$pim.".php");
269: 		
270: 		$this->pims[$id] = new $pim($this->DESK, $filepath, $webpath, $id);
271: 		$this->pims[$id]->Start();
272: 	}
273: 	
274: 	/**
275: 	 * Load installed PIM list
276: 	**/
277: 	private function LoadInstalledPIMS()
278: 	{
279: 		$q="SELECT * FROM ".$this->DESK->Database->Table("plugins");
280: 		$r=$this->DESK->Database->Query($q);
281: 		
282: 		$this->installed_pims = array();
283: 		
284: 		while ($row = $this->DESK->Database->FetchAssoc($r))
285: 		{
286: 			$this->installed_pims[$row['plugin']] = array(
287: 				"plugin" => $row['plugin'],
288: 				"id" => $row['pluginid'],
289: 				"active" => ($row['active'] == 1) ? true : false );
290: 		}
291: 		
292: 		$this->DESK->Database->Free($r);
293: 	}
294: 	
295: 	/**
296: 	 * Load PIMs (load and start activated PIMs)
297: 	**/
298: 	function LoadPIMS()
299: 	{
300: 		if ($this->installed_pims == null)
301: 			$this->LoadInstalledPIMS();
302: 		foreach($this->installed_pims as $plugin => $data)
303: 		{
304: 			if ($data['active'])
305: 				$this->LoadPIM($plugin);
306: 		}
307: 	}
308: 	
309: 	/**
310: 	 * Get a list of PIMS
311: 	 * @return array list of PIMS
312: 	**/
313: 	function ListPIMS()
314: 	{
315: 		if ($this->installed_pims == null)
316: 			$this->LoadInstalledPIMS();
317: 		
318: 		$out = array();
319: 			
320: 		$handle = opendir("plugins/");
321: 		
322: 		if ($handle !== false)
323: 		{
324: 			while(false !== ($file = readdir($handle)))
325: 			{
326: 				if ($file != "." && $file != ".." && is_dir("plugins/".$file))
327: 				{
328: 				
329: 					if (file_exists("plugins/".$file."/".$file.".php"))
330: 					{
331: 						$out[$file] = array(
332: 							"installed" => isset($this->installed_pims[$file]) ? true : false,
333: 							"data" => isset($this->installed_pims[$file]) ? $this->installed_pims[$file] : array()
334: 							);
335: 					}
336: 				}
337: 			}
338: 		
339: 			closedir($handle);
340: 		}
341: 		return $out;
342: 	}
343: 	
344: 	/**
345: 	 * Install PIM
346: 	 * @param string $plugin Plugin Name
347: 	**/
348: 	function InstallPIM($plugin)
349: 	{
350: 		$path = "plugins/".$plugin."/";
351: 		$file = $path.$plugin.".php";
352: 		include_once($file);
353: 		
354: 		$inst = new $plugin($this->DESK, $path, $path, 9999);
355: 		
356: 		$inst->Install();
357: 		
358: 		$q="INSERT INTO ".$this->DESK->Database->Table("plugins")."(".$this->DESK->Database->Field("plugin").",".$this->DESK->Database->Field("active").")";
359: 		$q.=" VALUES(".$this->DESK->Database->SafeQuote($plugin).",".$this->DESK->Database->Safe("0").")";
360: 		
361: 		$this->DESK->Database->Query($q);
362: 	}
363: 	
364: 	/**
365: 	 * Set a PIM as active or not
366: 	 * @param int $id ID (pluginid in database)
367: 	 * @param bool $active Active flag
368: 	**/
369: 	function ActivatePIM($id, $active)
370: 	{
371: 	
372: 		$q="SELECT * FROM ".$this->DESK->Database->Table("plugins")." WHERE ".$this->DESK->Database->Field("pluginid")."=".$this->DESK->Database->Safe($id);
373: 		$r=$this->DESK->Database->Query($q);
374: 		
375: 		if ($row=$this->DESK->Database->FetchAssoc($r))
376: 		{
377: 			$plugin = $row['plugin'];
378: 			$path = "plugins/".$plugin."/";
379: 			$file = $path.$plugin.".php";
380: 			include_once($file);
381: 		
382: 			$inst = new $plugin($this->DESK, $path, $path, 9999);
383: 			
384: 			if ($active)
385: 				$inst->Activate();
386: 			else
387: 				$inst->Deactivate();
388: 		
389: 			$q="UPDATE ".$this->DESK->Database->Table("plugins")." SET ".$this->DESK->Database->Field("active")."=";
390: 			if ($active)
391: 				$q.="1";
392: 			else
393: 				$q.="0";
394: 			$q.=" WHERE ".$this->DESK->Database->Field("pluginid")."=".$this->DESK->Database->Safe($id);
395: 		
396: 			$this->DESK->Database->Query($q);
397: 		}
398: 		
399: 		$this->DESK->Database->Free($r);
400: 	}
401: 		
402: 		
403: 	/**
404: 	 * Uninstall a PIM
405: 	 * @param int $id ID (pluginid in database)
406: 	**/
407: 	function UninstallPIM($id)
408: 	{
409: 	
410: 		$q="SELECT * FROM ".$this->DESK->Database->Table("plugins")." WHERE ".$this->DESK->Database->Field("pluginid")."=".$this->DESK->Database->Safe($id);
411: 		$r=$this->DESK->Database->Query($q);
412: 		
413: 		if ($row=$this->DESK->Database->FetchAssoc($r))
414: 		{
415: 			$plugin = $row['plugin'];
416: 			$path = "plugins/".$plugin."/";
417: 			$file = $path.$plugin.".php";
418: 			include_once($file);
419: 		
420: 			$inst = new $plugin($this->DESK, $path, $path, 9999);
421: 			
422: 			$inst->Uninstall();
423: 		
424: 			$q="DELETE FROM ".$this->DESK->Database->Table("plugins")." WHERE ".$this->DESK->Database->Field("pluginid")."=".$this->DESK->Database->Safe($id);
425: 		
426: 			$this->DESK->Database->Query($q);
427: 		}
428: 		
429: 		$this->DESK->Database->Free($r);
430: 	}
431: 	
432: 	/**
433: 	 * Call BuildMenu on all PIMs
434: 	**/
435: 	function BuildMenu()
436: 	{
437: 		foreach($this->pims as $id => $pim)
438: 			$pim->BuildMenu();
439: 	}
440: 	
441: }
442: 
443: 
444: ?>
445: