diff --git a/public/api/Db.php b/public/api/Db.php index 67a12cb..7623cfa 100644 --- a/public/api/Db.php +++ b/public/api/Db.php @@ -1,19 +1,27 @@ dbh = new PDO('mysql:host=localhost;dbname=lexiconga;charset=utf8', 'root', 'password'); $this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); + $this->last_error_info = null; } public function execute ($query, $params = array()) { $stmt = $this->dbh->prepare($query); - return $stmt->execute($params); + if ($stmt->execute($params)) { + $this->last_error_info = null; + return true; + } + $this->last_error_info = $stmt->errorInfo(); + return false; } public function query ($query, $params = array()) { $stmt = $this->dbh->prepare($query); $stmt->execute($params); + $this->last_error_info = $stmt->errorInfo(); return $stmt; } diff --git a/public/api/Dictionary.php b/public/api/Dictionary.php index eef5852..531f753 100644 --- a/public/api/Dictionary.php +++ b/public/api/Dictionary.php @@ -27,8 +27,8 @@ class Dictionary { } public function create ($user) { - $insert_dictionary_query = "INSERT INTO dictionaries (user, created_on) VALUES ($user, " . time() . ")"; - $insert_dictionary = $this->db->execute($insert_dictionary_query); + $insert_dictionary_query = "INSERT INTO dictionaries (user, created_on) VALUES (?, ?)"; + $insert_dictionary = $this->db->execute($insert_dictionary_query, array($user, time())); if ($insert_dictionary === true) { $new_dictionary_id = $this->db->lastInsertId(); @@ -42,10 +42,16 @@ VALUES ($new_dictionary_id, ?, ?)"; if ($insert_linguistics === true) { return $this->changeCurrent($user, $new_dictionary_id); + } else { + return array( + 'error' => '"INSERT INTO dictionary_linguistics" failed: ' . $this->db->last_error_info[2], + ); } } - return false; + return array( + 'error' => '"INSERT INTO dictionaries" failed: ' . $this->db->last_error_info[2], + ); } public function changeCurrent ($user, $dictionary) { diff --git a/public/api/User.php b/public/api/User.php index 0e935e0..9c57e92 100644 --- a/public/api/User.php +++ b/public/api/User.php @@ -47,7 +47,7 @@ class User { public function create ($email, $password, $user_data) { $insert_user_query = 'INSERT INTO users (email, password, public_name, username, allow_email, created_on) -VALUES (?, ?, ?, ?, ?, '. time() .')'; +VALUES (?, ?, ?, ?, ?, ?)'; $password_hash = password_hash($password, PASSWORD_DEFAULT); $insert_user = $this->db->execute($insert_user_query, array( @@ -56,13 +56,16 @@ VALUES (?, ?, ?, ?, ?, '. time() .')'; $user_data['publicName'] !== '' ? $user_data['publicName'] : null, $user_data['username'] !== '' ? $user_data['username'] : null, $user_data['allowEmail'] ? 1 : 0, + time(), )); if ($insert_user === true) { $new_user_id = $this->db->lastInsertId(); $new_dictionary = $this->dictionary->create($new_user_id); - if ($new_dictionary !== false) { + if (isset($new_dictionary['error'])) { + return $new_dictionary; + } else { return array( 'token' => $this->generateUserToken($new_user_id, $new_dictionary), 'user' => $this->getUserData($new_user_id), @@ -70,7 +73,9 @@ VALUES (?, ?, ?, ?, ?, '. time() .')'; } } - return false; + return array( + 'error' => '"INSERT INTO users" failed: ' . $this->db->last_error_info[2], + ); } public function setUserData ($token, $user_data) { @@ -119,14 +124,18 @@ VALUES (?, ?, ?, ?, ?, '. time() .')'; if ($user_data !== false) { $id = $user_data->id; $new_dictionary = $this->dictionary->create($id); - if ($new_dictionary !== false) { + if (!isset($new_dictionary['error'])) { return array( 'token' => $this->generateUserToken($id, $new_dictionary), 'dictionary' => $this->getCurrentDictionary($token), ); + } else { + return $new_dictionary; } } - return false; + return array( + 'error' => 'invalid token', + ); } public function changeCurrentDictionary ($token, $dictionary_hash) { diff --git a/public/api/index.php b/public/api/index.php index e684dca..0810d6a 100644 --- a/public/api/index.php +++ b/public/api/index.php @@ -34,14 +34,14 @@ switch ($action) { $user = new User(); if (!$user->emailExists($request['email'])) { $user_data = $user->create($request['email'], $request['password'], $request['userData']); - if ($user_data !== false) { + if (!isset($user_data['error'])) { return Response::json(array( 'data' => $user_data, 'error' => false, ), 201); } return Response::json(array( - 'data' => 'Could not create account: database error', + 'data' => 'Could not create account: ' . $user_data['error'], 'error' => true, ), 500); } @@ -127,14 +127,14 @@ switch ($action) { if ($token !== false) { $user = new User(); $new_data = $user->createNewDictionary($token); - if ($new_data !== false) { + if (!isset($new_data['error'])) { return Response::json(array( 'data' => $new_data, 'error' => false, ), 200); } return Response::json(array( - 'data' => 'Could not create dictionary: incorrect data', + 'data' => 'Could not create dictionary: ' . $new_data['error'], 'error' => true, ), 401); }