From 1fe76ba743a3418da9a0883b29756d442384d0bc Mon Sep 17 00:00:00 2001
From: Horus3
Date: Thu, 25 Sep 2014 10:33:47 +0200
Subject: init
---
class/mysql.php | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 153 insertions(+)
create mode 100644 class/mysql.php
(limited to 'class/mysql.php')
diff --git a/class/mysql.php b/class/mysql.php
new file mode 100644
index 0000000..8d75538
--- /dev/null
+++ b/class/mysql.php
@@ -0,0 +1,153 @@
+open();
+ }
+
+ public function open(){
+ try {
+ $this->db = new mysqli(DBHOST, DBUSER, DBPASSWORD, DBNAME);
+ } catch (Exception $e){
+ failure("
".$e->getMessage()."
", '500 Server Failure', false, 'Failed to open database connection.
');
+ }
+
+ if ( $this->db->connect_errno ){
+ failure("Can't connect to the database. MySQL gave this error code: " . $this->db->connect_errno . "
", '500 Server Failure', false, 'Connection to MySQL server failed.
');
+ }
+
+ if ( ! $this->db->ping() ){
+ failure("Can't reach MySQL server. Server says: " . $this->db->error . "
", '500 Server Failure', false, "Can't reach MySQL server!
");
+ }
+
+ if ( ! $this->db->set_charset(DBCHARSET) ){
+ failure("Can't set " . DBCHARSET . " as the charset on your MySQL server.
" , '500 Server Failure', false, "Setting Charset failed!
");
+ }
+
+ }
+
+ 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($string);
+ }
+
+ # 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 != "" ){
+ //$result->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( '|(?_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;';
+
+ $jg_table =
+ 'CREATE TABLE IF NOT EXISTS ' . DBPREFIX . 'member
+ ( member_id INTEGER AUTO_INCREMENT NOT NULL, PRIMARY KEY(member_id),
+ name varchar(70), UNIQUE(name),
+ adresse TEXT,
+ telefonnummer TEXT,
+ handynummer TEXT,
+ email varchar(70), UNIQUE(email),
+ geburtstag TEXT
+ )
+ ENGINE=InnoDB;';
+
+ if ( ! $this->execMultipleQueries('BEGIN; '. $user_table . ' ' . $banned_user_table . ' ' . $jg_table . ' COMMIT;') )
+ failure("There was a problem during bootstrapping the database schema. " . $this->db->error . "
", '500 Server Failure', false, "CREATE TABLE FAILED
");
+ }
+
+ public function __destruct(){
+ $this->close();
+ }
+}
--
cgit v1.2.3