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