pub / rememori

Simple file-based bookmarking and notes application
git clone https://src.jayvii.de/pub/rememori.git
Home | Log | Files | Exports | Refs | README | RSS

commit b5f31c237359cf64e89fd3885aed6ef5218bd88c
parent e4b25d5046231bc0b218e6db9dea740c3ab097b5
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Tue, 29 Oct 2024 10:18:47 +0100

feat: option to return api message as HTML

Diffstat:
Mapi/new.php | 40++++++++++++++--------------------------
Alib/api_new.php | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mlib/bookmarklet.php | 1+
3 files changed, 116 insertions(+), 26 deletions(-)

diff --git a/api/new.php b/api/new.php @@ -14,6 +14,7 @@ $category = validate_input_string(gather_post("category")); $filename = validate_input_string(gather_post("filename")); $content = gather_post("content"); + $return = gather_post("return"); $user = validate_input_string(gather_post("user")); $token = validate_input_string(gather_post("token")); @@ -21,6 +22,9 @@ if (empty($category)) { $category = validate_input_string(gather_get("category")); } + if (empty($return)) { + $return = validate_input_string(gather_get("return")); + } if (empty($filename)) { $filename = validate_input_string(gather_get("filename")); } @@ -34,24 +38,17 @@ $token = validate_input_string(gather_get("token")); } - /* always output JSON */ - header('Content-Type: application/json'); + /* if category is unset, set it! */ + if (empty($category)) { + $category = "unknown"; + } /* run authentification method. exit immediately if it fails */ $auth = auth_user($user, $token, -1); if ($auth !== true) { http_response_code(401); // unauthorized - exit( - "{" . - "\"status\": \"error\"," . - "\"message\": \"You could not be authenticated!\"" . - "}" - ); - } - - /* if category is unset, set it! */ - if (empty($category)) { - $category = "unknown"; + api_new_error($return, "You could not be authenticated"); + exit(1); } /* if filename is empty, figure it out */ @@ -82,21 +79,12 @@ if ($written === true) { http_response_code(200); // successful - exit( - "{" . - "\"status\": \"success\"," . - "\"message\": \"Note written to " . - $user . "/" . $category . "/" . $filename . "\"" . - "}" - ); + api_new_success($return, $user . "/" . $category . "/" . $filename); + exit(0); } else { http_response_code(500); // internal server error - exit( - "{" . - "\"status\": \"error\"," . - "\"message\": \"File could not be written for unknown reasons\"" . - "}" - ); + api_new_error($return, "Note could not be created for unknown reasons"); + exit(1); } ?> diff --git a/lib/api_new.php b/lib/api_new.php @@ -0,0 +1,101 @@ +<?php + +function api_new_error( + string $format, + string $message +) { + + /* if return format is html, return a HTML page */ + if ($format == "html") { + header('Content-Type: text/html'); + +?> + +<!DOCTYPE html> +<html> + + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <title>Rememori - ERROR</title> + <link rel="icon" type="image/png" href="/assets/img/favicon.png"> + <link rel="icon" type="image/png" sizes="16x16" href="/assets/img/favicon_16.png"> + <link rel="icon" type="image/png" sizes="32x32" href="/assets/img/favicon_32.png"> + <link rel="icon" type="image/png" sizes="64x64" href="/assets/img/favicon_64.png"> + <link rel="icon" type="image/png" sizes="128x128" href="/assets/img/favicon_128.png"> + <link rel="icon" type="x-image/ico" sizes="32x32" href="/assets/img/favicon.ico"> + <link rel="apple-touch-icon" type="image/png" href="/assets/img/favicon.png"> + <link rel=stylesheet href=/assets/css/simple.min.css media=all> + <link rel=stylesheet href=/assets/css/custom.css> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + </head> + + <body> + <header> + <h1>ERROR</h1> + </header> + <p><?php echo $message; ?></p> + </body> +</html> + +<?php + + } else { + /* Use JSON as fallback */ + header('Content-Type: application/json'); + echo "{" . PHP_EOL . + "\"status\": \"error\"," . PHP_EOL . + "\"message\": \"" . $message . "\"" . PHP_EOL . + "}" . PHP_EOL; + } // if-statement +} // function + +function api_new_success( + string $format, + string $path +) { + + /* if return format is html, return a HTML page */ + if ($format == "html") { + header('Content-Type: text/html'); + +?> + +<!DOCTYPE html> +<html> + + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <title>Rememori - Success!</title> + <link rel="icon" type="image/png" href="/assets/img/favicon.png"> + <link rel="icon" type="image/png" sizes="16x16" href="/assets/img/favicon_16.png"> + <link rel="icon" type="image/png" sizes="32x32" href="/assets/img/favicon_32.png"> + <link rel="icon" type="image/png" sizes="64x64" href="/assets/img/favicon_64.png"> + <link rel="icon" type="image/png" sizes="128x128" href="/assets/img/favicon_128.png"> + <link rel="icon" type="x-image/ico" sizes="32x32" href="/assets/img/favicon.ico"> + <link rel="apple-touch-icon" type="image/png" href="/assets/img/favicon.png"> + <link rel=stylesheet href=/assets/css/simple.min.css media=all> + <link rel=stylesheet href=/assets/css/custom.css> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + </head> + + <body> + <header> + <h1>Success!</h1> + </header> + <p>Note written to <?php echo $path; ?></p> + </body> +</html> + +<?php + + } else { + /* Use JSON as fallback */ + header('Content-Type: application/json'); + echo "{" . PHP_EOL . + "\"status\": \"success\"," . PHP_EOL . + "\"message\": \"Note written to" . $path . "\"" . PHP_EOL . + "}" . PHP_EOL; + } // if-state +} + +?> diff --git a/lib/bookmarklet.php b/lib/bookmarklet.php @@ -17,6 +17,7 @@ remori_window = window.open( ?user=" . $user . " &token=" . $token . " &category=' + category + ' +&return=html &content=' + window.location.href ); setTimeout(() => { remori_window.close(); }, 1500);