Copy previously-written API code from React version
This commit is contained in:
parent
8a11f4a6b1
commit
f642519125
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
class Db {
|
||||
private $dbh;
|
||||
public $last_error_info;
|
||||
function __construct() {
|
||||
$this->dbh = new PDO('mysql:host=localhost;dbname=lexiconga;charset=utf8', 'root', '');
|
||||
$this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
||||
$this->last_error_info = null;
|
||||
}
|
||||
|
||||
public function execute ($query, $params = array()) {
|
||||
// Run a query that doesn't require a result to be returned
|
||||
$stmt = $this->dbh->prepare($query);
|
||||
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()) {
|
||||
// Run a query that returns results
|
||||
$stmt = $this->dbh->prepare($query);
|
||||
$stmt->execute($params);
|
||||
$this->last_error_info = $stmt->errorInfo();
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
public function lastInsertId () {
|
||||
return $this->dbh->lastInsertId();
|
||||
}
|
||||
|
||||
public function errorInfo () {
|
||||
return $this->dbh->errorInfo();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,287 @@
|
|||
<?php
|
||||
require_once('./Db.php');
|
||||
require_once('./Token.php');
|
||||
|
||||
class Dictionary {
|
||||
private $db;
|
||||
private $token;
|
||||
private $defaults;
|
||||
function __construct () {
|
||||
$this->db = new Db();
|
||||
$this->token = new Token();
|
||||
|
||||
$this->defaults = array(
|
||||
'partsOfSpeech' => array("Noun","Adjective","Verb"),
|
||||
'phonology'=> array(
|
||||
'consonants' => array(),
|
||||
'vowels' => array(),
|
||||
'blends' => array(),
|
||||
'phonotactics' => array(
|
||||
'onset' => array(),
|
||||
'nucleus' => array(),
|
||||
'coda' => array(),
|
||||
'exceptions' => '',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
private function checkIfIdExists ($id) {
|
||||
$query = "SELECT id FROM dictionaries WHERE id=?";
|
||||
$results = $this->db->query($query, array($id))->fetchAll();
|
||||
return count($results) > 0;
|
||||
}
|
||||
|
||||
public function create ($user) {
|
||||
$new_id = mt_rand(1000, 999999999);
|
||||
$id_exists = $this->checkIfIdExists($new_id);
|
||||
while ($id_exists) {
|
||||
$new_id = mt_rand(1000, 999999999);
|
||||
$id_exists = $this->checkIfIdExists($new_id);
|
||||
}
|
||||
|
||||
$insert_dictionary_query = "INSERT INTO dictionaries (id, user, created_on) VALUES (?, ?, ?)";
|
||||
$insert_dictionary = $this->db->execute($insert_dictionary_query, array($new_id, $user, time()));
|
||||
|
||||
if ($insert_dictionary === true) {
|
||||
$insert_linguistics_query = "INSERT INTO dictionary_linguistics (dictionary, parts_of_speech, phonology)
|
||||
VALUES ($new_id, ?, ?)";
|
||||
$insert_linguistics = $this->db->execute($insert_linguistics_query, array(
|
||||
json_encode($this->defaults['partsOfSpeech']),
|
||||
json_encode($this->defaults['phonology']),
|
||||
));
|
||||
|
||||
if ($insert_linguistics === true) {
|
||||
return $this->changeCurrent($user, $new_id);
|
||||
} else {
|
||||
return array(
|
||||
'error' => '"INSERT INTO dictionary_linguistics" failed: ' . $this->db->last_error_info[2],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'error' => '"INSERT INTO dictionaries" failed: ' . $this->db->last_error_info[2],
|
||||
);
|
||||
}
|
||||
|
||||
public function changeCurrent ($user, $dictionary) {
|
||||
$update_query = 'UPDATE users SET current_dictionary=? WHERE id=?';
|
||||
$update = $this->db->query($update_query, array($dictionary, $user));
|
||||
if ($update->rowCount() > 0) {
|
||||
return $dictionary;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getAllNames ($user) {
|
||||
$query = "SELECT id, name, specification FROM dictionaries WHERE user=$user";
|
||||
$results = $this->db->query($query)->fetchAll();
|
||||
if ($results) {
|
||||
return array_map(function($result) {
|
||||
return array(
|
||||
'id' => $result['id'],
|
||||
'name' => $result['name'] . ' ' . $result['specification'],
|
||||
);
|
||||
}, $results);
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getDetails ($user, $dictionary) {
|
||||
$query = "SELECT * FROM dictionaries JOIN dictionary_linguistics ON dictionary = id WHERE user=$user AND id=$dictionary";
|
||||
$result = $this->db->query($query)->fetch();
|
||||
if ($result) {
|
||||
// Default json values in case they are somehow not created by front end first
|
||||
$partsOfSpeech = $result['parts_of_speech'] !== '' ? json_decode($result['parts_of_speech']) : $this->defaults['partsOfSpeech'];
|
||||
$phonology = $result['phonology'] !== '' ? json_decode($result['phonology']) : $this->defaults['phonology'];
|
||||
|
||||
return array(
|
||||
'id' => $result['id'],
|
||||
'name' => $result['name'],
|
||||
'specification' => $result['specification'],
|
||||
'description' => $result['description'],
|
||||
'partsOfSpeech' => $partsOfSpeech,
|
||||
'details' => array(
|
||||
'phonology' => $phonology,
|
||||
'orthography' => array(
|
||||
'notes' => $result['orthography_notes'],
|
||||
),
|
||||
'grammar' => array(
|
||||
'notes' => $result['grammar_notes'],
|
||||
),
|
||||
),
|
||||
'settings' => array(
|
||||
'allowDuplicates' => $result['allow_duplicates'] === '1' ? true : false,
|
||||
'caseSensitive' => $result['case_sensitive'] === '1' ? true : false,
|
||||
'sortByDefinition' => $result['sort_by_definition'] === '1' ? true : false,
|
||||
'isComplete' => $result['is_complete'] === '1' ? true : false,
|
||||
'isPublic' => $result['is_public'] === '1' ? true : false,
|
||||
),
|
||||
'lastUpdated' => is_null($result['last_updated']) ? null : $result['last_updated'],
|
||||
'createdOn' => $result['created_on'],
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function setDetails ($user, $dictionary, $dictionary_object) {
|
||||
$query1 = "UPDATE dictionaries
|
||||
SET name=:name,
|
||||
specification=:specification,
|
||||
description=:description,
|
||||
allow_duplicates=:allow_duplicates,
|
||||
case_sensitive=:case_sensitive,
|
||||
sort_by_definition=:sort_by_definition,
|
||||
is_complete=:is_complete,
|
||||
is_public=:is_public,
|
||||
last_updated=:last_updated,
|
||||
created_on=:created_on
|
||||
WHERE user=$user AND id=$dictionary";
|
||||
|
||||
// $result1 = $this->db->query($query1, array(
|
||||
$result1 = $this->db->execute($query1, array(
|
||||
':name' => $dictionary_object['name'],
|
||||
':specification' => $dictionary_object['specification'],
|
||||
':description' => $dictionary_object['description'],
|
||||
':allow_duplicates' => $dictionary_object['settings']['allowDuplicates'],
|
||||
':case_sensitive' => $dictionary_object['settings']['caseSensitive'],
|
||||
':sort_by_definition' => $dictionary_object['settings']['sortByDefinition'],
|
||||
':is_complete' => $dictionary_object['settings']['isComplete'],
|
||||
':is_public' => $dictionary_object['settings']['isPublic'],
|
||||
':last_updated' => $dictionary_object['lastUpdated'],
|
||||
':created_on' => $dictionary_object['createdOn'],
|
||||
));
|
||||
// if ($result1->rowCount() > 0) {
|
||||
if ($result1 === true) {
|
||||
$linguistics = $dictionary_object['details'];
|
||||
$query2 = "UPDATE dictionary_linguistics
|
||||
SET parts_of_speech=:parts_of_speech,
|
||||
phonology=:phonology,
|
||||
orthography_notes=:orthography_notes,
|
||||
grammar_notes=:grammar_notes
|
||||
WHERE dictionary=$dictionary";
|
||||
|
||||
// $result2 = $this->db->query($query2, array(
|
||||
$result2 = $this->db->execute($query2, array(
|
||||
':parts_of_speech' => json_encode($dictionary_object['partsOfSpeech']),
|
||||
':phonology' => json_encode($linguistics['phonology']),
|
||||
':orthography_notes' => $linguistics['orthography']['notes'],
|
||||
':grammar_notes' => $linguistics['grammar']['notes'],
|
||||
));
|
||||
|
||||
// if ($result2->rowCount() > 0) {
|
||||
if ($result2 === true) {
|
||||
return true;
|
||||
}
|
||||
// return $result2->errorInfo();
|
||||
}
|
||||
// return $result1->errorInfo();
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getWords ($user, $dictionary) {
|
||||
$query = "SELECT words.* FROM words JOIN dictionaries ON id = dictionary WHERE dictionary=$dictionary AND user=$user";
|
||||
$results = $this->db->query($query)->fetchAll();
|
||||
if ($results) {
|
||||
return array_map(function ($row) {
|
||||
return array(
|
||||
'id' => intval($row['word_id']),
|
||||
'name' => $row['name'],
|
||||
'pronunciation' => $row['pronunciation'],
|
||||
'partOfSpeech' => $row['part_of_speech'],
|
||||
'definition' => $row['definition'],
|
||||
'details' => $row['details'],
|
||||
'lastUpdated' => is_null($row['last_updated']) ? null : intval($row['last_updated']),
|
||||
'createdOn' => intval($row['created_on']),
|
||||
);
|
||||
}, $results);
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getDeletedWords ($user, $dictionary) {
|
||||
$query = "SELECT deleted_words.* FROM deleted_words JOIN dictionaries ON id = dictionary WHERE dictionary=$dictionary AND user=$user";
|
||||
$results = $this->db->query($query)->fetchAll();
|
||||
if ($results) {
|
||||
return array_map(function ($row) {
|
||||
return array(
|
||||
'id' => intval($row['word_id']),
|
||||
'deletedOn' => intval($row['deleted_on']),
|
||||
);
|
||||
}, $results);
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
public function setWords ($user, $dictionary, $words = array()) {
|
||||
$query = 'INSERT INTO words (dictionary, word_id, name, pronunciation, part_of_speech, definition, details, last_updated, created_on) VALUES ';
|
||||
$params = array();
|
||||
$word_ids = array();
|
||||
$most_recent_word_update = 0;
|
||||
foreach($words as $word) {
|
||||
$last_updated = is_null($word['lastUpdated']) ? $word['createdOn'] : $word['lastUpdated'];
|
||||
if ($most_recent_word_update < $last_updated) {
|
||||
$most_recent_word_update = $last_updated;
|
||||
}
|
||||
$word_ids[] = $word['id'];
|
||||
$query .= "(?, ?, ?, ?, ?, ?, ?, ?, ?), ";
|
||||
$params[] = $dictionary;
|
||||
$params[] = $word['id'];
|
||||
$params[] = $word['name'];
|
||||
$params[] = $word['pronunciation'];
|
||||
$params[] = $word['partOfSpeech'];
|
||||
$params[] = $word['definition'];
|
||||
$params[] = $word['details'];
|
||||
$params[] = $last_updated;
|
||||
$params[] = $word['createdOn'];
|
||||
}
|
||||
$query = trim($query, ', ') . ' ON DUPLICATE KEY UPDATE
|
||||
name=VALUES(name),
|
||||
pronunciation=VALUES(pronunciation),
|
||||
part_of_speech=VALUES(part_of_speech),
|
||||
definition=VALUES(definition),
|
||||
details=VALUES(details),
|
||||
last_updated=VALUES(last_updated)';
|
||||
|
||||
$results = $this->db->execute($query, $params);
|
||||
|
||||
// if ($results) {
|
||||
// $database_words = $this->getWords($user, $dictionary);
|
||||
// $database_ids = array_map(function($database_word) { return $database_word['id']; }, $database_words);
|
||||
// $words_to_delete = array_filter($database_ids, function($database_id) use($word_ids) { return !in_array($database_id, $word_ids); });
|
||||
// if ($words_to_delete) {
|
||||
// $delete_results = $this->deleteWords($dictionary, $words_to_delete);
|
||||
// return $delete_results;
|
||||
// }
|
||||
// }
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function deleteWords ($dictionary, $word_ids) {
|
||||
$insert_query = 'INSERT INTO deleted_words (dictionary, word_id, deleted_on) VALUES ';
|
||||
$insert_params = array();
|
||||
$delete_query = 'DELETE FROM words WHERE dictionary=? AND word_id IN (';
|
||||
$delete_params = array($dictionary);
|
||||
foreach($word_ids as $word_id) {
|
||||
$insert_query .= "(?, ?, ?), ";
|
||||
$insert_params[] = $dictionary;
|
||||
$insert_params[] = $word_id;
|
||||
$insert_params[] = time();
|
||||
|
||||
$delete_query .= '?, ';
|
||||
$delete_params[] = $word_id;
|
||||
}
|
||||
|
||||
$insert_query = trim($insert_query, ', ') . ' ON DUPLICATE KEY UPDATE deleted_on=VALUES(deleted_on)';
|
||||
$delete_query = trim($delete_query, ', ') . ')';
|
||||
|
||||
$insert_results = $this->db->execute($insert_query, $insert_params);
|
||||
if ($insert_results) {
|
||||
$delete_results = $this->db->execute($delete_query, $delete_params);
|
||||
return $delete_results;
|
||||
}
|
||||
return $insert_results;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,243 @@
|
|||
<?php
|
||||
require_once('./Db.php');
|
||||
require_once('./Token.php');
|
||||
require_once('./Dictionary.php');
|
||||
|
||||
class User {
|
||||
private $db;
|
||||
private $token;
|
||||
function __construct () {
|
||||
$this->db = new Db();
|
||||
$this->token = new Token();
|
||||
$this->dictionary = new Dictionary();
|
||||
}
|
||||
|
||||
public function logIn ($email, $password) {
|
||||
$query = 'SELECT * FROM users WHERE email=:email';
|
||||
$user = $this->db->query($query, array(':email' => $email))->fetch();
|
||||
if ($user) {
|
||||
if ($user['old_password'] !== null) {
|
||||
if ($user['old_password'] === crypt($password, $email)) {
|
||||
if ($this->upgradePassword($password)) {
|
||||
return $this->logIn($email, $password);
|
||||
}
|
||||
}
|
||||
} else if (password_verify($password, $user['password'])) {
|
||||
$this->db->execute('UPDATE users SET last_login=' . time() . ' WHERE id=' . $user['id']);
|
||||
return array(
|
||||
'token' => $this->generateUserToken($user['id'], $user['current_dictionary']),
|
||||
'user' => $this->getUserData($user['id']),
|
||||
);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function emailExists ($email) {
|
||||
$query = 'SELECT * FROM users WHERE email=?';
|
||||
$user = $this->db->query($query, array($email));
|
||||
return $user->rowCount() > 0;
|
||||
}
|
||||
|
||||
public function create ($email, $password, $user_data) {
|
||||
$insert_user_query = 'INSERT INTO users (email, password, public_name, allow_email, created_on)
|
||||
VALUES (?, ?, ?, ?, ?)';
|
||||
$password_hash = password_hash($password, PASSWORD_DEFAULT);
|
||||
|
||||
$insert_user = $this->db->execute($insert_user_query, array(
|
||||
$email,
|
||||
$password_hash,
|
||||
$user_data['publicName'] !== '' ? $user_data['publicName'] : 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 (isset($new_dictionary['error'])) {
|
||||
return $new_dictionary;
|
||||
} else {
|
||||
return array(
|
||||
'token' => $this->generateUserToken($new_user_id, $new_dictionary),
|
||||
'user' => $this->getUserData($new_user_id),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'error' => '"INSERT INTO users" failed: ' . $this->db->last_error_info[2],
|
||||
);
|
||||
}
|
||||
|
||||
public function setUserData ($token, $user_data) {
|
||||
$token_data = $this->token->decode($token);
|
||||
if ($token_data !== false) {
|
||||
$user_id = $token_data->id;
|
||||
$query = 'UPDATE users SET email=?, public_name=?, allow_email=? WHERE id=?';
|
||||
$properties = array(
|
||||
$user_data['email'],
|
||||
$user_data['publicName'],
|
||||
$user_data['allowEmails'],
|
||||
$user_id,
|
||||
);
|
||||
$update_success = $this->db->execute($query, $properties);
|
||||
if ($update_success) {
|
||||
return array(
|
||||
'token' => $token,
|
||||
'userData' => $user_data,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getUserData ($user_id) {
|
||||
$query = 'SELECT * FROM users WHERE id=?';
|
||||
$stmt = $this->db->query($query, array($user_id));
|
||||
$user = $stmt->fetch();
|
||||
if ($stmt && $user) {
|
||||
return array(
|
||||
'email' => $user['email'],
|
||||
'publicName' => $user['public_name'],
|
||||
'allowEmails' => $user['allow_email'] == 1 ? true : false,
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function createNewDictionary ($token) {
|
||||
$user_data = $this->token->decode($token);
|
||||
if ($user_data !== false) {
|
||||
$id = $user_data->id;
|
||||
$new_dictionary = $this->dictionary->create($id);
|
||||
if (!isset($new_dictionary['error'])) {
|
||||
$new_token = $this->generateUserToken($id, $new_dictionary);
|
||||
return array(
|
||||
'token' => $new_token,
|
||||
'dictionary' => $this->getCurrentDictionary($new_token),
|
||||
);
|
||||
} else {
|
||||
return $new_dictionary;
|
||||
}
|
||||
}
|
||||
return array(
|
||||
'error' => 'invalid token',
|
||||
);
|
||||
}
|
||||
|
||||
public function changeCurrentDictionary ($token, $dictionary_hash) {
|
||||
$user_data = $this->token->decode($token);
|
||||
if ($user_data !== false) {
|
||||
$id = $user_data->id;
|
||||
$dictionary_id = $this->token->unhash($dictionary_hash);
|
||||
if ($dictionary_id !== false) {
|
||||
$changed_dictionary = $this->dictionary->changeCurrent($id, $dictionary_id);
|
||||
if ($changed_dictionary !== false) {
|
||||
return array(
|
||||
'token' => $this->generateUserToken($id, $changed_dictionary),
|
||||
'dictionary' => $this->getCurrentDictionary($token),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function listAllDictionaryNames ($token) {
|
||||
$user_data = $this->token->decode($token);
|
||||
if ($user_data !== false) {
|
||||
$id = $user_data->id;
|
||||
return $this->dictionary->getAllNames($id);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getCurrentDictionary ($token) {
|
||||
$user_data = $this->token->decode($token);
|
||||
if ($user_data !== false) {
|
||||
$user = $user_data->id;
|
||||
$dictionary = $user_data->dictionary;
|
||||
return array(
|
||||
'details' => $this->dictionary->getDetails($user, $dictionary),
|
||||
'words' => $this->dictionary->getWords($user, $dictionary),
|
||||
'deletedWords' => $this->dictionary->getDeletedWords($user, $dictionary),
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function saveWholeCurrentDictionary ($token, $dictionary_data) {
|
||||
$user_data = $this->token->decode($token);
|
||||
if ($user_data !== false) {
|
||||
$user = $user_data->id;
|
||||
$dictionary = $user_data->dictionary;
|
||||
$details_updated = $this->dictionary->setDetails($user, $dictionary, $dictionary_data['details']);
|
||||
$words_updated = $this->dictionary->setWords($dictionary, $dictionary_data['words']);
|
||||
return $details_updated && $words_updated;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function updateCurrentDictionaryDetails ($token, $dictionary_details) {
|
||||
$user_data = $this->token->decode($token);
|
||||
if ($user_data !== false) {
|
||||
$user = $user_data->id;
|
||||
$dictionary = $user_data->dictionary;
|
||||
return $this->dictionary->setDetails($user, $dictionary, $dictionary_details);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function updateOrAddWordsToCurrentDictionary ($token, $words) {
|
||||
// Useful even for just one word
|
||||
$user_data = $this->token->decode($token);
|
||||
if ($user_data !== false) {
|
||||
$dictionary = $user_data->dictionary;
|
||||
$user = $user_data->id;
|
||||
$updated_words = $this->dictionary->setWords($user, $dictionary, $words);
|
||||
if ($updated_words) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function deleteWordsFromCurrentDictionary ($token, $word_ids) {
|
||||
// Useful even for just one word
|
||||
$user_data = $this->token->decode($token);
|
||||
if ($user_data !== false) {
|
||||
$dictionary = $user_data->dictionary;
|
||||
$user = $user_data->id;
|
||||
$deleted_word = $this->dictionary->deleteWords($dictionary, $word_ids);
|
||||
if ($deleted_word) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private function generateUserToken ($user_id, $dictionary_id) {
|
||||
$user_data = array(
|
||||
'id' => intval($user_id),
|
||||
'isMember' => $this->hasMembership($user_id),
|
||||
'dictionary' => intval($dictionary_id),
|
||||
);
|
||||
return $this->token->encode($user_data);
|
||||
}
|
||||
|
||||
private function hasMembership ($id) {
|
||||
$current_membership = "SELECT * FROM memberships WHERE user=$id AND start_date>=CURRENT_TIMESTAMP AND CURRENT_TIMESTAMP<expire_date";
|
||||
return $this->db->query($current_membership)->rowCount() > 0;
|
||||
}
|
||||
|
||||
private function upgradePassword ($password) {
|
||||
$new_password = password_hash($password, PASSWORD_DEFAULT);
|
||||
$update_query = 'UPDATE users SET old_password=NULL, password=? WHERE id=' . $user['id'];
|
||||
$stmt = $this->db->query($update_query, array($new_password));
|
||||
return $stmt->rowCount() === 1;
|
||||
}
|
||||
}
|
|
@ -1,26 +1,291 @@
|
|||
<?php
|
||||
require_once('./Response.php');
|
||||
require_once('./Token.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;
|
||||
|
||||
switch ($action) {
|
||||
case 'login': {
|
||||
if (isset($request['email']) && isset($request['password'])) {
|
||||
$token = new Token();
|
||||
$token = $token->encode([
|
||||
'email' => $request['email'],
|
||||
'password' => $request['password'],
|
||||
]);
|
||||
setcookie('token', $token);
|
||||
return Response::json([
|
||||
'data' => 'cookie saved',
|
||||
'error' => false,
|
||||
], 200);
|
||||
$user = new User();
|
||||
$user_data = $user->logIn($request['email'], $request['password']);
|
||||
if ($user_data !== false) {
|
||||
return Response::json(array(
|
||||
'data' => $user_data,
|
||||
'error' => false,
|
||||
), 200);
|
||||
}
|
||||
return Response::json(array(
|
||||
'data' => 'Could not log in: incorrect data',
|
||||
'error' => true,
|
||||
), 401);
|
||||
}
|
||||
break;
|
||||
return Response::json(array(
|
||||
'data' => 'Could not log in: required information missing',
|
||||
'error' => true,
|
||||
), 400);
|
||||
}
|
||||
case 'create-account': {
|
||||
if (isset($request['email']) && isset($request['password'])) {
|
||||
$user = new User();
|
||||
if (!$user->emailExists($request['email'])) {
|
||||
$user_data = $user->create($request['email'], $request['password'], $request['userData']);
|
||||
if (!isset($user_data['error'])) {
|
||||
return Response::json(array(
|
||||
'data' => $user_data,
|
||||
'error' => false,
|
||||
), 201);
|
||||
}
|
||||
return Response::json(array(
|
||||
'data' => 'Could not create account: ' . $user_data['error'],
|
||||
'error' => true,
|
||||
), 500);
|
||||
}
|
||||
return Response::json(array(
|
||||
'data' => 'Could not create account: duplicate email',
|
||||
'error' => true,
|
||||
), 403);
|
||||
}
|
||||
return Response::json(array(
|
||||
'data' => 'Could not create account: required information missing',
|
||||
'error' => true,
|
||||
), 400);
|
||||
}
|
||||
case 'check-email': {
|
||||
if (isset($request['email'])) {
|
||||
$user = new User();
|
||||
$email_exists = $user->emailExists($request['email']);
|
||||
return Response::json(array(
|
||||
'data' => $email_exists,
|
||||
'error' => false,
|
||||
), 200);
|
||||
}
|
||||
return Response::json(array(
|
||||
'data' => 'Could not check: required information missing',
|
||||
'error' => true,
|
||||
), 400);
|
||||
}
|
||||
case 'check-username': {
|
||||
if (isset($request['username'])) {
|
||||
$user = new User();
|
||||
$username_exists = $user->usernameExists($request['username']);
|
||||
return Response::json(array(
|
||||
'data' => $username_exists,
|
||||
'error' => false,
|
||||
), 200);
|
||||
}
|
||||
return Response::json(array(
|
||||
'data' => 'Could not check: 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);
|
||||
}
|
||||
case 'set-user-data': {
|
||||
if ($token !== false && isset($request['userData'])) {
|
||||
$user = new User();
|
||||
$updated_user = $user->setUserData($token, $request['userData']);
|
||||
if ($updated_user !== false) {
|
||||
return Response::json(array(
|
||||
'data' => $updated_user,
|
||||
'error' => false,
|
||||
), 200);
|
||||
}
|
||||
return Response::json(array(
|
||||
'data' => 'Could not set user data: missing data',
|
||||
'error' => true,
|
||||
), 400);
|
||||
}
|
||||
return Response::json(array(
|
||||
'data' => 'Could not get dictionaries: no token provided',
|
||||
'error' => true,
|
||||
), 403);
|
||||
}
|
||||
case 'create-new-dictionary': {
|
||||
if ($token !== false) {
|
||||
$user = new User();
|
||||
$new_data = $user->createNewDictionary($token);
|
||||
if (!isset($new_data['error'])) {
|
||||
return Response::json(array(
|
||||
'data' => $new_data,
|
||||
'error' => false,
|
||||
), 200);
|
||||
}
|
||||
return Response::json(array(
|
||||
'data' => 'Could not create dictionary: ' . $new_data['error'],
|
||||
'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();
|
||||
$new_data = $user->changeCurrentDictionary($token, $request['dictionary']);
|
||||
if ($new_data !== false) {
|
||||
return Response::json(array(
|
||||
'data' => $new_data,
|
||||
'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 '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);
|
||||
}
|
||||
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',
|
||||
'data' => $update_details_success,
|
||||
'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(
|
||||
'data' => 'Could not set words: invalid token',
|
||||
'error' => true,
|
||||
), 401);
|
||||
}
|
||||
return Response::json(array(
|
||||
'data' => 'Could not set words: required data missing',
|
||||
'error' => true,
|
||||
), 400);
|
||||
}
|
||||
case 'delete-word': {
|
||||
if ($token !== false && isset($request['word'])) {
|
||||
$user = new User();
|
||||
$delete_word_success = $user->deleteWordsFromCurrentDictionary($token, array($request['word']));
|
||||
if ($delete_word_success !== false) {
|
||||
return Response::json(array(
|
||||
'data' => 'Deleted successfully',
|
||||
'error' => false,
|
||||
), 200);
|
||||
}
|
||||
return Response::json(array(
|
||||
'data' => 'Could not delete word: invalid token',
|
||||
'error' => true,
|
||||
), 401);
|
||||
}
|
||||
return Response::json(array(
|
||||
'data' => 'Could not delete word: required data missing',
|
||||
'error' => true,
|
||||
), 400);
|
||||
}
|
||||
case 'delete-words': {
|
||||
if ($token !== false && isset($request['words'])) {
|
||||
$user = new User();
|
||||
$delete_word_success = $user->deleteWordsFromCurrentDictionary($token, $request['words']);
|
||||
if ($delete_word_success !== false) {
|
||||
return Response::json(array(
|
||||
'data' => 'Deleted successfully',
|
||||
'error' => false,
|
||||
), 200);
|
||||
}
|
||||
return Response::json(array(
|
||||
'data' => 'Could not delete words: invalid token',
|
||||
'error' => true,
|
||||
), 401);
|
||||
}
|
||||
return Response::json(array(
|
||||
'data' => 'Could not delete words: required data missing',
|
||||
'error' => true,
|
||||
), 400);
|
||||
}
|
||||
|
||||
default: {
|
||||
return Response::html('Hi!');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue