|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Icinga\Module\Trapdirector\TrapsController; |
|
4
|
|
|
use Icinga\Data\Db\DbConnection as IcingaDbConnection; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Exception for Database test |
|
8
|
|
|
* |
|
9
|
|
|
* @license GPL |
|
10
|
|
|
* @author Patrick Proy |
|
11
|
|
|
* @package trapdirector |
|
12
|
|
|
* @subpackage UI |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
class DBException extends Exception |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var array $returnArray */ |
|
18
|
|
|
private $returnArray; |
|
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
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get exception array |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getArray() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->returnArray; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Database functions for user interface |
|
45
|
|
|
* |
|
46
|
|
|
* @license GPL |
|
47
|
|
|
* @author Patrick Proy |
|
48
|
|
|
* @package trapdirector |
|
49
|
|
|
* @subpackage UI |
|
50
|
|
|
* |
|
51
|
|
|
*/ |
|
52
|
|
|
class UIDatabase |
|
53
|
|
|
{ |
|
54
|
|
|
|
|
55
|
|
|
/** @var TrapsController $trapController TrapController 'parent' class */ |
|
56
|
|
|
private $trapController; |
|
57
|
|
|
|
|
58
|
|
|
/** @var Zend_Db_Adapter_Abstract $trapDB Trap Database*/ |
|
59
|
|
|
private $trapDB; |
|
60
|
|
|
|
|
61
|
|
|
/** @var Zend_Db_Adapter_Abstract $trapDB Icinga IDO database*/ |
|
62
|
|
|
private $idoDB; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* |
|
66
|
|
|
* @param TrapsController $trapCtrl |
|
67
|
|
|
*/ |
|
68
|
|
|
function __construct(TrapsController $trapCtrl) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->trapController=$trapCtrl; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Test if database version >= min database version |
|
75
|
|
|
* |
|
76
|
|
|
* @param \Zend_Db_Adapter_Abstract $dbConn |
|
77
|
|
|
* @param int $min Minimum version |
|
78
|
|
|
* @param bool $test Test mode |
|
79
|
|
|
* @param string $DBname Name of DB |
|
80
|
|
|
* @throws DBException if test = true |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function testDbVersion($dbAdapter,int $min,bool $test, string $DBname) |
|
83
|
|
|
{ |
|
84
|
|
|
try |
|
85
|
|
|
{ |
|
86
|
|
|
$query = $dbAdapter->select() |
|
87
|
|
|
->from($this->trapController->getModuleConfig()->getDbConfigTableName(),'value') |
|
88
|
|
|
->where('name=\'db_version\''); |
|
89
|
|
|
$version=$dbAdapter->fetchRow($query); |
|
90
|
|
|
if ( ($version == null) || ! property_exists($version,'value') ) |
|
91
|
|
|
{ |
|
92
|
|
|
if ($test === true) |
|
93
|
|
|
{ |
|
94
|
|
|
throw new DBException(array(4,$DBname)); |
|
95
|
|
|
} |
|
96
|
|
|
$this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
|
97
|
|
|
exit(0); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
if ($version->value < $min) |
|
100
|
|
|
{ |
|
101
|
|
|
if ($test === true) |
|
102
|
|
|
{ |
|
103
|
|
|
throw new DBException(array(5,$version->value,$min)); |
|
104
|
|
|
} |
|
105
|
|
|
$this->trapController->redirectNow('trapdirector/settings?dberror=5'); |
|
106
|
|
|
exit(0); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
catch (Exception $e) |
|
110
|
|
|
{ |
|
111
|
|
|
if ($test === true) |
|
112
|
|
|
{ |
|
113
|
|
|
throw new DBException(array(3,$DBname,$e->getMessage())); |
|
114
|
|
|
} |
|
115
|
|
|
$this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
|
116
|
|
|
} |
|
117
|
|
|
return; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** Get Database connexion |
|
121
|
|
|
* @param $DBname string DB name in resource.ini_ge |
|
122
|
|
|
* @param $test bool if set to true, returns error code and not database |
|
123
|
|
|
* @param $test_version bool if set to flase, does not test database version of trapDB |
|
124
|
|
|
* @return Zend_Db_Adapter_Abstract|array|null : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function getDbByName($DBname,$test=false,$test_version=true) |
|
127
|
|
|
{ |
|
128
|
|
|
try |
|
129
|
|
|
{ |
|
130
|
|
|
$dbconn = IcingaDbConnection::fromResourceName($DBname); |
|
131
|
|
|
} |
|
132
|
|
|
catch (Exception $e) |
|
133
|
|
|
{ |
|
134
|
|
|
if ($test) return array(2,$DBname); |
|
135
|
|
|
$this->trapController->redirectNow('trapdirector/settings?dberror=2'); |
|
136
|
|
|
return null; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
try |
|
140
|
|
|
{ |
|
141
|
|
|
$dbAdapter=$dbconn->getDbAdapter(); |
|
142
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
catch (Exception $e) |
|
145
|
|
|
{ |
|
146
|
|
|
if ($test) return array(3,$DBname,$e->getMessage()); |
|
147
|
|
|
$this->trapController->redirectNow('trapdirector/settings?dberror=3'); |
|
148
|
|
|
return null; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if ($test_version == true) { |
|
152
|
|
|
$testRet=$this->testDbVersion($dbAdapter, $this->trapController->getModuleConfig()->getDbMinVersion(), $test, $DBname); |
|
|
|
|
|
|
153
|
|
|
if ($testRet != null) |
|
154
|
|
|
{ |
|
155
|
|
|
return $testRet; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
if ($test) return array(0,''); |
|
159
|
|
|
return $dbAdapter; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get Trap database |
|
164
|
|
|
* @param boolean $test |
|
165
|
|
|
* @return Zend_Db_Adapter_Abstract|array|null : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getDb($test=false) |
|
168
|
|
|
{ |
|
169
|
|
|
if ($this->trapDB != null && $test = false) return $this->trapDB; |
|
170
|
|
|
|
|
171
|
|
|
$dbresource=$this->trapController->Config()->get('config', 'database'); |
|
172
|
|
|
|
|
173
|
|
|
if ( ! $dbresource ) |
|
174
|
|
|
{ |
|
175
|
|
|
if ($test) return array(1,''); |
|
176
|
|
|
$this->trapController->redirectNow('trapdirector/settings?dberror=1'); |
|
177
|
|
|
return null; |
|
178
|
|
|
} |
|
179
|
|
|
$retDB=$this->getDbByName($dbresource,$test,true); |
|
180
|
|
|
|
|
181
|
|
|
if ($test === true) return $retDB; |
|
182
|
|
|
|
|
183
|
|
|
$this->trapDB=$retDB; |
|
184
|
|
|
return $this->trapDB; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Get IDO Database |
|
189
|
|
|
* @param boolean $test |
|
190
|
|
|
* @return Zend_Db_Adapter_Abstract|array]|NULL if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
191
|
|
|
*/ |
|
192
|
|
|
public function getIdoDb($test=false) |
|
193
|
|
|
{ |
|
194
|
|
|
if ($this->idoDB != null && $test = false) return $this->idoDB; |
|
195
|
|
|
// TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
|
196
|
|
|
$dbresource=$this->trapController->Config()->get('config', 'IDOdatabase');; |
|
197
|
|
|
|
|
198
|
|
|
if ( ! $dbresource ) |
|
199
|
|
|
{ |
|
200
|
|
|
if ($test) return array(1,'No database in config.ini'); |
|
201
|
|
|
$this->redirectNow('trapdirector/settings?idodberror=1'); |
|
|
|
|
|
|
202
|
|
|
return null; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
try |
|
206
|
|
|
{ |
|
207
|
|
|
$dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
208
|
|
|
} |
|
209
|
|
|
catch (Exception $e) |
|
210
|
|
|
{ |
|
211
|
|
|
if ($test) return array(2,"Database $dbresource does not exists in IcingaWeb2"); |
|
212
|
|
|
$this->redirectNow('trapdirector/settings?idodberror=2'); |
|
213
|
|
|
return null; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
if ($test === false) |
|
217
|
|
|
{ |
|
218
|
|
|
$this->idoDB = $dbconn->getDbAdapter(); |
|
219
|
|
|
return $this->idoDB; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
try |
|
223
|
|
|
{ |
|
224
|
|
|
$query = $dbconn->select() |
|
225
|
|
|
->from('icinga_dbversion',array('version')); |
|
226
|
|
|
$version=$dbconn->fetchRow($query); |
|
227
|
|
|
if ( ($version == null) || ! property_exists($version,'version') ) |
|
228
|
|
|
{ |
|
229
|
|
|
return array(4,"$dbresource does not look like an IDO database"); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
catch (Exception $e) |
|
233
|
|
|
{ |
|
234
|
|
|
return array(3,"Error connecting to $dbresource : " . $e->getMessage()); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
return array(0,''); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
} |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.