2017-12-24 20:00:45 +01:00
|
|
|
<?php
|
|
|
|
require_once('./Response.php');
|
|
|
|
require_once('./User.php');
|
|
|
|
|
2017-12-24 22:19:42 +01:00
|
|
|
$inputJSON = file_get_contents('php://input');
|
|
|
|
$request= json_decode($inputJSON, true);
|
|
|
|
|
|
|
|
$action = isset($request['action']) ? $request['action'] : '';
|
2017-12-24 23:14:34 +01:00
|
|
|
$token = isset($request['token']) ? $request['token'] : false;
|
2017-12-24 20:00:45 +01:00
|
|
|
|
|
|
|
switch ($action) {
|
|
|
|
case 'login': {
|
2017-12-24 22:19:42 +01:00
|
|
|
if (isset($request['email']) && isset($request['password'])) {
|
2017-12-24 20:00:45 +01:00
|
|
|
$user = new User();
|
2017-12-24 22:19:42 +01:00
|
|
|
$token = $user->logIn($request['email'], $request['password']);
|
2017-12-24 20:00:45 +01:00
|
|
|
if ($token !== false) {
|
2017-12-24 22:19:42 +01:00
|
|
|
return Response::json(array(
|
2017-12-24 20:00:45 +01:00
|
|
|
'data' => $token,
|
|
|
|
'error' => false,
|
|
|
|
), 200);
|
|
|
|
}
|
2017-12-24 22:19:42 +01:00
|
|
|
return Response::json(array(
|
2017-12-24 20:00:45 +01:00
|
|
|
'data' => 'Could not log in: incorrect data',
|
|
|
|
'error' => true,
|
2017-12-24 22:19:42 +01:00
|
|
|
), 401);
|
2017-12-24 20:00:45 +01:00
|
|
|
}
|
2017-12-24 22:19:42 +01:00
|
|
|
return Response::json(array(
|
2017-12-24 20:00:45 +01:00
|
|
|
'data' => 'Could not log in: required information missing',
|
|
|
|
'error' => true,
|
2017-12-24 22:19:42 +01:00
|
|
|
), 400);
|
2017-12-24 20:00:45 +01:00
|
|
|
}
|
2017-12-24 21:09:05 +01:00
|
|
|
case 'create-account': {
|
2017-12-24 22:19:42 +01:00
|
|
|
if (isset($request['email']) && isset($request['password'])) {
|
2017-12-24 21:09:05 +01:00
|
|
|
$user = new User();
|
2017-12-24 22:19:42 +01:00
|
|
|
if (!$user->emailExists($request['email'])) {
|
|
|
|
$token = $user->create($request['email'], $request['password']);
|
|
|
|
if ($token !== false) {
|
|
|
|
return Response::json(array(
|
|
|
|
'data' => $token,
|
|
|
|
'error' => false,
|
|
|
|
), 201);
|
|
|
|
}
|
|
|
|
return Response::json(array(
|
|
|
|
'data' => 'Could not create account: database error',
|
|
|
|
'error' => true,
|
|
|
|
), 500);
|
2017-12-24 21:09:05 +01:00
|
|
|
}
|
2017-12-24 22:19:42 +01:00
|
|
|
return Response::json(array(
|
|
|
|
'data' => 'Could not create account: duplicate email',
|
2017-12-24 21:09:05 +01:00
|
|
|
'error' => true,
|
2017-12-24 22:19:42 +01:00
|
|
|
), 403);
|
2017-12-24 21:09:05 +01:00
|
|
|
}
|
2017-12-24 22:19:42 +01:00
|
|
|
return Response::json(array(
|
2017-12-24 21:09:05 +01:00
|
|
|
'data' => 'Could not create account: required information missing',
|
|
|
|
'error' => true,
|
2017-12-24 22:19:42 +01:00
|
|
|
), 400);
|
|
|
|
}
|
2018-01-05 06:08:18 +01:00
|
|
|
case 'get-all-dictionary-names': {
|
2017-12-24 23:14:34 +01:00
|
|
|
if ($token !== false) {
|
|
|
|
$user = new User();
|
2018-01-05 06:08:18 +01:00
|
|
|
$all_dictionaries = $user->listAllDictionaryNames($token);
|
2017-12-24 23:14:34 +01:00
|
|
|
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);
|
|
|
|
}
|
2018-01-05 07:04:00 +01:00
|
|
|
case 'create-new-dictionary': {
|
|
|
|
if ($token !== false) {
|
|
|
|
$user = new User();
|
2018-01-07 00:53:58 +01:00
|
|
|
$new_data = $user->createNewDictionary($token);
|
|
|
|
if ($new_data !== false) {
|
2018-01-05 07:04:00 +01:00
|
|
|
return Response::json(array(
|
2018-01-07 00:53:58 +01:00
|
|
|
'data' => $new_data,
|
2018-01-05 07:04:00 +01:00
|
|
|
'error' => false,
|
|
|
|
), 200);
|
|
|
|
}
|
|
|
|
return Response::json(array(
|
|
|
|
'data' => 'Could not create dictionary: incorrect data',
|
|
|
|
'error' => true,
|
|
|
|
), 401);
|
|
|
|
}
|
|
|
|
return Response::json(array(
|
|
|
|
'data' => 'Could not create dictionary: no token provided',
|
|
|
|
'error' => true,
|
|
|
|
), 400);
|
|
|
|
}
|
|
|
|
case 'change-dictionary': {
|
|
|
|
if ($token !== false && isset($request['dictionary'])) {
|
|
|
|
$user = new User();
|
2018-01-07 00:53:58 +01:00
|
|
|
$new_data = $user->changeCurrentDictionary($token, $request['dictionary']);
|
|
|
|
if ($new_data !== false) {
|
2018-01-05 07:04:00 +01:00
|
|
|
return Response::json(array(
|
2018-01-07 00:53:58 +01:00
|
|
|
'data' => $new_data,
|
2018-01-05 07:04:00 +01:00
|
|
|
'error' => false,
|
|
|
|
), 200);
|
|
|
|
}
|
|
|
|
return Response::json(array(
|
|
|
|
'data' => 'Could not create dictionary: incorrect data',
|
|
|
|
'error' => true,
|
|
|
|
), 401);
|
|
|
|
}
|
|
|
|
return Response::json(array(
|
|
|
|
'data' => 'Could not create dictionary: no token provided',
|
|
|
|
'error' => true,
|
|
|
|
), 400);
|
|
|
|
}
|
2018-01-06 18:51:19 +01:00
|
|
|
case 'get-current-dictionary': {
|
|
|
|
if ($token !== false) {
|
|
|
|
$user = new User();
|
|
|
|
$dictionary_data = $user->getCurrentDictionary($token);
|
|
|
|
if ($dictionary_data !== false) {
|
|
|
|
return Response::json(array(
|
|
|
|
'data' => $dictionary_data,
|
|
|
|
'error' => false,
|
|
|
|
), 200);
|
|
|
|
}
|
|
|
|
return Response::json(array(
|
|
|
|
'data' => 'Could not get dictionary: invalid token',
|
|
|
|
'error' => true,
|
|
|
|
), 401);
|
|
|
|
}
|
|
|
|
return Response::json(array(
|
|
|
|
'data' => 'Could not get dictionary: no token provided',
|
|
|
|
'error' => true,
|
|
|
|
), 400);
|
|
|
|
}
|
2018-01-07 00:53:58 +01:00
|
|
|
case 'set-whole-current-dictionary': {
|
|
|
|
if ($token !== false && isset($request['dictionary'])) {
|
|
|
|
$user = new User();
|
|
|
|
$dictionary_data = $user->saveWholeCurrentDictionary($token, $request['dictionary']);
|
|
|
|
if ($dictionary_data !== false) {
|
|
|
|
return Response::json(array(
|
|
|
|
'data' => 'Updated successfully',
|
|
|
|
'error' => false,
|
|
|
|
), 200);
|
|
|
|
}
|
|
|
|
return Response::json(array(
|
|
|
|
'data' => 'Could not set dictionary: invalid token',
|
|
|
|
'error' => true,
|
|
|
|
), 401);
|
|
|
|
}
|
|
|
|
return Response::json(array(
|
|
|
|
'data' => 'Could not set dictionary: required data missing',
|
|
|
|
'error' => true,
|
|
|
|
), 400);
|
|
|
|
}
|
|
|
|
case 'set-dictionary-details': {
|
|
|
|
if ($token !== false && isset($request['details'])) {
|
|
|
|
$user = new User();
|
|
|
|
$update_details_success = $user->updateCurrentDictionaryDetails($token, $request['details']);
|
|
|
|
if ($update_details_success !== false) {
|
|
|
|
return Response::json(array(
|
|
|
|
'data' => 'Updated successfully',
|
|
|
|
'error' => false,
|
|
|
|
), 200);
|
|
|
|
}
|
|
|
|
return Response::json(array(
|
|
|
|
'data' => 'Could not set dictionary: invalid token',
|
|
|
|
'error' => true,
|
|
|
|
), 401);
|
|
|
|
}
|
|
|
|
return Response::json(array(
|
|
|
|
'data' => 'Could not set dictionary: required data missing',
|
|
|
|
'error' => true,
|
|
|
|
), 400);
|
|
|
|
}
|
|
|
|
case 'set-dictionary-words': {
|
|
|
|
if ($token !== false && isset($request['words'])) {
|
|
|
|
$user = new User();
|
|
|
|
$update_words_success = $user->updateOrAddWordsToCurrentDictionary($token, $request['words']);
|
|
|
|
if ($update_words_success !== false) {
|
|
|
|
return Response::json(array(
|
|
|
|
'data' => 'Updated successfully',
|
|
|
|
'error' => false,
|
|
|
|
), 200);
|
|
|
|
}
|
|
|
|
return Response::json(array(
|
2018-01-12 18:10:24 +01:00
|
|
|
'data' => 'Could not set words: invalid token',
|
2018-01-07 00:53:58 +01:00
|
|
|
'error' => true,
|
|
|
|
), 401);
|
|
|
|
}
|
|
|
|
return Response::json(array(
|
2018-01-12 18:10:24 +01:00
|
|
|
'data' => 'Could not set words: required data missing',
|
2018-01-07 00:53:58 +01:00
|
|
|
'error' => true,
|
|
|
|
), 400);
|
|
|
|
}
|
2017-12-24 22:19:42 +01:00
|
|
|
|
|
|
|
default: {
|
|
|
|
return Response::html('Hi!');
|
2017-12-24 21:09:05 +01:00
|
|
|
}
|
2017-12-24 20:00:45 +01:00
|
|
|
}
|