[6] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /** |
---|
| 4 | * Copyright (C) 2008-2011 FluxBB |
---|
| 5 | * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB |
---|
| 6 | * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | // Make sure we have built in support for MySQL |
---|
| 10 | if (!function_exists('mysqli_connect')) |
---|
| 11 | exit('This PHP environment doesn\'t have Improved MySQL (mysqli) support built in. Improved MySQL support is required if you want to use a MySQL 4.1 (or later) database to run this forum. Consult the PHP documentation for further assistance.'); |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | class DBLayer |
---|
| 15 | { |
---|
| 16 | var $prefix; |
---|
| 17 | var $link_id; |
---|
| 18 | var $query_result; |
---|
| 19 | |
---|
| 20 | var $saved_queries = array(); |
---|
| 21 | var $num_queries = 0; |
---|
| 22 | var $in_transaction = 0; |
---|
| 23 | |
---|
| 24 | var $error_no = false; |
---|
| 25 | var $error_msg = 'Unknown'; |
---|
| 26 | |
---|
| 27 | var $datatype_transformations = array( |
---|
| 28 | '%^SERIAL$%' => 'INT(10) UNSIGNED AUTO_INCREMENT' |
---|
| 29 | ); |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect) |
---|
| 33 | { |
---|
| 34 | $this->prefix = $db_prefix; |
---|
| 35 | |
---|
| 36 | // Was a custom port supplied with $db_host? |
---|
| 37 | if (strpos($db_host, ':') !== false) |
---|
| 38 | list($db_host, $db_port) = explode(':', $db_host); |
---|
| 39 | |
---|
| 40 | // Persistent connection in MySQLi are only available in PHP 5.3 and later releases |
---|
| 41 | $p_connect = $p_connect && version_compare(PHP_VERSION, '5.3.0', '>=') ? 'p:' : ''; |
---|
| 42 | |
---|
| 43 | if (isset($db_port)) |
---|
| 44 | $this->link_id = @mysqli_connect($p_connect.$db_host, $db_username, $db_password, $db_name, $db_port); |
---|
| 45 | else |
---|
| 46 | $this->link_id = @mysqli_connect($p_connect.$db_host, $db_username, $db_password, $db_name); |
---|
| 47 | |
---|
| 48 | if (!$this->link_id) |
---|
| 49 | error('Unable to connect to MySQL and select database. MySQL reported: '.mysqli_connect_error(), __FILE__, __LINE__); |
---|
| 50 | |
---|
| 51 | // Setup the client-server character set (UTF-8) |
---|
| 52 | if (!defined('FORUM_NO_SET_NAMES')) |
---|
| 53 | $this->set_names('utf8'); |
---|
| 54 | |
---|
| 55 | return $this->link_id; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | function start_transaction() |
---|
| 60 | { |
---|
| 61 | ++$this->in_transaction; |
---|
| 62 | |
---|
| 63 | mysqli_query($this->link_id, 'START TRANSACTION'); |
---|
| 64 | return; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | function end_transaction() |
---|
| 69 | { |
---|
| 70 | --$this->in_transaction; |
---|
| 71 | |
---|
| 72 | mysqli_query($this->link_id, 'COMMIT'); |
---|
| 73 | return; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | function query($sql, $unbuffered = false) |
---|
| 78 | { |
---|
| 79 | if (defined('PUN_SHOW_QUERIES')) |
---|
| 80 | $q_start = get_microtime(); |
---|
| 81 | |
---|
| 82 | $this->query_result = @mysqli_query($this->link_id, $sql); |
---|
| 83 | |
---|
| 84 | if ($this->query_result) |
---|
| 85 | { |
---|
| 86 | if (defined('PUN_SHOW_QUERIES')) |
---|
| 87 | $this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start)); |
---|
| 88 | |
---|
| 89 | ++$this->num_queries; |
---|
| 90 | |
---|
| 91 | return $this->query_result; |
---|
| 92 | } |
---|
| 93 | else |
---|
| 94 | { |
---|
| 95 | if (defined('PUN_SHOW_QUERIES')) |
---|
| 96 | $this->saved_queries[] = array($sql, 0); |
---|
| 97 | |
---|
| 98 | $this->error_no = @mysqli_errno($this->link_id); |
---|
| 99 | $this->error_msg = @mysqli_error($this->link_id); |
---|
| 100 | |
---|
| 101 | // Rollback transaction |
---|
| 102 | if ($this->in_transaction) |
---|
| 103 | mysqli_query($this->link_id, 'ROLLBACK'); |
---|
| 104 | |
---|
| 105 | --$this->in_transaction; |
---|
| 106 | |
---|
| 107 | return false; |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | function result($query_id = 0, $row = 0, $col = 0) |
---|
| 113 | { |
---|
| 114 | if ($query_id) |
---|
| 115 | { |
---|
| 116 | if ($row !== 0 && @mysqli_data_seek($query_id, $row) === false) |
---|
| 117 | return false; |
---|
| 118 | |
---|
| 119 | $cur_row = @mysqli_fetch_row($query_id); |
---|
| 120 | if ($cur_row === false) |
---|
| 121 | return false; |
---|
| 122 | |
---|
| 123 | return $cur_row[$col]; |
---|
| 124 | } |
---|
| 125 | else |
---|
| 126 | return false; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | function fetch_assoc($query_id = 0) |
---|
| 131 | { |
---|
| 132 | return ($query_id) ? @mysqli_fetch_assoc($query_id) : false; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | function fetch_row($query_id = 0) |
---|
| 137 | { |
---|
| 138 | return ($query_id) ? @mysqli_fetch_row($query_id) : false; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | function num_rows($query_id = 0) |
---|
| 143 | { |
---|
| 144 | return ($query_id) ? @mysqli_num_rows($query_id) : false; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | function affected_rows() |
---|
| 149 | { |
---|
| 150 | return ($this->link_id) ? @mysqli_affected_rows($this->link_id) : false; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | function insert_id() |
---|
| 155 | { |
---|
| 156 | return ($this->link_id) ? @mysqli_insert_id($this->link_id) : false; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | function get_num_queries() |
---|
| 161 | { |
---|
| 162 | return $this->num_queries; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | function get_saved_queries() |
---|
| 167 | { |
---|
| 168 | return $this->saved_queries; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | function free_result($query_id = false) |
---|
| 173 | { |
---|
| 174 | return ($query_id) ? @mysqli_free_result($query_id) : false; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | function escape($str) |
---|
| 179 | { |
---|
| 180 | return is_array($str) ? '' : mysqli_real_escape_string($this->link_id, $str); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | function error() |
---|
| 185 | { |
---|
| 186 | $result['error_sql'] = @current(@end($this->saved_queries)); |
---|
| 187 | $result['error_no'] = $this->error_no; |
---|
| 188 | $result['error_msg'] = $this->error_msg; |
---|
| 189 | |
---|
| 190 | return $result; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | function close() |
---|
| 195 | { |
---|
| 196 | if ($this->link_id) |
---|
| 197 | { |
---|
| 198 | if ($this->query_result) |
---|
| 199 | @mysqli_free_result($this->query_result); |
---|
| 200 | |
---|
| 201 | return @mysqli_close($this->link_id); |
---|
| 202 | } |
---|
| 203 | else |
---|
| 204 | return false; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | |
---|
| 208 | function get_names() |
---|
| 209 | { |
---|
| 210 | $result = $this->query('SHOW VARIABLES LIKE \'character_set_connection\''); |
---|
| 211 | return $this->result($result, 0, 1); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | function set_names($names) |
---|
| 216 | { |
---|
| 217 | return $this->query('SET NAMES \''.$this->escape($names).'\''); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | function get_version() |
---|
| 222 | { |
---|
| 223 | $result = $this->query('SELECT VERSION()'); |
---|
| 224 | |
---|
| 225 | return array( |
---|
| 226 | 'name' => 'MySQL Improved (InnoDB)', |
---|
| 227 | 'version' => preg_replace('%^([^-]+).*$%', '\\1', $this->result($result)) |
---|
| 228 | ); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | |
---|
| 232 | function table_exists($table_name, $no_prefix = false) |
---|
| 233 | { |
---|
| 234 | $result = $this->query('SHOW TABLES LIKE \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\''); |
---|
| 235 | return $this->num_rows($result) > 0; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | |
---|
| 239 | function field_exists($table_name, $field_name, $no_prefix = false) |
---|
| 240 | { |
---|
| 241 | $result = $this->query('SHOW COLUMNS FROM '.($no_prefix ? '' : $this->prefix).$table_name.' LIKE \''.$this->escape($field_name).'\''); |
---|
| 242 | return $this->num_rows($result) > 0; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | |
---|
| 246 | function index_exists($table_name, $index_name, $no_prefix = false) |
---|
| 247 | { |
---|
| 248 | $exists = false; |
---|
| 249 | |
---|
| 250 | $result = $this->query('SHOW INDEX FROM '.($no_prefix ? '' : $this->prefix).$table_name); |
---|
| 251 | while ($cur_index = $this->fetch_assoc($result)) |
---|
| 252 | { |
---|
| 253 | if (strtolower($cur_index['Key_name']) == strtolower(($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name)) |
---|
| 254 | { |
---|
| 255 | $exists = true; |
---|
| 256 | break; |
---|
| 257 | } |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | return $exists; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | |
---|
| 264 | function create_table($table_name, $schema, $no_prefix = false) |
---|
| 265 | { |
---|
| 266 | if ($this->table_exists($table_name, $no_prefix)) |
---|
| 267 | return true; |
---|
| 268 | |
---|
| 269 | $query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n"; |
---|
| 270 | |
---|
| 271 | // Go through every schema element and add it to the query |
---|
| 272 | foreach ($schema['FIELDS'] as $field_name => $field_data) |
---|
| 273 | { |
---|
| 274 | $field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']); |
---|
| 275 | |
---|
| 276 | $query .= $field_name.' '.$field_data['datatype']; |
---|
| 277 | |
---|
| 278 | if (isset($field_data['collation'])) |
---|
| 279 | $query .= 'CHARACTER SET utf8 COLLATE utf8_'.$field_data['collation']; |
---|
| 280 | |
---|
| 281 | if (!$field_data['allow_null']) |
---|
| 282 | $query .= ' NOT NULL'; |
---|
| 283 | |
---|
| 284 | if (isset($field_data['default'])) |
---|
| 285 | $query .= ' DEFAULT '.$field_data['default']; |
---|
| 286 | |
---|
| 287 | $query .= ",\n"; |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | // If we have a primary key, add it |
---|
| 291 | if (isset($schema['PRIMARY KEY'])) |
---|
| 292 | $query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n"; |
---|
| 293 | |
---|
| 294 | // Add unique keys |
---|
| 295 | if (isset($schema['UNIQUE KEYS'])) |
---|
| 296 | { |
---|
| 297 | foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields) |
---|
| 298 | $query .= 'UNIQUE KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$key_name.'('.implode(',', $key_fields).'),'."\n"; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | // Add indexes |
---|
| 302 | if (isset($schema['INDEXES'])) |
---|
| 303 | { |
---|
| 304 | foreach ($schema['INDEXES'] as $index_name => $index_fields) |
---|
| 305 | $query .= 'KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.'('.implode(',', $index_fields).'),'."\n"; |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | // We remove the last two characters (a newline and a comma) and add on the ending |
---|
| 309 | $query = substr($query, 0, strlen($query) - 2)."\n".') ENGINE = '.(isset($schema['ENGINE']) ? $schema['ENGINE'] : 'InnoDB').' CHARACTER SET utf8'; |
---|
| 310 | |
---|
| 311 | return $this->query($query) ? true : false; |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | |
---|
| 315 | function drop_table($table_name, $no_prefix = false) |
---|
| 316 | { |
---|
| 317 | if (!$this->table_exists($table_name, $no_prefix)) |
---|
| 318 | return true; |
---|
| 319 | |
---|
| 320 | return $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false; |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | |
---|
| 324 | function rename_table($old_table, $new_table, $no_prefix = false) |
---|
| 325 | { |
---|
| 326 | // If there new table exists and the old one doesn't, then we're happy |
---|
| 327 | if ($this->table_exists($new_table, $no_prefix) && !$this->table_exists($old_table, $no_prefix)) |
---|
| 328 | return true; |
---|
| 329 | |
---|
| 330 | return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$old_table.' RENAME TO '.($no_prefix ? '' : $this->prefix).$new_table) ? true : false; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | |
---|
| 334 | function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false) |
---|
| 335 | { |
---|
| 336 | if ($this->field_exists($table_name, $field_name, $no_prefix)) |
---|
| 337 | return true; |
---|
| 338 | |
---|
| 339 | $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type); |
---|
| 340 | |
---|
| 341 | if ($default_value !== null && !is_int($default_value) && !is_float($default_value)) |
---|
| 342 | $default_value = '\''.$this->escape($default_value).'\''; |
---|
| 343 | |
---|
| 344 | return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ADD '.$field_name.' '.$field_type.($allow_null ? ' ' : ' NOT NULL').($default_value !== null ? ' DEFAULT '.$default_value : ' ').($after_field != null ? ' AFTER '.$after_field : '')) ? true : false; |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | |
---|
| 348 | function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false) |
---|
| 349 | { |
---|
| 350 | if (!$this->field_exists($table_name, $field_name, $no_prefix)) |
---|
| 351 | return true; |
---|
| 352 | |
---|
| 353 | $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type); |
---|
| 354 | |
---|
| 355 | if ($default_value !== null && !is_int($default_value) && !is_float($default_value)) |
---|
| 356 | $default_value = '\''.$this->escape($default_value).'\''; |
---|
| 357 | |
---|
| 358 | return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' MODIFY '.$field_name.' '.$field_type.($allow_null ? ' ' : ' NOT NULL').($default_value !== null ? ' DEFAULT '.$default_value : ' ').($after_field != null ? ' AFTER '.$after_field : '')) ? true : false; |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | |
---|
| 362 | function drop_field($table_name, $field_name, $no_prefix = false) |
---|
| 363 | { |
---|
| 364 | if (!$this->field_exists($table_name, $field_name, $no_prefix)) |
---|
| 365 | return true; |
---|
| 366 | |
---|
| 367 | return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP '.$field_name) ? true : false; |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | |
---|
| 371 | function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false) |
---|
| 372 | { |
---|
| 373 | if ($this->index_exists($table_name, $index_name, $no_prefix)) |
---|
| 374 | return true; |
---|
| 375 | |
---|
| 376 | return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ADD '.($unique ? 'UNIQUE ' : '').'INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.' ('.implode(',', $index_fields).')') ? true : false; |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | |
---|
| 380 | function drop_index($table_name, $index_name, $no_prefix = false) |
---|
| 381 | { |
---|
| 382 | if (!$this->index_exists($table_name, $index_name, $no_prefix)) |
---|
| 383 | return true; |
---|
| 384 | |
---|
| 385 | return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) ? true : false; |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | function truncate_table($table_name, $no_prefix = false) |
---|
| 389 | { |
---|
| 390 | return $this->query('TRUNCATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false; |
---|
| 391 | } |
---|
| 392 | } |
---|