@@ -14,30 +14,30 @@ discard block |
||
14 | 14 | */ |
15 | 15 | class DBException extends Exception |
16 | 16 | { |
17 | - /** @var array $returnArray */ |
|
18 | - private $returnArray; |
|
17 | + /** @var array $returnArray */ |
|
18 | + private $returnArray; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Buil DBException |
|
22 | - * @param array $retarray |
|
23 | - * @param string $message |
|
24 | - * @param int $code |
|
25 | - * @param Exception $previous |
|
26 | - */ |
|
27 | - public function __construct(array $retarray, string $message = null, int $code = 0, Exception $previous = null) |
|
28 | - { |
|
29 | - parent::__construct($message,$code,$previous); |
|
30 | - $this->returnArray = $retarray; |
|
31 | - } |
|
20 | + /** |
|
21 | + * Buil DBException |
|
22 | + * @param array $retarray |
|
23 | + * @param string $message |
|
24 | + * @param int $code |
|
25 | + * @param Exception $previous |
|
26 | + */ |
|
27 | + public function __construct(array $retarray, string $message = null, int $code = 0, Exception $previous = null) |
|
28 | + { |
|
29 | + parent::__construct($message,$code,$previous); |
|
30 | + $this->returnArray = $retarray; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Get exception array |
|
35 | - * @return array |
|
36 | - */ |
|
37 | - public function getArray() |
|
38 | - { |
|
39 | - return $this->returnArray; |
|
40 | - } |
|
33 | + /** |
|
34 | + * Get exception array |
|
35 | + * @return array |
|
36 | + */ |
|
37 | + public function getArray() |
|
38 | + { |
|
39 | + return $this->returnArray; |
|
40 | + } |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | /** |
@@ -52,211 +52,211 @@ discard block |
||
52 | 52 | class UIDatabase |
53 | 53 | { |
54 | 54 | |
55 | - /** @var TrapsController $trapController TrapController 'parent' class */ |
|
56 | - private $trapController; |
|
55 | + /** @var TrapsController $trapController TrapController 'parent' class */ |
|
56 | + private $trapController; |
|
57 | 57 | |
58 | - /** @var Zend_Db_Adapter_Abstract $trapDB Trap Database*/ |
|
59 | - private $trapDB; |
|
58 | + /** @var Zend_Db_Adapter_Abstract $trapDB Trap Database*/ |
|
59 | + private $trapDB; |
|
60 | 60 | |
61 | - /** @var Zend_Db_Adapter_Abstract $trapDB Icinga IDO database*/ |
|
62 | - private $idoDB; |
|
61 | + /** @var Zend_Db_Adapter_Abstract $trapDB Icinga IDO database*/ |
|
62 | + private $idoDB; |
|
63 | 63 | |
64 | - /** @var array $testResult */ |
|
65 | - private $testResult; |
|
64 | + /** @var array $testResult */ |
|
65 | + private $testResult; |
|
66 | 66 | |
67 | - /** |
|
68 | - * |
|
69 | - * @param TrapsController $trapCtrl |
|
70 | - */ |
|
71 | - function __construct(TrapsController $trapCtrl) |
|
72 | - { |
|
73 | - $this->trapController=$trapCtrl; |
|
74 | - } |
|
67 | + /** |
|
68 | + * |
|
69 | + * @param TrapsController $trapCtrl |
|
70 | + */ |
|
71 | + function __construct(TrapsController $trapCtrl) |
|
72 | + { |
|
73 | + $this->trapController=$trapCtrl; |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Test if database version >= min database version |
|
78 | - * |
|
79 | - * @param \Zend_Db_Adapter_Abstract $dbConn |
|
80 | - * @param int $min Minimum version |
|
81 | - * @param bool $test Test mode |
|
82 | - * @param string $DBname Name of DB |
|
83 | - * @return bool true if OK, false if version < min version |
|
84 | - * @throws Exception if error and test = true |
|
85 | - */ |
|
86 | - protected function testDbVersion($dbAdapter,int $min,bool $test, string $DBname) |
|
87 | - { |
|
88 | - try |
|
89 | - { |
|
90 | - $query = $dbAdapter->select() |
|
91 | - ->from($this->trapController->getModuleConfig()->getDbConfigTableName(),'value') |
|
92 | - ->where('name=\'db_version\''); |
|
93 | - $version=$dbAdapter->fetchRow($query); |
|
94 | - if ( ($version == null) || ! property_exists($version,'value') ) |
|
95 | - { |
|
96 | - if ($test === true) |
|
97 | - { |
|
98 | - $this->testResult = array(4,$DBname); |
|
99 | - return false; |
|
100 | - } |
|
101 | - $this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
|
102 | - return false; |
|
103 | - } |
|
104 | - if ($version->value < $min) |
|
105 | - { |
|
106 | - if ($test === true) |
|
107 | - { |
|
108 | - $this->testResult = array(5,$version->value,$min); |
|
109 | - return false; |
|
110 | - } |
|
111 | - $this->trapController->redirectNow('trapdirector/settings?dberror=5'); |
|
112 | - return false; |
|
113 | - } |
|
114 | - } |
|
115 | - catch (Exception $e) |
|
116 | - { |
|
117 | - if ($test === true) |
|
118 | - { |
|
119 | - $this->testResult = array(3,$DBname,$e->getMessage()); |
|
120 | - return false; |
|
121 | - } |
|
122 | - $this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
|
123 | - return false; |
|
124 | - } |
|
125 | - return true; |
|
126 | - } |
|
76 | + /** |
|
77 | + * Test if database version >= min database version |
|
78 | + * |
|
79 | + * @param \Zend_Db_Adapter_Abstract $dbConn |
|
80 | + * @param int $min Minimum version |
|
81 | + * @param bool $test Test mode |
|
82 | + * @param string $DBname Name of DB |
|
83 | + * @return bool true if OK, false if version < min version |
|
84 | + * @throws Exception if error and test = true |
|
85 | + */ |
|
86 | + protected function testDbVersion($dbAdapter,int $min,bool $test, string $DBname) |
|
87 | + { |
|
88 | + try |
|
89 | + { |
|
90 | + $query = $dbAdapter->select() |
|
91 | + ->from($this->trapController->getModuleConfig()->getDbConfigTableName(),'value') |
|
92 | + ->where('name=\'db_version\''); |
|
93 | + $version=$dbAdapter->fetchRow($query); |
|
94 | + if ( ($version == null) || ! property_exists($version,'value') ) |
|
95 | + { |
|
96 | + if ($test === true) |
|
97 | + { |
|
98 | + $this->testResult = array(4,$DBname); |
|
99 | + return false; |
|
100 | + } |
|
101 | + $this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
|
102 | + return false; |
|
103 | + } |
|
104 | + if ($version->value < $min) |
|
105 | + { |
|
106 | + if ($test === true) |
|
107 | + { |
|
108 | + $this->testResult = array(5,$version->value,$min); |
|
109 | + return false; |
|
110 | + } |
|
111 | + $this->trapController->redirectNow('trapdirector/settings?dberror=5'); |
|
112 | + return false; |
|
113 | + } |
|
114 | + } |
|
115 | + catch (Exception $e) |
|
116 | + { |
|
117 | + if ($test === true) |
|
118 | + { |
|
119 | + $this->testResult = array(3,$DBname,$e->getMessage()); |
|
120 | + return false; |
|
121 | + } |
|
122 | + $this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
|
123 | + return false; |
|
124 | + } |
|
125 | + return true; |
|
126 | + } |
|
127 | 127 | |
128 | - /** Get Database connexion |
|
129 | - * @param $DBname string DB name in resource.ini_ge |
|
130 | - * @param $test bool if set to true, returns error code and not database |
|
131 | - * @param $test_version bool if set to flase, does not test database version of trapDB |
|
132 | - * @throws DBException if test = true and error |
|
133 | - * @return Zend_Db_Adapter_Abstract|null : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
134 | - */ |
|
135 | - protected function getDbByName($DBname , $test = false , $test_version = true) |
|
136 | - { |
|
137 | - try |
|
138 | - { |
|
139 | - $dbconn = IcingaDbConnection::fromResourceName($DBname); |
|
140 | - } |
|
141 | - catch (Exception $e) |
|
142 | - { |
|
143 | - if ($test === true) |
|
144 | - { |
|
145 | - throw new DBException(array(2,$DBname)); |
|
146 | - } |
|
147 | - $this->trapController->redirectNow('trapdirector/settings?dberror=2'); |
|
148 | - return null; |
|
149 | - } |
|
128 | + /** Get Database connexion |
|
129 | + * @param $DBname string DB name in resource.ini_ge |
|
130 | + * @param $test bool if set to true, returns error code and not database |
|
131 | + * @param $test_version bool if set to flase, does not test database version of trapDB |
|
132 | + * @throws DBException if test = true and error |
|
133 | + * @return Zend_Db_Adapter_Abstract|null : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
134 | + */ |
|
135 | + protected function getDbByName($DBname , $test = false , $test_version = true) |
|
136 | + { |
|
137 | + try |
|
138 | + { |
|
139 | + $dbconn = IcingaDbConnection::fromResourceName($DBname); |
|
140 | + } |
|
141 | + catch (Exception $e) |
|
142 | + { |
|
143 | + if ($test === true) |
|
144 | + { |
|
145 | + throw new DBException(array(2,$DBname)); |
|
146 | + } |
|
147 | + $this->trapController->redirectNow('trapdirector/settings?dberror=2'); |
|
148 | + return null; |
|
149 | + } |
|
150 | 150 | |
151 | - try |
|
152 | - { |
|
153 | - $dbAdapter=$dbconn->getDbAdapter(); |
|
151 | + try |
|
152 | + { |
|
153 | + $dbAdapter=$dbconn->getDbAdapter(); |
|
154 | 154 | |
155 | - } |
|
156 | - catch (Exception $e) |
|
157 | - { |
|
158 | - if ($test === true) |
|
159 | - { |
|
160 | - throw new DBException(array(3,$DBname,$e->getMessage())); |
|
161 | - } |
|
162 | - $this->trapController->redirectNow('trapdirector/settings?dberror=3'); |
|
163 | - return null; |
|
164 | - } |
|
155 | + } |
|
156 | + catch (Exception $e) |
|
157 | + { |
|
158 | + if ($test === true) |
|
159 | + { |
|
160 | + throw new DBException(array(3,$DBname,$e->getMessage())); |
|
161 | + } |
|
162 | + $this->trapController->redirectNow('trapdirector/settings?dberror=3'); |
|
163 | + return null; |
|
164 | + } |
|
165 | 165 | |
166 | - if ($test_version == true) { |
|
167 | - $testRet=$this->testDbVersion($dbAdapter, $this->trapController->getModuleConfig()->getDbMinVersion(), $test, $DBname); |
|
168 | - if ($testRet !== true) |
|
169 | - { |
|
170 | - throw new DBException($this->testResult); |
|
171 | - } |
|
172 | - } |
|
166 | + if ($test_version == true) { |
|
167 | + $testRet=$this->testDbVersion($dbAdapter, $this->trapController->getModuleConfig()->getDbMinVersion(), $test, $DBname); |
|
168 | + if ($testRet !== true) |
|
169 | + { |
|
170 | + throw new DBException($this->testResult); |
|
171 | + } |
|
172 | + } |
|
173 | 173 | |
174 | - return $dbAdapter; |
|
175 | - } |
|
174 | + return $dbAdapter; |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * Get Trap database |
|
179 | - * @param boolean $test |
|
180 | - * @return Zend_Db_Adapter_Abstract|array|null : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
181 | - */ |
|
182 | - public function getDb($test=false) |
|
183 | - { |
|
184 | - if ($this->trapDB != null && $test = false) return $this->trapDB; |
|
177 | + /** |
|
178 | + * Get Trap database |
|
179 | + * @param boolean $test |
|
180 | + * @return Zend_Db_Adapter_Abstract|array|null : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
181 | + */ |
|
182 | + public function getDb($test=false) |
|
183 | + { |
|
184 | + if ($this->trapDB != null && $test = false) return $this->trapDB; |
|
185 | 185 | |
186 | - $dbresource=$this->trapController->Config()->get('config', 'database'); |
|
186 | + $dbresource=$this->trapController->Config()->get('config', 'database'); |
|
187 | 187 | |
188 | - if ( ! $dbresource ) |
|
189 | - { |
|
190 | - if ($test === true) |
|
191 | - { |
|
192 | - throw new DBException(array(1,'')); |
|
193 | - } |
|
194 | - $this->trapController->redirectNow('trapdirector/settings?dberror=1'); |
|
195 | - return null; |
|
196 | - } |
|
188 | + if ( ! $dbresource ) |
|
189 | + { |
|
190 | + if ($test === true) |
|
191 | + { |
|
192 | + throw new DBException(array(1,'')); |
|
193 | + } |
|
194 | + $this->trapController->redirectNow('trapdirector/settings?dberror=1'); |
|
195 | + return null; |
|
196 | + } |
|
197 | 197 | |
198 | - $this->trapDB = $this->getDbByName($dbresource,$test,true); |
|
198 | + $this->trapDB = $this->getDbByName($dbresource,$test,true); |
|
199 | 199 | |
200 | - return $this->trapDB; |
|
201 | - } |
|
200 | + return $this->trapDB; |
|
201 | + } |
|
202 | 202 | |
203 | - /** |
|
204 | - * Get IDO Database |
|
205 | - * @param boolean $test |
|
206 | - * @return Zend_Db_Adapter_Abstract|array]|NULL if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
207 | - */ |
|
208 | - public function getIdoDb($test=false) |
|
209 | - { |
|
210 | - if ($this->idoDB != null && $test === false) return $this->idoDB; |
|
211 | - // TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
|
212 | - $dbresource=$this->trapController->Config()->get('config', 'IDOdatabase');; |
|
203 | + /** |
|
204 | + * Get IDO Database |
|
205 | + * @param boolean $test |
|
206 | + * @return Zend_Db_Adapter_Abstract|array]|NULL if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
207 | + */ |
|
208 | + public function getIdoDb($test=false) |
|
209 | + { |
|
210 | + if ($this->idoDB != null && $test === false) return $this->idoDB; |
|
211 | + // TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
|
212 | + $dbresource=$this->trapController->Config()->get('config', 'IDOdatabase');; |
|
213 | 213 | |
214 | - if ( ! $dbresource ) |
|
215 | - { |
|
216 | - if ($test === true) |
|
217 | - { |
|
218 | - throw new DBException(array(1,'No database in config.ini')); |
|
219 | - } |
|
220 | - $this->redirectNow('trapdirector/settings?idodberror=1'); |
|
221 | - return null; |
|
222 | - } |
|
214 | + if ( ! $dbresource ) |
|
215 | + { |
|
216 | + if ($test === true) |
|
217 | + { |
|
218 | + throw new DBException(array(1,'No database in config.ini')); |
|
219 | + } |
|
220 | + $this->redirectNow('trapdirector/settings?idodberror=1'); |
|
221 | + return null; |
|
222 | + } |
|
223 | 223 | |
224 | - try |
|
225 | - { |
|
226 | - $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
227 | - } |
|
228 | - catch (Exception $e) |
|
229 | - { |
|
230 | - if ($test === true) |
|
231 | - { |
|
232 | - throw new DBException( array(2,"Database $dbresource does not exists in IcingaWeb2") ); |
|
233 | - } |
|
234 | - $this->redirectNow('trapdirector/settings?idodberror=2'); |
|
235 | - return null; |
|
236 | - } |
|
224 | + try |
|
225 | + { |
|
226 | + $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
227 | + } |
|
228 | + catch (Exception $e) |
|
229 | + { |
|
230 | + if ($test === true) |
|
231 | + { |
|
232 | + throw new DBException( array(2,"Database $dbresource does not exists in IcingaWeb2") ); |
|
233 | + } |
|
234 | + $this->redirectNow('trapdirector/settings?idodberror=2'); |
|
235 | + return null; |
|
236 | + } |
|
237 | 237 | |
238 | - if ($test === false) |
|
239 | - { |
|
240 | - $this->idoDB = $dbconn->getDbAdapter(); |
|
241 | - return $this->idoDB; |
|
242 | - } |
|
238 | + if ($test === false) |
|
239 | + { |
|
240 | + $this->idoDB = $dbconn->getDbAdapter(); |
|
241 | + return $this->idoDB; |
|
242 | + } |
|
243 | 243 | |
244 | - try |
|
245 | - { |
|
246 | - $query = $dbconn->select() |
|
247 | - ->from('icinga_dbversion',array('version')); |
|
248 | - $version=$dbconn->fetchRow($query); |
|
249 | - if ( ($version == null) || ! property_exists($version,'version') ) |
|
250 | - { |
|
251 | - return array(4,"$dbresource does not look like an IDO database"); |
|
252 | - } |
|
253 | - } |
|
254 | - catch (Exception $e) |
|
255 | - { |
|
256 | - throw new DBException( array(3,"Error connecting to $dbresource : " . $e->getMessage())); |
|
257 | - } |
|
244 | + try |
|
245 | + { |
|
246 | + $query = $dbconn->select() |
|
247 | + ->from('icinga_dbversion',array('version')); |
|
248 | + $version=$dbconn->fetchRow($query); |
|
249 | + if ( ($version == null) || ! property_exists($version,'version') ) |
|
250 | + { |
|
251 | + return array(4,"$dbresource does not look like an IDO database"); |
|
252 | + } |
|
253 | + } |
|
254 | + catch (Exception $e) |
|
255 | + { |
|
256 | + throw new DBException( array(3,"Error connecting to $dbresource : " . $e->getMessage())); |
|
257 | + } |
|
258 | 258 | |
259 | - return $this->idoDB; |
|
260 | - } |
|
259 | + return $this->idoDB; |
|
260 | + } |
|
261 | 261 | |
262 | 262 | } |
263 | 263 | \ No newline at end of file |