From 2c3923f03b3c9c256ea6de6b05558d55a2632a74 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Fri, 30 Jan 2026 11:03:44 +0100 Subject: [PATCH] Finish API ? --- src/Endpoints/Checkexistence.php | 4 +- src/Endpoints/Deletefile.php | 7 ++- src/Endpoints/Download.php | 94 ++++++++++++++++++++++++++++++ src/Endpoints/Downloadfilelist.php | 3 + src/Endpoints/Editorfilelist.php | 7 ++- src/Endpoints/Fileexplorer.php | 10 ++-- src/Endpoints/Getfilesize.php | 76 ++++++++++++++++++++++++ src/Endpoints/Markasarchived.php | 7 ++- src/Endpoints/Markasprivate.php | 7 ++- src/Endpoints/Markaspublic.php | 7 ++- src/Endpoints/Uploadchunk.php | 7 ++- src/Library/ArchiveExplorer.php | 73 +++++++++++++++++++++++ src/Library/Data.php | 9 +++ src/Library/Log.php | 22 +++++++ src/RomhackPlazaFS.php | 2 + 15 files changed, 318 insertions(+), 17 deletions(-) create mode 100644 src/Endpoints/Download.php create mode 100644 src/Endpoints/Getfilesize.php create mode 100644 src/Library/ArchiveExplorer.php create mode 100644 src/Library/Log.php diff --git a/src/Endpoints/Checkexistence.php b/src/Endpoints/Checkexistence.php index 36811fd..6be8808 100644 --- a/src/Endpoints/Checkexistence.php +++ b/src/Endpoints/Checkexistence.php @@ -2,6 +2,7 @@ namespace RomhackPlazaFS\Endpoints; use RomhackPlazaFS\Library\Data; +use RomhackPlazaFS\Library\Log; use RomhackPlazaFS\RomhackPlazaFS; defined( 'ROMHACKPLAZA_FS' ) || exit; @@ -14,7 +15,7 @@ class Checkexistence extends Abstract_Endpoint { protected function private_endpoint(): bool { - return false; + return true; } protected function require_token(): bool @@ -50,6 +51,7 @@ class Checkexistence extends Abstract_Endpoint { 'file_exists' => $file_exists, ]; + new Log( __class__, "WP Id=" . $this->token_info['user_id'] ,$this->file_name, $this->post_id, $this->custom_post_type, "File exists=" . $file_exists ); RomhackPlazaFS::success_response( $resp ); } diff --git a/src/Endpoints/Deletefile.php b/src/Endpoints/Deletefile.php index 01a0db2..a0ed8b1 100644 --- a/src/Endpoints/Deletefile.php +++ b/src/Endpoints/Deletefile.php @@ -2,6 +2,7 @@ namespace RomhackPlazaFS\Endpoints; use RomhackPlazaFS\Library\Data; +use RomhackPlazaFS\Library\Log; use RomhackPlazaFS\RomhackPlazaFS; defined( 'ROMHACKPLAZA_FS' ) || exit; @@ -14,12 +15,12 @@ class Deletefile extends Abstract_Endpoint { protected function private_endpoint(): bool { - return false; + return true; } protected function require_token(): bool { - return false; + return true; } protected function get_post_params(): void { @@ -56,6 +57,8 @@ class Deletefile extends Abstract_Endpoint { Data::F_DELETED_FILE ); + new Log( __class__, "WP Id=" . $this->token_info['user_id'] ,$this->file_name, $this->post_id, $this->custom_post_type ); + if( $data->change_data_location( $new_data ) ) RomhackPlazaFS::success_response( [ 'status' => Data::STATUS_DELETED ] ); diff --git a/src/Endpoints/Download.php b/src/Endpoints/Download.php new file mode 100644 index 0000000..1cd6f4b --- /dev/null +++ b/src/Endpoints/Download.php @@ -0,0 +1,94 @@ +post_id = $this->swiffer_get( "entry_id", "int" ); + if( $this->post_id < 1 ) + RomhackPlazaFS::invalid_parameter_stop( "entry_id", "GET" ); + + $this->custom_post_type = $this->swiffer_get( "post_type", "string" ); + if( $this->custom_post_type === "" ) + RomhackPlazaFS::invalid_parameter_stop( "post_type", "GET" ); + + $this->file_name = $this->swiffer_get( "filename", "filename" ); + if( $this->file_name === "" ) + RomhackPlazaFS::invalid_parameter_stop( "filename", "GET" ); + + + $this->is_private = $this->swiffer_get( "is_private", "string" ); + if( $this->is_private !== "no" && $this->is_private !== "yes" ) + $this->is_private = "no"; + + $this->is_archived = $this->swiffer_get( "is_archived", "string" ); + if( $this->is_archived !== "no" && $this->is_archived !== "yes" ) + $this->is_archived = "no"; + + } + + protected function process(): void { + + $flags = 0; + if( $this->is_private === "yes" ) + $flags |= Data::F_PRIVATE_FILE; + else if( $this->is_archived === "yes" ) + $flags |= Data::F_ARCHIVED_FILE; + + $data = Data::setup( + $this->custom_post_type, + $this->post_id, + $this->file_name, + $flags + ); + + if( $_SERVER['HTTP_REFERER'] && !in_array( $_SERVER['HTTP_REFERER'], RomhackPlazaFS::dotenv_array( 'ROMHACKPLAZAFS_DOWNLOAD_ALLOWED_PARENT_DOMAIN' ) ) ) + RomhackPlazaFS::fatal_stop( 403, "You can't access to this ressource." ); + + if( !$data->file_exists() ) + RomhackPlazaFS::fatal_stop( 404, "File not found." ); + + new Log( __class__, $this->file_name, $this->post_id, $this->custom_post_type ); + + $this->download_header( $this->file_name, $data->file_path ); + $data->download(); + exit; + } + + +} \ No newline at end of file diff --git a/src/Endpoints/Downloadfilelist.php b/src/Endpoints/Downloadfilelist.php index ac2eac8..fde90b4 100644 --- a/src/Endpoints/Downloadfilelist.php +++ b/src/Endpoints/Downloadfilelist.php @@ -2,6 +2,7 @@ namespace RomhackPlazaFS\Endpoints; use RomhackPlazaFS\Library\Data; +use RomhackPlazaFS\Library\Log; use RomhackPlazaFS\RomhackPlazaFS; defined( 'ROMHACKPLAZA_FS' ) || exit; @@ -48,6 +49,8 @@ class Downloadfilelist extends Abstract_Endpoint { $response['files'] = $data->list_files( $this->custom_post_type, $this->post_id ); $response['archived'] = $archived->list_files( $this->custom_post_type, $this->post_id ); + new Log( __class__, $this->post_id, $this->custom_post_type ); + RomhackPlazaFS::success_response( $response ); } diff --git a/src/Endpoints/Editorfilelist.php b/src/Endpoints/Editorfilelist.php index be68aeb..0a300d4 100644 --- a/src/Endpoints/Editorfilelist.php +++ b/src/Endpoints/Editorfilelist.php @@ -2,6 +2,7 @@ namespace RomhackPlazaFS\Endpoints; use RomhackPlazaFS\Library\Data; +use RomhackPlazaFS\Library\Log; use RomhackPlazaFS\RomhackPlazaFS; defined( 'ROMHACKPLAZA_FS' ) || exit; @@ -12,12 +13,12 @@ class Editorfilelist extends Abstract_Endpoint { private string $custom_post_type; protected function private_endpoint(): bool { - return false; + return true; } protected function require_token(): bool { - return false; + return true; } protected function get_post_params(): void { @@ -54,6 +55,8 @@ class Editorfilelist extends Abstract_Endpoint { $response['private'] = $private->list_files( $this->custom_post_type, $this->post_id ); $response['archived'] = $archived->list_files( $this->custom_post_type, $this->post_id ); + new Log( __class__, "WP Id=" . $this->token_info['user_id'], $this->post_id, $this->custom_post_type ); + RomhackPlazaFS::success_response( $response ); } diff --git a/src/Endpoints/Fileexplorer.php b/src/Endpoints/Fileexplorer.php index d58f149..97bb709 100644 --- a/src/Endpoints/Fileexplorer.php +++ b/src/Endpoints/Fileexplorer.php @@ -1,7 +1,9 @@ file_exists() ) RomhackPlazaFS::fatal_stop( 404, "File not found." ); + new Log( __class__, "WP Id=" . $this->token_info['user_id'] ,$this->file_name, $this->post_id, $this->custom_post_type ); + $response = []; - $response['files'] = $data->list_files( $this->custom_post_type, $this->post_id ); - $response['private'] = $private->list_files( $this->custom_post_type, $this->post_id ); - $response['archived'] = $archived->list_files( $this->custom_post_type, $this->post_id ); + $response["files"] = new ArchiveExplorer( $data )->open(); RomhackPlazaFS::success_response( $response ); diff --git a/src/Endpoints/Getfilesize.php b/src/Endpoints/Getfilesize.php new file mode 100644 index 0000000..7a735d5 --- /dev/null +++ b/src/Endpoints/Getfilesize.php @@ -0,0 +1,76 @@ +post_id = $this->swiffer_get( "entry_id", "int" ); + if( $this->post_id < 1 ) + RomhackPlazaFS::invalid_parameter_stop( "entry_id", "GET" ); + + $this->custom_post_type = $this->swiffer_get( "post_type", "string" ); + if( $this->custom_post_type === "" ) + RomhackPlazaFS::invalid_parameter_stop( "post_type", "GET" ); + + $this->file_name = $this->swiffer_get( "filename", "filename" ); + if( $this->file_name === "" ) + RomhackPlazaFS::invalid_parameter_stop( "filename", "GET" ); + + $this->is_private = $this->swiffer_get( "is_private", "string" ); + if( $this->is_private !== "no" && $this->is_private !== "yes" ) + $this->is_private = "no"; + + $this->is_archived = $this->swiffer_get( "is_archived", "string" ); + if( $this->is_archived !== "no" && $this->is_archived !== "yes" ) + $this->is_archived = "no"; + + } + + protected function process(): void { + + $flags = 0; + if( $this->is_private === "yes" ) + $flags |= Data::F_PRIVATE_FILE; + else if( $this->is_archived === "yes" ) + $flags |= Data::F_ARCHIVED_FILE; + + $data = Data::setup( + $this->custom_post_type, + $this->post_id, + $this->file_name, + $flags + ); + + if( !$data->file_exists() ) + RomhackPlazaFS::fatal_stop( 404, "File not found." ); + + new Log( __class__,$this->file_name, $this->post_id, $this->custom_post_type ); + + RomhackPlazaFS::success_response( [ 'size' => $data->size() ] ); + } + + +} \ No newline at end of file diff --git a/src/Endpoints/Markasarchived.php b/src/Endpoints/Markasarchived.php index dd6f5a9..629ca08 100644 --- a/src/Endpoints/Markasarchived.php +++ b/src/Endpoints/Markasarchived.php @@ -2,6 +2,7 @@ namespace RomhackPlazaFS\Endpoints; use RomhackPlazaFS\Library\Data; +use RomhackPlazaFS\Library\Log; use RomhackPlazaFS\RomhackPlazaFS; defined( 'ROMHACKPLAZA_FS' ) || exit; @@ -14,12 +15,12 @@ class Markasarchived extends Abstract_Endpoint { protected function private_endpoint(): bool { - return false; + return true; } protected function require_token(): bool { - return false; + return true; } protected function get_post_params(): void { @@ -59,6 +60,8 @@ class Markasarchived extends Abstract_Endpoint { if( $new_data->file_exists() ) RomhackPlazaFS::fatal_stop( 500, "File already exists. You can't archive that have the same name than an other archived file." ); + new Log( __class__, "WP Id=" . $this->token_info['user_id'] ,$this->file_name, $this->post_id, $this->custom_post_type ); + if( $data->change_data_location( $new_data ) ) RomhackPlazaFS::success_response( [ 'status' => Data::STATUS_ARCHIVED ] ); diff --git a/src/Endpoints/Markasprivate.php b/src/Endpoints/Markasprivate.php index eb68792..13c8221 100644 --- a/src/Endpoints/Markasprivate.php +++ b/src/Endpoints/Markasprivate.php @@ -2,6 +2,7 @@ namespace RomhackPlazaFS\Endpoints; use RomhackPlazaFS\Library\Data; +use RomhackPlazaFS\Library\Log; use RomhackPlazaFS\RomhackPlazaFS; defined( 'ROMHACKPLAZA_FS' ) || exit; @@ -14,12 +15,12 @@ class Markasprivate extends Abstract_Endpoint { protected function private_endpoint(): bool { - return false; + return true; } protected function require_token(): bool { - return false; + return true; } protected function get_post_params(): void { @@ -59,6 +60,8 @@ class Markasprivate extends Abstract_Endpoint { if( $new_data->file_exists() ) RomhackPlazaFS::fatal_stop( 500, "File already exists. Can't replace. Please delete private file before." ); + new Log( __class__, "WP Id=" . $this->token_info['user_id'] ,$this->file_name, $this->post_id, $this->custom_post_type ); + if( $data->change_data_location( $new_data ) ) RomhackPlazaFS::success_response( [ 'status' => Data::STATUS_PRIVATE ] ); diff --git a/src/Endpoints/Markaspublic.php b/src/Endpoints/Markaspublic.php index 2e461ee..00cd167 100644 --- a/src/Endpoints/Markaspublic.php +++ b/src/Endpoints/Markaspublic.php @@ -2,6 +2,7 @@ namespace RomhackPlazaFS\Endpoints; use RomhackPlazaFS\Library\Data; +use RomhackPlazaFS\Library\Log; use RomhackPlazaFS\RomhackPlazaFS; defined( 'ROMHACKPLAZA_FS' ) || exit; @@ -14,12 +15,12 @@ class Markaspublic extends Abstract_Endpoint { protected function private_endpoint(): bool { - return false; + return true; } protected function require_token(): bool { - return false; + return true; } protected function get_post_params(): void { @@ -62,6 +63,8 @@ class Markaspublic extends Abstract_Endpoint { if( $data->change_data_location( $new_data ) ) RomhackPlazaFS::success_response( [ 'status' => Data::STATUS_PUBLISH ] ); + new Log( __class__, "WP Id=" . $this->token_info['user_id'] ,$this->file_name, $this->post_id, $this->custom_post_type ); + RomhackPlazaFS::fatal_stop( 500, "Error during file marking" ); } diff --git a/src/Endpoints/Uploadchunk.php b/src/Endpoints/Uploadchunk.php index 111ff48..b794e22 100644 --- a/src/Endpoints/Uploadchunk.php +++ b/src/Endpoints/Uploadchunk.php @@ -2,6 +2,7 @@ namespace RomhackPlazaFS\Endpoints; use RomhackPlazaFS\Library\Data; +use RomhackPlazaFS\Library\Log; use RomhackPlazaFS\RomhackPlazaFS; defined( 'ROMHACKPLAZA_FS' ) || exit; @@ -16,12 +17,12 @@ class Uploadchunk extends Abstract_Endpoint { protected function private_endpoint(): bool { - return false; + return true; } protected function require_token(): bool { - return false; + return true; } protected function get_post_params(): void { @@ -55,6 +56,8 @@ class Uploadchunk extends Abstract_Endpoint { $data->file_path_to_temp(); + new Log( __class__, "WP Id=" . $this->token_info['user_id'] ,$this->file_name, $this->post_id, $this->custom_post_type, $this->current_chunk + 1 . "/" . $this->total_chunks ); + if( $data->write( $this->current_chunk, $this->total_chunks ) ) RomhackPlazaFS::success_response( [ 'chunk' => $this->current_chunk, 'total_chunks' => $this->total_chunks, 'uploaded' => true ] ); diff --git a/src/Library/ArchiveExplorer.php b/src/Library/ArchiveExplorer.php new file mode 100644 index 0000000..5888431 --- /dev/null +++ b/src/Library/ArchiveExplorer.php @@ -0,0 +1,73 @@ +data = &$data; + } + + private function check_archive(): string|bool { + + if( !$this->data->file_exists() ) + return false; + + return match (mime_content_type($this->data->file_path)) { + 'application/zip' => "zip", + 'application/x-rar' => "rar", + 'application/vnd.rar' => "rar", + 'application/x-7z-compressed' => "7z", + default => false, + }; + + } + + public function open(): array { + + $file_format = $this->check_archive(); + if( !$file_format ) + RomhackPlazaFS::fatal_stop( 400, "Not an archive." ); + + $files = []; + + if( $file_format === "zip" ){ + + $archive = new \ZipArchive(); + $archive->open( $this->data->file_path ); + + for( $i = 0; $i < $archive->numFiles; $i++ ) + $files[] = $archive->statIndex($i)["name"]; + $archive->close(); + + } + else if( $file_format === "rar" ){ + + $cmd = 'unrar lb "' . $this->data->file_path . '"'; + $out = shell_exec( $cmd ); + $files = explode("\n", $out); + + } + else if( $file_format === "7z" ){ + + require_once $_ENV['ROMHACKPLAZAFS_FILE_PATH'] . 'vendor/SevenZipArchive/SevenZipArchive.php'; + $archive = new \SevenZipArchive( $this->data->file_path ); + + foreach ( $archive as $entry ) + $files[] = $entry["Name"]; + + } + + return $files; + + } + +} \ No newline at end of file diff --git a/src/Library/Data.php b/src/Library/Data.php index 4de0074..3541657 100644 --- a/src/Library/Data.php +++ b/src/Library/Data.php @@ -266,4 +266,13 @@ final class Data { } + public function download(){ + + if( ob_get_level() ) + ob_end_clean(); + + readfile( $this->file_path ); + + } + } \ No newline at end of file diff --git a/src/Library/Log.php b/src/Library/Log.php new file mode 100644 index 0000000..c5ced06 --- /dev/null +++ b/src/Library/Log.php @@ -0,0 +1,22 @@ +