From eea7a7b811c836a253d9d21a6a3897b2963cdc18 Mon Sep 17 00:00:00 2001 From: Artem Daniliants Date: Tue, 9 Mar 2010 13:05:52 +0200 Subject: [PATCH] Registration is functional --- Server/application/controllers/api.php | 29 +++++++++++++++++ Server/application/models/database_dump.sql | 46 +++++++++++++++++++++++++++ Server/application/models/user.php | 28 ++++++++++++++-- 3 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 Server/application/models/database_dump.sql diff --git a/Server/application/controllers/api.php b/Server/application/controllers/api.php index 1e6ca37..33c8558 100644 --- a/Server/application/controllers/api.php +++ b/Server/application/controllers/api.php @@ -19,4 +19,33 @@ class Api_Controller extends Controller{ /* * New user registration */ + public function register(){ + $xml = $this->get_xml(); + try { + $user = new User_Model($xml->login, $xml->password, $xml->email); + return "OK"; + } + catch (Exception $e) { + echo $e->getMessage() . "\n"; + die; + } + } + + /* + * Returns XML file supplied by client + */ + private function get_xml(){ + if (isset($_POST['xml'])){ + $xml = simplexml_load_string($_POST['xml']); + } + elseif (isset($_FILES['xml'])){ + $xml = simplexml_load_file($_FILES['xml']['tmp_name']); + } + else{ + header("HTTP/1.1 400 Bad Request"); + echo "Please supply required parameters"; + die; + } + return $xml; + } } \ No newline at end of file diff --git a/Server/application/models/database_dump.sql b/Server/application/models/database_dump.sql new file mode 100644 index 0000000..0908de3 --- /dev/null +++ b/Server/application/models/database_dump.sql @@ -0,0 +1,46 @@ +# Sequel Pro dump +# Version 1630 +# http://code.google.com/p/sequel-pro +# +# Host: localhost (MySQL 5.1.37) +# Database: speedfreak +# Generation Time: 2010-03-09 13:03:37 +0200 +# ************************************************************ + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + + +# Dump of table users +# ------------------------------------------------------------ + +DROP TABLE IF EXISTS `users`; + +CREATE TABLE `users` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `username` char(255) DEFAULT NULL, + `password` char(255) DEFAULT NULL, + `email` char(255) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `login_unique` (`username`), + UNIQUE KEY `email_unique` (`email`) +) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8; + + + + + + +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; diff --git a/Server/application/models/user.php b/Server/application/models/user.php index c79ac28..d408574 100644 --- a/Server/application/models/user.php +++ b/Server/application/models/user.php @@ -33,21 +33,43 @@ class User_Model extends Model { throw new Exception('Password too long'); elseif (valid::email($email) == False) throw new Exception('Invalid email supplied'); + elseif ($this->user_exists($username, $email)) + throw new Exception('User already exists (login or email matched)'); - $this->register($username, $password, $email); + if ($this->register($username, $password, $email)->valid()) + return true; + else + return false; + } } /* * Register new user + * * @param string $username Length 3-12 * @param string $password Length 6-255 (stored as sha1 hash in database) * @param string $email Valid email address * @return bool Returns True if operation was successfull and exception otherwise */ private function register($username, $password, $email){ - return $db->query("INSERT into users SET username=?, password=?, email=?", - $username, $password, $email); + return $this->db->query('INSERT into users SET username = ?, password = ?, email = ?', + $this->db->escape($username), $this->db->escape($password), $this->db->escape($email)); + } + + /* + * Check if user already exists in database + * + * @param string $username Username + * @param string $email Email address + * @return bool Returns True if user exists and false otherwise + */ + private function user_exists($username, $email){ + if ($this->db->query('SELECT id FROM users WHERE username = ? OR email = ?', + $this->db->escape($username), $this->db->escape($email))->count()>0) + return true; + else + return false; } } \ No newline at end of file -- 1.7.9.5