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