Lexiconga/backend/index.php

82 lines
2.4 KiB
PHP
Raw Normal View History

2017-12-24 20:00:45 +01:00
<?php
require_once('./Response.php');
require_once('./User.php');
$inputJSON = file_get_contents('php://input');
$request= json_decode($inputJSON, true);
$action = isset($request['action']) ? $request['action'] : '';
$token = isset($request['token']) ? $request['token'] : false;
2017-12-24 20:00:45 +01:00
switch ($action) {
case 'login': {
if (isset($request['email']) && isset($request['password'])) {
2017-12-24 20:00:45 +01:00
$user = new User();
$token = $user->logIn($request['email'], $request['password']);
2017-12-24 20:00:45 +01:00
if ($token !== false) {
return Response::json(array(
2017-12-24 20:00:45 +01:00
'data' => $token,
'error' => false,
), 200);
}
return Response::json(array(
2017-12-24 20:00:45 +01:00
'data' => 'Could not log in: incorrect data',
'error' => true,
), 401);
2017-12-24 20:00:45 +01:00
}
return Response::json(array(
2017-12-24 20:00:45 +01:00
'data' => 'Could not log in: required information missing',
'error' => true,
), 400);
2017-12-24 20:00:45 +01:00
}
2017-12-24 21:09:05 +01:00
case 'create-account': {
if (isset($request['email']) && isset($request['password'])) {
2017-12-24 21:09:05 +01:00
$user = new User();
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
}
return Response::json(array(
'data' => 'Could not create account: duplicate email',
2017-12-24 21:09:05 +01:00
'error' => true,
), 403);
2017-12-24 21:09:05 +01:00
}
return Response::json(array(
2017-12-24 21:09:05 +01:00
'data' => 'Could not create account: required information missing',
'error' => true,
), 400);
}
case 'get-all-dictionary-names': {
if ($token !== false) {
$user = new User();
$all_dictionaries = $user->listAllDictionaryNames($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!');
2017-12-24 21:09:05 +01:00
}
2017-12-24 20:00:45 +01:00
}