Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / inc / rulesengine.function.php @ b67d8923

Historique | Voir | Annoter | Télécharger (13 ko)

1
<?php
2
/*
3
 * @version $Id: rulesengine.function.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
define ("RULE_WILDCARD","*");
32
 // Get rules_option array
33

    
34
include_once (GLPI_ROOT."/inc/rules.constant.php");
35

    
36
/**
37
 * Try to match a definied rule
38
 * 
39
 * @param $field the field to match
40
 * @param $condition the condition (is, is_not, contain, not_contain,begin,end)
41
 * @param $pattern the pattern to match
42
 * @param $regex_result
43
 * @return true if the field match the rule, false if it doesn't match
44
**/
45
function matchRules($field, $condition, $pattern,&$regex_result) {
46

    
47
        //If pattern is wildcard, don't check the rule and return true
48
        if ($pattern == RULE_WILDCARD){
49
                return true;
50
        }
51

    
52
        // Trim for remove keyboard errors
53
        $field=trim($field);
54
        $pattern=trim($pattern);
55

    
56
        if ($condition != REGEX_MATCH && $condition != REGEX_NOT_MATCH){
57
                //Perform comparison with fields in lower case
58
                $field = strtolower($field);
59
                $pattern = strtolower($pattern);
60
        }
61
        
62
        switch ($condition) {
63
                case PATTERN_IS :
64
                        if ($field == $pattern){
65
                                return true;
66
                        }else {
67
                                return false;
68
                        }
69
                case PATTERN_IS_NOT :
70
                        if ($field == $pattern){
71
                                return false;
72
                        }else {
73
                                return true;
74
                        }
75
                case PATTERN_END:
76
                        $value = "/".$pattern."$/";
77
                        if (preg_match($value, $field) > 0){
78
                                return true;
79
                        }else {
80
                                return false;
81
                        }        
82
                case PATTERN_BEGIN:
83
                        if (empty($pattern)){
84
                                return false;
85
                        }
86
                        $value = strpos($field,$pattern);
87
                        if (($value !== false) && $value == 0){
88
                                return true;
89
                        }else{
90
                                return false;        
91
                        }
92
                case PATTERN_CONTAIN:
93
                        if (empty($pattern)){
94
                                return false;
95
                        }
96
                        $value = strpos($field,$pattern);
97
                        if (($value !== false) && $value >= 0){
98
                                return true;
99
                        }else{
100
                                return false;        
101
                        }
102
                case PATTERN_NOT_CONTAIN:
103
                        if (empty($pattern)){
104
                                return false;
105
                        }
106
                        $value = strpos($field,$pattern);
107
                        if ($value === false){
108
                                return true;
109
                        }else {
110
                                return false;
111
                        }
112
                case REGEX_MATCH:
113
                        $results = array();
114
                        if (preg_match($pattern."i",$field,$results)>0){
115
                                for ($i=1;$i<count($results);$i++){
116
                                        $regex_result[]=$results[$i];
117
                                }
118
                                return true;
119
                        }else {
120
                                return false;
121
                        }
122
                case REGEX_NOT_MATCH:
123
                        if (preg_match($pattern."i", $field) == 0){
124
                                return true;
125
                        }else {
126
                                return false;
127
                        }        
128
        }
129
        return false;
130
}
131

    
132

    
133
/**
134
 * Return the condition label by giving his ID
135
 * @param $ID condition's ID
136
 * @return condition's label
137
**/
138
function getConditionByID($ID){
139
        global $LANG;
140
        
141
        switch ($ID){
142
                case PATTERN_IS : 
143
                        return $LANG["rulesengine"][0];
144
                case PATTERN_IS_NOT:
145
                        return $LANG["rulesengine"][1];
146
                case PATTERN_CONTAIN:
147
                        return $LANG["rulesengine"][2];
148
                case PATTERN_NOT_CONTAIN:
149
                        return $LANG["rulesengine"][3];
150
                case PATTERN_BEGIN:
151
                        return $LANG["rulesengine"][4];
152
                case PATTERN_END:
153
                        return $LANG["rulesengine"][5];
154
                case REGEX_MATCH:
155
                        return $LANG["rulesengine"][26];
156
                case REGEX_NOT_MATCH:
157
                        return $LANG["rulesengine"][27];
158
        }
159
}
160

    
161

    
162
/**
163
 * Display a dropdown with all the criterias
164
**/
165
function dropdownRulesConditions($type,$name,$value=''){
166
        global $LANG;
167

    
168
        $elements[PATTERN_IS] = $LANG["rulesengine"][0];
169
        $elements[PATTERN_IS_NOT] = $LANG["rulesengine"][1];
170
        $elements[PATTERN_CONTAIN] = $LANG["rulesengine"][2];
171
        $elements[PATTERN_NOT_CONTAIN] = $LANG["rulesengine"][3];
172
        $elements[PATTERN_BEGIN] = $LANG["rulesengine"][4];
173
        $elements[PATTERN_END] = $LANG["rulesengine"][5];
174
        $elements[REGEX_MATCH] = $LANG["rulesengine"][26];
175
        $elements[REGEX_NOT_MATCH] = $LANG["rulesengine"][27];
176
        
177
        return dropdownArrayValues($name,$elements,$value);
178
}
179

    
180

    
181
/**
182
 * Display a dropdown with all the possible actions
183
**/
184
function dropdownRulesActions($rule_type,$name,$value=''){
185
        global $LANG,$CFG_GLPI,$RULES_ACTIONS;
186

    
187
        //All the elements have assign as default action
188
        $elements["assign"] = $LANG["rulesengine"][22];
189

    
190
        if (isset($RULES_ACTIONS[$rule_type][$value]['optional_actions'])){
191
                $actions = $RULES_ACTIONS[$rule_type][$value]['optional_actions'];
192
                if(in_array("regex_result",$actions)){
193
                        $elements["regex_result"] = $LANG["rulesengine"][45];
194
                }
195
                if(in_array("append_regex_result",$actions)){
196
                        $elements["append_regex_result"] = $LANG["rulesengine"][79];
197
                }
198
        }
199
        
200
        return dropdownArrayValues($name,$elements,$value);
201
}
202

    
203
function getActionByID($ID){
204
        global $LANG;
205
        
206
        switch ($ID){
207
                case "assign" : 
208
                        return $LANG["rulesengine"][22];
209
                case "regex_result":
210
                        return $LANG["rulesengine"][45];
211
                case "append_regex_result":
212
                        return $LANG["rulesengine"][79];                        
213
        }
214
}
215

    
216
function getRuleClass($type){
217
        switch ($type){
218
                case RULE_OCS_AFFECT_COMPUTER :
219
                        if (!class_exists('OcsAffectEntityRule')){
220
                                include_once(GLPI_ROOT."/inc/rulesengine.class.php");
221
                                include_once(GLPI_ROOT."/inc/rule.ocs.class.php");
222
                        }
223
                        return new OcsAffectEntityRule();
224
                        break;                
225
                case RULE_AFFECT_RIGHTS :
226
                        if (!class_exists('RightAffectRule')){
227
                                include_once(GLPI_ROOT."/inc/rulesengine.class.php");
228
                                include_once(GLPI_ROOT."/inc/rule.right.class.php");
229
                        }
230
                        return new RightAffectRule();
231
                        break;
232
                case RULE_TRACKING_AUTO_ACTION:
233
                        if (!class_exists('TrackingBusinessRule')){
234
                                include_once(GLPI_ROOT."/inc/rulesengine.class.php");
235
                                include_once(GLPI_ROOT."/inc/rule.tracking.class.php");
236
                        }
237
                        return new TrackingBusinessRule();
238
                        break;
239
                case RULE_SOFTWARE_CATEGORY:
240
                        if (!class_exists('SoftwareCategoriesRule')){
241
                                include_once(GLPI_ROOT."/inc/rulesengine.class.php");
242
                                include_once(GLPI_ROOT."/inc/rule.softwarecategories.class.php");
243
                        }
244
                        return new SoftwareCategoriesRule();
245
                        break;
246
                case RULE_DICTIONNARY_SOFTWARE:
247
                        if (!class_exists('DictionnarySoftwareRule')){
248
                                include_once(GLPI_ROOT."/inc/rulesengine.class.php");
249
                                include_once(GLPI_ROOT."/inc/rule.dictionnary.software.class.php");
250
                        }
251
                        return new DictionnarySoftwareRule;
252
                        break;
253
                case RULE_DICTIONNARY_MANUFACTURER:
254
                case RULE_DICTIONNARY_MODEL_NETWORKING :
255
                case RULE_DICTIONNARY_MODEL_COMPUTER:
256
                case RULE_DICTIONNARY_MODEL_MONITOR:
257
                case RULE_DICTIONNARY_MODEL_PRINTER:
258
                case RULE_DICTIONNARY_MODEL_PERIPHERAL:
259
                case RULE_DICTIONNARY_MODEL_PHONE:
260
                case RULE_DICTIONNARY_TYPE_NETWORKING :
261
                case RULE_DICTIONNARY_TYPE_COMPUTER:
262
                case RULE_DICTIONNARY_TYPE_PRINTER:
263
                case RULE_DICTIONNARY_TYPE_MONITOR:
264
                case RULE_DICTIONNARY_TYPE_PERIPHERAL:
265
                case RULE_DICTIONNARY_TYPE_PHONE:
266
                case RULE_DICTIONNARY_OS:
267
                case RULE_DICTIONNARY_OS_SP:
268
                case RULE_DICTIONNARY_OS_VERSION:
269
                        if (!class_exists('DictionnaryDropdownCollection')){
270
                                include_once(GLPI_ROOT."/inc/rulesengine.class.php");
271
                                include_once(GLPI_ROOT."/inc/rule.dictionnary.dropdown.class.php");
272
                        }
273
                        return new RuleDictionnaryDropdown($type);
274
                        break;
275
        }
276
}
277

    
278
function getRuleCollectionClass($type){
279
        switch ($type){
280
                case RULE_OCS_AFFECT_COMPUTER :
281
                        if (!class_exists('OcsRuleCollection')){
282
                                include_once(GLPI_ROOT."/inc/rulesengine.class.php");
283
                                include_once(GLPI_ROOT."/inc/rule.ocs.class.php");
284
                        }
285
                        return new OcsRuleCollection();
286
                        break;                
287
                case RULE_AFFECT_RIGHTS :
288
                        if (!class_exists('RightRuleCollection')){
289
                                include_once(GLPI_ROOT."/inc/rulesengine.class.php");
290
                                include_once(GLPI_ROOT."/inc/rule.right.class.php");
291
                        }
292
                        return new RightRuleCollection();
293
                        break;
294
                case RULE_TRACKING_AUTO_ACTION:
295
                        if (!class_exists('TrackingBusinessRuleCollection')){
296
                                include_once(GLPI_ROOT."/inc/rulesengine.class.php");
297
                                include_once(GLPI_ROOT."/inc/rule.tracking.class.php");
298
                        }
299
                        return new TrackingBusinessRuleCollection();
300
                        break;
301
                case RULE_SOFTWARE_CATEGORY:
302
                        if (!class_exists('SoftwareCategoriesRuleCollection')){
303
                                include_once(GLPI_ROOT."/inc/rulesengine.class.php");
304
                                include_once(GLPI_ROOT."/inc/rule.softwarecategories.class.php");
305
                        }
306
                        return new SoftwareCategoriesRuleCollection();
307
                        break;
308
                case RULE_DICTIONNARY_SOFTWARE:
309
                        if (!class_exists('DictionnarySoftwareCollection')){
310
                                include_once(GLPI_ROOT."/inc/rulesengine.class.php");
311
                                include_once(GLPI_ROOT."/inc/rule.dictionnary.software.class.php");
312
                        }
313
                        return new DictionnarySoftwareCollection;
314
                        break;
315
                case RULE_DICTIONNARY_MANUFACTURER:
316
                case RULE_DICTIONNARY_MODEL_NETWORKING :
317
                case RULE_DICTIONNARY_MODEL_COMPUTER:
318
                case RULE_DICTIONNARY_MODEL_MONITOR:
319
                case RULE_DICTIONNARY_MODEL_PRINTER:
320
                case RULE_DICTIONNARY_MODEL_PERIPHERAL:
321
                case RULE_DICTIONNARY_MODEL_PHONE:
322
                case RULE_DICTIONNARY_TYPE_NETWORKING :
323
                case RULE_DICTIONNARY_TYPE_COMPUTER:
324
                case RULE_DICTIONNARY_TYPE_PRINTER:
325
                case RULE_DICTIONNARY_TYPE_MONITOR:
326
                case RULE_DICTIONNARY_TYPE_PERIPHERAL:
327
                case RULE_DICTIONNARY_TYPE_PHONE:
328
                case RULE_DICTIONNARY_OS:
329
                case RULE_DICTIONNARY_OS_SP:
330
                case RULE_DICTIONNARY_OS_VERSION:
331
                        if (!class_exists('DictionnaryDropdownCollection')){
332
                                include_once(GLPI_ROOT."/inc/rulesengine.class.php");
333
                                include_once(GLPI_ROOT."/inc/rule.dictionnary.dropdown.class.php");
334
                        }
335
                        return new DictionnaryDropdownCollection($type);
336
                        break;
337
        }
338
}
339

    
340
function getRuleCollectionClassByTableName($tablename){
341
        
342
        switch ($tablename){
343
                case "glpi_software":
344
                        return getRuleCollectionClass(RULE_DICTIONNARY_SOFTWARE);
345
                case "glpi_dropdown_manufacturer":
346
                        return getRuleCollectionClass(RULE_DICTIONNARY_MANUFACTURER);
347
                case "glpi_dropdown_model":
348
                        return getRuleCollectionClass(RULE_DICTIONNARY_MODEL_COMPUTER);
349
                case "glpi_dropdown_model_monitors":
350
                        return getRuleCollectionClass(RULE_DICTIONNARY_MODEL_MONITOR);
351
                case "glpi_dropdown_model_printers":
352
                        return getRuleCollectionClass(RULE_DICTIONNARY_MODEL_PRINTER);
353
                case "glpi_dropdown_model_peripherals":
354
                        return getRuleCollectionClass(RULE_DICTIONNARY_MODEL_PERIPHERAL);
355
                case "glpi_dropdown_model_networking":
356
                        return getRuleCollectionClass(RULE_DICTIONNARY_MODEL_NETWORKING);
357
                case "glpi_dropdown_model_phones":
358
                        return getRuleCollectionClass(RULE_DICTIONNARY_MODEL_PHONE);
359
                case "glpi_type_computers":
360
                        return getRuleCollectionClass(RULE_DICTIONNARY_TYPE_COMPUTER);
361
                case "glpi_type_monitors":
362
                        return getRuleCollectionClass(RULE_DICTIONNARY_TYPE_MONITOR);
363
                case "glpi_type_printers":
364
                        return getRuleCollectionClass(RULE_DICTIONNARY_TYPE_PRINTER);
365
                case "glpi_type_peripherals":
366
                        return getRuleCollectionClass(RULE_DICTIONNARY_TYPE_PERIPHERAL);
367
                case "glpi_type_networking":
368
                        return getRuleCollectionClass(RULE_DICTIONNARY_TYPE_NETWORKING);
369
                case "glpi_dropdown_type_phone":
370
                        return getRuleCollectionClass(RULE_DICTIONNARY_TYPE_PHONE);
371
                case "glpi_dropdown_os":
372
                        return getRuleCollectionClass(RULE_DICTIONNARY_OS);
373
                case "glpi_dropdown_os_sp":
374
                        return getRuleCollectionClass(RULE_DICTIONNARY_OS_SP);
375
                case "glpi_dropdown_os_version":
376
                        return getRuleCollectionClass(RULE_DICTIONNARY_OS_VERSION);
377
                default:
378
                        break;                
379
        }
380
}
381

    
382
function getCacheTableByRuleType($type){
383
        
384
        $rulecollection = getRuleCollectionClass($type);
385
        return $rulecollection->cache_table;
386
}
387

    
388
function getRegexResultById($action,$regex_results){
389
        
390
        if (count($regex_results)>0){
391
                if (preg_match_all("/#([0-9])/",$action,$results)>0){
392
                        foreach($results[1] as $result){
393
                                $action=str_replace("#$result",(isset($regex_results[$result])?$regex_results[$result]:''),$action);
394
                        }
395
                }        
396
        }
397
        return $action;
398
}
399

    
400
/**
401
 * Get category name to display in commonheader by rule_type
402
**/
403
function getCategoryNameToDisplay($rule_type){
404
        
405
        switch ($rule_type){
406
                case RULE_OCS_AFFECT_COMPUTER:
407
                case RULE_AFFECT_RIGHTS:
408
                case RULE_SOFTWARE_CATEGORY:
409
                case RULE_TRACKING_AUTO_ACTION:
410
                        return "rule";
411
                default:
412
                        return "dictionnary";
413
        }
414
}
415

    
416
function getAlreadyUsedActionsByRuleID($rule_id,$rule_type){
417
        global $DB,$RULES_ACTIONS;
418
        $actions = array();
419

    
420
        $res = $DB->query("SELECT field FROM glpi_rules_actions WHERE FK_rules='".$rule_id."'");
421
        while ($action = $DB->fetch_array($res)){
422
                if (isset($RULES_ACTIONS[$rule_type][$action["field"]])) {
423
                        $actions[$action["field"]] = $action["field"];
424
                }
425
        }
426
        return $actions;        
427
}
428

    
429
function processManufacturerName($old_name)
430
{
431
        if ($old_name == null)
432
                return $old_name;
433
                
434
        $rulecollection = new DictionnaryDropdownCollection(RULE_DICTIONNARY_MANUFACTURER);
435
        $output=array();
436
        $rulecollection->processAllRules(array("name"=>addslashes($old_name)),$output,array());
437
        if (isset($output["name"]))
438
                return $output["name"];
439
        else
440
                return $old_name;        
441
}
442
?>
Redmine Appliance - Powered by TurnKey Linux