@@ -56,8 +56,7 @@ discard block |
||
| 56 | 56 | if ($pluginDir == '') |
| 57 | 57 | { |
| 58 | 58 | $this->pluginDir=dirname(__DIR__).'/Plugins'; |
| 59 | - } |
|
| 60 | - else |
|
| 59 | + } else |
|
| 61 | 60 | { |
| 62 | 61 | $this->pluginDir=$pluginDir; |
| 63 | 62 | } |
@@ -94,8 +93,7 @@ discard block |
||
| 94 | 93 | { |
| 95 | 94 | $this->logClass->log('No enabled plugins',DEBUG); |
| 96 | 95 | return; |
| 97 | - } |
|
| 98 | - else |
|
| 96 | + } else |
|
| 99 | 97 | { // Saved config : <plugin name>;<Catch all OID ? 1|0>;<Trap target ? 1|0>;<func 1 name>|<func 2 name>... ,<plugin2 name>.... |
| 100 | 98 | $this->logClass->log('Enabled plugins = '.$PluginList,DEBUG); |
| 101 | 99 | |
@@ -233,9 +231,11 @@ discard block |
||
| 233 | 231 | */ |
| 234 | 232 | public function registerPlugin(string $pluginName) |
| 235 | 233 | { |
| 236 | - if ( ! isset($this->pluginsList[$pluginName]) ) // Plugin isn't enable, create entry |
|
| 234 | + if ( ! isset($this->pluginsList[$pluginName]) ) { |
|
| 235 | + // Plugin isn't enable, create entry |
|
| 237 | 236 | { |
| 238 | 237 | $pluginListElmt = array(); |
| 238 | + } |
|
| 239 | 239 | $pluginListElmt['object'] = null; // class not loaded |
| 240 | 240 | $pluginListElmt['enabled'] = false; |
| 241 | 241 | $this->pluginsList[$pluginName] = $pluginListElmt; |
@@ -282,8 +282,7 @@ discard block |
||
| 282 | 282 | . $pluginName . ' and ' . $this->functionList[$fname]['plugin']); |
| 283 | 283 | } |
| 284 | 284 | |
| 285 | - } |
|
| 286 | - else |
|
| 285 | + } else |
|
| 287 | 286 | { |
| 288 | 287 | $this->functionList[$fname]=array(); |
| 289 | 288 | $this->functionList[$fname]['plugin'] = $pluginName; |
@@ -326,11 +325,13 @@ discard block |
||
| 326 | 325 | $retDisplay .= $e->getMessage() . ' / '; |
| 327 | 326 | } |
| 328 | 327 | } |
| 329 | - if ($checkEnabled === false) // Load all php files in plugin dir |
|
| 328 | + if ($checkEnabled === false) { |
|
| 329 | + // Load all php files in plugin dir |
|
| 330 | 330 | { |
| 331 | 331 | foreach (glob($this->pluginDir."/*.php") as $filename) |
| 332 | 332 | { |
| 333 | 333 | $pluginName=basename($filename,'.php'); |
| 334 | + } |
|
| 334 | 335 | if (!preg_match('/^[a-zA-Z0-9]+$/',$pluginName)) |
| 335 | 336 | { |
| 336 | 337 | $this->logClass->log("Invalid plugin name : ".$pluginName, WARN); |
@@ -348,8 +349,7 @@ discard block |
||
| 348 | 349 | if ($retDisplay == '') |
| 349 | 350 | { |
| 350 | 351 | return 'All plugins loaded OK'; |
| 351 | - } |
|
| 352 | - else |
|
| 352 | + } else |
|
| 353 | 353 | { |
| 354 | 354 | return $retDisplay; |
| 355 | 355 | } |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * $pluginsList[plugin name]['target'] : bool true if plugin can be trap processing target |
| 25 | 25 | * $pluginsList[plugin name]['enabled'] : bool true if plugin is in enabled list |
| 26 | 26 | **/ |
| 27 | - protected $pluginsList = array(); |
|
| 27 | + protected $pluginsList=array(); |
|
| 28 | 28 | |
| 29 | 29 | /** Array of functions names |
| 30 | 30 | * @var array $functionList |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | * @param string $plugin_dir optional plugin directory |
| 52 | 52 | * @throws \Exception |
| 53 | 53 | */ |
| 54 | - function __construct(Trap $trapClass,string $pluginDir='') |
|
| 54 | + function __construct(Trap $trapClass, string $pluginDir='') |
|
| 55 | 55 | { |
| 56 | 56 | if ($pluginDir == '') |
| 57 | 57 | { |
@@ -88,42 +88,42 @@ discard block |
||
| 88 | 88 | */ |
| 89 | 89 | private function loadEnabledPlugins() |
| 90 | 90 | { |
| 91 | - $PluginList = $this->trapClass->trapsDB->getDBConfig('enabled_plugins'); |
|
| 91 | + $PluginList=$this->trapClass->trapsDB->getDBConfig('enabled_plugins'); |
|
| 92 | 92 | |
| 93 | 93 | if ($PluginList === null || $PluginList == '') |
| 94 | 94 | { |
| 95 | - $this->logClass->log('No enabled plugins',DEBUG); |
|
| 95 | + $this->logClass->log('No enabled plugins', DEBUG); |
|
| 96 | 96 | return; |
| 97 | 97 | } |
| 98 | 98 | else |
| 99 | 99 | { // Saved config : <plugin name>;<Catch all OID ? 1|0>;<Trap target ? 1|0>;<func 1 name>|<func 2 name>... ,<plugin2 name>.... |
| 100 | - $this->logClass->log('Enabled plugins = '.$PluginList,DEBUG); |
|
| 100 | + $this->logClass->log('Enabled plugins = '.$PluginList, DEBUG); |
|
| 101 | 101 | |
| 102 | - $pluginArray = explode(',', $PluginList); |
|
| 102 | + $pluginArray=explode(',', $PluginList); |
|
| 103 | 103 | foreach ($pluginArray as $pluginElmt) |
| 104 | 104 | { |
| 105 | - $pluginElmt = explode(';',$pluginElmt); |
|
| 105 | + $pluginElmt=explode(';', $pluginElmt); |
|
| 106 | 106 | if ($pluginElmt === false || count($pluginElmt) != 4) |
| 107 | 107 | { |
| 108 | - throw new \Exception('Invalid plugin configuration : '. $PluginList ); |
|
| 108 | + throw new \Exception('Invalid plugin configuration : '.$PluginList); |
|
| 109 | 109 | } |
| 110 | 110 | $pluginName=$pluginElmt[0]; |
| 111 | 111 | |
| 112 | - $pluginListElmt = array(); |
|
| 113 | - $pluginListElmt['object'] = null; // class not loaded |
|
| 114 | - $pluginListElmt['allOID'] = ($pluginElmt[1]=='1') ? true : false; |
|
| 115 | - $pluginListElmt['target'] = ($pluginElmt[2]=='1') ? true : false; |
|
| 116 | - $pluginListElmt['enabled'] = true; |
|
| 112 | + $pluginListElmt=array(); |
|
| 113 | + $pluginListElmt['object']=null; // class not loaded |
|
| 114 | + $pluginListElmt['allOID']=($pluginElmt[1] == '1') ? true : false; |
|
| 115 | + $pluginListElmt['target']=($pluginElmt[2] == '1') ? true : false; |
|
| 116 | + $pluginListElmt['enabled']=true; |
|
| 117 | 117 | |
| 118 | - $this->pluginsList[$pluginName] = $pluginListElmt; |
|
| 118 | + $this->pluginsList[$pluginName]=$pluginListElmt; |
|
| 119 | 119 | |
| 120 | 120 | // deal with plugin functions |
| 121 | - $pluginFunctions = explode('|',$pluginElmt[3]); |
|
| 121 | + $pluginFunctions=explode('|', $pluginElmt[3]); |
|
| 122 | 122 | if ($pluginFunctions !== false) |
| 123 | 123 | { |
| 124 | 124 | foreach ($pluginFunctions as $function) |
| 125 | 125 | { |
| 126 | - $this->functionList[$function] = array( |
|
| 126 | + $this->functionList[$function]=array( |
|
| 127 | 127 | 'plugin' => $pluginName, |
| 128 | 128 | 'function' => null |
| 129 | 129 | ); |
@@ -155,16 +155,16 @@ discard block |
||
| 155 | 155 | { |
| 156 | 156 | continue; |
| 157 | 157 | } |
| 158 | - $functionString .= ($functionString == '') ? '' : '|'; // add separator if not empty |
|
| 159 | - $functionString .= $fName; |
|
| 158 | + $functionString.=($functionString == '') ? '' : '|'; // add separator if not empty |
|
| 159 | + $functionString.=$fName; |
|
| 160 | 160 | } |
| 161 | - $saveString .= ($saveString == '')?'':',' ; |
|
| 161 | + $saveString.=($saveString == '') ? '' : ','; |
|
| 162 | 162 | |
| 163 | - $allOID = ($value['allOID'] === true) ? 1 : 0; |
|
| 164 | - $target = ($value['target'] === true) ? 1 : 0; |
|
| 165 | - $saveString .= $name . ';' . $allOID . ';' . $target . ';' . $functionString ; |
|
| 163 | + $allOID=($value['allOID'] === true) ? 1 : 0; |
|
| 164 | + $target=($value['target'] === true) ? 1 : 0; |
|
| 165 | + $saveString.=$name.';'.$allOID.';'.$target.';'.$functionString; |
|
| 166 | 166 | } |
| 167 | - $this->logClass->log('Saving : ' . $saveString,DEBUG); |
|
| 167 | + $this->logClass->log('Saving : '.$saveString, DEBUG); |
|
| 168 | 168 | return $this->trapClass->trapsDB->setDBConfig('enabled_plugins', $saveString); |
| 169 | 169 | } |
| 170 | 170 | |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | { |
| 179 | 179 | if ($value['enabled'] == true) |
| 180 | 180 | { |
| 181 | - array_push($retArray,$name); |
|
| 181 | + array_push($retArray, $name); |
|
| 182 | 182 | } |
| 183 | 183 | } |
| 184 | 184 | return $retArray; |
@@ -190,29 +190,29 @@ discard block |
||
| 190 | 190 | * @param bool $enabled true to enable, false to disable |
| 191 | 191 | * @return bool true if OK, or false (error logged) |
| 192 | 192 | */ |
| 193 | - public function enablePlugin(string $pluginName,bool $enabled) |
|
| 193 | + public function enablePlugin(string $pluginName, bool $enabled) |
|
| 194 | 194 | { |
| 195 | 195 | if ($enabled === false) |
| 196 | 196 | { |
| 197 | 197 | // If plugin is defined set to disable |
| 198 | - if ( isset($this->pluginsList[$pluginName])) |
|
| 198 | + if (isset($this->pluginsList[$pluginName])) |
|
| 199 | 199 | { |
| 200 | - $this->pluginsList[$pluginName]['enabled'] = false; |
|
| 200 | + $this->pluginsList[$pluginName]['enabled']=false; |
|
| 201 | 201 | } |
| 202 | 202 | return $this->saveEnabledPlugins(); |
| 203 | 203 | } |
| 204 | 204 | // Check if plugin is loaded / exists |
| 205 | - if ( ! isset($this->pluginsList[$pluginName]) || |
|
| 205 | + if (!isset($this->pluginsList[$pluginName]) || |
|
| 206 | 206 | $this->pluginsList[$pluginName]['object'] === null) |
| 207 | 207 | { |
| 208 | 208 | try { |
| 209 | 209 | $this->registerPlugin($pluginName); |
| 210 | 210 | } catch (Exception $e) { |
| 211 | - $this->logClass->log('Cannot enable plugin : ' . $e->getMessage(),WARN); |
|
| 211 | + $this->logClass->log('Cannot enable plugin : '.$e->getMessage(), WARN); |
|
| 212 | 212 | return false; |
| 213 | 213 | } |
| 214 | 214 | } |
| 215 | - $this->pluginsList[$pluginName]['enabled'] = true; |
|
| 215 | + $this->pluginsList[$pluginName]['enabled']=true; |
|
| 216 | 216 | // save in DB and return |
| 217 | 217 | return $this->saveEnabledPlugins(); |
| 218 | 218 | } |
@@ -233,12 +233,12 @@ discard block |
||
| 233 | 233 | */ |
| 234 | 234 | public function registerPlugin(string $pluginName) |
| 235 | 235 | { |
| 236 | - if ( ! isset($this->pluginsList[$pluginName]) ) // Plugin isn't enable, create entry |
|
| 236 | + if (!isset($this->pluginsList[$pluginName])) // Plugin isn't enable, create entry |
|
| 237 | 237 | { |
| 238 | - $pluginListElmt = array(); |
|
| 239 | - $pluginListElmt['object'] = null; // class not loaded |
|
| 240 | - $pluginListElmt['enabled'] = false; |
|
| 241 | - $this->pluginsList[$pluginName] = $pluginListElmt; |
|
| 238 | + $pluginListElmt=array(); |
|
| 239 | + $pluginListElmt['object']=null; // class not loaded |
|
| 240 | + $pluginListElmt['enabled']=false; |
|
| 241 | + $this->pluginsList[$pluginName]=$pluginListElmt; |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | if ($this->pluginsList[$pluginName]['object'] !== null) |
@@ -247,13 +247,13 @@ discard block |
||
| 247 | 247 | } |
| 248 | 248 | try { |
| 249 | 249 | // Include plugin file |
| 250 | - include_once($this->pluginDir.'/' . $pluginName . '.php'); |
|
| 250 | + include_once($this->pluginDir.'/'.$pluginName.'.php'); |
|
| 251 | 251 | |
| 252 | 252 | // Create full class name with namespace |
| 253 | - $pluginClassName = __NAMESPACE__ . '\\Plugins\\' . $pluginName; |
|
| 253 | + $pluginClassName=__NAMESPACE__.'\\Plugins\\'.$pluginName; |
|
| 254 | 254 | |
| 255 | 255 | // Create class |
| 256 | - $newClass = new $pluginClassName(); |
|
| 256 | + $newClass=new $pluginClassName(); |
|
| 257 | 257 | |
| 258 | 258 | // Set logging |
| 259 | 259 | $newClass->setLoggingClass($this->logClass); |
@@ -276,33 +276,33 @@ discard block |
||
| 276 | 276 | { |
| 277 | 277 | if (isset($this->functionList[$fname])) |
| 278 | 278 | { |
| 279 | - if ($this->functionList[$fname]['plugin'] != $pluginName ) |
|
| 279 | + if ($this->functionList[$fname]['plugin'] != $pluginName) |
|
| 280 | 280 | { |
| 281 | - throw new Exception('Duplicate function name '.$fname . ' in ' |
|
| 282 | - . $pluginName . ' and ' . $this->functionList[$fname]['plugin']); |
|
| 281 | + throw new Exception('Duplicate function name '.$fname.' in ' |
|
| 282 | + . $pluginName.' and '.$this->functionList[$fname]['plugin']); |
|
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | } |
| 286 | 286 | else |
| 287 | 287 | { |
| 288 | 288 | $this->functionList[$fname]=array(); |
| 289 | - $this->functionList[$fname]['plugin'] = $pluginName; |
|
| 289 | + $this->functionList[$fname]['plugin']=$pluginName; |
|
| 290 | 290 | } |
| 291 | 291 | $this->functionList[$fname]['function']=$function['function']; |
| 292 | 292 | } |
| 293 | - $this->logClass->log('Registered plugin '.$pluginName,DEBUG); |
|
| 293 | + $this->logClass->log('Registered plugin '.$pluginName, DEBUG); |
|
| 294 | 294 | |
| 295 | 295 | } catch (Exception $e) { |
| 296 | 296 | unset($this->pluginsList[$pluginName]); |
| 297 | - $errorMessage = "Error registering plugin $pluginName : ".$e->getMessage(); |
|
| 298 | - $this->logClass->log($errorMessage,WARN); |
|
| 297 | + $errorMessage="Error registering plugin $pluginName : ".$e->getMessage(); |
|
| 298 | + $this->logClass->log($errorMessage, WARN); |
|
| 299 | 299 | // Disable the plugin |
| 300 | 300 | $this->enablePlugin($pluginName, false); |
| 301 | 301 | throw new \Exception($errorMessage); |
| 302 | 302 | } catch (Throwable $t) { |
| 303 | 303 | unset($this->pluginsList[$pluginName]); |
| 304 | - $errorMessage = $t->getMessage() . ' in file ' . $t->getFile() . ' line ' . $t->getLine(); |
|
| 305 | - $this->logClass->log($errorMessage,WARN); |
|
| 304 | + $errorMessage=$t->getMessage().' in file '.$t->getFile().' line '.$t->getLine(); |
|
| 305 | + $this->logClass->log($errorMessage, WARN); |
|
| 306 | 306 | // Disable the plugin |
| 307 | 307 | $this->enablePlugin($pluginName, false); |
| 308 | 308 | throw new \Exception($errorMessage); |
@@ -323,24 +323,24 @@ discard block |
||
| 323 | 323 | try { |
| 324 | 324 | $this->registerPlugin($pluginName); |
| 325 | 325 | } catch (Exception $e) { |
| 326 | - $retDisplay .= $e->getMessage() . ' / '; |
|
| 326 | + $retDisplay.=$e->getMessage().' / '; |
|
| 327 | 327 | } |
| 328 | 328 | } |
| 329 | 329 | if ($checkEnabled === false) // Load all php files in plugin dir |
| 330 | 330 | { |
| 331 | 331 | foreach (glob($this->pluginDir."/*.php") as $filename) |
| 332 | 332 | { |
| 333 | - $pluginName=basename($filename,'.php'); |
|
| 334 | - if (!preg_match('/^[a-zA-Z0-9]+$/',$pluginName)) |
|
| 333 | + $pluginName=basename($filename, '.php'); |
|
| 334 | + if (!preg_match('/^[a-zA-Z0-9]+$/', $pluginName)) |
|
| 335 | 335 | { |
| 336 | 336 | $this->logClass->log("Invalid plugin name : ".$pluginName, WARN); |
| 337 | - $retDisplay .= "Invalid plugin name : ".$pluginName . " / "; |
|
| 337 | + $retDisplay.="Invalid plugin name : ".$pluginName." / "; |
|
| 338 | 338 | break; |
| 339 | 339 | } |
| 340 | 340 | try { // Already registerd plugin will simply return false |
| 341 | 341 | $this->registerPlugin($pluginName); |
| 342 | 342 | } catch (Exception $e) { |
| 343 | - $retDisplay .= $e->getMessage() . ' / '; |
|
| 343 | + $retDisplay.=$e->getMessage().' / '; |
|
| 344 | 344 | } |
| 345 | 345 | } |
| 346 | 346 | } |
@@ -380,17 +380,17 @@ discard block |
||
| 380 | 380 | { |
| 381 | 381 | $this->registerPlugin($name); // can throw exception handled by caller |
| 382 | 382 | } |
| 383 | - $retObj = new stdClass(); |
|
| 384 | - $retObj->name = $name; |
|
| 385 | - $retObj->catchAllTraps = $this->pluginsList[$name]['allOID']; |
|
| 386 | - $retObj->processTraps = $this->pluginsList[$name]['target']; |
|
| 387 | - $retObj->description = $this->pluginsList[$name]['object']->description; |
|
| 383 | + $retObj=new stdClass(); |
|
| 384 | + $retObj->name=$name; |
|
| 385 | + $retObj->catchAllTraps=$this->pluginsList[$name]['allOID']; |
|
| 386 | + $retObj->processTraps=$this->pluginsList[$name]['target']; |
|
| 387 | + $retObj->description=$this->pluginsList[$name]['object']->description; |
|
| 388 | 388 | $functions=array(); |
| 389 | 389 | foreach ($this->functionList as $fName => $func) |
| 390 | 390 | { |
| 391 | 391 | if ($func['plugin'] == $name) |
| 392 | 392 | { |
| 393 | - array_push($functions,$fName); |
|
| 393 | + array_push($functions, $fName); |
|
| 394 | 394 | } |
| 395 | 395 | } |
| 396 | 396 | $retObj->funcArray=$functions; |
@@ -403,13 +403,13 @@ discard block |
||
| 403 | 403 | * @param string $pluginName |
| 404 | 404 | * @return boolean returns plugin object of false; |
| 405 | 405 | */ |
| 406 | - public function getFunction($funcName,&$pluginName) |
|
| 406 | + public function getFunction($funcName, &$pluginName) |
|
| 407 | 407 | { |
| 408 | - if (! isset($this->functionList[$funcName]) ) |
|
| 408 | + if (!isset($this->functionList[$funcName])) |
|
| 409 | 409 | { |
| 410 | 410 | return false; |
| 411 | 411 | } |
| 412 | - $pluginName = $this->functionList[$funcName]['plugin']; |
|
| 412 | + $pluginName=$this->functionList[$funcName]['plugin']; |
|
| 413 | 413 | return true; |
| 414 | 414 | } |
| 415 | 415 | |
@@ -421,21 +421,21 @@ discard block |
||
| 421 | 421 | */ |
| 422 | 422 | public function getFunctionDetails($funcName) |
| 423 | 423 | { |
| 424 | - if (! isset($this->functionList[$funcName]) ) |
|
| 424 | + if (!isset($this->functionList[$funcName])) |
|
| 425 | 425 | { |
| 426 | 426 | return false; |
| 427 | 427 | } |
| 428 | - $pluginName = $this->functionList[$funcName]['plugin']; // plugin name |
|
| 429 | - $plugin = $this->pluginsList[$pluginName]['object']; // plugin object |
|
| 428 | + $pluginName=$this->functionList[$funcName]['plugin']; // plugin name |
|
| 429 | + $plugin=$this->pluginsList[$pluginName]['object']; // plugin object |
|
| 430 | 430 | if ($plugin === null) |
| 431 | 431 | { |
| 432 | 432 | $this->registerPlugin($pluginName); // can throw exception handled by caller |
| 433 | 433 | } |
| 434 | - $retObj = new stdClass(); |
|
| 435 | - $retObj->name = $funcName; |
|
| 436 | - $retObj->plugin = $pluginName; |
|
| 437 | - $retObj->params = $plugin->functions[$funcName]['params']; |
|
| 438 | - $retObj->description = $plugin->functions[$funcName]['description']; |
|
| 434 | + $retObj=new stdClass(); |
|
| 435 | + $retObj->name=$funcName; |
|
| 436 | + $retObj->plugin=$pluginName; |
|
| 437 | + $retObj->params=$plugin->functions[$funcName]['params']; |
|
| 438 | + $retObj->description=$plugin->functions[$funcName]['description']; |
|
| 439 | 439 | return $retObj; |
| 440 | 440 | } |
| 441 | 441 | |
@@ -446,23 +446,23 @@ discard block |
||
| 446 | 446 | * @throws Exception |
| 447 | 447 | * @return bool |
| 448 | 448 | */ |
| 449 | - public function getFunctionEval(string $funcName,$params) : bool |
|
| 449 | + public function getFunctionEval(string $funcName, $params) : bool |
|
| 450 | 450 | { |
| 451 | - if (! isset($this->functionList[$funcName]) ) |
|
| 451 | + if (!isset($this->functionList[$funcName])) |
|
| 452 | 452 | { |
| 453 | - throw new Exception($funcName . ' not found.'); |
|
| 453 | + throw new Exception($funcName.' not found.'); |
|
| 454 | 454 | } |
| 455 | - $pluginName = $this->functionList[$funcName]['plugin']; // plugin name |
|
| 456 | - $plugin = $this->pluginsList[$pluginName]['object']; // plugin object |
|
| 455 | + $pluginName=$this->functionList[$funcName]['plugin']; // plugin name |
|
| 456 | + $plugin=$this->pluginsList[$pluginName]['object']; // plugin object |
|
| 457 | 457 | |
| 458 | 458 | if ($plugin === null) |
| 459 | 459 | { |
| 460 | 460 | $this->registerPlugin($pluginName); // can throw exception handled by caller |
| 461 | - $plugin = $this->pluginsList[$pluginName]['object']; |
|
| 461 | + $plugin=$this->pluginsList[$pluginName]['object']; |
|
| 462 | 462 | } |
| 463 | 463 | |
| 464 | - $propertyName = $this->functionList[$funcName]['function']; |
|
| 465 | - $this->logClass->log('Using property '. $propertyName . ' of class : '.$pluginName,DEBUG); |
|
| 464 | + $propertyName=$this->functionList[$funcName]['function']; |
|
| 465 | + $this->logClass->log('Using property '.$propertyName.' of class : '.$pluginName, DEBUG); |
|
| 466 | 466 | |
| 467 | 467 | return $plugin->{$propertyName}($params); |
| 468 | 468 | } |
@@ -475,17 +475,17 @@ discard block |
||
| 475 | 475 | //$this->logClass->log('eval cleanup : '.$functionString,DEBUG); |
| 476 | 476 | |
| 477 | 477 | // Match function call |
| 478 | - $num=preg_match('/^__([a-zA-Z0-9]+)\((.+)\)$/', $functionString , $matches); |
|
| 479 | - if ($num !=1) |
|
| 478 | + $num=preg_match('/^__([a-zA-Z0-9]+)\((.+)\)$/', $functionString, $matches); |
|
| 479 | + if ($num != 1) |
|
| 480 | 480 | { |
| 481 | - throw new \ErrorException('Function syntax error : ' . $functionString ); |
|
| 481 | + throw new \ErrorException('Function syntax error : '.$functionString); |
|
| 482 | 482 | } |
| 483 | - $this->logClass->log('Got function : '. $matches[1] . ', params : '.$matches[2],DEBUG); |
|
| 483 | + $this->logClass->log('Got function : '.$matches[1].', params : '.$matches[2], DEBUG); |
|
| 484 | 484 | $funcName=$matches[1]; |
| 485 | 485 | |
| 486 | 486 | // Get parameters comma separated |
| 487 | - $funcParams=str_getcsv($matches[2],',','"',"\\"); |
|
| 488 | - $this->logClass->log('Function params : ' . print_r($funcParams,true),DEBUG); |
|
| 487 | + $funcParams=str_getcsv($matches[2], ',', '"', "\\"); |
|
| 488 | + $this->logClass->log('Function params : '.print_r($funcParams, true), DEBUG); |
|
| 489 | 489 | |
| 490 | 490 | // return evaluation |
| 491 | 491 | return $this->getFunctionEval($funcName, $funcParams); |
@@ -520,7 +520,7 @@ discard block |
||
| 520 | 520 | */ |
| 521 | 521 | public function setLoggingClass($loggingClass) |
| 522 | 522 | { |
| 523 | - $this->loggingClass = $loggingClass; |
|
| 523 | + $this->loggingClass=$loggingClass; |
|
| 524 | 524 | } |
| 525 | 525 | |
| 526 | 526 | /** |
@@ -528,8 +528,8 @@ discard block |
||
| 528 | 528 | * @param string $message |
| 529 | 529 | * @param int $level DEBUG/INFO/WARN/CRIT |
| 530 | 530 | */ |
| 531 | - public function log($message,$level) |
|
| 531 | + public function log($message, $level) |
|
| 532 | 532 | { |
| 533 | - $this->loggingClass->log('[ '.get_class($this).'] '. $message, $level); |
|
| 533 | + $this->loggingClass->log('[ '.get_class($this).'] '.$message, $level); |
|
| 534 | 534 | } |
| 535 | 535 | } |
| 536 | 536 | \ No newline at end of file |
@@ -17,519 +17,519 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Plugins |
| 19 | 19 | { |
| 20 | - /** Array of plugin objects. Keys ar plugin name |
|
| 21 | - * @var PluginTemplate[] $pluginsList Plugins array with name as index |
|
| 22 | - * $pluginsList[plugin name]['object'] : plugin object (NULL of not loaded) |
|
| 23 | - * $pluginsList[plugin name]['allOID'] : bool true if plugin catches all oid |
|
| 24 | - * $pluginsList[plugin name]['target'] : bool true if plugin can be trap processing target |
|
| 25 | - * $pluginsList[plugin name]['enabled'] : bool true if plugin is in enabled list |
|
| 26 | - **/ |
|
| 27 | - protected $pluginsList = array(); |
|
| 20 | + /** Array of plugin objects. Keys ar plugin name |
|
| 21 | + * @var PluginTemplate[] $pluginsList Plugins array with name as index |
|
| 22 | + * $pluginsList[plugin name]['object'] : plugin object (NULL of not loaded) |
|
| 23 | + * $pluginsList[plugin name]['allOID'] : bool true if plugin catches all oid |
|
| 24 | + * $pluginsList[plugin name]['target'] : bool true if plugin can be trap processing target |
|
| 25 | + * $pluginsList[plugin name]['enabled'] : bool true if plugin is in enabled list |
|
| 26 | + **/ |
|
| 27 | + protected $pluginsList = array(); |
|
| 28 | 28 | |
| 29 | - /** Array of functions names |
|
| 30 | - * @var array $functionList |
|
| 31 | - * $functionList[name]['plugin'] : Plugin name |
|
| 32 | - * $functionList[name]['function'] : Plugin function to call (null if plugin not loaded) |
|
| 33 | - */ |
|
| 34 | - protected $functionList=array(); |
|
| 29 | + /** Array of functions names |
|
| 30 | + * @var array $functionList |
|
| 31 | + * $functionList[name]['plugin'] : Plugin name |
|
| 32 | + * $functionList[name]['function'] : Plugin function to call (null if plugin not loaded) |
|
| 33 | + */ |
|
| 34 | + protected $functionList=array(); |
|
| 35 | 35 | |
| 36 | - /** @var string[] $enabledPlugins list of enabled plugins */ |
|
| 37 | - //public $enabledPlugins = array(); |
|
| 36 | + /** @var string[] $enabledPlugins list of enabled plugins */ |
|
| 37 | + //public $enabledPlugins = array(); |
|
| 38 | 38 | |
| 39 | 39 | |
| 40 | - /** @var Logging $logClass */ |
|
| 41 | - protected $logClass; |
|
| 40 | + /** @var Logging $logClass */ |
|
| 41 | + protected $logClass; |
|
| 42 | 42 | |
| 43 | - /** @var Trap $trapClass */ |
|
| 44 | - protected $trapClass; |
|
| 43 | + /** @var Trap $trapClass */ |
|
| 44 | + protected $trapClass; |
|
| 45 | 45 | |
| 46 | - /** @var string $pluginDir */ |
|
| 47 | - protected $pluginDir; |
|
| 46 | + /** @var string $pluginDir */ |
|
| 47 | + protected $pluginDir; |
|
| 48 | 48 | |
| 49 | - /** Setup class |
|
| 50 | - * @param Trap $logClass the top trap class |
|
| 51 | - * @param string $plugin_dir optional plugin directory |
|
| 52 | - * @throws \Exception |
|
| 53 | - */ |
|
| 54 | - function __construct(Trap $trapClass,string $pluginDir='') |
|
| 55 | - { |
|
| 56 | - if ($pluginDir == '') |
|
| 57 | - { |
|
| 58 | - $this->pluginDir=dirname(__DIR__).'/Plugins'; |
|
| 59 | - } |
|
| 60 | - else |
|
| 61 | - { |
|
| 62 | - $this->pluginDir=$pluginDir; |
|
| 63 | - } |
|
| 64 | - // Set and check Logging class |
|
| 65 | - $this->trapClass=$trapClass; |
|
| 66 | - if ($this->trapClass === null) |
|
| 67 | - { |
|
| 68 | - throw new Exception('Log class not loaded into trap class'); |
|
| 69 | - } |
|
| 70 | - $this->logClass=$trapClass->logging; |
|
| 71 | - if ($this->logClass === null) |
|
| 72 | - { |
|
| 73 | - throw new Exception('Log class not loaded into trap class'); |
|
| 74 | - } |
|
| 75 | - // check DB class and get plugins list. |
|
| 76 | - if ($this->trapClass->trapsDB === null) |
|
| 77 | - { |
|
| 78 | - throw new Exception('Database class not loaded into trap class'); |
|
| 79 | - } |
|
| 80 | - $this->loadEnabledPlugins(); |
|
| 81 | - } |
|
| 49 | + /** Setup class |
|
| 50 | + * @param Trap $logClass the top trap class |
|
| 51 | + * @param string $plugin_dir optional plugin directory |
|
| 52 | + * @throws \Exception |
|
| 53 | + */ |
|
| 54 | + function __construct(Trap $trapClass,string $pluginDir='') |
|
| 55 | + { |
|
| 56 | + if ($pluginDir == '') |
|
| 57 | + { |
|
| 58 | + $this->pluginDir=dirname(__DIR__).'/Plugins'; |
|
| 59 | + } |
|
| 60 | + else |
|
| 61 | + { |
|
| 62 | + $this->pluginDir=$pluginDir; |
|
| 63 | + } |
|
| 64 | + // Set and check Logging class |
|
| 65 | + $this->trapClass=$trapClass; |
|
| 66 | + if ($this->trapClass === null) |
|
| 67 | + { |
|
| 68 | + throw new Exception('Log class not loaded into trap class'); |
|
| 69 | + } |
|
| 70 | + $this->logClass=$trapClass->logging; |
|
| 71 | + if ($this->logClass === null) |
|
| 72 | + { |
|
| 73 | + throw new Exception('Log class not loaded into trap class'); |
|
| 74 | + } |
|
| 75 | + // check DB class and get plugins list. |
|
| 76 | + if ($this->trapClass->trapsDB === null) |
|
| 77 | + { |
|
| 78 | + throw new Exception('Database class not loaded into trap class'); |
|
| 79 | + } |
|
| 80 | + $this->loadEnabledPlugins(); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Load enabled plugins from database config table. |
|
| 86 | - * Fills enabledPlugins and functionList properties |
|
| 87 | - * @throws \Exception |
|
| 88 | - */ |
|
| 89 | - private function loadEnabledPlugins() |
|
| 90 | - { |
|
| 91 | - $PluginList = $this->trapClass->trapsDB->getDBConfig('enabled_plugins'); |
|
| 84 | + /** |
|
| 85 | + * Load enabled plugins from database config table. |
|
| 86 | + * Fills enabledPlugins and functionList properties |
|
| 87 | + * @throws \Exception |
|
| 88 | + */ |
|
| 89 | + private function loadEnabledPlugins() |
|
| 90 | + { |
|
| 91 | + $PluginList = $this->trapClass->trapsDB->getDBConfig('enabled_plugins'); |
|
| 92 | 92 | |
| 93 | - if ($PluginList === null || $PluginList == '') |
|
| 94 | - { |
|
| 95 | - $this->logClass->log('No enabled plugins',DEBUG); |
|
| 96 | - return; |
|
| 97 | - } |
|
| 98 | - else |
|
| 99 | - { // Saved config : <plugin name>;<Catch all OID ? 1|0>;<Trap target ? 1|0>;<func 1 name>|<func 2 name>... ,<plugin2 name>.... |
|
| 100 | - $this->logClass->log('Enabled plugins = '.$PluginList,DEBUG); |
|
| 93 | + if ($PluginList === null || $PluginList == '') |
|
| 94 | + { |
|
| 95 | + $this->logClass->log('No enabled plugins',DEBUG); |
|
| 96 | + return; |
|
| 97 | + } |
|
| 98 | + else |
|
| 99 | + { // Saved config : <plugin name>;<Catch all OID ? 1|0>;<Trap target ? 1|0>;<func 1 name>|<func 2 name>... ,<plugin2 name>.... |
|
| 100 | + $this->logClass->log('Enabled plugins = '.$PluginList,DEBUG); |
|
| 101 | 101 | |
| 102 | - $pluginArray = explode(',', $PluginList); |
|
| 103 | - foreach ($pluginArray as $pluginElmt) |
|
| 104 | - { |
|
| 105 | - $pluginElmt = explode(';',$pluginElmt); |
|
| 106 | - if ($pluginElmt === false || count($pluginElmt) != 4) |
|
| 107 | - { |
|
| 108 | - throw new \Exception('Invalid plugin configuration : '. $PluginList ); |
|
| 109 | - } |
|
| 110 | - $pluginName=$pluginElmt[0]; |
|
| 102 | + $pluginArray = explode(',', $PluginList); |
|
| 103 | + foreach ($pluginArray as $pluginElmt) |
|
| 104 | + { |
|
| 105 | + $pluginElmt = explode(';',$pluginElmt); |
|
| 106 | + if ($pluginElmt === false || count($pluginElmt) != 4) |
|
| 107 | + { |
|
| 108 | + throw new \Exception('Invalid plugin configuration : '. $PluginList ); |
|
| 109 | + } |
|
| 110 | + $pluginName=$pluginElmt[0]; |
|
| 111 | 111 | |
| 112 | - $pluginListElmt = array(); |
|
| 113 | - $pluginListElmt['object'] = null; // class not loaded |
|
| 114 | - $pluginListElmt['allOID'] = ($pluginElmt[1]=='1') ? true : false; |
|
| 115 | - $pluginListElmt['target'] = ($pluginElmt[2]=='1') ? true : false; |
|
| 116 | - $pluginListElmt['enabled'] = true; |
|
| 112 | + $pluginListElmt = array(); |
|
| 113 | + $pluginListElmt['object'] = null; // class not loaded |
|
| 114 | + $pluginListElmt['allOID'] = ($pluginElmt[1]=='1') ? true : false; |
|
| 115 | + $pluginListElmt['target'] = ($pluginElmt[2]=='1') ? true : false; |
|
| 116 | + $pluginListElmt['enabled'] = true; |
|
| 117 | 117 | |
| 118 | - $this->pluginsList[$pluginName] = $pluginListElmt; |
|
| 118 | + $this->pluginsList[$pluginName] = $pluginListElmt; |
|
| 119 | 119 | |
| 120 | - // deal with plugin functions |
|
| 121 | - $pluginFunctions = explode('|',$pluginElmt[3]); |
|
| 122 | - if ($pluginFunctions !== false) |
|
| 123 | - { |
|
| 124 | - foreach ($pluginFunctions as $function) |
|
| 125 | - { |
|
| 126 | - $this->functionList[$function] = array( |
|
| 127 | - 'plugin' => $pluginName, |
|
| 128 | - 'function' => null |
|
| 129 | - ); |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - } |
|
| 120 | + // deal with plugin functions |
|
| 121 | + $pluginFunctions = explode('|',$pluginElmt[3]); |
|
| 122 | + if ($pluginFunctions !== false) |
|
| 123 | + { |
|
| 124 | + foreach ($pluginFunctions as $function) |
|
| 125 | + { |
|
| 126 | + $this->functionList[$function] = array( |
|
| 127 | + 'plugin' => $pluginName, |
|
| 128 | + 'function' => null |
|
| 129 | + ); |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - } |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - } |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - /** |
|
| 139 | - * Save enabled plugin array in DB config |
|
| 140 | - * @return bool true if OK, or false (error logged by DB Class) |
|
| 141 | - */ |
|
| 142 | - private function saveEnabledPlugins() |
|
| 143 | - { |
|
| 144 | - $saveString=''; |
|
| 145 | - foreach ($this->pluginsList as $name => $value) |
|
| 146 | - { |
|
| 147 | - if ($value['enabled'] == false) |
|
| 148 | - { |
|
| 149 | - continue; |
|
| 150 | - } |
|
| 151 | - $functionString=''; |
|
| 152 | - foreach ($this->functionList as $fName => $fvalue) |
|
| 153 | - { |
|
| 154 | - if ($fvalue['plugin'] != $name) |
|
| 155 | - { |
|
| 156 | - continue; |
|
| 157 | - } |
|
| 158 | - $functionString .= ($functionString == '') ? '' : '|'; // add separator if not empty |
|
| 159 | - $functionString .= $fName; |
|
| 160 | - } |
|
| 161 | - $saveString .= ($saveString == '')?'':',' ; |
|
| 138 | + /** |
|
| 139 | + * Save enabled plugin array in DB config |
|
| 140 | + * @return bool true if OK, or false (error logged by DB Class) |
|
| 141 | + */ |
|
| 142 | + private function saveEnabledPlugins() |
|
| 143 | + { |
|
| 144 | + $saveString=''; |
|
| 145 | + foreach ($this->pluginsList as $name => $value) |
|
| 146 | + { |
|
| 147 | + if ($value['enabled'] == false) |
|
| 148 | + { |
|
| 149 | + continue; |
|
| 150 | + } |
|
| 151 | + $functionString=''; |
|
| 152 | + foreach ($this->functionList as $fName => $fvalue) |
|
| 153 | + { |
|
| 154 | + if ($fvalue['plugin'] != $name) |
|
| 155 | + { |
|
| 156 | + continue; |
|
| 157 | + } |
|
| 158 | + $functionString .= ($functionString == '') ? '' : '|'; // add separator if not empty |
|
| 159 | + $functionString .= $fName; |
|
| 160 | + } |
|
| 161 | + $saveString .= ($saveString == '')?'':',' ; |
|
| 162 | 162 | |
| 163 | - $allOID = ($value['allOID'] === true) ? 1 : 0; |
|
| 164 | - $target = ($value['target'] === true) ? 1 : 0; |
|
| 165 | - $saveString .= $name . ';' . $allOID . ';' . $target . ';' . $functionString ; |
|
| 166 | - } |
|
| 167 | - $this->logClass->log('Saving : ' . $saveString,DEBUG); |
|
| 168 | - return $this->trapClass->trapsDB->setDBConfig('enabled_plugins', $saveString); |
|
| 169 | - } |
|
| 163 | + $allOID = ($value['allOID'] === true) ? 1 : 0; |
|
| 164 | + $target = ($value['target'] === true) ? 1 : 0; |
|
| 165 | + $saveString .= $name . ';' . $allOID . ';' . $target . ';' . $functionString ; |
|
| 166 | + } |
|
| 167 | + $this->logClass->log('Saving : ' . $saveString,DEBUG); |
|
| 168 | + return $this->trapClass->trapsDB->setDBConfig('enabled_plugins', $saveString); |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - /** Get enabled plugin list by name |
|
| 172 | - * @return array |
|
| 173 | - */ |
|
| 174 | - public function getEnabledPlugins() : array |
|
| 175 | - { |
|
| 176 | - $retArray=array(); |
|
| 177 | - foreach ($this->pluginsList as $name => $value) |
|
| 178 | - { |
|
| 179 | - if ($value['enabled'] == true) |
|
| 180 | - { |
|
| 181 | - array_push($retArray,$name); |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - return $retArray; |
|
| 185 | - } |
|
| 171 | + /** Get enabled plugin list by name |
|
| 172 | + * @return array |
|
| 173 | + */ |
|
| 174 | + public function getEnabledPlugins() : array |
|
| 175 | + { |
|
| 176 | + $retArray=array(); |
|
| 177 | + foreach ($this->pluginsList as $name => $value) |
|
| 178 | + { |
|
| 179 | + if ($value['enabled'] == true) |
|
| 180 | + { |
|
| 181 | + array_push($retArray,$name); |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + return $retArray; |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - /** Enable plugin (enabling an enabled plugin is OK, same for disabled). |
|
| 188 | - * and save in DB config |
|
| 189 | - * @param string $pluginName |
|
| 190 | - * @param bool $enabled true to enable, false to disable |
|
| 191 | - * @return bool true if OK, or false (error logged) |
|
| 192 | - */ |
|
| 193 | - public function enablePlugin(string $pluginName,bool $enabled) |
|
| 194 | - { |
|
| 195 | - if ($enabled === false) |
|
| 196 | - { |
|
| 197 | - // If plugin is defined set to disable |
|
| 198 | - if ( isset($this->pluginsList[$pluginName])) |
|
| 199 | - { |
|
| 200 | - $this->pluginsList[$pluginName]['enabled'] = false; |
|
| 201 | - } |
|
| 202 | - return $this->saveEnabledPlugins(); |
|
| 203 | - } |
|
| 204 | - // Check if plugin is loaded / exists |
|
| 205 | - if ( ! isset($this->pluginsList[$pluginName]) || |
|
| 206 | - $this->pluginsList[$pluginName]['object'] === null) |
|
| 207 | - { |
|
| 208 | - try { |
|
| 209 | - $this->registerPlugin($pluginName); |
|
| 210 | - } catch (Exception $e) { |
|
| 211 | - $this->logClass->log('Cannot enable plugin : ' . $e->getMessage(),WARN); |
|
| 212 | - return false; |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - $this->pluginsList[$pluginName]['enabled'] = true; |
|
| 216 | - // save in DB and return |
|
| 217 | - return $this->saveEnabledPlugins(); |
|
| 218 | - } |
|
| 187 | + /** Enable plugin (enabling an enabled plugin is OK, same for disabled). |
|
| 188 | + * and save in DB config |
|
| 189 | + * @param string $pluginName |
|
| 190 | + * @param bool $enabled true to enable, false to disable |
|
| 191 | + * @return bool true if OK, or false (error logged) |
|
| 192 | + */ |
|
| 193 | + public function enablePlugin(string $pluginName,bool $enabled) |
|
| 194 | + { |
|
| 195 | + if ($enabled === false) |
|
| 196 | + { |
|
| 197 | + // If plugin is defined set to disable |
|
| 198 | + if ( isset($this->pluginsList[$pluginName])) |
|
| 199 | + { |
|
| 200 | + $this->pluginsList[$pluginName]['enabled'] = false; |
|
| 201 | + } |
|
| 202 | + return $this->saveEnabledPlugins(); |
|
| 203 | + } |
|
| 204 | + // Check if plugin is loaded / exists |
|
| 205 | + if ( ! isset($this->pluginsList[$pluginName]) || |
|
| 206 | + $this->pluginsList[$pluginName]['object'] === null) |
|
| 207 | + { |
|
| 208 | + try { |
|
| 209 | + $this->registerPlugin($pluginName); |
|
| 210 | + } catch (Exception $e) { |
|
| 211 | + $this->logClass->log('Cannot enable plugin : ' . $e->getMessage(),WARN); |
|
| 212 | + return false; |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + $this->pluginsList[$pluginName]['enabled'] = true; |
|
| 216 | + // save in DB and return |
|
| 217 | + return $this->saveEnabledPlugins(); |
|
| 218 | + } |
|
| 219 | 219 | |
| 220 | - /** |
|
| 221 | - * Destroy plugin objects and reload them with new enabled list. |
|
| 222 | - * TODO : Code this function (ref DAEMON_MODE) |
|
| 223 | - */ |
|
| 224 | - public function reloadAllPlugins() |
|
| 225 | - { |
|
| 226 | - return; |
|
| 227 | - } |
|
| 220 | + /** |
|
| 221 | + * Destroy plugin objects and reload them with new enabled list. |
|
| 222 | + * TODO : Code this function (ref DAEMON_MODE) |
|
| 223 | + */ |
|
| 224 | + public function reloadAllPlugins() |
|
| 225 | + { |
|
| 226 | + return; |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | - /** Load plugin by name. Create entry if not in $pluginsList |
|
| 230 | - * @param string $pluginName Plugin name to load |
|
| 231 | - * @return bool true if created, false if already loaded |
|
| 232 | - * @throws Exception on error loading plugin |
|
| 233 | - */ |
|
| 234 | - public function registerPlugin(string $pluginName) |
|
| 235 | - { |
|
| 236 | - if ( ! isset($this->pluginsList[$pluginName]) ) // Plugin isn't enable, create entry |
|
| 237 | - { |
|
| 238 | - $pluginListElmt = array(); |
|
| 239 | - $pluginListElmt['object'] = null; // class not loaded |
|
| 240 | - $pluginListElmt['enabled'] = false; |
|
| 241 | - $this->pluginsList[$pluginName] = $pluginListElmt; |
|
| 242 | - } |
|
| 229 | + /** Load plugin by name. Create entry if not in $pluginsList |
|
| 230 | + * @param string $pluginName Plugin name to load |
|
| 231 | + * @return bool true if created, false if already loaded |
|
| 232 | + * @throws Exception on error loading plugin |
|
| 233 | + */ |
|
| 234 | + public function registerPlugin(string $pluginName) |
|
| 235 | + { |
|
| 236 | + if ( ! isset($this->pluginsList[$pluginName]) ) // Plugin isn't enable, create entry |
|
| 237 | + { |
|
| 238 | + $pluginListElmt = array(); |
|
| 239 | + $pluginListElmt['object'] = null; // class not loaded |
|
| 240 | + $pluginListElmt['enabled'] = false; |
|
| 241 | + $this->pluginsList[$pluginName] = $pluginListElmt; |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - if ($this->pluginsList[$pluginName]['object'] !== null) |
|
| 245 | - { |
|
| 246 | - return false; |
|
| 247 | - } |
|
| 248 | - try { |
|
| 249 | - // Include plugin file |
|
| 250 | - include_once($this->pluginDir.'/' . $pluginName . '.php'); |
|
| 244 | + if ($this->pluginsList[$pluginName]['object'] !== null) |
|
| 245 | + { |
|
| 246 | + return false; |
|
| 247 | + } |
|
| 248 | + try { |
|
| 249 | + // Include plugin file |
|
| 250 | + include_once($this->pluginDir.'/' . $pluginName . '.php'); |
|
| 251 | 251 | |
| 252 | - // Create full class name with namespace |
|
| 253 | - $pluginClassName = __NAMESPACE__ . '\\Plugins\\' . $pluginName; |
|
| 252 | + // Create full class name with namespace |
|
| 253 | + $pluginClassName = __NAMESPACE__ . '\\Plugins\\' . $pluginName; |
|
| 254 | 254 | |
| 255 | - // Create class |
|
| 256 | - $newClass = new $pluginClassName(); |
|
| 255 | + // Create class |
|
| 256 | + $newClass = new $pluginClassName(); |
|
| 257 | 257 | |
| 258 | - // Set logging |
|
| 259 | - $newClass->setLoggingClass($this->logClass); |
|
| 258 | + // Set logging |
|
| 259 | + $newClass->setLoggingClass($this->logClass); |
|
| 260 | 260 | |
| 261 | - // Add in plugin array |
|
| 262 | - $this->pluginsList[$pluginName]['object']=$newClass; |
|
| 263 | - $this->pluginsList[$pluginName]['allOID']=$newClass->catchAllTraps; |
|
| 264 | - $this->pluginsList[$pluginName]['target']=$newClass->processTraps; |
|
| 261 | + // Add in plugin array |
|
| 262 | + $this->pluginsList[$pluginName]['object']=$newClass; |
|
| 263 | + $this->pluginsList[$pluginName]['allOID']=$newClass->catchAllTraps; |
|
| 264 | + $this->pluginsList[$pluginName]['target']=$newClass->processTraps; |
|
| 265 | 265 | |
| 266 | - // Delete old functions |
|
| 267 | - foreach ($this->functionList as $fname => $fvalue) |
|
| 268 | - { |
|
| 269 | - if ($fvalue['plugin'] == $pluginName) |
|
| 270 | - { |
|
| 271 | - unset($this->functionList[$fname]); |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - // Add functions |
|
| 275 | - foreach ($newClass->functions as $fname => $function) |
|
| 276 | - { |
|
| 277 | - if (isset($this->functionList[$fname])) |
|
| 278 | - { |
|
| 279 | - if ($this->functionList[$fname]['plugin'] != $pluginName ) |
|
| 280 | - { |
|
| 281 | - throw new Exception('Duplicate function name '.$fname . ' in ' |
|
| 282 | - . $pluginName . ' and ' . $this->functionList[$fname]['plugin']); |
|
| 283 | - } |
|
| 266 | + // Delete old functions |
|
| 267 | + foreach ($this->functionList as $fname => $fvalue) |
|
| 268 | + { |
|
| 269 | + if ($fvalue['plugin'] == $pluginName) |
|
| 270 | + { |
|
| 271 | + unset($this->functionList[$fname]); |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + // Add functions |
|
| 275 | + foreach ($newClass->functions as $fname => $function) |
|
| 276 | + { |
|
| 277 | + if (isset($this->functionList[$fname])) |
|
| 278 | + { |
|
| 279 | + if ($this->functionList[$fname]['plugin'] != $pluginName ) |
|
| 280 | + { |
|
| 281 | + throw new Exception('Duplicate function name '.$fname . ' in ' |
|
| 282 | + . $pluginName . ' and ' . $this->functionList[$fname]['plugin']); |
|
| 283 | + } |
|
| 284 | 284 | |
| 285 | - } |
|
| 286 | - else |
|
| 287 | - { |
|
| 288 | - $this->functionList[$fname]=array(); |
|
| 289 | - $this->functionList[$fname]['plugin'] = $pluginName; |
|
| 290 | - } |
|
| 291 | - $this->functionList[$fname]['function']=$function['function']; |
|
| 292 | - } |
|
| 293 | - $this->logClass->log('Registered plugin '.$pluginName,DEBUG); |
|
| 285 | + } |
|
| 286 | + else |
|
| 287 | + { |
|
| 288 | + $this->functionList[$fname]=array(); |
|
| 289 | + $this->functionList[$fname]['plugin'] = $pluginName; |
|
| 290 | + } |
|
| 291 | + $this->functionList[$fname]['function']=$function['function']; |
|
| 292 | + } |
|
| 293 | + $this->logClass->log('Registered plugin '.$pluginName,DEBUG); |
|
| 294 | 294 | |
| 295 | - } catch (Exception $e) { |
|
| 296 | - unset($this->pluginsList[$pluginName]); |
|
| 297 | - $errorMessage = "Error registering plugin $pluginName : ".$e->getMessage(); |
|
| 298 | - $this->logClass->log($errorMessage,WARN); |
|
| 299 | - // Disable the plugin |
|
| 300 | - $this->enablePlugin($pluginName, false); |
|
| 301 | - throw new \Exception($errorMessage); |
|
| 302 | - } catch (Throwable $t) { |
|
| 303 | - unset($this->pluginsList[$pluginName]); |
|
| 304 | - $errorMessage = $t->getMessage() . ' in file ' . $t->getFile() . ' line ' . $t->getLine(); |
|
| 305 | - $this->logClass->log($errorMessage,WARN); |
|
| 306 | - // Disable the plugin |
|
| 307 | - $this->enablePlugin($pluginName, false); |
|
| 308 | - throw new \Exception($errorMessage); |
|
| 309 | - } |
|
| 310 | - return true; |
|
| 311 | - } |
|
| 295 | + } catch (Exception $e) { |
|
| 296 | + unset($this->pluginsList[$pluginName]); |
|
| 297 | + $errorMessage = "Error registering plugin $pluginName : ".$e->getMessage(); |
|
| 298 | + $this->logClass->log($errorMessage,WARN); |
|
| 299 | + // Disable the plugin |
|
| 300 | + $this->enablePlugin($pluginName, false); |
|
| 301 | + throw new \Exception($errorMessage); |
|
| 302 | + } catch (Throwable $t) { |
|
| 303 | + unset($this->pluginsList[$pluginName]); |
|
| 304 | + $errorMessage = $t->getMessage() . ' in file ' . $t->getFile() . ' line ' . $t->getLine(); |
|
| 305 | + $this->logClass->log($errorMessage,WARN); |
|
| 306 | + // Disable the plugin |
|
| 307 | + $this->enablePlugin($pluginName, false); |
|
| 308 | + throw new \Exception($errorMessage); |
|
| 309 | + } |
|
| 310 | + return true; |
|
| 311 | + } |
|
| 312 | 312 | |
| 313 | - /** Registers all plugins (check=false) or only those with name present in array (check=true) |
|
| 314 | - * @param bool $checkEnabled Check if plugin is enabled before loading it |
|
| 315 | - * @return string Errors encountered while registering plugins |
|
| 316 | - */ |
|
| 317 | - public function registerAllPlugins(bool $checkEnabled=true) |
|
| 318 | - { |
|
| 319 | - $retDisplay=''; |
|
| 320 | - // First load enabled plugins |
|
| 321 | - foreach (array_keys($this->pluginsList) as $pluginName) |
|
| 322 | - { |
|
| 323 | - try { |
|
| 324 | - $this->registerPlugin($pluginName); |
|
| 325 | - } catch (Exception $e) { |
|
| 326 | - $retDisplay .= $e->getMessage() . ' / '; |
|
| 327 | - } |
|
| 328 | - } |
|
| 329 | - if ($checkEnabled === false) // Load all php files in plugin dir |
|
| 330 | - { |
|
| 331 | - foreach (glob($this->pluginDir."/*.php") as $filename) |
|
| 332 | - { |
|
| 333 | - $pluginName=basename($filename,'.php'); |
|
| 334 | - if (!preg_match('/^[a-zA-Z0-9]+$/',$pluginName)) |
|
| 335 | - { |
|
| 336 | - $this->logClass->log("Invalid plugin name : ".$pluginName, WARN); |
|
| 337 | - $retDisplay .= "Invalid plugin name : ".$pluginName . " / "; |
|
| 338 | - break; |
|
| 339 | - } |
|
| 340 | - try { // Already registerd plugin will simply return false |
|
| 341 | - $this->registerPlugin($pluginName); |
|
| 342 | - } catch (Exception $e) { |
|
| 343 | - $retDisplay .= $e->getMessage() . ' / '; |
|
| 344 | - } |
|
| 345 | - } |
|
| 346 | - } |
|
| 313 | + /** Registers all plugins (check=false) or only those with name present in array (check=true) |
|
| 314 | + * @param bool $checkEnabled Check if plugin is enabled before loading it |
|
| 315 | + * @return string Errors encountered while registering plugins |
|
| 316 | + */ |
|
| 317 | + public function registerAllPlugins(bool $checkEnabled=true) |
|
| 318 | + { |
|
| 319 | + $retDisplay=''; |
|
| 320 | + // First load enabled plugins |
|
| 321 | + foreach (array_keys($this->pluginsList) as $pluginName) |
|
| 322 | + { |
|
| 323 | + try { |
|
| 324 | + $this->registerPlugin($pluginName); |
|
| 325 | + } catch (Exception $e) { |
|
| 326 | + $retDisplay .= $e->getMessage() . ' / '; |
|
| 327 | + } |
|
| 328 | + } |
|
| 329 | + if ($checkEnabled === false) // Load all php files in plugin dir |
|
| 330 | + { |
|
| 331 | + foreach (glob($this->pluginDir."/*.php") as $filename) |
|
| 332 | + { |
|
| 333 | + $pluginName=basename($filename,'.php'); |
|
| 334 | + if (!preg_match('/^[a-zA-Z0-9]+$/',$pluginName)) |
|
| 335 | + { |
|
| 336 | + $this->logClass->log("Invalid plugin name : ".$pluginName, WARN); |
|
| 337 | + $retDisplay .= "Invalid plugin name : ".$pluginName . " / "; |
|
| 338 | + break; |
|
| 339 | + } |
|
| 340 | + try { // Already registerd plugin will simply return false |
|
| 341 | + $this->registerPlugin($pluginName); |
|
| 342 | + } catch (Exception $e) { |
|
| 343 | + $retDisplay .= $e->getMessage() . ' / '; |
|
| 344 | + } |
|
| 345 | + } |
|
| 346 | + } |
|
| 347 | 347 | |
| 348 | - if ($retDisplay == '') |
|
| 349 | - { |
|
| 350 | - return 'All plugins loaded OK'; |
|
| 351 | - } |
|
| 352 | - else |
|
| 353 | - { |
|
| 354 | - return $retDisplay; |
|
| 355 | - } |
|
| 356 | - } |
|
| 348 | + if ($retDisplay == '') |
|
| 349 | + { |
|
| 350 | + return 'All plugins loaded OK'; |
|
| 351 | + } |
|
| 352 | + else |
|
| 353 | + { |
|
| 354 | + return $retDisplay; |
|
| 355 | + } |
|
| 356 | + } |
|
| 357 | 357 | |
| 358 | - /** |
|
| 359 | - * Returns array of name of loaded plugins |
|
| 360 | - * @return array |
|
| 361 | - */ |
|
| 362 | - public function pluginList() : array |
|
| 363 | - { |
|
| 364 | - return array_keys($this->pluginsList); |
|
| 365 | - } |
|
| 358 | + /** |
|
| 359 | + * Returns array of name of loaded plugins |
|
| 360 | + * @return array |
|
| 361 | + */ |
|
| 362 | + public function pluginList() : array |
|
| 363 | + { |
|
| 364 | + return array_keys($this->pluginsList); |
|
| 365 | + } |
|
| 366 | 366 | |
| 367 | - /** |
|
| 368 | - * Get plugin details |
|
| 369 | - * @param string $name name of plugins |
|
| 370 | - * @return boolean|stdClass result as stdClass or false if plugin not found. |
|
| 371 | - * @throws \Exception if registering is not possible |
|
| 372 | - */ |
|
| 373 | - public function pluginDetails(string $name) |
|
| 374 | - { |
|
| 375 | - if (!array_key_exists($name, $this->pluginsList)) |
|
| 376 | - { |
|
| 377 | - return false; |
|
| 378 | - } |
|
| 379 | - if ($this->pluginsList[$name]['object'] === null) |
|
| 380 | - { |
|
| 381 | - $this->registerPlugin($name); // can throw exception handled by caller |
|
| 382 | - } |
|
| 383 | - $retObj = new stdClass(); |
|
| 384 | - $retObj->name = $name; |
|
| 385 | - $retObj->catchAllTraps = $this->pluginsList[$name]['allOID']; |
|
| 386 | - $retObj->processTraps = $this->pluginsList[$name]['target']; |
|
| 387 | - $retObj->description = $this->pluginsList[$name]['object']->description; |
|
| 388 | - $functions=array(); |
|
| 389 | - foreach ($this->functionList as $fName => $func) |
|
| 390 | - { |
|
| 391 | - if ($func['plugin'] == $name) |
|
| 392 | - { |
|
| 393 | - array_push($functions,$fName); |
|
| 394 | - } |
|
| 395 | - } |
|
| 396 | - $retObj->funcArray=$functions; |
|
| 397 | - return $retObj; |
|
| 398 | - } |
|
| 367 | + /** |
|
| 368 | + * Get plugin details |
|
| 369 | + * @param string $name name of plugins |
|
| 370 | + * @return boolean|stdClass result as stdClass or false if plugin not found. |
|
| 371 | + * @throws \Exception if registering is not possible |
|
| 372 | + */ |
|
| 373 | + public function pluginDetails(string $name) |
|
| 374 | + { |
|
| 375 | + if (!array_key_exists($name, $this->pluginsList)) |
|
| 376 | + { |
|
| 377 | + return false; |
|
| 378 | + } |
|
| 379 | + if ($this->pluginsList[$name]['object'] === null) |
|
| 380 | + { |
|
| 381 | + $this->registerPlugin($name); // can throw exception handled by caller |
|
| 382 | + } |
|
| 383 | + $retObj = new stdClass(); |
|
| 384 | + $retObj->name = $name; |
|
| 385 | + $retObj->catchAllTraps = $this->pluginsList[$name]['allOID']; |
|
| 386 | + $retObj->processTraps = $this->pluginsList[$name]['target']; |
|
| 387 | + $retObj->description = $this->pluginsList[$name]['object']->description; |
|
| 388 | + $functions=array(); |
|
| 389 | + foreach ($this->functionList as $fName => $func) |
|
| 390 | + { |
|
| 391 | + if ($func['plugin'] == $name) |
|
| 392 | + { |
|
| 393 | + array_push($functions,$fName); |
|
| 394 | + } |
|
| 395 | + } |
|
| 396 | + $retObj->funcArray=$functions; |
|
| 397 | + return $retObj; |
|
| 398 | + } |
|
| 399 | 399 | |
| 400 | - /** |
|
| 401 | - * Get plugin name from function name |
|
| 402 | - * @param string $funcName |
|
| 403 | - * @param string $pluginName |
|
| 404 | - * @return boolean returns plugin object of false; |
|
| 405 | - */ |
|
| 406 | - public function getFunction($funcName,&$pluginName) |
|
| 407 | - { |
|
| 408 | - if (! isset($this->functionList[$funcName]) ) |
|
| 409 | - { |
|
| 410 | - return false; |
|
| 411 | - } |
|
| 412 | - $pluginName = $this->functionList[$funcName]['plugin']; |
|
| 413 | - return true; |
|
| 414 | - } |
|
| 400 | + /** |
|
| 401 | + * Get plugin name from function name |
|
| 402 | + * @param string $funcName |
|
| 403 | + * @param string $pluginName |
|
| 404 | + * @return boolean returns plugin object of false; |
|
| 405 | + */ |
|
| 406 | + public function getFunction($funcName,&$pluginName) |
|
| 407 | + { |
|
| 408 | + if (! isset($this->functionList[$funcName]) ) |
|
| 409 | + { |
|
| 410 | + return false; |
|
| 411 | + } |
|
| 412 | + $pluginName = $this->functionList[$funcName]['plugin']; |
|
| 413 | + return true; |
|
| 414 | + } |
|
| 415 | 415 | |
| 416 | - /** |
|
| 417 | - * Get functions params and description |
|
| 418 | - * @param string $funcName |
|
| 419 | - * @return boolean|stdClass false if not found or object (name,params,description) |
|
| 420 | - * @throws \Exception if registering is not possible |
|
| 421 | - */ |
|
| 422 | - public function getFunctionDetails($funcName) |
|
| 423 | - { |
|
| 424 | - if (! isset($this->functionList[$funcName]) ) |
|
| 425 | - { |
|
| 426 | - return false; |
|
| 427 | - } |
|
| 428 | - $pluginName = $this->functionList[$funcName]['plugin']; // plugin name |
|
| 429 | - $plugin = $this->pluginsList[$pluginName]['object']; // plugin object |
|
| 430 | - if ($plugin === null) |
|
| 431 | - { |
|
| 432 | - $this->registerPlugin($pluginName); // can throw exception handled by caller |
|
| 433 | - } |
|
| 434 | - $retObj = new stdClass(); |
|
| 435 | - $retObj->name = $funcName; |
|
| 436 | - $retObj->plugin = $pluginName; |
|
| 437 | - $retObj->params = $plugin->functions[$funcName]['params']; |
|
| 438 | - $retObj->description = $plugin->functions[$funcName]['description']; |
|
| 439 | - return $retObj; |
|
| 440 | - } |
|
| 416 | + /** |
|
| 417 | + * Get functions params and description |
|
| 418 | + * @param string $funcName |
|
| 419 | + * @return boolean|stdClass false if not found or object (name,params,description) |
|
| 420 | + * @throws \Exception if registering is not possible |
|
| 421 | + */ |
|
| 422 | + public function getFunctionDetails($funcName) |
|
| 423 | + { |
|
| 424 | + if (! isset($this->functionList[$funcName]) ) |
|
| 425 | + { |
|
| 426 | + return false; |
|
| 427 | + } |
|
| 428 | + $pluginName = $this->functionList[$funcName]['plugin']; // plugin name |
|
| 429 | + $plugin = $this->pluginsList[$pluginName]['object']; // plugin object |
|
| 430 | + if ($plugin === null) |
|
| 431 | + { |
|
| 432 | + $this->registerPlugin($pluginName); // can throw exception handled by caller |
|
| 433 | + } |
|
| 434 | + $retObj = new stdClass(); |
|
| 435 | + $retObj->name = $funcName; |
|
| 436 | + $retObj->plugin = $pluginName; |
|
| 437 | + $retObj->params = $plugin->functions[$funcName]['params']; |
|
| 438 | + $retObj->description = $plugin->functions[$funcName]['description']; |
|
| 439 | + return $retObj; |
|
| 440 | + } |
|
| 441 | 441 | |
| 442 | - /** |
|
| 443 | - * Evaluate function with parameters |
|
| 444 | - * @param string $funcName |
|
| 445 | - * @param mixed $params |
|
| 446 | - * @throws Exception |
|
| 447 | - * @return bool |
|
| 448 | - */ |
|
| 449 | - public function getFunctionEval(string $funcName,$params) : bool |
|
| 450 | - { |
|
| 451 | - if (! isset($this->functionList[$funcName]) ) |
|
| 452 | - { |
|
| 453 | - throw new Exception($funcName . ' not found.'); |
|
| 454 | - } |
|
| 455 | - $pluginName = $this->functionList[$funcName]['plugin']; // plugin name |
|
| 456 | - $plugin = $this->pluginsList[$pluginName]['object']; // plugin object |
|
| 442 | + /** |
|
| 443 | + * Evaluate function with parameters |
|
| 444 | + * @param string $funcName |
|
| 445 | + * @param mixed $params |
|
| 446 | + * @throws Exception |
|
| 447 | + * @return bool |
|
| 448 | + */ |
|
| 449 | + public function getFunctionEval(string $funcName,$params) : bool |
|
| 450 | + { |
|
| 451 | + if (! isset($this->functionList[$funcName]) ) |
|
| 452 | + { |
|
| 453 | + throw new Exception($funcName . ' not found.'); |
|
| 454 | + } |
|
| 455 | + $pluginName = $this->functionList[$funcName]['plugin']; // plugin name |
|
| 456 | + $plugin = $this->pluginsList[$pluginName]['object']; // plugin object |
|
| 457 | 457 | |
| 458 | - if ($plugin === null) |
|
| 459 | - { |
|
| 460 | - $this->registerPlugin($pluginName); // can throw exception handled by caller |
|
| 461 | - $plugin = $this->pluginsList[$pluginName]['object']; |
|
| 462 | - } |
|
| 458 | + if ($plugin === null) |
|
| 459 | + { |
|
| 460 | + $this->registerPlugin($pluginName); // can throw exception handled by caller |
|
| 461 | + $plugin = $this->pluginsList[$pluginName]['object']; |
|
| 462 | + } |
|
| 463 | 463 | |
| 464 | - $propertyName = $this->functionList[$funcName]['function']; |
|
| 465 | - $this->logClass->log('Using property '. $propertyName . ' of class : '.$pluginName,DEBUG); |
|
| 464 | + $propertyName = $this->functionList[$funcName]['function']; |
|
| 465 | + $this->logClass->log('Using property '. $propertyName . ' of class : '.$pluginName,DEBUG); |
|
| 466 | 466 | |
| 467 | - return $plugin->{$propertyName}($params); |
|
| 468 | - } |
|
| 467 | + return $plugin->{$propertyName}($params); |
|
| 468 | + } |
|
| 469 | 469 | |
| 470 | - public function evaluateFunctionString(string $functionString) : bool |
|
| 471 | - { |
|
| 472 | - $matches=array(); |
|
| 473 | - // Cleanup spaces |
|
| 474 | - //$functionString = $this->trapClass->ruleClass->eval_cleanup($functionString); |
|
| 475 | - //$this->logClass->log('eval cleanup : '.$functionString,DEBUG); |
|
| 470 | + public function evaluateFunctionString(string $functionString) : bool |
|
| 471 | + { |
|
| 472 | + $matches=array(); |
|
| 473 | + // Cleanup spaces |
|
| 474 | + //$functionString = $this->trapClass->ruleClass->eval_cleanup($functionString); |
|
| 475 | + //$this->logClass->log('eval cleanup : '.$functionString,DEBUG); |
|
| 476 | 476 | |
| 477 | - // Match function call |
|
| 478 | - $num=preg_match('/^__([a-zA-Z0-9]+)\((.+)\)$/', $functionString , $matches); |
|
| 479 | - if ($num !=1) |
|
| 480 | - { |
|
| 481 | - throw new \ErrorException('Function syntax error : ' . $functionString ); |
|
| 482 | - } |
|
| 483 | - $this->logClass->log('Got function : '. $matches[1] . ', params : '.$matches[2],DEBUG); |
|
| 484 | - $funcName=$matches[1]; |
|
| 477 | + // Match function call |
|
| 478 | + $num=preg_match('/^__([a-zA-Z0-9]+)\((.+)\)$/', $functionString , $matches); |
|
| 479 | + if ($num !=1) |
|
| 480 | + { |
|
| 481 | + throw new \ErrorException('Function syntax error : ' . $functionString ); |
|
| 482 | + } |
|
| 483 | + $this->logClass->log('Got function : '. $matches[1] . ', params : '.$matches[2],DEBUG); |
|
| 484 | + $funcName=$matches[1]; |
|
| 485 | 485 | |
| 486 | - // Get parameters comma separated |
|
| 487 | - $funcParams=str_getcsv($matches[2],',','"',"\\"); |
|
| 488 | - $this->logClass->log('Function params : ' . print_r($funcParams,true),DEBUG); |
|
| 486 | + // Get parameters comma separated |
|
| 487 | + $funcParams=str_getcsv($matches[2],',','"',"\\"); |
|
| 488 | + $this->logClass->log('Function params : ' . print_r($funcParams,true),DEBUG); |
|
| 489 | 489 | |
| 490 | - // return evaluation |
|
| 491 | - return $this->getFunctionEval($funcName, $funcParams); |
|
| 490 | + // return evaluation |
|
| 491 | + return $this->getFunctionEval($funcName, $funcParams); |
|
| 492 | 492 | |
| 493 | - } |
|
| 493 | + } |
|
| 494 | 494 | |
| 495 | 495 | } |
| 496 | 496 | |
| 497 | 497 | abstract class PluginTemplate |
| 498 | 498 | { |
| 499 | 499 | |
| 500 | - /** @var Logging $loggingClass */ |
|
| 501 | - private $loggingClass; |
|
| 500 | + /** @var Logging $loggingClass */ |
|
| 501 | + private $loggingClass; |
|
| 502 | 502 | |
| 503 | - /** @var string $name Name of plugin */ |
|
| 504 | - public $name; |
|
| 503 | + /** @var string $name Name of plugin */ |
|
| 504 | + public $name; |
|
| 505 | 505 | |
| 506 | - /** @var string $description Description of plugin */ |
|
| 507 | - public $description='Default plugin description'; |
|
| 506 | + /** @var string $description Description of plugin */ |
|
| 507 | + public $description='Default plugin description'; |
|
| 508 | 508 | |
| 509 | - /** @var array $functions Functions of this plugin for rule eval*/ |
|
| 510 | - public $functions=array(); |
|
| 509 | + /** @var array $functions Functions of this plugin for rule eval*/ |
|
| 510 | + public $functions=array(); |
|
| 511 | 511 | |
| 512 | - /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin */ |
|
| 513 | - public $catchAllTraps=false; |
|
| 512 | + /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin */ |
|
| 513 | + public $catchAllTraps=false; |
|
| 514 | 514 | |
| 515 | - /** @var boolean $processTraps Set to true if plugins can handle traps */ |
|
| 516 | - public $processTraps=false; |
|
| 515 | + /** @var boolean $processTraps Set to true if plugins can handle traps */ |
|
| 516 | + public $processTraps=false; |
|
| 517 | 517 | |
| 518 | - /** |
|
| 519 | - * @param \Trapdirector\Logging $loggingClass |
|
| 520 | - */ |
|
| 521 | - public function setLoggingClass($loggingClass) |
|
| 522 | - { |
|
| 523 | - $this->loggingClass = $loggingClass; |
|
| 524 | - } |
|
| 518 | + /** |
|
| 519 | + * @param \Trapdirector\Logging $loggingClass |
|
| 520 | + */ |
|
| 521 | + public function setLoggingClass($loggingClass) |
|
| 522 | + { |
|
| 523 | + $this->loggingClass = $loggingClass; |
|
| 524 | + } |
|
| 525 | 525 | |
| 526 | - /** |
|
| 527 | - * |
|
| 528 | - * @param string $message |
|
| 529 | - * @param int $level DEBUG/INFO/WARN/CRIT |
|
| 530 | - */ |
|
| 531 | - public function log($message,$level) |
|
| 532 | - { |
|
| 533 | - $this->loggingClass->log('[ '.get_class($this).'] '. $message, $level); |
|
| 534 | - } |
|
| 526 | + /** |
|
| 527 | + * |
|
| 528 | + * @param string $message |
|
| 529 | + * @param int $level DEBUG/INFO/WARN/CRIT |
|
| 530 | + */ |
|
| 531 | + public function log($message,$level) |
|
| 532 | + { |
|
| 533 | + $this->loggingClass->log('[ '.get_class($this).'] '. $message, $level); |
|
| 534 | + } |
|
| 535 | 535 | } |
| 536 | 536 | \ No newline at end of file |
@@ -9,383 +9,383 @@ |
||
| 9 | 9 | class Database |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - // Databases |
|
| 13 | - /** @var \PDO $trapDB trap database */ |
|
| 14 | - protected $trapDB=null; |
|
| 15 | - protected $idoDB=null; //< ido database |
|
| 16 | - public $trapDBType; //< Type of database for traps (mysql, pgsql) |
|
| 17 | - public $idoDBType; //< Type of database for ido (mysql, pgsql) |
|
| 12 | + // Databases |
|
| 13 | + /** @var \PDO $trapDB trap database */ |
|
| 14 | + protected $trapDB=null; |
|
| 15 | + protected $idoDB=null; //< ido database |
|
| 16 | + public $trapDBType; //< Type of database for traps (mysql, pgsql) |
|
| 17 | + public $idoDBType; //< Type of database for ido (mysql, pgsql) |
|
| 18 | 18 | |
| 19 | - protected $trapDSN; //< trap database connection params |
|
| 20 | - protected $trapUsername; //< trap database connection params |
|
| 21 | - protected $trapPass; //< trap database connection params |
|
| 22 | - public $dbPrefix; //< database tables prefix |
|
| 19 | + protected $trapDSN; //< trap database connection params |
|
| 20 | + protected $trapUsername; //< trap database connection params |
|
| 21 | + protected $trapPass; //< trap database connection params |
|
| 22 | + public $dbPrefix; //< database tables prefix |
|
| 23 | 23 | |
| 24 | - protected $idoSet; //< bool true is ido database set |
|
| 25 | - protected $idoDSN; //< trap database connection params |
|
| 26 | - protected $idoUsername; //< trap database connection params |
|
| 27 | - protected $idoPass; //< trap database connection params |
|
| 24 | + protected $idoSet; //< bool true is ido database set |
|
| 25 | + protected $idoDSN; //< trap database connection params |
|
| 26 | + protected $idoUsername; //< trap database connection params |
|
| 27 | + protected $idoPass; //< trap database connection params |
|
| 28 | 28 | |
| 29 | - // Logging function |
|
| 29 | + // Logging function |
|
| 30 | 30 | |
| 31 | - protected $logging; //< logging class |
|
| 31 | + protected $logging; //< logging class |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @param Logging $logClass : where to log |
|
| 35 | - * @param array $dbParam : array of named params type,host,dbname,username,[port],[password] |
|
| 36 | - */ |
|
| 37 | - function __construct($logClass,$dbParam,$dbPrefix) |
|
| 38 | - { |
|
| 39 | - $this->logging=$logClass; |
|
| 40 | - $this->dbPrefix=$dbPrefix; |
|
| 33 | + /** |
|
| 34 | + * @param Logging $logClass : where to log |
|
| 35 | + * @param array $dbParam : array of named params type,host,dbname,username,[port],[password] |
|
| 36 | + */ |
|
| 37 | + function __construct($logClass,$dbParam,$dbPrefix) |
|
| 38 | + { |
|
| 39 | + $this->logging=$logClass; |
|
| 40 | + $this->dbPrefix=$dbPrefix; |
|
| 41 | 41 | |
| 42 | - $this->trapDSN=$this->setupDSN($dbParam); |
|
| 43 | - $this->trapUsername = $dbParam['username']; |
|
| 44 | - $this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 45 | - $this->trapDBType=$dbParam['db']; |
|
| 46 | - $this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername.' / prefix : '. $this->dbPrefix,INFO); |
|
| 47 | - $this->db_connect_trap(); |
|
| 42 | + $this->trapDSN=$this->setupDSN($dbParam); |
|
| 43 | + $this->trapUsername = $dbParam['username']; |
|
| 44 | + $this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 45 | + $this->trapDBType=$dbParam['db']; |
|
| 46 | + $this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername.' / prefix : '. $this->dbPrefix,INFO); |
|
| 47 | + $this->db_connect_trap(); |
|
| 48 | 48 | |
| 49 | - } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Setup and connect to IDO database |
|
| 53 | - * @param array $dbParam : array of named params |
|
| 54 | - */ |
|
| 55 | - public function setupIDO($dbParam) |
|
| 56 | - { |
|
| 57 | - $this->idoDSN=$this->setupDSN($dbParam); |
|
| 58 | - $this->idoUsername = $dbParam['username']; |
|
| 59 | - $this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 60 | - $this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO); |
|
| 61 | - $this->idoDBType=$dbParam['db']; |
|
| 62 | - $this->db_connect_ido(); |
|
| 63 | - } |
|
| 51 | + /** |
|
| 52 | + * Setup and connect to IDO database |
|
| 53 | + * @param array $dbParam : array of named params |
|
| 54 | + */ |
|
| 55 | + public function setupIDO($dbParam) |
|
| 56 | + { |
|
| 57 | + $this->idoDSN=$this->setupDSN($dbParam); |
|
| 58 | + $this->idoUsername = $dbParam['username']; |
|
| 59 | + $this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 60 | + $this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO); |
|
| 61 | + $this->idoDBType=$dbParam['db']; |
|
| 62 | + $this->db_connect_ido(); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Connect to IDO database |
|
| 67 | - * @return \PDO |
|
| 68 | - */ |
|
| 69 | - public function db_connect_ido() |
|
| 70 | - { |
|
| 71 | - if ($this->idoDB != null) { |
|
| 72 | - // Check if connection is still alive |
|
| 73 | - try { |
|
| 74 | - $this->idoDB->query('select 1')->fetchColumn(); |
|
| 75 | - return $this->idoDB; |
|
| 76 | - } catch (Exception $e) { |
|
| 77 | - // select 1 failed, try to reconnect. |
|
| 78 | - $this->logging->log('Database IDO connection lost, reconnecting',WARN); |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - try { |
|
| 82 | - $this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass); |
|
| 83 | - } catch (PDOException $e) { |
|
| 84 | - $this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,''); |
|
| 85 | - } |
|
| 86 | - return $this->idoDB; |
|
| 87 | - } |
|
| 65 | + /** |
|
| 66 | + * Connect to IDO database |
|
| 67 | + * @return \PDO |
|
| 68 | + */ |
|
| 69 | + public function db_connect_ido() |
|
| 70 | + { |
|
| 71 | + if ($this->idoDB != null) { |
|
| 72 | + // Check if connection is still alive |
|
| 73 | + try { |
|
| 74 | + $this->idoDB->query('select 1')->fetchColumn(); |
|
| 75 | + return $this->idoDB; |
|
| 76 | + } catch (Exception $e) { |
|
| 77 | + // select 1 failed, try to reconnect. |
|
| 78 | + $this->logging->log('Database IDO connection lost, reconnecting',WARN); |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + try { |
|
| 82 | + $this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass); |
|
| 83 | + } catch (PDOException $e) { |
|
| 84 | + $this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,''); |
|
| 85 | + } |
|
| 86 | + return $this->idoDB; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Connect to Trap database |
|
| 91 | - * @return \PDO |
|
| 92 | - */ |
|
| 93 | - public function db_connect_trap() |
|
| 94 | - { |
|
| 95 | - if ($this->trapDB != null) { |
|
| 96 | - // Check if connection is still alive |
|
| 97 | - try { |
|
| 98 | - $this->trapDB->query('select 1')->fetchColumn(); |
|
| 99 | - return $this->trapDB; |
|
| 100 | - } catch (Exception $e) { |
|
| 101 | - // select 1 failed, try to reconnect. |
|
| 102 | - $this->logging->log('Database connection lost, reconnecting',WARN); |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - try { |
|
| 106 | - $this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass); |
|
| 107 | - } catch (PDOException $e) { |
|
| 108 | - $this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,''); |
|
| 109 | - } |
|
| 110 | - return $this->trapDB; |
|
| 111 | - } |
|
| 89 | + /** |
|
| 90 | + * Connect to Trap database |
|
| 91 | + * @return \PDO |
|
| 92 | + */ |
|
| 93 | + public function db_connect_trap() |
|
| 94 | + { |
|
| 95 | + if ($this->trapDB != null) { |
|
| 96 | + // Check if connection is still alive |
|
| 97 | + try { |
|
| 98 | + $this->trapDB->query('select 1')->fetchColumn(); |
|
| 99 | + return $this->trapDB; |
|
| 100 | + } catch (Exception $e) { |
|
| 101 | + // select 1 failed, try to reconnect. |
|
| 102 | + $this->logging->log('Database connection lost, reconnecting',WARN); |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + try { |
|
| 106 | + $this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass); |
|
| 107 | + } catch (PDOException $e) { |
|
| 108 | + $this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,''); |
|
| 109 | + } |
|
| 110 | + return $this->trapDB; |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * Setup dsn and check parameters |
|
| 115 | - * @param array $configElmt |
|
| 116 | - * @return string |
|
| 117 | - */ |
|
| 118 | - protected function setupDSN($configElmt) |
|
| 119 | - { |
|
| 120 | - if (!array_key_exists('db',$configElmt) || |
|
| 121 | - !array_key_exists('host',$configElmt) || |
|
| 122 | - !array_key_exists('dbname',$configElmt) || |
|
| 123 | - !array_key_exists('username',$configElmt)) |
|
| 124 | - { |
|
| 125 | - $this->logging->log('Missing DB params',ERROR); |
|
| 126 | - return ''; |
|
| 127 | - } |
|
| 113 | + /** |
|
| 114 | + * Setup dsn and check parameters |
|
| 115 | + * @param array $configElmt |
|
| 116 | + * @return string |
|
| 117 | + */ |
|
| 118 | + protected function setupDSN($configElmt) |
|
| 119 | + { |
|
| 120 | + if (!array_key_exists('db',$configElmt) || |
|
| 121 | + !array_key_exists('host',$configElmt) || |
|
| 122 | + !array_key_exists('dbname',$configElmt) || |
|
| 123 | + !array_key_exists('username',$configElmt)) |
|
| 124 | + { |
|
| 125 | + $this->logging->log('Missing DB params',ERROR); |
|
| 126 | + return ''; |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - // $dsn = 'mysql:dbname=traps;host=127.0.0.1'; |
|
| 130 | - $dsn= $configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
| 129 | + // $dsn = 'mysql:dbname=traps;host=127.0.0.1'; |
|
| 130 | + $dsn= $configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
| 131 | 131 | |
| 132 | - if (array_key_exists('port', $configElmt)) |
|
| 133 | - { |
|
| 134 | - $dsn .= ';port='.$configElmt['port']; |
|
| 135 | - } |
|
| 136 | - return $dsn; |
|
| 137 | - } |
|
| 132 | + if (array_key_exists('port', $configElmt)) |
|
| 133 | + { |
|
| 134 | + $dsn .= ';port='.$configElmt['port']; |
|
| 135 | + } |
|
| 136 | + return $dsn; |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - /** Set name=element in database config table |
|
| 140 | - * @param string $name |
|
| 141 | - * @param string $element |
|
| 142 | - * @return boolean true on success, else false (error logged) |
|
| 143 | - */ |
|
| 144 | - public function setDBConfig($name,$element) |
|
| 145 | - { |
|
| 146 | - $db_conn=$this->db_connect_trap(); |
|
| 147 | - $sql='SELECT id from '.$this->dbPrefix.'db_config WHERE ( name=\''.$name.'\' )'; |
|
| 148 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 149 | - $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
| 150 | - return false; |
|
| 151 | - } |
|
| 152 | - $value=$ret_code->fetch(); |
|
| 153 | - if ($value != null && isset($value['id'])) |
|
| 154 | - { // Entry exists -> update |
|
| 155 | - $sql='UPDATE '.$this->dbPrefix.'db_config SET value = \''.$element.'\' WHERE (id = '.$value['id'].')'; |
|
| 156 | - } |
|
| 157 | - else |
|
| 158 | - { // Entry does no exists -> create |
|
| 159 | - $sql='INSERT INTO '.$this->dbPrefix.'db_config (name,value) VALUES (\''.$name.'\' , \''.$element.'\' )'; |
|
| 160 | - } |
|
| 161 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 162 | - $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
| 163 | - return false; |
|
| 164 | - } |
|
| 165 | - $this->logging->log('Setting config '.$name.' = '.$element.' in database',INFO); |
|
| 166 | - return true; |
|
| 167 | - } |
|
| 139 | + /** Set name=element in database config table |
|
| 140 | + * @param string $name |
|
| 141 | + * @param string $element |
|
| 142 | + * @return boolean true on success, else false (error logged) |
|
| 143 | + */ |
|
| 144 | + public function setDBConfig($name,$element) |
|
| 145 | + { |
|
| 146 | + $db_conn=$this->db_connect_trap(); |
|
| 147 | + $sql='SELECT id from '.$this->dbPrefix.'db_config WHERE ( name=\''.$name.'\' )'; |
|
| 148 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 149 | + $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
| 150 | + return false; |
|
| 151 | + } |
|
| 152 | + $value=$ret_code->fetch(); |
|
| 153 | + if ($value != null && isset($value['id'])) |
|
| 154 | + { // Entry exists -> update |
|
| 155 | + $sql='UPDATE '.$this->dbPrefix.'db_config SET value = \''.$element.'\' WHERE (id = '.$value['id'].')'; |
|
| 156 | + } |
|
| 157 | + else |
|
| 158 | + { // Entry does no exists -> create |
|
| 159 | + $sql='INSERT INTO '.$this->dbPrefix.'db_config (name,value) VALUES (\''.$name.'\' , \''.$element.'\' )'; |
|
| 160 | + } |
|
| 161 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 162 | + $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
| 163 | + return false; |
|
| 164 | + } |
|
| 165 | + $this->logging->log('Setting config '.$name.' = '.$element.' in database',INFO); |
|
| 166 | + return true; |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * Get data from db_config |
|
| 171 | - * @param $element string name of param |
|
| 172 | - * @return mixed : value (or null) |
|
| 173 | - */ |
|
| 174 | - public function getDBConfig($element) |
|
| 175 | - { |
|
| 176 | - $db_conn=$this->db_connect_trap(); |
|
| 177 | - $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
| 178 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 179 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 180 | - return null; |
|
| 181 | - } |
|
| 182 | - $value=$ret_code->fetch(); |
|
| 183 | - if ($value != null && isset($value['value'])) |
|
| 184 | - { |
|
| 185 | - return $value['value']; |
|
| 186 | - } |
|
| 187 | - return null; |
|
| 188 | - } |
|
| 169 | + /** |
|
| 170 | + * Get data from db_config |
|
| 171 | + * @param $element string name of param |
|
| 172 | + * @return mixed : value (or null) |
|
| 173 | + */ |
|
| 174 | + public function getDBConfig($element) |
|
| 175 | + { |
|
| 176 | + $db_conn=$this->db_connect_trap(); |
|
| 177 | + $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
| 178 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 179 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 180 | + return null; |
|
| 181 | + } |
|
| 182 | + $value=$ret_code->fetch(); |
|
| 183 | + if ($value != null && isset($value['value'])) |
|
| 184 | + { |
|
| 185 | + return $value['value']; |
|
| 186 | + } |
|
| 187 | + return null; |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | 190 | |
| 191 | - //********* Schema Management *********************/ |
|
| 191 | + //********* Schema Management *********************/ |
|
| 192 | 192 | |
| 193 | - /** Create database schema |
|
| 194 | - * @param $schema_file string File to read schema from |
|
| 195 | - * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 196 | - */ |
|
| 197 | - public function create_schema($schema_file,$table_prefix) |
|
| 198 | - { |
|
| 199 | - //Read data from snmptrapd from stdin |
|
| 200 | - $input_stream=fopen($schema_file, 'r'); |
|
| 193 | + /** Create database schema |
|
| 194 | + * @param $schema_file string File to read schema from |
|
| 195 | + * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 196 | + */ |
|
| 197 | + public function create_schema($schema_file,$table_prefix) |
|
| 198 | + { |
|
| 199 | + //Read data from snmptrapd from stdin |
|
| 200 | + $input_stream=fopen($schema_file, 'r'); |
|
| 201 | 201 | |
| 202 | - if ($input_stream=== false) |
|
| 203 | - { |
|
| 204 | - $this->logging->log("Error reading schema !",ERROR,''); |
|
| 205 | - return; |
|
| 206 | - } |
|
| 207 | - $newline=''; |
|
| 208 | - $cur_table=''; |
|
| 209 | - $cur_table_array=array(); |
|
| 210 | - $db_conn=$this->db_connect_trap(); |
|
| 202 | + if ($input_stream=== false) |
|
| 203 | + { |
|
| 204 | + $this->logging->log("Error reading schema !",ERROR,''); |
|
| 205 | + return; |
|
| 206 | + } |
|
| 207 | + $newline=''; |
|
| 208 | + $cur_table=''; |
|
| 209 | + $cur_table_array=array(); |
|
| 210 | + $db_conn=$this->db_connect_trap(); |
|
| 211 | 211 | |
| 212 | - while (($line=fgets($input_stream)) !== false) |
|
| 213 | - { |
|
| 214 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 215 | - if (preg_match('/; *$/', $newline)) |
|
| 216 | - { |
|
| 217 | - $sql= $newline; |
|
| 218 | - if ($db_conn->query($sql) === false) { |
|
| 219 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 220 | - return; |
|
| 221 | - } |
|
| 222 | - if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 223 | - { |
|
| 224 | - $cur_table='table '.$cur_table_array[1]; |
|
| 225 | - } |
|
| 226 | - else |
|
| 227 | - { |
|
| 228 | - $cur_table='secret SQL stuff :-)'; |
|
| 229 | - } |
|
| 230 | - $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
| 231 | - $newline=''; |
|
| 232 | - } |
|
| 233 | - } |
|
| 212 | + while (($line=fgets($input_stream)) !== false) |
|
| 213 | + { |
|
| 214 | + $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 215 | + if (preg_match('/; *$/', $newline)) |
|
| 216 | + { |
|
| 217 | + $sql= $newline; |
|
| 218 | + if ($db_conn->query($sql) === false) { |
|
| 219 | + $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 220 | + return; |
|
| 221 | + } |
|
| 222 | + if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 223 | + { |
|
| 224 | + $cur_table='table '.$cur_table_array[1]; |
|
| 225 | + } |
|
| 226 | + else |
|
| 227 | + { |
|
| 228 | + $cur_table='secret SQL stuff :-)'; |
|
| 229 | + } |
|
| 230 | + $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
| 231 | + $newline=''; |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | - $sql= $newline; |
|
| 236 | - if ($sql != '' ) |
|
| 237 | - { |
|
| 238 | - if ($db_conn->query($sql) === false) { |
|
| 239 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 240 | - return; |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - $this->logging->log('Schema created',INFO); |
|
| 244 | - } |
|
| 235 | + $sql= $newline; |
|
| 236 | + if ($sql != '' ) |
|
| 237 | + { |
|
| 238 | + if ($db_conn->query($sql) === false) { |
|
| 239 | + $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 240 | + return; |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + $this->logging->log('Schema created',INFO); |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - /** |
|
| 247 | - * Update database schema from current (as set in db) to $target_version |
|
| 248 | - * @param $prefix string file prefix of sql update File |
|
| 249 | - * @param $target_version int target db version number |
|
| 250 | - * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 251 | - * @param bool $getmsg : only get messages from version upgrades |
|
| 252 | - * @return string : if $getmsg=true, return messages or 'ERROR' on error. |
|
| 253 | - */ |
|
| 254 | - public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
|
| 255 | - { |
|
| 256 | - // Get current db number |
|
| 257 | - $db_conn=$this->db_connect_trap(); |
|
| 258 | - $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' '; |
|
| 259 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 260 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 261 | - $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
| 262 | - return 'ERROR'; |
|
| 263 | - } |
|
| 264 | - $version=$ret_code->fetchAll(); |
|
| 265 | - $cur_version=$version[0]['value']; |
|
| 246 | + /** |
|
| 247 | + * Update database schema from current (as set in db) to $target_version |
|
| 248 | + * @param $prefix string file prefix of sql update File |
|
| 249 | + * @param $target_version int target db version number |
|
| 250 | + * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 251 | + * @param bool $getmsg : only get messages from version upgrades |
|
| 252 | + * @return string : if $getmsg=true, return messages or 'ERROR' on error. |
|
| 253 | + */ |
|
| 254 | + public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
|
| 255 | + { |
|
| 256 | + // Get current db number |
|
| 257 | + $db_conn=$this->db_connect_trap(); |
|
| 258 | + $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' '; |
|
| 259 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 260 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 261 | + $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
| 262 | + return 'ERROR'; |
|
| 263 | + } |
|
| 264 | + $version=$ret_code->fetchAll(); |
|
| 265 | + $cur_version=$version[0]['value']; |
|
| 266 | 266 | |
| 267 | - if ($this->trapDBType == 'pgsql') |
|
| 268 | - { |
|
| 269 | - $prefix .= 'update_pgsql/schema_'; |
|
| 270 | - } |
|
| 271 | - else |
|
| 272 | - { |
|
| 273 | - $prefix .= 'update_sql/schema_'; |
|
| 274 | - } |
|
| 275 | - //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
|
| 276 | - if ($getmsg === true) |
|
| 277 | - { |
|
| 278 | - return $this->update_schema_message($prefix, $cur_version, $target_version); |
|
| 279 | - } |
|
| 267 | + if ($this->trapDBType == 'pgsql') |
|
| 268 | + { |
|
| 269 | + $prefix .= 'update_pgsql/schema_'; |
|
| 270 | + } |
|
| 271 | + else |
|
| 272 | + { |
|
| 273 | + $prefix .= 'update_sql/schema_'; |
|
| 274 | + } |
|
| 275 | + //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
|
| 276 | + if ($getmsg === true) |
|
| 277 | + { |
|
| 278 | + return $this->update_schema_message($prefix, $cur_version, $target_version); |
|
| 279 | + } |
|
| 280 | 280 | |
| 281 | - if ($this->update_schema_do($prefix, $cur_version, $target_version, $table_prefix) === true) |
|
| 282 | - { |
|
| 283 | - return 'ERROR'; |
|
| 284 | - } |
|
| 285 | - return ''; |
|
| 281 | + if ($this->update_schema_do($prefix, $cur_version, $target_version, $table_prefix) === true) |
|
| 282 | + { |
|
| 283 | + return 'ERROR'; |
|
| 284 | + } |
|
| 285 | + return ''; |
|
| 286 | 286 | |
| 287 | - } |
|
| 287 | + } |
|
| 288 | 288 | |
| 289 | - /** |
|
| 290 | - * Update database schema from current (as set in db) to $target_version |
|
| 291 | - * @param string $prefix file prefix of sql update File |
|
| 292 | - * @param int $cur_version current db version number |
|
| 293 | - * @param int $target_version target db version number |
|
| 294 | - * @param string $table_prefix to replace #PREFIX# in schema file by this |
|
| 295 | - * @return bool : true on error |
|
| 296 | - */ |
|
| 297 | - public function update_schema_do($prefix,$cur_version,$target_version,$table_prefix) |
|
| 298 | - { |
|
| 299 | - while($cur_version<$target_version) |
|
| 300 | - { // TODO : execute pre & post scripts |
|
| 301 | - $cur_version++; |
|
| 302 | - $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
| 303 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 304 | - $input_stream=fopen($updateFile, 'r'); |
|
| 305 | - if ($input_stream=== false) |
|
| 306 | - { |
|
| 307 | - $this->logging->log("Error reading update file ". $updateFile,ERROR); |
|
| 308 | - return true; |
|
| 309 | - } |
|
| 310 | - $newline=''; |
|
| 311 | - $db_conn=$this->db_connect_trap(); |
|
| 312 | - $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 313 | - while (($line=fgets($input_stream)) !== false) |
|
| 314 | - { |
|
| 315 | - if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
| 316 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 317 | - if (preg_match('/; *$/', $newline)) |
|
| 318 | - { |
|
| 319 | - $sql_req=$db_conn->prepare($newline); |
|
| 320 | - if ($sql_req->execute() === false) { |
|
| 321 | - $this->logging->log('Error create schema : '.$newline,ERROR); |
|
| 322 | - return true; |
|
| 323 | - } |
|
| 324 | - $cur_table_array=array(); |
|
| 325 | - if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 326 | - { |
|
| 327 | - $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
| 328 | - } |
|
| 329 | - else |
|
| 330 | - { |
|
| 331 | - $cur_table='secret SQL stuff :-)'; |
|
| 332 | - //$cur_table=$newline; |
|
| 333 | - } |
|
| 334 | - $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
| 289 | + /** |
|
| 290 | + * Update database schema from current (as set in db) to $target_version |
|
| 291 | + * @param string $prefix file prefix of sql update File |
|
| 292 | + * @param int $cur_version current db version number |
|
| 293 | + * @param int $target_version target db version number |
|
| 294 | + * @param string $table_prefix to replace #PREFIX# in schema file by this |
|
| 295 | + * @return bool : true on error |
|
| 296 | + */ |
|
| 297 | + public function update_schema_do($prefix,$cur_version,$target_version,$table_prefix) |
|
| 298 | + { |
|
| 299 | + while($cur_version<$target_version) |
|
| 300 | + { // TODO : execute pre & post scripts |
|
| 301 | + $cur_version++; |
|
| 302 | + $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
| 303 | + $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 304 | + $input_stream=fopen($updateFile, 'r'); |
|
| 305 | + if ($input_stream=== false) |
|
| 306 | + { |
|
| 307 | + $this->logging->log("Error reading update file ". $updateFile,ERROR); |
|
| 308 | + return true; |
|
| 309 | + } |
|
| 310 | + $newline=''; |
|
| 311 | + $db_conn=$this->db_connect_trap(); |
|
| 312 | + $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 313 | + while (($line=fgets($input_stream)) !== false) |
|
| 314 | + { |
|
| 315 | + if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
| 316 | + $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 317 | + if (preg_match('/; *$/', $newline)) |
|
| 318 | + { |
|
| 319 | + $sql_req=$db_conn->prepare($newline); |
|
| 320 | + if ($sql_req->execute() === false) { |
|
| 321 | + $this->logging->log('Error create schema : '.$newline,ERROR); |
|
| 322 | + return true; |
|
| 323 | + } |
|
| 324 | + $cur_table_array=array(); |
|
| 325 | + if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 326 | + { |
|
| 327 | + $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
| 328 | + } |
|
| 329 | + else |
|
| 330 | + { |
|
| 331 | + $cur_table='secret SQL stuff :-)'; |
|
| 332 | + //$cur_table=$newline; |
|
| 333 | + } |
|
| 334 | + $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
| 335 | 335 | |
| 336 | - $newline=''; |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - fclose($input_stream); |
|
| 336 | + $newline=''; |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + fclose($input_stream); |
|
| 340 | 340 | |
| 341 | - $sql='UPDATE '.$this->dbPrefix.'db_config SET value='.$cur_version.' WHERE ( name=\'db_version\' )'; |
|
| 342 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 343 | - if ($db_conn->query($sql) === false) { |
|
| 344 | - $this->logging->log('Cannot update db version. Query : ' . $sql,WARN); |
|
| 345 | - return true; |
|
| 346 | - } |
|
| 341 | + $sql='UPDATE '.$this->dbPrefix.'db_config SET value='.$cur_version.' WHERE ( name=\'db_version\' )'; |
|
| 342 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 343 | + if ($db_conn->query($sql) === false) { |
|
| 344 | + $this->logging->log('Cannot update db version. Query : ' . $sql,WARN); |
|
| 345 | + return true; |
|
| 346 | + } |
|
| 347 | 347 | |
| 348 | - $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
| 349 | - } |
|
| 350 | - return false; |
|
| 351 | - } |
|
| 348 | + $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
| 349 | + } |
|
| 350 | + return false; |
|
| 351 | + } |
|
| 352 | 352 | |
| 353 | - /** |
|
| 354 | - * Get database message for update to $target_version |
|
| 355 | - * @param string $prefix file prefix of sql update File |
|
| 356 | - * @param int $cur_version current db version number |
|
| 357 | - * @param int $target_version target db version number |
|
| 358 | - * @return string : return messages or 'ERROR'. |
|
| 359 | - */ |
|
| 360 | - private function update_schema_message($prefix,$cur_version,$target_version) |
|
| 361 | - { |
|
| 353 | + /** |
|
| 354 | + * Get database message for update to $target_version |
|
| 355 | + * @param string $prefix file prefix of sql update File |
|
| 356 | + * @param int $cur_version current db version number |
|
| 357 | + * @param int $target_version target db version number |
|
| 358 | + * @return string : return messages or 'ERROR'. |
|
| 359 | + */ |
|
| 360 | + private function update_schema_message($prefix,$cur_version,$target_version) |
|
| 361 | + { |
|
| 362 | 362 | |
| 363 | - $message=''; |
|
| 364 | - $this->logging->log('getting message for upgrade',DEBUG ); |
|
| 365 | - while($cur_version<$target_version) |
|
| 366 | - { |
|
| 367 | - $cur_version++; |
|
| 368 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 369 | - $input_stream=fopen($updateFile, 'r'); |
|
| 370 | - if ($input_stream=== false) |
|
| 371 | - { |
|
| 372 | - $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
| 373 | - return 'ERROR'; |
|
| 374 | - } |
|
| 375 | - do |
|
| 376 | - { |
|
| 377 | - $line=fgets($input_stream); |
|
| 378 | - } |
|
| 379 | - while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
| 380 | - fclose($input_stream); |
|
| 381 | - if ($line === false) |
|
| 382 | - { |
|
| 383 | - $this->logging->log("No message in file ". $updateFile,2,''); |
|
| 384 | - return ''; |
|
| 385 | - } |
|
| 386 | - $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
| 387 | - } |
|
| 388 | - return $message; |
|
| 389 | - } |
|
| 363 | + $message=''; |
|
| 364 | + $this->logging->log('getting message for upgrade',DEBUG ); |
|
| 365 | + while($cur_version<$target_version) |
|
| 366 | + { |
|
| 367 | + $cur_version++; |
|
| 368 | + $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 369 | + $input_stream=fopen($updateFile, 'r'); |
|
| 370 | + if ($input_stream=== false) |
|
| 371 | + { |
|
| 372 | + $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
| 373 | + return 'ERROR'; |
|
| 374 | + } |
|
| 375 | + do |
|
| 376 | + { |
|
| 377 | + $line=fgets($input_stream); |
|
| 378 | + } |
|
| 379 | + while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
| 380 | + fclose($input_stream); |
|
| 381 | + if ($line === false) |
|
| 382 | + { |
|
| 383 | + $this->logging->log("No message in file ". $updateFile,2,''); |
|
| 384 | + return ''; |
|
| 385 | + } |
|
| 386 | + $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
| 387 | + } |
|
| 388 | + return $message; |
|
| 389 | + } |
|
| 390 | 390 | |
| 391 | 391 | } |
| 392 | 392 | \ No newline at end of file |
@@ -34,16 +34,16 @@ discard block |
||
| 34 | 34 | * @param Logging $logClass : where to log |
| 35 | 35 | * @param array $dbParam : array of named params type,host,dbname,username,[port],[password] |
| 36 | 36 | */ |
| 37 | - function __construct($logClass,$dbParam,$dbPrefix) |
|
| 37 | + function __construct($logClass, $dbParam, $dbPrefix) |
|
| 38 | 38 | { |
| 39 | 39 | $this->logging=$logClass; |
| 40 | 40 | $this->dbPrefix=$dbPrefix; |
| 41 | 41 | |
| 42 | 42 | $this->trapDSN=$this->setupDSN($dbParam); |
| 43 | - $this->trapUsername = $dbParam['username']; |
|
| 44 | - $this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 43 | + $this->trapUsername=$dbParam['username']; |
|
| 44 | + $this->trapPass=(array_key_exists('password', $dbParam)) ? $dbParam['password'] : ''; |
|
| 45 | 45 | $this->trapDBType=$dbParam['db']; |
| 46 | - $this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername.' / prefix : '. $this->dbPrefix,INFO); |
|
| 46 | + $this->logging->log('DSN : '.$this->trapDSN.';user '.$this->trapUsername.' / prefix : '.$this->dbPrefix, INFO); |
|
| 47 | 47 | $this->db_connect_trap(); |
| 48 | 48 | |
| 49 | 49 | } |
@@ -55,9 +55,9 @@ discard block |
||
| 55 | 55 | public function setupIDO($dbParam) |
| 56 | 56 | { |
| 57 | 57 | $this->idoDSN=$this->setupDSN($dbParam); |
| 58 | - $this->idoUsername = $dbParam['username']; |
|
| 59 | - $this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 60 | - $this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO); |
|
| 58 | + $this->idoUsername=$dbParam['username']; |
|
| 59 | + $this->idoPass=(array_key_exists('password', $dbParam)) ? $dbParam['password'] : ''; |
|
| 60 | + $this->logging->log('DSN : '.$this->idoDSN.';user '.$this->idoUsername, INFO); |
|
| 61 | 61 | $this->idoDBType=$dbParam['db']; |
| 62 | 62 | $this->db_connect_ido(); |
| 63 | 63 | } |
@@ -75,13 +75,13 @@ discard block |
||
| 75 | 75 | return $this->idoDB; |
| 76 | 76 | } catch (Exception $e) { |
| 77 | 77 | // select 1 failed, try to reconnect. |
| 78 | - $this->logging->log('Database IDO connection lost, reconnecting',WARN); |
|
| 78 | + $this->logging->log('Database IDO connection lost, reconnecting', WARN); |
|
| 79 | 79 | } |
| 80 | 80 | } |
| 81 | 81 | try { |
| 82 | - $this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass); |
|
| 82 | + $this->idoDB=new PDO($this->idoDSN, $this->idoUsername, $this->idoPass); |
|
| 83 | 83 | } catch (PDOException $e) { |
| 84 | - $this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,''); |
|
| 84 | + $this->logging->log('Connection failed to IDO : '.$e->getMessage(), ERROR, ''); |
|
| 85 | 85 | } |
| 86 | 86 | return $this->idoDB; |
| 87 | 87 | } |
@@ -99,13 +99,13 @@ discard block |
||
| 99 | 99 | return $this->trapDB; |
| 100 | 100 | } catch (Exception $e) { |
| 101 | 101 | // select 1 failed, try to reconnect. |
| 102 | - $this->logging->log('Database connection lost, reconnecting',WARN); |
|
| 102 | + $this->logging->log('Database connection lost, reconnecting', WARN); |
|
| 103 | 103 | } |
| 104 | 104 | } |
| 105 | 105 | try { |
| 106 | - $this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass); |
|
| 106 | + $this->trapDB=new PDO($this->trapDSN, $this->trapUsername, $this->trapPass); |
|
| 107 | 107 | } catch (PDOException $e) { |
| 108 | - $this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,''); |
|
| 108 | + $this->logging->log('Connection failed : '.$e->getMessage(), ERROR, ''); |
|
| 109 | 109 | } |
| 110 | 110 | return $this->trapDB; |
| 111 | 111 | } |
@@ -117,21 +117,21 @@ discard block |
||
| 117 | 117 | */ |
| 118 | 118 | protected function setupDSN($configElmt) |
| 119 | 119 | { |
| 120 | - if (!array_key_exists('db',$configElmt) || |
|
| 121 | - !array_key_exists('host',$configElmt) || |
|
| 122 | - !array_key_exists('dbname',$configElmt) || |
|
| 123 | - !array_key_exists('username',$configElmt)) |
|
| 120 | + if (!array_key_exists('db', $configElmt) || |
|
| 121 | + !array_key_exists('host', $configElmt) || |
|
| 122 | + !array_key_exists('dbname', $configElmt) || |
|
| 123 | + !array_key_exists('username', $configElmt)) |
|
| 124 | 124 | { |
| 125 | - $this->logging->log('Missing DB params',ERROR); |
|
| 125 | + $this->logging->log('Missing DB params', ERROR); |
|
| 126 | 126 | return ''; |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | // $dsn = 'mysql:dbname=traps;host=127.0.0.1'; |
| 130 | - $dsn= $configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
| 130 | + $dsn=$configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
| 131 | 131 | |
| 132 | 132 | if (array_key_exists('port', $configElmt)) |
| 133 | 133 | { |
| 134 | - $dsn .= ';port='.$configElmt['port']; |
|
| 134 | + $dsn.=';port='.$configElmt['port']; |
|
| 135 | 135 | } |
| 136 | 136 | return $dsn; |
| 137 | 137 | } |
@@ -141,12 +141,12 @@ discard block |
||
| 141 | 141 | * @param string $element |
| 142 | 142 | * @return boolean true on success, else false (error logged) |
| 143 | 143 | */ |
| 144 | - public function setDBConfig($name,$element) |
|
| 144 | + public function setDBConfig($name, $element) |
|
| 145 | 145 | { |
| 146 | 146 | $db_conn=$this->db_connect_trap(); |
| 147 | 147 | $sql='SELECT id from '.$this->dbPrefix.'db_config WHERE ( name=\''.$name.'\' )'; |
| 148 | 148 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 149 | - $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
| 149 | + $this->logging->log('Error setting config element : '.$sql, WARN, ''); |
|
| 150 | 150 | return false; |
| 151 | 151 | } |
| 152 | 152 | $value=$ret_code->fetch(); |
@@ -159,10 +159,10 @@ discard block |
||
| 159 | 159 | $sql='INSERT INTO '.$this->dbPrefix.'db_config (name,value) VALUES (\''.$name.'\' , \''.$element.'\' )'; |
| 160 | 160 | } |
| 161 | 161 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 162 | - $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
| 162 | + $this->logging->log('Error setting config element : '.$sql, WARN, ''); |
|
| 163 | 163 | return false; |
| 164 | 164 | } |
| 165 | - $this->logging->log('Setting config '.$name.' = '.$element.' in database',INFO); |
|
| 165 | + $this->logging->log('Setting config '.$name.' = '.$element.' in database', INFO); |
|
| 166 | 166 | return true; |
| 167 | 167 | } |
| 168 | 168 | |
@@ -176,7 +176,7 @@ discard block |
||
| 176 | 176 | $db_conn=$this->db_connect_trap(); |
| 177 | 177 | $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
| 178 | 178 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 179 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 179 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
| 180 | 180 | return null; |
| 181 | 181 | } |
| 182 | 182 | $value=$ret_code->fetch(); |
@@ -194,14 +194,14 @@ discard block |
||
| 194 | 194 | * @param $schema_file string File to read schema from |
| 195 | 195 | * @param $table_prefix string to replace #PREFIX# in schema file by this |
| 196 | 196 | */ |
| 197 | - public function create_schema($schema_file,$table_prefix) |
|
| 197 | + public function create_schema($schema_file, $table_prefix) |
|
| 198 | 198 | { |
| 199 | 199 | //Read data from snmptrapd from stdin |
| 200 | 200 | $input_stream=fopen($schema_file, 'r'); |
| 201 | 201 | |
| 202 | - if ($input_stream=== false) |
|
| 202 | + if ($input_stream === false) |
|
| 203 | 203 | { |
| 204 | - $this->logging->log("Error reading schema !",ERROR,''); |
|
| 204 | + $this->logging->log("Error reading schema !", ERROR, ''); |
|
| 205 | 205 | return; |
| 206 | 206 | } |
| 207 | 207 | $newline=''; |
@@ -211,15 +211,15 @@ discard block |
||
| 211 | 211 | |
| 212 | 212 | while (($line=fgets($input_stream)) !== false) |
| 213 | 213 | { |
| 214 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 214 | + $newline.=chop(preg_replace('/#PREFIX#/', $table_prefix, $line)); |
|
| 215 | 215 | if (preg_match('/; *$/', $newline)) |
| 216 | 216 | { |
| 217 | - $sql= $newline; |
|
| 217 | + $sql=$newline; |
|
| 218 | 218 | if ($db_conn->query($sql) === false) { |
| 219 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 219 | + $this->logging->log('Error create schema : '.$sql, ERROR, ''); |
|
| 220 | 220 | return; |
| 221 | 221 | } |
| 222 | - if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 222 | + if (preg_match('/^ *CREATE TABLE ([^ ]+)/', $newline, $cur_table_array)) |
|
| 223 | 223 | { |
| 224 | 224 | $cur_table='table '.$cur_table_array[1]; |
| 225 | 225 | } |
@@ -227,20 +227,20 @@ discard block |
||
| 227 | 227 | { |
| 228 | 228 | $cur_table='secret SQL stuff :-)'; |
| 229 | 229 | } |
| 230 | - $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
| 230 | + $this->logging->log('Creating : '.$cur_table, INFO); |
|
| 231 | 231 | $newline=''; |
| 232 | 232 | } |
| 233 | 233 | } |
| 234 | 234 | |
| 235 | - $sql= $newline; |
|
| 236 | - if ($sql != '' ) |
|
| 235 | + $sql=$newline; |
|
| 236 | + if ($sql != '') |
|
| 237 | 237 | { |
| 238 | 238 | if ($db_conn->query($sql) === false) { |
| 239 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 239 | + $this->logging->log('Error create schema : '.$sql, ERROR, ''); |
|
| 240 | 240 | return; |
| 241 | 241 | } |
| 242 | 242 | } |
| 243 | - $this->logging->log('Schema created',INFO); |
|
| 243 | + $this->logging->log('Schema created', INFO); |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | /** |
@@ -251,14 +251,14 @@ discard block |
||
| 251 | 251 | * @param bool $getmsg : only get messages from version upgrades |
| 252 | 252 | * @return string : if $getmsg=true, return messages or 'ERROR' on error. |
| 253 | 253 | */ |
| 254 | - public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
|
| 254 | + public function update_schema($prefix, $target_version, $table_prefix, $getmsg=false) |
|
| 255 | 255 | { |
| 256 | 256 | // Get current db number |
| 257 | 257 | $db_conn=$this->db_connect_trap(); |
| 258 | 258 | $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' '; |
| 259 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 259 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
| 260 | 260 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 261 | - $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
| 261 | + $this->logging->log('Cannot get db version. Query : '.$sql, 2, ''); |
|
| 262 | 262 | return 'ERROR'; |
| 263 | 263 | } |
| 264 | 264 | $version=$ret_code->fetchAll(); |
@@ -266,11 +266,11 @@ discard block |
||
| 266 | 266 | |
| 267 | 267 | if ($this->trapDBType == 'pgsql') |
| 268 | 268 | { |
| 269 | - $prefix .= 'update_pgsql/schema_'; |
|
| 269 | + $prefix.='update_pgsql/schema_'; |
|
| 270 | 270 | } |
| 271 | 271 | else |
| 272 | 272 | { |
| 273 | - $prefix .= 'update_sql/schema_'; |
|
| 273 | + $prefix.='update_sql/schema_'; |
|
| 274 | 274 | } |
| 275 | 275 | //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
| 276 | 276 | if ($getmsg === true) |
@@ -294,17 +294,17 @@ discard block |
||
| 294 | 294 | * @param string $table_prefix to replace #PREFIX# in schema file by this |
| 295 | 295 | * @return bool : true on error |
| 296 | 296 | */ |
| 297 | - public function update_schema_do($prefix,$cur_version,$target_version,$table_prefix) |
|
| 297 | + public function update_schema_do($prefix, $cur_version, $target_version, $table_prefix) |
|
| 298 | 298 | { |
| 299 | - while($cur_version<$target_version) |
|
| 299 | + while ($cur_version < $target_version) |
|
| 300 | 300 | { // TODO : execute pre & post scripts |
| 301 | 301 | $cur_version++; |
| 302 | - $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
| 303 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 302 | + $this->logging->log('Updating to version : '.$cur_version, INFO); |
|
| 303 | + $updateFile=$prefix.'v'.($cur_version - 1).'_v'.$cur_version.'.sql'; |
|
| 304 | 304 | $input_stream=fopen($updateFile, 'r'); |
| 305 | - if ($input_stream=== false) |
|
| 305 | + if ($input_stream === false) |
|
| 306 | 306 | { |
| 307 | - $this->logging->log("Error reading update file ". $updateFile,ERROR); |
|
| 307 | + $this->logging->log("Error reading update file ".$updateFile, ERROR); |
|
| 308 | 308 | return true; |
| 309 | 309 | } |
| 310 | 310 | $newline=''; |
@@ -313,25 +313,25 @@ discard block |
||
| 313 | 313 | while (($line=fgets($input_stream)) !== false) |
| 314 | 314 | { |
| 315 | 315 | if (preg_match('/^#/', $line)) continue; // ignore comment lines |
| 316 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 316 | + $newline.=chop(preg_replace('/#PREFIX#/', $table_prefix, $line)); |
|
| 317 | 317 | if (preg_match('/; *$/', $newline)) |
| 318 | 318 | { |
| 319 | 319 | $sql_req=$db_conn->prepare($newline); |
| 320 | 320 | if ($sql_req->execute() === false) { |
| 321 | - $this->logging->log('Error create schema : '.$newline,ERROR); |
|
| 321 | + $this->logging->log('Error create schema : '.$newline, ERROR); |
|
| 322 | 322 | return true; |
| 323 | 323 | } |
| 324 | 324 | $cur_table_array=array(); |
| 325 | - if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 325 | + if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/', $newline, $cur_table_array)) |
|
| 326 | 326 | { |
| 327 | - $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
| 327 | + $cur_table=$cur_table_array[1].' SQL table '.$cur_table_array[2]; |
|
| 328 | 328 | } |
| 329 | 329 | else |
| 330 | 330 | { |
| 331 | 331 | $cur_table='secret SQL stuff :-)'; |
| 332 | 332 | //$cur_table=$newline; |
| 333 | 333 | } |
| 334 | - $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
| 334 | + $this->logging->log('Doing : '.$cur_table, INFO); |
|
| 335 | 335 | |
| 336 | 336 | $newline=''; |
| 337 | 337 | } |
@@ -339,13 +339,13 @@ discard block |
||
| 339 | 339 | fclose($input_stream); |
| 340 | 340 | |
| 341 | 341 | $sql='UPDATE '.$this->dbPrefix.'db_config SET value='.$cur_version.' WHERE ( name=\'db_version\' )'; |
| 342 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 342 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
| 343 | 343 | if ($db_conn->query($sql) === false) { |
| 344 | - $this->logging->log('Cannot update db version. Query : ' . $sql,WARN); |
|
| 344 | + $this->logging->log('Cannot update db version. Query : '.$sql, WARN); |
|
| 345 | 345 | return true; |
| 346 | 346 | } |
| 347 | 347 | |
| 348 | - $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
| 348 | + $this->logging->log('Schema updated to version : '.$cur_version, INFO); |
|
| 349 | 349 | } |
| 350 | 350 | return false; |
| 351 | 351 | } |
@@ -357,33 +357,33 @@ discard block |
||
| 357 | 357 | * @param int $target_version target db version number |
| 358 | 358 | * @return string : return messages or 'ERROR'. |
| 359 | 359 | */ |
| 360 | - private function update_schema_message($prefix,$cur_version,$target_version) |
|
| 360 | + private function update_schema_message($prefix, $cur_version, $target_version) |
|
| 361 | 361 | { |
| 362 | 362 | |
| 363 | 363 | $message=''; |
| 364 | - $this->logging->log('getting message for upgrade',DEBUG ); |
|
| 365 | - while($cur_version<$target_version) |
|
| 364 | + $this->logging->log('getting message for upgrade', DEBUG); |
|
| 365 | + while ($cur_version < $target_version) |
|
| 366 | 366 | { |
| 367 | 367 | $cur_version++; |
| 368 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 368 | + $updateFile=$prefix.'v'.($cur_version - 1).'_v'.$cur_version.'.sql'; |
|
| 369 | 369 | $input_stream=fopen($updateFile, 'r'); |
| 370 | - if ($input_stream=== false) |
|
| 370 | + if ($input_stream === false) |
|
| 371 | 371 | { |
| 372 | - $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
| 372 | + $this->logging->log("Error reading update file ".$updateFile, 2, ''); |
|
| 373 | 373 | return 'ERROR'; |
| 374 | 374 | } |
| 375 | 375 | do |
| 376 | 376 | { |
| 377 | 377 | $line=fgets($input_stream); |
| 378 | 378 | } |
| 379 | - while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
| 379 | + while ($line !== false && !preg_match('/#MESSAGE/', $line)); |
|
| 380 | 380 | fclose($input_stream); |
| 381 | 381 | if ($line === false) |
| 382 | 382 | { |
| 383 | - $this->logging->log("No message in file ". $updateFile,2,''); |
|
| 383 | + $this->logging->log("No message in file ".$updateFile, 2, ''); |
|
| 384 | 384 | return ''; |
| 385 | 385 | } |
| 386 | - $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
| 386 | + $message.=($cur_version - 1).'->'.$cur_version.' : '.preg_replace('/#MESSAGE : /', '', $line)."\n"; |
|
| 387 | 387 | } |
| 388 | 388 | return $message; |
| 389 | 389 | } |
@@ -153,8 +153,7 @@ discard block |
||
| 153 | 153 | if ($value != null && isset($value['id'])) |
| 154 | 154 | { // Entry exists -> update |
| 155 | 155 | $sql='UPDATE '.$this->dbPrefix.'db_config SET value = \''.$element.'\' WHERE (id = '.$value['id'].')'; |
| 156 | - } |
|
| 157 | - else |
|
| 156 | + } else |
|
| 158 | 157 | { // Entry does no exists -> create |
| 159 | 158 | $sql='INSERT INTO '.$this->dbPrefix.'db_config (name,value) VALUES (\''.$name.'\' , \''.$element.'\' )'; |
| 160 | 159 | } |
@@ -222,8 +221,7 @@ discard block |
||
| 222 | 221 | if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
| 223 | 222 | { |
| 224 | 223 | $cur_table='table '.$cur_table_array[1]; |
| 225 | - } |
|
| 226 | - else |
|
| 224 | + } else |
|
| 227 | 225 | { |
| 228 | 226 | $cur_table='secret SQL stuff :-)'; |
| 229 | 227 | } |
@@ -267,8 +265,7 @@ discard block |
||
| 267 | 265 | if ($this->trapDBType == 'pgsql') |
| 268 | 266 | { |
| 269 | 267 | $prefix .= 'update_pgsql/schema_'; |
| 270 | - } |
|
| 271 | - else |
|
| 268 | + } else |
|
| 272 | 269 | { |
| 273 | 270 | $prefix .= 'update_sql/schema_'; |
| 274 | 271 | } |
@@ -312,7 +309,10 @@ discard block |
||
| 312 | 309 | $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
| 313 | 310 | while (($line=fgets($input_stream)) !== false) |
| 314 | 311 | { |
| 315 | - if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
| 312 | + if (preg_match('/^#/', $line)) { |
|
| 313 | + continue; |
|
| 314 | + } |
|
| 315 | + // ignore comment lines |
|
| 316 | 316 | $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
| 317 | 317 | if (preg_match('/; *$/', $newline)) |
| 318 | 318 | { |
@@ -325,8 +325,7 @@ discard block |
||
| 325 | 325 | if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
| 326 | 326 | { |
| 327 | 327 | $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
| 328 | - } |
|
| 329 | - else |
|
| 328 | + } else |
|
| 330 | 329 | { |
| 331 | 330 | $cur_table='secret SQL stuff :-)'; |
| 332 | 331 | //$cur_table=$newline; |
@@ -21,8 +21,8 @@ discard block |
||
| 21 | 21 | // Filters |
| 22 | 22 | |
| 23 | 23 | protected $filter; |
| 24 | - protected $enforcedFilters = array(); |
|
| 25 | - protected $searchColumns = array(); |
|
| 24 | + protected $enforcedFilters=array(); |
|
| 25 | + protected $searchColumns=array(); |
|
| 26 | 26 | |
| 27 | 27 | protected function getTitles() { |
| 28 | 28 | // TODO : check moduleconfig is set |
@@ -38,60 +38,60 @@ discard block |
||
| 38 | 38 | public function render() |
| 39 | 39 | { |
| 40 | 40 | $data=$this->getTable(); |
| 41 | - $view = $this->getView(); |
|
| 42 | - $this->columnCount = count($this->getTitles()); |
|
| 41 | + $view=$this->getView(); |
|
| 42 | + $this->columnCount=count($this->getTitles()); |
|
| 43 | 43 | $this->lastDay=null; |
| 44 | 44 | // Table start |
| 45 | - $htm = '<table class="simple common-table table-row-selectable">'; |
|
| 45 | + $htm='<table class="simple common-table table-row-selectable">'; |
|
| 46 | 46 | |
| 47 | 47 | // Titles |
| 48 | - $htm .= "<thead>\n <tr>\n"; |
|
| 49 | - $titles = $this->getTitles(); |
|
| 48 | + $htm.="<thead>\n <tr>\n"; |
|
| 49 | + $titles=$this->getTitles(); |
|
| 50 | 50 | foreach ($titles as $title) |
| 51 | 51 | { |
| 52 | - $htm .= ' <th>' . $view->escape($view->translate($title)) . "</th>\n"; |
|
| 52 | + $htm.=' <th>'.$view->escape($view->translate($title))."</th>\n"; |
|
| 53 | 53 | } |
| 54 | - $htm .= " </tr>\n</thead>\n"; |
|
| 54 | + $htm.=" </tr>\n</thead>\n"; |
|
| 55 | 55 | |
| 56 | 56 | // Rows |
| 57 | - $htm .= "<tbody>\n"; |
|
| 57 | + $htm.="<tbody>\n"; |
|
| 58 | 58 | |
| 59 | 59 | foreach ($data as $row) |
| 60 | 60 | { |
| 61 | - $firstCol = true; |
|
| 61 | + $firstCol=true; |
|
| 62 | 62 | // Put date header |
| 63 | - $htm .= $this->renderDayIfNew($row->timestamp); |
|
| 63 | + $htm.=$this->renderDayIfNew($row->timestamp); |
|
| 64 | 64 | |
| 65 | 65 | |
| 66 | 66 | // Render row |
| 67 | - $htm .= '<tr>'; |
|
| 68 | - foreach ( $titles as $rowkey => $title) |
|
| 67 | + $htm.='<tr>'; |
|
| 68 | + foreach ($titles as $rowkey => $title) |
|
| 69 | 69 | { |
| 70 | 70 | // Check missing value |
| 71 | 71 | if (property_exists($row, $rowkey)) |
| 72 | 72 | { |
| 73 | - $val = ($rowkey=='timestamp') ? strftime('%T',$row->$rowkey) : $row->$rowkey; |
|
| 73 | + $val=($rowkey == 'timestamp') ? strftime('%T', $row->$rowkey) : $row->$rowkey; |
|
| 74 | 74 | } else { |
| 75 | - $val = '-'; |
|
| 75 | + $val='-'; |
|
| 76 | 76 | } |
| 77 | 77 | if ($firstCol == true) { // Put link in first column for trap detail. |
| 78 | - $htm .= '<td>' |
|
| 78 | + $htm.='<td>' |
|
| 79 | 79 | . $view->qlink( |
| 80 | 80 | $view->escape($val), |
| 81 | 81 | Url::fromPath( |
| 82 | - $this->moduleConfig->urlPath() . '/received/trapdetail', |
|
| 82 | + $this->moduleConfig->urlPath().'/received/trapdetail', |
|
| 83 | 83 | array('id' => $row->id) |
| 84 | 84 | ) |
| 85 | 85 | ) |
| 86 | 86 | . '</td>'; |
| 87 | 87 | } else { |
| 88 | - $htm .= '<td>' . $view->escape($val) . '</td>'; |
|
| 88 | + $htm.='<td>'.$view->escape($val).'</td>'; |
|
| 89 | 89 | } |
| 90 | 90 | $firstCol=false; |
| 91 | 91 | } |
| 92 | - $htm .= "<tr>\n"; |
|
| 92 | + $htm.="<tr>\n"; |
|
| 93 | 93 | } |
| 94 | - $htm .= "</tbody></table>\n"; |
|
| 94 | + $htm.="</tbody></table>\n"; |
|
| 95 | 95 | //$htm .= "Filter : " . $this->filter."<br>\n"; |
| 96 | 96 | return $htm; |
| 97 | 97 | |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | { |
| 102 | 102 | $db=$this->db(); |
| 103 | 103 | |
| 104 | - $query = $db->select()->from( |
|
| 104 | + $query=$db->select()->from( |
|
| 105 | 105 | $this->moduleConfig->getTrapTableName(), |
| 106 | 106 | array('COUNT(*)') |
| 107 | 107 | ); |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | |
| 113 | 113 | public function getPaginator() |
| 114 | 114 | { |
| 115 | - $paginator = new Paginator(); |
|
| 115 | + $paginator=new Paginator(); |
|
| 116 | 116 | $paginator->setQuery($this); |
| 117 | 117 | |
| 118 | 118 | return $paginator; |
@@ -124,7 +124,7 @@ discard block |
||
| 124 | 124 | { |
| 125 | 125 | $db=$this->db(); |
| 126 | 126 | |
| 127 | - $query = $this->getBaseQuery(); |
|
| 127 | + $query=$this->getBaseQuery(); |
|
| 128 | 128 | $this->applyFiltersToQuery($query); |
| 129 | 129 | if ($this->hasLimit() || $this->hasOffset()) { |
| 130 | 130 | $query->limit($this->getLimit(), $this->getOffset()); |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | { |
| 138 | 138 | $db=$this->db(); |
| 139 | 139 | |
| 140 | - $query = $db->select()->from( |
|
| 140 | + $query=$db->select()->from( |
|
| 141 | 141 | $this->moduleConfig->getTrapTableName(), |
| 142 | 142 | $this->moduleConfig->getTrapListDisplayColumns() |
| 143 | 143 | )->order('timestamp DESC'); |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | protected $filter_Handler; |
| 151 | 151 | protected $filter_query=''; |
| 152 | 152 | protected $filter_done=''; |
| 153 | - protected $filter_query_list=array('q','done'); |
|
| 153 | + protected $filter_query_list=array('q', 'done'); |
|
| 154 | 154 | public function renderFilterHTML() |
| 155 | 155 | { |
| 156 | 156 | $htm=' <form id="filter" name="mainFilter" |
@@ -160,17 +160,17 @@ discard block |
||
| 160 | 160 | $htm.='<input type="text" name="q" title="Search is simple! Try to combine multiple words" |
| 161 | 161 | placeholder="Search..." class="search" value="'.$this->filter_query.'">'; |
| 162 | 162 | $htm.='<input type="checkbox" id="checkbox_done" name="done" value="1" class="autosubmit" '; |
| 163 | - if ($this->filter_done == 1) { $htm.=' checked ';} |
|
| 163 | + if ($this->filter_done == 1) { $htm.=' checked '; } |
|
| 164 | 164 | $htm.='> <label for="checkbox_done">Hide processed traps</label>'; |
| 165 | 165 | $htm.='</form>'; |
| 166 | 166 | return $htm; |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | - public function updateFilter($handler,$filter) |
|
| 169 | + public function updateFilter($handler, $filter) |
|
| 170 | 170 | { |
| 171 | 171 | $this->filter_Handler=$handler->remove($this->filter_query_list)->__toString(); |
| 172 | - $this->filter_query=(isset($filter['q']))?$this->filter_query=$filter['q']:''; |
|
| 173 | - $this->filter_done=(isset($filter['done']))?$this->filter_done=$filter['done']:0; |
|
| 172 | + $this->filter_query=(isset($filter['q'])) ? $this->filter_query=$filter['q'] : ''; |
|
| 173 | + $this->filter_done=(isset($filter['done'])) ? $this->filter_done=$filter['done'] : 0; |
|
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | protected function getSearchColumns() |
@@ -185,20 +185,20 @@ discard block |
||
| 185 | 185 | |
| 186 | 186 | public function setFilter($filter) |
| 187 | 187 | { |
| 188 | - $this->filter = $filter; |
|
| 188 | + $this->filter=$filter; |
|
| 189 | 189 | return $this; |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | public function getFilterEditor(Request $request) |
| 193 | 193 | { |
| 194 | - $filterEditor = Widget::create('filterEditor') |
|
| 194 | + $filterEditor=Widget::create('filterEditor') |
|
| 195 | 195 | ->setColumns(array_keys($this->getColumns())) |
| 196 | 196 | ->setSearchColumns(array_keys($this->getSearchColumns())) |
| 197 | 197 | ->preserveParams('limit', 'sort', 'dir', 'view', 'backend') |
| 198 | 198 | ->ignoreParams('page') |
| 199 | 199 | ->handleRequest($request); |
| 200 | 200 | |
| 201 | - $filter = $filterEditor->getFilter(); |
|
| 201 | + $filter=$filterEditor->getFilter(); |
|
| 202 | 202 | $this->setFilter($filter); |
| 203 | 203 | |
| 204 | 204 | return $filterEditor; |
@@ -212,9 +212,9 @@ discard block |
||
| 212 | 212 | { |
| 213 | 213 | $sql.='('; |
| 214 | 214 | $first=1; |
| 215 | - foreach($this->moduleConfig->getTrapListSearchColumns() as $column) |
|
| 215 | + foreach ($this->moduleConfig->getTrapListSearchColumns() as $column) |
|
| 216 | 216 | { |
| 217 | - if ($first==0) $sql.=' OR '; |
|
| 217 | + if ($first == 0) $sql.=' OR '; |
|
| 218 | 218 | $first=0; |
| 219 | 219 | $sql.=" CAST(".$column." AS char(100)) LIKE '%".$this->filter_query."%' "; |
| 220 | 220 | } |
@@ -18,9 +18,9 @@ discard block |
||
| 18 | 18 | |
| 19 | 19 | // Filters |
| 20 | 20 | |
| 21 | - protected $filter; |
|
| 22 | - protected $enforcedFilters = array(); |
|
| 23 | - protected $searchColumns = array(); |
|
| 21 | + protected $filter; |
|
| 22 | + protected $enforcedFilters = array(); |
|
| 23 | + protected $searchColumns = array(); |
|
| 24 | 24 | |
| 25 | 25 | protected function getTitles() { |
| 26 | 26 | // TODO : check moduleconfig is set |
@@ -28,10 +28,10 @@ discard block |
||
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | // ****************** Render table in html |
| 31 | - public function __toString() |
|
| 32 | - { |
|
| 33 | - return $this->render(); |
|
| 34 | - } |
|
| 31 | + public function __toString() |
|
| 32 | + { |
|
| 33 | + return $this->render(); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | 36 | public function render() |
| 37 | 37 | { |
@@ -95,26 +95,26 @@ discard block |
||
| 95 | 95 | |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - public function count() |
|
| 99 | - { |
|
| 100 | - $db=$this->db(); |
|
| 98 | + public function count() |
|
| 99 | + { |
|
| 100 | + $db=$this->db(); |
|
| 101 | 101 | |
| 102 | 102 | $query = $db->select()->from( |
| 103 | - $this->moduleConfig->getTrapTableName(), |
|
| 104 | - array('COUNT(*)') |
|
| 105 | - ); |
|
| 103 | + $this->moduleConfig->getTrapTableName(), |
|
| 104 | + array('COUNT(*)') |
|
| 105 | + ); |
|
| 106 | 106 | $this->applyFiltersToQuery($query); |
| 107 | 107 | |
| 108 | - return $db->fetchOne($query); |
|
| 109 | - } |
|
| 108 | + return $db->fetchOne($query); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - public function getPaginator() |
|
| 112 | - { |
|
| 113 | - $paginator = new Paginator(); |
|
| 114 | - $paginator->setQuery($this); |
|
| 111 | + public function getPaginator() |
|
| 112 | + { |
|
| 113 | + $paginator = new Paginator(); |
|
| 114 | + $paginator->setQuery($this); |
|
| 115 | 115 | |
| 116 | - return $paginator; |
|
| 117 | - } |
|
| 116 | + return $paginator; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | 119 | // ****************** DB connection and query |
| 120 | 120 | |
@@ -124,24 +124,24 @@ discard block |
||
| 124 | 124 | |
| 125 | 125 | $query = $this->getBaseQuery(); |
| 126 | 126 | $this->applyFiltersToQuery($query); |
| 127 | - if ($this->hasLimit() || $this->hasOffset()) { |
|
| 128 | - $query->limit($this->getLimit(), $this->getOffset()); |
|
| 129 | - } |
|
| 127 | + if ($this->hasLimit() || $this->hasOffset()) { |
|
| 128 | + $query->limit($this->getLimit(), $this->getOffset()); |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | 131 | return $db->fetchAll($query); |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | - public function getBaseQuery() |
|
| 135 | - { |
|
| 134 | + public function getBaseQuery() |
|
| 135 | + { |
|
| 136 | 136 | $db=$this->db(); |
| 137 | 137 | |
| 138 | 138 | $query = $db->select()->from( |
| 139 | - $this->moduleConfig->getTrapTableName(), |
|
| 140 | - $this->moduleConfig->getTrapListDisplayColumns() |
|
| 141 | - )->order('timestamp DESC'); |
|
| 139 | + $this->moduleConfig->getTrapTableName(), |
|
| 140 | + $this->moduleConfig->getTrapListDisplayColumns() |
|
| 141 | + )->order('timestamp DESC'); |
|
| 142 | 142 | |
| 143 | - return $query; |
|
| 144 | - } |
|
| 143 | + return $query; |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | 146 | // ****************** Filters |
| 147 | 147 | |
@@ -171,39 +171,39 @@ discard block |
||
| 171 | 171 | $this->filter_done=(isset($filter['done']))?$this->filter_done=$filter['done']:0; |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - protected function getSearchColumns() |
|
| 175 | - { |
|
| 176 | - return $this->getColumns(); |
|
| 177 | - } |
|
| 174 | + protected function getSearchColumns() |
|
| 175 | + { |
|
| 176 | + return $this->getColumns(); |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | 179 | public function getColumns() |
| 180 | 180 | { |
| 181 | 181 | return $this->moduleConfig->getTrapListDisplayColumns(); |
| 182 | 182 | } |
| 183 | 183 | |
| 184 | - public function setFilter($filter) |
|
| 185 | - { |
|
| 186 | - $this->filter = $filter; |
|
| 187 | - return $this; |
|
| 188 | - } |
|
| 184 | + public function setFilter($filter) |
|
| 185 | + { |
|
| 186 | + $this->filter = $filter; |
|
| 187 | + return $this; |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | 190 | public function getFilterEditor(Request $request) |
| 191 | - { |
|
| 192 | - $filterEditor = Widget::create('filterEditor') |
|
| 193 | - ->setColumns(array_keys($this->getColumns())) |
|
| 194 | - ->setSearchColumns(array_keys($this->getSearchColumns())) |
|
| 195 | - ->preserveParams('limit', 'sort', 'dir', 'view', 'backend') |
|
| 196 | - ->ignoreParams('page') |
|
| 197 | - ->handleRequest($request); |
|
| 191 | + { |
|
| 192 | + $filterEditor = Widget::create('filterEditor') |
|
| 193 | + ->setColumns(array_keys($this->getColumns())) |
|
| 194 | + ->setSearchColumns(array_keys($this->getSearchColumns())) |
|
| 195 | + ->preserveParams('limit', 'sort', 'dir', 'view', 'backend') |
|
| 196 | + ->ignoreParams('page') |
|
| 197 | + ->handleRequest($request); |
|
| 198 | 198 | |
| 199 | - $filter = $filterEditor->getFilter(); |
|
| 200 | - $this->setFilter($filter); |
|
| 199 | + $filter = $filterEditor->getFilter(); |
|
| 200 | + $this->setFilter($filter); |
|
| 201 | 201 | |
| 202 | - return $filterEditor; |
|
| 203 | - } |
|
| 202 | + return $filterEditor; |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | - protected function applyFiltersToQuery($query) |
|
| 206 | - { |
|
| 205 | + protected function applyFiltersToQuery($query) |
|
| 206 | + { |
|
| 207 | 207 | |
| 208 | 208 | $sql=''; |
| 209 | 209 | if ($this->filter_query != '') |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | $sql.="(status != 'done')"; |
| 225 | 225 | } |
| 226 | 226 | if ($sql != '') $query->where($sql); |
| 227 | - return $query; |
|
| 228 | - } |
|
| 227 | + return $query; |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | 230 | } |
| 231 | 231 | \ No newline at end of file |
@@ -212,7 +212,9 @@ discard block |
||
| 212 | 212 | $first=1; |
| 213 | 213 | foreach($this->moduleConfig->getTrapListSearchColumns() as $column) |
| 214 | 214 | { |
| 215 | - if ($first==0) $sql.=' OR '; |
|
| 215 | + if ($first==0) { |
|
| 216 | + $sql.=' OR '; |
|
| 217 | + } |
|
| 216 | 218 | $first=0; |
| 217 | 219 | $sql.=" CAST(".$column." AS char(100)) LIKE '%".$this->filter_query."%' "; |
| 218 | 220 | } |
@@ -220,10 +222,14 @@ discard block |
||
| 220 | 222 | } |
| 221 | 223 | if ($this->filter_done == 1) |
| 222 | 224 | { |
| 223 | - if ($sql != '') $sql.=' AND '; |
|
| 225 | + if ($sql != '') { |
|
| 226 | + $sql.=' AND '; |
|
| 227 | + } |
|
| 224 | 228 | $sql.="(status != 'done')"; |
| 225 | 229 | } |
| 226 | - if ($sql != '') $query->where($sql); |
|
| 230 | + if ($sql != '') { |
|
| 231 | + $query->where($sql); |
|
| 232 | + } |
|
| 227 | 233 | return $query; |
| 228 | 234 | } |
| 229 | 235 | |
@@ -21,104 +21,104 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class NetworkRule extends PluginTemplate |
| 23 | 23 | { |
| 24 | - /** @var string $description Description of plugin */ |
|
| 25 | - public $description='Network functions to use into rules |
|
| 24 | + /** @var string $description Description of plugin */ |
|
| 25 | + public $description='Network functions to use into rules |
|
| 26 | 26 | test test test'; |
| 27 | 27 | |
| 28 | - /** @var array[] $functions Functions of this plugin for rule eval. |
|
| 29 | - * If no functions are declared, set to empty array |
|
| 30 | - * $functions[<name>]['function'] : Name of the function to be called in this class |
|
| 31 | - * $functions[<name>]['params'] : Description of input parameters of function. |
|
| 32 | - * $functions[<name>]['description'] : Description. Can be multiline. |
|
| 33 | - */ |
|
| 34 | - public $functions=array( |
|
| 35 | - 'inNetwork' => array( // The name of the function in rules |
|
| 36 | - 'function' => 'isInNetwork', // Name of the function |
|
| 37 | - 'params' => '<IP to test>,<Network IP>,<Network mask (CIDR)>', // parameters description |
|
| 38 | - 'description' => 'Test if IP is in network, ex : __inNetwork(192.168.123.5,192.168.123.0,24) returns true |
|
| 28 | + /** @var array[] $functions Functions of this plugin for rule eval. |
|
| 29 | + * If no functions are declared, set to empty array |
|
| 30 | + * $functions[<name>]['function'] : Name of the function to be called in this class |
|
| 31 | + * $functions[<name>]['params'] : Description of input parameters of function. |
|
| 32 | + * $functions[<name>]['description'] : Description. Can be multiline. |
|
| 33 | + */ |
|
| 34 | + public $functions=array( |
|
| 35 | + 'inNetwork' => array( // The name of the function in rules |
|
| 36 | + 'function' => 'isInNetwork', // Name of the function |
|
| 37 | + 'params' => '<IP to test>,<Network IP>,<Network mask (CIDR)>', // parameters description |
|
| 38 | + 'description' => 'Test if IP is in network, ex : __inNetwork(192.168.123.5,192.168.123.0,24) returns true |
|
| 39 | 39 | Does not work with IPV6' // Description (can be multiline). |
| 40 | - ), |
|
| 41 | - 'test' => array( // The name of the function in rules |
|
| 42 | - 'function' => 'testParam', // Name of the function |
|
| 43 | - 'params' => '<boolean to return as string>', // parameters description |
|
| 44 | - 'description' => 'Returns value passed as argument' // Description (can be multiline). |
|
| 45 | - ) |
|
| 46 | - ); |
|
| 40 | + ), |
|
| 41 | + 'test' => array( // The name of the function in rules |
|
| 42 | + 'function' => 'testParam', // Name of the function |
|
| 43 | + 'params' => '<boolean to return as string>', // parameters description |
|
| 44 | + 'description' => 'Returns value passed as argument' // Description (can be multiline). |
|
| 45 | + ) |
|
| 46 | + ); |
|
| 47 | 47 | |
| 48 | - /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin NOT IMPLEMENTED */ |
|
| 49 | - public $catchAllTraps=false; |
|
| 48 | + /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin NOT IMPLEMENTED */ |
|
| 49 | + public $catchAllTraps=false; |
|
| 50 | 50 | |
| 51 | - /** @var boolean $processTraps Set to true if plugins can handle traps NOT IMPLEMENTED */ |
|
| 52 | - public $processTraps=false; |
|
| 51 | + /** @var boolean $processTraps Set to true if plugins can handle traps NOT IMPLEMENTED */ |
|
| 52 | + public $processTraps=false; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Constructor. Can throw exceptions on error, but no logging at this point. |
|
| 56 | - * @throws \Exception |
|
| 57 | - * @return \Trapdirector\Plugins\NetworkRule |
|
| 58 | - */ |
|
| 59 | - function __construct() |
|
| 60 | - { |
|
| 61 | - $this->name=basename(__FILE__,'.php'); |
|
| 62 | - return $this; |
|
| 63 | - } |
|
| 54 | + /** |
|
| 55 | + * Constructor. Can throw exceptions on error, but no logging at this point. |
|
| 56 | + * @throws \Exception |
|
| 57 | + * @return \Trapdirector\Plugins\NetworkRule |
|
| 58 | + */ |
|
| 59 | + function __construct() |
|
| 60 | + { |
|
| 61 | + $this->name=basename(__FILE__,'.php'); |
|
| 62 | + return $this; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Function called by trapdirector if found in rules |
|
| 67 | - * Parameters check has to be done in function. |
|
| 68 | - * @param array $params Function parameters |
|
| 69 | - * @throws Exception |
|
| 70 | - * @return bool Evaluation |
|
| 71 | - */ |
|
| 72 | - public function isInNetwork(array $params) : bool |
|
| 73 | - { |
|
| 74 | - // Check param numbers and thrown exception if not correct. |
|
| 75 | - if (count($params)!=3) |
|
| 76 | - { |
|
| 77 | - throw new Exception('Invalid number of parameters : ' . count($params)); |
|
| 78 | - } |
|
| 65 | + /** |
|
| 66 | + * Function called by trapdirector if found in rules |
|
| 67 | + * Parameters check has to be done in function. |
|
| 68 | + * @param array $params Function parameters |
|
| 69 | + * @throws Exception |
|
| 70 | + * @return bool Evaluation |
|
| 71 | + */ |
|
| 72 | + public function isInNetwork(array $params) : bool |
|
| 73 | + { |
|
| 74 | + // Check param numbers and thrown exception if not correct. |
|
| 75 | + if (count($params)!=3) |
|
| 76 | + { |
|
| 77 | + throw new Exception('Invalid number of parameters : ' . count($params)); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - $ip = $params[0]; |
|
| 81 | - $net = $params[1]; |
|
| 82 | - $masq = $params[2]; |
|
| 80 | + $ip = $params[0]; |
|
| 81 | + $net = $params[1]; |
|
| 82 | + $masq = $params[2]; |
|
| 83 | 83 | |
| 84 | 84 | |
| 85 | - $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG); |
|
| 85 | + $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG); |
|
| 86 | 86 | |
| 87 | - $ip2 = ip2long($ip); |
|
| 88 | - $net2 = ip2long($net); |
|
| 87 | + $ip2 = ip2long($ip); |
|
| 88 | + $net2 = ip2long($net); |
|
| 89 | 89 | |
| 90 | - if ($ip2 === false ) |
|
| 91 | - { |
|
| 92 | - $this->log('Invalid IP : #' . $ip.'#',WARN); |
|
| 93 | - throw new Exception('Invalid IP'); |
|
| 94 | - } |
|
| 95 | - if ($net2 === false) |
|
| 96 | - { |
|
| 97 | - $this->log('Invalid network',WARN); |
|
| 98 | - throw new Exception('Invalid net'); |
|
| 99 | - } |
|
| 100 | - if ($masq<1 || $masq > 32) |
|
| 101 | - { |
|
| 102 | - $this->log('Invalid masq',WARN); |
|
| 103 | - throw new Exception('Invalid net masq'); |
|
| 104 | - } |
|
| 105 | - // $range is in IP/CIDR format eg 127.0.0.1/24 |
|
| 90 | + if ($ip2 === false ) |
|
| 91 | + { |
|
| 92 | + $this->log('Invalid IP : #' . $ip.'#',WARN); |
|
| 93 | + throw new Exception('Invalid IP'); |
|
| 94 | + } |
|
| 95 | + if ($net2 === false) |
|
| 96 | + { |
|
| 97 | + $this->log('Invalid network',WARN); |
|
| 98 | + throw new Exception('Invalid net'); |
|
| 99 | + } |
|
| 100 | + if ($masq<1 || $masq > 32) |
|
| 101 | + { |
|
| 102 | + $this->log('Invalid masq',WARN); |
|
| 103 | + throw new Exception('Invalid net masq'); |
|
| 104 | + } |
|
| 105 | + // $range is in IP/CIDR format eg 127.0.0.1/24 |
|
| 106 | 106 | |
| 107 | - $masq = pow( 2, ( 32 - $masq ) ) - 1; |
|
| 108 | - $masq = ~ $masq; |
|
| 109 | - return ( ( $ip2 & $masq ) == ( $net2 & $masq ) ); |
|
| 107 | + $masq = pow( 2, ( 32 - $masq ) ) - 1; |
|
| 108 | + $masq = ~ $masq; |
|
| 109 | + return ( ( $ip2 & $masq ) == ( $net2 & $masq ) ); |
|
| 110 | 110 | |
| 111 | - } |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - public function testParam(array $param) |
|
| 114 | - { |
|
| 115 | - if (count($param)!=1) |
|
| 116 | - { |
|
| 117 | - throw new Exception('Invalid number of parameters : ' . count($param)); |
|
| 118 | - } |
|
| 119 | - if ($param[0] == 'true') return true; |
|
| 120 | - return false; |
|
| 121 | - } |
|
| 113 | + public function testParam(array $param) |
|
| 114 | + { |
|
| 115 | + if (count($param)!=1) |
|
| 116 | + { |
|
| 117 | + throw new Exception('Invalid number of parameters : ' . count($param)); |
|
| 118 | + } |
|
| 119 | + if ($param[0] == 'true') return true; |
|
| 120 | + return false; |
|
| 121 | + } |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | */ |
| 59 | 59 | function __construct() |
| 60 | 60 | { |
| 61 | - $this->name=basename(__FILE__,'.php'); |
|
| 61 | + $this->name=basename(__FILE__, '.php'); |
|
| 62 | 62 | return $this; |
| 63 | 63 | } |
| 64 | 64 | |
@@ -72,49 +72,49 @@ discard block |
||
| 72 | 72 | public function isInNetwork(array $params) : bool |
| 73 | 73 | { |
| 74 | 74 | // Check param numbers and thrown exception if not correct. |
| 75 | - if (count($params)!=3) |
|
| 75 | + if (count($params) != 3) |
|
| 76 | 76 | { |
| 77 | - throw new Exception('Invalid number of parameters : ' . count($params)); |
|
| 77 | + throw new Exception('Invalid number of parameters : '.count($params)); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - $ip = $params[0]; |
|
| 81 | - $net = $params[1]; |
|
| 82 | - $masq = $params[2]; |
|
| 80 | + $ip=$params[0]; |
|
| 81 | + $net=$params[1]; |
|
| 82 | + $masq=$params[2]; |
|
| 83 | 83 | |
| 84 | 84 | |
| 85 | - $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG); |
|
| 85 | + $this->log('#'.$ip.'# / #'.$net.'# / #'.$masq, DEBUG); |
|
| 86 | 86 | |
| 87 | - $ip2 = ip2long($ip); |
|
| 88 | - $net2 = ip2long($net); |
|
| 87 | + $ip2=ip2long($ip); |
|
| 88 | + $net2=ip2long($net); |
|
| 89 | 89 | |
| 90 | - if ($ip2 === false ) |
|
| 90 | + if ($ip2 === false) |
|
| 91 | 91 | { |
| 92 | - $this->log('Invalid IP : #' . $ip.'#',WARN); |
|
| 92 | + $this->log('Invalid IP : #'.$ip.'#', WARN); |
|
| 93 | 93 | throw new Exception('Invalid IP'); |
| 94 | 94 | } |
| 95 | 95 | if ($net2 === false) |
| 96 | 96 | { |
| 97 | - $this->log('Invalid network',WARN); |
|
| 97 | + $this->log('Invalid network', WARN); |
|
| 98 | 98 | throw new Exception('Invalid net'); |
| 99 | 99 | } |
| 100 | - if ($masq<1 || $masq > 32) |
|
| 100 | + if ($masq < 1 || $masq > 32) |
|
| 101 | 101 | { |
| 102 | - $this->log('Invalid masq',WARN); |
|
| 102 | + $this->log('Invalid masq', WARN); |
|
| 103 | 103 | throw new Exception('Invalid net masq'); |
| 104 | 104 | } |
| 105 | 105 | // $range is in IP/CIDR format eg 127.0.0.1/24 |
| 106 | 106 | |
| 107 | - $masq = pow( 2, ( 32 - $masq ) ) - 1; |
|
| 108 | - $masq = ~ $masq; |
|
| 109 | - return ( ( $ip2 & $masq ) == ( $net2 & $masq ) ); |
|
| 107 | + $masq=pow(2, (32 - $masq)) - 1; |
|
| 108 | + $masq=~ $masq; |
|
| 109 | + return (($ip2 & $masq) == ($net2 & $masq)); |
|
| 110 | 110 | |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | public function testParam(array $param) |
| 114 | 114 | { |
| 115 | - if (count($param)!=1) |
|
| 115 | + if (count($param) != 1) |
|
| 116 | 116 | { |
| 117 | - throw new Exception('Invalid number of parameters : ' . count($param)); |
|
| 117 | + throw new Exception('Invalid number of parameters : '.count($param)); |
|
| 118 | 118 | } |
| 119 | 119 | if ($param[0] == 'true') return true; |
| 120 | 120 | return false; |
@@ -116,7 +116,9 @@ |
||
| 116 | 116 | { |
| 117 | 117 | throw new Exception('Invalid number of parameters : ' . count($param)); |
| 118 | 118 | } |
| 119 | - if ($param[0] == 'true') return true; |
|
| 119 | + if ($param[0] == 'true') { |
|
| 120 | + return true; |
|
| 121 | + } |
|
| 120 | 122 | return false; |
| 121 | 123 | } |
| 122 | 124 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | * @param \Zend_Db_Adapter_Abstract $db current database |
| 22 | 22 | * @param TrapModuleConfig $config TrapModuleConfig class instance |
| 23 | 23 | */ |
| 24 | - public function __construct($snmptranslate,$snmptranslate_dirs,$db,$config) |
|
| 24 | + public function __construct($snmptranslate, $snmptranslate_dirs, $db, $config) |
|
| 25 | 25 | { |
| 26 | 26 | $this->snmptranslate=$snmptranslate; |
| 27 | 27 | $this->snmptranslate_dirs=$snmptranslate_dirs; |
@@ -38,19 +38,19 @@ discard block |
||
| 38 | 38 | |
| 39 | 39 | public function getMIBList() |
| 40 | 40 | { |
| 41 | - $dbconn = $this->db; |
|
| 41 | + $dbconn=$this->db; |
|
| 42 | 42 | $query=$dbconn->select() |
| 43 | 43 | ->distinct() |
| 44 | 44 | ->from( |
| 45 | 45 | $this->config->getMIBCacheTableName(), |
| 46 | 46 | array('mib' => 'mib')) |
| 47 | 47 | ->where("type = '21'") |
| 48 | - ->order('mib ASC'); ; |
|
| 48 | + ->order('mib ASC'); ; |
|
| 49 | 49 | $names=$dbconn->fetchAll($query); |
| 50 | 50 | $mib=array(); |
| 51 | - foreach($names as $val) |
|
| 51 | + foreach ($names as $val) |
|
| 52 | 52 | { |
| 53 | - array_push($mib,$val->mib); |
|
| 53 | + array_push($mib, $val->mib); |
|
| 54 | 54 | } |
| 55 | 55 | return $mib; |
| 56 | 56 | |
@@ -64,12 +64,12 @@ discard block |
||
| 64 | 64 | public function getTrapList($mib) |
| 65 | 65 | { |
| 66 | 66 | $traps=array(); |
| 67 | - $dbconn = $this->db; |
|
| 67 | + $dbconn=$this->db; |
|
| 68 | 68 | $query=$dbconn->select() |
| 69 | 69 | ->from( |
| 70 | 70 | $this->config->getMIBCacheTableName(), |
| 71 | 71 | array('name' => 'name', 'oid' => 'oid', 'description' => 'description')) |
| 72 | - ->where("mib = '".$mib."' AND type='21'") ; |
|
| 72 | + ->where("mib = '".$mib."' AND type='21'"); |
|
| 73 | 73 | $names=$dbconn->fetchAll($query); |
| 74 | 74 | foreach ($names as $val) |
| 75 | 75 | { |
@@ -87,19 +87,19 @@ discard block |
||
| 87 | 87 | $objects=array(); |
| 88 | 88 | |
| 89 | 89 | // Get trap id in DB |
| 90 | - $dbconn = $this->db; |
|
| 90 | + $dbconn=$this->db; |
|
| 91 | 91 | $query=$dbconn->select() |
| 92 | 92 | ->from( |
| 93 | 93 | $this->config->getMIBCacheTableName(), |
| 94 | 94 | array('id' => 'id')) |
| 95 | - ->where("oid = '".$trap."'") ; |
|
| 95 | + ->where("oid = '".$trap."'"); |
|
| 96 | 96 | $id=$dbconn->fetchRow($query); |
| 97 | - if ( ($id == null) || ! property_exists($id,'id') ) return null; |
|
| 97 | + if (($id == null) || !property_exists($id, 'id')) return null; |
|
| 98 | 98 | |
| 99 | 99 | $query=$dbconn->select() |
| 100 | 100 | ->from( |
| 101 | 101 | array('c' => $this->config->getMIBCacheTableName()), |
| 102 | - array('name' => 'c.name','mib' => 'c.mib','oid' => 'c.oid','type_enum'=>'c.type_enum', |
|
| 102 | + array('name' => 'c.name', 'mib' => 'c.mib', 'oid' => 'c.oid', 'type_enum'=>'c.type_enum', |
|
| 103 | 103 | 'type' => 'c.syntax', 'text_conv' => 'c.textual_convention', 'disp' => 'display_hint', |
| 104 | 104 | 'description' => 'c.description')) |
| 105 | 105 | ->join( |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | 'o.trap_id='.$id->id ) |
| 108 | 108 | ->where("o.object_id = c.id"); |
| 109 | 109 | $listObjects=$dbconn->fetchAll($query); |
| 110 | - if ( count($listObjects)==0 ) return null; |
|
| 110 | + if (count($listObjects) == 0) return null; |
|
| 111 | 111 | |
| 112 | 112 | foreach ($listObjects as $val) |
| 113 | 113 | { |
@@ -128,14 +128,14 @@ discard block |
||
| 128 | 128 | */ |
| 129 | 129 | public function translateOID($oid) |
| 130 | 130 | { |
| 131 | - if (!preg_match('/^\./',$oid)) $oid = '.' . $oid; // Add a leading '.' |
|
| 132 | - $retArray=array('oid' => $oid, 'mib' => null, 'name'=>null,'type'=>null); |
|
| 133 | - $dbconn = $this->db; |
|
| 131 | + if (!preg_match('/^\./', $oid)) $oid='.'.$oid; // Add a leading '.' |
|
| 132 | + $retArray=array('oid' => $oid, 'mib' => null, 'name'=>null, 'type'=>null); |
|
| 133 | + $dbconn=$this->db; |
|
| 134 | 134 | |
| 135 | 135 | $query=$dbconn->select() |
| 136 | 136 | ->from( |
| 137 | 137 | array('o' => $this->config->getMIBCacheTableName()), |
| 138 | - array('mib'=>'o.mib','name' => 'o.name','type'=>'o.syntax', |
|
| 138 | + array('mib'=>'o.mib', 'name' => 'o.name', 'type'=>'o.syntax', |
|
| 139 | 139 | 'type_enum'=>'o.type_enum', 'description'=>'o.description')) |
| 140 | 140 | ->where('o.oid=\''.$oid.'\''); |
| 141 | 141 | $object=$dbconn->fetchRow($query); |
@@ -151,18 +151,18 @@ discard block |
||
| 151 | 151 | |
| 152 | 152 | // Try to get oid name from snmptranslate |
| 153 | 153 | $matches=array(); |
| 154 | - $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 154 | + $translate=exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 155 | 155 | ' '.$oid); |
| 156 | - $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
| 157 | - if ($ret_code===0 || $ret_code===false) { |
|
| 156 | + $ret_code=preg_match('/(.*)::(.*)/', $translate, $matches); |
|
| 157 | + if ($ret_code === 0 || $ret_code === false) { |
|
| 158 | 158 | return null; |
| 159 | 159 | } |
| 160 | 160 | $retArray['mib']=$matches[1]; |
| 161 | 161 | $retArray['name']=$matches[2]; |
| 162 | 162 | |
| 163 | - $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.' -Td -On ' . $matches[0] . |
|
| 163 | + $translate=exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.' -Td -On '.$matches[0]. |
|
| 164 | 164 | " | grep SYNTAX | sed 's/SYNTAX[[:blank:]]*//'"); |
| 165 | - if (preg_match('/(.*)\{(.*)\}/',$translate,$matches)) |
|
| 165 | + if (preg_match('/(.*)\{(.*)\}/', $translate, $matches)) |
|
| 166 | 166 | { |
| 167 | 167 | $retArray['type']=$matches[1]; |
| 168 | 168 | $retArray['type_enum']=$matches[2]; |
@@ -197,9 +197,9 @@ discard block |
||
| 197 | 197 | * @param string $type filter by type (21=trap) |
| 198 | 198 | * @return number number of entries in db. |
| 199 | 199 | */ |
| 200 | - public function countObjects($mib=null,$type=null) |
|
| 200 | + public function countObjects($mib=null, $type=null) |
|
| 201 | 201 | { |
| 202 | - $dbconn = $this->db; |
|
| 202 | + $dbconn=$this->db; |
|
| 203 | 203 | $query=$dbconn->select() |
| 204 | 204 | ->from( |
| 205 | 205 | $this->config->getMIBCacheTableName(), |
@@ -207,11 +207,11 @@ discard block |
||
| 207 | 207 | $where=null; |
| 208 | 208 | if ($mib !== null) |
| 209 | 209 | { |
| 210 | - $where ="mib = '$mib' "; |
|
| 210 | + $where="mib = '$mib' "; |
|
| 211 | 211 | } |
| 212 | 212 | if ($type !== null) |
| 213 | 213 | { |
| 214 | - $where=($where !== null)?' AND ':''; |
|
| 214 | + $where=($where !== null) ? ' AND ' : ''; |
|
| 215 | 215 | $where.="type='$type'"; |
| 216 | 216 | } |
| 217 | 217 | if ($where !== null) |
@@ -227,10 +227,10 @@ discard block |
||
| 227 | 227 | * @param integer $id |
| 228 | 228 | * @return array trap details |
| 229 | 229 | */ |
| 230 | - public function getTrapDetails($oid=null,$id=null) |
|
| 230 | + public function getTrapDetails($oid=null, $id=null) |
|
| 231 | 231 | { |
| 232 | 232 | // Get trap id in DB |
| 233 | - if ($oid===null) |
|
| 233 | + if ($oid === null) |
|
| 234 | 234 | { |
| 235 | 235 | $where="c.id = '$id'"; |
| 236 | 236 | } |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | $query=$this->db->select() |
| 242 | 242 | ->from( |
| 243 | 243 | array('c' => $this->config->getMIBCacheTableName()), |
| 244 | - array('name' => 'c.name','mib' => 'c.mib','oid' => 'c.oid','type_enum'=>'c.type_enum', |
|
| 244 | + array('name' => 'c.name', 'mib' => 'c.mib', 'oid' => 'c.oid', 'type_enum'=>'c.type_enum', |
|
| 245 | 245 | 'type' => 'c.syntax', 'text_conv' => 'c.textual_convention', 'disp' => 'display_hint', |
| 246 | 246 | 'description' => 'c.description')) |
| 247 | 247 | ->where($where); |
@@ -94,7 +94,9 @@ discard block |
||
| 94 | 94 | array('id' => 'id')) |
| 95 | 95 | ->where("oid = '".$trap."'") ; |
| 96 | 96 | $id=$dbconn->fetchRow($query); |
| 97 | - if ( ($id == null) || ! property_exists($id,'id') ) return null; |
|
| 97 | + if ( ($id == null) || ! property_exists($id,'id') ) { |
|
| 98 | + return null; |
|
| 99 | + } |
|
| 98 | 100 | |
| 99 | 101 | $query=$dbconn->select() |
| 100 | 102 | ->from( |
@@ -107,7 +109,9 @@ discard block |
||
| 107 | 109 | 'o.trap_id='.$id->id ) |
| 108 | 110 | ->where("o.object_id = c.id"); |
| 109 | 111 | $listObjects=$dbconn->fetchAll($query); |
| 110 | - if ( count($listObjects)==0 ) return null; |
|
| 112 | + if ( count($listObjects)==0 ) { |
|
| 113 | + return null; |
|
| 114 | + } |
|
| 111 | 115 | |
| 112 | 116 | foreach ($listObjects as $val) |
| 113 | 117 | { |
@@ -128,7 +132,10 @@ discard block |
||
| 128 | 132 | */ |
| 129 | 133 | public function translateOID($oid) |
| 130 | 134 | { |
| 131 | - if (!preg_match('/^\./',$oid)) $oid = '.' . $oid; // Add a leading '.' |
|
| 135 | + if (!preg_match('/^\./',$oid)) { |
|
| 136 | + $oid = '.' . $oid; |
|
| 137 | + } |
|
| 138 | + // Add a leading '.' |
|
| 132 | 139 | $retArray=array('oid' => $oid, 'mib' => null, 'name'=>null,'type'=>null); |
| 133 | 140 | $dbconn = $this->db; |
| 134 | 141 | |
@@ -166,8 +173,7 @@ discard block |
||
| 166 | 173 | { |
| 167 | 174 | $retArray['type']=$matches[1]; |
| 168 | 175 | $retArray['type_enum']=$matches[2]; |
| 169 | - } |
|
| 170 | - else |
|
| 176 | + } else |
|
| 171 | 177 | { |
| 172 | 178 | $retArray['type']=$translate; |
| 173 | 179 | $retArray['type_enum']=''; |
@@ -233,8 +239,7 @@ discard block |
||
| 233 | 239 | if ($oid===null) |
| 234 | 240 | { |
| 235 | 241 | $where="c.id = '$id'"; |
| 236 | - } |
|
| 237 | - else |
|
| 242 | + } else |
|
| 238 | 243 | { |
| 239 | 244 | $where="c.oid = '$oid'"; |
| 240 | 245 | } |
@@ -31,10 +31,10 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Get all mibs in db which have at least one trap |
|
| 36 | - * @return array |
|
| 37 | - */ |
|
| 34 | + /** |
|
| 35 | + * Get all mibs in db which have at least one trap |
|
| 36 | + * @return array |
|
| 37 | + */ |
|
| 38 | 38 | |
| 39 | 39 | public function getMIBList() |
| 40 | 40 | { |
@@ -58,9 +58,9 @@ discard block |
||
| 58 | 58 | |
| 59 | 59 | |
| 60 | 60 | /** Get trap list from a mib |
| 61 | - * @param $mib string mib name |
|
| 62 | - * @return array : traps |
|
| 63 | - */ |
|
| 61 | + * @param $mib string mib name |
|
| 62 | + * @return array : traps |
|
| 63 | + */ |
|
| 64 | 64 | public function getTrapList($mib) |
| 65 | 65 | { |
| 66 | 66 | $traps=array(); |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | $query=$dbconn->select() |
| 69 | 69 | ->from( |
| 70 | 70 | $this->config->getMIBCacheTableName(), |
| 71 | - array('name' => 'name', 'oid' => 'oid', 'description' => 'description')) |
|
| 71 | + array('name' => 'name', 'oid' => 'oid', 'description' => 'description')) |
|
| 72 | 72 | ->where("mib = '".$mib."' AND type='21'") ; |
| 73 | 73 | $names=$dbconn->fetchAll($query); |
| 74 | 74 | foreach ($names as $val) |
@@ -79,9 +79,9 @@ discard block |
||
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | /** Get objects a trap can have |
| 82 | - * @param string $trap oid of trap |
|
| 83 | - * @return array|null : null if trap not found, or array ( <oid> => name/mib/type ) |
|
| 84 | - */ |
|
| 82 | + * @param string $trap oid of trap |
|
| 83 | + * @return array|null : null if trap not found, or array ( <oid> => name/mib/type ) |
|
| 84 | + */ |
|
| 85 | 85 | public function getObjectList($trap) |
| 86 | 86 | { |
| 87 | 87 | $objects=array(); |
@@ -97,15 +97,15 @@ discard block |
||
| 97 | 97 | if ( ($id == null) || ! property_exists($id,'id') ) return null; |
| 98 | 98 | |
| 99 | 99 | $query=$dbconn->select() |
| 100 | - ->from( |
|
| 101 | - array('c' => $this->config->getMIBCacheTableName()), |
|
| 102 | - array('name' => 'c.name','mib' => 'c.mib','oid' => 'c.oid','type_enum'=>'c.type_enum', |
|
| 103 | - 'type' => 'c.syntax', 'text_conv' => 'c.textual_convention', 'disp' => 'display_hint', |
|
| 104 | - 'description' => 'c.description')) |
|
| 105 | - ->join( |
|
| 106 | - array('o' => $this->config->getMIBCacheTableTrapObjName()), |
|
| 107 | - 'o.trap_id='.$id->id ) |
|
| 108 | - ->where("o.object_id = c.id"); |
|
| 100 | + ->from( |
|
| 101 | + array('c' => $this->config->getMIBCacheTableName()), |
|
| 102 | + array('name' => 'c.name','mib' => 'c.mib','oid' => 'c.oid','type_enum'=>'c.type_enum', |
|
| 103 | + 'type' => 'c.syntax', 'text_conv' => 'c.textual_convention', 'disp' => 'display_hint', |
|
| 104 | + 'description' => 'c.description')) |
|
| 105 | + ->join( |
|
| 106 | + array('o' => $this->config->getMIBCacheTableTrapObjName()), |
|
| 107 | + 'o.trap_id='.$id->id ) |
|
| 108 | + ->where("o.object_id = c.id"); |
|
| 109 | 109 | $listObjects=$dbconn->fetchAll($query); |
| 110 | 110 | if ( count($listObjects)==0 ) return null; |
| 111 | 111 | |
@@ -123,12 +123,12 @@ discard block |
||
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | /** translate oid in MIB::Name |
| 126 | - * @param string $oid |
|
| 127 | - * @return array|null : return array with index (oid -> oid, mib -> mib name, name -> oid name, type -> oid type) |
|
| 128 | - */ |
|
| 126 | + * @param string $oid |
|
| 127 | + * @return array|null : return array with index (oid -> oid, mib -> mib name, name -> oid name, type -> oid type) |
|
| 128 | + */ |
|
| 129 | 129 | public function translateOID($oid) |
| 130 | 130 | { |
| 131 | - if (!preg_match('/^\./',$oid)) $oid = '.' . $oid; // Add a leading '.' |
|
| 131 | + if (!preg_match('/^\./',$oid)) $oid = '.' . $oid; // Add a leading '.' |
|
| 132 | 132 | $retArray=array('oid' => $oid, 'mib' => null, 'name'=>null,'type'=>null); |
| 133 | 133 | $dbconn = $this->db; |
| 134 | 134 | |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | ->from( |
| 137 | 137 | array('o' => $this->config->getMIBCacheTableName()), |
| 138 | 138 | array('mib'=>'o.mib','name' => 'o.name','type'=>'o.syntax', |
| 139 | - 'type_enum'=>'o.type_enum', 'description'=>'o.description')) |
|
| 139 | + 'type_enum'=>'o.type_enum', 'description'=>'o.description')) |
|
| 140 | 140 | ->where('o.oid=\''.$oid.'\''); |
| 141 | 141 | $object=$dbconn->fetchRow($query); |
| 142 | 142 | if ($object != null) |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | // Try to get oid name from snmptranslate |
| 153 | 153 | $matches=array(); |
| 154 | 154 | $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
| 155 | - ' '.$oid); |
|
| 155 | + ' '.$oid); |
|
| 156 | 156 | $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
| 157 | 157 | if ($ret_code===0 || $ret_code===false) { |
| 158 | 158 | return null; |
@@ -164,8 +164,8 @@ discard block |
||
| 164 | 164 | " | grep SYNTAX | sed 's/SYNTAX[[:blank:]]*//'"); |
| 165 | 165 | if (preg_match('/(.*)\{(.*)\}/',$translate,$matches)) |
| 166 | 166 | { |
| 167 | - $retArray['type']=$matches[1]; |
|
| 168 | - $retArray['type_enum']=$matches[2]; |
|
| 167 | + $retArray['type']=$matches[1]; |
|
| 168 | + $retArray['type_enum']=$matches[2]; |
|
| 169 | 169 | } |
| 170 | 170 | else |
| 171 | 171 | { |
@@ -229,25 +229,25 @@ discard block |
||
| 229 | 229 | */ |
| 230 | 230 | public function getTrapDetails($oid=null,$id=null) |
| 231 | 231 | { |
| 232 | - // Get trap id in DB |
|
| 233 | - if ($oid===null) |
|
| 234 | - { |
|
| 235 | - $where="c.id = '$id'"; |
|
| 236 | - } |
|
| 237 | - else |
|
| 238 | - { |
|
| 239 | - $where="c.oid = '$oid'"; |
|
| 240 | - } |
|
| 241 | - $query=$this->db->select() |
|
| 242 | - ->from( |
|
| 243 | - array('c' => $this->config->getMIBCacheTableName()), |
|
| 244 | - array('name' => 'c.name','mib' => 'c.mib','oid' => 'c.oid','type_enum'=>'c.type_enum', |
|
| 245 | - 'type' => 'c.syntax', 'text_conv' => 'c.textual_convention', 'disp' => 'display_hint', |
|
| 246 | - 'description' => 'c.description')) |
|
| 247 | - ->where($where); |
|
| 248 | - $trap=$this->db->fetchRow($query); |
|
| 232 | + // Get trap id in DB |
|
| 233 | + if ($oid===null) |
|
| 234 | + { |
|
| 235 | + $where="c.id = '$id'"; |
|
| 236 | + } |
|
| 237 | + else |
|
| 238 | + { |
|
| 239 | + $where="c.oid = '$oid'"; |
|
| 240 | + } |
|
| 241 | + $query=$this->db->select() |
|
| 242 | + ->from( |
|
| 243 | + array('c' => $this->config->getMIBCacheTableName()), |
|
| 244 | + array('name' => 'c.name','mib' => 'c.mib','oid' => 'c.oid','type_enum'=>'c.type_enum', |
|
| 245 | + 'type' => 'c.syntax', 'text_conv' => 'c.textual_convention', 'disp' => 'display_hint', |
|
| 246 | + 'description' => 'c.description')) |
|
| 247 | + ->where($where); |
|
| 248 | + $trap=$this->db->fetchRow($query); |
|
| 249 | 249 | |
| 250 | - return $trap; |
|
| 250 | + return $trap; |
|
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | } |
@@ -21,7 +21,9 @@ discard block |
||
| 21 | 21 | $this->prepareTabs()->activate('traps'); |
| 22 | 22 | |
| 23 | 23 | $dbConn = $this->getUIDatabase()->getDb(); |
| 24 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 24 | + if ($dbConn === null) { |
|
| 25 | + throw new \ErrorException('uncatched db error'); |
|
| 26 | + } |
|
| 25 | 27 | $this->getTrapListTable()->setConnection($dbConn); |
| 26 | 28 | |
| 27 | 29 | // Apply pagination limits |
@@ -58,7 +60,9 @@ discard block |
||
| 58 | 60 | $queryArray=$this->getModuleConfig()->trapDetailQuery(); |
| 59 | 61 | |
| 60 | 62 | $dbConn = $this->getUIDatabase()->getDbConn(); |
| 61 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 63 | + if ($dbConn === null) { |
|
| 64 | + throw new \ErrorException('uncatched db error'); |
|
| 65 | + } |
|
| 62 | 66 | |
| 63 | 67 | // URL to add a handler |
| 64 | 68 | $this->view->addHandlerUrl=Url::fromPath( |
@@ -78,9 +82,10 @@ discard block |
||
| 78 | 82 | ->from($this->moduleConfig->getTrapTableName(),$elmts) |
| 79 | 83 | ->where('id=?',$trapid); |
| 80 | 84 | $trapDetail=$dbConn->fetchRow($query); |
| 81 | - if ( $trapDetail == null) throw new Exception('No traps was found with id = '.$trapid); |
|
| 82 | - } |
|
| 83 | - catch (Exception $e) |
|
| 85 | + if ( $trapDetail == null) { |
|
| 86 | + throw new Exception('No traps was found with id = '.$trapid); |
|
| 87 | + } |
|
| 88 | + } catch (Exception $e) |
|
| 84 | 89 | { |
| 85 | 90 | $this->displayExitError('Trap detail',$e->getMessage()); |
| 86 | 91 | return; |
@@ -111,8 +116,7 @@ discard block |
||
| 111 | 116 | ->from($this->moduleConfig->getTrapDataTableName(),$data_elmts) |
| 112 | 117 | ->where('trap_id=?',$trapid); |
| 113 | 118 | $trapDetail=$dbConn->fetchAll($query); |
| 114 | - } |
|
| 115 | - catch (Exception $e) |
|
| 119 | + } catch (Exception $e) |
|
| 116 | 120 | { |
| 117 | 121 | $this->displayExitError('Trap detail',$e->getMessage()); |
| 118 | 122 | } |
@@ -120,8 +124,7 @@ discard block |
||
| 120 | 124 | if ($trapDetail == null ) |
| 121 | 125 | { |
| 122 | 126 | $this->view->data=false; |
| 123 | - } |
|
| 124 | - else |
|
| 127 | + } else |
|
| 125 | 128 | { |
| 126 | 129 | $this->view->data=true; |
| 127 | 130 | // Store result in array. |
@@ -148,7 +151,9 @@ discard block |
||
| 148 | 151 | $this->prepareTabs()->activate('hosts'); |
| 149 | 152 | |
| 150 | 153 | $dbConn = $this->getUIDatabase()->getDb(); |
| 151 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 154 | + if ($dbConn === null) { |
|
| 155 | + throw new \ErrorException('uncatched db error'); |
|
| 156 | + } |
|
| 152 | 157 | |
| 153 | 158 | $this->getTrapHostListTable()->setConnection($dbConn); |
| 154 | 159 | |
@@ -209,8 +214,7 @@ discard block |
||
| 209 | 214 | $ip=$postData['IP']; |
| 210 | 215 | $oid=$postData['OID']; |
| 211 | 216 | $action=$postData['action']; |
| 212 | - } |
|
| 213 | - else |
|
| 217 | + } else |
|
| 214 | 218 | { |
| 215 | 219 | $this->_helper->json(array('status'=>'Missing variables')); |
| 216 | 220 | return; |
@@ -40,8 +40,8 @@ discard block |
||
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | /** |
| 43 | - * Trap detail page |
|
| 44 | - */ |
|
| 43 | + * Trap detail page |
|
| 44 | + */ |
|
| 45 | 45 | public function trapdetailAction() |
| 46 | 46 | { |
| 47 | 47 | |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | // Do DB query for trap. |
| 75 | 75 | try |
| 76 | 76 | { |
| 77 | - $query = $dbConn->select() |
|
| 77 | + $query = $dbConn->select() |
|
| 78 | 78 | ->from($this->moduleConfig->getTrapTableName(),$elmts) |
| 79 | 79 | ->where('id=?',$trapid); |
| 80 | 80 | $trapDetail=$dbConn->fetchRow($query); |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | } |
| 108 | 108 | try |
| 109 | 109 | { |
| 110 | - $query = $dbConn->select() |
|
| 110 | + $query = $dbConn->select() |
|
| 111 | 111 | ->from($this->moduleConfig->getTrapDataTableName(),$data_elmts) |
| 112 | 112 | ->where('trap_id=?',$trapid); |
| 113 | 113 | $trapDetail=$dbConn->fetchAll($query); |
@@ -144,24 +144,24 @@ discard block |
||
| 144 | 144 | */ |
| 145 | 145 | public function hostsAction() |
| 146 | 146 | { |
| 147 | - $this->checkReadPermission(); |
|
| 148 | - $this->prepareTabs()->activate('hosts'); |
|
| 147 | + $this->checkReadPermission(); |
|
| 148 | + $this->prepareTabs()->activate('hosts'); |
|
| 149 | 149 | |
| 150 | - $dbConn = $this->getUIDatabase()->getDb(); |
|
| 151 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 150 | + $dbConn = $this->getUIDatabase()->getDb(); |
|
| 151 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 152 | 152 | |
| 153 | - $this->getTrapHostListTable()->setConnection($dbConn); |
|
| 153 | + $this->getTrapHostListTable()->setConnection($dbConn); |
|
| 154 | 154 | |
| 155 | - // Apply pagination limits |
|
| 156 | - $this->view->table=$this->applyPaginationLimits($this->getTrapHostListTable(),$this->getModuleConfig()->itemListDisplay()); |
|
| 155 | + // Apply pagination limits |
|
| 156 | + $this->view->table=$this->applyPaginationLimits($this->getTrapHostListTable(),$this->getModuleConfig()->itemListDisplay()); |
|
| 157 | 157 | |
| 158 | - // Set Filter |
|
| 159 | - //$postData=$this->getRequest()->getPost(); |
|
| 160 | - $filter=array(); |
|
| 161 | - $filter['q']=$this->params->get('q');//(isset($postData['q']))?$postData['q']:''; |
|
| 162 | - $filter['done']=$this->params->get('done'); |
|
| 163 | - $this->view->filter=$filter; |
|
| 164 | - $this->view->table->updateFilter(Url::fromRequest(),$filter); |
|
| 158 | + // Set Filter |
|
| 159 | + //$postData=$this->getRequest()->getPost(); |
|
| 160 | + $filter=array(); |
|
| 161 | + $filter['q']=$this->params->get('q');//(isset($postData['q']))?$postData['q']:''; |
|
| 162 | + $filter['done']=$this->params->get('done'); |
|
| 163 | + $this->view->filter=$filter; |
|
| 164 | + $this->view->table->updateFilter(Url::fromRequest(),$filter); |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | public function deleteAction() |
@@ -181,12 +181,12 @@ discard block |
||
| 181 | 181 | return $this->getTabs()->add('traps', array( |
| 182 | 182 | 'label' => $this->translate('Traps'), |
| 183 | 183 | 'url' => $this->getModuleConfig()->urlPath() . '/received') |
| 184 | - ) |
|
| 185 | - ->add('hosts', array( |
|
| 186 | - 'label' => $this->translate('Hosts'), |
|
| 187 | - 'url' => $this->getModuleConfig()->urlPath() . '/received/hosts') |
|
| 188 | - ) |
|
| 189 | - ->add('delete', array( |
|
| 184 | + ) |
|
| 185 | + ->add('hosts', array( |
|
| 186 | + 'label' => $this->translate('Hosts'), |
|
| 187 | + 'url' => $this->getModuleConfig()->urlPath() . '/received/hosts') |
|
| 188 | + ) |
|
| 189 | + ->add('delete', array( |
|
| 190 | 190 | 'label' => $this->translate('Delete'), |
| 191 | 191 | 'url' => $this->getModuleConfig()->urlPath() . '/received/delete') |
| 192 | 192 | ); |
@@ -222,7 +222,7 @@ discard block |
||
| 222 | 222 | } |
| 223 | 223 | if ($action =="delete") |
| 224 | 224 | { |
| 225 | - $this->_helper->json(array('status'=>'OK','count'=>$this->getUIDatabase()->deleteTrap($ip,$oid))); |
|
| 225 | + $this->_helper->json(array('status'=>'OK','count'=>$this->getUIDatabase()->deleteTrap($ip,$oid))); |
|
| 226 | 226 | return; |
| 227 | 227 | } |
| 228 | 228 | $this->_helper->json(array('status'=>'unknown action')); |
@@ -20,20 +20,20 @@ discard block |
||
| 20 | 20 | $this->checkReadPermission(); |
| 21 | 21 | $this->prepareTabs()->activate('traps'); |
| 22 | 22 | |
| 23 | - $dbConn = $this->getUIDatabase()->getDb(); |
|
| 23 | + $dbConn=$this->getUIDatabase()->getDb(); |
|
| 24 | 24 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 25 | 25 | $this->getTrapListTable()->setConnection($dbConn); |
| 26 | 26 | |
| 27 | 27 | // Apply pagination limits |
| 28 | - $this->view->table=$this->applyPaginationLimits($this->getTrapListTable(),$this->getModuleConfig()->itemListDisplay()); |
|
| 28 | + $this->view->table=$this->applyPaginationLimits($this->getTrapListTable(), $this->getModuleConfig()->itemListDisplay()); |
|
| 29 | 29 | |
| 30 | 30 | // Set Filter |
| 31 | 31 | //$postData=$this->getRequest()->getPost(); |
| 32 | 32 | $filter=array(); |
| 33 | - $filter['q']=$this->params->get('q');//(isset($postData['q']))?$postData['q']:''; |
|
| 33 | + $filter['q']=$this->params->get('q'); //(isset($postData['q']))?$postData['q']:''; |
|
| 34 | 34 | $filter['done']=$this->params->get('done'); |
| 35 | 35 | $this->view->filter=$filter; |
| 36 | - $this->view->table->updateFilter(Url::fromRequest(),$filter); |
|
| 36 | + $this->view->table->updateFilter(Url::fromRequest(), $filter); |
|
| 37 | 37 | |
| 38 | 38 | //$this->view->filterEditor = $this->getTrapListTable()->getFilterEditor($this->getRequest()); |
| 39 | 39 | |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | |
| 48 | 48 | $this->checkReadPermission(); |
| 49 | 49 | // set up tab |
| 50 | - $this->getTabs()->add('get',array( |
|
| 50 | + $this->getTabs()->add('get', array( |
|
| 51 | 51 | 'active' => true, |
| 52 | 52 | 'label' => $this->translate('Detailed status'), |
| 53 | 53 | 'url' => Url::fromRequest() |
@@ -57,12 +57,12 @@ discard block |
||
| 57 | 57 | $this->view->trapid=$trapid; |
| 58 | 58 | $queryArray=$this->getModuleConfig()->trapDetailQuery(); |
| 59 | 59 | |
| 60 | - $dbConn = $this->getUIDatabase()->getDbConn(); |
|
| 60 | + $dbConn=$this->getUIDatabase()->getDbConn(); |
|
| 61 | 61 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 62 | 62 | |
| 63 | 63 | // URL to add a handler |
| 64 | 64 | $this->view->addHandlerUrl=Url::fromPath( |
| 65 | - $this->getModuleConfig()->urlPath() . '/handler/add', |
|
| 65 | + $this->getModuleConfig()->urlPath().'/handler/add', |
|
| 66 | 66 | array('fromid' => $trapid)); |
| 67 | 67 | // *************** Get main data |
| 68 | 68 | // extract columns and titles; |
@@ -74,28 +74,28 @@ discard block |
||
| 74 | 74 | // Do DB query for trap. |
| 75 | 75 | try |
| 76 | 76 | { |
| 77 | - $query = $dbConn->select() |
|
| 78 | - ->from($this->moduleConfig->getTrapTableName(),$elmts) |
|
| 79 | - ->where('id=?',$trapid); |
|
| 77 | + $query=$dbConn->select() |
|
| 78 | + ->from($this->moduleConfig->getTrapTableName(), $elmts) |
|
| 79 | + ->where('id=?', $trapid); |
|
| 80 | 80 | $trapDetail=$dbConn->fetchRow($query); |
| 81 | - if ( $trapDetail == null) throw new Exception('No traps was found with id = '.$trapid); |
|
| 81 | + if ($trapDetail == null) throw new Exception('No traps was found with id = '.$trapid); |
|
| 82 | 82 | } |
| 83 | 83 | catch (Exception $e) |
| 84 | 84 | { |
| 85 | - $this->displayExitError('Trap detail',$e->getMessage()); |
|
| 85 | + $this->displayExitError('Trap detail', $e->getMessage()); |
|
| 86 | 86 | return; |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | // Store result in array (with Titles). |
| 90 | 90 | foreach ($queryArray as $key => $val) { |
| 91 | 91 | if ($key == 'timestamp') { |
| 92 | - $cval=strftime('%c',$trapDetail->$key); |
|
| 92 | + $cval=strftime('%c', $trapDetail->$key); |
|
| 93 | 93 | } else { |
| 94 | 94 | $cval=$trapDetail->$key; |
| 95 | 95 | } |
| 96 | - array_push($queryArray[$key],$cval); |
|
| 96 | + array_push($queryArray[$key], $cval); |
|
| 97 | 97 | } |
| 98 | - $this->view->rowset = $queryArray; |
|
| 98 | + $this->view->rowset=$queryArray; |
|
| 99 | 99 | |
| 100 | 100 | // ************** Check for additionnal data |
| 101 | 101 | |
@@ -107,17 +107,17 @@ discard block |
||
| 107 | 107 | } |
| 108 | 108 | try |
| 109 | 109 | { |
| 110 | - $query = $dbConn->select() |
|
| 111 | - ->from($this->moduleConfig->getTrapDataTableName(),$data_elmts) |
|
| 112 | - ->where('trap_id=?',$trapid); |
|
| 110 | + $query=$dbConn->select() |
|
| 111 | + ->from($this->moduleConfig->getTrapDataTableName(), $data_elmts) |
|
| 112 | + ->where('trap_id=?', $trapid); |
|
| 113 | 113 | $trapDetail=$dbConn->fetchAll($query); |
| 114 | 114 | } |
| 115 | 115 | catch (Exception $e) |
| 116 | 116 | { |
| 117 | - $this->displayExitError('Trap detail',$e->getMessage()); |
|
| 117 | + $this->displayExitError('Trap detail', $e->getMessage()); |
|
| 118 | 118 | } |
| 119 | 119 | // TODO : code this in a better & simpler way |
| 120 | - if ($trapDetail == null ) |
|
| 120 | + if ($trapDetail == null) |
|
| 121 | 121 | { |
| 122 | 122 | $this->view->data=false; |
| 123 | 123 | } |
@@ -129,9 +129,9 @@ discard block |
||
| 129 | 129 | foreach ($trapDetail as $key => $val) |
| 130 | 130 | { |
| 131 | 131 | $trapval[$key]=array(); |
| 132 | - foreach (array_keys($queryArrayData) as $vkey ) |
|
| 132 | + foreach (array_keys($queryArrayData) as $vkey) |
|
| 133 | 133 | { |
| 134 | - array_push($trapval[$key],$val->$vkey); |
|
| 134 | + array_push($trapval[$key], $val->$vkey); |
|
| 135 | 135 | } |
| 136 | 136 | } |
| 137 | 137 | $this->view->data_val=$trapval; |
@@ -147,21 +147,21 @@ discard block |
||
| 147 | 147 | $this->checkReadPermission(); |
| 148 | 148 | $this->prepareTabs()->activate('hosts'); |
| 149 | 149 | |
| 150 | - $dbConn = $this->getUIDatabase()->getDb(); |
|
| 150 | + $dbConn=$this->getUIDatabase()->getDb(); |
|
| 151 | 151 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 152 | 152 | |
| 153 | 153 | $this->getTrapHostListTable()->setConnection($dbConn); |
| 154 | 154 | |
| 155 | 155 | // Apply pagination limits |
| 156 | - $this->view->table=$this->applyPaginationLimits($this->getTrapHostListTable(),$this->getModuleConfig()->itemListDisplay()); |
|
| 156 | + $this->view->table=$this->applyPaginationLimits($this->getTrapHostListTable(), $this->getModuleConfig()->itemListDisplay()); |
|
| 157 | 157 | |
| 158 | 158 | // Set Filter |
| 159 | 159 | //$postData=$this->getRequest()->getPost(); |
| 160 | 160 | $filter=array(); |
| 161 | - $filter['q']=$this->params->get('q');//(isset($postData['q']))?$postData['q']:''; |
|
| 161 | + $filter['q']=$this->params->get('q'); //(isset($postData['q']))?$postData['q']:''; |
|
| 162 | 162 | $filter['done']=$this->params->get('done'); |
| 163 | 163 | $this->view->filter=$filter; |
| 164 | - $this->view->table->updateFilter(Url::fromRequest(),$filter); |
|
| 164 | + $this->view->table->updateFilter(Url::fromRequest(), $filter); |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | public function deleteAction() |
@@ -180,15 +180,15 @@ discard block |
||
| 180 | 180 | { |
| 181 | 181 | return $this->getTabs()->add('traps', array( |
| 182 | 182 | 'label' => $this->translate('Traps'), |
| 183 | - 'url' => $this->getModuleConfig()->urlPath() . '/received') |
|
| 183 | + 'url' => $this->getModuleConfig()->urlPath().'/received') |
|
| 184 | 184 | ) |
| 185 | 185 | ->add('hosts', array( |
| 186 | 186 | 'label' => $this->translate('Hosts'), |
| 187 | - 'url' => $this->getModuleConfig()->urlPath() . '/received/hosts') |
|
| 187 | + 'url' => $this->getModuleConfig()->urlPath().'/received/hosts') |
|
| 188 | 188 | ) |
| 189 | 189 | ->add('delete', array( |
| 190 | 190 | 'label' => $this->translate('Delete'), |
| 191 | - 'url' => $this->getModuleConfig()->urlPath() . '/received/delete') |
|
| 191 | + 'url' => $this->getModuleConfig()->urlPath().'/received/delete') |
|
| 192 | 192 | ); |
| 193 | 193 | } |
| 194 | 194 | |
@@ -215,14 +215,14 @@ discard block |
||
| 215 | 215 | $this->_helper->json(array('status'=>'Missing variables')); |
| 216 | 216 | return; |
| 217 | 217 | } |
| 218 | - if ($action =="count") |
|
| 218 | + if ($action == "count") |
|
| 219 | 219 | { |
| 220 | - $this->_helper->json(array('status'=>'OK','count'=>$this->getUIDatabase()->countTrap($ip,$oid))); |
|
| 220 | + $this->_helper->json(array('status'=>'OK', 'count'=>$this->getUIDatabase()->countTrap($ip, $oid))); |
|
| 221 | 221 | return; |
| 222 | 222 | } |
| 223 | - if ($action =="delete") |
|
| 223 | + if ($action == "delete") |
|
| 224 | 224 | { |
| 225 | - $this->_helper->json(array('status'=>'OK','count'=>$this->getUIDatabase()->deleteTrap($ip,$oid))); |
|
| 225 | + $this->_helper->json(array('status'=>'OK', 'count'=>$this->getUIDatabase()->deleteTrap($ip, $oid))); |
|
| 226 | 226 | return; |
| 227 | 227 | } |
| 228 | 228 | $this->_helper->json(array('status'=>'unknown action')); |
@@ -116,8 +116,7 @@ discard block |
||
| 116 | 116 | $this->trapController->redirectNow('trapdirector/settings?dberror=5'); |
| 117 | 117 | return false; |
| 118 | 118 | } |
| 119 | - } |
|
| 120 | - catch (Exception $e) |
|
| 119 | + } catch (Exception $e) |
|
| 121 | 120 | { |
| 122 | 121 | if ($test === true) |
| 123 | 122 | { |
@@ -142,8 +141,7 @@ discard block |
||
| 142 | 141 | try |
| 143 | 142 | { |
| 144 | 143 | $dbconn = IcingaDbConnection::fromResourceName($DBname); |
| 145 | - } |
|
| 146 | - catch (Exception $e) |
|
| 144 | + } catch (Exception $e) |
|
| 147 | 145 | { |
| 148 | 146 | if ($test === true) |
| 149 | 147 | { |
@@ -157,8 +155,7 @@ discard block |
||
| 157 | 155 | { |
| 158 | 156 | $dbAdapter=$dbconn->getDbAdapter(); |
| 159 | 157 | |
| 160 | - } |
|
| 161 | - catch (Exception $e) |
|
| 158 | + } catch (Exception $e) |
|
| 162 | 159 | { |
| 163 | 160 | if ($test === true) |
| 164 | 161 | { |
@@ -185,7 +182,9 @@ discard block |
||
| 185 | 182 | */ |
| 186 | 183 | public function getDb() |
| 187 | 184 | { |
| 188 | - if ( $this->trapDB != null ) return $this->trapDB; |
|
| 185 | + if ( $this->trapDB != null ) { |
|
| 186 | + return $this->trapDB; |
|
| 187 | + } |
|
| 189 | 188 | |
| 190 | 189 | |
| 191 | 190 | $dbresource=$this->trapController->Config()->get('config', 'database'); |
@@ -213,7 +212,9 @@ discard block |
||
| 213 | 212 | */ |
| 214 | 213 | public function getDbConn() |
| 215 | 214 | { |
| 216 | - if ($this->getDb() == null) return null; |
|
| 215 | + if ($this->getDb() == null) { |
|
| 216 | + return null; |
|
| 217 | + } |
|
| 217 | 218 | return $this->getDb()->getConnection(); |
| 218 | 219 | } |
| 219 | 220 | |
@@ -243,7 +244,9 @@ discard block |
||
| 243 | 244 | */ |
| 244 | 245 | public function getIdoDb() |
| 245 | 246 | { |
| 246 | - if ( $this->idoDB != null ) return $this->idoDB; |
|
| 247 | + if ( $this->idoDB != null ) { |
|
| 248 | + return $this->idoDB; |
|
| 249 | + } |
|
| 247 | 250 | // TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
| 248 | 251 | $dbresource=$this->trapController->Config()->get('config', 'IDOdatabase');; |
| 249 | 252 | |
@@ -256,8 +259,7 @@ discard block |
||
| 256 | 259 | try |
| 257 | 260 | { |
| 258 | 261 | $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
| 259 | - } |
|
| 260 | - catch (Exception $e) |
|
| 262 | + } catch (Exception $e) |
|
| 261 | 263 | { |
| 262 | 264 | $this->trapController->redirectNow('trapdirector/settings?idodberror=2'); |
| 263 | 265 | return null; |
@@ -274,7 +276,9 @@ discard block |
||
| 274 | 276 | */ |
| 275 | 277 | public function getIdoDbConn() |
| 276 | 278 | { |
| 277 | - if ($this->getIdoDb() == null) return null; |
|
| 279 | + if ($this->getIdoDb() == null) { |
|
| 280 | + return null; |
|
| 281 | + } |
|
| 278 | 282 | return $this->getIdoDb()->getConnection(); |
| 279 | 283 | } |
| 280 | 284 | |
@@ -296,8 +300,7 @@ discard block |
||
| 296 | 300 | try |
| 297 | 301 | { |
| 298 | 302 | $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
| 299 | - } |
|
| 300 | - catch (Exception $e) |
|
| 303 | + } catch (Exception $e) |
|
| 301 | 304 | { |
| 302 | 305 | throw new DBException( array(2,"Database $dbresource does not exists in IcingaWeb2") ); |
| 303 | 306 | } |
@@ -311,8 +314,7 @@ discard block |
||
| 311 | 314 | { |
| 312 | 315 | throw new DBException( array(4,"$dbresource does not look like an IDO database")); |
| 313 | 316 | } |
| 314 | - } |
|
| 315 | - catch (Exception $e) |
|
| 317 | + } catch (Exception $e) |
|
| 316 | 318 | { |
| 317 | 319 | throw new DBException( array(3,"Error connecting to $dbresource : " . $e->getMessage())); |
| 318 | 320 | } |
@@ -29,10 +29,10 @@ discard block |
||
| 29 | 29 | * @param int $code |
| 30 | 30 | * @param Exception $previous |
| 31 | 31 | */ |
| 32 | - public function __construct(array $retarray, string $message = null, int $code = 0, Exception $previous = null) |
|
| 32 | + public function __construct(array $retarray, string $message=null, int $code=0, Exception $previous=null) |
|
| 33 | 33 | { |
| 34 | - parent::__construct($message,$code,$previous); |
|
| 35 | - $this->returnArray = $retarray; |
|
| 34 | + parent::__construct($message, $code, $previous); |
|
| 35 | + $this->returnArray=$retarray; |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | /** |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | class UIDatabase //extends TrapDBQuery |
| 58 | 58 | { |
| 59 | - use TrapDBQuery,IdoDBQuery; |
|
| 59 | + use TrapDBQuery, IdoDBQuery; |
|
| 60 | 60 | |
| 61 | 61 | /** @var TrapsController $trapController TrapController 'parent' class */ |
| 62 | 62 | protected $trapController=null; |
@@ -94,19 +94,19 @@ discard block |
||
| 94 | 94 | * @return bool true if OK, false if version < min version |
| 95 | 95 | * @throws Exception if error and test = true |
| 96 | 96 | */ |
| 97 | - protected function testDbVersion($dbAdapter,int $min,bool $test, string $DBname) |
|
| 97 | + protected function testDbVersion($dbAdapter, int $min, bool $test, string $DBname) |
|
| 98 | 98 | { |
| 99 | 99 | try |
| 100 | 100 | { |
| 101 | - $query = $dbAdapter->select() |
|
| 102 | - ->from($this->trapController->getModuleConfig()->getDbConfigTableName(),'value') |
|
| 101 | + $query=$dbAdapter->select() |
|
| 102 | + ->from($this->trapController->getModuleConfig()->getDbConfigTableName(), 'value') |
|
| 103 | 103 | ->where('name=\'db_version\''); |
| 104 | 104 | $version=$dbAdapter->fetchRow($query); |
| 105 | - if ( ($version == null) || ! property_exists($version,'value') ) |
|
| 105 | + if (($version == null) || !property_exists($version, 'value')) |
|
| 106 | 106 | { |
| 107 | 107 | if ($test === true) |
| 108 | 108 | { |
| 109 | - $this->testResult = array(4,$DBname); |
|
| 109 | + $this->testResult=array(4, $DBname); |
|
| 110 | 110 | return false; |
| 111 | 111 | } |
| 112 | 112 | $this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | { |
| 117 | 117 | if ($test === true) |
| 118 | 118 | { |
| 119 | - $this->testResult = array(5,$version->value,$min); |
|
| 119 | + $this->testResult=array(5, $version->value, $min); |
|
| 120 | 120 | return false; |
| 121 | 121 | } |
| 122 | 122 | $this->trapController->redirectNow('trapdirector/settings?dberror=5'); |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | { |
| 128 | 128 | if ($test === true) |
| 129 | 129 | { |
| 130 | - $this->testResult = array(3,$DBname,$e->getMessage()); |
|
| 130 | + $this->testResult=array(3, $DBname, $e->getMessage()); |
|
| 131 | 131 | return false; |
| 132 | 132 | } |
| 133 | 133 | $this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
@@ -143,17 +143,17 @@ discard block |
||
| 143 | 143 | * @throws DBException if test = true and error |
| 144 | 144 | * @return Selectable|null : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
| 145 | 145 | */ |
| 146 | - protected function getDbByName($DBname , $test = false , $test_version = true) |
|
| 146 | + protected function getDbByName($DBname, $test=false, $test_version=true) |
|
| 147 | 147 | { |
| 148 | 148 | try |
| 149 | 149 | { |
| 150 | - $dbconn = IcingaDbConnection::fromResourceName($DBname); |
|
| 150 | + $dbconn=IcingaDbConnection::fromResourceName($DBname); |
|
| 151 | 151 | } |
| 152 | 152 | catch (Exception $e) |
| 153 | 153 | { |
| 154 | 154 | if ($test === true) |
| 155 | 155 | { |
| 156 | - throw new DBException(array(2,$DBname)); |
|
| 156 | + throw new DBException(array(2, $DBname)); |
|
| 157 | 157 | } |
| 158 | 158 | $this->trapController->redirectNow('trapdirector/settings?dberror=2'); |
| 159 | 159 | return null; |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | { |
| 169 | 169 | if ($test === true) |
| 170 | 170 | { |
| 171 | - throw new DBException(array(3,$DBname,$e->getMessage())); |
|
| 171 | + throw new DBException(array(3, $DBname, $e->getMessage())); |
|
| 172 | 172 | } |
| 173 | 173 | $this->trapController->redirectNow('trapdirector/settings?dberror=3'); |
| 174 | 174 | return null; |
@@ -191,19 +191,19 @@ discard block |
||
| 191 | 191 | */ |
| 192 | 192 | public function getDb() |
| 193 | 193 | { |
| 194 | - if ( $this->trapDB != null ) return $this->trapDB; |
|
| 194 | + if ($this->trapDB != null) return $this->trapDB; |
|
| 195 | 195 | |
| 196 | 196 | |
| 197 | 197 | $dbresource=$this->trapController->Config()->get('config', 'database'); |
| 198 | 198 | |
| 199 | - if ( ! $dbresource ) |
|
| 199 | + if (!$dbresource) |
|
| 200 | 200 | { |
| 201 | 201 | $this->trapController->redirectNow('trapdirector/settings?dberror=1'); |
| 202 | 202 | return null; |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | try { |
| 206 | - $this->trapDB = $this->getDbByName($dbresource,false,true); |
|
| 206 | + $this->trapDB=$this->getDbByName($dbresource, false, true); |
|
| 207 | 207 | } catch (DBException $e) { |
| 208 | 208 | return null; // Should not happen as test = false |
| 209 | 209 | } |
@@ -233,12 +233,12 @@ discard block |
||
| 233 | 233 | { |
| 234 | 234 | $dbresource=$this->trapController->Config()->get('config', 'database'); |
| 235 | 235 | |
| 236 | - if ( ! $dbresource ) |
|
| 236 | + if (!$dbresource) |
|
| 237 | 237 | { |
| 238 | - throw new DBException(array(1,'')); |
|
| 238 | + throw new DBException(array(1, '')); |
|
| 239 | 239 | } |
| 240 | 240 | |
| 241 | - $this->trapDB = $this->getDbByName($dbresource,true,true); |
|
| 241 | + $this->trapDB=$this->getDbByName($dbresource, true, true); |
|
| 242 | 242 | return; |
| 243 | 243 | } |
| 244 | 244 | |
@@ -249,11 +249,11 @@ discard block |
||
| 249 | 249 | */ |
| 250 | 250 | public function getIdoDb() |
| 251 | 251 | { |
| 252 | - if ( $this->idoDB != null ) return $this->idoDB; |
|
| 252 | + if ($this->idoDB != null) return $this->idoDB; |
|
| 253 | 253 | // TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
| 254 | - $dbresource=$this->trapController->Config()->get('config', 'IDOdatabase');; |
|
| 254 | + $dbresource=$this->trapController->Config()->get('config', 'IDOdatabase'); ; |
|
| 255 | 255 | |
| 256 | - if ( ! $dbresource ) |
|
| 256 | + if (!$dbresource) |
|
| 257 | 257 | { |
| 258 | 258 | $this->trapController->redirectNow('trapdirector/settings?idodberror=1'); |
| 259 | 259 | return null; |
@@ -261,7 +261,7 @@ discard block |
||
| 261 | 261 | |
| 262 | 262 | try |
| 263 | 263 | { |
| 264 | - $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
| 264 | + $dbconn=IcingaDbConnection::fromResourceName($dbresource); |
|
| 265 | 265 | } |
| 266 | 266 | catch (Exception $e) |
| 267 | 267 | { |
@@ -269,7 +269,7 @@ discard block |
||
| 269 | 269 | return null; |
| 270 | 270 | } |
| 271 | 271 | |
| 272 | - $this->idoDB = $dbconn; |
|
| 272 | + $this->idoDB=$dbconn; |
|
| 273 | 273 | return $this->idoDB; |
| 274 | 274 | } |
| 275 | 275 | |
@@ -292,35 +292,35 @@ discard block |
||
| 292 | 292 | public function testGetIdoDb() |
| 293 | 293 | { |
| 294 | 294 | // TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
| 295 | - $dbresource=$this->trapController->Config()->get('config', 'IDOdatabase');; |
|
| 295 | + $dbresource=$this->trapController->Config()->get('config', 'IDOdatabase'); ; |
|
| 296 | 296 | |
| 297 | - if ( ! $dbresource ) |
|
| 297 | + if (!$dbresource) |
|
| 298 | 298 | { |
| 299 | - throw new DBException(array(1,'No database in config.ini')); |
|
| 299 | + throw new DBException(array(1, 'No database in config.ini')); |
|
| 300 | 300 | } |
| 301 | 301 | |
| 302 | 302 | try |
| 303 | 303 | { |
| 304 | - $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
| 304 | + $dbconn=IcingaDbConnection::fromResourceName($dbresource); |
|
| 305 | 305 | } |
| 306 | 306 | catch (Exception $e) |
| 307 | 307 | { |
| 308 | - throw new DBException( array(2,"Database $dbresource does not exists in IcingaWeb2") ); |
|
| 308 | + throw new DBException(array(2, "Database $dbresource does not exists in IcingaWeb2")); |
|
| 309 | 309 | } |
| 310 | 310 | |
| 311 | 311 | try |
| 312 | 312 | { |
| 313 | - $query = $dbconn->select() |
|
| 314 | - ->from('icinga_dbversion',array('version')); |
|
| 313 | + $query=$dbconn->select() |
|
| 314 | + ->from('icinga_dbversion', array('version')); |
|
| 315 | 315 | $version=$dbconn->fetchRow($query); |
| 316 | - if ( ($version == null) || ! property_exists($version,'version') ) |
|
| 316 | + if (($version == null) || !property_exists($version, 'version')) |
|
| 317 | 317 | { |
| 318 | - throw new DBException( array(4,"$dbresource does not look like an IDO database")); |
|
| 318 | + throw new DBException(array(4, "$dbresource does not look like an IDO database")); |
|
| 319 | 319 | } |
| 320 | 320 | } |
| 321 | 321 | catch (Exception $e) |
| 322 | 322 | { |
| 323 | - throw new DBException( array(3,"Error connecting to $dbresource : " . $e->getMessage())); |
|
| 323 | + throw new DBException(array(3, "Error connecting to $dbresource : ".$e->getMessage())); |
|
| 324 | 324 | } |
| 325 | 325 | |
| 326 | 326 | return; |
@@ -19,30 +19,30 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | class DBException extends Exception |
| 21 | 21 | { |
| 22 | - /** @var array $returnArray */ |
|
| 23 | - private $returnArray; |
|
| 22 | + /** @var array $returnArray */ |
|
| 23 | + private $returnArray; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Buil DBException |
|
| 27 | - * @param array $retarray |
|
| 28 | - * @param string $message |
|
| 29 | - * @param int $code |
|
| 30 | - * @param Exception $previous |
|
| 31 | - */ |
|
| 32 | - public function __construct(array $retarray, string $message = null, int $code = 0, Exception $previous = null) |
|
| 33 | - { |
|
| 34 | - parent::__construct($message,$code,$previous); |
|
| 35 | - $this->returnArray = $retarray; |
|
| 36 | - } |
|
| 25 | + /** |
|
| 26 | + * Buil DBException |
|
| 27 | + * @param array $retarray |
|
| 28 | + * @param string $message |
|
| 29 | + * @param int $code |
|
| 30 | + * @param Exception $previous |
|
| 31 | + */ |
|
| 32 | + public function __construct(array $retarray, string $message = null, int $code = 0, Exception $previous = null) |
|
| 33 | + { |
|
| 34 | + parent::__construct($message,$code,$previous); |
|
| 35 | + $this->returnArray = $retarray; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Get exception array |
|
| 40 | - * @return array |
|
| 41 | - */ |
|
| 42 | - public function getArray() |
|
| 43 | - { |
|
| 44 | - return $this->returnArray; |
|
| 45 | - } |
|
| 38 | + /** |
|
| 39 | + * Get exception array |
|
| 40 | + * @return array |
|
| 41 | + */ |
|
| 42 | + public function getArray() |
|
| 43 | + { |
|
| 44 | + return $this->returnArray; |
|
| 45 | + } |
|
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | /** |
@@ -56,278 +56,278 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | class UIDatabase //extends TrapDBQuery |
| 58 | 58 | { |
| 59 | - use TrapDBQuery,IdoDBQuery; |
|
| 59 | + use TrapDBQuery,IdoDBQuery; |
|
| 60 | 60 | |
| 61 | - /** @var TrapsController $trapController TrapController 'parent' class */ |
|
| 62 | - protected $trapController=null; |
|
| 61 | + /** @var TrapsController $trapController TrapController 'parent' class */ |
|
| 62 | + protected $trapController=null; |
|
| 63 | 63 | |
| 64 | - /** @var Selectable $trapDB Trap Database*/ |
|
| 65 | - protected $trapDB=null; |
|
| 64 | + /** @var Selectable $trapDB Trap Database*/ |
|
| 65 | + protected $trapDB=null; |
|
| 66 | 66 | |
| 67 | - /** @var Selectable $trapDB Icinga IDO database*/ |
|
| 68 | - protected $idoDB=null; |
|
| 67 | + /** @var Selectable $trapDB Icinga IDO database*/ |
|
| 68 | + protected $idoDB=null; |
|
| 69 | 69 | |
| 70 | - /** @var array $testResult */ |
|
| 71 | - protected $testResult; |
|
| 70 | + /** @var array $testResult */ |
|
| 71 | + protected $testResult; |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * |
|
| 75 | - * @param TrapsController $trapCtrl |
|
| 76 | - */ |
|
| 77 | - function __construct(TrapsController $trapCtrl) |
|
| 78 | - { |
|
| 79 | - $this->trapController=$trapCtrl; |
|
| 80 | - } |
|
| 73 | + /** |
|
| 74 | + * |
|
| 75 | + * @param TrapsController $trapCtrl |
|
| 76 | + */ |
|
| 77 | + function __construct(TrapsController $trapCtrl) |
|
| 78 | + { |
|
| 79 | + $this->trapController=$trapCtrl; |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Get TrapsController instance |
|
| 84 | - * @return TrapsController |
|
| 85 | - */ |
|
| 86 | - protected function getTrapCtrl() |
|
| 87 | - { |
|
| 88 | - return $this->trapController; |
|
| 89 | - } |
|
| 82 | + /** |
|
| 83 | + * Get TrapsController instance |
|
| 84 | + * @return TrapsController |
|
| 85 | + */ |
|
| 86 | + protected function getTrapCtrl() |
|
| 87 | + { |
|
| 88 | + return $this->trapController; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Test if database version >= min database version |
|
| 93 | - * |
|
| 94 | - * @param \Zend_Db_Adapter_Abstract $dbConn |
|
| 95 | - * @param int $min Minimum version |
|
| 96 | - * @param bool $test Test mode |
|
| 97 | - * @param string $DBname Name of DB |
|
| 98 | - * @return bool true if OK, false if version < min version |
|
| 99 | - * @throws Exception if error and test = true |
|
| 100 | - */ |
|
| 101 | - protected function testDbVersion($dbAdapter,int $min,bool $test, string $DBname) |
|
| 102 | - { |
|
| 103 | - try |
|
| 104 | - { |
|
| 105 | - $query = $dbAdapter->select() |
|
| 106 | - ->from($this->trapController->getModuleConfig()->getDbConfigTableName(),'value') |
|
| 107 | - ->where('name=\'db_version\''); |
|
| 108 | - $version=$dbAdapter->fetchRow($query); |
|
| 109 | - if ( ($version == null) || ! property_exists($version,'value') ) |
|
| 110 | - { |
|
| 111 | - if ($test === true) |
|
| 112 | - { |
|
| 113 | - $this->testResult = array(4,$DBname); |
|
| 114 | - return false; |
|
| 115 | - } |
|
| 116 | - $this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
|
| 117 | - return false; |
|
| 118 | - } |
|
| 119 | - if ($version->value < $min) |
|
| 120 | - { |
|
| 121 | - if ($test === true) |
|
| 122 | - { |
|
| 123 | - $this->testResult = array(5,$version->value,$min); |
|
| 124 | - return false; |
|
| 125 | - } |
|
| 126 | - $this->trapController->redirectNow('trapdirector/settings?dberror=5'); |
|
| 127 | - return false; |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - catch (Exception $e) |
|
| 131 | - { |
|
| 132 | - if ($test === true) |
|
| 133 | - { |
|
| 134 | - $this->testResult = array(3,$DBname,$e->getMessage()); |
|
| 135 | - return false; |
|
| 136 | - } |
|
| 137 | - $this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
|
| 138 | - return false; |
|
| 139 | - } |
|
| 140 | - return true; |
|
| 141 | - } |
|
| 91 | + /** |
|
| 92 | + * Test if database version >= min database version |
|
| 93 | + * |
|
| 94 | + * @param \Zend_Db_Adapter_Abstract $dbConn |
|
| 95 | + * @param int $min Minimum version |
|
| 96 | + * @param bool $test Test mode |
|
| 97 | + * @param string $DBname Name of DB |
|
| 98 | + * @return bool true if OK, false if version < min version |
|
| 99 | + * @throws Exception if error and test = true |
|
| 100 | + */ |
|
| 101 | + protected function testDbVersion($dbAdapter,int $min,bool $test, string $DBname) |
|
| 102 | + { |
|
| 103 | + try |
|
| 104 | + { |
|
| 105 | + $query = $dbAdapter->select() |
|
| 106 | + ->from($this->trapController->getModuleConfig()->getDbConfigTableName(),'value') |
|
| 107 | + ->where('name=\'db_version\''); |
|
| 108 | + $version=$dbAdapter->fetchRow($query); |
|
| 109 | + if ( ($version == null) || ! property_exists($version,'value') ) |
|
| 110 | + { |
|
| 111 | + if ($test === true) |
|
| 112 | + { |
|
| 113 | + $this->testResult = array(4,$DBname); |
|
| 114 | + return false; |
|
| 115 | + } |
|
| 116 | + $this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
|
| 117 | + return false; |
|
| 118 | + } |
|
| 119 | + if ($version->value < $min) |
|
| 120 | + { |
|
| 121 | + if ($test === true) |
|
| 122 | + { |
|
| 123 | + $this->testResult = array(5,$version->value,$min); |
|
| 124 | + return false; |
|
| 125 | + } |
|
| 126 | + $this->trapController->redirectNow('trapdirector/settings?dberror=5'); |
|
| 127 | + return false; |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + catch (Exception $e) |
|
| 131 | + { |
|
| 132 | + if ($test === true) |
|
| 133 | + { |
|
| 134 | + $this->testResult = array(3,$DBname,$e->getMessage()); |
|
| 135 | + return false; |
|
| 136 | + } |
|
| 137 | + $this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
|
| 138 | + return false; |
|
| 139 | + } |
|
| 140 | + return true; |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - /** Get Database connexion |
|
| 144 | - * @param $DBname string DB name in resource.ini_ge |
|
| 145 | - * @param $test bool if set to true, returns error code and not database |
|
| 146 | - * @param $test_version bool if set to flase, does not test database version of trapDB |
|
| 147 | - * @throws DBException if test = true and error |
|
| 148 | - * @return Selectable|null : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
| 149 | - */ |
|
| 150 | - protected function getDbByName($DBname , $test = false , $test_version = true) |
|
| 151 | - { |
|
| 152 | - try |
|
| 153 | - { |
|
| 154 | - $dbconn = IcingaDbConnection::fromResourceName($DBname); |
|
| 155 | - } |
|
| 156 | - catch (Exception $e) |
|
| 157 | - { |
|
| 158 | - if ($test === true) |
|
| 159 | - { |
|
| 160 | - throw new DBException(array(2,$DBname)); |
|
| 161 | - } |
|
| 162 | - $this->trapController->redirectNow('trapdirector/settings?dberror=2'); |
|
| 163 | - return null; |
|
| 164 | - } |
|
| 143 | + /** Get Database connexion |
|
| 144 | + * @param $DBname string DB name in resource.ini_ge |
|
| 145 | + * @param $test bool if set to true, returns error code and not database |
|
| 146 | + * @param $test_version bool if set to flase, does not test database version of trapDB |
|
| 147 | + * @throws DBException if test = true and error |
|
| 148 | + * @return Selectable|null : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
| 149 | + */ |
|
| 150 | + protected function getDbByName($DBname , $test = false , $test_version = true) |
|
| 151 | + { |
|
| 152 | + try |
|
| 153 | + { |
|
| 154 | + $dbconn = IcingaDbConnection::fromResourceName($DBname); |
|
| 155 | + } |
|
| 156 | + catch (Exception $e) |
|
| 157 | + { |
|
| 158 | + if ($test === true) |
|
| 159 | + { |
|
| 160 | + throw new DBException(array(2,$DBname)); |
|
| 161 | + } |
|
| 162 | + $this->trapController->redirectNow('trapdirector/settings?dberror=2'); |
|
| 163 | + return null; |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - try |
|
| 167 | - { |
|
| 168 | - $dbAdapter=$dbconn->getDbAdapter(); |
|
| 166 | + try |
|
| 167 | + { |
|
| 168 | + $dbAdapter=$dbconn->getDbAdapter(); |
|
| 169 | 169 | |
| 170 | - } |
|
| 171 | - catch (Exception $e) |
|
| 172 | - { |
|
| 173 | - if ($test === true) |
|
| 174 | - { |
|
| 175 | - throw new DBException(array(3,$DBname,$e->getMessage())); |
|
| 176 | - } |
|
| 177 | - $this->trapController->redirectNow('trapdirector/settings?dberror=3'); |
|
| 178 | - return null; |
|
| 179 | - } |
|
| 170 | + } |
|
| 171 | + catch (Exception $e) |
|
| 172 | + { |
|
| 173 | + if ($test === true) |
|
| 174 | + { |
|
| 175 | + throw new DBException(array(3,$DBname,$e->getMessage())); |
|
| 176 | + } |
|
| 177 | + $this->trapController->redirectNow('trapdirector/settings?dberror=3'); |
|
| 178 | + return null; |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - if ($test_version == true) { |
|
| 182 | - $testRet=$this->testDbVersion($dbAdapter, $this->trapController->getModuleConfig()->getDbMinVersion(), $test, $DBname); |
|
| 183 | - if ($testRet !== true) |
|
| 184 | - { |
|
| 185 | - throw new DBException($this->testResult); |
|
| 186 | - } |
|
| 187 | - } |
|
| 181 | + if ($test_version == true) { |
|
| 182 | + $testRet=$this->testDbVersion($dbAdapter, $this->trapController->getModuleConfig()->getDbMinVersion(), $test, $DBname); |
|
| 183 | + if ($testRet !== true) |
|
| 184 | + { |
|
| 185 | + throw new DBException($this->testResult); |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | - return $dbconn; |
|
| 190 | - } |
|
| 189 | + return $dbconn; |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | - /** |
|
| 193 | - * Get Trap database |
|
| 194 | - * @return Selectable|null : returns DB connexion or null on error. |
|
| 195 | - */ |
|
| 196 | - public function getDb() |
|
| 197 | - { |
|
| 198 | - if ( $this->trapDB != null ) return $this->trapDB; |
|
| 192 | + /** |
|
| 193 | + * Get Trap database |
|
| 194 | + * @return Selectable|null : returns DB connexion or null on error. |
|
| 195 | + */ |
|
| 196 | + public function getDb() |
|
| 197 | + { |
|
| 198 | + if ( $this->trapDB != null ) return $this->trapDB; |
|
| 199 | 199 | |
| 200 | 200 | |
| 201 | - $dbresource=$this->trapController->Config()->get('config', 'database'); |
|
| 201 | + $dbresource=$this->trapController->Config()->get('config', 'database'); |
|
| 202 | 202 | |
| 203 | - if ( ! $dbresource ) |
|
| 204 | - { |
|
| 205 | - $this->trapController->redirectNow('trapdirector/settings?dberror=1'); |
|
| 206 | - return null; |
|
| 207 | - } |
|
| 203 | + if ( ! $dbresource ) |
|
| 204 | + { |
|
| 205 | + $this->trapController->redirectNow('trapdirector/settings?dberror=1'); |
|
| 206 | + return null; |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | - try { |
|
| 210 | - $this->trapDB = $this->getDbByName($dbresource,false,true); |
|
| 211 | - } catch (DBException $e) { |
|
| 212 | - return null; // Should not happen as test = false |
|
| 213 | - } |
|
| 209 | + try { |
|
| 210 | + $this->trapDB = $this->getDbByName($dbresource,false,true); |
|
| 211 | + } catch (DBException $e) { |
|
| 212 | + return null; // Should not happen as test = false |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - //$this->trapDB->getConnection(); |
|
| 215 | + //$this->trapDB->getConnection(); |
|
| 216 | 216 | |
| 217 | - return $this->trapDB; |
|
| 218 | - } |
|
| 217 | + return $this->trapDB; |
|
| 218 | + } |
|
| 219 | 219 | |
| 220 | - /** |
|
| 221 | - * Get Zend adapter of DB. |
|
| 222 | - * @return \Zend_Db_Adapter_Abstract|null |
|
| 223 | - */ |
|
| 224 | - public function getDbConn() |
|
| 225 | - { |
|
| 226 | - if ($this->getDb() == null) return null; |
|
| 227 | - return $this->getDb()->getConnection(); |
|
| 228 | - } |
|
| 220 | + /** |
|
| 221 | + * Get Zend adapter of DB. |
|
| 222 | + * @return \Zend_Db_Adapter_Abstract|null |
|
| 223 | + */ |
|
| 224 | + public function getDbConn() |
|
| 225 | + { |
|
| 226 | + if ($this->getDb() == null) return null; |
|
| 227 | + return $this->getDb()->getConnection(); |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - /** |
|
| 231 | - * Test Trap database |
|
| 232 | - * @param boolean $test |
|
| 233 | - * @throws DBException on error. |
|
| 234 | - * @return \Zend_Db_Adapter_Abstract|array|null : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
| 235 | - */ |
|
| 236 | - public function testGetDb() |
|
| 237 | - { |
|
| 238 | - $dbresource=$this->trapController->Config()->get('config', 'database'); |
|
| 230 | + /** |
|
| 231 | + * Test Trap database |
|
| 232 | + * @param boolean $test |
|
| 233 | + * @throws DBException on error. |
|
| 234 | + * @return \Zend_Db_Adapter_Abstract|array|null : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
| 235 | + */ |
|
| 236 | + public function testGetDb() |
|
| 237 | + { |
|
| 238 | + $dbresource=$this->trapController->Config()->get('config', 'database'); |
|
| 239 | 239 | |
| 240 | - if ( ! $dbresource ) |
|
| 241 | - { |
|
| 242 | - throw new DBException(array(1,'')); |
|
| 243 | - } |
|
| 240 | + if ( ! $dbresource ) |
|
| 241 | + { |
|
| 242 | + throw new DBException(array(1,'')); |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | - $this->trapDB = $this->getDbByName($dbresource,true,true); |
|
| 246 | - return; |
|
| 247 | - } |
|
| 245 | + $this->trapDB = $this->getDbByName($dbresource,true,true); |
|
| 246 | + return; |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | 249 | |
| 250 | - /** |
|
| 251 | - * Get IDO Database |
|
| 252 | - * @return \Zend_Db_Adapter_Abstract|NULL returns DB connexion or null on error. |
|
| 253 | - */ |
|
| 254 | - public function getIdoDb() |
|
| 255 | - { |
|
| 256 | - if ( $this->idoDB != null ) return $this->idoDB; |
|
| 257 | - // TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
|
| 258 | - $dbresource=$this->trapController->Config()->get('config', 'IDOdatabase');; |
|
| 250 | + /** |
|
| 251 | + * Get IDO Database |
|
| 252 | + * @return \Zend_Db_Adapter_Abstract|NULL returns DB connexion or null on error. |
|
| 253 | + */ |
|
| 254 | + public function getIdoDb() |
|
| 255 | + { |
|
| 256 | + if ( $this->idoDB != null ) return $this->idoDB; |
|
| 257 | + // TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
|
| 258 | + $dbresource=$this->trapController->Config()->get('config', 'IDOdatabase');; |
|
| 259 | 259 | |
| 260 | - if ( ! $dbresource ) |
|
| 261 | - { |
|
| 262 | - $this->trapController->redirectNow('trapdirector/settings?idodberror=1'); |
|
| 263 | - return null; |
|
| 264 | - } |
|
| 260 | + if ( ! $dbresource ) |
|
| 261 | + { |
|
| 262 | + $this->trapController->redirectNow('trapdirector/settings?idodberror=1'); |
|
| 263 | + return null; |
|
| 264 | + } |
|
| 265 | 265 | |
| 266 | - try |
|
| 267 | - { |
|
| 268 | - $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
| 269 | - } |
|
| 270 | - catch (Exception $e) |
|
| 271 | - { |
|
| 272 | - $this->trapController->redirectNow('trapdirector/settings?idodberror=2'); |
|
| 273 | - return null; |
|
| 274 | - } |
|
| 266 | + try |
|
| 267 | + { |
|
| 268 | + $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
| 269 | + } |
|
| 270 | + catch (Exception $e) |
|
| 271 | + { |
|
| 272 | + $this->trapController->redirectNow('trapdirector/settings?idodberror=2'); |
|
| 273 | + return null; |
|
| 274 | + } |
|
| 275 | 275 | |
| 276 | - $this->idoDB = $dbconn; |
|
| 277 | - return $this->idoDB; |
|
| 278 | - } |
|
| 276 | + $this->idoDB = $dbconn; |
|
| 277 | + return $this->idoDB; |
|
| 278 | + } |
|
| 279 | 279 | |
| 280 | 280 | |
| 281 | - /** |
|
| 282 | - * Get Zend adapter of DB. |
|
| 283 | - * @return \Zend_Db_Adapter_Abstract|null |
|
| 284 | - */ |
|
| 285 | - public function getIdoDbConn() |
|
| 286 | - { |
|
| 287 | - if ($this->getIdoDb() == null) return null; |
|
| 288 | - return $this->getIdoDb()->getConnection(); |
|
| 289 | - } |
|
| 281 | + /** |
|
| 282 | + * Get Zend adapter of DB. |
|
| 283 | + * @return \Zend_Db_Adapter_Abstract|null |
|
| 284 | + */ |
|
| 285 | + public function getIdoDbConn() |
|
| 286 | + { |
|
| 287 | + if ($this->getIdoDb() == null) return null; |
|
| 288 | + return $this->getIdoDb()->getConnection(); |
|
| 289 | + } |
|
| 290 | 290 | |
| 291 | - /** |
|
| 292 | - * Get IDO Database |
|
| 293 | - * @param boolean $test |
|
| 294 | - * @throws DBException on error |
|
| 295 | - */ |
|
| 296 | - public function testGetIdoDb() |
|
| 297 | - { |
|
| 298 | - // TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
|
| 299 | - $dbresource=$this->trapController->Config()->get('config', 'IDOdatabase');; |
|
| 291 | + /** |
|
| 292 | + * Get IDO Database |
|
| 293 | + * @param boolean $test |
|
| 294 | + * @throws DBException on error |
|
| 295 | + */ |
|
| 296 | + public function testGetIdoDb() |
|
| 297 | + { |
|
| 298 | + // TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
|
| 299 | + $dbresource=$this->trapController->Config()->get('config', 'IDOdatabase');; |
|
| 300 | 300 | |
| 301 | - if ( ! $dbresource ) |
|
| 302 | - { |
|
| 303 | - throw new DBException(array(1,'No database in config.ini')); |
|
| 304 | - } |
|
| 301 | + if ( ! $dbresource ) |
|
| 302 | + { |
|
| 303 | + throw new DBException(array(1,'No database in config.ini')); |
|
| 304 | + } |
|
| 305 | 305 | |
| 306 | - try |
|
| 307 | - { |
|
| 308 | - $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
| 309 | - } |
|
| 310 | - catch (Exception $e) |
|
| 311 | - { |
|
| 312 | - throw new DBException( array(2,"Database $dbresource does not exists in IcingaWeb2") ); |
|
| 313 | - } |
|
| 306 | + try |
|
| 307 | + { |
|
| 308 | + $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
| 309 | + } |
|
| 310 | + catch (Exception $e) |
|
| 311 | + { |
|
| 312 | + throw new DBException( array(2,"Database $dbresource does not exists in IcingaWeb2") ); |
|
| 313 | + } |
|
| 314 | 314 | |
| 315 | - try |
|
| 316 | - { |
|
| 317 | - $query = $dbconn->select() |
|
| 318 | - ->from('icinga_dbversion',array('version')); |
|
| 319 | - $version=$dbconn->fetchRow($query); |
|
| 320 | - if ( ($version == null) || ! property_exists($version,'version') ) |
|
| 321 | - { |
|
| 322 | - throw new DBException( array(4,"$dbresource does not look like an IDO database")); |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - catch (Exception $e) |
|
| 326 | - { |
|
| 327 | - throw new DBException( array(3,"Error connecting to $dbresource : " . $e->getMessage())); |
|
| 328 | - } |
|
| 315 | + try |
|
| 316 | + { |
|
| 317 | + $query = $dbconn->select() |
|
| 318 | + ->from('icinga_dbversion',array('version')); |
|
| 319 | + $version=$dbconn->fetchRow($query); |
|
| 320 | + if ( ($version == null) || ! property_exists($version,'version') ) |
|
| 321 | + { |
|
| 322 | + throw new DBException( array(4,"$dbresource does not look like an IDO database")); |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + catch (Exception $e) |
|
| 326 | + { |
|
| 327 | + throw new DBException( array(3,"Error connecting to $dbresource : " . $e->getMessage())); |
|
| 328 | + } |
|
| 329 | 329 | |
| 330 | - return; |
|
| 331 | - } |
|
| 330 | + return; |
|
| 331 | + } |
|
| 332 | 332 | |
| 333 | 333 | } |
| 334 | 334 | \ No newline at end of file |
@@ -23,14 +23,14 @@ discard block |
||
| 23 | 23 | public function getHostByIP($ip) |
| 24 | 24 | { |
| 25 | 25 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 26 | - $dbConn = $this->getIdoDbConn(); |
|
| 26 | + $dbConn=$this->getIdoDbConn(); |
|
| 27 | 27 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 28 | 28 | |
| 29 | 29 | // TODO : check for SQL injections |
| 30 | 30 | $query=$dbConn->select() |
| 31 | 31 | ->from( |
| 32 | 32 | array('a' => 'icinga_objects'), |
| 33 | - array('name' => 'a.name1','id' => 'object_id')) |
|
| 33 | + array('name' => 'a.name1', 'id' => 'object_id')) |
|
| 34 | 34 | ->join( |
| 35 | 35 | array('b' => 'icinga_hosts'), |
| 36 | 36 | 'b.host_object_id=a.object_id', |
@@ -46,14 +46,14 @@ discard block |
||
| 46 | 46 | public function getHostByName($name) |
| 47 | 47 | { |
| 48 | 48 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 49 | - $dbConn = $this->getIdoDbConn(); |
|
| 49 | + $dbConn=$this->getIdoDbConn(); |
|
| 50 | 50 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 51 | 51 | |
| 52 | 52 | // TODO : check for SQL injections |
| 53 | 53 | $query=$dbConn->select() |
| 54 | 54 | ->from( |
| 55 | 55 | array('a' => 'icinga_objects'), |
| 56 | - array('name' => 'a.name1','id' => 'object_id')) |
|
| 56 | + array('name' => 'a.name1', 'id' => 'object_id')) |
|
| 57 | 57 | ->join( |
| 58 | 58 | array('b' => 'icinga_hosts'), |
| 59 | 59 | 'b.host_object_id=a.object_id', |
@@ -69,13 +69,13 @@ discard block |
||
| 69 | 69 | public function getHostGroupByName($ip) |
| 70 | 70 | { |
| 71 | 71 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 72 | - $dbConn = $this->getIdoDbConn(); |
|
| 72 | + $dbConn=$this->getIdoDbConn(); |
|
| 73 | 73 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 74 | 74 | // TODO : check for SQL injections |
| 75 | 75 | $query=$dbConn->select() |
| 76 | 76 | ->from( |
| 77 | 77 | array('a' => 'icinga_objects'), |
| 78 | - array('name' => 'a.name1','id' => 'object_id')) |
|
| 78 | + array('name' => 'a.name1', 'id' => 'object_id')) |
|
| 79 | 79 | ->join( |
| 80 | 80 | array('b' => 'icinga_hostgroups'), |
| 81 | 81 | 'b.hostgroup_object_id=a.object_id', |
@@ -91,8 +91,8 @@ discard block |
||
| 91 | 91 | */ |
| 92 | 92 | public function getHostInfoByID($id) |
| 93 | 93 | { |
| 94 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 95 | - $dbConn = $this->getIdoDbConn(); |
|
| 94 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
| 95 | + $dbConn=$this->getIdoDbConn(); |
|
| 96 | 96 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 97 | 97 | $query=$dbConn->select() |
| 98 | 98 | ->from( |
@@ -113,18 +113,18 @@ discard block |
||
| 113 | 113 | */ |
| 114 | 114 | public function getHostByObjectID($id) // TODO : duplicate of getHostInfoByID above |
| 115 | 115 | { |
| 116 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 117 | - $dbConn = $this->getIdoDbConn(); |
|
| 116 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
| 117 | + $dbConn=$this->getIdoDbConn(); |
|
| 118 | 118 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 119 | 119 | $query=$dbConn->select() |
| 120 | 120 | ->from( |
| 121 | 121 | array('a' => 'icinga_objects'), |
| 122 | - array('name' => 'a.name1','id' => 'a.object_id')) |
|
| 122 | + array('name' => 'a.name1', 'id' => 'a.object_id')) |
|
| 123 | 123 | ->join( |
| 124 | 124 | array('b' => 'icinga_hosts'), |
| 125 | 125 | 'b.host_object_id=a.object_id', |
| 126 | - array('display_name' => 'b.display_name' , 'ip' => 'b.address', 'ip6' => 'b.address6')) |
|
| 127 | - ->where('a.object_id = ?',$id); |
|
| 126 | + array('display_name' => 'b.display_name', 'ip' => 'b.address', 'ip6' => 'b.address6')) |
|
| 127 | + ->where('a.object_id = ?', $id); |
|
| 128 | 128 | return $dbConn->fetchRow($query); |
| 129 | 129 | } |
| 130 | 130 | |
@@ -136,17 +136,17 @@ discard block |
||
| 136 | 136 | public function getServicesByHostid($id) |
| 137 | 137 | { |
| 138 | 138 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 139 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 140 | - $dbConn = $this->getIdoDbConn(); |
|
| 139 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
| 140 | + $dbConn=$this->getIdoDbConn(); |
|
| 141 | 141 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 142 | 142 | $query=$dbConn->select() |
| 143 | 143 | ->from( |
| 144 | 144 | array('s' => 'icinga_services'), |
| 145 | - array('name' => 's.display_name','id' => 's.service_object_id')) |
|
| 145 | + array('name' => 's.display_name', 'id' => 's.service_object_id')) |
|
| 146 | 146 | ->join( |
| 147 | 147 | array('a' => 'icinga_objects'), |
| 148 | 148 | 's.service_object_id=a.object_id', |
| 149 | - array('is_active'=>'a.is_active','name2'=>'a.name2')) |
|
| 149 | + array('is_active'=>'a.is_active', 'name2'=>'a.name2')) |
|
| 150 | 150 | ->where('s.host_object_id='.$id.' AND a.is_active = 1'); |
| 151 | 151 | return $dbConn->fetchAll($query); |
| 152 | 152 | } |
@@ -159,8 +159,8 @@ discard block |
||
| 159 | 159 | */ |
| 160 | 160 | public function getServicesByHostGroupid($id) |
| 161 | 161 | { |
| 162 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 163 | - $dbConn = $this->getIdoDbConn(); |
|
| 162 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
| 163 | + $dbConn=$this->getIdoDbConn(); |
|
| 164 | 164 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 165 | 165 | $query=$dbConn->select() |
| 166 | 166 | ->from( |
@@ -177,11 +177,11 @@ discard block |
||
| 177 | 177 | foreach ($hosts as $key => $host) |
| 178 | 178 | { // For each host, get all services and add in common_services if not found or add counter |
| 179 | 179 | $host_services=$this->getServicesByHostid($host->host_object_id); |
| 180 | - foreach($host_services as $service) |
|
| 180 | + foreach ($host_services as $service) |
|
| 181 | 181 | { |
| 182 | 182 | if (isset($common_services[$service->name2]['num'])) |
| 183 | 183 | { |
| 184 | - $common_services[$service->name2]['num'] +=1; |
|
| 184 | + $common_services[$service->name2]['num']+=1; |
|
| 185 | 185 | } |
| 186 | 186 | else |
| 187 | 187 | { |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | { |
| 198 | 198 | if ($common_services[$key]['num'] == $num_hosts) |
| 199 | 199 | { |
| 200 | - array_push($result,array($key,$common_services[$key]['name'])); |
|
| 200 | + array_push($result, array($key, $common_services[$key]['name'])); |
|
| 201 | 201 | } |
| 202 | 202 | } |
| 203 | 203 | |
@@ -210,9 +210,9 @@ discard block |
||
| 210 | 210 | * @param $name string service name |
| 211 | 211 | * @return array service id |
| 212 | 212 | */ |
| 213 | - public function getServiceIDByName($hostname,$name) |
|
| 213 | + public function getServiceIDByName($hostname, $name) |
|
| 214 | 214 | { |
| 215 | - $dbConn = $this->getIdoDbConn(); |
|
| 215 | + $dbConn=$this->getIdoDbConn(); |
|
| 216 | 216 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 217 | 217 | |
| 218 | 218 | if ($name == null) |
@@ -223,7 +223,7 @@ discard block |
||
| 223 | 223 | $query=$dbConn->select() |
| 224 | 224 | ->from( |
| 225 | 225 | array('s' => 'icinga_services'), |
| 226 | - array('name' => 's.display_name','id' => 's.service_object_id')) |
|
| 226 | + array('name' => 's.display_name', 'id' => 's.service_object_id')) |
|
| 227 | 227 | ->join( |
| 228 | 228 | array('a' => 'icinga_objects'), |
| 229 | 229 | 's.service_object_id=a.object_id', |
@@ -241,14 +241,14 @@ discard block |
||
| 241 | 241 | public function getObjectNameByid($id) |
| 242 | 242 | { |
| 243 | 243 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 244 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 245 | - $dbConn = $this->getIdoDbConn(); |
|
| 244 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
| 245 | + $dbConn=$this->getIdoDbConn(); |
|
| 246 | 246 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 247 | 247 | |
| 248 | 248 | $query=$dbConn->select() |
| 249 | 249 | ->from( |
| 250 | 250 | array('a' => 'icinga_objects'), |
| 251 | - array('name1' => 'a.name1','name2' => 'a.name2')) |
|
| 251 | + array('name1' => 'a.name1', 'name2' => 'a.name2')) |
|
| 252 | 252 | ->where('a.object_id='.$id.' AND a.is_active = 1'); |
| 253 | 253 | |
| 254 | 254 | return $dbConn->fetchRow($query); |
@@ -24,7 +24,9 @@ discard block |
||
| 24 | 24 | { |
| 25 | 25 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 26 | 26 | $dbConn = $this->getIdoDbConn(); |
| 27 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 27 | + if ($dbConn === null) { |
|
| 28 | + throw new \ErrorException('uncatched db error'); |
|
| 29 | + } |
|
| 28 | 30 | |
| 29 | 31 | // TODO : check for SQL injections |
| 30 | 32 | $query=$dbConn->select() |
@@ -47,7 +49,9 @@ discard block |
||
| 47 | 49 | { |
| 48 | 50 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 49 | 51 | $dbConn = $this->getIdoDbConn(); |
| 50 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 52 | + if ($dbConn === null) { |
|
| 53 | + throw new \ErrorException('uncatched db error'); |
|
| 54 | + } |
|
| 51 | 55 | |
| 52 | 56 | // TODO : check for SQL injections |
| 53 | 57 | $query=$dbConn->select() |
@@ -70,7 +74,9 @@ discard block |
||
| 70 | 74 | { |
| 71 | 75 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 72 | 76 | $dbConn = $this->getIdoDbConn(); |
| 73 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 77 | + if ($dbConn === null) { |
|
| 78 | + throw new \ErrorException('uncatched db error'); |
|
| 79 | + } |
|
| 74 | 80 | // TODO : check for SQL injections |
| 75 | 81 | $query=$dbConn->select() |
| 76 | 82 | ->from( |
@@ -93,7 +99,9 @@ discard block |
||
| 93 | 99 | { |
| 94 | 100 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
| 95 | 101 | $dbConn = $this->getIdoDbConn(); |
| 96 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 102 | + if ($dbConn === null) { |
|
| 103 | + throw new \ErrorException('uncatched db error'); |
|
| 104 | + } |
|
| 97 | 105 | $query=$dbConn->select() |
| 98 | 106 | ->from( |
| 99 | 107 | array('a' => 'icinga_objects'), |
@@ -115,7 +123,9 @@ discard block |
||
| 115 | 123 | { |
| 116 | 124 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
| 117 | 125 | $dbConn = $this->getIdoDbConn(); |
| 118 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 126 | + if ($dbConn === null) { |
|
| 127 | + throw new \ErrorException('uncatched db error'); |
|
| 128 | + } |
|
| 119 | 129 | $query=$dbConn->select() |
| 120 | 130 | ->from( |
| 121 | 131 | array('a' => 'icinga_objects'), |
@@ -138,7 +148,9 @@ discard block |
||
| 138 | 148 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 139 | 149 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
| 140 | 150 | $dbConn = $this->getIdoDbConn(); |
| 141 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 151 | + if ($dbConn === null) { |
|
| 152 | + throw new \ErrorException('uncatched db error'); |
|
| 153 | + } |
|
| 142 | 154 | $query=$dbConn->select() |
| 143 | 155 | ->from( |
| 144 | 156 | array('s' => 'icinga_services'), |
@@ -161,7 +173,9 @@ discard block |
||
| 161 | 173 | { |
| 162 | 174 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
| 163 | 175 | $dbConn = $this->getIdoDbConn(); |
| 164 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 176 | + if ($dbConn === null) { |
|
| 177 | + throw new \ErrorException('uncatched db error'); |
|
| 178 | + } |
|
| 165 | 179 | $query=$dbConn->select() |
| 166 | 180 | ->from( |
| 167 | 181 | array('s' => 'icinga_hostgroup_members'), |
@@ -182,8 +196,7 @@ discard block |
||
| 182 | 196 | if (isset($common_services[$service->name2]['num'])) |
| 183 | 197 | { |
| 184 | 198 | $common_services[$service->name2]['num'] +=1; |
| 185 | - } |
|
| 186 | - else |
|
| 199 | + } else |
|
| 187 | 200 | { |
| 188 | 201 | $common_services[$service->name2]['num']=1; |
| 189 | 202 | $common_services[$service->name2]['name']=$service->name; |
@@ -213,7 +226,9 @@ discard block |
||
| 213 | 226 | public function getServiceIDByName($hostname,$name) |
| 214 | 227 | { |
| 215 | 228 | $dbConn = $this->getIdoDbConn(); |
| 216 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 229 | + if ($dbConn === null) { |
|
| 230 | + throw new \ErrorException('uncatched db error'); |
|
| 231 | + } |
|
| 217 | 232 | |
| 218 | 233 | if ($name == null) |
| 219 | 234 | { |
@@ -243,7 +258,9 @@ discard block |
||
| 243 | 258 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 244 | 259 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
| 245 | 260 | $dbConn = $this->getIdoDbConn(); |
| 246 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 261 | + if ($dbConn === null) { |
|
| 262 | + throw new \ErrorException('uncatched db error'); |
|
| 263 | + } |
|
| 247 | 264 | |
| 248 | 265 | $query=$dbConn->select() |
| 249 | 266 | ->from( |
@@ -18,247 +18,247 @@ |
||
| 18 | 18 | trait IdoDBQuery |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** @return TrapsController */ |
|
| 22 | - abstract protected function getTrapCtrl(); |
|
| 21 | + /** @return TrapsController */ |
|
| 22 | + abstract protected function getTrapCtrl(); |
|
| 23 | 23 | |
| 24 | - /** @return Zend_Db_Adapter_Abstract : returns DB connexion or null on error */ |
|
| 25 | - abstract public function getIdoDbConn(); |
|
| 24 | + /** @return Zend_Db_Adapter_Abstract : returns DB connexion or null on error */ |
|
| 25 | + abstract public function getIdoDbConn(); |
|
| 26 | 26 | |
| 27 | - /** Get host(s) by IP (v4 or v6) or by name in IDO database |
|
| 28 | - * does not catch exceptions |
|
| 29 | - * @return array of objects ( name, id (object_id), display_name) |
|
| 30 | - */ |
|
| 31 | - public function getHostByIP($ip) |
|
| 32 | - { |
|
| 33 | - // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 34 | - $dbConn = $this->getIdoDbConn(); |
|
| 35 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 27 | + /** Get host(s) by IP (v4 or v6) or by name in IDO database |
|
| 28 | + * does not catch exceptions |
|
| 29 | + * @return array of objects ( name, id (object_id), display_name) |
|
| 30 | + */ |
|
| 31 | + public function getHostByIP($ip) |
|
| 32 | + { |
|
| 33 | + // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 34 | + $dbConn = $this->getIdoDbConn(); |
|
| 35 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 36 | 36 | |
| 37 | - // TODO : check for SQL injections |
|
| 38 | - $query=$dbConn->select() |
|
| 39 | - ->from( |
|
| 40 | - array('a' => 'icinga_objects'), |
|
| 41 | - array('name' => 'a.name1','id' => 'object_id')) |
|
| 42 | - ->join( |
|
| 43 | - array('b' => 'icinga_hosts'), |
|
| 44 | - 'b.host_object_id=a.object_id', |
|
| 45 | - array('display_name' => 'b.display_name')) |
|
| 46 | - ->where("(b.address LIKE '%".$ip."%' OR b.address6 LIKE '%".$ip."%' OR a.name1 LIKE '%".$ip."%' OR b.display_name LIKE '%".$ip."%') and a.is_active = 1"); |
|
| 47 | - return $dbConn->fetchAll($query); |
|
| 48 | - } |
|
| 37 | + // TODO : check for SQL injections |
|
| 38 | + $query=$dbConn->select() |
|
| 39 | + ->from( |
|
| 40 | + array('a' => 'icinga_objects'), |
|
| 41 | + array('name' => 'a.name1','id' => 'object_id')) |
|
| 42 | + ->join( |
|
| 43 | + array('b' => 'icinga_hosts'), |
|
| 44 | + 'b.host_object_id=a.object_id', |
|
| 45 | + array('display_name' => 'b.display_name')) |
|
| 46 | + ->where("(b.address LIKE '%".$ip."%' OR b.address6 LIKE '%".$ip."%' OR a.name1 LIKE '%".$ip."%' OR b.display_name LIKE '%".$ip."%') and a.is_active = 1"); |
|
| 47 | + return $dbConn->fetchAll($query); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** Get host(s) by name in IDO database |
|
| 51 | - * does not catch exceptions |
|
| 52 | - * @return array of objects ( name, id (object_id), display_name) |
|
| 53 | - */ |
|
| 54 | - public function getHostByName($name) |
|
| 55 | - { |
|
| 56 | - // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 57 | - $dbConn = $this->getIdoDbConn(); |
|
| 58 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 50 | + /** Get host(s) by name in IDO database |
|
| 51 | + * does not catch exceptions |
|
| 52 | + * @return array of objects ( name, id (object_id), display_name) |
|
| 53 | + */ |
|
| 54 | + public function getHostByName($name) |
|
| 55 | + { |
|
| 56 | + // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 57 | + $dbConn = $this->getIdoDbConn(); |
|
| 58 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 59 | 59 | |
| 60 | - // TODO : check for SQL injections |
|
| 61 | - $query=$dbConn->select() |
|
| 62 | - ->from( |
|
| 63 | - array('a' => 'icinga_objects'), |
|
| 64 | - array('name' => 'a.name1','id' => 'object_id')) |
|
| 65 | - ->join( |
|
| 66 | - array('b' => 'icinga_hosts'), |
|
| 67 | - 'b.host_object_id=a.object_id', |
|
| 68 | - array('display_name' => 'b.display_name')) |
|
| 69 | - ->where("a.name1 = '$name'"); |
|
| 70 | - return $dbConn->fetchAll($query); |
|
| 71 | - } |
|
| 60 | + // TODO : check for SQL injections |
|
| 61 | + $query=$dbConn->select() |
|
| 62 | + ->from( |
|
| 63 | + array('a' => 'icinga_objects'), |
|
| 64 | + array('name' => 'a.name1','id' => 'object_id')) |
|
| 65 | + ->join( |
|
| 66 | + array('b' => 'icinga_hosts'), |
|
| 67 | + 'b.host_object_id=a.object_id', |
|
| 68 | + array('display_name' => 'b.display_name')) |
|
| 69 | + ->where("a.name1 = '$name'"); |
|
| 70 | + return $dbConn->fetchAll($query); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** Get host groups by name in IDO database |
|
| 74 | - * does not catch exceptions |
|
| 75 | - * @return array of objects ( name, id (object_id), display_name) |
|
| 76 | - */ |
|
| 77 | - public function getHostGroupByName($ip) |
|
| 78 | - { |
|
| 79 | - // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 80 | - $dbConn = $this->getIdoDbConn(); |
|
| 81 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 82 | - // TODO : check for SQL injections |
|
| 83 | - $query=$dbConn->select() |
|
| 84 | - ->from( |
|
| 85 | - array('a' => 'icinga_objects'), |
|
| 86 | - array('name' => 'a.name1','id' => 'object_id')) |
|
| 87 | - ->join( |
|
| 88 | - array('b' => 'icinga_hostgroups'), |
|
| 89 | - 'b.hostgroup_object_id=a.object_id', |
|
| 90 | - array('display_name' => 'b.alias')) |
|
| 91 | - ->where("(a.name1 LIKE '%".$ip."%' OR b.alias LIKE '%".$ip."%') and a.is_active = 1"); |
|
| 92 | - return $dbConn->fetchAll($query); |
|
| 93 | - } |
|
| 73 | + /** Get host groups by name in IDO database |
|
| 74 | + * does not catch exceptions |
|
| 75 | + * @return array of objects ( name, id (object_id), display_name) |
|
| 76 | + */ |
|
| 77 | + public function getHostGroupByName($ip) |
|
| 78 | + { |
|
| 79 | + // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 80 | + $dbConn = $this->getIdoDbConn(); |
|
| 81 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 82 | + // TODO : check for SQL injections |
|
| 83 | + $query=$dbConn->select() |
|
| 84 | + ->from( |
|
| 85 | + array('a' => 'icinga_objects'), |
|
| 86 | + array('name' => 'a.name1','id' => 'object_id')) |
|
| 87 | + ->join( |
|
| 88 | + array('b' => 'icinga_hostgroups'), |
|
| 89 | + 'b.hostgroup_object_id=a.object_id', |
|
| 90 | + array('display_name' => 'b.alias')) |
|
| 91 | + ->where("(a.name1 LIKE '%".$ip."%' OR b.alias LIKE '%".$ip."%') and a.is_active = 1"); |
|
| 92 | + return $dbConn->fetchAll($query); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** Get host IP (v4 and v6) by name in IDO database |
|
| 96 | - * does not catch exceptions |
|
| 97 | - * @return array ( name, display_name, ip4, ip6) |
|
| 98 | - */ |
|
| 99 | - public function getHostInfoByID($id) |
|
| 100 | - { |
|
| 101 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 102 | - $dbConn = $this->getIdoDbConn(); |
|
| 103 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 104 | - $query=$dbConn->select() |
|
| 105 | - ->from( |
|
| 106 | - array('a' => 'icinga_objects'), |
|
| 107 | - array('name' => 'a.name1')) |
|
| 108 | - ->join( |
|
| 109 | - array('b' => 'icinga_hosts'), |
|
| 110 | - 'b.host_object_id=a.object_id', |
|
| 111 | - array('ip4' => 'b.address', 'ip6' => 'b.address6', 'display_name' => 'b.display_name')) |
|
| 112 | - ->where("a.object_id = '".$id."'"); |
|
| 113 | - return $dbConn->fetchRow($query); |
|
| 114 | - } |
|
| 95 | + /** Get host IP (v4 and v6) by name in IDO database |
|
| 96 | + * does not catch exceptions |
|
| 97 | + * @return array ( name, display_name, ip4, ip6) |
|
| 98 | + */ |
|
| 99 | + public function getHostInfoByID($id) |
|
| 100 | + { |
|
| 101 | + if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 102 | + $dbConn = $this->getIdoDbConn(); |
|
| 103 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 104 | + $query=$dbConn->select() |
|
| 105 | + ->from( |
|
| 106 | + array('a' => 'icinga_objects'), |
|
| 107 | + array('name' => 'a.name1')) |
|
| 108 | + ->join( |
|
| 109 | + array('b' => 'icinga_hosts'), |
|
| 110 | + 'b.host_object_id=a.object_id', |
|
| 111 | + array('ip4' => 'b.address', 'ip6' => 'b.address6', 'display_name' => 'b.display_name')) |
|
| 112 | + ->where("a.object_id = '".$id."'"); |
|
| 113 | + return $dbConn->fetchRow($query); |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - /** Get host by objectid in IDO database |
|
| 117 | - * does not catch exceptions |
|
| 118 | - * @return array of objects ( id, name, display_name, ip, ip6, ) |
|
| 119 | - */ |
|
| 120 | - public function getHostByObjectID($id) // TODO : duplicate of getHostInfoByID above |
|
| 121 | - { |
|
| 122 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 123 | - $dbConn = $this->getIdoDbConn(); |
|
| 124 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 125 | - $query=$dbConn->select() |
|
| 126 | - ->from( |
|
| 127 | - array('a' => 'icinga_objects'), |
|
| 128 | - array('name' => 'a.name1','id' => 'a.object_id')) |
|
| 129 | - ->join( |
|
| 130 | - array('b' => 'icinga_hosts'), |
|
| 131 | - 'b.host_object_id=a.object_id', |
|
| 132 | - array('display_name' => 'b.display_name' , 'ip' => 'b.address', 'ip6' => 'b.address6')) |
|
| 133 | - ->where('a.object_id = ?',$id); |
|
| 134 | - return $dbConn->fetchRow($query); |
|
| 135 | - } |
|
| 116 | + /** Get host by objectid in IDO database |
|
| 117 | + * does not catch exceptions |
|
| 118 | + * @return array of objects ( id, name, display_name, ip, ip6, ) |
|
| 119 | + */ |
|
| 120 | + public function getHostByObjectID($id) // TODO : duplicate of getHostInfoByID above |
|
| 121 | + { |
|
| 122 | + if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 123 | + $dbConn = $this->getIdoDbConn(); |
|
| 124 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 125 | + $query=$dbConn->select() |
|
| 126 | + ->from( |
|
| 127 | + array('a' => 'icinga_objects'), |
|
| 128 | + array('name' => 'a.name1','id' => 'a.object_id')) |
|
| 129 | + ->join( |
|
| 130 | + array('b' => 'icinga_hosts'), |
|
| 131 | + 'b.host_object_id=a.object_id', |
|
| 132 | + array('display_name' => 'b.display_name' , 'ip' => 'b.address', 'ip6' => 'b.address6')) |
|
| 133 | + ->where('a.object_id = ?',$id); |
|
| 134 | + return $dbConn->fetchRow($query); |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - /** Get services from object ( host_object_id) in IDO database |
|
| 138 | - * does not catch exceptions |
|
| 139 | - * @param $id int object_id |
|
| 140 | - * @return array display_name (of service), service_object_id |
|
| 141 | - */ |
|
| 142 | - public function getServicesByHostid($id) |
|
| 143 | - { |
|
| 144 | - // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 145 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 146 | - $dbConn = $this->getIdoDbConn(); |
|
| 147 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 148 | - $query=$dbConn->select() |
|
| 149 | - ->from( |
|
| 150 | - array('s' => 'icinga_services'), |
|
| 151 | - array('name' => 's.display_name','id' => 's.service_object_id')) |
|
| 152 | - ->join( |
|
| 153 | - array('a' => 'icinga_objects'), |
|
| 154 | - 's.service_object_id=a.object_id', |
|
| 155 | - array('is_active'=>'a.is_active','name2'=>'a.name2')) |
|
| 156 | - ->where('s.host_object_id='.$id.' AND a.is_active = 1'); |
|
| 157 | - return $dbConn->fetchAll($query); |
|
| 158 | - } |
|
| 137 | + /** Get services from object ( host_object_id) in IDO database |
|
| 138 | + * does not catch exceptions |
|
| 139 | + * @param $id int object_id |
|
| 140 | + * @return array display_name (of service), service_object_id |
|
| 141 | + */ |
|
| 142 | + public function getServicesByHostid($id) |
|
| 143 | + { |
|
| 144 | + // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 145 | + if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 146 | + $dbConn = $this->getIdoDbConn(); |
|
| 147 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 148 | + $query=$dbConn->select() |
|
| 149 | + ->from( |
|
| 150 | + array('s' => 'icinga_services'), |
|
| 151 | + array('name' => 's.display_name','id' => 's.service_object_id')) |
|
| 152 | + ->join( |
|
| 153 | + array('a' => 'icinga_objects'), |
|
| 154 | + 's.service_object_id=a.object_id', |
|
| 155 | + array('is_active'=>'a.is_active','name2'=>'a.name2')) |
|
| 156 | + ->where('s.host_object_id='.$id.' AND a.is_active = 1'); |
|
| 157 | + return $dbConn->fetchAll($query); |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** Get services from hostgroup object id ( hostgroup_object_id) in IDO database |
|
| 161 | - * gets all hosts in hostgroup and return common services |
|
| 162 | - * does not catch exceptions |
|
| 163 | - * @param $id int object_id |
|
| 164 | - * @return array display_name (of service), service_object_id |
|
| 165 | - */ |
|
| 166 | - public function getServicesByHostGroupid($id) |
|
| 167 | - { |
|
| 168 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 169 | - $dbConn = $this->getIdoDbConn(); |
|
| 170 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 171 | - $query=$dbConn->select() |
|
| 172 | - ->from( |
|
| 173 | - array('s' => 'icinga_hostgroup_members'), |
|
| 174 | - array('host_object_id' => 's.host_object_id')) |
|
| 175 | - ->join( |
|
| 176 | - array('a' => 'icinga_hostgroups'), |
|
| 177 | - 's.hostgroup_id=a.hostgroup_id', |
|
| 178 | - 'hostgroup_object_id') |
|
| 179 | - ->where('a.hostgroup_object_id='.$id); |
|
| 180 | - $hosts=$dbConn->fetchAll($query); |
|
| 181 | - $common_services=array(); |
|
| 182 | - $num_hosts=count($hosts); |
|
| 183 | - foreach ($hosts as $key => $host) |
|
| 184 | - { // For each host, get all services and add in common_services if not found or add counter |
|
| 185 | - $host_services=$this->getServicesByHostid($host->host_object_id); |
|
| 186 | - foreach($host_services as $service) |
|
| 187 | - { |
|
| 188 | - if (isset($common_services[$service->name2]['num'])) |
|
| 189 | - { |
|
| 190 | - $common_services[$service->name2]['num'] +=1; |
|
| 191 | - } |
|
| 192 | - else |
|
| 193 | - { |
|
| 194 | - $common_services[$service->name2]['num']=1; |
|
| 195 | - $common_services[$service->name2]['name']=$service->name; |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - $result=array(); |
|
| 160 | + /** Get services from hostgroup object id ( hostgroup_object_id) in IDO database |
|
| 161 | + * gets all hosts in hostgroup and return common services |
|
| 162 | + * does not catch exceptions |
|
| 163 | + * @param $id int object_id |
|
| 164 | + * @return array display_name (of service), service_object_id |
|
| 165 | + */ |
|
| 166 | + public function getServicesByHostGroupid($id) |
|
| 167 | + { |
|
| 168 | + if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 169 | + $dbConn = $this->getIdoDbConn(); |
|
| 170 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 171 | + $query=$dbConn->select() |
|
| 172 | + ->from( |
|
| 173 | + array('s' => 'icinga_hostgroup_members'), |
|
| 174 | + array('host_object_id' => 's.host_object_id')) |
|
| 175 | + ->join( |
|
| 176 | + array('a' => 'icinga_hostgroups'), |
|
| 177 | + 's.hostgroup_id=a.hostgroup_id', |
|
| 178 | + 'hostgroup_object_id') |
|
| 179 | + ->where('a.hostgroup_object_id='.$id); |
|
| 180 | + $hosts=$dbConn->fetchAll($query); |
|
| 181 | + $common_services=array(); |
|
| 182 | + $num_hosts=count($hosts); |
|
| 183 | + foreach ($hosts as $key => $host) |
|
| 184 | + { // For each host, get all services and add in common_services if not found or add counter |
|
| 185 | + $host_services=$this->getServicesByHostid($host->host_object_id); |
|
| 186 | + foreach($host_services as $service) |
|
| 187 | + { |
|
| 188 | + if (isset($common_services[$service->name2]['num'])) |
|
| 189 | + { |
|
| 190 | + $common_services[$service->name2]['num'] +=1; |
|
| 191 | + } |
|
| 192 | + else |
|
| 193 | + { |
|
| 194 | + $common_services[$service->name2]['num']=1; |
|
| 195 | + $common_services[$service->name2]['name']=$service->name; |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + $result=array(); |
|
| 200 | 200 | |
| 201 | - //print_r($common_services); |
|
| 202 | - foreach (array_keys($common_services) as $key) |
|
| 203 | - { |
|
| 204 | - if ($common_services[$key]['num'] == $num_hosts) |
|
| 205 | - { |
|
| 206 | - array_push($result,array($key,$common_services[$key]['name'])); |
|
| 207 | - } |
|
| 208 | - } |
|
| 201 | + //print_r($common_services); |
|
| 202 | + foreach (array_keys($common_services) as $key) |
|
| 203 | + { |
|
| 204 | + if ($common_services[$key]['num'] == $num_hosts) |
|
| 205 | + { |
|
| 206 | + array_push($result,array($key,$common_services[$key]['name'])); |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | 209 | |
| 210 | - return $result; |
|
| 211 | - } |
|
| 210 | + return $result; |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | - /** Get services object id by host name / service name in IDO database |
|
| 214 | - * does not catch exceptions |
|
| 215 | - * @param $hostname string host name |
|
| 216 | - * @param $name string service name |
|
| 217 | - * @return array service id |
|
| 218 | - */ |
|
| 219 | - public function getServiceIDByName($hostname,$name) |
|
| 220 | - { |
|
| 221 | - $dbConn = $this->getIdoDbConn(); |
|
| 222 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 213 | + /** Get services object id by host name / service name in IDO database |
|
| 214 | + * does not catch exceptions |
|
| 215 | + * @param $hostname string host name |
|
| 216 | + * @param $name string service name |
|
| 217 | + * @return array service id |
|
| 218 | + */ |
|
| 219 | + public function getServiceIDByName($hostname,$name) |
|
| 220 | + { |
|
| 221 | + $dbConn = $this->getIdoDbConn(); |
|
| 222 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 223 | 223 | |
| 224 | - if ($name == null) |
|
| 225 | - { |
|
| 226 | - return array(); |
|
| 227 | - } |
|
| 224 | + if ($name == null) |
|
| 225 | + { |
|
| 226 | + return array(); |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | - $query=$dbConn->select() |
|
| 230 | - ->from( |
|
| 231 | - array('s' => 'icinga_services'), |
|
| 232 | - array('name' => 's.display_name','id' => 's.service_object_id')) |
|
| 233 | - ->join( |
|
| 234 | - array('a' => 'icinga_objects'), |
|
| 235 | - 's.service_object_id=a.object_id', |
|
| 236 | - 'is_active') |
|
| 237 | - ->where('a.name2=\''.$name.'\' AND a.name1=\''.$hostname.'\' AND a.is_active = 1'); |
|
| 229 | + $query=$dbConn->select() |
|
| 230 | + ->from( |
|
| 231 | + array('s' => 'icinga_services'), |
|
| 232 | + array('name' => 's.display_name','id' => 's.service_object_id')) |
|
| 233 | + ->join( |
|
| 234 | + array('a' => 'icinga_objects'), |
|
| 235 | + 's.service_object_id=a.object_id', |
|
| 236 | + 'is_active') |
|
| 237 | + ->where('a.name2=\''.$name.'\' AND a.name1=\''.$hostname.'\' AND a.is_active = 1'); |
|
| 238 | 238 | |
| 239 | - return $dbConn->fetchAll($query); |
|
| 240 | - } |
|
| 239 | + return $dbConn->fetchAll($query); |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - /** Get object name from object_id in IDO database |
|
| 243 | - * does not catch exceptions |
|
| 244 | - * @param int $id object_id (default to null, used first if not null) |
|
| 245 | - * @return array name1 (host) name2 (service) |
|
| 246 | - */ |
|
| 247 | - public function getObjectNameByid($id) |
|
| 248 | - { |
|
| 249 | - // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 250 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 251 | - $dbConn = $this->getIdoDbConn(); |
|
| 252 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 242 | + /** Get object name from object_id in IDO database |
|
| 243 | + * does not catch exceptions |
|
| 244 | + * @param int $id object_id (default to null, used first if not null) |
|
| 245 | + * @return array name1 (host) name2 (service) |
|
| 246 | + */ |
|
| 247 | + public function getObjectNameByid($id) |
|
| 248 | + { |
|
| 249 | + // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 250 | + if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 251 | + $dbConn = $this->getIdoDbConn(); |
|
| 252 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 253 | 253 | |
| 254 | - $query=$dbConn->select() |
|
| 255 | - ->from( |
|
| 256 | - array('a' => 'icinga_objects'), |
|
| 257 | - array('name1' => 'a.name1','name2' => 'a.name2')) |
|
| 258 | - ->where('a.object_id='.$id.' AND a.is_active = 1'); |
|
| 254 | + $query=$dbConn->select() |
|
| 255 | + ->from( |
|
| 256 | + array('a' => 'icinga_objects'), |
|
| 257 | + array('name1' => 'a.name1','name2' => 'a.name2')) |
|
| 258 | + ->where('a.object_id='.$id.' AND a.is_active = 1'); |
|
| 259 | 259 | |
| 260 | - return $dbConn->fetchRow($query); |
|
| 261 | - } |
|
| 260 | + return $dbConn->fetchRow($query); |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | 263 | |
| 264 | 264 | } |
| 265 | 265 | \ No newline at end of file |
@@ -74,8 +74,7 @@ discard block |
||
| 74 | 74 | } |
| 75 | 75 | $this->getLogging()->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
| 76 | 76 | return 1; |
| 77 | - } |
|
| 78 | - else |
|
| 77 | + } else |
|
| 79 | 78 | { |
| 80 | 79 | $this->getLogging()->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
| 81 | 80 | return 0; |
@@ -97,7 +96,9 @@ discard block |
||
| 97 | 96 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
| 98 | 97 | ', :syntax, :type_enum, :description )'; |
| 99 | 98 | |
| 100 | - if ($this->getTrapsDB()->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 99 | + if ($this->getTrapsDB()->trapDBType == 'pgsql') { |
|
| 100 | + $sql .= 'RETURNING id'; |
|
| 101 | + } |
|
| 101 | 102 | |
| 102 | 103 | $sqlQuery=$db_conn->prepare($sql); |
| 103 | 104 | |
@@ -139,7 +140,9 @@ discard block |
||
| 139 | 140 | } |
| 140 | 141 | |
| 141 | 142 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 142 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 143 | + if ($inserted_id==false) { |
|
| 144 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 145 | + } |
|
| 143 | 146 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
| 144 | 147 | break; |
| 145 | 148 | default: |
@@ -12,183 +12,183 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | trait MibDatabase |
| 14 | 14 | { |
| 15 | - /** @var array $oidDesc MUST be overide by calling class */ |
|
| 16 | - //protected $oidDesc; |
|
| 15 | + /** @var array $oidDesc MUST be overide by calling class */ |
|
| 16 | + //protected $oidDesc; |
|
| 17 | 17 | |
| 18 | - /** @var array $dbOidIndex MUST be overide by calling class */ |
|
| 19 | - //protected $dbOidIndex; |
|
| 18 | + /** @var array $dbOidIndex MUST be overide by calling class */ |
|
| 19 | + //protected $dbOidIndex; |
|
| 20 | 20 | |
| 21 | - /** @return \Trapdirector\Logging */ |
|
| 22 | - abstract public function getLogging(); |
|
| 21 | + /** @return \Trapdirector\Logging */ |
|
| 22 | + abstract public function getLogging(); |
|
| 23 | 23 | |
| 24 | - /** @return \Trapdirector\Database */ |
|
| 25 | - abstract public function getTrapsDB(); |
|
| 24 | + /** @return \Trapdirector\Database */ |
|
| 25 | + abstract public function getTrapsDB(); |
|
| 26 | 26 | |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Update or add an OID to database uses $this->dbOidIndex for mem cache |
|
| 30 | - * and $this->oidDesc doe data |
|
| 31 | - * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 32 | - */ |
|
| 33 | - public function update_oid() |
|
| 34 | - { |
|
| 35 | - $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 36 | - // Quote description. |
|
| 37 | - $this->oidDesc['description']=$db_conn->quote($this->oidDesc['description']); |
|
| 28 | + /** |
|
| 29 | + * Update or add an OID to database uses $this->dbOidIndex for mem cache |
|
| 30 | + * and $this->oidDesc doe data |
|
| 31 | + * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 32 | + */ |
|
| 33 | + public function update_oid() |
|
| 34 | + { |
|
| 35 | + $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 36 | + // Quote description. |
|
| 37 | + $this->oidDesc['description']=$db_conn->quote($this->oidDesc['description']); |
|
| 38 | 38 | |
| 39 | - if (isset($this->dbOidIndex[$this->oidDesc['oid']])) |
|
| 40 | - { // oid exists in db, so update |
|
| 41 | - return $this->update_oid_update(); |
|
| 42 | - } |
|
| 43 | - // create new OID. |
|
| 44 | - return $this->update_oid_create(); |
|
| 39 | + if (isset($this->dbOidIndex[$this->oidDesc['oid']])) |
|
| 40 | + { // oid exists in db, so update |
|
| 41 | + return $this->update_oid_update(); |
|
| 42 | + } |
|
| 43 | + // create new OID. |
|
| 44 | + return $this->update_oid_create(); |
|
| 45 | 45 | |
| 46 | - } |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Update object in DB with object in dbOidIndex if name/mib/type has changed. |
|
| 50 | - * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 51 | - */ |
|
| 52 | - private function update_oid_update() |
|
| 53 | - { |
|
| 48 | + /** |
|
| 49 | + * Update object in DB with object in dbOidIndex if name/mib/type has changed. |
|
| 50 | + * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 51 | + */ |
|
| 52 | + private function update_oid_update() |
|
| 53 | + { |
|
| 54 | 54 | |
| 55 | - $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 55 | + $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 56 | 56 | |
| 57 | - if ($this->dbOidIndex[$this->oidDesc['oid']]['key'] == -1) |
|
| 58 | - { // newly created. |
|
| 59 | - return 0; |
|
| 60 | - } |
|
| 61 | - $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll |
|
| 62 | - $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element |
|
| 63 | - if ( $this->oidDesc['name'] != $dbOid['name'] || |
|
| 64 | - $this->oidDesc['mib'] != $dbOid['mib'] || |
|
| 65 | - $this->oidDesc['type'] !=$dbOid['type'] |
|
| 66 | - ) |
|
| 67 | - { // Do update |
|
| 68 | - $sql='UPDATE '.$this->getTrapsDB()->dbPrefix.'mib_cache SET '. |
|
| 69 | - 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
| 70 | - ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
| 71 | - ' WHERE id= :id'; |
|
| 72 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 57 | + if ($this->dbOidIndex[$this->oidDesc['oid']]['key'] == -1) |
|
| 58 | + { // newly created. |
|
| 59 | + return 0; |
|
| 60 | + } |
|
| 61 | + $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll |
|
| 62 | + $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element |
|
| 63 | + if ( $this->oidDesc['name'] != $dbOid['name'] || |
|
| 64 | + $this->oidDesc['mib'] != $dbOid['mib'] || |
|
| 65 | + $this->oidDesc['type'] !=$dbOid['type'] |
|
| 66 | + ) |
|
| 67 | + { // Do update |
|
| 68 | + $sql='UPDATE '.$this->getTrapsDB()->dbPrefix.'mib_cache SET '. |
|
| 69 | + 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
| 70 | + ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
| 71 | + ' WHERE id= :id'; |
|
| 72 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 73 | 73 | |
| 74 | - $sqlParam=array( |
|
| 75 | - ':name' => $this->oidDesc['name'], |
|
| 76 | - ':type' => $this->oidDesc['type'], |
|
| 77 | - ':mib' => $this->oidDesc['mib'], |
|
| 78 | - ':tc' => $this->oidDesc['textconv']??'null', |
|
| 79 | - ':display_hint' => $this->oidDesc['dispHint']??'null' , |
|
| 80 | - ':syntax' => $this->oidDesc['syntax']==null??'null', |
|
| 81 | - ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 82 | - ':description' => $this->oidDesc['description']??'null', |
|
| 83 | - ':id' => $this->dbOidAll[$this->dbOidIndex[$this->oidDesc['oid']]['id']] |
|
| 84 | - ); |
|
| 74 | + $sqlParam=array( |
|
| 75 | + ':name' => $this->oidDesc['name'], |
|
| 76 | + ':type' => $this->oidDesc['type'], |
|
| 77 | + ':mib' => $this->oidDesc['mib'], |
|
| 78 | + ':tc' => $this->oidDesc['textconv']??'null', |
|
| 79 | + ':display_hint' => $this->oidDesc['dispHint']??'null' , |
|
| 80 | + ':syntax' => $this->oidDesc['syntax']==null??'null', |
|
| 81 | + ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 82 | + ':description' => $this->oidDesc['description']??'null', |
|
| 83 | + ':id' => $this->dbOidAll[$this->dbOidIndex[$this->oidDesc['oid']]['id']] |
|
| 84 | + ); |
|
| 85 | 85 | |
| 86 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 87 | - $this->getLogging()->log('Error in query : ' . $sql,ERROR,''); |
|
| 88 | - } |
|
| 89 | - $this->getLogging()->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 90 | - return 1; |
|
| 91 | - } |
|
| 92 | - else |
|
| 93 | - { |
|
| 94 | - $this->getLogging()->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 95 | - return 0; |
|
| 96 | - } |
|
| 97 | - } |
|
| 86 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 87 | + $this->getLogging()->log('Error in query : ' . $sql,ERROR,''); |
|
| 88 | + } |
|
| 89 | + $this->getLogging()->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 90 | + return 1; |
|
| 91 | + } |
|
| 92 | + else |
|
| 93 | + { |
|
| 94 | + $this->getLogging()->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 95 | + return 0; |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Create object in DB with object in dbOidIndex |
|
| 101 | - * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 102 | - */ |
|
| 103 | - private function update_oid_create() |
|
| 104 | - { |
|
| 105 | - // Insert data |
|
| 99 | + /** |
|
| 100 | + * Create object in DB with object in dbOidIndex |
|
| 101 | + * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 102 | + */ |
|
| 103 | + private function update_oid_create() |
|
| 104 | + { |
|
| 105 | + // Insert data |
|
| 106 | 106 | |
| 107 | - $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 108 | - $sql='INSERT INTO '.$this->getTrapsDB()->dbPrefix.'mib_cache '. |
|
| 109 | - '(oid, name, type , mib, textual_convention, display_hint '. |
|
| 110 | - ', syntax, type_enum , description ) ' . |
|
| 111 | - 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
| 112 | - ', :syntax, :type_enum, :description )'; |
|
| 107 | + $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 108 | + $sql='INSERT INTO '.$this->getTrapsDB()->dbPrefix.'mib_cache '. |
|
| 109 | + '(oid, name, type , mib, textual_convention, display_hint '. |
|
| 110 | + ', syntax, type_enum , description ) ' . |
|
| 111 | + 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
| 112 | + ', :syntax, :type_enum, :description )'; |
|
| 113 | 113 | |
| 114 | - if ($this->getTrapsDB()->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 114 | + if ($this->getTrapsDB()->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 115 | 115 | |
| 116 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 116 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 117 | 117 | |
| 118 | - $sqlParam=array( |
|
| 119 | - ':oid' => $this->oidDesc['oid'], |
|
| 120 | - ':name' => $this->oidDesc['name'], |
|
| 121 | - ':type' => $this->oidDesc['type'], |
|
| 122 | - ':mib' => $this->oidDesc['mib'], |
|
| 123 | - ':tc' => $this->oidDesc['textconv']??'null', |
|
| 124 | - ':display_hint' => $this->oidDesc['dispHint']??'null', |
|
| 125 | - ':syntax' => $this->oidDesc['syntax']??'null', |
|
| 126 | - ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 127 | - ':description' => $this->oidDesc['description']??'null' |
|
| 128 | - ); |
|
| 118 | + $sqlParam=array( |
|
| 119 | + ':oid' => $this->oidDesc['oid'], |
|
| 120 | + ':name' => $this->oidDesc['name'], |
|
| 121 | + ':type' => $this->oidDesc['type'], |
|
| 122 | + ':mib' => $this->oidDesc['mib'], |
|
| 123 | + ':tc' => $this->oidDesc['textconv']??'null', |
|
| 124 | + ':display_hint' => $this->oidDesc['dispHint']??'null', |
|
| 125 | + ':syntax' => $this->oidDesc['syntax']??'null', |
|
| 126 | + ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 127 | + ':description' => $this->oidDesc['description']??'null' |
|
| 128 | + ); |
|
| 129 | 129 | |
| 130 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 131 | - $this->getLogging()->log('Error in query : ' . $sql,1,''); |
|
| 132 | - } |
|
| 130 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 131 | + $this->getLogging()->log('Error in query : ' . $sql,1,''); |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - switch ($this->getTrapsDB()->trapDBType) |
|
| 135 | - { |
|
| 136 | - case 'pgsql': |
|
| 137 | - // Get last id to insert oid/values in secondary table |
|
| 138 | - if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 139 | - $this->getLogging()->log('Error getting id - pgsql - ',1,''); |
|
| 140 | - } |
|
| 141 | - if (! isset($inserted_id_ret['id'])) { |
|
| 142 | - $this->getLogging()->log('Error getting id - pgsql - empty.',ERROR); |
|
| 143 | - return 0; |
|
| 144 | - } |
|
| 145 | - $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id']; |
|
| 146 | - break; |
|
| 147 | - case 'mysql': |
|
| 148 | - // Get last id to insert oid/values in secondary table |
|
| 149 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 150 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 151 | - $this->getLogging()->log('Erreur getting id - mysql - ',ERROR); |
|
| 152 | - return 0; |
|
| 153 | - } |
|
| 134 | + switch ($this->getTrapsDB()->trapDBType) |
|
| 135 | + { |
|
| 136 | + case 'pgsql': |
|
| 137 | + // Get last id to insert oid/values in secondary table |
|
| 138 | + if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 139 | + $this->getLogging()->log('Error getting id - pgsql - ',1,''); |
|
| 140 | + } |
|
| 141 | + if (! isset($inserted_id_ret['id'])) { |
|
| 142 | + $this->getLogging()->log('Error getting id - pgsql - empty.',ERROR); |
|
| 143 | + return 0; |
|
| 144 | + } |
|
| 145 | + $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id']; |
|
| 146 | + break; |
|
| 147 | + case 'mysql': |
|
| 148 | + // Get last id to insert oid/values in secondary table |
|
| 149 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 150 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 151 | + $this->getLogging()->log('Erreur getting id - mysql - ',ERROR); |
|
| 152 | + return 0; |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 156 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 157 | - $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
|
| 158 | - break; |
|
| 159 | - default: |
|
| 160 | - $this->getLogging()->log('Error SQL type Unknown : '.$this->getTrapsDB()->trapDBType,ERROR); |
|
| 161 | - return 0; |
|
| 162 | - } |
|
| 155 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 156 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 157 | + $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
|
| 158 | + break; |
|
| 159 | + default: |
|
| 160 | + $this->getLogging()->log('Error SQL type Unknown : '.$this->getTrapsDB()->trapDBType,ERROR); |
|
| 161 | + return 0; |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - // Set as newly created. |
|
| 165 | - $this->dbOidIndex[$this->oidDesc['oid']]['key']=-1; |
|
| 166 | - return 2; |
|
| 167 | - } |
|
| 164 | + // Set as newly created. |
|
| 165 | + $this->dbOidIndex[$this->oidDesc['oid']]['key']=-1; |
|
| 166 | + return 2; |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * get all objects for a trap. |
|
| 171 | - * @param integer $trapId |
|
| 172 | - * @return array : array of cached objects |
|
| 173 | - */ |
|
| 174 | - private function cache_db_objects($trapId) |
|
| 175 | - { |
|
| 176 | - $dbObjects=array(); // cache of objects for trap in db |
|
| 177 | - $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 178 | - // Get all objects |
|
| 179 | - $sql='SELECT * FROM '.$this->getTrapsDB()->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
| 180 | - $this->getLogging()->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 181 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 182 | - $this->getLogging()->log('No result in query : ' . $sql,1,''); |
|
| 183 | - } |
|
| 184 | - $dbObjectsRaw=$ret_code->fetchAll(); |
|
| 169 | + /** |
|
| 170 | + * get all objects for a trap. |
|
| 171 | + * @param integer $trapId |
|
| 172 | + * @return array : array of cached objects |
|
| 173 | + */ |
|
| 174 | + private function cache_db_objects($trapId) |
|
| 175 | + { |
|
| 176 | + $dbObjects=array(); // cache of objects for trap in db |
|
| 177 | + $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 178 | + // Get all objects |
|
| 179 | + $sql='SELECT * FROM '.$this->getTrapsDB()->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
| 180 | + $this->getLogging()->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 181 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 182 | + $this->getLogging()->log('No result in query : ' . $sql,1,''); |
|
| 183 | + } |
|
| 184 | + $dbObjectsRaw=$ret_code->fetchAll(); |
|
| 185 | 185 | |
| 186 | - foreach ($dbObjectsRaw as $val) |
|
| 187 | - { |
|
| 188 | - $dbObjects[$val['object_id']]=1; |
|
| 189 | - } |
|
| 190 | - return $dbObjects; |
|
| 191 | - } |
|
| 186 | + foreach ($dbObjectsRaw as $val) |
|
| 187 | + { |
|
| 188 | + $dbObjects[$val['object_id']]=1; |
|
| 189 | + } |
|
| 190 | + return $dbObjects; |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | 193 | |
| 194 | 194 | } |
| 195 | 195 | \ No newline at end of file |
@@ -60,9 +60,9 @@ discard block |
||
| 60 | 60 | } |
| 61 | 61 | $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll |
| 62 | 62 | $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element |
| 63 | - if ( $this->oidDesc['name'] != $dbOid['name'] || |
|
| 63 | + if ($this->oidDesc['name'] != $dbOid['name'] || |
|
| 64 | 64 | $this->oidDesc['mib'] != $dbOid['mib'] || |
| 65 | - $this->oidDesc['type'] !=$dbOid['type'] |
|
| 65 | + $this->oidDesc['type'] != $dbOid['type'] |
|
| 66 | 66 | ) |
| 67 | 67 | { // Do update |
| 68 | 68 | $sql='UPDATE '.$this->getTrapsDB()->dbPrefix.'mib_cache SET '. |
@@ -75,23 +75,23 @@ discard block |
||
| 75 | 75 | ':name' => $this->oidDesc['name'], |
| 76 | 76 | ':type' => $this->oidDesc['type'], |
| 77 | 77 | ':mib' => $this->oidDesc['mib'], |
| 78 | - ':tc' => $this->oidDesc['textconv']??'null', |
|
| 79 | - ':display_hint' => $this->oidDesc['dispHint']??'null' , |
|
| 80 | - ':syntax' => $this->oidDesc['syntax']==null??'null', |
|
| 81 | - ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 82 | - ':description' => $this->oidDesc['description']??'null', |
|
| 78 | + ':tc' => $this->oidDesc['textconv'] ?? 'null', |
|
| 79 | + ':display_hint' => $this->oidDesc['dispHint'] ?? 'null', |
|
| 80 | + ':syntax' => $this->oidDesc['syntax'] == null ?? 'null', |
|
| 81 | + ':type_enum' => $this->oidDesc['type_enum'] ?? 'null', |
|
| 82 | + ':description' => $this->oidDesc['description'] ?? 'null', |
|
| 83 | 83 | ':id' => $this->dbOidAll[$this->dbOidIndex[$this->oidDesc['oid']]['id']] |
| 84 | 84 | ); |
| 85 | 85 | |
| 86 | 86 | if ($sqlQuery->execute($sqlParam) === false) { |
| 87 | - $this->getLogging()->log('Error in query : ' . $sql,ERROR,''); |
|
| 87 | + $this->getLogging()->log('Error in query : '.$sql, ERROR, ''); |
|
| 88 | 88 | } |
| 89 | - $this->getLogging()->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 89 | + $this->getLogging()->log('Trap updated : '.$this->oidDesc['name'].' / OID : '.$this->oidDesc['oid'], DEBUG); |
|
| 90 | 90 | return 1; |
| 91 | 91 | } |
| 92 | 92 | else |
| 93 | 93 | { |
| 94 | - $this->getLogging()->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 94 | + $this->getLogging()->log('Trap unchanged : '.$this->oidDesc['name'].' / OID : '.$this->oidDesc['oid'], DEBUG); |
|
| 95 | 95 | return 0; |
| 96 | 96 | } |
| 97 | 97 | } |
@@ -107,11 +107,11 @@ discard block |
||
| 107 | 107 | $db_conn=$this->getTrapsDB()->db_connect_trap(); |
| 108 | 108 | $sql='INSERT INTO '.$this->getTrapsDB()->dbPrefix.'mib_cache '. |
| 109 | 109 | '(oid, name, type , mib, textual_convention, display_hint '. |
| 110 | - ', syntax, type_enum , description ) ' . |
|
| 110 | + ', syntax, type_enum , description ) '. |
|
| 111 | 111 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
| 112 | 112 | ', :syntax, :type_enum, :description )'; |
| 113 | 113 | |
| 114 | - if ($this->getTrapsDB()->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 114 | + if ($this->getTrapsDB()->trapDBType == 'pgsql') $sql.='RETURNING id'; |
|
| 115 | 115 | |
| 116 | 116 | $sqlQuery=$db_conn->prepare($sql); |
| 117 | 117 | |
@@ -120,15 +120,15 @@ discard block |
||
| 120 | 120 | ':name' => $this->oidDesc['name'], |
| 121 | 121 | ':type' => $this->oidDesc['type'], |
| 122 | 122 | ':mib' => $this->oidDesc['mib'], |
| 123 | - ':tc' => $this->oidDesc['textconv']??'null', |
|
| 124 | - ':display_hint' => $this->oidDesc['dispHint']??'null', |
|
| 125 | - ':syntax' => $this->oidDesc['syntax']??'null', |
|
| 126 | - ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 127 | - ':description' => $this->oidDesc['description']??'null' |
|
| 123 | + ':tc' => $this->oidDesc['textconv'] ?? 'null', |
|
| 124 | + ':display_hint' => $this->oidDesc['dispHint'] ?? 'null', |
|
| 125 | + ':syntax' => $this->oidDesc['syntax'] ?? 'null', |
|
| 126 | + ':type_enum' => $this->oidDesc['type_enum'] ?? 'null', |
|
| 127 | + ':description' => $this->oidDesc['description'] ?? 'null' |
|
| 128 | 128 | ); |
| 129 | 129 | |
| 130 | 130 | if ($sqlQuery->execute($sqlParam) === false) { |
| 131 | - $this->getLogging()->log('Error in query : ' . $sql,1,''); |
|
| 131 | + $this->getLogging()->log('Error in query : '.$sql, 1, ''); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | switch ($this->getTrapsDB()->trapDBType) |
@@ -136,10 +136,10 @@ discard block |
||
| 136 | 136 | case 'pgsql': |
| 137 | 137 | // Get last id to insert oid/values in secondary table |
| 138 | 138 | if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
| 139 | - $this->getLogging()->log('Error getting id - pgsql - ',1,''); |
|
| 139 | + $this->getLogging()->log('Error getting id - pgsql - ', 1, ''); |
|
| 140 | 140 | } |
| 141 | - if (! isset($inserted_id_ret['id'])) { |
|
| 142 | - $this->getLogging()->log('Error getting id - pgsql - empty.',ERROR); |
|
| 141 | + if (!isset($inserted_id_ret['id'])) { |
|
| 142 | + $this->getLogging()->log('Error getting id - pgsql - empty.', ERROR); |
|
| 143 | 143 | return 0; |
| 144 | 144 | } |
| 145 | 145 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id']; |
@@ -148,16 +148,16 @@ discard block |
||
| 148 | 148 | // Get last id to insert oid/values in secondary table |
| 149 | 149 | $sql='SELECT LAST_INSERT_ID();'; |
| 150 | 150 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 151 | - $this->getLogging()->log('Erreur getting id - mysql - ',ERROR); |
|
| 151 | + $this->getLogging()->log('Erreur getting id - mysql - ', ERROR); |
|
| 152 | 152 | return 0; |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 156 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 156 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 157 | 157 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
| 158 | 158 | break; |
| 159 | 159 | default: |
| 160 | - $this->getLogging()->log('Error SQL type Unknown : '.$this->getTrapsDB()->trapDBType,ERROR); |
|
| 160 | + $this->getLogging()->log('Error SQL type Unknown : '.$this->getTrapsDB()->trapDBType, ERROR); |
|
| 161 | 161 | return 0; |
| 162 | 162 | } |
| 163 | 163 | |
@@ -177,9 +177,9 @@ discard block |
||
| 177 | 177 | $db_conn=$this->getTrapsDB()->db_connect_trap(); |
| 178 | 178 | // Get all objects |
| 179 | 179 | $sql='SELECT * FROM '.$this->getTrapsDB()->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
| 180 | - $this->getLogging()->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 180 | + $this->getLogging()->log('SQL query get all traps: '.$sql, DEBUG); |
|
| 181 | 181 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 182 | - $this->getLogging()->log('No result in query : ' . $sql,1,''); |
|
| 182 | + $this->getLogging()->log('No result in query : '.$sql, 1, ''); |
|
| 183 | 183 | } |
| 184 | 184 | $dbObjectsRaw=$ret_code->fetchAll(); |
| 185 | 185 | |