@@ -1,19 +1,19 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | require_once(dirname(__FILE__).'/settings.php'); |
| 3 | 3 | |
| 4 | -class Connection{ |
|
| 4 | +class Connection { |
|
| 5 | 5 | public $db = null; |
| 6 | 6 | public $dbs = array(); |
| 7 | 7 | public $latest_schema = 34; |
| 8 | 8 | |
| 9 | - public function __construct($dbc = null,$dbname = null,$user = null,$pass = null) { |
|
| 9 | + public function __construct($dbc = null, $dbname = null, $user = null, $pass = null) { |
|
| 10 | 10 | global $globalDBdriver; |
| 11 | 11 | if ($dbc === null) { |
| 12 | 12 | if ($this->db === null && $dbname === null) { |
| 13 | 13 | if ($user === null && $pass === null) { |
| 14 | 14 | $this->createDBConnection(); |
| 15 | 15 | } else { |
| 16 | - $this->createDBConnection(null,$user,$pass); |
|
| 16 | + $this->createDBConnection(null, $user, $pass); |
|
| 17 | 17 | } |
| 18 | 18 | } else { |
| 19 | 19 | $this->createDBConnection($dbname); |
@@ -91,14 +91,14 @@ discard block |
||
| 91 | 91 | while (true) { |
| 92 | 92 | try { |
| 93 | 93 | if ($globalDBSdriver == 'mysql') { |
| 94 | - $this->dbs[$DBname] = new PDO("$globalDBSdriver:host=$globalDBShost;port=$globalDBSport;dbname=$globalDBSname;charset=utf8", $globalDBSuser, $globalDBSpass); |
|
| 94 | + $this->dbs[$DBname] = new PDO("$globalDBSdriver:host=$globalDBShost;port=$globalDBSport;dbname=$globalDBSname;charset=utf8", $globalDBSuser, $globalDBSpass); |
|
| 95 | 95 | $this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'"); |
| 96 | 96 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
| 97 | - $this->dbs[$DBname]->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER); |
|
| 98 | - if (!isset($globalDBTimeOut)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,500); |
|
| 99 | - else $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,$globalDBTimeOut); |
|
| 100 | - if (!isset($globalDBPersistent)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,true); |
|
| 101 | - else $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,$globalDBPersistent); |
|
| 97 | + $this->dbs[$DBname]->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); |
|
| 98 | + if (!isset($globalDBTimeOut)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT, 500); |
|
| 99 | + else $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT, $globalDBTimeOut); |
|
| 100 | + if (!isset($globalDBPersistent)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT, true); |
|
| 101 | + else $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT, $globalDBPersistent); |
|
| 102 | 102 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
| 103 | 103 | $this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); |
| 104 | 104 | // Workaround against "ONLY_FULL_GROUP_BY" mode |
@@ -108,19 +108,19 @@ discard block |
||
| 108 | 108 | $this->dbs[$DBname]->exec('SET SESSION time_zone = "+00:00"'); |
| 109 | 109 | //$this->dbs[$DBname]->exec('SET @@session.time_zone = "+00:00"'); |
| 110 | 110 | } else { |
| 111 | - $this->dbs[$DBname] = new PDO("$globalDBSdriver:host=$globalDBShost;port=$globalDBSport;dbname=$globalDBSname;options='--client_encoding=utf8'", $globalDBSuser, $globalDBSpass); |
|
| 111 | + $this->dbs[$DBname] = new PDO("$globalDBSdriver:host=$globalDBShost;port=$globalDBSport;dbname=$globalDBSname;options='--client_encoding=utf8'", $globalDBSuser, $globalDBSpass); |
|
| 112 | 112 | //$this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'"); |
| 113 | 113 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
| 114 | - $this->dbs[$DBname]->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER); |
|
| 115 | - if (!isset($globalDBTimeOut)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,200); |
|
| 116 | - else $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,$globalDBTimeOut); |
|
| 117 | - if (!isset($globalDBPersistent)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,true); |
|
| 118 | - else $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,$globalDBPersistent); |
|
| 114 | + $this->dbs[$DBname]->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); |
|
| 115 | + if (!isset($globalDBTimeOut)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT, 200); |
|
| 116 | + else $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT, $globalDBTimeOut); |
|
| 117 | + if (!isset($globalDBPersistent)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT, true); |
|
| 118 | + else $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT, $globalDBPersistent); |
|
| 119 | 119 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
| 120 | 120 | $this->dbs[$DBname]->exec('SET timezone="UTC"'); |
| 121 | 121 | } |
| 122 | 122 | break; |
| 123 | - } catch(PDOException $e) { |
|
| 123 | + } catch (PDOException $e) { |
|
| 124 | 124 | $i++; |
| 125 | 125 | if (isset($globalDebug) && $globalDebug) echo 'Error connecting to DB: '.$globalDBSname.' - Error: '.$e->getMessage()."\n"; |
| 126 | 126 | //exit; |
@@ -144,10 +144,10 @@ discard block |
||
| 144 | 144 | try { |
| 145 | 145 | //$Connection = new Connection(); |
| 146 | 146 | $results = $this->db->query($query); |
| 147 | - } catch(PDOException $e) { |
|
| 147 | + } catch (PDOException $e) { |
|
| 148 | 148 | return false; |
| 149 | 149 | } |
| 150 | - if($results->rowCount()>0) { |
|
| 150 | + if ($results->rowCount() > 0) { |
|
| 151 | 151 | return true; |
| 152 | 152 | } |
| 153 | 153 | else return false; |
@@ -168,8 +168,8 @@ discard block |
||
| 168 | 168 | return false; |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | - } catch(PDOException $e) { |
|
| 172 | - if($e->getCode() != 'HY000' || !stristr($e->getMessage(), 'server has gone away')) { |
|
| 171 | + } catch (PDOException $e) { |
|
| 172 | + if ($e->getCode() != 'HY000' || !stristr($e->getMessage(), 'server has gone away')) { |
|
| 173 | 173 | throw $e; |
| 174 | 174 | } |
| 175 | 175 | //echo 'error ! '.$e->getMessage(); |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | /* |
| 182 | 182 | * Check if index exist |
| 183 | 183 | */ |
| 184 | - public function indexExists($table,$index) |
|
| 184 | + public function indexExists($table, $index) |
|
| 185 | 185 | { |
| 186 | 186 | global $globalDBdriver, $globalDBname; |
| 187 | 187 | if ($globalDBdriver == 'mysql') { |
@@ -192,11 +192,11 @@ discard block |
||
| 192 | 192 | try { |
| 193 | 193 | //$Connection = new Connection(); |
| 194 | 194 | $results = $this->db->query($query); |
| 195 | - } catch(PDOException $e) { |
|
| 195 | + } catch (PDOException $e) { |
|
| 196 | 196 | return false; |
| 197 | 197 | } |
| 198 | 198 | $nb = $results->fetchAll(PDO::FETCH_ASSOC); |
| 199 | - if($nb[0]['nb'] > 0) { |
|
| 199 | + if ($nb[0]['nb'] > 0) { |
|
| 200 | 200 | return true; |
| 201 | 201 | } |
| 202 | 202 | else return false; |
@@ -211,7 +211,7 @@ discard block |
||
| 211 | 211 | $query = "SELECT * FROM ".$table." LIMIT 0"; |
| 212 | 212 | try { |
| 213 | 213 | $results = $this->db->query($query); |
| 214 | - } catch(PDOException $e) { |
|
| 214 | + } catch (PDOException $e) { |
|
| 215 | 215 | return "error : ".$e->getMessage()."\n"; |
| 216 | 216 | } |
| 217 | 217 | $columns = array(); |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | * Check if a column name exist in a table |
| 228 | 228 | * @return Boolean column exist or not |
| 229 | 229 | */ |
| 230 | - public function checkColumnName($table,$name) |
|
| 230 | + public function checkColumnName($table, $name) |
|
| 231 | 231 | { |
| 232 | 232 | global $globalDBdriver, $globalDBname; |
| 233 | 233 | if ($globalDBdriver == 'mysql') { |
@@ -237,8 +237,8 @@ discard block |
||
| 237 | 237 | } |
| 238 | 238 | try { |
| 239 | 239 | $sth = $this->db()->prepare($query); |
| 240 | - $sth->execute(array(':database' => $globalDBname,':table' => $table,':name' => $name)); |
|
| 241 | - } catch(PDOException $e) { |
|
| 240 | + $sth->execute(array(':database' => $globalDBname, ':table' => $table, ':name' => $name)); |
|
| 241 | + } catch (PDOException $e) { |
|
| 242 | 242 | echo "error : ".$e->getMessage()."\n"; |
| 243 | 243 | } |
| 244 | 244 | $result = $sth->fetch(PDO::FETCH_ASSOC); |
@@ -277,7 +277,7 @@ discard block |
||
| 277 | 277 | try { |
| 278 | 278 | $sth = $this->db->prepare($query); |
| 279 | 279 | $sth->execute(); |
| 280 | - } catch(PDOException $e) { |
|
| 280 | + } catch (PDOException $e) { |
|
| 281 | 281 | return "error : ".$e->getMessage()."\n"; |
| 282 | 282 | } |
| 283 | 283 | $result = $sth->fetch(PDO::FETCH_ASSOC); |
@@ -64,8 +64,11 @@ discard block |
||
| 64 | 64 | $globalDBSname = $globalDBname; |
| 65 | 65 | $globalDBSuser = $globalDBuser; |
| 66 | 66 | $globalDBSpass = $globalDBpass; |
| 67 | - if (!isset($globalDBport) || $globalDBport === NULL || $globalDBport == '') $globalDBSport = 3306; |
|
| 68 | - else $globalDBSport = $globalDBport; |
|
| 67 | + if (!isset($globalDBport) || $globalDBport === NULL || $globalDBport == '') { |
|
| 68 | + $globalDBSport = 3306; |
|
| 69 | + } else { |
|
| 70 | + $globalDBSport = $globalDBport; |
|
| 71 | + } |
|
| 69 | 72 | } else { |
| 70 | 73 | $DBname = 'default'; |
| 71 | 74 | $globalDBSdriver = $globalDBdriver; |
@@ -73,8 +76,11 @@ discard block |
||
| 73 | 76 | $globalDBSname = $globalDBname; |
| 74 | 77 | $globalDBSuser = $user; |
| 75 | 78 | $globalDBSpass = $pass; |
| 76 | - if (!isset($globalDBport) || $globalDBport === NULL || $globalDBport == '') $globalDBSport = 3306; |
|
| 77 | - else $globalDBSport = $globalDBport; |
|
| 79 | + if (!isset($globalDBport) || $globalDBport === NULL || $globalDBport == '') { |
|
| 80 | + $globalDBSport = 3306; |
|
| 81 | + } else { |
|
| 82 | + $globalDBSport = $globalDBport; |
|
| 83 | + } |
|
| 78 | 84 | } |
| 79 | 85 | } else { |
| 80 | 86 | $globalDBSdriver = $globalDB[$DBname]['driver']; |
@@ -82,11 +88,16 @@ discard block |
||
| 82 | 88 | $globalDBSname = $globalDB[$DBname]['name']; |
| 83 | 89 | $globalDBSuser = $globalDB[$DBname]['user']; |
| 84 | 90 | $globalDBSpass = $globalDB[$DBname]['pass']; |
| 85 | - if (isset($globalDB[$DBname]['port'])) $globalDBSport = $globalDB[$DBname]['port']; |
|
| 86 | - else $globalDBSport = 3306; |
|
| 91 | + if (isset($globalDB[$DBname]['port'])) { |
|
| 92 | + $globalDBSport = $globalDB[$DBname]['port']; |
|
| 93 | + } else { |
|
| 94 | + $globalDBSport = 3306; |
|
| 95 | + } |
|
| 87 | 96 | } |
| 88 | 97 | // Set number of try to connect to DB |
| 89 | - if (!isset($globalDBretry) || $globalDBretry == '' || $globalDBretry === NULL) $globalDBretry = 5; |
|
| 98 | + if (!isset($globalDBretry) || $globalDBretry == '' || $globalDBretry === NULL) { |
|
| 99 | + $globalDBretry = 5; |
|
| 100 | + } |
|
| 90 | 101 | $i = 0; |
| 91 | 102 | while (true) { |
| 92 | 103 | try { |
@@ -95,10 +106,16 @@ discard block |
||
| 95 | 106 | $this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'"); |
| 96 | 107 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
| 97 | 108 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER); |
| 98 | - if (!isset($globalDBTimeOut)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,500); |
|
| 99 | - else $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,$globalDBTimeOut); |
|
| 100 | - if (!isset($globalDBPersistent)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,true); |
|
| 101 | - else $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,$globalDBPersistent); |
|
| 109 | + if (!isset($globalDBTimeOut)) { |
|
| 110 | + $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,500); |
|
| 111 | + } else { |
|
| 112 | + $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,$globalDBTimeOut); |
|
| 113 | + } |
|
| 114 | + if (!isset($globalDBPersistent)) { |
|
| 115 | + $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,true); |
|
| 116 | + } else { |
|
| 117 | + $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,$globalDBPersistent); |
|
| 118 | + } |
|
| 102 | 119 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
| 103 | 120 | $this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); |
| 104 | 121 | // Workaround against "ONLY_FULL_GROUP_BY" mode |
@@ -112,23 +129,35 @@ discard block |
||
| 112 | 129 | //$this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'"); |
| 113 | 130 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
| 114 | 131 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER); |
| 115 | - if (!isset($globalDBTimeOut)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,200); |
|
| 116 | - else $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,$globalDBTimeOut); |
|
| 117 | - if (!isset($globalDBPersistent)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,true); |
|
| 118 | - else $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,$globalDBPersistent); |
|
| 132 | + if (!isset($globalDBTimeOut)) { |
|
| 133 | + $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,200); |
|
| 134 | + } else { |
|
| 135 | + $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,$globalDBTimeOut); |
|
| 136 | + } |
|
| 137 | + if (!isset($globalDBPersistent)) { |
|
| 138 | + $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,true); |
|
| 139 | + } else { |
|
| 140 | + $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,$globalDBPersistent); |
|
| 141 | + } |
|
| 119 | 142 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
| 120 | 143 | $this->dbs[$DBname]->exec('SET timezone="UTC"'); |
| 121 | 144 | } |
| 122 | 145 | break; |
| 123 | 146 | } catch(PDOException $e) { |
| 124 | 147 | $i++; |
| 125 | - if (isset($globalDebug) && $globalDebug) echo 'Error connecting to DB: '.$globalDBSname.' - Error: '.$e->getMessage()."\n"; |
|
| 148 | + if (isset($globalDebug) && $globalDebug) { |
|
| 149 | + echo 'Error connecting to DB: '.$globalDBSname.' - Error: '.$e->getMessage()."\n"; |
|
| 150 | + } |
|
| 126 | 151 | //exit; |
| 127 | - if ($i > $globalDBretry) return false; |
|
| 152 | + if ($i > $globalDBretry) { |
|
| 153 | + return false; |
|
| 154 | + } |
|
| 128 | 155 | //return false; |
| 129 | 156 | } |
| 130 | 157 | } |
| 131 | - if ($DBname === 'default') $this->db = $this->dbs['default']; |
|
| 158 | + if ($DBname === 'default') { |
|
| 159 | + $this->db = $this->dbs['default']; |
|
| 160 | + } |
|
| 132 | 161 | return true; |
| 133 | 162 | } |
| 134 | 163 | |
@@ -140,7 +169,9 @@ discard block |
||
| 140 | 169 | } else { |
| 141 | 170 | $query = "SELECT * FROM pg_catalog.pg_tables WHERE tablename = '".$table."'"; |
| 142 | 171 | } |
| 143 | - if ($this->db == NULL) return false; |
|
| 172 | + if ($this->db == NULL) { |
|
| 173 | + return false; |
|
| 174 | + } |
|
| 144 | 175 | try { |
| 145 | 176 | //$Connection = new Connection(); |
| 146 | 177 | $results = $this->db->query($query); |
@@ -149,21 +180,28 @@ discard block |
||
| 149 | 180 | } |
| 150 | 181 | if($results->rowCount()>0) { |
| 151 | 182 | return true; |
| 183 | + } else { |
|
| 184 | + return false; |
|
| 152 | 185 | } |
| 153 | - else return false; |
|
| 154 | 186 | } |
| 155 | 187 | |
| 156 | 188 | public function connectionExists() |
| 157 | 189 | { |
| 158 | 190 | global $globalDBdriver, $globalDBCheckConnection; |
| 159 | - if (isset($globalDBCheckConnection) && $globalDBCheckConnection === FALSE) return true; |
|
| 191 | + if (isset($globalDBCheckConnection) && $globalDBCheckConnection === FALSE) { |
|
| 192 | + return true; |
|
| 193 | + } |
|
| 160 | 194 | $query = "SELECT 1 + 1"; |
| 161 | - if ($this->db === null) return false; |
|
| 195 | + if ($this->db === null) { |
|
| 196 | + return false; |
|
| 197 | + } |
|
| 162 | 198 | try { |
| 163 | 199 | $sum = @$this->db->query($query); |
| 164 | 200 | if ($sum instanceof \PDOStatement) { |
| 165 | 201 | $sum = $sum->fetchColumn(0); |
| 166 | - } else $sum = 0; |
|
| 202 | + } else { |
|
| 203 | + $sum = 0; |
|
| 204 | + } |
|
| 167 | 205 | if (intval($sum) !== 2) { |
| 168 | 206 | return false; |
| 169 | 207 | } |
@@ -198,8 +236,9 @@ discard block |
||
| 198 | 236 | $nb = $results->fetchAll(PDO::FETCH_ASSOC); |
| 199 | 237 | if($nb[0]['nb'] > 0) { |
| 200 | 238 | return true; |
| 239 | + } else { |
|
| 240 | + return false; |
|
| 201 | 241 | } |
| 202 | - else return false; |
|
| 203 | 242 | } |
| 204 | 243 | |
| 205 | 244 | /* |
@@ -243,9 +282,12 @@ discard block |
||
| 243 | 282 | } |
| 244 | 283 | $result = $sth->fetch(PDO::FETCH_ASSOC); |
| 245 | 284 | $sth->closeCursor(); |
| 246 | - if ($result['nb'] > 0) return true; |
|
| 247 | - else return false; |
|
| 248 | -/* } else { |
|
| 285 | + if ($result['nb'] > 0) { |
|
| 286 | + return true; |
|
| 287 | + } else { |
|
| 288 | + return false; |
|
| 289 | + } |
|
| 290 | + /* } else { |
|
| 249 | 291 | $query = "SELECT * FROM ".$table." LIMIT 0"; |
| 250 | 292 | try { |
| 251 | 293 | $results = $this->db->query($query); |
@@ -284,7 +326,9 @@ discard block |
||
| 284 | 326 | $sth->closeCursor(); |
| 285 | 327 | return $result['value']; |
| 286 | 328 | } |
| 287 | - } else return $version; |
|
| 329 | + } else { |
|
| 330 | + return $version; |
|
| 331 | + } |
|
| 288 | 332 | } |
| 289 | 333 | |
| 290 | 334 | /* |
@@ -292,8 +336,11 @@ discard block |
||
| 292 | 336 | * @return Boolean if latest version or not |
| 293 | 337 | */ |
| 294 | 338 | public function latest() { |
| 295 | - if ($this->check_schema_version() == $this->latest_schema) return true; |
|
| 296 | - else return false; |
|
| 339 | + if ($this->check_schema_version() == $this->latest_schema) { |
|
| 340 | + return true; |
|
| 341 | + } else { |
|
| 342 | + return false; |
|
| 343 | + } |
|
| 297 | 344 | } |
| 298 | 345 | |
| 299 | 346 | } |