|
|
@ -1,4 +1,5 @@
|
|
|
|
<?php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once "gateway/user_gateway.php";
|
|
|
|
require_once "gateway/user_gateway.php";
|
|
|
|
require_once "gateway/file_gateway.php";
|
|
|
|
require_once "gateway/file_gateway.php";
|
|
|
@ -26,9 +27,9 @@ return function (App $app) {
|
|
|
|
$app->add(function ($request, $handler) {
|
|
|
|
$app->add(function ($request, $handler) {
|
|
|
|
$response = $handler->handle($request);
|
|
|
|
$response = $handler->handle($request);
|
|
|
|
return $response
|
|
|
|
return $response
|
|
|
|
->withHeader('Access-Control-Allow-Origin', '*')
|
|
|
|
->withHeader('Access-Control-Allow-Origin', '*')
|
|
|
|
->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
|
|
|
|
->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
|
|
|
|
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
|
|
|
|
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
$app->get('/', function (Request $req, Response $res) {
|
|
|
|
$app->get('/', function (Request $req, Response $res) {
|
|
|
@ -40,11 +41,11 @@ return function (App $app) {
|
|
|
|
// Create User
|
|
|
|
// Create User
|
|
|
|
$app->post('/user', function (Request $req, Response $res) {
|
|
|
|
$app->post('/user', function (Request $req, Response $res) {
|
|
|
|
$req_body = $req->getParsedBody();
|
|
|
|
$req_body = $req->getParsedBody();
|
|
|
|
if(!array_key_exists('email',$req_body) || !array_key_exists('hash', $req_body) || !array_key_exists('username', $req_body)) {
|
|
|
|
if (!array_key_exists('email', $req_body) || !array_key_exists('hash', $req_body) || !array_key_exists('username', $req_body)) {
|
|
|
|
return $res->withStatus(400);
|
|
|
|
return $res->withStatus(400);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$code = (new UserGateway)->createUser($req_body['email'], $req_body['hash'], $req_body['username']);
|
|
|
|
$code = (new UserGateway)->createUser($req_body['email'], $req_body['hash'], $req_body['username']);
|
|
|
|
if($code === -1) return $res->withStatus(409);
|
|
|
|
if ($code === -1) return $res->withStatus(409);
|
|
|
|
|
|
|
|
|
|
|
|
$res->getBody()->write(json_encode($code));
|
|
|
|
$res->getBody()->write(json_encode($code));
|
|
|
|
return $res;
|
|
|
|
return $res;
|
|
|
@ -53,14 +54,14 @@ return function (App $app) {
|
|
|
|
// Delete User
|
|
|
|
// Delete User
|
|
|
|
$app->delete('/user', function (Request $req, Response $res) {
|
|
|
|
$app->delete('/user', function (Request $req, Response $res) {
|
|
|
|
$token = $req->getHeader('Authorization')[0];
|
|
|
|
$token = $req->getHeader('Authorization')[0];
|
|
|
|
if(!(new Token)->verifyToken($token)) {
|
|
|
|
if (!(new Token)->verifyToken($token)) {
|
|
|
|
return $res->withStatus(401);
|
|
|
|
return $res->withStatus(401);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$uuid = (new Token)->getUuidFromToken($token);
|
|
|
|
$uuid = (new Token)->getUuidFromToken($token);
|
|
|
|
$code = (new UserGateway)->deleteUser($uuid);
|
|
|
|
$code = (new UserGateway)->deleteUser($uuid);
|
|
|
|
|
|
|
|
|
|
|
|
switch($code) {
|
|
|
|
switch ($code) {
|
|
|
|
case 0:
|
|
|
|
case 0:
|
|
|
|
return $res->withStatus(200);
|
|
|
|
return $res->withStatus(200);
|
|
|
|
case -1:
|
|
|
|
case -1:
|
|
|
@ -77,7 +78,7 @@ return function (App $app) {
|
|
|
|
$hash = $args['hash'];
|
|
|
|
$hash = $args['hash'];
|
|
|
|
|
|
|
|
|
|
|
|
$value = (new UserGateway)->login($email, $hash);
|
|
|
|
$value = (new UserGateway)->login($email, $hash);
|
|
|
|
switch($value) {
|
|
|
|
switch ($value) {
|
|
|
|
case -1:
|
|
|
|
case -1:
|
|
|
|
return $res->withStatus(404);
|
|
|
|
return $res->withStatus(404);
|
|
|
|
case -2:
|
|
|
|
case -2:
|
|
|
@ -90,15 +91,15 @@ return function (App $app) {
|
|
|
|
return $res;
|
|
|
|
return $res;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
$app->get('/user/info', function(Request $req, Response $res) {
|
|
|
|
$app->get('/user/info', function (Request $req, Response $res) {
|
|
|
|
$token = $req->getHeader('Authorization')[0];
|
|
|
|
$token = $req->getHeader('Authorization')[0];
|
|
|
|
if(!(new Token)->verifyToken($token)) {
|
|
|
|
if (!(new Token)->verifyToken($token)) {
|
|
|
|
return $res->withStatus(401);
|
|
|
|
return $res->withStatus(401);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$uuid = (new Token)->getUuidFromToken($token);
|
|
|
|
$uuid = (new Token)->getUuidFromToken($token);
|
|
|
|
$code = (new UserGateway)->getInfo($uuid);
|
|
|
|
$code = (new UserGateway)->getInfo($uuid);
|
|
|
|
switch($code) {
|
|
|
|
switch ($code) {
|
|
|
|
case -1:
|
|
|
|
case -1:
|
|
|
|
return $res->withStatus(404);
|
|
|
|
return $res->withStatus(404);
|
|
|
|
case -2:
|
|
|
|
case -2:
|
|
|
@ -110,32 +111,32 @@ return function (App $app) {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Update Mail
|
|
|
|
// Update Mail
|
|
|
|
$app->put('/user/email', function(Request $req, Response $res) {
|
|
|
|
$app->put('/user/email', function (Request $req, Response $res) {
|
|
|
|
$token = $req->getHeader('Authorization')[0];
|
|
|
|
$token = $req->getHeader('Authorization')[0];
|
|
|
|
if(!(new Token)->verifyToken($token)) {
|
|
|
|
if (!(new Token)->verifyToken($token)) {
|
|
|
|
return $res->withStatus(401);
|
|
|
|
return $res->withStatus(401);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$body = $req->getParsedBody();
|
|
|
|
$body = $req->getParsedBody();
|
|
|
|
if(!isset($body['email'])) {
|
|
|
|
if (!isset($body['email'])) {
|
|
|
|
return $res->withStatus(400);
|
|
|
|
return $res->withStatus(400);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$new_email = $req->getParsedBody()['email'];
|
|
|
|
$new_email = $req->getParsedBody()['email'];
|
|
|
|
|
|
|
|
|
|
|
|
$uuid = (new Token)->getUuidFromToken($token);
|
|
|
|
$uuid = (new Token)->getUuidFromToken($token);
|
|
|
|
$code = (new UserGateway)->updateMail($uuid, $new_email);
|
|
|
|
$code = (new UserGateway)->updateMail($uuid, $new_email);
|
|
|
|
if($code === -1) return $res->withStatus(500);
|
|
|
|
if ($code === -1) return $res->withStatus(500);
|
|
|
|
return $res->withStatus(200);
|
|
|
|
return $res->withStatus(200);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Update Username
|
|
|
|
// Update Username
|
|
|
|
$app->put('/user/username', function(Request $req, Response $res) {
|
|
|
|
$app->put('/user/username', function (Request $req, Response $res) {
|
|
|
|
$token = $req->getHeader('Authorization')[0];
|
|
|
|
$token = $req->getHeader('Authorization')[0];
|
|
|
|
if(!(new Token)->verifyToken($token)){
|
|
|
|
if (!(new Token)->verifyToken($token)) {
|
|
|
|
return $res->withStatus(401);
|
|
|
|
return $res->withStatus(401);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$body = $req->getParsedBody();
|
|
|
|
$body = $req->getParsedBody();
|
|
|
|
if(!isset($body['username'])) {
|
|
|
|
if (!isset($body['username'])) {
|
|
|
|
return $res->withStatus(400);
|
|
|
|
return $res->withStatus(400);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$new_username = $req->getParsedBody()['username'];
|
|
|
|
$new_username = $req->getParsedBody()['username'];
|
|
|
@ -143,7 +144,7 @@ return function (App $app) {
|
|
|
|
|
|
|
|
|
|
|
|
$uuid = (new Token)->getUuidFromToken($token);
|
|
|
|
$uuid = (new Token)->getUuidFromToken($token);
|
|
|
|
$code = (new UserGateway)->updateUsername($uuid, $new_username);
|
|
|
|
$code = (new UserGateway)->updateUsername($uuid, $new_username);
|
|
|
|
if($code === -1) return $res->withStatus(500);
|
|
|
|
if ($code === -1) return $res->withStatus(500);
|
|
|
|
return $res->withStatus(200);
|
|
|
|
return $res->withStatus(200);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
@ -152,13 +153,13 @@ return function (App $app) {
|
|
|
|
$app->get('/user/files', function (Request $req, Response $res) {
|
|
|
|
$app->get('/user/files', function (Request $req, Response $res) {
|
|
|
|
$token = $req->getHeader('Authorization')[0];
|
|
|
|
$token = $req->getHeader('Authorization')[0];
|
|
|
|
$save_folder = '/home/hel/smartfit_hdd';
|
|
|
|
$save_folder = '/home/hel/smartfit_hdd';
|
|
|
|
if(!(new Token)->verifyToken($token)) {
|
|
|
|
if (!(new Token)->verifyToken($token)) {
|
|
|
|
return $res->withStatus(401);
|
|
|
|
return $res->withStatus(401);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$uuid = (new Token)->getUuidFromToken($token);
|
|
|
|
$uuid = (new Token)->getUuidFromToken($token);
|
|
|
|
$code = (new FileGateway)->listFiles($uuid);
|
|
|
|
$code = (new FileGateway)->listFiles($uuid);
|
|
|
|
if($code === -1) return $res->withStatus(500);
|
|
|
|
if ($code === -1) return $res->withStatus(500);
|
|
|
|
$res->getBody()->write(json_encode($code));
|
|
|
|
$res->getBody()->write(json_encode($code));
|
|
|
|
return $res;
|
|
|
|
return $res;
|
|
|
|
});
|
|
|
|
});
|
|
|
@ -168,20 +169,20 @@ return function (App $app) {
|
|
|
|
$token = $req->getHeader('Authorization')[0];
|
|
|
|
$token = $req->getHeader('Authorization')[0];
|
|
|
|
$file_uuid = $args['uuid'];
|
|
|
|
$file_uuid = $args['uuid'];
|
|
|
|
$save_folder = '/home/hel/smartfit_hdd';
|
|
|
|
$save_folder = '/home/hel/smartfit_hdd';
|
|
|
|
if(!(new Token)->verifyToken($token)) {
|
|
|
|
if (!(new Token)->verifyToken($token)) {
|
|
|
|
return $res->withStatus(401);
|
|
|
|
return $res->withStatus(401);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$user_uuid = (new Token)->getUuidFromToken($token);
|
|
|
|
$user_uuid = (new Token)->getUuidFromToken($token);
|
|
|
|
$filename = (new FileGateway)->getFilename($file_uuid, $user_uuid);
|
|
|
|
$filename = (new FileGateway)->getFilename($file_uuid, $user_uuid);
|
|
|
|
switch($filename) {
|
|
|
|
switch ($filename) {
|
|
|
|
case -1:
|
|
|
|
case -1:
|
|
|
|
return $res->withStatus(500);
|
|
|
|
return $res->withStatus(500);
|
|
|
|
case -2:
|
|
|
|
case -2:
|
|
|
|
return $res->withStatus(404);
|
|
|
|
return $res->withStatus(404);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$download_file = fopen($save_folder.'/'.$user_uuid.'/'.$filename, 'r');
|
|
|
|
$download_file = fopen($save_folder . '/' . $user_uuid . '/' . $filename, 'r');
|
|
|
|
$res->getBody()->write(fread($download_file, (int)fstat($download_file)['size']));
|
|
|
|
$res->getBody()->write(fread($download_file, (int)fstat($download_file)['size']));
|
|
|
|
return $res;
|
|
|
|
return $res;
|
|
|
|
});
|
|
|
|
});
|
|
|
@ -191,23 +192,23 @@ return function (App $app) {
|
|
|
|
$token = $req->getHeader('Authorization')[0];
|
|
|
|
$token = $req->getHeader('Authorization')[0];
|
|
|
|
$file_uuid = $args['uuid'];
|
|
|
|
$file_uuid = $args['uuid'];
|
|
|
|
$save_folder = '/home/hel/smartfit_hdd';
|
|
|
|
$save_folder = '/home/hel/smartfit_hdd';
|
|
|
|
if(!(new Token)->verifyToken($token)) {
|
|
|
|
if (!(new Token)->verifyToken($token)) {
|
|
|
|
return $res->withStatus(401);
|
|
|
|
return $res->withStatus(401);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$user_uuid = (new Token)->getUuidFromToken($token);
|
|
|
|
$user_uuid = (new Token)->getUuidFromToken($token);
|
|
|
|
$filename = (new FileGateway)->getFilename($file_uuid, $user_uuid);
|
|
|
|
$filename = (new FileGateway)->getFilename($file_uuid, $user_uuid);
|
|
|
|
switch($filename) {
|
|
|
|
switch ($filename) {
|
|
|
|
case -1:
|
|
|
|
case -1:
|
|
|
|
return $res->withStatus(500);
|
|
|
|
return $res->withStatus(500);
|
|
|
|
case -2:
|
|
|
|
case -2:
|
|
|
|
return $res->withStatus(404);
|
|
|
|
return $res->withStatus(404);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$code = (new FileGateway)->deleteFile($file_uuid, $user_uuid);
|
|
|
|
$code = (new FileGateway)->deleteFile($file_uuid, $user_uuid);
|
|
|
|
if($code === -1) return $res->withStatus(500);
|
|
|
|
if ($code === -1) return $res->withStatus(500);
|
|
|
|
|
|
|
|
|
|
|
|
$file_path = $save_folder.'/'.$user_uuid.'/'.$filename;
|
|
|
|
$file_path = $save_folder . '/' . $user_uuid . '/' . $filename;
|
|
|
|
if(file_exists($file_path)) {
|
|
|
|
if (file_exists($file_path)) {
|
|
|
|
unlink($file_path);
|
|
|
|
unlink($file_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -219,7 +220,7 @@ return function (App $app) {
|
|
|
|
$app->post('/user/files', function (Request $req, Response $res) {
|
|
|
|
$app->post('/user/files', function (Request $req, Response $res) {
|
|
|
|
$token = $req->getHeader('Authorization')[0];
|
|
|
|
$token = $req->getHeader('Authorization')[0];
|
|
|
|
$save_folder = '/home/hel/smartfit_hdd';
|
|
|
|
$save_folder = '/home/hel/smartfit_hdd';
|
|
|
|
if(!(new Token)->verifyToken($token)) {
|
|
|
|
if (!(new Token)->verifyToken($token)) {
|
|
|
|
return $res->withStatus(401);
|
|
|
|
return $res->withStatus(401);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -230,16 +231,16 @@ return function (App $app) {
|
|
|
|
$filename = $file->getClientFilename();
|
|
|
|
$filename = $file->getClientFilename();
|
|
|
|
|
|
|
|
|
|
|
|
$code = (new FileGateway)->listFiles($uuid);
|
|
|
|
$code = (new FileGateway)->listFiles($uuid);
|
|
|
|
if(array_search($filename, array_column($code, 'filename'), false) !== false) return $res->withStatus(409);
|
|
|
|
if (array_search($filename, array_column($code, 'filename'), false) !== false) return $res->withStatus(409);
|
|
|
|
|
|
|
|
|
|
|
|
$file_save_folder = $save_folder.'/'.$uuid.'/';
|
|
|
|
$file_save_folder = $save_folder . '/' . $uuid . '/';
|
|
|
|
if(!is_dir($file_save_folder)) {
|
|
|
|
if (!is_dir($file_save_folder)) {
|
|
|
|
mkdir($file_save_folder, 0777, false);
|
|
|
|
mkdir($file_save_folder, 0777, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$file->moveTo($file_save_folder.'/'.$filename);
|
|
|
|
$file->moveTo($file_save_folder . '/' . $filename);
|
|
|
|
|
|
|
|
|
|
|
|
$code = (new FileGateway)->createFile($filename, $uuid, $category, $creation_date);
|
|
|
|
$code = (new FileGateway)->createFile($filename, $uuid, $category, $creation_date);
|
|
|
|
if($code === -1) return $res->withStatus(500);
|
|
|
|
if ($code === -1) return $res->withStatus(500);
|
|
|
|
|
|
|
|
|
|
|
|
return $res->withStatus(200);
|
|
|
|
return $res->withStatus(200);
|
|
|
|
});
|
|
|
|
});
|
|
|
|