2019-05-13 17:18:31 -06:00
< ? php
require_once ( './Response.php' );
2019-05-14 15:28:42 -06:00
require_once ( './User.php' );
2019-05-13 17:18:31 -06:00
$inputJSON = file_get_contents ( 'php://input' );
2019-05-21 16:18:44 -06:00
$inputJSON = strip_tags ( $inputJSON );
2019-05-13 17:18:31 -06:00
$request = json_decode ( $inputJSON , true );
2019-05-21 16:18:44 -06:00
if ( ! $request ) {
// If malformed/unparseable JSON, fail.
return Response :: json ( array (
'data' => 'Malformed request data' ,
'error' => true ,
), 400 );
}
2019-05-13 17:18:31 -06:00
$action = isset ( $request [ 'action' ]) ? $request [ 'action' ] : '' ;
2019-05-14 15:49:54 -06:00
$token = isset ( $_COOKIE [ 'token' ]) ? $_COOKIE [ 'token' ] : false ;
2019-05-13 17:18:31 -06:00
switch ( $action ) {
2019-05-23 16:06:16 -06:00
case 'validate-token' : {
if ( $token !== false ) {
$user = new User ();
$user_data = $user -> validateToken ( $token );
if ( $user_data !== false ) {
return Response :: json ( array (
'data' => $user_data ,
'error' => false ,
), 200 );
}
return Response :: json ( array (
'data' => 'Could not validate token: incorrect data' ,
'error' => true ,
), 401 );
}
return Response :: json ( array (
'data' => 'Could not validate token: required information missing' ,
'error' => true ,
), 400 );
}
2019-05-13 17:18:31 -06:00
case 'login' : {
2019-05-29 16:35:51 -06:00
session_start ();
if ( isset ( $_SESSION [ 'unlock' ])) {
if ( time () < $_SESSION [ 'unlock' ]) {
$seconds_left = ( $_SESSION [ 'unlock' ] - time ());
$minutes_left = floor ( $seconds_left / 60 );
$seconds_left = $seconds_left % 60 ;
return Response :: json ( array (
'data' => 'Too many failed login attempts. You must wait another '
. ( $minutes_left > 0 ? $minutes_left . ' minutes ' : '' )
. ( $minutes_left > 0 && $seconds_left > 0 ? 'and ' : '' )
. ( $seconds_left > 0 ? $seconds_left . ' seconds ' : '' )
. 'until you can log in again.' ,
'error' => true ,
), 403 );
} else {
unset ( $_SESSION [ 'failures' ]);
unset ( $_SESSION [ 'unlock' ]);
}
}
2019-05-13 17:18:31 -06:00
if ( isset ( $request [ 'email' ]) && isset ( $request [ 'password' ])) {
2019-05-14 15:28:42 -06:00
$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 );
}
2019-05-29 16:35:51 -06:00
if ( ! isset ( $_SESSION [ 'failures' ])) {
$_SESSION [ 'failures' ] = 0 ;
}
$_SESSION [ 'failures' ] ++ ;
if ( $_SESSION [ 'failures' ] >= LOGIN_FAILURES_ALLOWED ) {
$_SESSION [ 'unlock' ] = time () + ( LOGIN_FAILURES_LOCKOUT_MINUTES * 60 );
return Response :: json ( array (
'data' => 'Too many failed login attempts. You must wait ' . LOGIN_FAILURES_LOCKOUT_MINUTES . ' minutes until you can log in again.' ,
'error' => true ,
), 403 );
}
2019-05-14 15:28:42 -06:00
return Response :: json ( array (
2019-05-29 16:35:51 -06:00
'data' => 'Incorrect email or password.<br>After ' . ( LOGIN_FAILURES_ALLOWED - $_SESSION [ 'failures' ]) . ' more failures, you will be locked out for ' . LOGIN_FAILURES_LOCKOUT_MINUTES . ' minutes.' ,
2019-05-14 15:28:42 -06:00
'error' => true ,
), 401 );
}
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 ,
2019-05-13 17:18:31 -06:00
'error' => false ,
2019-05-14 15:28:42 -06:00
), 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' ]);
2019-05-23 18:09:01 -06:00
if ( $updated_user === true ) {
2019-05-14 15:28:42 -06:00
return Response :: json ( array (
'data' => $updated_user ,
'error' => false ,
), 200 );
}
2019-05-23 18:09:01 -06:00
if ( isset ( $updated_user [ 'error' ])) {
return Response :: json ( array (
'data' => $updated_user [ 'error' ],
'error' => false ,
), 500 );
}
2019-05-14 15:28:42 -06:00
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 (
2019-05-23 17:00:13 -06:00
'data' => 'Could not change dictionary: incorrect data' ,
2019-05-14 15:28:42 -06:00
'error' => true ,
), 401 );
}
return Response :: json ( array (
2019-05-23 17:00:13 -06:00
'data' => 'Could not change dictionary: no token provided' ,
2019-05-14 15:28:42 -06:00
'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 );
}
2019-05-24 18:50:31 -06:00
case 'get-public-dictionary' : {
if ( isset ( $request [ 'dictionary' ])) {
$dictionary = new Dictionary ();
$dictionary_data = $dictionary -> getPublicDictionaryDetails ( $request [ 'dictionary' ]);
if ( $dictionary_data !== false ) {
$dictionary_data [ 'words' ] = $dictionary -> getPublicDictionaryWords ( $request [ 'dictionary' ]);
return Response :: json ( array (
'data' => $dictionary_data ,
'error' => false ,
), 200 );
}
return Response :: json ( array (
'data' => 'Could not get dictionary: invalid id' ,
'error' => true ,
), 401 );
}
return Response :: json ( array (
'data' => 'Could not get dictionary: no id provided' ,
'error' => true ,
), 400 );
}
2019-05-14 15:28:42 -06:00
case 'set-whole-current-dictionary' : {
if ( $token !== false && isset ( $request [ 'dictionary' ])) {
$user = new User ();
$dictionary_data = $user -> saveWholeCurrentDictionary ( $token , $request [ 'dictionary' ]);
2019-05-21 16:29:05 -06:00
if ( $dictionary_data !== false && ! isset ( $dictionary_data [ 'error' ])) {
2019-05-14 15:28:42 -06:00
return Response :: json ( array (
2019-05-21 16:29:05 -06:00
'data' => $dictionary_data ,
2019-05-14 15:28:42 -06:00
'error' => false ,
), 200 );
}
2019-05-21 16:29:05 -06:00
if ( isset ( $dictionary_data [ 'error' ])) {
return Response :: json ( array (
'data' => $dictionary_data [ 'message' ],
'error' => true ,
), 500 );
}
2019-05-14 15:28:42 -06:00
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 );
}
2019-05-23 19:56:45 -06:00
case 'delete-current-dictionary' : {
if ( $token !== false ) {
$user = new User ();
$dictionary_deleted = $user -> deleteCurrentDictionary ( $token );
if ( $dictionary_deleted !== false ) {
return Response :: json ( array (
'data' => $dictionary_deleted ,
'error' => false ,
), 200 );
}
return Response :: json ( array (
'data' => 'Could not delete dictionary: invalid token' ,
'error' => true ,
), 401 );
}
return Response :: json ( array (
'data' => 'Could not delete dictionary: no token provided' ,
'error' => true ,
), 400 );
}
2019-05-14 15:28:42 -06:00
case 'set-dictionary-details' : {
if ( $token !== false && isset ( $request [ 'details' ])) {
$user = new User ();
$update_details_success = $user -> updateCurrentDictionaryDetails ( $token , $request [ 'details' ]);
2019-05-21 18:54:19 -06:00
if ( $update_details_success === true ) {
2019-05-14 15:28:42 -06:00
return Response :: json ( array (
// 'data' => 'Updated successfully',
'data' => $update_details_success ,
'error' => false ,
), 200 );
}
2019-05-21 18:54:19 -06:00
if ( isset ( $update_details_success [ 'error' ])) {
return Response :: json ( array (
'data' => $update_details_success [ 'error' ],
'error' => true ,
), 500 );
}
2019-05-14 15:28:42 -06:00
return Response :: json ( array (
'data' => 'Could not set dictionary: invalid token' ,
'error' => true ,
), 401 );
2019-05-13 17:18:31 -06:00
}
2019-05-14 15:28:42 -06:00
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' ]);
2019-05-22 14:21:42 -06:00
if ( $update_words_success === true ) {
2019-05-14 15:28:42 -06:00
return Response :: json ( array (
2019-05-22 14:21:42 -06:00
'data' => $update_words_success ,
2019-05-14 15:28:42 -06:00
'error' => false ,
), 200 );
}
2019-05-22 14:21:42 -06:00
if ( isset ( $update_words_success [ 'error' ])) {
return Response :: json ( array (
'data' => $update_words_success [ 'error' ],
'error' => true ,
), 500 );
}
2019-05-14 15:28:42 -06:00
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' : {
2019-05-23 15:07:31 -06:00
if ( $token !== false && isset ( $request [ 'wordIds' ])) {
2019-05-14 15:28:42 -06:00
$user = new User ();
2019-05-23 15:07:31 -06:00
$delete_words_success = $user -> deleteWordsFromCurrentDictionary ( $token , $request [ 'wordIds' ]);
if ( $delete_words_success === true ) {
2019-05-14 15:28:42 -06:00
return Response :: json ( array (
2019-05-23 15:07:31 -06:00
'data' => $delete_words_success ,
2019-05-14 15:28:42 -06:00
'error' => false ,
), 200 );
}
2019-05-23 15:07:31 -06:00
if ( isset ( $delete_words_success [ 'error' ])) {
return Response :: json ( array (
'data' => $delete_words_success [ 'error' ],
'error' => true ,
), 500 );
}
2019-05-14 15:28:42 -06:00
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!' );
2019-05-13 17:18:31 -06:00
}
2019-05-14 15:28:42 -06:00
}