3 * FusionForge plugin system
5 * Copyright 2002, Roland Mas
6 * Copyright 2010, Franck Villaume - Capgemini
7 * Copyright 2001-2009, Xerox Corporation, Codendi Team
8 * Copyright 2010, Mélanie Le Bail
9 * Copyright 2011, Alain Peyrat - Alcatel-Lucent
10 * http://fusionforge.org
12 * This file is part of FusionForge. FusionForge is free software;
13 * you can redistribute it and/or modify it under the terms of the
14 * GNU General Public License as published by the Free Software
15 * Foundation; either version 2 of the Licence, or (at your option)
18 * FusionForge is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 class Plugin extends Error {
37 * Plugin() - constructor
41 function Plugin($id=0) {
44 $this->hooks = array();
48 * GetHooks() - get list of hooks to subscribe to.
50 * @return array List of strings.
56 * _addHooks() - add a hook to the list of hooks.
58 * @return string name of the added hook
60 function _addHook($name) {
61 return $this->hooks[]=$name;
65 * GetName() - get plugin name.
67 * @return string the plugin name.
74 * getInstallDir() - get installation dir for the plugin.
76 * @return string the directory where the plugin should be linked.
78 function getInstallDir() {
79 if (isset($this->installdir) && $this->installdir)
80 return $this->installdir;
82 return 'plugins/'.$this->name;
86 * provide() - return true if plugin provides the feature.
88 * @return bool if feature is provided or not.
90 function provide($feature) {
91 return (isset($this->provides[$feature]) && $this->provides[$feature]);
95 * Added for Codendi compatibility
96 * getPluginPath() - get installation dir for the plugin.
98 * @return string the directory where the plugin should be linked.
100 function getPluginPath() {
101 if (isset($this->installdir) && $this->installdir)
102 return $this->installdir;
104 return 'plugins/'.$this->name;
108 * CallHook() - call a particular hook.
110 * @param string the "handle" of the hook.
111 * @param array array of parameters to pass the hook.
112 * @return bool true only
114 function CallHook($hookname, &$params) {
119 * getGroups - get a list of all groups using a plugin.
121 * @return array array containing group objects.
123 function getGroups() {
125 $res = db_query_params('SELECT group_plugin.group_id
126 FROM group_plugin, plugins
127 WHERE group_plugin.plugin_id=plugins.plugin_id
128 AND plugins.plugin_name=$1
129 ORDER BY group_plugin.group_id ASC',
131 $rows = db_numrows($res);
133 for ($i=0; $i<$rows; $i++) {
134 $group_id = db_result($res,$i,'group_id');
135 $result[] = group_get_object($group_id);
141 * getThemePath - returns the directory of the theme for this plugin
143 * @return string the directory
145 function getThemePath(){
146 return util_make_url('plugins/'.$this->name.'/themes/default');
149 function registerRoleValues(&$params, $values) {
150 $role =& $params['role'];
154 $this->installCode();
155 $this->installConfig();
156 $this->installDatabase();
159 function installCode() {
161 $path = forge_get_config('plugins_path') . '/' . $name;
162 $installdir = $this->getInstallDir();
164 // Create a symbolic links to plugins/<plugin>/www (if directory exists).
165 if (is_dir($path . '/www')) { // if the plugin has a www dir make a link to it
166 // The apache group or user should have write perms the www/plugins folder...
167 if (!is_link('../'.$installdir)) {
168 $code = symlink($path . '/www', '../'.$installdir);
170 $this->setError('['.'../'.$installdir.'->'.$path . '/www]'.
171 '<br />Soft link to www couldn\'t be created. Check the write permissions for apache in gforge www/plugins dir or create the link manually.');
176 // Create a symbolic links to plugins/<plugin>/etc/plugins/<plugin> (if directory exists).
177 if (is_dir($path . '/etc/plugins/' . $name)) {
178 // The apache group or user should have write perms in /etc/gforge/plugins folder...
179 if (!is_link(forge_get_config('config_path'). '/plugins/'.$name) && !is_dir(forge_get_config('config_path'). '/plugins/'.$name)) {
180 $code = symlink($path . '/etc/plugins/' . $name, forge_get_config('config_path'). '/plugins/'.$name);
182 $this->setError('['.forge_get_config('config_path'). '/plugins/'.$name.'->'.$path . '/etc/plugins/' . $name . ']'.
183 _('<br />Config file could not be linked to etc/gforge/plugins/%1$s. Check the write permissions for apache in /etc/gforge/plugins or create the link manually.'), $name);
189 function installConfig() {
191 $path = forge_get_config('plugins_path') . '/' . $name;
193 // Create a symbolic links to plugins/<plugin>/etc/plugins/<plugin> (if directory exists).
194 if (is_dir($path . '/etc/plugins/' . $name)) {
195 // The apache group or user should have write perms in /etc/gforge/plugins folder...
196 if (!is_link(forge_get_config('config_path'). '/plugins/'.$name) && !is_dir(forge_get_config('config_path'). '/plugins/'.$name)) {
197 $code = symlink($path . '/etc/plugins/' . $name, forge_get_config('config_path'). '/plugins/'.$name);
199 $this->setError('['.forge_get_config('config_path'). '/plugins/'.$name.'->'.$path . '/etc/plugins/' . $name . ']'.
200 _('<br />Config file could not be linked to etc/gforge/plugins/%1$s. Check the write permissions for apache in /etc/gforge/plugins or create the link manually.'), $name);
206 function installDatabase() {
208 $path = forge_get_config('plugins_path') . '/' . $name . '/db';
210 require_once $GLOBALS['gfcommon'].'include/DatabaseInstaller.class.php';
211 $di = new DatabaseInstaller($name, $path);
213 // Search for database tables, if present then upgrade.
214 $res=db_query_params ('SELECT COUNT(*) FROM pg_class WHERE (relname=$1 OR relname like $2) AND relkind=$3',
215 array ('plugin_'.$name, 'plugin_'.$name.'_%', 'r'));
216 $count = db_result($res,0,0);
224 function groupisactivecheckbox (&$params) {
225 //Check if the group is active
226 // this code creates the checkbox in the project edit public info page to activate/deactivate the plugin
228 $title = _('current plugin status is:').' '.forge_get_config('plugin_status', $this->name);
229 $imgStatus = 'plugin_status_valid.png';
231 $group = group_get_object($params['group']);
233 if ( forge_get_config('plugin_status',$this->name) !== 'valid' ) {
235 $imgStatus = 'plugin_status_broken.png';
237 if ( forge_get_config('installation_environment') === 'development' || $group->usesPlugin($this->name)) {
242 $flag = strtolower('use_'.$this->name);
245 echo ' <input type="checkbox" name="'.$flag.'" value="1" ';
246 // checked or unchecked?
247 if ($group->usesPlugin($this->name)) {
248 echo 'checked="checked"';
253 echo '<strong>'. sprintf(_('Use %s Plugin'), $this->text) .'</strong>';
254 echo html_image($imgStatus, '16', '16',array('alt'=>$title, 'title'=>$title));
261 * @return bool actually only true ...
263 function groupisactivecheckboxpost(&$params) {
264 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
265 $group = group_get_object($params['group']);
266 $flag = strtolower('use_'.$this->name);
267 if ( getIntFromRequest($flag) == 1 ) {
268 $group->setPluginUse($this->name);
270 $group->setPluginUse($this->name, false);
275 function userisactivecheckbox(&$params) {
276 //check if user is active
277 // this code creates the checkbox in the user account manteinance page to activate/deactivate the plugin
279 $title = _('current plugin status is:').' '.forge_get_config('plugin_status', $this->name);
280 $imgStatus = 'plugin_status_valid.png';
282 $user = $params['user'];
284 if ( forge_get_config('plugin_status', $this->name) !== 'valid' ) {
286 $imgStatus = 'plugin_status_broken.png';
288 if ( forge_get_config('installation_environment') === 'development' || $user->usesPlugin($this->name)) {
292 $flag = strtolower('use_'.$this->name);
295 echo ' <input type="checkbox" name="'.$flag.'" value="1" ';
296 // checked or unchecked?
297 if ($user->usesPlugin($this->name)) {
298 echo 'checked="checked"';
300 echo ' /> '. sprintf(_('Use %s Plugin'), $this->text);
301 echo html_image($imgStatus, '16', '16',array('alt'=>$title, 'title'=>$title));
307 function userisactivecheckboxpost(&$params) {
308 // this code actually activates/deactivates the plugin after the form was submitted in the user account manteinance page
309 $user = $params['user'];
310 $flag = strtolower('use_'.$this->name);
311 if (getIntFromRequest($flag) == 1) {
312 $user->setPluginUse($this->name);
314 $user->setPluginUse($this->name, false);
319 class PluginSpecificRoleSetting {
323 var $values = array();
324 var $default_values = array();
327 function PluginSpecificRoleSetting(&$role, $name, $global = false) {
328 $this->global = $global;
329 $this->role =& $role;
333 function SetAllowedValues($values) {
334 $this->role->role_values = array_replace_recursive($this->role->role_values,
335 array($this->name => $values));
337 $this->role->global_settings[] = $this->name;
341 function SetDefaultValues($defaults) {
342 foreach ($defaults as $rname => $v) {
343 $this->role->defaults[$rname][$this->name] = $v;
347 function setValueDescriptions($descs) {
348 global $rbac_permission_names ;
349 foreach ($descs as $k => $v) {
350 $rbac_permission_names[$this->name.$k] = $v;
354 function setDescription($desc) {
355 global $rbac_edit_section_names ;
356 $rbac_edit_section_names[$this->name] = $desc;
362 // c-file-style: "bsd"