3 # FILE: PluginManager.php
5 # Part of the ScoutLib application support library
6 # Copyright 2009-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu
15 # ---- PUBLIC INTERFACE --------------------------------------------------
26 # save framework and directory list for later use
27 $this->AF = $AppFramework;
28 $this->DirsToSearch = $PluginDirectories;
30 # get our own database handle
33 # hook into events to load plugin PHP and HTML files
34 $this->AF->HookEvent(
"EVENT_PHP_FILE_LOAD", array($this,
"FindPluginPhpFile"),
36 $this->AF->HookEvent(
"EVENT_HTML_FILE_LOAD", array($this,
"FindPluginHtmlFile"),
39 # tell PluginCaller helper object how to get to us
55 # clear any existing errors
56 $this->ErrMsgs = array();
58 # load list of all base plugin files
59 $this->FindPlugins($this->DirsToSearch);
61 # for each plugin found
62 foreach ($this->PluginNames as $PluginName)
64 # bring in plugin class file
65 include_once($this->PluginFiles[$PluginName]);
67 # if plugin class was defined by file
68 if (class_exists($PluginName))
70 # if plugin class is a valid descendant of base plugin class
71 $Plugin =
new $PluginName;
72 if (is_subclass_of($Plugin,
"Plugin"))
74 # set hooks needed for plugin to access plugin manager services
75 $Plugin->SetCfgSaveCallback(array(__CLASS__,
"CfgSaveCallback"));
78 $this->Plugins[$PluginName] = $Plugin;
80 $this->Plugins[$PluginName]->Register();
82 # check required plugin attributes
83 $Attribs[$PluginName] = $this->Plugins[$PluginName]->GetAttributes();
84 if (!strlen($Attribs[$PluginName][
"Name"]))
86 $this->ErrMsgs[$PluginName][] =
"Plugin <b>".$PluginName.
"</b>"
87 .
" could not be loaded because it"
88 .
" did not have a <i>Name</i> attribute set.";
90 unset($this->Plugins[$PluginName]);
92 if (!strlen($Attribs[$PluginName][
"Version"]))
94 $this->ErrMsgs[$PluginName][] =
"Plugin <b>".$PluginName.
"</b>"
95 .
" could not be loaded because it"
96 .
" did not have a <i>Version</i> attribute set.";
98 unset($this->Plugins[$PluginName]);
103 $this->ErrMsgs[$PluginName][] =
"Plugin <b>".$PluginName.
"</b>"
104 .
" could not be loaded because <i>".$PluginName.
"</i> was"
105 .
" not a subclass of base <i>Plugin</i> class";
110 $this->ErrMsgs[$PluginName][] =
"Expected class <i>".$PluginName
111 .
"</i> not found in plugin file <i>"
112 .$this->PluginFiles[$PluginName].
"</i>";
116 # check plugin dependencies
117 $this->CheckDependencies();
119 # load plugin configurations
120 $this->DB->Query(
"SELECT BaseName,Cfg FROM PluginInfo");
121 $Cfgs = $this->DB->FetchColumn(
"Cfg",
"BaseName");
124 foreach ($this->Plugins as $PluginName => $Plugin)
126 # if plugin is enabled
129 # if plugin has its own subdirectory
130 if ($this->PluginHasDir[$PluginName])
132 # if plugin has its own object directory
133 $Dir = dirname($this->PluginFiles[$PluginName]);
134 if (is_dir($Dir.
"/objects"))
136 # add object directory to class autoloading list
141 # add plugin directory to class autoloading list
146 # set configuration values if available
147 if (isset($Cfgs[$PluginName]))
149 $Plugin->SetAllCfg(unserialize($Cfgs[$PluginName]));
152 # install or upgrade plugins if needed
153 $this->InstallPlugin($Plugin);
157 # check plugin dependencies again in case an install or upgrade failed
158 $this->CheckDependencies();
160 # set up plugin configuration options
161 foreach ($this->Plugins as $PluginName => $Plugin)
163 if ($ForcePluginConfigOptLoad || $this->
PluginEnabled[$PluginName])
165 $ErrMsg = $Plugin->SetUpConfigOptions();
166 if ($ErrMsg !== NULL)
168 $this->ErrMsgs[$PluginName][] =
"Configuration option setup"
169 .
" failed for plugin <b>".$PluginName.
"</b>: <i>"
176 # initialize enabled plugins
177 foreach ($this->Plugins as $PluginName => $Plugin)
181 $ErrMsg = $Plugin->Initialize();
182 if ($ErrMsg !== NULL)
184 $this->ErrMsgs[$PluginName][] =
"Initialization failed for"
185 .
" plugin <b>".$PluginName.
"</b>: <i>".$ErrMsg.
"</i>";
191 # register any events declared by enabled plugins
192 foreach ($this->Plugins as $PluginName => $Plugin)
196 $Events = $Plugin->DeclareEvents();
197 if (count($Events)) { $this->AF->RegisterEvent($Events); }
201 # hook enabled plugins to events
202 foreach ($this->Plugins as $PluginName => $Plugin)
206 $EventsToHook = $Plugin->HookEvents();
207 if (count($EventsToHook))
209 foreach ($EventsToHook as $EventName => $PluginMethods)
211 if (!is_array($PluginMethods))
212 { $PluginMethods = array($PluginMethods); }
214 foreach ($PluginMethods as $PluginMethod)
216 if ($this->AF->IsStaticOnlyEvent($EventName))
219 $Result = $this->AF->HookEvent(
220 $EventName, array($Caller,
"CallPluginMethod"));
224 $Result = $this->AF->HookEvent(
225 $EventName, array($Plugin, $PluginMethod));
227 if ($Result === FALSE)
229 $this->ErrMsgs[$PluginName][] =
230 "Unable to hook requested event <i>"
231 .$EventName.
"</i> for plugin <b>"
240 # report to caller whether any problems were encountered
241 return count($this->ErrMsgs) ? FALSE : TRUE;
251 return $this->ErrMsgs;
261 return isset($this->Plugins[$PluginName])
262 ? $this->Plugins[$PluginName] : NULL;
274 return $this->
GetPlugin($this->PageFilePlugin);
284 foreach ($this->Plugins as $PluginName => $Plugin)
286 $Info[$PluginName] = $Plugin->GetAttributes();
287 $Info[$PluginName][
"Enabled"] =
288 isset($this->PluginInfo[$PluginName][
"Enabled"])
289 ? $this->PluginInfo[$PluginName][
"Enabled"] : FALSE;
290 $Info[$PluginName][
"Installed"] =
291 isset($this->PluginInfo[$PluginName][
"Installed"])
292 ? $this->PluginInfo[$PluginName][
"Installed"] : FALSE;
293 $Info[$PluginName][
"ClassFile"] = $this->PluginFiles[$PluginName];
305 $Dependents = array();
307 foreach ($AllAttribs as $Name => $Attribs)
309 if (array_key_exists($PluginName, $Attribs[
"Requires"]))
311 $Dependents[] = $Name;
313 $Dependents = array_merge($Dependents, $SubDependents);
336 if ($NewValue !== NULL)
338 $this->DB->Query(
"UPDATE PluginInfo"
339 .
" SET Enabled = ".($NewValue ?
"1" :
"0")
340 .
" WHERE BaseName = '".addslashes($PluginName).
"'");
342 $this->PluginInfo[$PluginName][
"Enabled"] = $NewValue;
358 # if plugin is installed
359 if ($this->PluginInfo[$PluginName][
"Installed"])
361 # call uninstall method for plugin
362 $Result = $this->Plugins[$PluginName]->Uninstall();
364 # if plugin uninstall method succeeded
365 if ($Result === NULL)
367 # remove plugin info from database
368 $this->DB->Query(
"DELETE FROM PluginInfo"
369 .
" WHERE BaseName = '".addslashes($PluginName).
"'");
371 # drop our data for the plugin
372 unset($this->Plugins[$PluginName]);
373 unset($this->PluginInfo[$PluginName]);
375 unset($this->PluginNames[$PluginName]);
376 unset($this->PluginFiles[$PluginName]);
380 # report results (if any) to caller
385 # ---- PRIVATE INTERFACE -------------------------------------------------
387 private $Plugins = array();
388 private $PluginFiles = array();
389 private $PluginNames = array();
390 private $PluginHasDir = array();
391 private $PluginInfo = array();
392 private $PluginEnabled = array();
393 private $PageFilePlugin = NULL;
395 private $DirsToSearch;
396 private $ErrMsgs = array();
404 private function FindPlugins($DirsToSearch)
407 foreach ($DirsToSearch as $Dir)
409 # if directory exists
412 # for each file in directory
413 $FileNames = scandir($Dir);
414 foreach ($FileNames as $FileName)
416 # if file looks like base plugin file
417 if (preg_match(
"/^[a-zA-Z_][a-zA-Z0-9_]*\.php$/", $FileName))
420 $PluginName = preg_replace(
"/\.php$/",
"", $FileName);
421 $this->PluginNames[$PluginName] = $PluginName;
422 $this->PluginFiles[$PluginName] = $Dir.
"/".$FileName;
423 $this->PluginHasDir[$PluginName] = FALSE;
425 # else if file looks like plugin directory
426 elseif (is_dir($Dir.
"/".$FileName)
427 && preg_match(
"/^[a-zA-Z_][a-zA-Z0-9_]*/", $FileName))
429 # if there is a base plugin file in the directory
430 $PluginName = $FileName;
431 $PluginFile = $Dir.
"/".$PluginName.
"/".$PluginName.
".php";
432 if (file_exists($PluginFile))
434 # add plugin and directory to lists
435 $this->PluginNames[$PluginName] = $PluginName;
436 $this->PluginFiles[$PluginName] = $PluginFile;
437 $this->PluginHasDir[$PluginName] = TRUE;
439 # else if we don't already have a base plugin file
440 elseif (!array_key_exists($PluginName, $this->PluginFiles))
442 $this->ErrMsgs[$PluginName][] =
443 "Expected plugin file <i>".$PluginName.
".php</i> not"
444 .
" found in plugin subdirectory <i>"
445 .$Dir.
"/".$PluginName.
"</i>";
458 private function InstallPlugin($Plugin)
460 # cache all plugin info from database
461 if (count($this->PluginInfo) == 0)
463 $this->DB->Query(
"SELECT * FROM PluginInfo");
464 while ($Row = $this->DB->FetchRow())
466 $this->PluginInfo[$Row[
"BaseName"]] = $Row;
470 # if plugin was not found in database
471 $PluginName = get_class($Plugin);
472 $Attribs = $Plugin->GetAttributes();
473 if (!isset($this->PluginInfo[$PluginName]))
475 # add plugin to database
476 $this->DB->Query(
"INSERT INTO PluginInfo"
477 .
" (BaseName, Version, Installed, Enabled)"
478 .
" VALUES ('".addslashes($PluginName).
"', "
479 .
" '".addslashes($Attribs[
"Version"]).
"', "
481 .
" ".($Attribs[
"EnabledByDefault"] ? 1 : 0).
")");
483 # read plugin settings back in
484 $this->DB->Query(
"SELECT * FROM PluginInfo"
485 .
" WHERE BaseName = '".addslashes($PluginName).
"'");
486 $this->PluginInfo[$PluginName] = $this->DB->FetchRow();
489 # store plugin settings for later use
490 $this->
PluginEnabled[$PluginName] = $this->PluginInfo[$PluginName][
"Enabled"];
492 # if plugin is enabled
495 # if plugin has not been installed
496 if (!$this->PluginInfo[$PluginName][
"Installed"])
498 # set default values if present
499 $Attribs = $Plugin->GetAttributes();
500 if (isset($Attribs[
"CfgSetup"]))
502 foreach ($Attribs[
"CfgSetup"] as $CfgValName => $CfgSetup)
504 if (isset($CfgSetup[
"Default"]))
506 $Plugin->ConfigSetting($CfgValName, $CfgSetup[
"Default"]);
512 $ErrMsg = $Plugin->Install();
514 # if install succeeded
517 # mark plugin as installed
518 $this->DB->Query(
"UPDATE PluginInfo SET Installed = 1"
519 .
" WHERE BaseName = '".addslashes($PluginName).
"'");
520 $this->PluginInfo[$PluginName][
"Installed"] = 1;
527 # record error message about installation failure
528 $this->ErrMsgs[$PluginName][] =
"Installation of plugin <b>"
529 .$PluginName.
"</b> failed: <i>".$ErrMsg.
"</i>";;
534 # if plugin version is newer than version in database
535 if (version_compare($Attribs[
"Version"],
536 $this->PluginInfo[$PluginName][
"Version"]) == 1)
539 $ErrMsg = $Plugin->Upgrade($this->PluginInfo[$PluginName][
"Version"]);
541 # if upgrade succeeded
544 # update plugin version in database
545 $Attribs = $Plugin->GetAttributes();
546 $this->DB->Query(
"UPDATE PluginInfo"
547 .
" SET Version = '".addslashes($Attribs[
"Version"]).
"'"
548 .
" WHERE BaseName = '".addslashes($PluginName).
"'");
549 $this->PluginInfo[$PluginName][
"Version"] = $Attribs[
"Version"];
556 # record error message about upgrade failure
557 $this->ErrMsgs[$PluginName][] =
"Upgrade of plugin <b>"
558 .$PluginName.
"</b> from version <i>"
559 .addslashes($this->PluginInfo[$PluginName][
"Version"])
560 .
"</i> to version <i>"
561 .addslashes($Attribs[
"Version"]).
"</i> failed: <i>"
565 # else if plugin version is older than version in database
566 elseif (version_compare($Attribs[
"Version"],
567 $this->PluginInfo[$PluginName][
"Version"]) == -1)
572 # record error message about version conflict
573 $this->ErrMsgs[$PluginName][] =
"Plugin <b>"
574 .$PluginName.
"</b> is older (<i>"
575 .addslashes($Attribs[
"Version"])
576 .
"</i>) than previously-installed version (<i>"
577 .addslashes($this->PluginInfo[$PluginName][
"Version"]).
"</i>).";
587 private function CheckDependencies()
589 # look until all enabled plugins check out okay
592 # start out assuming all plugins are okay
596 foreach ($this->Plugins as $PluginName => $Plugin)
598 # if plugin is currently enabled
601 # load plugin attributes
602 if (!isset($Attribs[$PluginName]))
604 $Attribs[$PluginName] =
605 $this->Plugins[$PluginName]->GetAttributes();
608 # for each dependency for this plugin
609 foreach ($Attribs[$PluginName][
"Requires"]
610 as $ReqName => $ReqVersion)
612 # handle PHP version requirements
613 if ($ReqName ==
"PHP")
615 if (version_compare($ReqVersion, phpversion(),
">"))
617 $this->ErrMsgs[$PluginName][] =
"PHP version "
618 .
"<i>".$ReqVersion.
"</i>"
619 .
" required by <b>".$PluginName.
"</b>"
620 .
" was not available. (Current PHP version"
621 .
" is <i>".phpversion().
"</i>.)";
624 # handle PHP extension requirements
625 elseif (preg_match(
"/^PHPX_/", $ReqName))
627 list($Dummy, $ExtensionName) = split(
"_", $ReqName, 2);
628 if (!extension_loaded($ExtensionName))
630 $this->ErrMsgs[$PluginName][] =
"PHP extension "
631 .
"<i>".$ExtensionName.
"</i>"
632 .
" required by <b>".$PluginName.
"</b>"
633 .
" was not available.";
636 # handle dependencies on other plugins
639 # load plugin attributes if not already loaded
640 if (isset($this->Plugins[$ReqName])
641 && !isset($Attribs[$ReqName]))
644 $this->Plugins[$ReqName]->GetAttributes();
647 # if target plugin is not present or is disabled or is too old
650 || (version_compare($ReqVersion,
651 $Attribs[$ReqName][
"Version"],
">")))
653 # add error message and disable plugin
654 $this->ErrMsgs[$PluginName][] =
"Plugin <i>"
655 .$ReqName.
" ".$ReqVersion.
"</i>"
656 .
" required by <b>".$PluginName.
"</b>"
657 .
" was not available.";
660 # set flag indicating plugin did not check out
667 }
while ($AllOkay == FALSE);
680 # build list of possible locations for file
682 "local/plugins/%PLUGIN%/pages/%PAGE%.php",
683 "plugins/%PLUGIN%/pages/%PAGE%.php",
684 "local/plugins/%PLUGIN%/%PAGE%.php",
685 "plugins/%PLUGIN%/%PAGE%.php",
688 # look for file and return (possibly) updated page to caller
689 return $this->FindPluginPageFile($PageName, $Locations);
703 # build list of possible locations for file
705 "local/plugins/%PLUGIN%/interface/%ACTIVEUI%/%PAGE%.html",
706 "plugins/%PLUGIN%/interface/%ACTIVEUI%/%PAGE%.html",
707 "local/plugins/%PLUGIN%/interface/default/%PAGE%.html",
708 "plugins/%PLUGIN%/interface/default/%PAGE%.html",
709 "local/plugins/%PLUGIN%/%PAGE%.html",
710 "plugins/%PLUGIN%/%PAGE%.html",
714 $Params = $this->FindPluginPageFile($PageName, $Locations);
716 # if plugin HTML file was found
717 if ($Params[
"PageName"] != $PageName)
719 # add subdirectories for plugin to search paths
720 $Dir = preg_replace(
"%^local/%",
"", dirname($Params[
"PageName"]));
721 $GLOBALS[
"AF"]->AddImageDirectories(array(
722 "local/".$Dir.
"/images",
725 $GLOBALS[
"AF"]->AddIncludeDirectories(array(
726 "local/".$Dir.
"/include",
729 $GLOBALS[
"AF"]->AddFunctionDirectories(array(
730 "local/".$Dir.
"/include",
735 # return possibly revised HTML file name to caller
748 private function FindPluginPageFile($PageName, $Locations)
750 # set up return value assuming we will not find plugin page file
751 $ReturnValue[
"PageName"] = $PageName;
753 # look for plugin name and plugin page name in base page name
754 preg_match(
"/P_([A-Za-z].[A-Za-z0-9]*)_([A-Za-z0-9_-]+)/", $PageName, $Matches);
756 # if base page name contained name of enabled plugin with its own subdirectory
757 if ((count($Matches) == 3)
759 && $this->PluginHasDir[$Matches[1]])
761 # for each possible location
762 $ActiveUI = $GLOBALS[
"AF"]->ActiveUserInterface();
763 $PluginName = $Matches[1];
764 $PageName = $Matches[2];
765 foreach ($Locations as $Loc)
767 # make any needed substitutions into path
768 $FileName = str_replace(array(
"%ACTIVEUI%",
"%PLUGIN%",
"%PAGE%"),
769 array($ActiveUI, $PluginName, $PageName), $Loc);
771 # if file exists in this location
772 if (file_exists($FileName))
774 # set return value to contain full plugin page file name
775 $ReturnValue[
"PageName"] = $FileName;
777 # save plugin name as home of current page
778 $this->PageFilePlugin = $PluginName;
780 # set G_Plugin to plugin associated with current page
789 # return array containing page name or page file name to caller
803 $DB->Query(
"UPDATE PluginInfo SET Cfg = '".addslashes(serialize($Cfg))
804 .
"' WHERE BaseName = '".addslashes($BaseName).
"'");
830 $this->PluginName = $PluginName;
831 $this->MethodName = $MethodName;
841 $Args = func_get_args();
842 $Plugin = self::$Manager->GetPlugin($this->PluginName);
843 return call_user_func_array(array($Plugin, $this->MethodName), $Args);
852 return $this->PluginName.
"::".$this->MethodName;
862 return array(
"PluginName",
"MethodName");
LoadPlugins($ForcePluginConfigOptLoad=FALSE)
Load and initialize plugins.
GetErrorMessages()
Retrieve any error messages generated during plugin loading.
__sleep()
Sleep method, specifying which values are to be saved when we are serialized.
__construct($PluginName, $MethodName)
Class constructor, which stores the plugin name and the name of the method to be called.
Manager to load and invoke plugins.
static CfgSaveCallback($BaseName, $Cfg)
SQL database abstraction object with smart query caching.
UninstallPlugin($PluginName)
Uninstall plugin and (optionally) delete any associated data.
GetDependents($PluginName)
Returns a list of plugins dependent on the specified plugin.
FindPluginPhpFile($PageName)
static AddObjectDirectory($Dir, $Prefix="", $ClassPattern=NULL, $ClassReplacement=NULL)
Add directory to be searched for object files when autoloading.
GetActivePluginList()
Get list of active (i.e.
FindPluginHtmlFile($PageName)
GetPluginAttributes()
Retrieve info about currently loaded plugins.
GetPlugin($PluginName)
Retrieve specified plugin.
PluginEnabled($PluginName, $NewValue=NULL)
Get/set whether specified plugin is enabled.
__construct($AppFramework, $PluginDirectories)
PluginManager class constructor.
GetPluginForCurrentPage()
Retrieve plugin for current page (if any).
GetCallbackAsText()
Get full method name as a text string.
const ORDER_LAST
Run hooked function last (i.e.
static $Manager
PluginManager to use to retrieve appropriate plugins.
CallPluginMethod()
Call the method that was specified in our constructor.