ryxeo-glpi-git / inc / plugin.function.php @ b67d8923
Historique | Voir | Annoter | Télécharger (9,57 ko)
1 |
<?php
|
---|---|
2 |
/*
|
3 |
* @version $Id: plugin.function.php 7763 2009-01-06 18:44:50Z moyo $
|
4 |
-------------------------------------------------------------------------
|
5 |
GLPI - Gestionnaire Libre de Parc Informatique
|
6 |
Copyright (C) 2003-2009 by the INDEPNET Development Team.
|
7 |
|
8 |
http://indepnet.net/ http://glpi-project.org
|
9 |
-------------------------------------------------------------------------
|
10 |
|
11 |
LICENSE
|
12 |
|
13 |
This file is part of GLPI.
|
14 |
|
15 |
GLPI is free software; you can redistribute it and/or modify
|
16 |
it under the terms of the GNU General Public License as published by
|
17 |
the Free Software Foundation; either version 2 of the License, or
|
18 |
(at your option) any later version.
|
19 |
|
20 |
GLPI is distributed in the hope that it will be useful,
|
21 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
22 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
23 |
GNU General Public License for more details.
|
24 |
|
25 |
You should have received a copy of the GNU General Public License
|
26 |
along with GLPI; if not, write to the Free Software
|
27 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
28 |
--------------------------------------------------------------------------
|
29 |
*/
|
30 |
|
31 |
// Based on cacti plugin system
|
32 |
// ----------------------------------------------------------------------
|
33 |
// Original Author of file:
|
34 |
// Purpose of file:
|
35 |
// ----------------------------------------------------------------------
|
36 |
|
37 |
if (!defined('GLPI_ROOT')){ |
38 |
die("Sorry. You can't access directly to this file"); |
39 |
} |
40 |
|
41 |
|
42 |
|
43 |
global $PLUGIN_HOOKS; |
44 |
$PLUGIN_HOOKS = array(); |
45 |
global $CFG_GLPI_PLUGINS; |
46 |
$CFG_GLPI_PLUGINS = array(); |
47 |
|
48 |
|
49 |
/**
|
50 |
* Init plugins list reading plugins directory
|
51 |
* @return nothing
|
52 |
*/
|
53 |
function initPlugins(){ |
54 |
|
55 |
$_SESSION["glpi_plugins"]=array(); |
56 |
$dirplug=GLPI_ROOT."/plugins"; |
57 |
$dh = opendir($dirplug); |
58 |
while (false !== ($filename = readdir($dh))) { |
59 |
if ($filename!=".svn"&&$filename!="."&&$filename!=".."&&is_dir($dirplug."/".$filename)){ |
60 |
$_SESSION["glpi_plugins"][]=$filename; |
61 |
} |
62 |
} |
63 |
|
64 |
} |
65 |
/**
|
66 |
* Init a plugin including setup.php file
|
67 |
* launching plugin_init_NAME function after checking compatibility
|
68 |
*
|
69 |
* @param $name Name of hook to use
|
70 |
* @return nothing
|
71 |
*/
|
72 |
function usePlugin ($name) { |
73 |
global $CFG_GLPI, $PLUGIN_HOOKS; |
74 |
|
75 |
if (file_exists(GLPI_ROOT . "/plugins/$name/setup.php")) { |
76 |
include_once(GLPI_ROOT . "/plugins/$name/setup.php"); |
77 |
|
78 |
$function = "plugin_version_$name"; |
79 |
if (function_exists($function)) { |
80 |
$info=$function(); |
81 |
|
82 |
if ((isset($info["minGlpiVersion"]) && $info["minGlpiVersion"]>GLPI_VERSION) || |
83 |
(isset($info["maxGlpiVersion"]) && $info["maxGlpiVersion"]<GLPI_VERSION)) { |
84 |
|
85 |
// Note this to have the plugin listed (as incompatible) on config page.
|
86 |
$PLUGIN_HOOKS["config_page"][$name]=false; |
87 |
|
88 |
return false; |
89 |
} |
90 |
|
91 |
} |
92 |
$function = "plugin_init_$name"; |
93 |
|
94 |
if (function_exists($function)) { |
95 |
$function();
|
96 |
} |
97 |
} |
98 |
} |
99 |
|
100 |
/**
|
101 |
* This function executes a hook.
|
102 |
* @param $name Name of hook to fire
|
103 |
* @param $param Parameters if needed
|
104 |
* @return mixed $data
|
105 |
*/
|
106 |
function doHook ($name,$param=NULL) { |
107 |
global $PLUGIN_HOOKS; |
108 |
if ($param==NULL){ |
109 |
$data = func_get_args(); |
110 |
} else {
|
111 |
$data=$param; |
112 |
} |
113 |
|
114 |
if (isset($PLUGIN_HOOKS[$name]) && is_array($PLUGIN_HOOKS[$name])) { |
115 |
foreach ($PLUGIN_HOOKS[$name] as $function) { |
116 |
if (function_exists($function)) { |
117 |
$function($data); |
118 |
} |
119 |
} |
120 |
} |
121 |
|
122 |
/* Variable-length argument lists have a slight problem when */
|
123 |
/* passing values by reference. Pity. This is a workaround. */
|
124 |
return $data; |
125 |
} |
126 |
/**
|
127 |
* This function executes a hook.
|
128 |
* @param $name Name of hook to fire
|
129 |
* @param $parm Parameters
|
130 |
* @return mixed $data
|
131 |
*/
|
132 |
function doHookFunction($name,$parm=NULL) { |
133 |
global $PLUGIN_HOOKS; |
134 |
$ret = $parm; |
135 |
|
136 |
if (isset($PLUGIN_HOOKS[$name]) |
137 |
&& is_array($PLUGIN_HOOKS[$name])) { |
138 |
foreach ($PLUGIN_HOOKS[$name] as $function) { |
139 |
if (function_exists($function)) { |
140 |
$ret = $function($ret); |
141 |
} |
142 |
} |
143 |
} |
144 |
|
145 |
/* Variable-length argument lists have a slight problem when */
|
146 |
/* passing values by reference. Pity. This is a workaround. */
|
147 |
return $ret; |
148 |
} |
149 |
|
150 |
/**
|
151 |
* Display plugin actions for a device type
|
152 |
* @param $type ID of the device type
|
153 |
* @param $ID ID of the item
|
154 |
* @param $onglet Heading corresponding of the datas to display
|
155 |
* @param $withtemplate is the item display like a template ?
|
156 |
* @return true if display have been done
|
157 |
*/
|
158 |
function displayPluginAction($type,$ID,$onglet,$withtemplate=0){ |
159 |
global $PLUGIN_HOOKS; |
160 |
// Show all Case
|
161 |
if ($onglet==-1){ |
162 |
if (isset($PLUGIN_HOOKS["headings_action"])&&is_array($PLUGIN_HOOKS["headings_action"])&&count($PLUGIN_HOOKS["headings_action"])) |
163 |
foreach ($PLUGIN_HOOKS["headings_action"] as $plug => $function) |
164 |
if (function_exists($function)){ |
165 |
|
166 |
$actions=$function($type); |
167 |
|
168 |
if (is_array($actions)&&count($actions)) |
169 |
foreach ($actions as $key => $action){ |
170 |
if (function_exists($action)){ |
171 |
echo "<br>"; |
172 |
$action($type,$ID,$withtemplate); |
173 |
} |
174 |
|
175 |
} |
176 |
} |
177 |
return true; |
178 |
|
179 |
} else {
|
180 |
$split=split("_",$onglet); |
181 |
if (count($split)==2){ |
182 |
list($plug,$ID_onglet)=$split; |
183 |
if (isset($PLUGIN_HOOKS["headings_action"][$plug])){ |
184 |
$function=$PLUGIN_HOOKS["headings_action"][$plug]; |
185 |
if (function_exists($function)){ |
186 |
|
187 |
$actions=$function($type); |
188 |
|
189 |
if (isset($actions[$ID_onglet])&&function_exists($actions[$ID_onglet])){ |
190 |
$function=$actions[$ID_onglet]; |
191 |
$function($type,$ID,$withtemplate); |
192 |
return true; |
193 |
} |
194 |
} |
195 |
} |
196 |
|
197 |
} |
198 |
} |
199 |
return false; |
200 |
} |
201 |
/**
|
202 |
* Display plugin headgsin for a device type
|
203 |
* @param $target page to link including ID
|
204 |
* @param $type ID of the device type
|
205 |
* @param $withtemplate is the item display like a template ?
|
206 |
* @param $actif active onglet
|
207 |
* @return true if display have been done
|
208 |
*/
|
209 |
function displayPluginHeadings($target,$type,$withtemplate,$actif){ |
210 |
global $PLUGIN_HOOKS,$LANG; |
211 |
$template=""; |
212 |
if(!empty($withtemplate)){ |
213 |
$template="&withtemplate=$withtemplate"; |
214 |
} |
215 |
$display_onglets=array(); |
216 |
if (isset($PLUGIN_HOOKS["headings"]) && is_array($PLUGIN_HOOKS["headings"])) { |
217 |
foreach ($PLUGIN_HOOKS["headings"] as $plug => $function) { |
218 |
|
219 |
if (function_exists($function)) { |
220 |
$onglet=$function($type,$withtemplate); |
221 |
|
222 |
if (is_array($onglet)&&count($onglet)) |
223 |
foreach ($onglet as $key => $val) |
224 |
$display_onglets[$plug."_".$key]=$val; |
225 |
//echo "<li".(($actif==$plug."_".$key)?" class='actif'":"")."><a href='$target&onglet=".$plug."_".$key."$template'>".$val."</a></li>";
|
226 |
} |
227 |
} |
228 |
if (count($display_onglets)){ |
229 |
echo "<li class='invisible'> </li>"; |
230 |
|
231 |
echo "<li".(ereg($plug,$actif)?" class='actif'":"")." style='position:relative;' onmouseout=\"cleanhide('onglet_plugins')\" onmouseover=\"cleandisplay('onglet_plugins')\"><a href='#'>".$LANG["common"][29]."</a>"; |
232 |
|
233 |
echo "<div id='onglet_plugins' ><dl>"; |
234 |
foreach ($display_onglets as $key => $val){ |
235 |
echo "<dt><a href='$target&onglet=".$key.$template."'>".$val."</a></dt>"; |
236 |
} |
237 |
echo "</dl></div>"; |
238 |
echo "</li>"; |
239 |
|
240 |
} |
241 |
|
242 |
} |
243 |
|
244 |
} |
245 |
|
246 |
/**
|
247 |
* Get cron jobs for plugins
|
248 |
*
|
249 |
* @return Array containing plugin cron jobs
|
250 |
*/
|
251 |
function getPluginsCronJobs(){ |
252 |
global $PLUGIN_HOOKS; |
253 |
$tasks=array(); |
254 |
if (isset($PLUGIN_HOOKS["cron"]) && is_array($PLUGIN_HOOKS["cron"])) { |
255 |
foreach ($PLUGIN_HOOKS["cron"] as $plug => $time) { |
256 |
$tasks["plugin_".$plug]=$time; |
257 |
} |
258 |
} |
259 |
return $tasks; |
260 |
} |
261 |
|
262 |
/**
|
263 |
* Get dropdowns for plugins
|
264 |
*
|
265 |
* @return Array containing plugin dropdowns
|
266 |
*/
|
267 |
function getPluginsDropdowns(){ |
268 |
$dps=array(); |
269 |
if (isset($_SESSION["glpi_plugins"]) && is_array($_SESSION["glpi_plugins"])) { |
270 |
foreach ($_SESSION["glpi_plugins"] as $plug) { |
271 |
$function="plugin_version_$plug"; |
272 |
$function2="plugin_".$plug."_getDropdown"; |
273 |
if (function_exists($function2)) { |
274 |
$name=$function(); |
275 |
$tab=$function2(); |
276 |
if (is_array($tab)){ |
277 |
$dps=array_merge($dps,array($name['name']=>$tab)); |
278 |
} |
279 |
} |
280 |
} |
281 |
} |
282 |
return $dps; |
283 |
} |
284 |
/**
|
285 |
* Get database relations for plugins
|
286 |
*
|
287 |
* @return Array containing plugin database relations
|
288 |
*/
|
289 |
function getPluginsDatabaseRelations(){ |
290 |
$dps=array(); |
291 |
if (isset($_SESSION["glpi_plugins"]) && is_array($_SESSION["glpi_plugins"])) { |
292 |
foreach ($_SESSION["glpi_plugins"] as $plug) { |
293 |
$function2="plugin_".$plug."_getDatabaseRelations"; |
294 |
if (function_exists($function2)) { |
295 |
$dps=array_merge($dps,$function2()); |
296 |
} |
297 |
} |
298 |
} |
299 |
return $dps; |
300 |
} |
301 |
|
302 |
/**
|
303 |
* Get search options for plugins
|
304 |
*
|
305 |
* @return Array containing plugin search options
|
306 |
*/
|
307 |
function getPluginSearchOption(){ |
308 |
global $PLUGIN_HOOKS; |
309 |
$sopt=array(); |
310 |
if (isset($PLUGIN_HOOKS['plugin_types'])&&count($PLUGIN_HOOKS['plugin_types'])){ |
311 |
$tab=array_unique($PLUGIN_HOOKS['plugin_types']); |
312 |
foreach ($tab as $plug){ |
313 |
$function="plugin_".$plug."_getSearchOption"; |
314 |
if (function_exists($function)) { |
315 |
$tmp=$function(); |
316 |
if (count($tmp)){ |
317 |
foreach ($tmp as $key => $val){ |
318 |
if (!isset($sopt[$key])){ |
319 |
$sopt[$key]=array(); |
320 |
} |
321 |
$sopt[$key]+=$val; |
322 |
} |
323 |
} |
324 |
} |
325 |
} |
326 |
} |
327 |
return $sopt; |
328 |
} |
329 |
/**
|
330 |
* Define a new device type used in a plugin
|
331 |
* @param $plugin plugin of the device type
|
332 |
* @param $name name of the device_type to define the constant
|
333 |
* @param $ID number used as constant
|
334 |
* @param $class class defined for manipulate this device type
|
335 |
* @param $table table describing the device
|
336 |
* @param $formpage Form page for the item
|
337 |
* @param $typename string defining the name of the new type (used in CommonItem)
|
338 |
* @return nothing
|
339 |
*/
|
340 |
|
341 |
function pluginNewType($plugin,$name,$ID,$class,$table,$formpage='',$typename=''){ |
342 |
global $PLUGIN_HOOKS,$LINK_ID_TABLE,$INFOFORM_PAGES; |
343 |
|
344 |
define($name,$ID); |
345 |
$LINK_ID_TABLE[$ID]=$table; |
346 |
$INFOFORM_PAGES[$ID]="plugins/$plugin/$formpage"; |
347 |
$PLUGIN_HOOKS['plugin_types'][$ID]=$plugin; |
348 |
$PLUGIN_HOOKS['plugin_typenames'][$ID]=$typename; |
349 |
$PLUGIN_HOOKS['plugin_classes'][$ID]=$class; |
350 |
} |
351 |
?>
|