1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Icinga\Module\Trapdirector\TrapsActions; |
4
|
|
|
|
5
|
|
|
use Icinga\Module\Trapdirector\TrapsController; |
6
|
|
|
use Exception; |
7
|
|
|
use Icinga\Data\Db\DbConnection as IcingaDbConnection; |
8
|
|
|
use Icinga\Data\Selectable; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Exception for Database test |
13
|
|
|
* |
14
|
|
|
* @license GPL |
15
|
|
|
* @author Patrick Proy |
16
|
|
|
* @package trapdirector |
17
|
|
|
* @subpackage UI |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
class DBException extends Exception |
21
|
|
|
{ |
22
|
|
|
/** @var array $returnArray */ |
23
|
|
|
private $returnArray; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Buil DBException |
27
|
|
|
* @param array $retarray |
28
|
|
|
* @param string $message |
29
|
|
|
* @param int $code |
30
|
|
|
* @param Exception $previous |
31
|
|
|
*/ |
32
|
|
|
public function __construct(array $retarray, string $message = null, int $code = 0, Exception $previous = null) |
33
|
|
|
{ |
34
|
|
|
parent::__construct($message,$code,$previous); |
35
|
|
|
$this->returnArray = $retarray; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get exception array |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function getArray() |
43
|
|
|
{ |
44
|
|
|
return $this->returnArray; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Database functions for user interface |
50
|
|
|
* |
51
|
|
|
* @license GPL |
52
|
|
|
* @author Patrick Proy |
53
|
|
|
* @package trapdirector |
54
|
|
|
* @subpackage UI |
55
|
|
|
* |
56
|
|
|
*/ |
57
|
|
|
class UIDatabase //extends TrapDBQuery |
58
|
|
|
{ |
59
|
|
|
use TrapDBQuery,IdoDBQuery; |
|
|
|
|
60
|
|
|
|
61
|
|
|
/** @var TrapsController $trapController TrapController 'parent' class */ |
62
|
|
|
protected $trapController=null; |
63
|
|
|
|
64
|
|
|
/** @var Selectable $trapDB Trap Database*/ |
65
|
|
|
protected $trapDB=null; |
66
|
|
|
|
67
|
|
|
/** @var Selectable $trapDB Icinga IDO database*/ |
68
|
|
|
protected $idoDB=null; |
69
|
|
|
|
70
|
|
|
/** @var array $testResult */ |
71
|
|
|
protected $testResult; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* |
75
|
|
|
* @param TrapsController $trapCtrl |
76
|
|
|
*/ |
77
|
|
|
function __construct(TrapsController $trapCtrl) |
78
|
|
|
{ |
79
|
|
|
$this->trapController=$trapCtrl; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get TrapsController instance |
84
|
|
|
* @return TrapsController |
85
|
|
|
*/ |
86
|
|
|
protected function getTrapCtrl() |
87
|
|
|
{ |
88
|
|
|
return $this->trapController; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Test if database version >= min database version |
93
|
|
|
* |
94
|
|
|
* @param \Zend_Db_Adapter_Abstract $dbConn |
95
|
|
|
* @param int $min Minimum version |
96
|
|
|
* @param bool $test Test mode |
97
|
|
|
* @param string $DBname Name of DB |
98
|
|
|
* @return bool true if OK, false if version < min version |
99
|
|
|
* @throws Exception if error and test = true |
100
|
|
|
*/ |
101
|
|
|
protected function testDbVersion($dbAdapter,int $min,bool $test, string $DBname) |
102
|
|
|
{ |
103
|
|
|
try |
104
|
|
|
{ |
105
|
|
|
$query = $dbAdapter->select() |
106
|
|
|
->from($this->trapController->getModuleConfig()->getDbConfigTableName(),'value') |
107
|
|
|
->where('name=\'db_version\''); |
108
|
|
|
$version=$dbAdapter->fetchRow($query); |
109
|
|
|
if ( ($version == null) || ! property_exists($version,'value') ) |
110
|
|
|
{ |
111
|
|
|
if ($test === true) |
112
|
|
|
{ |
113
|
|
|
$this->testResult = array(4,$DBname); |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
$this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
if ($version->value < $min) |
120
|
|
|
{ |
121
|
|
|
if ($test === true) |
122
|
|
|
{ |
123
|
|
|
$this->testResult = array(5,$version->value,$min); |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
$this->trapController->redirectNow('trapdirector/settings?dberror=5'); |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
catch (Exception $e) |
131
|
|
|
{ |
132
|
|
|
if ($test === true) |
133
|
|
|
{ |
134
|
|
|
$this->testResult = array(3,$DBname,$e->getMessage()); |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
$this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
138
|
|
|
return false; |
139
|
|
|
} |
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** Get Database connexion |
144
|
|
|
* @param $DBname string DB name in resource.ini_ge |
145
|
|
|
* @param $test bool if set to true, returns error code and not database |
146
|
|
|
* @param $test_version bool if set to flase, does not test database version of trapDB |
147
|
|
|
* @throws DBException if test = true and error |
148
|
|
|
* @return Selectable|null : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
149
|
|
|
*/ |
150
|
|
|
protected function getDbByName($DBname , $test = false , $test_version = true) |
151
|
|
|
{ |
152
|
|
|
try |
153
|
|
|
{ |
154
|
|
|
$dbconn = IcingaDbConnection::fromResourceName($DBname); |
155
|
|
|
} |
156
|
|
|
catch (Exception $e) |
157
|
|
|
{ |
158
|
|
|
if ($test === true) |
159
|
|
|
{ |
160
|
|
|
throw new DBException(array(2,$DBname)); |
161
|
|
|
} |
162
|
|
|
$this->trapController->redirectNow('trapdirector/settings?dberror=2'); |
163
|
|
|
return null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
try |
167
|
|
|
{ |
168
|
|
|
$dbAdapter=$dbconn->getDbAdapter(); |
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
catch (Exception $e) |
172
|
|
|
{ |
173
|
|
|
if ($test === true) |
174
|
|
|
{ |
175
|
|
|
throw new DBException(array(3,$DBname,$e->getMessage())); |
176
|
|
|
} |
177
|
|
|
$this->trapController->redirectNow('trapdirector/settings?dberror=3'); |
178
|
|
|
return null; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ($test_version == true) { |
182
|
|
|
$testRet=$this->testDbVersion($dbAdapter, $this->trapController->getModuleConfig()->getDbMinVersion(), $test, $DBname); |
183
|
|
|
if ($testRet !== true) |
184
|
|
|
{ |
185
|
|
|
throw new DBException($this->testResult); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $dbconn; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get Trap database |
194
|
|
|
* @return Selectable|null : returns DB connexion or null on error. |
195
|
|
|
*/ |
196
|
|
|
public function getDb() |
197
|
|
|
{ |
198
|
|
|
if ( $this->trapDB != null ) return $this->trapDB; |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
$dbresource=$this->trapController->Config()->get('config', 'database'); |
202
|
|
|
|
203
|
|
|
if ( ! $dbresource ) |
204
|
|
|
{ |
205
|
|
|
$this->trapController->redirectNow('trapdirector/settings?dberror=1'); |
206
|
|
|
return null; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
try { |
210
|
|
|
$this->trapDB = $this->getDbByName($dbresource,false,true); |
211
|
|
|
} catch (DBException $e) { |
212
|
|
|
return null; // Should not happen as test = false |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
//$this->trapDB->getConnection(); |
216
|
|
|
|
217
|
|
|
return $this->trapDB; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Get Zend adapter of DB. |
222
|
|
|
* @return \Zend_Db_Adapter_Abstract|null |
223
|
|
|
*/ |
224
|
|
|
public function getDbConn() |
225
|
|
|
{ |
226
|
|
|
if ($this->getDb() == null) return null; |
227
|
|
|
return $this->getDb()->getConnection(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Test Trap database |
232
|
|
|
* @param boolean $test |
233
|
|
|
* @throws DBException on error. |
234
|
|
|
* @return \Zend_Db_Adapter_Abstract|array|null : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
235
|
|
|
*/ |
236
|
|
|
public function testGetDb() |
237
|
|
|
{ |
238
|
|
|
$dbresource=$this->trapController->Config()->get('config', 'database'); |
239
|
|
|
|
240
|
|
|
if ( ! $dbresource ) |
241
|
|
|
{ |
242
|
|
|
throw new DBException(array(1,'')); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$this->trapDB = $this->getDbByName($dbresource,true,true); |
246
|
|
|
return; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Get IDO Database |
252
|
|
|
* @return \Zend_Db_Adapter_Abstract|NULL returns DB connexion or null on error. |
253
|
|
|
*/ |
254
|
|
|
public function getIdoDb() |
255
|
|
|
{ |
256
|
|
|
if ( $this->idoDB != null ) return $this->idoDB; |
257
|
|
|
// TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
258
|
|
|
$dbresource=$this->trapController->Config()->get('config', 'IDOdatabase');; |
259
|
|
|
|
260
|
|
|
if ( ! $dbresource ) |
261
|
|
|
{ |
262
|
|
|
$this->trapController->redirectNow('trapdirector/settings?idodberror=1'); |
263
|
|
|
return null; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
try |
267
|
|
|
{ |
268
|
|
|
$dbconn = IcingaDbConnection::fromResourceName($dbresource); |
269
|
|
|
} |
270
|
|
|
catch (Exception $e) |
271
|
|
|
{ |
272
|
|
|
$this->trapController->redirectNow('trapdirector/settings?idodberror=2'); |
273
|
|
|
return null; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$this->idoDB = $dbconn; |
277
|
|
|
return $this->idoDB; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Get Zend adapter of DB. |
283
|
|
|
* @return \Zend_Db_Adapter_Abstract|null |
284
|
|
|
*/ |
285
|
|
|
public function getIdoDbConn() |
286
|
|
|
{ |
287
|
|
|
if ($this->getIdoDb() == null) return null; |
288
|
|
|
return $this->getIdoDb()->getConnection(); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Get IDO Database |
293
|
|
|
* @param boolean $test |
294
|
|
|
* @throws DBException on error |
295
|
|
|
*/ |
296
|
|
|
public function testGetIdoDb() |
297
|
|
|
{ |
298
|
|
|
// TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
299
|
|
|
$dbresource=$this->trapController->Config()->get('config', 'IDOdatabase');; |
300
|
|
|
|
301
|
|
|
if ( ! $dbresource ) |
302
|
|
|
{ |
303
|
|
|
throw new DBException(array(1,'No database in config.ini')); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
try |
307
|
|
|
{ |
308
|
|
|
$dbconn = IcingaDbConnection::fromResourceName($dbresource); |
309
|
|
|
} |
310
|
|
|
catch (Exception $e) |
311
|
|
|
{ |
312
|
|
|
throw new DBException( array(2,"Database $dbresource does not exists in IcingaWeb2") ); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
try |
316
|
|
|
{ |
317
|
|
|
$query = $dbconn->select() |
318
|
|
|
->from('icinga_dbversion',array('version')); |
319
|
|
|
$version=$dbconn->fetchRow($query); |
320
|
|
|
if ( ($version == null) || ! property_exists($version,'version') ) |
321
|
|
|
{ |
322
|
|
|
throw new DBException( array(4,"$dbresource does not look like an IDO database")); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
catch (Exception $e) |
326
|
|
|
{ |
327
|
|
|
throw new DBException( array(3,"Error connecting to $dbresource : " . $e->getMessage())); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
return; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
} |