Check for undefined variables.
1 | <?php |
||
2 | |||
3 | namespace Icinga\Module\TrapDirector\Controllers; |
||
4 | |||
5 | use Icinga\Web\Url; |
||
6 | |||
7 | use Exception; |
||
8 | |||
9 | use Icinga\Module\Trapdirector\TrapsController; |
||
10 | use Icinga\Module\Trapdirector\Tables\HandlerTable; |
||
11 | |||
12 | |||
13 | //use Icinga\Web\Form as Form; |
||
14 | /** Rules management |
||
15 | |||
16 | */ |
||
17 | class HandlerController extends TrapsController |
||
18 | { |
||
19 | |||
20 | /** index : list existing rules |
||
21 | */ |
||
22 | public function indexAction() |
||
23 | { |
||
24 | $this->checkReadPermission(); |
||
25 | $this->prepareTabs()->activate('status'); |
||
26 | |||
27 | $dbConn = $this->getUIDatabase()->getDb(); |
||
28 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
29 | |||
30 | $handlerTable = new HandlerTable( |
||
31 | $this->moduleConfig->getTrapRuleName(), |
||
32 | $this->moduleConfig->getHandlerListTitles(), |
||
33 | $this->moduleConfig->getHandlerListDisplayColumns(), |
||
34 | $this->moduleConfig->getHandlerColumns(), |
||
35 | $dbConn->getConnection(), |
||
36 | $this->view, |
||
37 | $this->moduleConfig->urlPath()); |
||
38 | |||
39 | $handlerTable->setMaxPerPage($this->itemListDisplay()); |
||
40 | |||
41 | $handlerTable->setMibloader($this->getMIB()); |
||
42 | |||
43 | $getVars = $this->getRequest()->getParams(); |
||
44 | $handlerTable->getParams($getVars); |
||
45 | |||
46 | if ($handlerTable->isOrderSet() == FALSE) |
||
47 | { // Id no order set order and grouping to categories |
||
48 | $handlerTable->setGrouping('rule_type'); |
||
49 | $handlerTable->setCategoriesArray($this->getHandlersCategory()); |
||
50 | $handlerTable->setOrder(array('rule_type'=>'DESC')); |
||
51 | |||
52 | } |
||
53 | |||
54 | $this->view->handlerTable = $handlerTable; |
||
55 | |||
56 | |||
57 | // TODO : Obsolete remove after new table validation. |
||
58 | |||
59 | /** |
||
60 | $this->getHandlerListTable()->setConnection($dbConn); |
||
61 | $this->getHandlerListTable()->setMibloader($this->getMIB()); |
||
62 | // Apply pagination limits |
||
63 | $this->view->table=$this->applyPaginationLimits($this->getHandlerListTable(),$this->getModuleConfig()->itemListDisplay()); |
||
64 | |||
65 | // Set Filter |
||
66 | $this->view->filterEditor = $this->getHandlerListTable()->getFilterEditor($this->getRequest()); |
||
67 | |||
68 | //$this->displayExitError('Handler/indexAction','Not implemented'); |
||
69 | */ |
||
70 | } |
||
71 | |||
72 | /** test_rule : test a rule |
||
73 | */ |
||
74 | public function testruleAction() |
||
75 | { |
||
76 | $this->checkReadPermission(); |
||
77 | $this->getTabs()->add('get',array( |
||
78 | 'active' => true, |
||
79 | 'label' => $this->translate('Test Rule'), |
||
80 | 'url' => Url::fromRequest() |
||
81 | )); |
||
82 | |||
83 | |||
84 | if ($this->params->get('rule') !== null) |
||
85 | { |
||
86 | $this->view->rule= $this->params->get('rule'); |
||
87 | } |
||
88 | else |
||
89 | { |
||
90 | $this->view->rule=''; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Setup default veiw values for add action |
||
96 | */ |
||
97 | private function add_setup_vars() |
||
98 | { |
||
99 | // variables to send to view |
||
100 | $this->view->hostlist=array(); // host list to input datalist |
||
101 | $this->view->hostname=''; // Host name in input text |
||
102 | $this->view->serviceGet=false; // Set to true to get list of service if only one host set |
||
103 | $this->view->serviceSet=null; // Select service in services select (must have serviceGet=true). |
||
104 | $this->view->mainoid=''; // Trap OID |
||
105 | $this->view->mib=''; // Trap mib |
||
106 | $this->view->name=''; // Trap name |
||
107 | $this->view->trapListForMIB=array(); // Trap list if mib exists for trap |
||
108 | $this->view->objectList=array(); // objects sent with trap |
||
109 | $this->view->display=''; // Initial display |
||
110 | $this->view->rule=''; // rule display |
||
111 | $this->view->revertOK=''; // revert OK in seconds |
||
112 | $this->view->hostid=-1; // normally set by javascript serviceGet() |
||
113 | $this->view->ruleid=-1; // Rule id in DB for update & delete |
||
114 | $this->view->setToUpdate=false; // set form as update form |
||
115 | $this->view->setRuleMatch=-1; // set action on rule match (default nothing) |
||
116 | $this->view->setRuleNoMatch=-1; // set action on rule no match (default nothing) |
||
117 | |||
118 | $this->view->selectGroup=false; // Select by group if true |
||
119 | $this->view->hostgroupid=-1; // host group id |
||
120 | $this->view->serviceGroupGet=false; // Get list of service for group (set serviceSet to select one) |
||
121 | |||
122 | $this->view->modifier=null; |
||
123 | $this->view->modified=null; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Setup new handler display from existing trap |
||
128 | * @param integer $trapid : trap id in DB |
||
129 | */ |
||
130 | private function add_from_existing($trapid) |
||
131 | { |
||
132 | /********** Setup from existing trap ***************/ |
||
133 | // Get the full trap info |
||
134 | $trapDetail=$this->getTrapDetail($trapid); |
||
135 | |||
136 | $hostfilter=$trapDetail->source_ip; |
||
137 | |||
138 | // Get host |
||
139 | try |
||
140 | { |
||
141 | $hosts=$this->getIdoConn()->getHostByIP($hostfilter); |
||
142 | } |
||
143 | catch (Exception $e) |
||
144 | { |
||
145 | $this->displayExitError('Add handler : get host by IP/Name ',$e->getMessage()); |
||
146 | } |
||
147 | |||
148 | |||
149 | // if one unique host found -> put id text input |
||
150 | if (count($hosts)==1) { |
||
151 | $this->view->hostname=$hosts[0]->name; |
||
152 | //$hostid=$hosts[0]->id; |
||
153 | // Tell JS to get services when page is loaded |
||
154 | $this->view->serviceGet=true; |
||
155 | |||
156 | } |
||
157 | else |
||
158 | { |
||
159 | foreach($hosts as $key=>$val) |
||
160 | { |
||
161 | array_push($this->view->hostlist,$hosts[$key]->name); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | // set up trap oid and objects received by the trap |
||
166 | |||
167 | $this->view->mainoid=$trapDetail->trap_oid; |
||
168 | if ($trapDetail->trap_name_mib != null) |
||
169 | { |
||
170 | $this->view->mib=$trapDetail->trap_name_mib; |
||
171 | $this->view->name=$trapDetail->trap_name; |
||
172 | $this->view->trapListForMIB=$this->getMIB() |
||
173 | ->getTrapList($trapDetail->trap_name_mib); |
||
174 | } |
||
175 | |||
176 | // Get all objects that can be in trap from MIB |
||
177 | $allObjects=$this->getMIB()->getObjectList($trapDetail->trap_oid); |
||
178 | // Get all objects in current Trap |
||
179 | $currentTrapObjects=$this->getTrapobjects($trapid); |
||
180 | $oid_index=1; |
||
181 | foreach ($currentTrapObjects as $key => $val) |
||
182 | { |
||
183 | $currentObjectType='Unknown'; |
||
184 | $currentObjectTypeEnum='Unknown'; |
||
185 | if (isset($allObjects[$val->oid]['type'])) |
||
186 | { |
||
187 | $currentObjectType=$allObjects[$val->oid]['type']; |
||
188 | $currentObjectTypeEnum=$allObjects[$val->oid]['type_enum']; |
||
189 | } |
||
190 | $currentObject=array( |
||
191 | $oid_index, |
||
192 | $val->oid, |
||
193 | $val->oid_name_mib, |
||
194 | $val->oid_name, |
||
195 | $val->value, |
||
196 | $currentObjectType, |
||
197 | $currentObjectTypeEnum |
||
198 | ); |
||
199 | $oid_index++; |
||
200 | array_push($this->view->objectList,$currentObject); |
||
201 | // set currrent object to null in allObjects |
||
202 | if (isset($allObjects[$val->oid])) |
||
203 | { |
||
204 | $allObjects[$val->oid]=null; |
||
205 | } |
||
206 | } |
||
207 | if ($allObjects!=null) // in case trap doesn't have objects or is not resolved |
||
208 | { |
||
209 | foreach ($allObjects as $key => $val) |
||
210 | { |
||
211 | if ($val==null) { continue; } |
||
212 | array_push($this->view->objectList, array( |
||
213 | $oid_index, |
||
214 | $key, |
||
215 | $allObjects[$key]['mib'], |
||
216 | $allObjects[$key]['name'], |
||
217 | '', |
||
218 | $allObjects[$key]['type'], |
||
219 | $allObjects[$key]['type_enum'] |
||
220 | )); |
||
221 | $oid_index++; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | // Add a simple display |
||
226 | $this->view->display='Trap '.$trapDetail->trap_name.' received'; |
||
227 | $this->view->create_basic_rule=true; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Check if host & service still exists or set warning message |
||
232 | * @param object $ruleDetail |
||
233 | */ |
||
234 | private function add_check_host_exists($ruleDetail) |
||
235 | { |
||
236 | // Check if hostname still exists |
||
237 | $host_get=$this->getIdoConn()->getHostByName($this->view->hostname); |
||
238 | |||
239 | if (count($host_get)==0) |
||
240 | { |
||
241 | $this->view->warning_message='Host '.$this->view->hostname. ' doesn\'t exists anymore'; |
||
242 | $this->view->serviceGet=false; |
||
243 | } |
||
244 | else |
||
245 | { |
||
246 | // Tell JS to get services when page is loaded |
||
247 | $this->view->serviceGet=true; |
||
248 | // get service id for form to set : |
||
249 | $serviceID=$this->getIdoConn()->getServiceIDByName($this->view->hostname,$ruleDetail->service_name); |
||
250 | if (count($serviceID) ==0) |
||
251 | { |
||
252 | $this->view->warning_message=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
||
253 | } |
||
254 | else |
||
255 | { |
||
256 | $this->view->serviceSet=$serviceID[0]->id; |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Check if hostgroup & service still exists or set warning message |
||
263 | * @param array $ruleDetail |
||
264 | */ |
||
265 | private function add_check_hostgroup_exists($ruleDetail) |
||
266 | { |
||
267 | // Check if groupe exists |
||
268 | $group_get=$this->getIdoConn()->getHostGroupByName($this->view->hostgroupname); |
||
269 | if (count($group_get)==0) |
||
270 | { |
||
271 | $this->view->warning_message='HostGroup '.$this->view->hostgroupname. ' doesn\'t exists anymore'; |
||
272 | $this->view->serviceGroupGet=false; |
||
273 | } |
||
274 | else |
||
275 | { |
||
276 | $grpServices=$this->getIdoConn()->getServicesByHostGroupid($group_get[0]->id); |
||
277 | $foundGrpService=0; |
||
278 | foreach ($grpServices as $grpService) |
||
279 | { |
||
280 | if ($grpService[0] == $ruleDetail->service_name) |
||
281 | { |
||
282 | $foundGrpService=1; |
||
283 | $this->view->serviceSet=$ruleDetail->service_name; |
||
284 | } |
||
285 | } |
||
286 | |||
287 | // Tell JS to get services when page is loaded |
||
288 | $this->view->serviceGroupGet=true; |
||
289 | if ($foundGrpService==0) |
||
290 | { |
||
291 | $this->view->warning_message.=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
||
292 | } |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Create object list array with all OIDs in rule & display |
||
298 | * Replace in rule & display by $<n>$ |
||
299 | * @param string $display |
||
300 | * @param string $rule |
||
301 | * @return array |
||
302 | */ |
||
303 | private function add_create_trap_object_list(&$display, &$rule) |
||
304 | { |
||
305 | $curObjectList=array(); |
||
306 | $index=1; |
||
307 | // check in display & rule for : OID(<oid>) |
||
308 | $matches=array(); |
||
309 | while ( preg_match('/_OID\(([\.0-9\*]+)\)/',$display,$matches) || |
||
310 | preg_match('/_OID\(([\.0-9\*]+)\)/',$rule,$matches)) |
||
311 | { |
||
312 | $curOid=$matches[1]; |
||
313 | |||
314 | if ( (preg_match('/\*/',$curOid) == 0 ) |
||
315 | && ($object=$this->getMIB()->translateOID($curOid)) != null) |
||
316 | { |
||
317 | array_push($curObjectList, array( |
||
318 | $index, |
||
319 | $curOid, |
||
320 | $object['mib'], |
||
321 | $object['name'], |
||
322 | '', |
||
323 | $object['type'], |
||
324 | $object['type_enum'] |
||
325 | )); |
||
326 | } |
||
327 | else |
||
328 | { |
||
329 | array_push($curObjectList, array( |
||
330 | $index, |
||
331 | $curOid, |
||
332 | 'not found', |
||
333 | 'not found', |
||
334 | '', |
||
335 | 'not found', |
||
336 | 'not found' |
||
337 | )); |
||
338 | } |
||
339 | $curOid = preg_replace('/\*/','\*',$curOid); |
||
340 | $display=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$display); |
||
341 | $rule=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$rule); |
||
342 | $index++; |
||
343 | } |
||
344 | return $curObjectList; |
||
345 | } |
||
346 | |||
347 | /** Add a handler |
||
348 | * Get params fromid : setup from existing trap (id of trap table) |
||
349 | * Get param ruleid : edit from existing handler (id of rule table) |
||
350 | */ |
||
351 | public function addAction() |
||
352 | { |
||
353 | $this->checkConfigPermission(); |
||
354 | // set up tab |
||
355 | $this->prepareTabs(); |
||
356 | $this->getTabs()->add('get',array( |
||
357 | 'active' => true, |
||
358 | 'label' => $this->translate('Add handler'), |
||
359 | 'url' => Url::fromRequest() |
||
360 | )); |
||
361 | |||
362 | $this->add_setup_vars(); // setup default $this->view variables. |
||
363 | |||
364 | // Get Mib List from DB |
||
365 | $this->view->mibList=$this->getMIB()->getMIBList(); |
||
366 | |||
367 | // Get categories |
||
368 | $this->view->categoryList = $this->getHandlersCategory(); |
||
369 | |||
370 | //$this->view->trapvalues=false; // Set to true to display 'value' colum in objects |
||
371 | |||
372 | if (($trapid = $this->params->get('fromid')) !== null) { |
||
373 | /********** Setup from existing trap ***************/ |
||
374 | $this->add_from_existing($trapid); |
||
375 | return; |
||
376 | } |
||
377 | |||
378 | |||
379 | if ($this->params->get('ruleid') !== null) { |
||
380 | /************* Rule editing ***************/ |
||
381 | $ruleid=$this->params->get('ruleid'); |
||
382 | $this->view->ruleid=$ruleid; |
||
383 | $this->view->setToUpdate=true; |
||
384 | |||
385 | // Get rule info from DB |
||
386 | $ruleDetail=$this->getRuleDetail($ruleid); |
||
387 | $this->view->hostname=$ruleDetail->host_name; |
||
388 | $this->view->revertOK=$ruleDetail->revert_ok; |
||
389 | $this->view->setRuleMatch=$ruleDetail->action_match; |
||
390 | $this->view->setRuleNoMatch=$ruleDetail->action_nomatch; |
||
391 | $this->view->hostgroupname=$ruleDetail->host_group_name; |
||
392 | $this->view->modified=gmdate("Y-m-d\TH:i:s\Z",$ruleDetail->modified); |
||
393 | $this->view->modifier=$ruleDetail->modifier; |
||
394 | |||
395 | $this->view->comment = $ruleDetail->comment; |
||
396 | $this->view->category = $ruleDetail->category; |
||
397 | |||
398 | // Warning message if host/service don't exists anymore |
||
399 | $this->view->warning_message=''; |
||
400 | if ($this->view->hostname != null) |
||
401 | { |
||
402 | $this->view->selectGroup=false; |
||
403 | // Check if hostname still exists |
||
404 | $this->add_check_host_exists($ruleDetail); |
||
405 | } |
||
406 | else |
||
407 | { |
||
408 | $this->view->selectGroup=true; |
||
409 | $this->add_check_hostgroup_exists($ruleDetail); // Check if groupe exists |
||
410 | } |
||
411 | |||
412 | $this->view->mainoid=$ruleDetail->trap_oid; |
||
413 | $oidName=$this->getMIB()->translateOID($ruleDetail->trap_oid); |
||
414 | if ($oidName != null) // oid is found in mibs |
||
415 | { |
||
416 | $this->view->mib=$oidName['mib']; |
||
417 | $this->view->name=$oidName['name']; |
||
418 | $this->view->trapListForMIB=$this->getMIB() |
||
419 | ->getTrapList($oidName['mib']); |
||
420 | } |
||
421 | // Create object list with : display & rules references (OID) and complete with all objects if found |
||
422 | $display=$ruleDetail->display; |
||
423 | $rule=$ruleDetail->rule; |
||
424 | |||
425 | // Create object list array with all OIDs in rule & display |
||
426 | $curObjectList=$this->add_create_trap_object_list($display, $rule); |
||
427 | |||
428 | // set display |
||
429 | $this->view->display=$display; |
||
430 | $this->view->rule=$rule; |
||
431 | |||
432 | $this->view->objectList=$curObjectList; |
||
433 | } |
||
434 | } |
||
435 | |||
436 | /** Validate form and output message to user |
||
437 | * @param in postdata |
||
438 | * @return string status : OK / <Message> |
||
439 | **/ |
||
440 | protected function handlerformAction() |
||
441 | { |
||
442 | $postData=$this->getRequest()->getPost(); |
||
443 | //print_r($postData ).'<br>'; |
||
444 | |||
445 | $params=array( |
||
446 | // id (also db) => array('post' => post id, 'val' => default val, 'db' => send to table) |
||
447 | 'hostgroup' => array('post' => 'hostgroup', 'db'=>false), |
||
448 | 'db_rule' => array('post' => 'db_rule', 'db'=>false), |
||
449 | 'hostid' => array('post' => 'hostid', 'db'=>false), |
||
450 | 'host_name' => array('post' => 'hostname', 'val' => null, 'db'=>true), |
||
451 | 'host_group_name'=> array('post' => null, 'val' => null, 'db'=>true), |
||
452 | 'serviceid' => array('post' => 'serviceid', 'db'=>false), |
||
453 | 'service_name' => array('post' => 'serviceName', 'db'=>true), |
||
454 | 'comment' => array('post' => 'comment', 'val' => '', 'db'=>true), |
||
455 | 'rule_type' => array('post' => 'category', 'val' => 0, 'db'=>true), |
||
456 | 'trap_oid' => array('post' => 'oid', 'db'=>true), |
||
457 | 'revert_ok' => array('post' => 'revertOK', 'val' => 0, 'db'=>true), |
||
458 | 'display' => array('post' => 'display', 'val' => '', 'db'=>true), |
||
459 | 'rule' => array('post' => 'rule', 'val' => '', 'db'=>true), |
||
460 | 'action_match' => array('post' => 'ruleMatch', 'val' => -1, 'db'=>true), |
||
461 | 'action_nomatch'=> array('post' => 'ruleNoMatch', 'val' => -1, 'db'=>true), |
||
462 | 'ip4' => array('post' => null, 'val' => null, 'db'=>true), |
||
463 | 'ip6' => array('post' => null, 'val' => null, 'db'=>true), |
||
464 | 'action_form' => array('post' => 'action_form', 'val' => null, 'db'=>false) |
||
465 | ); |
||
466 | |||
467 | if (isset($postData[$params['action_form']['post']]) |
||
468 | && $postData[$params['action_form']['post']] == 'delete' ) |
||
469 | { |
||
470 | try |
||
471 | { |
||
472 | $this->getUIDatabase()->deleteRule($postData[$params['db_rule']['post']]); |
||
473 | } |
||
474 | catch (Exception $e) |
||
475 | { |
||
476 | $this->_helper->json(array('status'=>$e->getMessage(),'location'=>'Deleting Rule')); |
||
477 | return; |
||
478 | } |
||
479 | //$this->Module()-> |
||
480 | $this->_helper->json(array( |
||
481 | 'status'=>'OK', |
||
482 | 'redirect'=>'trapdirector/handler' |
||
483 | |||
484 | )); |
||
485 | } |
||
486 | foreach (array_keys($params) as $key) |
||
487 | { |
||
488 | if ($params[$key]['post']==null) continue; // data not sent in post vars |
||
489 | if (! isset($postData[$params[$key]['post']])) |
||
490 | { |
||
491 | // should not happen as the js checks data |
||
492 | $this->_helper->json(array('status'=>'No ' . $key)); |
||
493 | } |
||
494 | else |
||
495 | { |
||
496 | $data=$postData[$params[$key]['post']]; |
||
497 | if ($data!=null && $data !="") |
||
498 | { |
||
499 | $params[$key]['val']=$postData[$params[$key]['post']]; |
||
500 | } |
||
501 | } |
||
502 | } |
||
503 | $this->getIdoConn(); //Set apiMode |
||
504 | try |
||
505 | { |
||
506 | $isHostGroup=($params['hostgroup']['val'] == 1)?true:false; |
||
507 | if (! $isHostGroup ) |
||
508 | { // checks if selection by host |
||
509 | $hostAddr=$this->getIdoConn()->getHostInfoByID($params['hostid']['val']); |
||
510 | if ($hostAddr === NULL) throw new \Exception("No object found"); |
||
511 | $params['ip4']['val']=$hostAddr->ip4; |
||
512 | $params['ip6']['val']=$hostAddr->ip6; |
||
513 | $checkHostName=$hostAddr->name; |
||
514 | if ($params['host_name']['val'] != $checkHostName) |
||
515 | { |
||
516 | $this->_helper->json(array('status'=>"Invalid host id : Please re enter host name")); |
||
517 | return; |
||
518 | } |
||
519 | if ($this->apiMode == TRUE) |
||
520 | { |
||
521 | $serviceName=$this->getIdoConn()->getServiceById($params['serviceid']['val']); |
||
522 | if (count($serviceName) == 0 ) |
||
523 | { |
||
524 | $this->_helper->json(array('status'=>"Invalid service id : Please re enter service",'sent'=>$params['serviceid']['val'],'found'=>$serviceName[0]->__name)); |
||
525 | return; |
||
526 | } |
||
527 | } |
||
528 | else |
||
529 | { |
||
530 | if (!is_numeric($params['serviceid']['val'])) |
||
531 | { |
||
532 | $this->_helper->json(array('status'=>"Invalid service id ". $params['serviceid']['val'])); |
||
533 | return; |
||
534 | } |
||
535 | |||
536 | $serviceName=$this->getUIDatabase()->getObjectNameByid($params['serviceid']['val']); |
||
537 | if ($params['service_name']['val'] != $serviceName->name2) |
||
538 | { |
||
539 | $this->_helper->json(array('status'=>"Invalid service id : Please re enter service")); |
||
540 | return; |
||
541 | } |
||
542 | } |
||
543 | } |
||
544 | else |
||
545 | { |
||
546 | if ($this->apiMode == TRUE) |
||
547 | { |
||
548 | $object=$this->getIdoConn()->getHostGroupById($params['hostid']['val']); |
||
549 | if (count($object) == 0 || $params['host_name']['val'] != $object->__name) |
||
550 | { |
||
551 | $this->_helper->json(array('status'=>"Invalid object group id : Please re enter service")); |
||
552 | return; |
||
553 | } |
||
554 | } |
||
555 | else |
||
556 | { |
||
557 | $object=$this->getUIDatabase()->getObjectNameByid($params['hostid']['val']); |
||
558 | if ($params['host_name']['val'] != $object->name1) |
||
559 | { |
||
560 | $this->_helper->json(array('status'=>"Invalid object group id : Please re enter service")); |
||
561 | return; |
||
562 | } |
||
563 | } |
||
564 | // Put param in correct column (group_name) |
||
565 | $params['host_group_name']['val'] = $params['host_name']['val']; |
||
566 | $params['host_name']['val']=null; |
||
567 | } |
||
568 | $dbparams=array(); |
||
569 | foreach ($params as $key=>$val) |
||
570 | { |
||
571 | if ($val['db']==true ) |
||
572 | { |
||
573 | $dbparams[$key] = $val['val']; |
||
574 | } |
||
575 | } |
||
576 | // echo '<br>'; print_r($dbparams);echo '<br>'; |
||
577 | |||
578 | if ($params['db_rule']['val'] == -1 || $params['action_form']['val'] == 'clone') |
||
579 | { // If no rule number or action is clone, add the handler |
||
580 | $ruleID=$this->getUIDatabase()->addHandlerRule($dbparams); |
||
581 | } |
||
582 | else |
||
583 | { |
||
584 | $this->getUIDatabase()->updateHandlerRule($dbparams,$params['db_rule']['val']); |
||
585 | $ruleID=$params['db_rule']['val']; |
||
586 | } |
||
587 | } |
||
588 | catch (Exception $e) |
||
589 | { |
||
590 | $this->_helper->json(array('status'=>$e->getMessage(),'location'=>'Add/update Rule','line'=>$e->getLine(),'file'=>$e->getFile())); |
||
591 | return; |
||
592 | } |
||
593 | $this->_helper->json(array('status'=>'OK', 'id' => $ruleID)); |
||
594 | |||
595 | } |
||
596 | |||
597 | /** Get trap detail by trapid. |
||
598 | * @param integer $trapid : id of trap in received table |
||
599 | * @return array (objects) |
||
600 | */ |
||
601 | protected function getTrapDetail($trapid) |
||
602 | { |
||
603 | if (!preg_match('/^[0-9]+$/',$trapid)) { throw new Exception('Invalid id'); } |
||
604 | $queryArray=$this->getModuleConfig()->trapDetailQuery(); |
||
605 | |||
606 | $dbConn = $this->getUIDatabase()->getDbConn(); |
||
607 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
608 | // *************** Get main data |
||
609 | // extract columns and titles; |
||
610 | $elmts=NULL; |
||
611 | foreach ($queryArray as $key => $val) { |
||
612 | $elmts[$key]=$val[1]; |
||
613 | } |
||
614 | try |
||
615 | { |
||
616 | $query = $dbConn->select() |
||
617 | ->from($this->getModuleConfig()->getTrapTableName(),$elmts) |
||
618 | ->where('id=?',$trapid); |
||
619 | $trapDetail=$dbConn->fetchRow($query); |
||
620 | if ( $trapDetail == null ) |
||
621 | { |
||
622 | $trapDetail = 'NULL'; |
||
623 | throw new Exception('No traps was found with id = '.$trapid); |
||
624 | } |
||
625 | } |
||
626 | catch (Exception $e) |
||
627 | { |
||
628 | $this->displayExitError('Add handler : get trap detail returning : '.print_r($trapDetail,true),$e->getMessage()); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
629 | return; |
||
630 | } |
||
631 | |||
632 | return $trapDetail; |
||
633 | |||
634 | } |
||
635 | |||
636 | /** Get trap objects |
||
637 | * @param integer $trapid : trap id |
||
638 | * @return array : full column in db of trap id |
||
639 | */ |
||
640 | protected function getTrapobjects($trapid) |
||
641 | { |
||
642 | if (!preg_match('/^[0-9]+$/',$trapid)) { throw new Exception('Invalid id'); } |
||
643 | $queryArrayData=$this->getModuleConfig()->trapDataDetailQuery(); |
||
644 | |||
645 | $dbConn = $this->getUIDatabase()->getDbConn(); |
||
646 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
647 | // *************** Get object data |
||
648 | // extract columns and titles; |
||
649 | $data_elmts=NULL; |
||
650 | foreach ($queryArrayData as $key => $val) { |
||
651 | $data_elmts[$key]=$val[1]; |
||
652 | } |
||
653 | try |
||
654 | { |
||
655 | $query = $dbConn->select() |
||
656 | ->from($this->moduleConfig->getTrapDataTableName(),$data_elmts) |
||
657 | ->where('trap_id=?',$trapid); |
||
658 | $trapDetail=$dbConn->fetchAll($query); |
||
659 | // if ( $trapDetail == null ) throw new Exception('No traps was found with id = '.$trapid); |
||
660 | } |
||
661 | catch (Exception $e) |
||
662 | { |
||
663 | $this->displayExitError('Add handler : get trap data detail : ',$e->getMessage()); |
||
664 | return array(); |
||
665 | } |
||
666 | |||
667 | return $trapDetail; |
||
668 | } |
||
669 | |||
670 | /** Get rule detail by ruleid. |
||
671 | * @param integer $ruleid int id of rule in rule table |
||
672 | * @return object|array : column objects in db |
||
673 | * |
||
674 | */ |
||
675 | protected function getRuleDetail($ruleid) |
||
676 | { |
||
677 | if (!preg_match('/^[0-9]+$/',$ruleid)) { throw new Exception('Invalid id'); } |
||
678 | $queryArray=$this->getModuleConfig()->ruleDetailQuery(); |
||
679 | |||
680 | $dbConn = $this->getUIDatabase()->getDbConn(); |
||
681 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
682 | // *************** Get main data |
||
683 | try |
||
684 | { |
||
685 | $query = $dbConn->select() |
||
686 | ->from($this->getModuleConfig()->getTrapRuleName(),$queryArray) |
||
687 | ->where('id=?',$ruleid); |
||
688 | $ruleDetail=$dbConn->fetchRow($query); |
||
689 | if ( $ruleDetail == null ) throw new Exception('No rule was found with id = '.$ruleid); |
||
690 | } |
||
691 | catch (Exception $e) |
||
692 | { |
||
693 | $this->displayExitError('Update handler : get rule detail',$e->getMessage()); |
||
694 | throw new Exception('Error : ' . $e->getMessage()); |
||
695 | } |
||
696 | |||
697 | return $ruleDetail; |
||
698 | |||
699 | } |
||
700 | |||
701 | /** Setup tabs for rules |
||
702 | */ |
||
703 | protected function prepareTabs() |
||
704 | { |
||
705 | return $this->getTabs()->add('status', array( |
||
706 | 'label' => $this->translate('Trap handlers'), |
||
707 | 'url' => $this->getModuleConfig()->urlPath() . '/handler') |
||
708 | ); |
||
709 | } |
||
710 | |||
711 | } |