diff options
| author | Horus3 | 2014-09-23 20:16:17 +0200 |
|---|---|---|
| committer | Horus3 | 2014-09-23 20:16:17 +0200 |
| commit | 790524e3dee3ddcf5a8250adc8b38853d0014c9f (patch) | |
| tree | 2965207a742727b3a10eb0b32dbca5c61b9ef372 /bootstrap/class/mysql.php | |
| parent | 3c94eb3f608f9bf0dc8d19583abe273b3a67e5ff (diff) | |
| download | jungegemeinde-790524e3dee3ddcf5a8250adc8b38853d0014c9f.tar.gz | |
rebuild with bootstrap
Diffstat (limited to 'bootstrap/class/mysql.php')
| -rw-r--r-- | bootstrap/class/mysql.php | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/bootstrap/class/mysql.php b/bootstrap/class/mysql.php new file mode 100644 index 0000000..0140994 --- /dev/null +++ b/bootstrap/class/mysql.php @@ -0,0 +1,141 @@ +<?php + +class db { + + protected $db; + + public function __construct(){ + $this->open(); + } + + public function open(){ + try { + $this->db = new mysqli(DBHOST, DBUSER, DBPASSWORD, DBNAME); + } catch (Exception $e){ + failure("<p>".$e->getMessage()."</p>", '500 Server Failure', false, '<h1>Failed to open database connection.</h1>'); + } + + if ( $this->db->connect_errno() ){ + failure("<p>Can't connect to the database. MySQL gave this error code: " . $this->db->connect_errno . "</p>", '500 Server Failure', false, '<h1>Connection to MySQL server failed.</h1>'); + } + + if ( ! $this->db->ping() ){ + failure("<p>Can't reach MySQL server. Server says: " . $this->db->error . "</p>", '500 Server Failure', false, "<h1>Can't reach MySQL server!</h1>"); + } + + if ( ! $this->db->set_charset(DBCHARSET) ){ + failure("<p>Can't set " . DBCHARSET . " as the charset on your MySQL server.</p>" , '500 Server Failure', false, "<h1>Setting Charset failed!</h1>"); + } + + } + + public function close(){ + $this->db->close(); + } + + public function check(){ + if ( ! $this->db->ping() ){ + return false; + } + + return true; + } + + # does a single MySQL query with output (SELECT, INSERT, UPDATE... ) + public function doQuery($string){ + if ( ! $this->check() ) + return false; + + return $this->db->query($sql); + } + + # does multiple queries WITHOUT output (INSERT, UPDATE, DELETE... ) + public function execMultipleQueries($sql){ + if ( ! $this->check() ) + return false; + + $result = $this->db->multi_query($sql); + if ( ! $result ) + return false; + + do { + if( ! $this->db->more_results() ) + break; + if ( ! $this->db->next_result() ){ + if ( $this->db->error != "" ){ + $res->free(); + return false; + } + } + } while (true); + + return true; + } + + # code by WordPress. See @link https://core.trac.wordpress.org/browser/branches/4.0/src/wp-includes/wp-db.php#L1154 + # syntax like sprintf() + public function prepare( $query, $args ) { + if ( is_null( $query ) ) + return; + + // This is not meant to be foolproof -- but it will catch obviously incorrect usage. + if ( strpos( $query, '%' ) === false ) { + return false; + } + + $args = func_get_args(); + array_shift( $args ); + + // If args were passed as an array (as in vsprintf), move them up + if ( isset( $args[0] ) && is_array($args[0]) ) + $args = $args[0]; + + $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it + $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting + $query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware + $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s + + array_walk( $args, array( $this, '_escape_by_ref' ) ); + + return @vsprintf( $query, $args ); + } + + private function _escape_by_ref( &$string ){ + if ( ! is_float( $string ) ) + $string = $this->_real_escape( $string ); + } + + private function _real_escape( $string ){ + return $this->db->real_escape_string($string); + } + # WordPress End + + public function createTables(){ + $user_table = + 'CREATE TABLE IF NOT EXISTS ' . DBPREFIX . 'user + ( id INTEGER AUTO_INCREMENT NOT NULL, PRIMARY KEY(id), + name VARCHAR(70), UNIQUE(name), + password VARCHAR(70), UNIQUE(password), + email VARCHAR(70), UNIQUE(email), + register INTEGER, + ENGINE=InnoDB;'; + + $banned_user_table = + 'CREATE TABLE IF NOT EXISTS ' . DBPREFIX . 'banned_user + ( banned_id INTEGER AUTO_INCREMENT NOT NULL, PRIMARY KEY(banned_id), + login_attempts INTEGER, + ip TEXT, + session_id TEXT, + time INTEGER, + user INTEGER + ) + ENGINE=InnoDB;'; + + if ( ! $this->execMultipleQueries('BEGIN; '. $user_table . ' ' . $banned_user_table . ' END;') ) + failure("<p>There was a problem during bootstrapping the database schema. " . $this->db->error . "</p>", '500 Server Failure', false, "<h1>CREATE TABLE FAILED</h1>"); + } + + public function __destruct(){ + $this->close(); + } +} |
