File: PluginManager.class.php

package info (click to toggle)
fusionforge 5.3.2%2B20141104-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 60,472 kB
  • sloc: php: 271,846; sql: 36,817; python: 14,575; perl: 6,406; sh: 5,980; xml: 4,294; pascal: 1,411; makefile: 911; cpp: 52; awk: 27
file content (387 lines) | stat: -rw-r--r-- 10,234 bytes parent folder | download | duplicates (3)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/**
 * FusionForge plugin system
 *
 * Copyright 2002, 2009, Roland Mas
 * http://fusionforge.org
 *
 * This file is part of FusionForge. FusionForge is free software;
 * you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the Licence, or (at your option)
 * any later version.
 *
 * FusionForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

class PluginManager extends Error {
	var $plugins_objects;
	var $plugins_to_hooks;
	var $hooks_to_plugins;
	var $returned_values = array();

	/**
	 * PluginManager() - constructor
	 *
	 */
	function PluginManager() {
		$this->Error();
		$this->plugins_objects = array();
		$this->plugins_to_hooks = array();
		$this->hooks_to_plugins = array();
	}
	//to work with Codendi files
	static function instance() {
		global $PLUGINMANAGER_OBJ;
		if (!isset($PLUGINMANAGER_OBJ) || !$PLUGINMANAGER_OBJ) {
			$PLUGINMANAGER_OBJ = new PluginManager;
		}
		return $PLUGINMANAGER_OBJ;

	}
	/**
	 * GetPlugins() - get a list of installed plugins
	 *
	 * @return	hash of plugin id => plugin names
	 */
	function GetPlugins() {
		if (!isset($this->plugins_data)) {
			$this->plugins_data = array();
			$res = db_query_params('SELECT plugin_id, plugin_name FROM plugins',
					array());
			$rows = db_numrows($res);
			for ($i=0; $i<$rows; $i++) {
				$plugin_id = db_result($res, $i, 'plugin_id');
				$this->plugins_data[$plugin_id] = db_result($res, $i, 'plugin_name');
			}
		}
		return $this->plugins_data;
	}

	/**
	 * GetPluginObject() - get a particular plugin object
	 *
	 * @param	string	$pluginname	name of plugin
	 * @return	object	plugin object or false if not available
	 */
	function GetPluginObject($pluginname) {
		if (!isset($this->plugins_objects[$pluginname])) {
			return false;
		}
		return $this->plugins_objects[$pluginname];
	}

	//Added for Codendi compatibility
	function getPluginByName($pluginname) {
		return @$this->plugins_objects[$pluginname];
	}

	/**
	 * PluginIsInstalled() - is a plugin installed?
	 *
	 * @param	string	$pluginname	name of plugin
	 * @return	bool	true if installed
	 */
	function PluginIsInstalled($pluginname) {
		$plugins_data = $this->getPlugins();
		foreach ($plugins_data as $p_id => $p_name) {
			if ($p_name == $pluginname) {
				return true;
			}
		}
		return false;
	}

	/**
	 * isPluginAvailable() - is a plugin available?
	 *
	 * @param	string	$plugin	name of plugin
	 * @return	bool	true if installed
	 */
	function isPluginAvailable($plugin) {
		$pluginname = $plugin->GetName();
		$plugins_data = $this->getPlugins();
		foreach ($plugins_data as $p_id => $p_name) {
			if ($p_name == $pluginname) {
				return true;
			}
		}
		return false;
	}
	function activate($pluginname) {
		$res = db_query_params('INSERT INTO plugins (plugin_name,plugin_desc) VALUES ($1,$2)',
				array($pluginname, "This is the $pluginname plugin"));

		$res = db_query_params ('SELECT plugin_id, plugin_name FROM plugins WHERE plugin_name=$1',
				array($pluginname));
		if (db_numrows($res) == 1) {
			$plugin_id = db_result($res, 0, 'plugin_id');
			$this->plugins_data[$plugin_id] = db_result($res,0,'plugin_name');
		} else {
			return false;
		}
		return $res;
	}

	function deactivate($pluginname) {
		$res = db_query_params('DELETE FROM plugins WHERE plugin_name = $1', array($pluginname));

		$p_id = NULL;
		
		if (!isset($this->plugins_data)) {
			$this->plugins_data = array();
		}
		foreach ($this->plugins_data as $i => $n) {
			if ($n == $pluginname) {
				$p_id = $i;
			}
		}
		if ($p_id != NULL) {
			unset($this->plugins_data[$p_id]);
		}
		return $res;
	}

	/**
	 * LoadPlugin() - load a specific plugin
	 *
	 */
	function LoadPlugin($p_name) {
		global $gfplugins, $gfcommon, $gfwww;

		$plugins_data = $this->GetPlugins();
		$include_path = forge_get_config('plugins_path');
		$filename = $include_path . '/'. $p_name . "/common/".$p_name."-init.php";
		if (file_exists($filename)) {
			require_once ($filename);
		} else {
			$filename = $include_path . '/'. $p_name . "/common/".$p_name."Plugin.class.php";
			if (file_exists($filename)) {
				require_once($filename);
				$p_class = $p_name.'Plugin';
				register_plugin (new $p_class);
			} else { //if we didn't find it in common/ it may be an old plugin that has its files in include/
				$filename = $include_path . '/' . $p_name . "/include/".$p_name."-init.php";
				if (file_exists($filename)) {
					require_once ($filename);
				} else {
					// we can't find the plugin so we remove it from the array
					foreach ($plugins_data as $i => $n) {
						if ($n == $p_name) {
							$p_id = $i;
						}
					}
					unset($this->plugins_data[$p_id]);
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * LoadPlugins() - load available plugins
	 *
	 */
	function LoadPlugins() {
		$plugins_data = $this->GetPlugins();
		foreach ($plugins_data as $p_id => $p_name) {
			if (!$this->LoadPlugin($p_name)) {
				// we can't find the plugin so we remove it from the array
				unset($this->plugins_data[$p_id]);
			}
		}
		return true;
	}

	/**
	 * SetupHooks() - setup all hooks for installed plugins
	 *
	 */
	function SetupHooks() {
		$this->hooks_to_plugins = array();
		foreach ($this->plugins_to_hooks as $p_name => $hook_list) {
			foreach ($hook_list as $hook_name) {
				if (!isset($this->hooks_to_plugins[$hook_name])) {
					$this->hooks_to_plugins[$hook_name] = array();
				}
				$this->hooks_to_plugins[$hook_name][] = $p_name;
			}
		}
		return true;
	}

	/**
	 * RegisterPlugin() - register a plugin
	 *
	 * @param	object	$pluginobject	an object of a subclass of the Plugin class
	 * @return	bool
	 */
	function RegisterPlugin(&$pluginobject) {
		if (!$pluginobject->GetName()) {
			exit_error(_("Some plugin did not provide a name. I'd gladly tell you which one, but obviously I cannot. Sorry."),'');
		}
		$p_name = $pluginobject->GetName();
		$this->plugins_objects[$p_name] =& $pluginobject;
		$this->plugins_to_hooks[$p_name] = array_unique($pluginobject->GetHooks());
		return true;
	}

	/**
	 * RunHooks() - call hooks from a particular point
	 *
	 * @param	string	$hookname - name of the hook
	 * @param	array	$params - array of extra parameters
	 *
	 * @return	boolean, true if all returned true.
	 */
	function RunHooks($hookname, & $params) {
		$result = true;
		$this->returned_values[$hookname] = array();
		if (isset($this->hooks_to_plugins[$hookname])) {
			$p_list = $this->hooks_to_plugins[$hookname];
			foreach ($p_list as $p_name) {
				$p_obj = $this->plugins_objects[$p_name];
				if (method_exists($p_obj, $hookname)) {
					$returned = $p_obj->$hookname($params);
				} else {
					$returned = $p_obj->CallHook($hookname, $params);
				}
				$this->returned_values[$hookname][$p_name] = $returned;
				$result = $result && $returned;
			}
		}
		// Return true only if all the plugins have returned true.
		return $result;
	}

	function getReturnedValues($hookname) {
		return $this->returned_values[$hookname];
	}

	function getReturnedValuesAsString($hookname) {
		$return = '';

		if (isset($this->returned_values[$hookname])) {
			foreach ($this->returned_values[$hookname] as $value) {
				$return .= $value;
			}
		}

		return $return;
	}

	/**
	 * CountHookListeners() - number of listeners on a particular hook
	 *
	 * @param	string	name of the hook
	 * @return	int	nb of listeners for this hookname
	 */
	function CountHookListeners($hookname) {
		if (isset($this->hooks_to_plugins[$hookname])) {
			$p_list = $this->hooks_to_plugins[$hookname];
			return count($p_list);
		} else {
			return 0;
		}
	}

	function isPluginAllowedForProject($p, $group_id) {
		$Group = group_get_object($group_id);
		return $Group->usesPlugin($p->getName());
	}
}

/**
 * plugin_manager_get_object() - get the PluginManager object
 *
 * @return	PluginManager the PluginManager object
 */
function &plugin_manager_get_object() {
	global $PLUGINMANAGER_OBJ;
	if (!isset($PLUGINMANAGER_OBJ) || !$PLUGINMANAGER_OBJ) {
		$PLUGINMANAGER_OBJ = new PluginManager;
	}
	return $PLUGINMANAGER_OBJ;
}

/**
 * plugin_get_object() - get a particular Plugin object
 *
 * @param	string	$pluginname - a plugin name
 * @return	Plugin The Plugin object
 */
function &plugin_get_object($pluginname) {
	global $PLUGINMANAGER_OBJ;
	$result=$PLUGINMANAGER_OBJ->Getpluginobject($pluginname);
	return $result;
}

/**
 * register_plugin() - register a plugin
 *
 * @param	pluginobject - an object of a subclass of the Plugin class
 * @return	bool
 */
function register_plugin(&$pluginobject) {
	$pm =& plugin_manager_get_object();
	return $pm->RegisterPlugin($pluginobject);
}

/**
 * plugin_hook() - run a set of hooks
 *
 * @param	string	$hookname - name of the hook
 * @param	array		$params - parameters for the hook
 * @return	bool
 */
function plugin_hook($hookname, $params = array()) {
	$pm =& plugin_manager_get_object();
	return $pm->RunHooks($hookname, $params);
}

/**
 * plugin_hook_by_reference() - run a set of hooks with params passed by reference
 *
 * @param	string	$hookname - name of the hook
 * @param	array	$params - parameters for the hook
 * @return	bool
 */
function plugin_hook_by_reference($hookname, &$params) {
	$pm =& plugin_manager_get_object();
	return $pm->RunHooks($hookname, $params);
}

/**
 * plugin_hook_listeners() - count the number of listeners on a hook
 *
 * @param	string	$hookname - name of the hook
 * @return	int
 */
function plugin_hook_listeners($hookname) {
	$pm =& plugin_manager_get_object();
	return $pm->CountHookListeners($hookname);
}

/**
 * setup_plugin_manager() - initialise the plugin infrastructure
 *
 */
function setup_plugin_manager() {
	$pm =& plugin_manager_get_object();
	$pm->LoadPlugins();
	$pm->SetupHooks();
	return true;
}

// Local Variables:
// mode: php
// c-file-style: "bsd"
// End: