1
|
|
|
<?php |
2
|
|
|
require_once(dirname(__FILE__).'/settings.php'); |
3
|
|
|
|
4
|
|
|
class Connection{ |
5
|
|
|
public $db = null; |
6
|
|
|
public $dbs = array(); |
7
|
|
|
public $latest_schema = 37; |
8
|
|
|
|
9
|
|
|
public function __construct($dbc = null,$dbname = null,$user = null,$pass = null) { |
10
|
|
|
global $globalDBdriver, $globalNoDB; |
11
|
|
|
if (isset($globalNoDB) && $globalNoDB === TRUE) return true; |
|
|
|
|
12
|
|
|
if ($dbc === null) { |
13
|
|
|
if ($this->db === null && $dbname === null) { |
14
|
|
|
if ($user === null && $pass === null) { |
15
|
|
|
$this->createDBConnection(); |
16
|
|
|
} else { |
17
|
|
|
$this->createDBConnection(null,$user,$pass); |
18
|
|
|
} |
19
|
|
|
} else { |
20
|
|
|
$this->createDBConnection($dbname); |
21
|
|
|
} |
22
|
|
|
} elseif ($dbname === null || $dbname === 'default') { |
23
|
|
|
$this->db = $dbc; |
24
|
|
|
if ($this->connectionExists() === false) { |
25
|
|
|
/* |
26
|
|
|
echo 'Restart Connection !!!'."\n"; |
27
|
|
|
$e = new \Exception; |
28
|
|
|
var_dump($e->getTraceAsString()); |
29
|
|
|
*/ |
30
|
|
|
$this->createDBConnection(); |
31
|
|
|
} |
32
|
|
|
} else { |
33
|
|
|
//$this->connectionExists(); |
34
|
|
|
$this->dbs[$dbname] = $dbc; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function db() { |
39
|
|
|
if ($this->db === null) { |
40
|
|
|
$this->__construct(); |
41
|
|
|
} |
42
|
|
|
if ($this->db === null) { |
43
|
|
|
echo 'Can\'t connect to database. Check configuration and database status.'; |
44
|
|
|
die; |
45
|
|
|
} else { |
46
|
|
|
return $this->db; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Creates the database connection |
52
|
|
|
* |
53
|
|
|
* @return Boolean of the database connection |
54
|
|
|
* |
55
|
|
|
*/ |
56
|
|
|
|
57
|
|
|
public function createDBConnection($DBname = null, $user = null, $pass = null) |
58
|
|
|
{ |
59
|
|
|
global $globalDBdriver, $globalDBhost, $globalDBuser, $globalDBpass, $globalDBname, $globalDebug, $globalDB, $globalDBport, $globalDBTimeOut, $globalDBretry, $globalDBPersistent; |
60
|
|
|
if ($DBname === null) { |
61
|
|
|
if ($user === null && $pass === null) { |
62
|
|
|
$DBname = 'default'; |
63
|
|
|
$globalDBSdriver = $globalDBdriver; |
64
|
|
|
$globalDBShost = $globalDBhost; |
65
|
|
|
$globalDBSname = $globalDBname; |
66
|
|
|
$globalDBSuser = $globalDBuser; |
67
|
|
|
$globalDBSpass = $globalDBpass; |
68
|
|
|
if (!isset($globalDBport) || $globalDBport === NULL || $globalDBport == '') $globalDBSport = 3306; |
69
|
|
|
else $globalDBSport = $globalDBport; |
70
|
|
|
} else { |
71
|
|
|
$DBname = 'default'; |
72
|
|
|
$globalDBSdriver = $globalDBdriver; |
73
|
|
|
$globalDBShost = $globalDBhost; |
74
|
|
|
$globalDBSname = $globalDBname; |
75
|
|
|
$globalDBSuser = $user; |
76
|
|
|
$globalDBSpass = $pass; |
77
|
|
|
if (!isset($globalDBport) || $globalDBport === NULL || $globalDBport == '') $globalDBSport = 3306; |
78
|
|
|
else $globalDBSport = $globalDBport; |
79
|
|
|
} |
80
|
|
|
} else { |
81
|
|
|
$globalDBSdriver = $globalDB[$DBname]['driver']; |
82
|
|
|
$globalDBShost = $globalDB[$DBname]['host']; |
83
|
|
|
$globalDBSname = $globalDB[$DBname]['name']; |
84
|
|
|
$globalDBSuser = $globalDB[$DBname]['user']; |
85
|
|
|
$globalDBSpass = $globalDB[$DBname]['pass']; |
86
|
|
|
if (isset($globalDB[$DBname]['port'])) $globalDBSport = $globalDB[$DBname]['port']; |
87
|
|
|
else $globalDBSport = 3306; |
88
|
|
|
} |
89
|
|
|
// Set number of try to connect to DB |
90
|
|
|
if (!isset($globalDBretry) || $globalDBretry == '' || $globalDBretry === NULL) $globalDBretry = 5; |
91
|
|
|
$i = 0; |
92
|
|
|
while (true) { |
93
|
|
|
try { |
94
|
|
|
if ($globalDBSdriver == 'mysql') { |
95
|
|
|
$this->dbs[$DBname] = new PDO("$globalDBSdriver:host=$globalDBShost;port=$globalDBSport;dbname=$globalDBSname;charset=utf8", $globalDBSuser, $globalDBSpass); |
96
|
|
|
$this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'"); |
97
|
|
|
$this->dbs[$DBname]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
98
|
|
|
$this->dbs[$DBname]->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER); |
99
|
|
|
if (!isset($globalDBTimeOut)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,500); |
100
|
|
|
else $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,$globalDBTimeOut); |
101
|
|
|
if (!isset($globalDBPersistent)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,true); |
102
|
|
|
else $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,$globalDBPersistent); |
103
|
|
|
$this->dbs[$DBname]->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
104
|
|
|
$this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); |
105
|
|
|
// Workaround against "ONLY_FULL_GROUP_BY" mode |
106
|
|
|
// to enable it : $this->dbs[$DBname]->exec('SET sql_mode = "NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY"'); |
107
|
|
|
$this->dbs[$DBname]->exec('SET sql_mode = "NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"'); |
108
|
|
|
// Force usage of UTC |
109
|
|
|
$this->dbs[$DBname]->exec('SET SESSION time_zone = "+00:00"'); |
110
|
|
|
//$this->dbs[$DBname]->exec('SET @@session.time_zone = "+00:00"'); |
111
|
|
|
} else { |
112
|
|
|
$this->dbs[$DBname] = new PDO("$globalDBSdriver:host=$globalDBShost;port=$globalDBSport;dbname=$globalDBSname;options='--client_encoding=utf8'", $globalDBSuser, $globalDBSpass); |
113
|
|
|
//$this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'"); |
114
|
|
|
$this->dbs[$DBname]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
115
|
|
|
$this->dbs[$DBname]->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER); |
116
|
|
|
if (!isset($globalDBTimeOut)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,200); |
117
|
|
|
else $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,$globalDBTimeOut); |
118
|
|
|
if (!isset($globalDBPersistent)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,true); |
119
|
|
|
else $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,$globalDBPersistent); |
120
|
|
|
$this->dbs[$DBname]->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
121
|
|
|
$this->dbs[$DBname]->exec('SET timezone="UTC"'); |
122
|
|
|
} |
123
|
|
|
break; |
124
|
|
|
} catch(PDOException $e) { |
125
|
|
|
$i++; |
126
|
|
|
if (isset($globalDebug) && $globalDebug) echo 'Error connecting to DB: '.$globalDBSname.' - Error: '.$e->getMessage()."\n"; |
127
|
|
|
//exit; |
128
|
|
|
if ($i > $globalDBretry) return false; |
129
|
|
|
//return false; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
if ($DBname === 'default') $this->db = $this->dbs['default']; |
133
|
|
|
return true; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function tableExists($table) |
137
|
|
|
{ |
138
|
|
|
global $globalDBdriver, $globalDBname; |
139
|
|
|
if ($globalDBdriver == 'mysql') { |
140
|
|
|
$query = "SHOW TABLES LIKE '".$table."'"; |
141
|
|
|
} else { |
142
|
|
|
$query = "SELECT * FROM pg_catalog.pg_tables WHERE tablename = '".$table."'"; |
143
|
|
|
} |
144
|
|
|
if ($this->db == NULL) return false; |
145
|
|
|
try { |
146
|
|
|
//$Connection = new Connection(); |
147
|
|
|
$results = $this->db->query($query); |
|
|
|
|
148
|
|
|
} catch(PDOException $e) { |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
if($results->rowCount()>0) { |
152
|
|
|
return true; |
153
|
|
|
} |
154
|
|
|
else return false; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function connectionExists() |
158
|
|
|
{ |
159
|
|
|
global $globalDBdriver, $globalDBCheckConnection; |
160
|
|
|
if (isset($globalDBCheckConnection) && $globalDBCheckConnection === FALSE) return true; |
161
|
|
|
$query = "SELECT 1 + 1"; |
162
|
|
|
if ($this->db === null) return false; |
163
|
|
|
try { |
164
|
|
|
$sum = @$this->db->query($query); |
|
|
|
|
165
|
|
|
if ($sum instanceof \PDOStatement) { |
166
|
|
|
$sum = $sum->fetchColumn(0); |
167
|
|
|
} else $sum = 0; |
168
|
|
|
if (intval($sum) !== 2) { |
169
|
|
|
return false; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
} catch(PDOException $e) { |
173
|
|
|
if($e->getCode() != 'HY000' || !stristr($e->getMessage(), 'server has gone away')) { |
174
|
|
|
throw $e; |
175
|
|
|
} |
176
|
|
|
//echo 'error ! '.$e->getMessage(); |
177
|
|
|
return false; |
178
|
|
|
} |
179
|
|
|
return true; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/* |
183
|
|
|
* Check if index exist |
184
|
|
|
*/ |
185
|
|
|
public function indexExists($table,$index) |
186
|
|
|
{ |
187
|
|
|
global $globalDBdriver, $globalDBname; |
188
|
|
|
if ($globalDBdriver == 'mysql') { |
189
|
|
|
$query = "SELECT COUNT(*) as nb FROM INFORMATION_SCHEMA.STATISTICS WHERE table_schema=DATABASE() AND table_name='".$table."' AND index_name='".$index."'"; |
190
|
|
|
} else { |
191
|
|
|
$query = "SELECT count(*) as nb FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relname = '".$index."' AND n.nspname = '".$table."'"; |
192
|
|
|
} |
193
|
|
|
try { |
194
|
|
|
//$Connection = new Connection(); |
195
|
|
|
$results = $this->db->query($query); |
|
|
|
|
196
|
|
|
} catch(PDOException $e) { |
197
|
|
|
return false; |
198
|
|
|
} |
199
|
|
|
$nb = $results->fetchAll(PDO::FETCH_ASSOC); |
200
|
|
|
if($nb[0]['nb'] > 0) { |
201
|
|
|
return true; |
202
|
|
|
} |
203
|
|
|
else return false; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/* |
207
|
|
|
* Get columns name of a table |
208
|
|
|
* @return Array all column name in table |
209
|
|
|
*/ |
210
|
|
|
public function getColumnName($table) |
211
|
|
|
{ |
212
|
|
|
$query = "SELECT * FROM ".$table." LIMIT 0"; |
213
|
|
|
try { |
214
|
|
|
$results = $this->db->query($query); |
|
|
|
|
215
|
|
|
} catch(PDOException $e) { |
216
|
|
|
return "error : ".$e->getMessage()."\n"; |
217
|
|
|
} |
218
|
|
|
$columns = array(); |
219
|
|
|
$colcnt = $results->columnCount(); |
220
|
|
|
for ($i = 0; $i < $colcnt; $i++) { |
221
|
|
|
$col = $results->getColumnMeta($i); |
222
|
|
|
$columns[] = $col['name']; |
223
|
|
|
} |
224
|
|
|
return $columns; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function getColumnType($table,$column) { |
228
|
|
|
$select = $this->db->query('SELECT '.$column.' FROM '.$table); |
|
|
|
|
229
|
|
|
$tomet = $select->getColumnMeta(0); |
230
|
|
|
return $tomet['native_type']; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/* |
234
|
|
|
* Check if a column name exist in a table |
235
|
|
|
* @return Boolean column exist or not |
236
|
|
|
*/ |
237
|
|
|
public function checkColumnName($table,$name) |
238
|
|
|
{ |
239
|
|
|
global $globalDBdriver, $globalDBname; |
240
|
|
|
if ($globalDBdriver == 'mysql') { |
241
|
|
|
$query = "SELECT COUNT(*) as nb FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = :database AND TABLE_NAME = :table AND COLUMN_NAME = :name LIMIT 1"; |
242
|
|
|
} else { |
243
|
|
|
$query = "SELECT COUNT(*) as nb FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_CATALOG = :database AND TABLE_NAME = :table AND COLUMN_NAME = :name LIMIT 1"; |
244
|
|
|
} |
245
|
|
|
try { |
246
|
|
|
$sth = $this->db()->prepare($query); |
|
|
|
|
247
|
|
|
$sth->execute(array(':database' => $globalDBname,':table' => $table,':name' => $name)); |
248
|
|
|
} catch(PDOException $e) { |
249
|
|
|
echo "error : ".$e->getMessage()."\n"; |
250
|
|
|
} |
251
|
|
|
$result = $sth->fetch(PDO::FETCH_ASSOC); |
252
|
|
|
$sth->closeCursor(); |
253
|
|
|
if ($result['nb'] > 0) return true; |
254
|
|
|
else return false; |
255
|
|
|
/* } else { |
256
|
|
|
$query = "SELECT * FROM ".$table." LIMIT 0"; |
257
|
|
|
try { |
258
|
|
|
$results = $this->db->query($query); |
259
|
|
|
} catch(PDOException $e) { |
260
|
|
|
return "error : ".$e->getMessage()."\n"; |
261
|
|
|
} |
262
|
|
|
$colcnt = $results->columnCount(); |
263
|
|
|
for ($i = 0; $i < $colcnt; $i++) { |
264
|
|
|
$col = $results->getColumnMeta($i); |
265
|
|
|
if ($name == $col['name']) return true; |
266
|
|
|
} |
267
|
|
|
return false; |
268
|
|
|
} |
269
|
|
|
*/ |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/* |
273
|
|
|
* Get schema version |
274
|
|
|
* @return integer schema version |
275
|
|
|
*/ |
276
|
|
|
public function check_schema_version() { |
277
|
|
|
$version = 0; |
278
|
|
|
if ($this->tableExists('aircraft')) { |
279
|
|
|
if (!$this->tableExists('config')) { |
280
|
|
|
$version = '1'; |
281
|
|
|
return $version; |
282
|
|
|
} else { |
283
|
|
|
$query = "SELECT value FROM config WHERE name = 'schema_version' LIMIT 1"; |
284
|
|
|
try { |
285
|
|
|
$sth = $this->db->prepare($query); |
286
|
|
|
$sth->execute(); |
287
|
|
|
} catch(PDOException $e) { |
288
|
|
|
return "error : ".$e->getMessage()."\n"; |
289
|
|
|
} |
290
|
|
|
$result = $sth->fetch(PDO::FETCH_ASSOC); |
291
|
|
|
$sth->closeCursor(); |
292
|
|
|
return $result['value']; |
293
|
|
|
} |
294
|
|
|
} else return $version; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/* |
298
|
|
|
* Check if schema version is latest_schema |
299
|
|
|
* @return Boolean if latest version or not |
300
|
|
|
*/ |
301
|
|
|
public function latest() { |
302
|
|
|
global $globalNoDB; |
303
|
|
|
if (isset($globalNoDB) && $globalNoDB === TRUE) return true; |
304
|
|
|
if ($this->check_schema_version() == $this->latest_schema) return true; |
305
|
|
|
else return false; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
} |
309
|
|
|
?> |
310
|
|
|
|