diff --git a/backend/User.php b/backend/User.php index 59fcdf7..1261cb2 100644 --- a/backend/User.php +++ b/backend/User.php @@ -14,7 +14,7 @@ class User { $query = 'SELECT * FROM users WHERE email=?'; $user = $this->db->query($query, array($email))->fetch(); if ($user) { - if ($user['old_password'] !== 'NULL') { + if ($user['old_password'] !== null) { if ($user['old_password'] === crypt($password, $email)) { if ($this->upgradePassword($password)) { return $this->logIn($email, $password); @@ -90,9 +90,28 @@ class User { return false; } + public function getAllDictionaries ($token) { + $user_data = $this->token->decode($token); + if ($user_data !== false) { + $id = $user_data->id; + $query = "SELECT id, name FROM dictionaries WHERE user=$id"; + $results = $this->db->query($query)->fetchAll(); + if ($results) { + return array_map(function($result) { + return array( + 'id' => $this->token->hash($result['id']), + 'name' => $result['name'], + ); + }, $results); + } + return array(); + } + return false; + } + private function hasMembership ($id) { $current_membership = "SELECT * FROM memberships WHERE user=$id AND start_date>=CURRENT_TIMESTAMP AND CURRENT_TIMESTAMPdb->query($current_membership)->rowCount() > 0; + return $this->db->query($current_membership)->rowCount() > 0; } private function upgradePassword ($password) { diff --git a/backend/index.php b/backend/index.php index 5a3c841..aa7e563 100644 --- a/backend/index.php +++ b/backend/index.php @@ -6,7 +6,7 @@ $inputJSON = file_get_contents('php://input'); $request= json_decode($inputJSON, true); $action = isset($request['action']) ? $request['action'] : ''; -$token = isset($request['token']) ? $request['token'] : ''; +$token = isset($request['token']) ? $request['token'] : false; switch ($action) { case 'login': { @@ -55,6 +55,26 @@ switch ($action) { 'error' => true, ), 400); } + case 'get-all-dictionaries': { + if ($token !== false) { + $user = new User(); + $all_dictionaries = $user->getAllDictionaries($token); + if ($all_dictionaries !== false) { + return Response::json(array( + 'data' => $all_dictionaries, + 'error' => false, + ), 200); + } + return Response::json(array( + 'data' => 'Could not get dictionaries: invalid token', + 'error' => true, + ), 400); + } + return Response::json(array( + 'data' => 'Could not get dictionaries: no token provided', + 'error' => true, + ), 403); + } default: { return Response::html('Hi!');