ryxeo-glpi-git / inc / rule.ocs.class.php @ b67d8923
Historique | Voir | Annoter | Télécharger (10,5 ko)
1 | b67d8923 | Eric Seigne | <?php
|
---|---|---|---|
2 | /*
|
||
3 | * @version $Id: rule.ocs.class.php 7875 2009-01-23 15:16:47Z 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 | // ----------------------------------------------------------------------
|
||
32 | // Original Author of file: Walid Nouh
|
||
33 | // Purpose of file:
|
||
34 | // ----------------------------------------------------------------------
|
||
35 | if (!defined('GLPI_ROOT')) { |
||
36 | die("Sorry. You can't access directly to this file"); |
||
37 | } |
||
38 | |||
39 | /// OCS Rules collection class
|
||
40 | class OcsRuleCollection extends RuleCollection { |
||
41 | |||
42 | ///Store the id of the ocs server
|
||
43 | var $ocs_server_id; |
||
44 | |||
45 | /**
|
||
46 | * Constructor
|
||
47 | * @param $ocs_server_id ID of the OCS server
|
||
48 | **/
|
||
49 | function OcsRuleCollection($ocs_server_id=-1) { |
||
50 | $this->rule_type = RULE_OCS_AFFECT_COMPUTER; |
||
51 | $this->rule_class_name = 'OcsAffectEntityRule'; |
||
52 | $this->ocs_server_id = $ocs_server_id; |
||
53 | $this->stop_on_first_match=true; |
||
54 | $this->right="rule_ocs"; |
||
55 | } |
||
56 | |||
57 | function getTitle() { |
||
58 | global $LANG; |
||
59 | return $LANG["rulesengine"][18]; |
||
60 | } |
||
61 | |||
62 | function prepareInputDataForProcess($input,$computer_id){ |
||
63 | global $DBocs; |
||
64 | $tables = $this->getTablesForQuery(); |
||
65 | $fields = $this->getFieldsForQuery(); |
||
66 | $linked_fields = $this->getFKFieldsForQuery(); |
||
67 | |||
68 | $rule_parameters = array (); |
||
69 | |||
70 | $sql = ""; |
||
71 | $begin_sql = "SELECT "; |
||
72 | $select_sql = ""; |
||
73 | $from_sql = ""; |
||
74 | $where_sql = ""; |
||
75 | |||
76 | //Build the select request
|
||
77 | foreach ($fields as $field) { |
||
78 | switch (strtoupper($field)) { |
||
79 | //OCS server ID is provided by extra_params -> get the configuration associated with the ocs server
|
||
80 | case "OCS_SERVER" : |
||
81 | $rule_parameters["OCS_SERVER"] = $this->ocs_server_id; |
||
82 | break;
|
||
83 | //TAG and DOMAIN should come from the OCS DB
|
||
84 | default :
|
||
85 | $select_sql .= ($select_sql != "" ? " , " : "") . $field; |
||
86 | break;
|
||
87 | } |
||
88 | |||
89 | } |
||
90 | |||
91 | //Build the FROM part of the request
|
||
92 | //Remove all the non duplicated table names
|
||
93 | $tables = array_unique($tables); |
||
94 | foreach ($tables as $table) { |
||
95 | $from_sql .= ($from_sql != "" ? " , " : "") . $table; |
||
96 | } |
||
97 | |||
98 | //Build the WHERE part of the request
|
||
99 | foreach ($linked_fields as $linked_field) { |
||
100 | $where_sql .= ($where_sql != "" ? " AND " : "") . $linked_field . "=hardware.ID "; |
||
101 | } |
||
102 | |||
103 | if ($select_sql != "" && $from_sql != "" && $where_sql != "") { |
||
104 | //Build the all request
|
||
105 | $sql = $begin_sql . $select_sql . " FROM " . $from_sql . " WHERE " . $where_sql . " AND hardware.ID='" . $computer_id."'"; |
||
106 | |||
107 | checkOCSconnection($this->ocs_server_id);
|
||
108 | $result = $DBocs->query($sql); |
||
109 | $ocs_datas = array (); |
||
110 | |||
111 | $fields = $this->getFieldsForQuery(1); |
||
112 | |||
113 | //May have more than one line : for example in case of multiple network cards
|
||
114 | if ($DBocs->numrows($result) > 0){ |
||
115 | while ($datas = $DBocs->fetch_array($result)){ |
||
116 | foreach ($fields as $field) { |
||
117 | if ($field != "OCS_SERVER" && isset($datas[$field])) |
||
118 | $ocs_datas[$field][] = $datas[$field]; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | } |
||
123 | |||
124 | //This cas should never happend but...
|
||
125 | //Sometimes OCS can't find network ports but fill the right ip in hardware table...
|
||
126 | //So let's use the ip to proceed rules (if IP is a criteria of course)
|
||
127 | if (in_array("IPADDRESS",$fields) && !isset($ocs_datas['IPADDRESS'])) |
||
128 | $ocs_datas['IPADDRESS']=getOcsGeneralIpAddress($this->ocs_server_id,$computer_id); |
||
129 | |||
130 | return array_merge($rule_parameters, $ocs_datas); |
||
131 | } else
|
||
132 | return $rule_parameters; |
||
133 | } |
||
134 | |||
135 | |||
136 | |||
137 | /**
|
||
138 | * Get the list of all tables to include in the query
|
||
139 | * @return an array of table names
|
||
140 | */
|
||
141 | function getTablesForQuery() |
||
142 | { |
||
143 | global $RULES_CRITERIAS; |
||
144 | |||
145 | $tables = array(); |
||
146 | foreach ($RULES_CRITERIAS[$this->rule_type] as $criteria){ |
||
147 | if ((!isset($criteria['virtual']) || !$criteria['virtual']) && $criteria['table'] != '' && !array_key_exists($criteria["table"],$tables)) { |
||
148 | $tables[]=$criteria['table']; |
||
149 | } |
||
150 | } |
||
151 | return $tables; |
||
152 | } |
||
153 | |||
154 | /**
|
||
155 | * Get fields needed to process criterias
|
||
156 | * @param $withouttable fields without tablename ?
|
||
157 | * @return an array of needed fields
|
||
158 | */
|
||
159 | function getFieldsForQuery($withouttable=0){ |
||
160 | global $RULES_CRITERIAS; |
||
161 | |||
162 | $fields = array(); |
||
163 | foreach ($RULES_CRITERIAS[$this->rule_type] as $key => $criteria){ |
||
164 | |||
165 | if ($withouttable) |
||
166 | { |
||
167 | if (strcasecmp($key,$criteria['field']) != 0) |
||
168 | $fields[]=$key; |
||
169 | else
|
||
170 | $fields[]=$criteria['field']; |
||
171 | } |
||
172 | else
|
||
173 | { |
||
174 | //If the field is different from the key
|
||
175 | if (strcasecmp($key,$criteria['field']) != 0) |
||
176 | $as = " AS ".$key; |
||
177 | else
|
||
178 | $as =""; |
||
179 | |||
180 | //If the field name is not null AND a table name is provided
|
||
181 | if (($criteria['field'] != '' && (!isset($criteria['virtual']) || !$criteria['virtual']))){ |
||
182 | if ( $criteria['table'] != '') { |
||
183 | $fields[]=$criteria['table'].".".$criteria['field'].$as; |
||
184 | } else{
|
||
185 | $fields[]=$criteria['field'].$as; |
||
186 | } |
||
187 | } |
||
188 | else
|
||
189 | $fields[]=$criteria['id']; |
||
190 | } |
||
191 | } |
||
192 | return $fields; |
||
193 | } |
||
194 | |||
195 | |||
196 | /**
|
||
197 | * Get foreign fields needed to process criterias
|
||
198 | * @return an array of needed fields
|
||
199 | */
|
||
200 | function getFKFieldsForQuery() { |
||
201 | global $RULES_CRITERIAS; |
||
202 | |||
203 | $fields = array(); |
||
204 | foreach ($RULES_CRITERIAS[$this->rule_type] as $criteria){ |
||
205 | //If the field name is not null AND a table name is provided
|
||
206 | if ( (!isset($criteria['virtual']) || !$criteria['virtual']) && $criteria['linkfield'] != ''){ |
||
207 | $fields[]=$criteria['table'].".".$criteria['linkfield']; |
||
208 | } |
||
209 | } |
||
210 | return $fields; |
||
211 | } |
||
212 | |||
213 | |||
214 | } |
||
215 | |||
216 | /// OCS Rules class
|
||
217 | class OcsAffectEntityRule extends Rule { |
||
218 | |||
219 | |||
220 | /**
|
||
221 | * Constructor
|
||
222 | * @param $ocs_server_id ID of the OCS server
|
||
223 | **/
|
||
224 | function OcsAffectEntityRule($ocs_server_id=-1) { |
||
225 | $this->table = "glpi_rules_descriptions"; |
||
226 | $this->type = -1; |
||
227 | $this->rule_type = RULE_OCS_AFFECT_COMPUTER; |
||
228 | $this->right="rule_ocs"; |
||
229 | $this->can_sort=true; |
||
230 | } |
||
231 | |||
232 | function getTitle() { |
||
233 | global $LANG; |
||
234 | return $LANG["rulesengine"][18]; |
||
235 | } |
||
236 | |||
237 | function maxActionsCount(){ |
||
238 | // Unlimited
|
||
239 | return 1; |
||
240 | } |
||
241 | /**
|
||
242 | * Display form to add rules
|
||
243 | * @param $target
|
||
244 | * @param $ID
|
||
245 | */
|
||
246 | function showAndAddRuleForm($target, $ID) { |
||
247 | global $LANG, $CFG_GLPI; |
||
248 | |||
249 | $canedit = haveRight($this->right, "w"); |
||
250 | |||
251 | echo "<form name='entityaffectation_form' id='entityaffectation_form' method='post' action=\"$target\">"; |
||
252 | |||
253 | if ($canedit) { |
||
254 | |||
255 | echo "<div class='center'>"; |
||
256 | echo "<table class='tab_cadre_fixe'>"; |
||
257 | echo "<tr class='tab_bg_1'><th colspan='4'>" . $LANG["rulesengine"][18] . "</tr><tr><td class='tab_bg_2' align='center'>"; |
||
258 | echo $LANG["common"][16] . ":"; |
||
259 | echo "</td><td align='center' class='tab_bg_2'>"; |
||
260 | autocompletionTextField("name", "glpi_rules_descriptions", "name", "", 30); |
||
261 | echo $LANG["joblist"][6] . ":"; |
||
262 | autocompletionTextField("description", "glpi_rules_descriptions", "description", "", 30); |
||
263 | echo "</td><td align='center' class='tab_bg_2'>"; |
||
264 | echo $LANG["rulesengine"][9] . ":"; |
||
265 | $this->dropdownRulesMatch("match", "AND"); |
||
266 | echo "</td><td align='center' class='tab_bg_2'>"; |
||
267 | echo "<input type=hidden name='rule_type' value=\"" . $this->rule_type . "\">"; |
||
268 | echo "<input type=hidden name='FK_entities' value=\"-1\">"; |
||
269 | echo "<input type=hidden name='affectentity' value=\"" . $ID . "\">"; |
||
270 | echo "<input type='submit' name='add_rule' value=\"" . $LANG["buttons"][8] . "\" class='submit'>"; |
||
271 | echo "</td></tr>"; |
||
272 | |||
273 | echo "</table></div><br>"; |
||
274 | |||
275 | } |
||
276 | |||
277 | echo "<div class='center'><table class='tab_cadrehov'><tr><th colspan='3'>" . $LANG["entity"][5] . "</th></tr>"; |
||
278 | |||
279 | //Get all rules and actions
|
||
280 | $rules = $this->getRulesByID( $ID, 0, 1); |
||
281 | |||
282 | if (!empty ($rules)) { |
||
283 | |||
284 | foreach ($rules as $rule) { |
||
285 | echo "<tr class='tab_bg_1'>"; |
||
286 | |||
287 | if ($canedit) { |
||
288 | echo "<td width='10'>"; |
||
289 | $sel = ""; |
||
290 | if (isset ($_GET["select"]) && $_GET["select"] == "all") |
||
291 | $sel = "checked"; |
||
292 | echo "<input type='checkbox' name='item[" . $rule->fields["ID"] . "]' value='1' $sel>"; |
||
293 | echo "</td>"; |
||
294 | } |
||
295 | |||
296 | if ($canedit) |
||
297 | echo "<td><a href=\"" . $CFG_GLPI["root_doc"] . "/front/rule.ocs.form.php?ID=" . $rule->fields["ID"] . "&onglet=1\">" . $rule->fields["name"] . "</a></td>"; |
||
298 | else
|
||
299 | echo "<td>" . $rule->fields["name"] . "</td>"; |
||
300 | |||
301 | echo "<td>" . $rule->fields["description"] . "</td>"; |
||
302 | echo "</tr>"; |
||
303 | } |
||
304 | } |
||
305 | echo "</table></div>"; |
||
306 | |||
307 | if ($canedit) { |
||
308 | echo "<div class='center'>"; |
||
309 | echo "<table width='80%'>"; |
||
310 | echo "<tr><td><img src=\"" . $CFG_GLPI["root_doc"] . "/pics/arrow-left.png\" alt=''></td><td class='center'><a onclick= \"if ( markAllRows('entityaffectation_form') ) return false;\" href='" . $_SERVER['PHP_SELF'] . "?ID=$ID&select=all'>" . $LANG["buttons"][18] . "</a></td>"; |
||
311 | |||
312 | echo "<td>/</td><td class='center'><a onclick= \"if ( unMarkAllRows('entityaffectation_form') ) return false;\" href='" . $_SERVER['PHP_SELF'] . "?ID=$ID&select=none'>" . $LANG["buttons"][19] . "</a>"; |
||
313 | echo "</td><td align='left' width='80%'>"; |
||
314 | echo "<input type='submit' name='delete_computer_rule' value=\"" . $LANG["buttons"][6] . "\" class='submit'>"; |
||
315 | echo "</td>"; |
||
316 | echo "</table>"; |
||
317 | |||
318 | echo "</div>"; |
||
319 | |||
320 | } |
||
321 | echo "</form>"; |
||
322 | } |
||
323 | |||
324 | |||
325 | /**
|
||
326 | * Return all rules from database
|
||
327 | * @param $ID ID of the rules
|
||
328 | * @param withcriterias import rules criterias too
|
||
329 | * @param withactions import rules actions too
|
||
330 | */
|
||
331 | function getRulesByID($ID, $withcriterias, $withactions) { |
||
332 | global $DB; |
||
333 | $ocs_affect_computer_rules = array (); |
||
334 | |||
335 | |||
336 | //Get all the rules whose rule_type is $rule_type and entity is $ID
|
||
337 | $sql="SELECT * FROM `glpi_rules_actions` as gra, glpi_rules_descriptions as grd WHERE gra.FK_rules=grd.ID AND gra.field='FK_entities' and grd.rule_type='".$this->rule_type."' and gra.value='".$ID."'"; |
||
338 | |||
339 | $result = $DB->query($sql); |
||
340 | while ($rule = $DB->fetch_array($result)) { |
||
341 | $affect_rule = new Rule; |
||
342 | $affect_rule->getRuleWithCriteriasAndActions($rule["ID"], 0, 1); |
||
343 | $ocs_affect_computer_rules[] = $affect_rule; |
||
344 | } |
||
345 | |||
346 | return $ocs_affect_computer_rules; |
||
347 | } |
||
348 | |||
349 | } |
||
350 | |||
351 | |||
352 | ?> |