/*
* 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
--- /dev/null
+# 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 */;
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