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 * Copyright 2013,2014 Franck Villaume - TrivialDev
11 * http://fusionforge.org
13 * This file is part of FusionForge. FusionForge is free software;
14 * you can redistribute it and/or modify it under the terms of the
15 * GNU General Public License as published by the Free Software
16 * Foundation; either version 2 of the Licence, or (at your option)
19 * FusionForge is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 class Plugin extends FFError {
37 var $pkg_desc = 'No description available.';
42 function __construct($id=0) {
43 parent::__construct();
45 $this->hooks = array();
49 * GetHooks() - get list of hooks to subscribe to.
51 * @return array List of strings.
57 * _addHooks() - add a hook to the list of hooks.
60 * @return string name of the added hook
62 function _addHook($name) {
63 return $this->hooks[]=$name;
67 * GetName() - get plugin name.
69 * @return string the plugin name.
76 * getInstallDir() - get installation dir for the plugin.
78 * @return string the directory where the plugin should be linked.
80 function getInstallDir() {
81 if (isset($this->installdir) && $this->installdir) {
82 return $this->installdir;
84 return 'plugins/'.$this->name;
89 * provide() - return true if plugin provides the feature.
91 * @param string $feature
92 * @return bool if feature is provided or not.
94 function provide($feature) {
95 return (isset($this->provides[$feature]) && $this->provides[$feature]);
99 * CallHook() - call a particular hook.
101 * @param string $hookname the "handle" of the hook.
102 * @param array $params array of parameters to pass the hook.
103 * @return bool true only
105 function CallHook($hookname, &$params) {
110 * getID - get the numeric ID of a plugin
112 * @return int identifier of the plugin
119 $res = db_query_params('SELECT plugin_id FROM plugins WHERE plugin_name=$1',
121 $this->id = db_result($res,0,'plugin_id');
126 * getGroups - get a list of all groups using a plugin.
128 * @return array array containing group objects.
130 function getGroups() {
132 $res = db_query_params('SELECT group_plugin.group_id
133 FROM group_plugin, plugins
134 WHERE group_plugin.plugin_id=plugins.plugin_id
135 AND plugins.plugin_name=$1
136 ORDER BY group_plugin.group_id ASC',
138 $rows = db_numrows($res);
140 for ($i=0; $i<$rows; $i++) {
141 $group_id = db_result($res,$i,'group_id');
142 $result[] = group_get_object($group_id);
148 * getThemePath - returns the directory of the theme for this plugin
150 * @return string the directory
152 function getThemePath(){
153 return 'plugins/'.$this->name.'/themes/default';
157 $this->installCode();
158 $this->installConfig();
159 $this->installDatabase();
162 function installCode() {
163 $path = forge_get_config('plugins_path') . '/' . $this->name;
164 $installdir = $this->getInstallDir();
166 // Create a symbolic links to plugins/<plugin>/www (if directory exists).
167 if (is_dir($path . '/www')) { // if the plugin has a www dir make a link to it
168 // The apache group or user should have write perms the www/plugins folder...
169 $www = dirname(dirname(dirname(__FILE__))).'/www';
170 if (!is_link($www.'/'.$installdir)) {
171 $code = symlink($path . '/www', $www.'/'.$installdir);
173 $this->setError('['.$www.'/'.$installdir.'->'.$path . '/www]<br />'.
174 _('Soft link to www could not be created. Check the write permissions for apache in fusionforge www/plugins dir or create the link manually.'));
179 // Create a symbolic links to plugins/<plugin>/etc/plugins/<plugin> (if directory exists).
180 if (is_dir($path . '/etc/plugins/' . $this->name)) {
181 // The apache group or user should have write perms in /etc/fusionforge/plugins folder...
182 if (!is_link(forge_get_config('config_path'). '/plugins/'.$this->name) && !is_dir(forge_get_config('config_path'). '/plugins/'.$this->name)) {
183 $code = symlink($path . '/etc/plugins/' . $this->name, forge_get_config('config_path'). '/plugins/'.$this->name);
185 $this->setError('['.forge_get_config('config_path'). '/plugins/'.$this->name.'->'.$path . '/etc/plugins/' . $this->name . ']'.'<br />'.
186 sprintf(_('Config file could not be linked to %s. Check the write permissions for apache in /etc/fusionforge/plugins or create the link manually.'), forge_get_config('config_path').'/plugins/'.$this->name));
192 function installConfig() {
193 $path = forge_get_config('plugins_path') . '/' . $this->name;
195 // Create a symbolic links to plugins/<plugin>/etc/plugins/<plugin> (if directory exists).
196 if (is_dir($path . '/etc/plugins/' . $this->name)) {
197 // The apache group or user should have write perms in /etc/fusionforge/plugins folder...
198 if (!is_link(forge_get_config('config_path'). '/plugins/'.$this->name) && !is_dir(forge_get_config('config_path'). '/plugins/'.$this->name)) {
199 $code = symlink($path . '/etc/plugins/' . $this->name, forge_get_config('config_path'). '/plugins/'.$this->name);
201 $this->setError('['.forge_get_config('config_path'). '/plugins/'.$this->name.'->'.$path . '/etc/plugins/' . $this->name . ']'.'<br />'.
202 sprintf(_('Config file could not be linked to %s. Check the write permissions for apache in /etc/fusionforge/plugins or create the link manually.'), forge_get_config('config_path').'/plugins/'.$this->name));
208 function installDatabase() {
209 $path = forge_get_config('plugins_path') . '/' . $this->name . '/db';
211 require_once $GLOBALS['gfcommon'].'include/DatabaseInstaller.class.php';
212 $di = new DatabaseInstaller($this->name, $path);
214 // Search for database tables, if present then upgrade.
215 $tablename = str_replace('-', '_', $this->name);
216 $res=db_query_params ('SELECT COUNT(*) FROM pg_class WHERE (relname=$1 OR relname like $2) AND relkind=$3',
217 array ('plugin_'.$tablename, 'plugin_'.$tablename.'_%', 'r'));
218 $count = db_result($res,0,0);
226 function groupisactivecheckbox (&$params) {
227 // Check if the group is active
228 // This code creates the checkbox in the project edit public info page
229 // to activate/deactivate the plugin
231 $title = _('Current plugin status is').' '.forge_get_config('plugin_status', $this->name);
232 $imgStatus = 'plugin_status_valid.png';
234 $group = group_get_object($params['group']);
236 if ( forge_get_config('plugin_status', $this->name) !== 'valid' ) {
238 $imgStatus = 'plugin_status_broken.png';
240 if ( forge_get_config('installation_environment') === 'development' || $group->usesPlugin($this->name)) {
245 $flag = strtolower('use_'.$this->name);
248 echo ' <input id="'.$flag.'" type="checkbox" name="'.$flag.'" value="1" ';
249 // checked or unchecked?
250 if ($group->usesPlugin($this->name)) {
251 echo 'checked="checked"';
255 echo '<td title="'.$this->pkg_desc.'">';
256 echo "<label for='".$flag."'><strong>";
257 printf(_("Use %s"), $this->text);
258 echo "</strong></label>";
260 echo html_image($imgStatus, 16, 16, array('alt'=>$title, 'title'=>$title));
267 * @return bool actually only true ...
269 function groupisactivecheckboxpost(&$params) {
270 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
271 $group = group_get_object($params['group']);
272 $flag = strtolower('use_'.$this->name);
273 if (getIntFromRequest($flag) == 1) {
274 $group->setPluginUse($this->name);
276 $group->setPluginUse($this->name, false);
281 function userisactivecheckbox(&$params) {
282 // Check if user is active
283 // This code creates the checkbox in the user account maintenance page
284 // to activate/deactivate the plugin
286 $title = _('Current plugin status is').' '.forge_get_config('plugin_status', $this->name);
287 $imgStatus = 'plugin_status_valid.png';
289 $user = $params['user'];
291 if ( forge_get_config('plugin_status', $this->name) !== 'valid' ) {
293 $imgStatus = 'plugin_status_broken.png';
295 if ( forge_get_config('installation_environment') === 'development' || $user->usesPlugin($this->name)) {
299 $flag = strtolower('use_'.$this->name);
301 echo ' <input id="'.$flag.'" type="checkbox" name="'.$flag.'" value="1" ';
302 // checked or unchecked?
303 if ($user->usesPlugin($this->name)) {
304 echo 'checked="checked"';
307 echo "<label for='".$flag."'><strong>";
308 printf(_("Use %s"), $this->text);
309 echo "</strong></label>";
310 echo html_image($imgStatus, 16, 16, array('alt'=>$title, 'title'=>$title));
315 function userisactivecheckboxpost(&$params) {
316 // this code actually activates/deactivates the plugin after the form was submitted in the user account manteinance page
317 $user = $params['user'];
318 $flag = strtolower('use_'.$this->name);
319 if (getIntFromRequest($flag) == 1) {
320 $user->setPluginUse($this->name);
322 $user->setPluginUse($this->name, false);
326 function getPluginDescription() {
327 return $this->pkg_desc;
331 class PluginSpecificRoleSetting {
335 var $values = array();
336 var $default_values = array();
339 function __construct(&$role, $name, $global = false) {
340 $this->global = $global;
341 $this->role =& $role;
345 function SetAllowedValues($values) {
346 $this->role->role_values = array_replace_recursive($this->role->role_values,
347 array($this->name => $values));
349 $this->role->global_settings[] = $this->name;
353 function SetDefaultValues($defaults) {
354 foreach ($defaults as $rname => $v) {
355 $this->role->defaults[$rname][$this->name] = $v;
359 function setValueDescriptions($descs) {
360 global $rbac_permission_names ;
361 foreach ($descs as $k => $v) {
362 $rbac_permission_names[$this->name.$k] = $v;
366 function setDescription($desc) {
367 global $rbac_edit_section_names ;
368 $rbac_edit_section_names[$this->name] = $desc;
374 // c-file-style: "bsd"