File: 0.00.1a/core/PluginManager.php (View as Code)

1: 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 CSS 108: **/ 109: private $css = array(); 110: 111: /** 112: * Array of API modes for PIMS 113: **/ 114: private $pim_api = array(); 115: 116: /** 117: * Installed PIM List 118: **/ 119: private $installed_pims = null; 120: 121: /** 122: * Constructor 123: * @param mixed $freeDESK FreeDESK instance 124: **/ 125: function PluginManager(&$freeDESK) 126: { 127: $this->DESK = &$freeDESK; 128: $this->DESK->PermissionManager->Register("sysadmin_plugins",false); 129: } 130: 131: /** 132: * Register a plugin 133: * @param mixed $plugin Plugin to register 134: **/ 135: function Register(&$plugin) 136: { 137: $this->plugins[]=$plugin; 138: } 139: 140: /** 141: * Register a page for display 142: * @param string $id Page identifier 143: * @param string $page Page path (fully-qualified) 144: * @param bool $autoperm Automatically register permission for the page (optional, default true) 145: **/ 146: function RegisterPage($id, $page, $autoperm=true) 147: { 148: $this->pages[$id]=$page; 149: if ($autoperm) 150: $this->DESK->PermissionManager->Register("page.".$id,false); 151: } 152: 153: /** 154: * Register a PIM page for use in a PIM 155: * @param string $page Page identifier 156: * @param int $id PIM internal ID 157: **/ 158: function RegisterPIMPage($page, $id) 159: { 160: $this->pim_pages[$page] = $id; 161: } 162: 163: /** 164: * Register a script for inclusion 165: * @param string $script Web path to script 166: **/ 167: function RegisterScript($script) 168: { 169: $this->scripts[] = $script; 170: } 171: 172: /** 173: * Register CSS for inclusion (after Skin CSS) 174: * @param string $css Web path to CSS 175: **/ 176: function RegisterCSS($css) 177: { 178: $this->css[]=$css; 179: } 180: 181: /** 182: * Register an API call for inclusion 183: * @param string $mode API Mode 184: * @param int $id Plugin ID 185: **/ 186: function RegisterPIMAPI($mode, $id) 187: { 188: $this->pim_api[$mode] = $id; 189: } 190: 191: /** 192: * Perform an API call 193: * @param string $mode API Call 194: * @return bool False if no API call made true if called 195: **/ 196: function API($mode) 197: { 198: if (isset($this->pim_api[$mode])) 199: if (isset($this->pims[$this->pim_api[$mode]])) 200: { 201: $this->pims[$this->pim_api[$mode]]->API($mode); 202: return true; 203: } 204: return false; 205: } 206: 207: /** 208: * Get list of scripts 209: * @return array List of scripts 210: **/ 211: function GetScripts() 212: { 213: return $this->scripts; 214: } 215: 216: /** 217: * Get list of CSS 218: * @return array List of CSS 219: **/ 220: function GetCSS() 221: { 222: return $this->css; 223: } 224: 225: /** 226: * Get a page by ID 227: * @param string $id Page identifier 228: * @return mixed Page path (fully-qualified) or bool false on failure 229: **/ 230: function GetPage($id) 231: { 232: if (isset($this->pages[$id])) 233: return $this->pages[$id]; 234: return false; 235: } 236: 237: /** 238: * Call a PIM Page 239: * @param $id Page identifier 240: * @return bool true if exists and called or false if not 241: **/ 242: function PIMPage($id) 243: { 244: if (isset($this->pim_pages[$id])) 245: { 246: $pimid = $this->pim_pages[$id]; 247: if (isset($this->pims[$pimid])) 248: { 249: $this->pims[$pimid]->Page($id); 250: return true; 251: } 252: } 253: return false; 254: } 255: 256: /** 257: * Return all plugins 258: * @return array Array of all plugins 259: **/ 260: function GetAll() 261: { 262: return $this->plugins; 263: } 264: 265: /** 266: * Return plugins by type 267: * @param string $type Plugin Type 268: * @return array List of plugins 269: **/ 270: function GetType($type) 271: { 272: $output=array(); 273: foreach($this->plugins as $plugin) 274: { 275: if ($plugin->type == $type) 276: $output[]=$plugin; 277: } 278: return $output; 279: } 280: 281: /** 282: * Load a PIM 283: * @param string $pim Plugin Module Directory 284: **/ 285: function LoadPIM($pim) 286: { 287: $filepath = $this->DESK->BaseDir."plugins/".$pim."/"; 288: $webpath = $this->DESK->BaseDir."plugins/".$pim."/"; 289: $id = $this->pim_counter++; 290: 291: include($filepath.$pim.".php"); 292: 293: $this->pims[$id] = new $pim($this->DESK, $filepath, $webpath, $id); 294: $this->pims[$id]->Start(); 295: } 296: 297: /** 298: * Load installed PIM list 299: **/ 300: private function LoadInstalledPIMS() 301: { 302: $q="SELECT * FROM ".$this->DESK->Database->Table("plugins"); 303: $r=$this->DESK->Database->Query($q); 304: 305: $this->installed_pims = array(); 306: 307: while ($row = $this->DESK->Database->FetchAssoc($r)) 308: { 309: $this->installed_pims[$row['plugin']] = array( 310: "plugin" => $row['plugin'], 311: "id" => $row['pluginid'], 312: "active" => ($row['active'] == 1) ? true : false ); 313: } 314: 315: $this->DESK->Database->Free($r); 316: } 317: 318: /** 319: * Load PIMs (load and start activated PIMs) 320: **/ 321: function LoadPIMS() 322: { 323: if ($this->installed_pims == null) 324: $this->LoadInstalledPIMS(); 325: foreach($this->installed_pims as $plugin => $data) 326: { 327: if ($data['active']) 328: $this->LoadPIM($plugin); 329: } 330: } 331: 332: /** 333: * Get a list of PIMS 334: * @return array list of PIMS 335: **/ 336: function ListPIMS() 337: { 338: if ($this->installed_pims == null) 339: $this->LoadInstalledPIMS(); 340: 341: $out = array(); 342: 343: $handle = opendir("plugins/"); 344: 345: if ($handle !== false) 346: { 347: while(false !== ($file = readdir($handle))) 348: { 349: if ($file != "." && $file != ".." && is_dir("plugins/".$file)) 350: { 351: 352: if (file_exists("plugins/".$file."/".$file.".php")) 353: { 354: $out[$file] = array( 355: "installed" => isset($this->installed_pims[$file]) ? true : false, 356: "data" => isset($this->installed_pims[$file]) ? $this->installed_pims[$file] : array() 357: ); 358: } 359: } 360: } 361: 362: closedir($handle); 363: } 364: return $out; 365: } 366: 367: /** 368: * Install PIM 369: * @param string $plugin Plugin Name 370: **/ 371: function InstallPIM($plugin) 372: { 373: $path = "plugins/".$plugin."/"; 374: $file = $path.$plugin.".php"; 375: include_once($file); 376: 377: $inst = new $plugin($this->DESK, $path, $path, 9999); 378: 379: $inst->Install(); 380: 381: $q="INSERT INTO ".$this->DESK->Database->Table("plugins")."(".$this->DESK->Database->Field("plugin").",".$this->DESK->Database->Field("active").")"; 382: $q.=" VALUES(".$this->DESK->Database->SafeQuote($plugin).",".$this->DESK->Database->Safe("0").")"; 383: 384: $this->DESK->Database->Query($q); 385: } 386: 387: /** 388: * Set a PIM as active or not 389: * @param int $id ID (pluginid in database) 390: * @param bool $active Active flag 391: **/ 392: function ActivatePIM($id, $active) 393: { 394: 395: $q="SELECT * FROM ".$this->DESK->Database->Table("plugins")." WHERE ".$this->DESK->Database->Field("pluginid")."=".$this->DESK->Database->Safe($id); 396: $r=$this->DESK->Database->Query($q); 397: 398: if ($row=$this->DESK->Database->FetchAssoc($r)) 399: { 400: $plugin = $row['plugin']; 401: $path = "plugins/".$plugin."/"; 402: $file = $path.$plugin.".php"; 403: include_once($file); 404: 405: $inst = new $plugin($this->DESK, $path, $path, 9999); 406: 407: if ($active) 408: $inst->Activate(); 409: else 410: $inst->Deactivate(); 411: 412: $q="UPDATE ".$this->DESK->Database->Table("plugins")." SET ".$this->DESK->Database->Field("active")."="; 413: if ($active) 414: $q.="1"; 415: else 416: $q.="0"; 417: $q.=" WHERE ".$this->DESK->Database->Field("pluginid")."=".$this->DESK->Database->Safe($id); 418: 419: $this->DESK->Database->Query($q); 420: } 421: 422: $this->DESK->Database->Free($r); 423: } 424: 425: 426: /** 427: * Uninstall a PIM 428: * @param int $id ID (pluginid in database) 429: **/ 430: function UninstallPIM($id) 431: { 432: 433: $q="SELECT * FROM ".$this->DESK->Database->Table("plugins")." WHERE ".$this->DESK->Database->Field("pluginid")."=".$this->DESK->Database->Safe($id); 434: $r=$this->DESK->Database->Query($q); 435: 436: if ($row=$this->DESK->Database->FetchAssoc($r)) 437: { 438: $plugin = $row['plugin']; 439: $path = "plugins/".$plugin."/"; 440: $file = $path.$plugin.".php"; 441: include_once($file); 442: 443: $inst = new $plugin($this->DESK, $path, $path, 9999); 444: 445: $inst->Uninstall(); 446: 447: $q="DELETE FROM ".$this->DESK->Database->Table("plugins")." WHERE ".$this->DESK->Database->Field("pluginid")."=".$this->DESK->Database->Safe($id); 448: 449: $this->DESK->Database->Query($q); 450: } 451: 452: $this->DESK->Database->Free($r); 453: } 454: 455: /** 456: * Call BuildMenu on all PIMs 457: **/ 458: function BuildMenu() 459: { 460: foreach($this->pims as $id => $pim) 461: $pim->BuildMenu(); 462: } 463: 464: } 465: 466: 467: ?> 468: