From 6e1d4a6d8b9288356e64674f41209be4f4c2acfe Mon Sep 17 00:00:00 2001 From: Andreas Habel Date: Tue, 20 Oct 2009 19:03:02 +0200 Subject: New accounts will be written to a mysql database --- includes/conf/mysql.conf.php | 8 ++++ includes/libs/libmysql.php | 89 ++++++++++++++++++++++++++++++++++++ includes/libs/libstrutils.php | 10 +++++ includes/models/account.php | 102 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 209 insertions(+) create mode 100644 includes/conf/mysql.conf.php create mode 100644 includes/libs/libmysql.php create mode 100644 includes/libs/libstrutils.php create mode 100644 includes/models/account.php (limited to 'includes') diff --git a/includes/conf/mysql.conf.php b/includes/conf/mysql.conf.php new file mode 100644 index 0000000..05122cd --- /dev/null +++ b/includes/conf/mysql.conf.php @@ -0,0 +1,8 @@ + \ No newline at end of file diff --git a/includes/libs/libmysql.php b/includes/libs/libmysql.php new file mode 100644 index 0000000..5a3a06c --- /dev/null +++ b/includes/libs/libmysql.php @@ -0,0 +1,89 @@ +conn = mysql_connect( $conf['mysql_hostname'], + $conf['mysql_username'], + $conf['mysql_password'] ) + or die ("Connection to database failed!" . mysql_error()); + + mysql_select_db( $conf['mysql_database'], $this->conn ) + or die ("Selection of database failed! " . mysql_error()); + } + + private function checkConnect() + { + if (!isset($this->conn)) + { + die("Not connected to database"); + } + } + + // returns the value in the first row and column + public function getValue( $sql ) + { + $this->checkConnect(); + + $res = mysql_query( $sql, $this->conn ); + if (!$res) + { + die('Error while calling database: ' . mysql_error()); + } + $vals = mysql_fetch_row( $res ); + mysql_free_result( $res ); + return $vals[0]; + } + + // executes some sql and returns affected rows + public function exec( $sql ) + { + $this->checkConnect(); + + $res = mysql_query( $sql, $this->conn ); + if (!$res) + { + die('Error while calling database: ' . mysql_error()); + } + $numrows = mysql_affected_rows( $this->conn ); + return $numrows; + } + + public function escape( $string ) + { + $this->checkConnect(); + + return mysql_real_escape_string( $string, $this->conn ); + } + + public function disconnect() + { + if ( mysql_ping( $this->conn ) ) + { + mysql_close( $this->conn ); + } + } + +} + + +?> \ No newline at end of file diff --git a/includes/libs/libstrutils.php b/includes/libs/libstrutils.php new file mode 100644 index 0000000..9c097af --- /dev/null +++ b/includes/libs/libstrutils.php @@ -0,0 +1,10 @@ + \ No newline at end of file diff --git a/includes/models/account.php b/includes/models/account.php new file mode 100644 index 0000000..ea091f5 --- /dev/null +++ b/includes/models/account.php @@ -0,0 +1,102 @@ +getValue( $sql ); + } + + public static function existsUsername($str) + { + $db = Database::getInstance(); + $sql = sprintf("SELECT COUNT(*) FROM " . TMWAccount::ACCOUNT_TBL . + " WHERE USERNAME = '%s'", $db->escape($str)); + return ($db->getValue($sql) == 1); + } + + public function setUsername($name){ $this->username = $name; } + public function setPassword($pwd){ $this->password = $pwd; } + public function setEMail($email){ $this->email = $email; } + public function setGender($gender){ $this->gender = $gender; } + + public function validate() + { + $errors = array(); + + // check here for correct values.. + if (strlen($this->username) < 4) + $errors[] = "Username is too short"; + + if (strlen($this->password) < 4) + $errors[] = "Password is too short"; + + if (!check_chars($this->username)) + $errors[] = 'Username contains invalid characters. ' . BAD_STRING_DESC; + + if (!check_chars($this->password)) + $errors[] = 'Password contains invalid characters. ' . BAD_STRING_DESC; + + if ($this->gender != TMWAccount::GENDER_MALE && + $this->gender != TMWAccount::GENDER_FEMALE ) + { + $errors[] = 'Gender has to be Male or Female!'; + } + + if (!filter_var($this->email, FILTER_VALIDATE_EMAIL)) + { + $errors[] = 'EMail has wrong format.'; + } + + + // returns true if everything is fine ( test with === true) + if (count($errors) == 0) + { + return true; + } + else + { + return $errors; + } + } + + + + public function storeAccount() + { + $db = Database::getInstance(); + $sql = sprintf( "INSERT INTO " . TMWAccount::ACCOUNT_TBL . + " (USERNAME, PASSWORD, EMAIL, GENDER) " . + "VALUES ('%s', '%s', '%s', %d) ", + $db->escape($this->username), + $db->escape($this->password), + $db->escape($this->email), + $this->gender); + + $rows = $db->exec( $sql ); + return ( $rows == 1 ); + } +} + +?> \ No newline at end of file -- cgit v1.2.3-60-g2f50