Registration is functional
authorArtem Daniliants <artem@daniliants.com>
Tue, 9 Mar 2010 11:05:52 +0000 (13:05 +0200)
committerArtem Daniliants <artem@daniliants.com>
Tue, 9 Mar 2010 11:05:52 +0000 (13:05 +0200)
Server/application/controllers/api.php
Server/application/models/database_dump.sql [new file with mode: 0644]
Server/application/models/user.php

index 1e6ca37..33c8558 100644 (file)
@@ -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 (file)
index 0000000..0908de3
--- /dev/null
@@ -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 */;
index c79ac28..d408574 100644 (file)
@@ -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