@@ -68,7 +68,9 @@ |
||
68 | 68 | switch ($outputType) |
69 | 69 | { |
70 | 70 | case 'file': |
71 | - if ($outputFile == null) throw new Exception("File logging without file !"); |
|
71 | + if ($outputFile == null) { |
|
72 | + throw new Exception("File logging without file !"); |
|
73 | + } |
|
72 | 74 | $this->setFile($outputFile); |
73 | 75 | $this->setDestination('file'); |
74 | 76 | break; |
@@ -9,113 +9,113 @@ |
||
9 | 9 | class Logging |
10 | 10 | { |
11 | 11 | |
12 | - //**** Options from config database |
|
13 | - // Default values |
|
14 | - public $debugLevel=2; // 0=No output 1=critical 2=warning 3=trace 4=ALL |
|
15 | - public $outputMode='syslog'; // alert type : file, syslog, display |
|
16 | - public $outputFile="/tmp/trapdebug.txt"; |
|
17 | - protected $logLevels=array("","Error","Warning","Info","Debug"); |
|
18 | - protected $outputList=array('file', 'syslog', 'display'); |
|
12 | + //**** Options from config database |
|
13 | + // Default values |
|
14 | + public $debugLevel=2; // 0=No output 1=critical 2=warning 3=trace 4=ALL |
|
15 | + public $outputMode='syslog'; // alert type : file, syslog, display |
|
16 | + public $outputFile="/tmp/trapdebug.txt"; |
|
17 | + protected $logLevels=array("","Error","Warning","Info","Debug"); |
|
18 | + protected $outputList=array('file', 'syslog', 'display'); |
|
19 | 19 | |
20 | - /** Send log. Throws exception on critical error |
|
21 | - * @param string $message Message to log |
|
22 | - * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
23 | - * @param string $destination file/syslog/display |
|
24 | - * @return void |
|
25 | - * @throws Exception |
|
26 | - **/ |
|
27 | - public function log( $message, $level, $destination ='') |
|
28 | - { |
|
29 | - if ($this->debugLevel >= $level) |
|
30 | - { |
|
31 | - $date = '['. date("Y/m/d H:i:s") . '] '; // no date in syslog as already there |
|
32 | - $message = '[TrapDirector] ['.$this->logLevels[$level].']: ' .$message . "\n"; |
|
20 | + /** Send log. Throws exception on critical error |
|
21 | + * @param string $message Message to log |
|
22 | + * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
23 | + * @param string $destination file/syslog/display |
|
24 | + * @return void |
|
25 | + * @throws Exception |
|
26 | + **/ |
|
27 | + public function log( $message, $level, $destination ='') |
|
28 | + { |
|
29 | + if ($this->debugLevel >= $level) |
|
30 | + { |
|
31 | + $date = '['. date("Y/m/d H:i:s") . '] '; // no date in syslog as already there |
|
32 | + $message = '[TrapDirector] ['.$this->logLevels[$level].']: ' .$message . "\n"; |
|
33 | 33 | |
34 | - $output = ( $destination != '' ) ? $destination : $this->outputMode; |
|
35 | - switch ($output) |
|
36 | - { |
|
37 | - case 'file': |
|
38 | - file_put_contents ($this->outputFile, $date.$message , FILE_APPEND); |
|
39 | - break; |
|
40 | - case 'syslog': |
|
41 | - switch($level) |
|
42 | - { |
|
43 | - case 1 : $prio = LOG_ERR;break; |
|
44 | - case 2 : $prio = LOG_WARNING;break; |
|
45 | - case 3 : $prio = LOG_INFO;break; |
|
46 | - case 4 : $prio = LOG_INFO;break; // LOG_DEBUG isn't always displayed in syslog |
|
47 | - default: $prio = LOG_ERR; |
|
48 | - } |
|
49 | - syslog($prio,$message); |
|
50 | - break; |
|
51 | - case 'display': |
|
52 | - echo $date.$message; |
|
53 | - break; |
|
54 | - default : // nothing we can do at this point |
|
55 | - throw new Exception($date.$message); |
|
56 | - } |
|
57 | - } |
|
58 | - if ($level == 1) |
|
59 | - { |
|
60 | - throw new Exception($message); |
|
61 | - } |
|
62 | - } |
|
34 | + $output = ( $destination != '' ) ? $destination : $this->outputMode; |
|
35 | + switch ($output) |
|
36 | + { |
|
37 | + case 'file': |
|
38 | + file_put_contents ($this->outputFile, $date.$message , FILE_APPEND); |
|
39 | + break; |
|
40 | + case 'syslog': |
|
41 | + switch($level) |
|
42 | + { |
|
43 | + case 1 : $prio = LOG_ERR;break; |
|
44 | + case 2 : $prio = LOG_WARNING;break; |
|
45 | + case 3 : $prio = LOG_INFO;break; |
|
46 | + case 4 : $prio = LOG_INFO;break; // LOG_DEBUG isn't always displayed in syslog |
|
47 | + default: $prio = LOG_ERR; |
|
48 | + } |
|
49 | + syslog($prio,$message); |
|
50 | + break; |
|
51 | + case 'display': |
|
52 | + echo $date.$message; |
|
53 | + break; |
|
54 | + default : // nothing we can do at this point |
|
55 | + throw new Exception($date.$message); |
|
56 | + } |
|
57 | + } |
|
58 | + if ($level == 1) |
|
59 | + { |
|
60 | + throw new Exception($message); |
|
61 | + } |
|
62 | + } |
|
63 | 63 | |
64 | 64 | |
65 | - public function setLogging($debugLvl,$outputType,$outputFile=null) |
|
66 | - { |
|
67 | - $this->setLevel($debugLvl); |
|
68 | - switch ($outputType) |
|
69 | - { |
|
70 | - case 'file': |
|
71 | - if ($outputFile == null) throw new Exception("File logging without file !"); |
|
72 | - $this->setFile($outputFile); |
|
73 | - $this->setDestination('file'); |
|
74 | - break; |
|
75 | - default: |
|
76 | - $this->setDestination($outputType); |
|
77 | - } |
|
78 | - } |
|
65 | + public function setLogging($debugLvl,$outputType,$outputFile=null) |
|
66 | + { |
|
67 | + $this->setLevel($debugLvl); |
|
68 | + switch ($outputType) |
|
69 | + { |
|
70 | + case 'file': |
|
71 | + if ($outputFile == null) throw new Exception("File logging without file !"); |
|
72 | + $this->setFile($outputFile); |
|
73 | + $this->setDestination('file'); |
|
74 | + break; |
|
75 | + default: |
|
76 | + $this->setDestination($outputType); |
|
77 | + } |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Set logging level |
|
82 | - * @param integer $level |
|
83 | - * @throws Exception |
|
84 | - */ |
|
85 | - public function setLevel($level) |
|
86 | - { |
|
87 | - if (!is_integer($level) || $level < 0 || $level > 10) |
|
88 | - { |
|
89 | - throw new Exception('Invalid log level'); |
|
90 | - } |
|
91 | - $this->debugLevel=$level; |
|
92 | - } |
|
80 | + /** |
|
81 | + * Set logging level |
|
82 | + * @param integer $level |
|
83 | + * @throws Exception |
|
84 | + */ |
|
85 | + public function setLevel($level) |
|
86 | + { |
|
87 | + if (!is_integer($level) || $level < 0 || $level > 10) |
|
88 | + { |
|
89 | + throw new Exception('Invalid log level'); |
|
90 | + } |
|
91 | + $this->debugLevel=$level; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Set logging destination |
|
96 | - * @param string $destination |
|
97 | - * @throws Exception |
|
98 | - */ |
|
99 | - public function setDestination($destination) |
|
100 | - { |
|
101 | - if (!is_string($destination) || ! in_array($destination, $this->outputList)) |
|
102 | - { |
|
103 | - throw new Exception('Invalid log destination'); |
|
104 | - } |
|
105 | - $this->outputMode=$destination; |
|
106 | - } |
|
107 | - /** |
|
108 | - * Set file destination |
|
109 | - * @param string $file |
|
110 | - * @throws Exception |
|
111 | - */ |
|
112 | - public function setFile($file) |
|
113 | - { |
|
114 | - if (!is_string($file)) |
|
115 | - { |
|
116 | - throw new Exception('Invalid log file'); |
|
117 | - } |
|
118 | - $this->outputFile=$file; |
|
119 | - } |
|
94 | + /** |
|
95 | + * Set logging destination |
|
96 | + * @param string $destination |
|
97 | + * @throws Exception |
|
98 | + */ |
|
99 | + public function setDestination($destination) |
|
100 | + { |
|
101 | + if (!is_string($destination) || ! in_array($destination, $this->outputList)) |
|
102 | + { |
|
103 | + throw new Exception('Invalid log destination'); |
|
104 | + } |
|
105 | + $this->outputMode=$destination; |
|
106 | + } |
|
107 | + /** |
|
108 | + * Set file destination |
|
109 | + * @param string $file |
|
110 | + * @throws Exception |
|
111 | + */ |
|
112 | + public function setFile($file) |
|
113 | + { |
|
114 | + if (!is_string($file)) |
|
115 | + { |
|
116 | + throw new Exception('Invalid log file'); |
|
117 | + } |
|
118 | + $this->outputFile=$file; |
|
119 | + } |
|
120 | 120 | |
121 | 121 | } |
122 | 122 | \ No newline at end of file |
@@ -4,17 +4,17 @@ discard block |
||
4 | 4 | |
5 | 5 | use Exception; |
6 | 6 | |
7 | -define("ERROR", 1);define("WARN", 2);define("INFO", 3);define("DEBUG", 4); |
|
7 | +define("ERROR", 1); define("WARN", 2); define("INFO", 3); define("DEBUG", 4); |
|
8 | 8 | |
9 | 9 | class Logging |
10 | 10 | { |
11 | 11 | |
12 | 12 | //**** Options from config database |
13 | 13 | // Default values |
14 | - public $debugLevel=2; // 0=No output 1=critical 2=warning 3=trace 4=ALL |
|
14 | + public $debugLevel=2; // 0=No output 1=critical 2=warning 3=trace 4=ALL |
|
15 | 15 | public $outputMode='syslog'; // alert type : file, syslog, display |
16 | 16 | public $outputFile="/tmp/trapdebug.txt"; |
17 | - protected $logLevels=array("","Error","Warning","Info","Debug"); |
|
17 | + protected $logLevels=array("", "Error", "Warning", "Info", "Debug"); |
|
18 | 18 | protected $outputList=array('file', 'syslog', 'display'); |
19 | 19 | |
20 | 20 | /** Send log. Throws exception on critical error |
@@ -24,29 +24,29 @@ discard block |
||
24 | 24 | * @return void |
25 | 25 | * @throws Exception |
26 | 26 | **/ |
27 | - public function log( $message, $level, $destination ='') |
|
27 | + public function log($message, $level, $destination='') |
|
28 | 28 | { |
29 | 29 | if ($this->debugLevel >= $level) |
30 | 30 | { |
31 | - $date = '['. date("Y/m/d H:i:s") . '] '; // no date in syslog as already there |
|
32 | - $message = '[TrapDirector] ['.$this->logLevels[$level].']: ' .$message . "\n"; |
|
31 | + $date='['.date("Y/m/d H:i:s").'] '; // no date in syslog as already there |
|
32 | + $message='[TrapDirector] ['.$this->logLevels[$level].']: '.$message."\n"; |
|
33 | 33 | |
34 | - $output = ( $destination != '' ) ? $destination : $this->outputMode; |
|
34 | + $output=($destination != '') ? $destination : $this->outputMode; |
|
35 | 35 | switch ($output) |
36 | 36 | { |
37 | 37 | case 'file': |
38 | - file_put_contents ($this->outputFile, $date.$message , FILE_APPEND); |
|
38 | + file_put_contents($this->outputFile, $date.$message, FILE_APPEND); |
|
39 | 39 | break; |
40 | 40 | case 'syslog': |
41 | - switch($level) |
|
41 | + switch ($level) |
|
42 | 42 | { |
43 | - case 1 : $prio = LOG_ERR;break; |
|
44 | - case 2 : $prio = LOG_WARNING;break; |
|
45 | - case 3 : $prio = LOG_INFO;break; |
|
46 | - case 4 : $prio = LOG_INFO;break; // LOG_DEBUG isn't always displayed in syslog |
|
47 | - default: $prio = LOG_ERR; |
|
43 | + case 1 : $prio=LOG_ERR; break; |
|
44 | + case 2 : $prio=LOG_WARNING; break; |
|
45 | + case 3 : $prio=LOG_INFO; break; |
|
46 | + case 4 : $prio=LOG_INFO; break; // LOG_DEBUG isn't always displayed in syslog |
|
47 | + default: $prio=LOG_ERR; |
|
48 | 48 | } |
49 | - syslog($prio,$message); |
|
49 | + syslog($prio, $message); |
|
50 | 50 | break; |
51 | 51 | case 'display': |
52 | 52 | echo $date.$message; |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | } |
63 | 63 | |
64 | 64 | |
65 | - public function setLogging($debugLvl,$outputType,$outputFile=null) |
|
65 | + public function setLogging($debugLvl, $outputType, $outputFile=null) |
|
66 | 66 | { |
67 | 67 | $this->setLevel($debugLvl); |
68 | 68 | switch ($outputType) |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | */ |
99 | 99 | public function setDestination($destination) |
100 | 100 | { |
101 | - if (!is_string($destination) || ! in_array($destination, $this->outputList)) |
|
101 | + if (!is_string($destination) || !in_array($destination, $this->outputList)) |
|
102 | 102 | { |
103 | 103 | throw new Exception('Invalid log destination'); |
104 | 104 | } |
@@ -14,40 +14,40 @@ |
||
14 | 14 | |
15 | 15 | if (!array_key_exists('v',$options) || !array_key_exists('c',$options) || !array_key_exists('b',$options)|| !array_key_exists('a',$options)) |
16 | 16 | { |
17 | - printf("Need version -v, path -a, database -b (mysql,pgsql) command -c (create/update)\n"); |
|
18 | - exit(1); |
|
17 | + printf("Need version -v, path -a, database -b (mysql,pgsql) command -c (create/update)\n"); |
|
18 | + exit(1); |
|
19 | 19 | } |
20 | 20 | $command=$options['c']; |
21 | 21 | $path=$options['a']; |
22 | 22 | try { |
23 | - switch($command) |
|
24 | - { |
|
25 | - case 'create': |
|
26 | - $schema=($options['b']=='mysql')?'schema_v'.$options['v'].'.sql':'schema_v'.$options['v'].'.pgsql'; |
|
27 | - $schema=$path.'/SQL/'.$schema; |
|
28 | - $trap->trapsDB->create_schema($schema, 'traps_'); |
|
29 | - break; |
|
30 | - case 'update': |
|
31 | - $message=$trap->trapsDB->update_schema($path."/SQL/",$options['v'], 'traps_',true); |
|
32 | - printf("Update message : %s\n",$message); |
|
33 | - if ($message == 'ERROR') |
|
34 | - { |
|
35 | - exit(1); |
|
36 | - } |
|
37 | - printf("Messages DONE, updating : \n"); |
|
38 | - $message=$trap->trapsDB->update_schema($path."/SQL/",$options['v'], 'traps_'); |
|
39 | - if ($message == 'ERROR') |
|
40 | - { |
|
41 | - exit(1); |
|
42 | - } |
|
43 | - break; |
|
44 | - default: |
|
45 | - printf("Unknown command\n"); |
|
46 | - exit(1); |
|
47 | - } |
|
23 | + switch($command) |
|
24 | + { |
|
25 | + case 'create': |
|
26 | + $schema=($options['b']=='mysql')?'schema_v'.$options['v'].'.sql':'schema_v'.$options['v'].'.pgsql'; |
|
27 | + $schema=$path.'/SQL/'.$schema; |
|
28 | + $trap->trapsDB->create_schema($schema, 'traps_'); |
|
29 | + break; |
|
30 | + case 'update': |
|
31 | + $message=$trap->trapsDB->update_schema($path."/SQL/",$options['v'], 'traps_',true); |
|
32 | + printf("Update message : %s\n",$message); |
|
33 | + if ($message == 'ERROR') |
|
34 | + { |
|
35 | + exit(1); |
|
36 | + } |
|
37 | + printf("Messages DONE, updating : \n"); |
|
38 | + $message=$trap->trapsDB->update_schema($path."/SQL/",$options['v'], 'traps_'); |
|
39 | + if ($message == 'ERROR') |
|
40 | + { |
|
41 | + exit(1); |
|
42 | + } |
|
43 | + break; |
|
44 | + default: |
|
45 | + printf("Unknown command\n"); |
|
46 | + exit(1); |
|
47 | + } |
|
48 | 48 | } catch (Exception $e) { |
49 | - printf("Caught Exception %s\n",$e->getMessage()); |
|
50 | - exit (1); |
|
49 | + printf("Caught Exception %s\n",$e->getMessage()); |
|
50 | + exit (1); |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | exit(0); |
@@ -3,16 +3,16 @@ discard block |
||
3 | 3 | |
4 | 4 | require_once 'bin/trap_class.php'; |
5 | 5 | |
6 | -$options = getopt("c:v:d:b:a:"); |
|
6 | +$options=getopt("c:v:d:b:a:"); |
|
7 | 7 | |
8 | -$icingaweb2Etc=(array_key_exists('d',$options))?$options['d']:"/etc/icingaweb2"; |
|
8 | +$icingaweb2Etc=(array_key_exists('d', $options)) ? $options['d'] : "/etc/icingaweb2"; |
|
9 | 9 | |
10 | -$debugLevel=4;// 0=No output 1=critical 2=warning 3=trace 4=ALL |
|
10 | +$debugLevel=4; // 0=No output 1=critical 2=warning 3=trace 4=ALL |
|
11 | 11 | |
12 | -$trap = new trap($icingaweb2Etc,$debugLevel,'display'); |
|
13 | -$trap->setLogging($debugLevel,'display'); |
|
12 | +$trap=new trap($icingaweb2Etc, $debugLevel, 'display'); |
|
13 | +$trap->setLogging($debugLevel, 'display'); |
|
14 | 14 | |
15 | -if (!array_key_exists('v',$options) || !array_key_exists('c',$options) || !array_key_exists('b',$options)|| !array_key_exists('a',$options)) |
|
15 | +if (!array_key_exists('v', $options) || !array_key_exists('c', $options) || !array_key_exists('b', $options) || !array_key_exists('a', $options)) |
|
16 | 16 | { |
17 | 17 | printf("Need version -v, path -a, database -b (mysql,pgsql) command -c (create/update)\n"); |
18 | 18 | exit(1); |
@@ -20,22 +20,22 @@ discard block |
||
20 | 20 | $command=$options['c']; |
21 | 21 | $path=$options['a']; |
22 | 22 | try { |
23 | - switch($command) |
|
23 | + switch ($command) |
|
24 | 24 | { |
25 | 25 | case 'create': |
26 | - $schema=($options['b']=='mysql')?'schema_v'.$options['v'].'.sql':'schema_v'.$options['v'].'.pgsql'; |
|
26 | + $schema=($options['b'] == 'mysql') ? 'schema_v'.$options['v'].'.sql' : 'schema_v'.$options['v'].'.pgsql'; |
|
27 | 27 | $schema=$path.'/SQL/'.$schema; |
28 | 28 | $trap->trapsDB->create_schema($schema, 'traps_'); |
29 | 29 | break; |
30 | 30 | case 'update': |
31 | - $message=$trap->trapsDB->update_schema($path."/SQL/",$options['v'], 'traps_',true); |
|
32 | - printf("Update message : %s\n",$message); |
|
31 | + $message=$trap->trapsDB->update_schema($path."/SQL/", $options['v'], 'traps_', true); |
|
32 | + printf("Update message : %s\n", $message); |
|
33 | 33 | if ($message == 'ERROR') |
34 | 34 | { |
35 | 35 | exit(1); |
36 | 36 | } |
37 | 37 | printf("Messages DONE, updating : \n"); |
38 | - $message=$trap->trapsDB->update_schema($path."/SQL/",$options['v'], 'traps_'); |
|
38 | + $message=$trap->trapsDB->update_schema($path."/SQL/", $options['v'], 'traps_'); |
|
39 | 39 | if ($message == 'ERROR') |
40 | 40 | { |
41 | 41 | exit(1); |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | exit(1); |
47 | 47 | } |
48 | 48 | } catch (Exception $e) { |
49 | - printf("Caught Exception %s\n",$e->getMessage()); |
|
49 | + printf("Caught Exception %s\n", $e->getMessage()); |
|
50 | 50 | exit (1); |
51 | 51 | } |
52 | 52 |
@@ -74,8 +74,7 @@ discard block |
||
74 | 74 | try |
75 | 75 | { |
76 | 76 | $result=$this->request('GET', "", NULL, NULL); |
77 | - } |
|
78 | - catch (Exception $e) |
|
77 | + } catch (Exception $e) |
|
79 | 78 | { |
80 | 79 | return array(true, 'Error with API : '.$e->getMessage()); |
81 | 80 | } |
@@ -165,8 +164,7 @@ discard block |
||
165 | 164 | if (property_exists($result,'status')) |
166 | 165 | { |
167 | 166 | $message=$result->status; |
168 | - } |
|
169 | - else |
|
167 | + } else |
|
170 | 168 | { |
171 | 169 | $message="Unkown status"; |
172 | 170 | } |
@@ -177,8 +175,7 @@ discard block |
||
177 | 175 | if (isset($result->results[0])) |
178 | 176 | { |
179 | 177 | return array(true,'code '.$result->results[0]->code.' : '.$result->results[0]->status); |
180 | - } |
|
181 | - else |
|
178 | + } else |
|
182 | 179 | { |
183 | 180 | return array(false,'Service not found'); |
184 | 181 | } |
@@ -220,8 +217,7 @@ discard block |
||
220 | 217 | if (property_exists($result,'status')) |
221 | 218 | { |
222 | 219 | throw new Exception('Ret code ' .$result->error.' : ' . $result->status); |
223 | - } |
|
224 | - else |
|
220 | + } else |
|
225 | 221 | { |
226 | 222 | throw new Exception('Ret code ' .$result->error.' : Unkown status'); |
227 | 223 | } |
@@ -266,8 +262,7 @@ discard block |
||
266 | 262 | if (property_exists($result,'status')) |
267 | 263 | { |
268 | 264 | throw new Exception('Ret code ' .$result->error.' : ' . $result->status); |
269 | - } |
|
270 | - else |
|
265 | + } else |
|
271 | 266 | { |
272 | 267 | throw new Exception('Ret code ' .$result->error.' : Unkown status'); |
273 | 268 | } |
@@ -8,363 +8,363 @@ |
||
8 | 8 | |
9 | 9 | class Icinga2API |
10 | 10 | { |
11 | - protected $version = 'v1'; //< icinga2 api version |
|
11 | + protected $version = 'v1'; //< icinga2 api version |
|
12 | 12 | |
13 | - protected $host; //< icinga2 host name or IP |
|
14 | - protected $port; //< icinga2 api port |
|
13 | + protected $host; //< icinga2 host name or IP |
|
14 | + protected $port; //< icinga2 api port |
|
15 | 15 | |
16 | - protected $user; //< user name |
|
17 | - protected $pass; //< user password |
|
18 | - protected $usercert; //< user key for certificate auth (NOT IMPLEMENTED) |
|
19 | - protected $authmethod='pass'; //< Authentication : 'pass' or 'cert' |
|
16 | + protected $user; //< user name |
|
17 | + protected $pass; //< user password |
|
18 | + protected $usercert; //< user key for certificate auth (NOT IMPLEMENTED) |
|
19 | + protected $authmethod='pass'; //< Authentication : 'pass' or 'cert' |
|
20 | 20 | |
21 | - protected $curl; |
|
22 | - // http://php.net/manual/de/function.json-last-error.php#119985 |
|
23 | - protected $errorReference = [ |
|
24 | - JSON_ERROR_NONE => 'No error has occurred.', |
|
25 | - JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded.', |
|
26 | - JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON.', |
|
27 | - JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded.', |
|
28 | - JSON_ERROR_SYNTAX => 'Syntax error.', |
|
29 | - JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded.', |
|
30 | - JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded.', |
|
31 | - JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded.', |
|
32 | - JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given.', |
|
33 | - ]; |
|
34 | - const JSON_UNKNOWN_ERROR = 'Unknown error.'; |
|
21 | + protected $curl; |
|
22 | + // http://php.net/manual/de/function.json-last-error.php#119985 |
|
23 | + protected $errorReference = [ |
|
24 | + JSON_ERROR_NONE => 'No error has occurred.', |
|
25 | + JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded.', |
|
26 | + JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON.', |
|
27 | + JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded.', |
|
28 | + JSON_ERROR_SYNTAX => 'Syntax error.', |
|
29 | + JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded.', |
|
30 | + JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded.', |
|
31 | + JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded.', |
|
32 | + JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given.', |
|
33 | + ]; |
|
34 | + const JSON_UNKNOWN_ERROR = 'Unknown error.'; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Creates Icinga2API object |
|
38 | - * |
|
39 | - * @param string $host host name or IP |
|
40 | - * @param number $port API port |
|
41 | - */ |
|
42 | - public function __construct($host, $port = 5665) |
|
43 | - { |
|
44 | - $this->host=$host; |
|
45 | - $this->port=$port; |
|
46 | - } |
|
47 | - /** |
|
48 | - * Set user & pass |
|
49 | - * @param string $user |
|
50 | - * @param string $pass |
|
51 | - */ |
|
52 | - public function setCredentials($user,$pass) |
|
53 | - { |
|
54 | - $this->user=$user; |
|
55 | - $this->pass=$pass; |
|
56 | - $this->authmethod='pass'; |
|
57 | - } |
|
36 | + /** |
|
37 | + * Creates Icinga2API object |
|
38 | + * |
|
39 | + * @param string $host host name or IP |
|
40 | + * @param number $port API port |
|
41 | + */ |
|
42 | + public function __construct($host, $port = 5665) |
|
43 | + { |
|
44 | + $this->host=$host; |
|
45 | + $this->port=$port; |
|
46 | + } |
|
47 | + /** |
|
48 | + * Set user & pass |
|
49 | + * @param string $user |
|
50 | + * @param string $pass |
|
51 | + */ |
|
52 | + public function setCredentials($user,$pass) |
|
53 | + { |
|
54 | + $this->user=$user; |
|
55 | + $this->pass=$pass; |
|
56 | + $this->authmethod='pass'; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Set user & certificate (NOT IMPLEMENTED @throws RuntimeException) |
|
61 | - * @param string $user |
|
62 | - * @param string $usercert |
|
63 | - */ |
|
64 | - public function setCredentialskey($user,$usercert) |
|
65 | - { |
|
66 | - $this->user=$user; |
|
67 | - $this->usercert=$usercert; |
|
68 | - $this->authmethod='cert'; |
|
69 | - throw new RuntimeException('Certificate auth not implemented'); |
|
70 | - } |
|
59 | + /** |
|
60 | + * Set user & certificate (NOT IMPLEMENTED @throws RuntimeException) |
|
61 | + * @param string $user |
|
62 | + * @param string $usercert |
|
63 | + */ |
|
64 | + public function setCredentialskey($user,$usercert) |
|
65 | + { |
|
66 | + $this->user=$user; |
|
67 | + $this->usercert=$usercert; |
|
68 | + $this->authmethod='cert'; |
|
69 | + throw new RuntimeException('Certificate auth not implemented'); |
|
70 | + } |
|
71 | 71 | |
72 | - public function test(array $permissions) |
|
73 | - { |
|
74 | - try |
|
75 | - { |
|
76 | - $result=$this->request('GET', "", NULL, NULL); |
|
77 | - } |
|
78 | - catch (Exception $e) |
|
79 | - { |
|
80 | - return array(true, 'Error with API : '.$e->getMessage()); |
|
81 | - } |
|
82 | - //var_dump($result); |
|
83 | - $permOk=1; |
|
84 | - $permMissing=''; |
|
85 | - if (property_exists($result, 'results') && property_exists($result->results[0], 'permissions')) |
|
86 | - { |
|
72 | + public function test(array $permissions) |
|
73 | + { |
|
74 | + try |
|
75 | + { |
|
76 | + $result=$this->request('GET', "", NULL, NULL); |
|
77 | + } |
|
78 | + catch (Exception $e) |
|
79 | + { |
|
80 | + return array(true, 'Error with API : '.$e->getMessage()); |
|
81 | + } |
|
82 | + //var_dump($result); |
|
83 | + $permOk=1; |
|
84 | + $permMissing=''; |
|
85 | + if (property_exists($result, 'results') && property_exists($result->results[0], 'permissions')) |
|
86 | + { |
|
87 | 87 | |
88 | - foreach ( $permissions as $mustPermission) |
|
89 | - { |
|
90 | - $curPermOK=0; |
|
91 | - foreach ( $result->results[0]->permissions as $curPermission) |
|
92 | - { |
|
93 | - $curPermission=preg_replace('/\*/','.*',$curPermission); // put * as .* to created a regexp |
|
94 | - if (preg_match('#'.$curPermission.'#',$mustPermission)) |
|
95 | - { |
|
96 | - $curPermOK=1; |
|
97 | - break; |
|
98 | - } |
|
99 | - } |
|
100 | - if ($curPermOK == 0) |
|
101 | - { |
|
102 | - $permOk=0; |
|
103 | - $permMissing=$mustPermission; |
|
104 | - break; |
|
105 | - } |
|
106 | - } |
|
107 | - if ($permOk == 0) |
|
108 | - { |
|
109 | - return array(true,'API connection OK, but missing permission : '.$permMissing); |
|
110 | - } |
|
111 | - return array(false,'API connection OK'); |
|
88 | + foreach ( $permissions as $mustPermission) |
|
89 | + { |
|
90 | + $curPermOK=0; |
|
91 | + foreach ( $result->results[0]->permissions as $curPermission) |
|
92 | + { |
|
93 | + $curPermission=preg_replace('/\*/','.*',$curPermission); // put * as .* to created a regexp |
|
94 | + if (preg_match('#'.$curPermission.'#',$mustPermission)) |
|
95 | + { |
|
96 | + $curPermOK=1; |
|
97 | + break; |
|
98 | + } |
|
99 | + } |
|
100 | + if ($curPermOK == 0) |
|
101 | + { |
|
102 | + $permOk=0; |
|
103 | + $permMissing=$mustPermission; |
|
104 | + break; |
|
105 | + } |
|
106 | + } |
|
107 | + if ($permOk == 0) |
|
108 | + { |
|
109 | + return array(true,'API connection OK, but missing permission : '.$permMissing); |
|
110 | + } |
|
111 | + return array(false,'API connection OK'); |
|
112 | 112 | |
113 | - } |
|
114 | - return array(true,'API connection OK, but cannot get permissions'); |
|
115 | - } |
|
113 | + } |
|
114 | + return array(true,'API connection OK, but cannot get permissions'); |
|
115 | + } |
|
116 | 116 | |
117 | 117 | |
118 | - protected function url($url) { |
|
119 | - return sprintf('https://%s:%d/%s/%s', $this->host, $this->port, $this->version, $url); |
|
120 | - } |
|
118 | + protected function url($url) { |
|
119 | + return sprintf('https://%s:%d/%s/%s', $this->host, $this->port, $this->version, $url); |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * Create or return curl ressource |
|
124 | - * @throws Exception |
|
125 | - * @return resource |
|
126 | - */ |
|
127 | - protected function curl() { |
|
128 | - if ($this->curl === null) { |
|
129 | - $this->curl = curl_init(sprintf('https://%s:%d', $this->host, $this->port)); |
|
130 | - if ($this->curl === false) { |
|
131 | - throw new Exception('CURL INIT ERROR'); |
|
132 | - } |
|
133 | - } |
|
134 | - return $this->curl; |
|
135 | - } |
|
122 | + /** |
|
123 | + * Create or return curl ressource |
|
124 | + * @throws Exception |
|
125 | + * @return resource |
|
126 | + */ |
|
127 | + protected function curl() { |
|
128 | + if ($this->curl === null) { |
|
129 | + $this->curl = curl_init(sprintf('https://%s:%d', $this->host, $this->port)); |
|
130 | + if ($this->curl === false) { |
|
131 | + throw new Exception('CURL INIT ERROR'); |
|
132 | + } |
|
133 | + } |
|
134 | + return $this->curl; |
|
135 | + } |
|
136 | 136 | |
137 | - /** |
|
138 | - * Send a passive service check |
|
139 | - * @param string $host : host name |
|
140 | - * @param string $service : service name |
|
141 | - * @param int $state : state of service |
|
142 | - * @param string $display : service passive check output |
|
143 | - * @param string $perfdata : performance data as string |
|
144 | - * @return array (status = true (oK) or false (nok), string message) |
|
145 | - */ |
|
146 | - public function serviceCheckResult($host,$service,$state,$display,$perfdata='') |
|
147 | - { |
|
148 | - //Send a POST request to the URL endpoint /v1/actions/process-check-result |
|
149 | - //actions/process-check-result?service=example.localdomain!passive-ping6 |
|
150 | - $url='actions/process-check-result'; |
|
151 | - $body=array( |
|
152 | - "filter" => 'service.name=="'.$service.'" && service.host_name=="'.$host.'"', |
|
153 | - 'type' => 'Service', |
|
154 | - "exit_status" => $state, |
|
155 | - "plugin_output" => $display, |
|
156 | - "performance_data" => $perfdata |
|
157 | - ); |
|
158 | - try |
|
159 | - { |
|
160 | - $result=$this->request('POST', $url, null, $body); |
|
161 | - } catch (Exception $e) |
|
162 | - { |
|
163 | - return array(false, $e->getMessage()); |
|
164 | - } |
|
165 | - if (property_exists($result,'error') ) |
|
166 | - { |
|
167 | - if (property_exists($result,'status')) |
|
168 | - { |
|
169 | - $message=$result->status; |
|
170 | - } |
|
171 | - else |
|
172 | - { |
|
173 | - $message="Unkown status"; |
|
174 | - } |
|
175 | - return array(false , 'Ret code ' .$result->error.' : '.$message); |
|
176 | - } |
|
177 | - if (property_exists($result, 'results')) |
|
178 | - { |
|
179 | - if (isset($result->results[0])) |
|
180 | - { |
|
181 | - return array(true,'code '.$result->results[0]->code.' : '.$result->results[0]->status); |
|
182 | - } |
|
183 | - else |
|
184 | - { |
|
185 | - return array(false,'Service not found'); |
|
186 | - } |
|
137 | + /** |
|
138 | + * Send a passive service check |
|
139 | + * @param string $host : host name |
|
140 | + * @param string $service : service name |
|
141 | + * @param int $state : state of service |
|
142 | + * @param string $display : service passive check output |
|
143 | + * @param string $perfdata : performance data as string |
|
144 | + * @return array (status = true (oK) or false (nok), string message) |
|
145 | + */ |
|
146 | + public function serviceCheckResult($host,$service,$state,$display,$perfdata='') |
|
147 | + { |
|
148 | + //Send a POST request to the URL endpoint /v1/actions/process-check-result |
|
149 | + //actions/process-check-result?service=example.localdomain!passive-ping6 |
|
150 | + $url='actions/process-check-result'; |
|
151 | + $body=array( |
|
152 | + "filter" => 'service.name=="'.$service.'" && service.host_name=="'.$host.'"', |
|
153 | + 'type' => 'Service', |
|
154 | + "exit_status" => $state, |
|
155 | + "plugin_output" => $display, |
|
156 | + "performance_data" => $perfdata |
|
157 | + ); |
|
158 | + try |
|
159 | + { |
|
160 | + $result=$this->request('POST', $url, null, $body); |
|
161 | + } catch (Exception $e) |
|
162 | + { |
|
163 | + return array(false, $e->getMessage()); |
|
164 | + } |
|
165 | + if (property_exists($result,'error') ) |
|
166 | + { |
|
167 | + if (property_exists($result,'status')) |
|
168 | + { |
|
169 | + $message=$result->status; |
|
170 | + } |
|
171 | + else |
|
172 | + { |
|
173 | + $message="Unkown status"; |
|
174 | + } |
|
175 | + return array(false , 'Ret code ' .$result->error.' : '.$message); |
|
176 | + } |
|
177 | + if (property_exists($result, 'results')) |
|
178 | + { |
|
179 | + if (isset($result->results[0])) |
|
180 | + { |
|
181 | + return array(true,'code '.$result->results[0]->code.' : '.$result->results[0]->status); |
|
182 | + } |
|
183 | + else |
|
184 | + { |
|
185 | + return array(false,'Service not found'); |
|
186 | + } |
|
187 | 187 | |
188 | - } |
|
189 | - return array(false,'Unkown result, open issue with this : '.print_r($result,true)); |
|
190 | - } |
|
188 | + } |
|
189 | + return array(false,'Unkown result, open issue with this : '.print_r($result,true)); |
|
190 | + } |
|
191 | 191 | |
192 | - /** |
|
193 | - * return array of host by IP (4 or 6) |
|
194 | - * @param string $ip |
|
195 | - * @throws Exception |
|
196 | - * @return array objects : array('__name','name','display_name') |
|
197 | - */ |
|
198 | - public function getHostByIP($ip) |
|
199 | - { |
|
200 | - /* |
|
192 | + /** |
|
193 | + * return array of host by IP (4 or 6) |
|
194 | + * @param string $ip |
|
195 | + * @throws Exception |
|
196 | + * @return array objects : array('__name','name','display_name') |
|
197 | + */ |
|
198 | + public function getHostByIP($ip) |
|
199 | + { |
|
200 | + /* |
|
201 | 201 | * curl -k -s -u trapdirector:trapdirector -H 'X-HTTP-Method-Override: GET' -X POST 'https://localhost:5665/v1/objects/hosts' |
202 | 202 | * -d '{"filter":"host.group==\"test_trap\"","attrs": ["address" ,"address6"]}' |
203 | 203 | |
204 | 204 | {"results":[{"attrs":{"__name":"Icinga host","address":"127.0.0.1","display_name":"Icinga host","name":"Icinga host"},"joins":{},"meta":{},"name":"Icinga host","type":"Host"}]} |
205 | 205 | */ |
206 | 206 | |
207 | - $url='objects/hosts'; |
|
208 | - $body=array( |
|
209 | - "filter" => 'host.address=="'.$ip.'" || host.address6=="'.$ip.'"', |
|
210 | - "attrs" => array('__name','name','display_name') |
|
211 | - ); |
|
212 | - try |
|
213 | - { |
|
214 | - $result=$this->request('POST', $url, array('X-HTTP-Method-Override: GET'), $body); |
|
215 | - } catch (Exception $e) |
|
216 | - { |
|
217 | - throw new Exception($e->getMessage()); |
|
218 | - } |
|
207 | + $url='objects/hosts'; |
|
208 | + $body=array( |
|
209 | + "filter" => 'host.address=="'.$ip.'" || host.address6=="'.$ip.'"', |
|
210 | + "attrs" => array('__name','name','display_name') |
|
211 | + ); |
|
212 | + try |
|
213 | + { |
|
214 | + $result=$this->request('POST', $url, array('X-HTTP-Method-Override: GET'), $body); |
|
215 | + } catch (Exception $e) |
|
216 | + { |
|
217 | + throw new Exception($e->getMessage()); |
|
218 | + } |
|
219 | 219 | |
220 | - if (property_exists($result,'error') ) |
|
221 | - { |
|
222 | - if (property_exists($result,'status')) |
|
223 | - { |
|
224 | - throw new Exception('Ret code ' .$result->error.' : ' . $result->status); |
|
225 | - } |
|
226 | - else |
|
227 | - { |
|
228 | - throw new Exception('Ret code ' .$result->error.' : Unkown status'); |
|
229 | - } |
|
230 | - } |
|
231 | - if (property_exists($result, 'results')) |
|
232 | - { |
|
233 | - $numHost=0; |
|
234 | - $hostArray=array(); |
|
235 | - while (isset($result->results[$numHost]) && property_exists ($result->results[$numHost],'attrs')) |
|
236 | - { |
|
237 | - $hostArray[$numHost] = $result->results[$numHost]->attrs; |
|
238 | - $numHost++; |
|
239 | - } |
|
240 | - return $hostArray; |
|
241 | - } |
|
242 | - throw new Exception('Unkown result'); |
|
243 | - } |
|
220 | + if (property_exists($result,'error') ) |
|
221 | + { |
|
222 | + if (property_exists($result,'status')) |
|
223 | + { |
|
224 | + throw new Exception('Ret code ' .$result->error.' : ' . $result->status); |
|
225 | + } |
|
226 | + else |
|
227 | + { |
|
228 | + throw new Exception('Ret code ' .$result->error.' : Unkown status'); |
|
229 | + } |
|
230 | + } |
|
231 | + if (property_exists($result, 'results')) |
|
232 | + { |
|
233 | + $numHost=0; |
|
234 | + $hostArray=array(); |
|
235 | + while (isset($result->results[$numHost]) && property_exists ($result->results[$numHost],'attrs')) |
|
236 | + { |
|
237 | + $hostArray[$numHost] = $result->results[$numHost]->attrs; |
|
238 | + $numHost++; |
|
239 | + } |
|
240 | + return $hostArray; |
|
241 | + } |
|
242 | + throw new Exception('Unkown result'); |
|
243 | + } |
|
244 | 244 | |
245 | - /** |
|
246 | - * Get all host and IP from hostgroup |
|
247 | - * @param string $hostGroup |
|
248 | - * @throws Exception |
|
249 | - * @return array : attributes : address, address6, name |
|
250 | - */ |
|
251 | - public function getHostsIPByHostGroup($hostGroup) |
|
252 | - { |
|
253 | - $url='objects/hosts'; |
|
254 | - $body=array( |
|
255 | - "filter" => '\"'.$hostGroup.'\" in host.groups', |
|
256 | - "attrs" => array('address','address','name') |
|
257 | - ); |
|
258 | - try |
|
259 | - { |
|
260 | - $result=$this->request('POST', $url, array('X-HTTP-Method-Override: GET'), $body); |
|
261 | - } catch (Exception $e) |
|
262 | - { |
|
263 | - throw new Exception($e->getMessage()); |
|
264 | - } |
|
245 | + /** |
|
246 | + * Get all host and IP from hostgroup |
|
247 | + * @param string $hostGroup |
|
248 | + * @throws Exception |
|
249 | + * @return array : attributes : address, address6, name |
|
250 | + */ |
|
251 | + public function getHostsIPByHostGroup($hostGroup) |
|
252 | + { |
|
253 | + $url='objects/hosts'; |
|
254 | + $body=array( |
|
255 | + "filter" => '\"'.$hostGroup.'\" in host.groups', |
|
256 | + "attrs" => array('address','address','name') |
|
257 | + ); |
|
258 | + try |
|
259 | + { |
|
260 | + $result=$this->request('POST', $url, array('X-HTTP-Method-Override: GET'), $body); |
|
261 | + } catch (Exception $e) |
|
262 | + { |
|
263 | + throw new Exception($e->getMessage()); |
|
264 | + } |
|
265 | 265 | |
266 | - if (property_exists($result,'error') ) |
|
267 | - { |
|
268 | - if (property_exists($result,'status')) |
|
269 | - { |
|
270 | - throw new Exception('Ret code ' .$result->error.' : ' . $result->status); |
|
271 | - } |
|
272 | - else |
|
273 | - { |
|
274 | - throw new Exception('Ret code ' .$result->error.' : Unkown status'); |
|
275 | - } |
|
276 | - } |
|
277 | - if (property_exists($result, 'results')) |
|
278 | - { |
|
279 | - $numHost=0; |
|
280 | - $hostArray=array(); |
|
281 | - while (isset($result->results[$numHost]) && property_exists ($result->results[$numHost],'attrs')) |
|
282 | - { |
|
283 | - $hostArray[$numHost] = $result->results[$numHost]->attrs; |
|
284 | - $hostArray[$numHost]->name = $result->results[$numHost]->name; |
|
285 | - $numHost++; |
|
286 | - } |
|
287 | - return $hostArray; |
|
288 | - } |
|
289 | - throw new Exception('Unkown result'); |
|
290 | - } |
|
266 | + if (property_exists($result,'error') ) |
|
267 | + { |
|
268 | + if (property_exists($result,'status')) |
|
269 | + { |
|
270 | + throw new Exception('Ret code ' .$result->error.' : ' . $result->status); |
|
271 | + } |
|
272 | + else |
|
273 | + { |
|
274 | + throw new Exception('Ret code ' .$result->error.' : Unkown status'); |
|
275 | + } |
|
276 | + } |
|
277 | + if (property_exists($result, 'results')) |
|
278 | + { |
|
279 | + $numHost=0; |
|
280 | + $hostArray=array(); |
|
281 | + while (isset($result->results[$numHost]) && property_exists ($result->results[$numHost],'attrs')) |
|
282 | + { |
|
283 | + $hostArray[$numHost] = $result->results[$numHost]->attrs; |
|
284 | + $hostArray[$numHost]->name = $result->results[$numHost]->name; |
|
285 | + $numHost++; |
|
286 | + } |
|
287 | + return $hostArray; |
|
288 | + } |
|
289 | + throw new Exception('Unkown result'); |
|
290 | + } |
|
291 | 291 | |
292 | - /** |
|
293 | - * Send request to API |
|
294 | - * @param string $method get/post/... |
|
295 | - * @param string $url (after /v1/ ) |
|
296 | - * @param array $headers |
|
297 | - * @param array $body |
|
298 | - * @throws Exception |
|
299 | - * @return array |
|
300 | - */ |
|
301 | - public function request($method, $url, $headers, $body) { |
|
302 | - $auth = sprintf('%s:%s', $this->user, $this->pass); |
|
303 | - $curlHeaders = array("Accept: application/json"); |
|
304 | - if ($body !== null) { |
|
305 | - $body = json_encode($body); |
|
306 | - array_push($curlHeaders, 'Content-Type: application/json'); |
|
307 | - //array_push($curlHeaders, 'X-HTTP-Method-Override: GET'); |
|
308 | - } |
|
309 | - //var_dump($body); |
|
310 | - //var_dump($this->url($url)); |
|
311 | - if ($headers !== null) { |
|
312 | - $curlFinalHeaders = array_merge($curlHeaders, $headers); |
|
313 | - } else |
|
314 | - { |
|
315 | - $curlFinalHeaders=$curlHeaders; |
|
316 | - } |
|
317 | - $curl = $this->curl(); |
|
318 | - $opts = array( |
|
319 | - CURLOPT_URL => $this->url($url), |
|
320 | - CURLOPT_HTTPHEADER => $curlFinalHeaders, |
|
321 | - CURLOPT_USERPWD => $auth, |
|
322 | - CURLOPT_CUSTOMREQUEST => strtoupper($method), |
|
323 | - CURLOPT_RETURNTRANSFER => true, |
|
324 | - CURLOPT_CONNECTTIMEOUT => 10, |
|
325 | - CURLOPT_SSL_VERIFYHOST => false, |
|
326 | - CURLOPT_SSL_VERIFYPEER => false, |
|
327 | - ); |
|
328 | - if ($body !== null) { |
|
329 | - $opts[CURLOPT_POSTFIELDS] = $body; |
|
330 | - } |
|
331 | - curl_setopt_array($curl, $opts); |
|
332 | - $res = curl_exec($curl); |
|
333 | - if ($res === false) { |
|
334 | - throw new Exception('CURL ERROR: ' . curl_error($curl)); |
|
335 | - } |
|
336 | - $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
337 | - if ($statusCode === 401) { |
|
338 | - throw new Exception('Unable to authenticate, please check your API credentials'); |
|
339 | - } |
|
340 | - return $this->fromJsonResult($res); |
|
341 | - } |
|
292 | + /** |
|
293 | + * Send request to API |
|
294 | + * @param string $method get/post/... |
|
295 | + * @param string $url (after /v1/ ) |
|
296 | + * @param array $headers |
|
297 | + * @param array $body |
|
298 | + * @throws Exception |
|
299 | + * @return array |
|
300 | + */ |
|
301 | + public function request($method, $url, $headers, $body) { |
|
302 | + $auth = sprintf('%s:%s', $this->user, $this->pass); |
|
303 | + $curlHeaders = array("Accept: application/json"); |
|
304 | + if ($body !== null) { |
|
305 | + $body = json_encode($body); |
|
306 | + array_push($curlHeaders, 'Content-Type: application/json'); |
|
307 | + //array_push($curlHeaders, 'X-HTTP-Method-Override: GET'); |
|
308 | + } |
|
309 | + //var_dump($body); |
|
310 | + //var_dump($this->url($url)); |
|
311 | + if ($headers !== null) { |
|
312 | + $curlFinalHeaders = array_merge($curlHeaders, $headers); |
|
313 | + } else |
|
314 | + { |
|
315 | + $curlFinalHeaders=$curlHeaders; |
|
316 | + } |
|
317 | + $curl = $this->curl(); |
|
318 | + $opts = array( |
|
319 | + CURLOPT_URL => $this->url($url), |
|
320 | + CURLOPT_HTTPHEADER => $curlFinalHeaders, |
|
321 | + CURLOPT_USERPWD => $auth, |
|
322 | + CURLOPT_CUSTOMREQUEST => strtoupper($method), |
|
323 | + CURLOPT_RETURNTRANSFER => true, |
|
324 | + CURLOPT_CONNECTTIMEOUT => 10, |
|
325 | + CURLOPT_SSL_VERIFYHOST => false, |
|
326 | + CURLOPT_SSL_VERIFYPEER => false, |
|
327 | + ); |
|
328 | + if ($body !== null) { |
|
329 | + $opts[CURLOPT_POSTFIELDS] = $body; |
|
330 | + } |
|
331 | + curl_setopt_array($curl, $opts); |
|
332 | + $res = curl_exec($curl); |
|
333 | + if ($res === false) { |
|
334 | + throw new Exception('CURL ERROR: ' . curl_error($curl)); |
|
335 | + } |
|
336 | + $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
337 | + if ($statusCode === 401) { |
|
338 | + throw new Exception('Unable to authenticate, please check your API credentials'); |
|
339 | + } |
|
340 | + return $this->fromJsonResult($res); |
|
341 | + } |
|
342 | 342 | |
343 | - /** |
|
344 | - * |
|
345 | - * @param string $json json encoded |
|
346 | - * @throws Exception |
|
347 | - * @return array json decoded |
|
348 | - */ |
|
349 | - protected function fromJsonResult($json) { |
|
350 | - $result = @json_decode($json); |
|
351 | - //var_dump($json); |
|
352 | - if ($result === null) { |
|
353 | - throw new Exception('Parsing JSON failed: '.$this->getLastJsonErrorMessage(json_last_error())); |
|
354 | - } |
|
355 | - return $result; |
|
356 | - } |
|
343 | + /** |
|
344 | + * |
|
345 | + * @param string $json json encoded |
|
346 | + * @throws Exception |
|
347 | + * @return array json decoded |
|
348 | + */ |
|
349 | + protected function fromJsonResult($json) { |
|
350 | + $result = @json_decode($json); |
|
351 | + //var_dump($json); |
|
352 | + if ($result === null) { |
|
353 | + throw new Exception('Parsing JSON failed: '.$this->getLastJsonErrorMessage(json_last_error())); |
|
354 | + } |
|
355 | + return $result; |
|
356 | + } |
|
357 | 357 | |
358 | - /** |
|
359 | - * Return text error no json error |
|
360 | - * @param string $errorCode |
|
361 | - * @return string |
|
362 | - */ |
|
363 | - protected function getLastJsonErrorMessage($errorCode) { |
|
364 | - if (!array_key_exists($errorCode, $this->errorReference)) { |
|
365 | - return self::JSON_UNKNOWN_ERROR; |
|
366 | - } |
|
367 | - return $this->errorReference[$errorCode]; |
|
368 | - } |
|
358 | + /** |
|
359 | + * Return text error no json error |
|
360 | + * @param string $errorCode |
|
361 | + * @return string |
|
362 | + */ |
|
363 | + protected function getLastJsonErrorMessage($errorCode) { |
|
364 | + if (!array_key_exists($errorCode, $this->errorReference)) { |
|
365 | + return self::JSON_UNKNOWN_ERROR; |
|
366 | + } |
|
367 | + return $this->errorReference[$errorCode]; |
|
368 | + } |
|
369 | 369 | } |
370 | 370 |
@@ -8,19 +8,19 @@ discard block |
||
8 | 8 | |
9 | 9 | class Icinga2API |
10 | 10 | { |
11 | - protected $version = 'v1'; //< icinga2 api version |
|
11 | + protected $version='v1'; //< icinga2 api version |
|
12 | 12 | |
13 | - protected $host; //< icinga2 host name or IP |
|
14 | - protected $port; //< icinga2 api port |
|
13 | + protected $host; //< icinga2 host name or IP |
|
14 | + protected $port; //< icinga2 api port |
|
15 | 15 | |
16 | - protected $user; //< user name |
|
17 | - protected $pass; //< user password |
|
18 | - protected $usercert; //< user key for certificate auth (NOT IMPLEMENTED) |
|
19 | - protected $authmethod='pass'; //< Authentication : 'pass' or 'cert' |
|
16 | + protected $user; //< user name |
|
17 | + protected $pass; //< user password |
|
18 | + protected $usercert; //< user key for certificate auth (NOT IMPLEMENTED) |
|
19 | + protected $authmethod='pass'; //< Authentication : 'pass' or 'cert' |
|
20 | 20 | |
21 | 21 | protected $curl; |
22 | 22 | // http://php.net/manual/de/function.json-last-error.php#119985 |
23 | - protected $errorReference = [ |
|
23 | + protected $errorReference=[ |
|
24 | 24 | JSON_ERROR_NONE => 'No error has occurred.', |
25 | 25 | JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded.', |
26 | 26 | JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON.', |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded.', |
32 | 32 | JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given.', |
33 | 33 | ]; |
34 | - const JSON_UNKNOWN_ERROR = 'Unknown error.'; |
|
34 | + const JSON_UNKNOWN_ERROR='Unknown error.'; |
|
35 | 35 | |
36 | 36 | /** |
37 | 37 | * Creates Icinga2API object |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | * @param string $host host name or IP |
40 | 40 | * @param number $port API port |
41 | 41 | */ |
42 | - public function __construct($host, $port = 5665) |
|
42 | + public function __construct($host, $port=5665) |
|
43 | 43 | { |
44 | 44 | $this->host=$host; |
45 | 45 | $this->port=$port; |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | * @param string $user |
50 | 50 | * @param string $pass |
51 | 51 | */ |
52 | - public function setCredentials($user,$pass) |
|
52 | + public function setCredentials($user, $pass) |
|
53 | 53 | { |
54 | 54 | $this->user=$user; |
55 | 55 | $this->pass=$pass; |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | * @param string $user |
62 | 62 | * @param string $usercert |
63 | 63 | */ |
64 | - public function setCredentialskey($user,$usercert) |
|
64 | + public function setCredentialskey($user, $usercert) |
|
65 | 65 | { |
66 | 66 | $this->user=$user; |
67 | 67 | $this->usercert=$usercert; |
@@ -85,13 +85,13 @@ discard block |
||
85 | 85 | if (property_exists($result, 'results') && property_exists($result->results[0], 'permissions')) |
86 | 86 | { |
87 | 87 | |
88 | - foreach ( $permissions as $mustPermission) |
|
88 | + foreach ($permissions as $mustPermission) |
|
89 | 89 | { |
90 | 90 | $curPermOK=0; |
91 | - foreach ( $result->results[0]->permissions as $curPermission) |
|
91 | + foreach ($result->results[0]->permissions as $curPermission) |
|
92 | 92 | { |
93 | - $curPermission=preg_replace('/\*/','.*',$curPermission); // put * as .* to created a regexp |
|
94 | - if (preg_match('#'.$curPermission.'#',$mustPermission)) |
|
93 | + $curPermission=preg_replace('/\*/', '.*', $curPermission); // put * as .* to created a regexp |
|
94 | + if (preg_match('#'.$curPermission.'#', $mustPermission)) |
|
95 | 95 | { |
96 | 96 | $curPermOK=1; |
97 | 97 | break; |
@@ -106,12 +106,12 @@ discard block |
||
106 | 106 | } |
107 | 107 | if ($permOk == 0) |
108 | 108 | { |
109 | - return array(true,'API connection OK, but missing permission : '.$permMissing); |
|
109 | + return array(true, 'API connection OK, but missing permission : '.$permMissing); |
|
110 | 110 | } |
111 | - return array(false,'API connection OK'); |
|
111 | + return array(false, 'API connection OK'); |
|
112 | 112 | |
113 | 113 | } |
114 | - return array(true,'API connection OK, but cannot get permissions'); |
|
114 | + return array(true, 'API connection OK, but cannot get permissions'); |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | */ |
127 | 127 | protected function curl() { |
128 | 128 | if ($this->curl === null) { |
129 | - $this->curl = curl_init(sprintf('https://%s:%d', $this->host, $this->port)); |
|
129 | + $this->curl=curl_init(sprintf('https://%s:%d', $this->host, $this->port)); |
|
130 | 130 | if ($this->curl === false) { |
131 | 131 | throw new Exception('CURL INIT ERROR'); |
132 | 132 | } |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | * @param string $perfdata : performance data as string |
144 | 144 | * @return array (status = true (oK) or false (nok), string message) |
145 | 145 | */ |
146 | - public function serviceCheckResult($host,$service,$state,$display,$perfdata='') |
|
146 | + public function serviceCheckResult($host, $service, $state, $display, $perfdata='') |
|
147 | 147 | { |
148 | 148 | //Send a POST request to the URL endpoint /v1/actions/process-check-result |
149 | 149 | //actions/process-check-result?service=example.localdomain!passive-ping6 |
@@ -162,9 +162,9 @@ discard block |
||
162 | 162 | { |
163 | 163 | return array(false, $e->getMessage()); |
164 | 164 | } |
165 | - if (property_exists($result,'error') ) |
|
165 | + if (property_exists($result, 'error')) |
|
166 | 166 | { |
167 | - if (property_exists($result,'status')) |
|
167 | + if (property_exists($result, 'status')) |
|
168 | 168 | { |
169 | 169 | $message=$result->status; |
170 | 170 | } |
@@ -172,21 +172,21 @@ discard block |
||
172 | 172 | { |
173 | 173 | $message="Unkown status"; |
174 | 174 | } |
175 | - return array(false , 'Ret code ' .$result->error.' : '.$message); |
|
175 | + return array(false, 'Ret code '.$result->error.' : '.$message); |
|
176 | 176 | } |
177 | 177 | if (property_exists($result, 'results')) |
178 | 178 | { |
179 | 179 | if (isset($result->results[0])) |
180 | 180 | { |
181 | - return array(true,'code '.$result->results[0]->code.' : '.$result->results[0]->status); |
|
181 | + return array(true, 'code '.$result->results[0]->code.' : '.$result->results[0]->status); |
|
182 | 182 | } |
183 | 183 | else |
184 | 184 | { |
185 | - return array(false,'Service not found'); |
|
185 | + return array(false, 'Service not found'); |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | } |
189 | - return array(false,'Unkown result, open issue with this : '.print_r($result,true)); |
|
189 | + return array(false, 'Unkown result, open issue with this : '.print_r($result, true)); |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | /** |
@@ -207,7 +207,7 @@ discard block |
||
207 | 207 | $url='objects/hosts'; |
208 | 208 | $body=array( |
209 | 209 | "filter" => 'host.address=="'.$ip.'" || host.address6=="'.$ip.'"', |
210 | - "attrs" => array('__name','name','display_name') |
|
210 | + "attrs" => array('__name', 'name', 'display_name') |
|
211 | 211 | ); |
212 | 212 | try |
213 | 213 | { |
@@ -217,24 +217,24 @@ discard block |
||
217 | 217 | throw new Exception($e->getMessage()); |
218 | 218 | } |
219 | 219 | |
220 | - if (property_exists($result,'error') ) |
|
220 | + if (property_exists($result, 'error')) |
|
221 | 221 | { |
222 | - if (property_exists($result,'status')) |
|
222 | + if (property_exists($result, 'status')) |
|
223 | 223 | { |
224 | - throw new Exception('Ret code ' .$result->error.' : ' . $result->status); |
|
224 | + throw new Exception('Ret code '.$result->error.' : '.$result->status); |
|
225 | 225 | } |
226 | 226 | else |
227 | 227 | { |
228 | - throw new Exception('Ret code ' .$result->error.' : Unkown status'); |
|
228 | + throw new Exception('Ret code '.$result->error.' : Unkown status'); |
|
229 | 229 | } |
230 | 230 | } |
231 | 231 | if (property_exists($result, 'results')) |
232 | 232 | { |
233 | 233 | $numHost=0; |
234 | 234 | $hostArray=array(); |
235 | - while (isset($result->results[$numHost]) && property_exists ($result->results[$numHost],'attrs')) |
|
235 | + while (isset($result->results[$numHost]) && property_exists($result->results[$numHost], 'attrs')) |
|
236 | 236 | { |
237 | - $hostArray[$numHost] = $result->results[$numHost]->attrs; |
|
237 | + $hostArray[$numHost]=$result->results[$numHost]->attrs; |
|
238 | 238 | $numHost++; |
239 | 239 | } |
240 | 240 | return $hostArray; |
@@ -253,7 +253,7 @@ discard block |
||
253 | 253 | $url='objects/hosts'; |
254 | 254 | $body=array( |
255 | 255 | "filter" => '\"'.$hostGroup.'\" in host.groups', |
256 | - "attrs" => array('address','address','name') |
|
256 | + "attrs" => array('address', 'address', 'name') |
|
257 | 257 | ); |
258 | 258 | try |
259 | 259 | { |
@@ -263,25 +263,25 @@ discard block |
||
263 | 263 | throw new Exception($e->getMessage()); |
264 | 264 | } |
265 | 265 | |
266 | - if (property_exists($result,'error') ) |
|
266 | + if (property_exists($result, 'error')) |
|
267 | 267 | { |
268 | - if (property_exists($result,'status')) |
|
268 | + if (property_exists($result, 'status')) |
|
269 | 269 | { |
270 | - throw new Exception('Ret code ' .$result->error.' : ' . $result->status); |
|
270 | + throw new Exception('Ret code '.$result->error.' : '.$result->status); |
|
271 | 271 | } |
272 | 272 | else |
273 | 273 | { |
274 | - throw new Exception('Ret code ' .$result->error.' : Unkown status'); |
|
274 | + throw new Exception('Ret code '.$result->error.' : Unkown status'); |
|
275 | 275 | } |
276 | 276 | } |
277 | 277 | if (property_exists($result, 'results')) |
278 | 278 | { |
279 | 279 | $numHost=0; |
280 | 280 | $hostArray=array(); |
281 | - while (isset($result->results[$numHost]) && property_exists ($result->results[$numHost],'attrs')) |
|
281 | + while (isset($result->results[$numHost]) && property_exists($result->results[$numHost], 'attrs')) |
|
282 | 282 | { |
283 | - $hostArray[$numHost] = $result->results[$numHost]->attrs; |
|
284 | - $hostArray[$numHost]->name = $result->results[$numHost]->name; |
|
283 | + $hostArray[$numHost]=$result->results[$numHost]->attrs; |
|
284 | + $hostArray[$numHost]->name=$result->results[$numHost]->name; |
|
285 | 285 | $numHost++; |
286 | 286 | } |
287 | 287 | return $hostArray; |
@@ -299,23 +299,23 @@ discard block |
||
299 | 299 | * @return array |
300 | 300 | */ |
301 | 301 | public function request($method, $url, $headers, $body) { |
302 | - $auth = sprintf('%s:%s', $this->user, $this->pass); |
|
303 | - $curlHeaders = array("Accept: application/json"); |
|
302 | + $auth=sprintf('%s:%s', $this->user, $this->pass); |
|
303 | + $curlHeaders=array("Accept: application/json"); |
|
304 | 304 | if ($body !== null) { |
305 | - $body = json_encode($body); |
|
305 | + $body=json_encode($body); |
|
306 | 306 | array_push($curlHeaders, 'Content-Type: application/json'); |
307 | 307 | //array_push($curlHeaders, 'X-HTTP-Method-Override: GET'); |
308 | 308 | } |
309 | 309 | //var_dump($body); |
310 | 310 | //var_dump($this->url($url)); |
311 | 311 | if ($headers !== null) { |
312 | - $curlFinalHeaders = array_merge($curlHeaders, $headers); |
|
312 | + $curlFinalHeaders=array_merge($curlHeaders, $headers); |
|
313 | 313 | } else |
314 | 314 | { |
315 | 315 | $curlFinalHeaders=$curlHeaders; |
316 | 316 | } |
317 | - $curl = $this->curl(); |
|
318 | - $opts = array( |
|
317 | + $curl=$this->curl(); |
|
318 | + $opts=array( |
|
319 | 319 | CURLOPT_URL => $this->url($url), |
320 | 320 | CURLOPT_HTTPHEADER => $curlFinalHeaders, |
321 | 321 | CURLOPT_USERPWD => $auth, |
@@ -326,14 +326,14 @@ discard block |
||
326 | 326 | CURLOPT_SSL_VERIFYPEER => false, |
327 | 327 | ); |
328 | 328 | if ($body !== null) { |
329 | - $opts[CURLOPT_POSTFIELDS] = $body; |
|
329 | + $opts[CURLOPT_POSTFIELDS]=$body; |
|
330 | 330 | } |
331 | 331 | curl_setopt_array($curl, $opts); |
332 | - $res = curl_exec($curl); |
|
332 | + $res=curl_exec($curl); |
|
333 | 333 | if ($res === false) { |
334 | - throw new Exception('CURL ERROR: ' . curl_error($curl)); |
|
334 | + throw new Exception('CURL ERROR: '.curl_error($curl)); |
|
335 | 335 | } |
336 | - $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
336 | + $statusCode=curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
337 | 337 | if ($statusCode === 401) { |
338 | 338 | throw new Exception('Unable to authenticate, please check your API credentials'); |
339 | 339 | } |
@@ -347,7 +347,7 @@ discard block |
||
347 | 347 | * @return array json decoded |
348 | 348 | */ |
349 | 349 | protected function fromJsonResult($json) { |
350 | - $result = @json_decode($json); |
|
350 | + $result=@json_decode($json); |
|
351 | 351 | //var_dump($json); |
352 | 352 | if ($result === null) { |
353 | 353 | throw new Exception('Parsing JSON failed: '.$this->getLastJsonErrorMessage(json_last_error())); |
@@ -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,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')); |
@@ -21,19 +21,19 @@ discard block |
||
21 | 21 | /************ Trapdb ***********/ |
22 | 22 | try |
23 | 23 | { |
24 | - $dbConn = $this->getUIDatabase()->getDbConn(); |
|
24 | + $dbConn=$this->getUIDatabase()->getDbConn(); |
|
25 | 25 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
26 | - $query = $dbConn->select()->from( |
|
26 | + $query=$dbConn->select()->from( |
|
27 | 27 | $this->getModuleConfig()->getTrapTableName(), |
28 | 28 | array('COUNT(*)') |
29 | 29 | ); |
30 | 30 | $this->view->trap_count=$dbConn->fetchOne($query); |
31 | - $query = $dbConn->select()->from( |
|
31 | + $query=$dbConn->select()->from( |
|
32 | 32 | $this->getModuleConfig()->getTrapDataTableName(), |
33 | 33 | array('COUNT(*)') |
34 | 34 | ); |
35 | 35 | $this->view->trap_object_count=$dbConn->fetchOne($query); |
36 | - $query = $dbConn->select()->from( |
|
36 | + $query=$dbConn->select()->from( |
|
37 | 37 | $this->getModuleConfig()->getTrapRuleName(), |
38 | 38 | array('COUNT(*)') |
39 | 39 | ); |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | } |
45 | 45 | catch (Exception $e) |
46 | 46 | { |
47 | - $this->displayExitError('status',$e->getMessage()); |
|
47 | + $this->displayExitError('status', $e->getMessage()); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | /*************** Log destination *******************/ |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | } |
60 | 60 | catch (Exception $e) |
61 | 61 | { |
62 | - $this->displayExitError('status',$e->getMessage()); |
|
62 | + $this->displayExitError('status', $e->getMessage()); |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | } |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | if ($action == 'update_mib_db') |
86 | 86 | { // Do the update in background |
87 | 87 | $return=exec('icingacli trapdirector mib update --pid /tmp/trapdirector_update.pid'); |
88 | - if (preg_match('/OK/',$return)) |
|
88 | + if (preg_match('/OK/', $return)) |
|
89 | 89 | { |
90 | 90 | $this->_helper->json(array('status'=>'OK')); |
91 | 91 | } |
@@ -94,32 +94,32 @@ discard block |
||
94 | 94 | } |
95 | 95 | if ($action == 'check_update') |
96 | 96 | { |
97 | - $file=@fopen('/tmp/trapdirector_update.pid','r'); |
|
97 | + $file=@fopen('/tmp/trapdirector_update.pid', 'r'); |
|
98 | 98 | if ($file == false) |
99 | 99 | { // process is dead |
100 | - $this->_helper->json(array('status'=>'tu quoque fili','err'=>'Cannot open file')); |
|
100 | + $this->_helper->json(array('status'=>'tu quoque fili', 'err'=>'Cannot open file')); |
|
101 | 101 | return; |
102 | 102 | } |
103 | 103 | $pid=fgets($file); |
104 | 104 | $output=array(); |
105 | 105 | $retVal=0; |
106 | - exec('ps '.$pid,$output,$retVal); |
|
106 | + exec('ps '.$pid, $output, $retVal); |
|
107 | 107 | if ($retVal == 0) |
108 | 108 | { // process is alive |
109 | 109 | $this->_helper->json(array('status'=>'Alive and kicking')); |
110 | 110 | } |
111 | 111 | else |
112 | 112 | { // process is dead |
113 | - $this->_helper->json(array('status'=>'tu quoque fili','err'=>'no proc'.$pid)); |
|
113 | + $this->_helper->json(array('status'=>'tu quoque fili', 'err'=>'no proc'.$pid)); |
|
114 | 114 | } |
115 | 115 | } |
116 | - $this->_helper->json(array('status'=>'ERR : no '.$action.' action possible' )); |
|
116 | + $this->_helper->json(array('status'=>'ERR : no '.$action.' action possible')); |
|
117 | 117 | } |
118 | 118 | /** Check for mib file UPLOAD */ |
119 | 119 | if (isset($_FILES['mibfile'])) |
120 | 120 | { |
121 | - $name=filter_var($_FILES['mibfile']['name'],FILTER_SANITIZE_STRING); |
|
122 | - $DirConf=explode(':',$this->Config()->get('config', 'snmptranslate_dirs')); |
|
121 | + $name=filter_var($_FILES['mibfile']['name'], FILTER_SANITIZE_STRING); |
|
122 | + $DirConf=explode(':', $this->Config()->get('config', 'snmptranslate_dirs')); |
|
123 | 123 | $destDir=array_shift($DirConf); |
124 | 124 | if (!is_dir($destDir)) |
125 | 125 | { |
@@ -133,9 +133,9 @@ discard block |
||
133 | 133 | } |
134 | 134 | else |
135 | 135 | { |
136 | - $destination = $destDir .'/'.$name; //$this->Module()->getBaseDir() . "/mibs/$name"; |
|
137 | - $sourceTmpNam=filter_var($_FILES['mibfile']['tmp_name'],FILTER_SANITIZE_STRING); |
|
138 | - if (move_uploaded_file($sourceTmpNam,$destination)===false) |
|
136 | + $destination=$destDir.'/'.$name; //$this->Module()->getBaseDir() . "/mibs/$name"; |
|
137 | + $sourceTmpNam=filter_var($_FILES['mibfile']['tmp_name'], FILTER_SANITIZE_STRING); |
|
138 | + if (move_uploaded_file($sourceTmpNam, $destination) === false) |
|
139 | 139 | { |
140 | 140 | $this->view->uploadStatus="ERROR, file $destination not loaded. Check file and path name or selinux violations"; |
141 | 141 | } |
@@ -151,13 +151,13 @@ discard block |
||
151 | 151 | } |
152 | 152 | |
153 | 153 | // snmptranslate tests |
154 | - $snmptranslate = $this->Config()->get('config', 'snmptranslate'); |
|
154 | + $snmptranslate=$this->Config()->get('config', 'snmptranslate'); |
|
155 | 155 | $this->view->snmptranslate_bin=$snmptranslate; |
156 | 156 | $this->view->snmptranslate_state='warn'; |
157 | - if (is_executable ( $snmptranslate )) |
|
157 | + if (is_executable($snmptranslate)) |
|
158 | 158 | { |
159 | - $translate=exec($snmptranslate . ' 1'); |
|
160 | - if (preg_match('/iso/',$translate)) |
|
159 | + $translate=exec($snmptranslate.' 1'); |
|
160 | + if (preg_match('/iso/', $translate)) |
|
161 | 161 | { |
162 | 162 | $this->view->snmptranslate='works fine'; |
163 | 163 | $this->view->snmptranslate_state='ok'; |
@@ -175,46 +175,46 @@ discard block |
||
175 | 175 | // mib database |
176 | 176 | |
177 | 177 | $this->view->mibDbCount=$this->getMIB()->countObjects(); |
178 | - $this->view->mibDbCountTrap=$this->getMIB()->countObjects(null,21); |
|
178 | + $this->view->mibDbCountTrap=$this->getMIB()->countObjects(null, 21); |
|
179 | 179 | |
180 | 180 | // mib dirs |
181 | 181 | $DirConf=$this->Config()->get('config', 'snmptranslate_dirs'); |
182 | - $dirArray=explode(':',$DirConf); |
|
182 | + $dirArray=explode(':', $DirConf); |
|
183 | 183 | |
184 | 184 | // Get base directories from net-snmp-config |
185 | 185 | $output=$matches=array(); |
186 | 186 | $retVal=0; |
187 | - $sysDirs=exec('net-snmp-config --default-mibdirs',$output,$retVal); |
|
188 | - if ($retVal==0) |
|
187 | + $sysDirs=exec('net-snmp-config --default-mibdirs', $output, $retVal); |
|
188 | + if ($retVal == 0) |
|
189 | 189 | { |
190 | - $dirArray=array_merge($dirArray,explode(':',$sysDirs)); |
|
190 | + $dirArray=array_merge($dirArray, explode(':', $sysDirs)); |
|
191 | 191 | } |
192 | 192 | else |
193 | 193 | { |
194 | - $translateOut=exec($this->Config()->get('config', 'snmptranslate') . ' -Dinit_mib .1.3 2>&1 | grep MIBDIRS'); |
|
195 | - if (preg_match('/MIBDIRS.*\'([^\']+)\'/',$translateOut,$matches)) |
|
194 | + $translateOut=exec($this->Config()->get('config', 'snmptranslate').' -Dinit_mib .1.3 2>&1 | grep MIBDIRS'); |
|
195 | + if (preg_match('/MIBDIRS.*\'([^\']+)\'/', $translateOut, $matches)) |
|
196 | 196 | { |
197 | - $dirArray=array_merge($dirArray,explode(':',$matches[1])); |
|
197 | + $dirArray=array_merge($dirArray, explode(':', $matches[1])); |
|
198 | 198 | } |
199 | 199 | else |
200 | 200 | { |
201 | - array_push($dirArray,'Install net-snmp-config to see system directories'); |
|
201 | + array_push($dirArray, 'Install net-snmp-config to see system directories'); |
|
202 | 202 | } |
203 | 203 | } |
204 | 204 | |
205 | 205 | $this->view->dirArray=$dirArray; |
206 | 206 | |
207 | 207 | $output=null; |
208 | - foreach (explode(':',$DirConf) as $mibdir) |
|
208 | + foreach (explode(':', $DirConf) as $mibdir) |
|
209 | 209 | { |
210 | - exec('ls '.$mibdir.' | grep -v traplist.txt',$output); |
|
210 | + exec('ls '.$mibdir.' | grep -v traplist.txt', $output); |
|
211 | 211 | } |
212 | 212 | //$i=0;$listFiles='';while (isset($output[$i])) $listFiles.=$output[$i++]; |
213 | 213 | //$this->view->fileList=explode(' ',$listFiles); |
214 | 214 | $this->view->fileList=$output; |
215 | 215 | |
216 | 216 | // Zend form |
217 | - $this->view->form= new UploadForm(); |
|
217 | + $this->view->form=new UploadForm(); |
|
218 | 218 | //$this->view->form= new Form('upload-form'); |
219 | 219 | |
220 | 220 | |
@@ -238,18 +238,18 @@ discard block |
||
238 | 238 | $this->view->templateForm_output=''; |
239 | 239 | if (isset($postData['template_name']) && isset($postData['template_revert_time'])) |
240 | 240 | { |
241 | - $template_create = 'icingacli director service create --json \'{ "check_command": "dummy", '; |
|
242 | - $template_create .= '"check_interval": "' .$postData['template_revert_time']. '", "check_timeout": "20", "disabled": false, "enable_active_checks": true, "enable_event_handler": true, "enable_notifications": true, "enable_passive_checks": true, "enable_perfdata": true, "max_check_attempts": "1", '; |
|
243 | - $template_create .= '"object_name": "'.$postData['template_name'].'", "object_type": "template", "retry_interval": "'.$postData['template_revert_time'].'"}\''; |
|
241 | + $template_create='icingacli director service create --json \'{ "check_command": "dummy", '; |
|
242 | + $template_create.='"check_interval": "'.$postData['template_revert_time'].'", "check_timeout": "20", "disabled": false, "enable_active_checks": true, "enable_event_handler": true, "enable_notifications": true, "enable_passive_checks": true, "enable_perfdata": true, "max_check_attempts": "1", '; |
|
243 | + $template_create.='"object_name": "'.$postData['template_name'].'", "object_type": "template", "retry_interval": "'.$postData['template_revert_time'].'"}\''; |
|
244 | 244 | $output=array(); |
245 | 245 | $ret_code=0; |
246 | - exec($template_create,$output,$ret_code); |
|
246 | + exec($template_create, $output, $ret_code); |
|
247 | 247 | if ($ret_code != 0) |
248 | 248 | { |
249 | - $this->displayExitError("Status -> Services","Error creating template : ".$output[0].'<br>Command was : '.$template_create); |
|
249 | + $this->displayExitError("Status -> Services", "Error creating template : ".$output[0].'<br>Command was : '.$template_create); |
|
250 | 250 | } |
251 | - exec('icingacli director config deploy',$output,$ret_code); |
|
252 | - $this->view->templateForm_output='Template '.$postData['template_name']. ' created'; |
|
251 | + exec('icingacli director config deploy', $output, $ret_code); |
|
252 | + $this->view->templateForm_output='Template '.$postData['template_name'].' created'; |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | // template creation form |
@@ -265,15 +265,15 @@ discard block |
||
265 | 265 | { |
266 | 266 | $this->prepareTabs()->activate('plugins'); |
267 | 267 | |
268 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
268 | + require_once($this->Module()->getBaseDir().'/bin/trap_class.php'); |
|
269 | 269 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
270 | - $Trap = new Trap($icingaweb2_etc,4); |
|
270 | + $Trap=new Trap($icingaweb2_etc, 4); |
|
271 | 271 | |
272 | - $this->view->pluginLoaded = htmlentities($Trap->pluginClass->registerAllPlugins(false)); |
|
272 | + $this->view->pluginLoaded=htmlentities($Trap->pluginClass->registerAllPlugins(false)); |
|
273 | 273 | |
274 | - $enabledPlugins = $Trap->pluginClass->getEnabledPlugins(); |
|
274 | + $enabledPlugins=$Trap->pluginClass->getEnabledPlugins(); |
|
275 | 275 | |
276 | - $pluginList = $Trap->pluginClass->pluginList(); |
|
276 | + $pluginList=$Trap->pluginClass->pluginList(); |
|
277 | 277 | |
278 | 278 | // Plugin list and fill function name list |
279 | 279 | $functionList=array(); |
@@ -281,16 +281,16 @@ discard block |
||
281 | 281 | foreach ($pluginList as $plugin) |
282 | 282 | { |
283 | 283 | $pluginDetails=$Trap->pluginClass->pluginDetails($plugin); |
284 | - $pluginDetails->enabled = (in_array($plugin, $enabledPlugins)) ? true : false; |
|
285 | - $pluginDetails->catchAllTraps = ($pluginDetails->catchAllTraps === true )? 'Yes' : 'No'; |
|
286 | - $pluginDetails->processTraps = ($pluginDetails->processTraps === true )? 'Yes' : 'No'; |
|
287 | - $pluginDetails->description = htmlentities($pluginDetails->description); |
|
288 | - $pluginDetails->description = preg_replace('/\n/','<br>',$pluginDetails->description); |
|
284 | + $pluginDetails->enabled=(in_array($plugin, $enabledPlugins)) ? true : false; |
|
285 | + $pluginDetails->catchAllTraps=($pluginDetails->catchAllTraps === true) ? 'Yes' : 'No'; |
|
286 | + $pluginDetails->processTraps=($pluginDetails->processTraps === true) ? 'Yes' : 'No'; |
|
287 | + $pluginDetails->description=htmlentities($pluginDetails->description); |
|
288 | + $pluginDetails->description=preg_replace('/\n/', '<br>', $pluginDetails->description); |
|
289 | 289 | array_push($this->view->pluginArray, $pluginDetails); |
290 | 290 | // Get functions for function details |
291 | 291 | foreach ($pluginDetails->funcArray as $function) |
292 | 292 | { |
293 | - array_push($functionList,$function); |
|
293 | + array_push($functionList, $function); |
|
294 | 294 | } |
295 | 295 | } |
296 | 296 | |
@@ -298,10 +298,10 @@ discard block |
||
298 | 298 | $this->view->functionList=array(); |
299 | 299 | foreach ($functionList as $function) |
300 | 300 | { |
301 | - $functionDetail = $Trap->pluginClass->getFunctionDetails($function); |
|
302 | - $functionDetail->params = htmlentities($functionDetail->params); |
|
303 | - $functionDetail->description = htmlentities($functionDetail->description); |
|
304 | - $functionDetail->description = preg_replace('/\n/','<br>',$functionDetail->description); |
|
301 | + $functionDetail=$Trap->pluginClass->getFunctionDetails($function); |
|
302 | + $functionDetail->params=htmlentities($functionDetail->params); |
|
303 | + $functionDetail->description=htmlentities($functionDetail->description); |
|
304 | + $functionDetail->description=preg_replace('/\n/', '<br>', $functionDetail->description); |
|
305 | 305 | array_push($this->view->functionList, $functionDetail); |
306 | 306 | } |
307 | 307 | |
@@ -311,16 +311,16 @@ discard block |
||
311 | 311 | { |
312 | 312 | return $this->getTabs()->add('status', array( |
313 | 313 | 'label' => $this->translate('Status'), |
314 | - 'url' => $this->getModuleConfig()->urlPath() . '/status') |
|
314 | + 'url' => $this->getModuleConfig()->urlPath().'/status') |
|
315 | 315 | )->add('mib', array( |
316 | 316 | 'label' => $this->translate('MIB Management'), |
317 | - 'url' => $this->getModuleConfig()->urlPath() . '/status/mib') |
|
317 | + 'url' => $this->getModuleConfig()->urlPath().'/status/mib') |
|
318 | 318 | )->add('services', array( |
319 | 319 | 'label' => $this->translate('Services management'), |
320 | - 'url' => $this->getModuleConfig()->urlPath() . '/status/services') |
|
320 | + 'url' => $this->getModuleConfig()->urlPath().'/status/services') |
|
321 | 321 | )->add('plugins', array( |
322 | 322 | 'label' => $this->translate('Plugins management'), |
323 | - 'url' => $this->getModuleConfig()->urlPath() . '/status/plugins') |
|
323 | + 'url' => $this->getModuleConfig()->urlPath().'/status/plugins') |
|
324 | 324 | ); |
325 | 325 | } |
326 | 326 | } |
@@ -328,7 +328,7 @@ discard block |
||
328 | 328 | // TODO : see if useless |
329 | 329 | class UploadForm extends Form |
330 | 330 | { |
331 | - public function __construct($options = null) |
|
331 | + public function __construct($options=null) |
|
332 | 332 | { |
333 | 333 | parent::__construct($options); |
334 | 334 | $this->addElements2(); |
@@ -337,11 +337,11 @@ discard block |
||
337 | 337 | public function addElements2() |
338 | 338 | { |
339 | 339 | // File Input |
340 | - $file = new File('mib-file'); |
|
340 | + $file=new File('mib-file'); |
|
341 | 341 | $file->setLabel('Mib upload'); |
342 | 342 | //->setAttrib('multiple', null); |
343 | 343 | $this->addElement($file); |
344 | - $button = new Submit("upload",array('ignore'=>false)); |
|
345 | - $this->addElement($button);//->setIgnore(false); |
|
344 | + $button=new Submit("upload", array('ignore'=>false)); |
|
345 | + $this->addElement($button); //->setIgnore(false); |
|
346 | 346 | } |
347 | 347 | } |
@@ -21,8 +21,8 @@ discard block |
||
21 | 21 | /************ Trapdb ***********/ |
22 | 22 | try |
23 | 23 | { |
24 | - $dbConn = $this->getUIDatabase()->getDbConn(); |
|
25 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
24 | + $dbConn = $this->getUIDatabase()->getDbConn(); |
|
25 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
26 | 26 | $query = $dbConn->select()->from( |
27 | 27 | $this->getModuleConfig()->getTrapTableName(), |
28 | 28 | array('COUNT(*)') |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | |
52 | 52 | try |
53 | 53 | { |
54 | - $this->view->currentLogDestination=$this->getUIDatabase()->getDBConfigValue('log_destination'); |
|
54 | + $this->view->currentLogDestination=$this->getUIDatabase()->getDBConfigValue('log_destination'); |
|
55 | 55 | $this->view->logDestinations=$this->getModuleConfig()->getLogDestinations(); |
56 | 56 | $this->view->currentLogFile=$this->getUIDatabase()->getDBConfigValue('log_file'); |
57 | 57 | $this->view->logLevels=$this->getModuleConfig()->getlogLevels(); |
@@ -65,10 +65,10 @@ discard block |
||
65 | 65 | } |
66 | 66 | |
67 | 67 | /** Mib management |
68 | - * Post param : action=update_mib_db : update mib database |
|
69 | - * Post param : ation=check_update : check if mib update is finished |
|
70 | - * File post : mibfile -> save mib file |
|
71 | - */ |
|
68 | + * Post param : action=update_mib_db : update mib database |
|
69 | + * Post param : ation=check_update : check if mib update is finished |
|
70 | + * File post : mibfile -> save mib file |
|
71 | + */ |
|
72 | 72 | public function mibAction() |
73 | 73 | { |
74 | 74 | $this->prepareTabs()->activate('mib'); |
@@ -87,22 +87,22 @@ discard block |
||
87 | 87 | $return=exec('icingacli trapdirector mib update --pid /tmp/trapdirector_update.pid'); |
88 | 88 | if (preg_match('/OK/',$return)) |
89 | 89 | { |
90 | - $this->_helper->json(array('status'=>'OK')); |
|
90 | + $this->_helper->json(array('status'=>'OK')); |
|
91 | 91 | } |
92 | 92 | // Error |
93 | 93 | $this->_helper->json(array('status'=>$return)); |
94 | 94 | } |
95 | 95 | if ($action == 'check_update') |
96 | 96 | { |
97 | - $file=@fopen('/tmp/trapdirector_update.pid','r'); |
|
98 | - if ($file == false) |
|
99 | - { // process is dead |
|
100 | - $this->_helper->json(array('status'=>'tu quoque fili','err'=>'Cannot open file')); |
|
101 | - return; |
|
102 | - } |
|
103 | - $pid=fgets($file); |
|
104 | - $output=array(); |
|
105 | - $retVal=0; |
|
97 | + $file=@fopen('/tmp/trapdirector_update.pid','r'); |
|
98 | + if ($file == false) |
|
99 | + { // process is dead |
|
100 | + $this->_helper->json(array('status'=>'tu quoque fili','err'=>'Cannot open file')); |
|
101 | + return; |
|
102 | + } |
|
103 | + $pid=fgets($file); |
|
104 | + $output=array(); |
|
105 | + $retVal=0; |
|
106 | 106 | exec('ps '.$pid,$output,$retVal); |
107 | 107 | if ($retVal == 0) |
108 | 108 | { // process is alive |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | } |
111 | 111 | else |
112 | 112 | { // process is dead |
113 | - $this->_helper->json(array('status'=>'tu quoque fili','err'=>'no proc'.$pid)); |
|
113 | + $this->_helper->json(array('status'=>'tu quoque fili','err'=>'no proc'.$pid)); |
|
114 | 114 | } |
115 | 115 | } |
116 | 116 | $this->_helper->json(array('status'=>'ERR : no '.$action.' action possible' )); |
@@ -118,32 +118,32 @@ discard block |
||
118 | 118 | /** Check for mib file UPLOAD */ |
119 | 119 | if (isset($_FILES['mibfile'])) |
120 | 120 | { |
121 | - $name=filter_var($_FILES['mibfile']['name'],FILTER_SANITIZE_STRING); |
|
121 | + $name=filter_var($_FILES['mibfile']['name'],FILTER_SANITIZE_STRING); |
|
122 | 122 | $DirConf=explode(':',$this->Config()->get('config', 'snmptranslate_dirs')); |
123 | 123 | $destDir=array_shift($DirConf); |
124 | 124 | if (!is_dir($destDir)) |
125 | 125 | { |
126 | - $this->view->uploadStatus="ERROR : no $destDir directory, check module configuration"; |
|
126 | + $this->view->uploadStatus="ERROR : no $destDir directory, check module configuration"; |
|
127 | 127 | } |
128 | 128 | else |
129 | 129 | { |
130 | - if (!is_writable($destDir)) |
|
131 | - { |
|
132 | - $this->view->uploadStatus="ERROR : $destDir directory is not writable"; |
|
133 | - } |
|
134 | - else |
|
135 | - { |
|
136 | - $destination = $destDir .'/'.$name; //$this->Module()->getBaseDir() . "/mibs/$name"; |
|
137 | - $sourceTmpNam=filter_var($_FILES['mibfile']['tmp_name'],FILTER_SANITIZE_STRING); |
|
138 | - if (move_uploaded_file($sourceTmpNam,$destination)===false) |
|
139 | - { |
|
140 | - $this->view->uploadStatus="ERROR, file $destination not loaded. Check file and path name or selinux violations"; |
|
141 | - } |
|
142 | - else |
|
143 | - { |
|
144 | - $this->view->uploadStatus="File $name uploaded in $destDir"; |
|
145 | - } |
|
146 | - } |
|
130 | + if (!is_writable($destDir)) |
|
131 | + { |
|
132 | + $this->view->uploadStatus="ERROR : $destDir directory is not writable"; |
|
133 | + } |
|
134 | + else |
|
135 | + { |
|
136 | + $destination = $destDir .'/'.$name; //$this->Module()->getBaseDir() . "/mibs/$name"; |
|
137 | + $sourceTmpNam=filter_var($_FILES['mibfile']['tmp_name'],FILTER_SANITIZE_STRING); |
|
138 | + if (move_uploaded_file($sourceTmpNam,$destination)===false) |
|
139 | + { |
|
140 | + $this->view->uploadStatus="ERROR, file $destination not loaded. Check file and path name or selinux violations"; |
|
141 | + } |
|
142 | + else |
|
143 | + { |
|
144 | + $this->view->uploadStatus="File $name uploaded in $destDir"; |
|
145 | + } |
|
146 | + } |
|
147 | 147 | } |
148 | 148 | |
149 | 149 | } |
@@ -263,47 +263,47 @@ discard block |
||
263 | 263 | */ |
264 | 264 | public function pluginsAction() |
265 | 265 | { |
266 | - $this->prepareTabs()->activate('plugins'); |
|
266 | + $this->prepareTabs()->activate('plugins'); |
|
267 | 267 | |
268 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
269 | - $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
270 | - $Trap = new Trap($icingaweb2_etc,4); |
|
268 | + require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
269 | + $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
270 | + $Trap = new Trap($icingaweb2_etc,4); |
|
271 | 271 | |
272 | - $this->view->pluginLoaded = htmlentities($Trap->pluginClass->registerAllPlugins(false)); |
|
272 | + $this->view->pluginLoaded = htmlentities($Trap->pluginClass->registerAllPlugins(false)); |
|
273 | 273 | |
274 | - $enabledPlugins = $Trap->pluginClass->getEnabledPlugins(); |
|
274 | + $enabledPlugins = $Trap->pluginClass->getEnabledPlugins(); |
|
275 | 275 | |
276 | - $pluginList = $Trap->pluginClass->pluginList(); |
|
276 | + $pluginList = $Trap->pluginClass->pluginList(); |
|
277 | 277 | |
278 | - // Plugin list and fill function name list |
|
279 | - $functionList=array(); |
|
280 | - $this->view->pluginArray=array(); |
|
281 | - foreach ($pluginList as $plugin) |
|
282 | - { |
|
283 | - $pluginDetails=$Trap->pluginClass->pluginDetails($plugin); |
|
284 | - $pluginDetails->enabled = (in_array($plugin, $enabledPlugins)) ? true : false; |
|
285 | - $pluginDetails->catchAllTraps = ($pluginDetails->catchAllTraps === true )? 'Yes' : 'No'; |
|
286 | - $pluginDetails->processTraps = ($pluginDetails->processTraps === true )? 'Yes' : 'No'; |
|
287 | - $pluginDetails->description = htmlentities($pluginDetails->description); |
|
288 | - $pluginDetails->description = preg_replace('/\n/','<br>',$pluginDetails->description); |
|
289 | - array_push($this->view->pluginArray, $pluginDetails); |
|
290 | - // Get functions for function details |
|
291 | - foreach ($pluginDetails->funcArray as $function) |
|
292 | - { |
|
293 | - array_push($functionList,$function); |
|
294 | - } |
|
295 | - } |
|
278 | + // Plugin list and fill function name list |
|
279 | + $functionList=array(); |
|
280 | + $this->view->pluginArray=array(); |
|
281 | + foreach ($pluginList as $plugin) |
|
282 | + { |
|
283 | + $pluginDetails=$Trap->pluginClass->pluginDetails($plugin); |
|
284 | + $pluginDetails->enabled = (in_array($plugin, $enabledPlugins)) ? true : false; |
|
285 | + $pluginDetails->catchAllTraps = ($pluginDetails->catchAllTraps === true )? 'Yes' : 'No'; |
|
286 | + $pluginDetails->processTraps = ($pluginDetails->processTraps === true )? 'Yes' : 'No'; |
|
287 | + $pluginDetails->description = htmlentities($pluginDetails->description); |
|
288 | + $pluginDetails->description = preg_replace('/\n/','<br>',$pluginDetails->description); |
|
289 | + array_push($this->view->pluginArray, $pluginDetails); |
|
290 | + // Get functions for function details |
|
291 | + foreach ($pluginDetails->funcArray as $function) |
|
292 | + { |
|
293 | + array_push($functionList,$function); |
|
294 | + } |
|
295 | + } |
|
296 | 296 | |
297 | - // Function list with details |
|
298 | - $this->view->functionList=array(); |
|
299 | - foreach ($functionList as $function) |
|
300 | - { |
|
301 | - $functionDetail = $Trap->pluginClass->getFunctionDetails($function); |
|
302 | - $functionDetail->params = htmlentities($functionDetail->params); |
|
303 | - $functionDetail->description = htmlentities($functionDetail->description); |
|
304 | - $functionDetail->description = preg_replace('/\n/','<br>',$functionDetail->description); |
|
305 | - array_push($this->view->functionList, $functionDetail); |
|
306 | - } |
|
297 | + // Function list with details |
|
298 | + $this->view->functionList=array(); |
|
299 | + foreach ($functionList as $function) |
|
300 | + { |
|
301 | + $functionDetail = $Trap->pluginClass->getFunctionDetails($function); |
|
302 | + $functionDetail->params = htmlentities($functionDetail->params); |
|
303 | + $functionDetail->description = htmlentities($functionDetail->description); |
|
304 | + $functionDetail->description = preg_replace('/\n/','<br>',$functionDetail->description); |
|
305 | + array_push($this->view->functionList, $functionDetail); |
|
306 | + } |
|
307 | 307 | |
308 | 308 | } |
309 | 309 | |
@@ -318,30 +318,30 @@ discard block |
||
318 | 318 | )->add('services', array( |
319 | 319 | 'label' => $this->translate('Services management'), |
320 | 320 | 'url' => $this->getModuleConfig()->urlPath() . '/status/services') |
321 | - )->add('plugins', array( |
|
322 | - 'label' => $this->translate('Plugins management'), |
|
323 | - 'url' => $this->getModuleConfig()->urlPath() . '/status/plugins') |
|
324 | - ); |
|
321 | + )->add('plugins', array( |
|
322 | + 'label' => $this->translate('Plugins management'), |
|
323 | + 'url' => $this->getModuleConfig()->urlPath() . '/status/plugins') |
|
324 | + ); |
|
325 | 325 | } |
326 | 326 | } |
327 | 327 | |
328 | 328 | // TODO : see if useless |
329 | 329 | class UploadForm extends Form |
330 | 330 | { |
331 | - public function __construct($options = null) |
|
332 | - { |
|
333 | - parent::__construct($options); |
|
334 | - $this->addElements2(); |
|
335 | - } |
|
331 | + public function __construct($options = null) |
|
332 | + { |
|
333 | + parent::__construct($options); |
|
334 | + $this->addElements2(); |
|
335 | + } |
|
336 | 336 | |
337 | - public function addElements2() |
|
338 | - { |
|
339 | - // File Input |
|
340 | - $file = new File('mib-file'); |
|
341 | - $file->setLabel('Mib upload'); |
|
342 | - //->setAttrib('multiple', null); |
|
343 | - $this->addElement($file); |
|
337 | + public function addElements2() |
|
338 | + { |
|
339 | + // File Input |
|
340 | + $file = new File('mib-file'); |
|
341 | + $file->setLabel('Mib upload'); |
|
342 | + //->setAttrib('multiple', null); |
|
343 | + $this->addElement($file); |
|
344 | 344 | $button = new Submit("upload",array('ignore'=>false)); |
345 | 345 | $this->addElement($button);//->setIgnore(false); |
346 | - } |
|
346 | + } |
|
347 | 347 | } |
@@ -22,7 +22,9 @@ discard block |
||
22 | 22 | try |
23 | 23 | { |
24 | 24 | $dbConn = $this->getUIDatabase()->getDbConn(); |
25 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
25 | + if ($dbConn === null) { |
|
26 | + throw new \ErrorException('uncatched db error'); |
|
27 | + } |
|
26 | 28 | $query = $dbConn->select()->from( |
27 | 29 | $this->getModuleConfig()->getTrapTableName(), |
28 | 30 | array('COUNT(*)') |
@@ -41,8 +43,7 @@ discard block |
||
41 | 43 | |
42 | 44 | $this->view->trap_days_delete=$this->getUIDatabase()->getDBConfigValue('db_remove_days'); |
43 | 45 | |
44 | - } |
|
45 | - catch (Exception $e) |
|
46 | + } catch (Exception $e) |
|
46 | 47 | { |
47 | 48 | $this->displayExitError('status',$e->getMessage()); |
48 | 49 | } |
@@ -56,8 +57,7 @@ discard block |
||
56 | 57 | $this->view->currentLogFile=$this->getUIDatabase()->getDBConfigValue('log_file'); |
57 | 58 | $this->view->logLevels=$this->getModuleConfig()->getlogLevels(); |
58 | 59 | $this->view->currentLogLevel=$this->getUIDatabase()->getDBConfigValue('log_level'); |
59 | - } |
|
60 | - catch (Exception $e) |
|
60 | + } catch (Exception $e) |
|
61 | 61 | { |
62 | 62 | $this->displayExitError('status',$e->getMessage()); |
63 | 63 | } |
@@ -107,8 +107,7 @@ discard block |
||
107 | 107 | if ($retVal == 0) |
108 | 108 | { // process is alive |
109 | 109 | $this->_helper->json(array('status'=>'Alive and kicking')); |
110 | - } |
|
111 | - else |
|
110 | + } else |
|
112 | 111 | { // process is dead |
113 | 112 | $this->_helper->json(array('status'=>'tu quoque fili','err'=>'no proc'.$pid)); |
114 | 113 | } |
@@ -124,22 +123,19 @@ discard block |
||
124 | 123 | if (!is_dir($destDir)) |
125 | 124 | { |
126 | 125 | $this->view->uploadStatus="ERROR : no $destDir directory, check module configuration"; |
127 | - } |
|
128 | - else |
|
126 | + } else |
|
129 | 127 | { |
130 | 128 | if (!is_writable($destDir)) |
131 | 129 | { |
132 | 130 | $this->view->uploadStatus="ERROR : $destDir directory is not writable"; |
133 | - } |
|
134 | - else |
|
131 | + } else |
|
135 | 132 | { |
136 | 133 | $destination = $destDir .'/'.$name; //$this->Module()->getBaseDir() . "/mibs/$name"; |
137 | 134 | $sourceTmpNam=filter_var($_FILES['mibfile']['tmp_name'],FILTER_SANITIZE_STRING); |
138 | 135 | if (move_uploaded_file($sourceTmpNam,$destination)===false) |
139 | 136 | { |
140 | 137 | $this->view->uploadStatus="ERROR, file $destination not loaded. Check file and path name or selinux violations"; |
141 | - } |
|
142 | - else |
|
138 | + } else |
|
143 | 139 | { |
144 | 140 | $this->view->uploadStatus="File $name uploaded in $destDir"; |
145 | 141 | } |
@@ -161,13 +157,11 @@ discard block |
||
161 | 157 | { |
162 | 158 | $this->view->snmptranslate='works fine'; |
163 | 159 | $this->view->snmptranslate_state='ok'; |
164 | - } |
|
165 | - else |
|
160 | + } else |
|
166 | 161 | { |
167 | 162 | $this->view->snmptranslate='can execute but no resolution'; |
168 | 163 | } |
169 | - } |
|
170 | - else |
|
164 | + } else |
|
171 | 165 | { |
172 | 166 | $this->view->snmptranslate='cannot execute'; |
173 | 167 | } |
@@ -188,15 +182,13 @@ discard block |
||
188 | 182 | if ($retVal==0) |
189 | 183 | { |
190 | 184 | $dirArray=array_merge($dirArray,explode(':',$sysDirs)); |
191 | - } |
|
192 | - else |
|
185 | + } else |
|
193 | 186 | { |
194 | 187 | $translateOut=exec($this->Config()->get('config', 'snmptranslate') . ' -Dinit_mib .1.3 2>&1 | grep MIBDIRS'); |
195 | 188 | if (preg_match('/MIBDIRS.*\'([^\']+)\'/',$translateOut,$matches)) |
196 | 189 | { |
197 | 190 | $dirArray=array_merge($dirArray,explode(':',$matches[1])); |
198 | - } |
|
199 | - else |
|
191 | + } else |
|
200 | 192 | { |
201 | 193 | array_push($dirArray,'Install net-snmp-config to see system directories'); |
202 | 194 | } |