summaryrefslogtreecommitdiff
path: root/src/lib/PluginManager.class.php
blob: 417eecc007251bf85fc399b347fddf9bd5ff5a56 (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
<?php

class PluginManager {
	public $plugins = array();
	private $loaded = false;
	/**
	 * Return an instance of the plugin with $plugin_name
	 */
	public function loadPlugin($plugin_name) {
		global $mm; $db = $mm->database();
		
		require_once("$plugin_name.class.php");
		$obj = new $plugin_name;
		$params = call_user_func("$plugin_name::configList");
		foreach ($params as $param => $type) {
			$value = $db->getPluginConf($plugin_name, $param);
			if ($value!==false) {
				switch ($type) {
				case 'text':
				case 'password':
					$value = "$value";
					break;
				case 'int':
					$value = (int)$value;
					break;
				}
				$obj->configSet($param, $value);
			}
		}
		return $obj;
	}
	
	/**
	 * Return an array of available plugin names.
	 */
	public function listPlugins() {
		$plugins = array();
		
		$dirs = explode(PATH_SEPARATOR, PLUGINPATH);
		foreach ($dirs as $dir) {
			// Find all files in $dir with the ext `.class.php'
			$files = glob($dir.'/*.class.php');
			foreach ($files as $file) {
				$plugins[] = preg_replace('@\.class\.php$@', '$1', basename($file));
			}
		}

		return $plugins;
	}
	
	/**
	 * Return an array of enabled plugin names.
	 */
	public function getActivePlugins() {
		global $mm; $db = $mm->database();
		$string = $db->getSysConf('plugins');
		return $db->valueToArray($string);
	}

	/**
	 * Set the enabled plugins.
	 */
	public function setActivePlugins($plugins) {
		global $mm; $db = $mm->database();
		$string = $db->arrayToValue($plugins);
		return $db->setSysConf('plugins', $string);
	}
	
	/**
	 * Load the enabled plugins.
	 */
	public function loadPlugins() {
		if ($this->loaded) return;
		$plugin_names = $this->getActivePlugins();
		foreach ($plugin_names as $name) {
			$this->plugins[$name] = $this->loadPlugin($name);
		}
		$this->loaded = true;
	}

	public function callHook($hook, $arg=null) {
		$this->loadPlugins();
		$ret = array();
		foreach ($this->plugins as $name => $plugin) {
			$ret[$name] = call_user_func(array($plugin, $hook),
			                             &$arg);
		}
		return $ret;
	}
}