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 4e7d5aea6d31d59a72952ce804f006a17001e5f9
parent a1c3e08141bc1cd3150f8d897b26dc8dc87ee2d5
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Tue, 29 Oct 2024 10:43:07 +0100

fix: use multilanguage responses; fix: only load required functions

Diffstat:
Mapi/new.php | 11++++++-----
Mapi/rss.php | 20+++++++++++---------
Mconfig/i18n.php | 10++++++++++
Alib/api.php | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dlib/api_new.php | 101-------------------------------------------------------------------------------
5 files changed, 129 insertions(+), 115 deletions(-)

diff --git a/api/new.php b/api/new.php @@ -1,8 +1,9 @@ <?php /* Load library functions */ - foreach (glob($_SERVER["DOCUMENT_ROOT"] . "/lib/*.php") as $lib) { - include($lib); + $libs = array("api.php", "write.php", "users.php", "helpers.php"); + foreach ($libs as $lib) { + include($_SERVER["DOCUMENT_ROOT"] . "/lib/" . $lib); } /* Load configurations */ @@ -47,7 +48,7 @@ $auth = auth_user($user, $token, -1); if ($auth !== true) { http_response_code(401); // unauthorized - api_new_error($return, "You could not be authenticated"); + api_error($return, $GLOBALS["i18n_noauth"]); exit(1); } @@ -79,11 +80,11 @@ if ($written === true) { http_response_code(200); // successful - api_new_success($return, $user . "/" . $category . "/" . $filename); + api_write_success($return, $user . "/" . $category . "/" . $filename); exit(0); } else { http_response_code(500); // internal server error - api_new_error($return, "Note could not be created for unknown reasons"); + api_write_error($return, $GLOBALS["i18n_unknown_error"]); exit(1); } diff --git a/api/rss.php b/api/rss.php @@ -1,8 +1,9 @@ <?php /* Load library functions */ - foreach (glob($_SERVER["DOCUMENT_ROOT"] . "/lib/*.php") as $lib) { - include($lib); + $libs = array("api.php", "users.php", "helpers.php", "read.php"); + foreach ($libs as $lib) { + include($_SERVER["DOCUMENT_ROOT"] . "/lib/" . $lib); } /* Load configurations */ @@ -14,6 +15,8 @@ $category = validate_input_string(gather_post("category")); $user = validate_input_string(gather_post("user")); $token = validate_input_string(gather_post("token")); + $return = validate_input_string(gather_post("return")); + /* Fallback to GET request, if no POST was found */ if (empty($category)) { @@ -25,18 +28,17 @@ if (empty($token)) { $token = validate_input_string(gather_get("token")); } + if (empty($return)) { + $return = validate_input_string(gather_get("return")); + } + /* run authentification method. exit immediately if it fails */ $auth = auth_user($user, $token, -1); if ($auth !== true) { http_response_code(401); // unauthorized - header('Content-Type: application/json'); - exit( - "{" . - "\"status\": \"error\"," . - "\"message\": \"You could not be authenticated!\"" . - "}" - ); + api_error($return, $GLOBALS["i18n_noauth"]); + exit(1); } /* fetch notes in the given category */ diff --git a/config/i18n.php b/config/i18n.php @@ -6,6 +6,10 @@ $acceptLang = ["en", "de"]; $lang = in_array($lang, $acceptLang) ? $lang : "en"; if ($lang == "en") { + $GLOBALS["i18n_error"] = "Error"; + $GLOBALS["i18n_success"] = "Success"; + $GLOBALS["i18n_unknown_error"] = "An unknown error occurred!"; + $GLOBALS["i18n_note_written_to"] = "Note writtent to"; $GLOBALS["i18n_development"] = "Sourcecode"; $GLOBALS["i18n_note"] = "Note"; $GLOBALS["i18n_notes"] = "Notes"; @@ -19,6 +23,7 @@ if ($lang == "en") { $GLOBALS["i18n_login"] = "Login"; $GLOBALS["i18n_user"] = "Username"; $GLOBALS["i18n_pass"] = "Password"; + $GLOBALS["i18n_noauth"] = "You could not be authenticated"; $GLOBALS["i18n_logout"] = "Logout"; $GLOBALS["i18n_categories"] = "Categories"; $GLOBALS["i18n_delete"] = "Delete"; @@ -33,6 +38,10 @@ if ($lang == "en") { $GLOBALS["i18n_bookmarklet_prompt"] = "Under which category do you want to remember this site?"; } if ($lang == "de") { + $GLOBALS["i18n_error"] = "Fehler"; + $GLOBALS["i18n_success"] = "Erfolg"; + $GLOBALS["i18n_unknown_error"] = "Unbekannter Fehler aufgetreten!"; + $GLOBALS["i18n_note_written_to"] = "Notiz geschrieben in"; $GLOBALS["i18n_development"] = "Quellcode"; $GLOBALS["i18n_note"] = "Notiz"; $GLOBALS["i18n_notes"] = "Notizen"; @@ -49,6 +58,7 @@ if ($lang == "de") { $GLOBALS["i18n_logout"] = "Logout"; $GLOBALS["i18n_user"] = "Name der:die Nutzer:in"; $GLOBALS["i18n_pass"] = "Passwort"; + $GLOBALS["i18n_noauth"] = "Authentifizierung gescheitert"; $GLOBALS["i18n_cookie"] = "Eingelogged bleiben (das setzt einen Cookie)"; $GLOBALS["i18n_cookie_session"] = "Nein, nur temporär"; $GLOBALS["i18n_cookie_long"] = "Ja, lass mich für 30 Tage eingelogged"; diff --git a/lib/api.php b/lib/api.php @@ -0,0 +1,102 @@ +<?php + +function api_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</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><?php echo $GLOBALS["i18n_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_write_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</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><?php echo $GLOBALS["i18n_success"]; ?></h1> + </header> + <p><?php echo $GLOBALS["i18n_note_written_to"] . " " . $path; ?></p> + </body> +</html> + +<?php + + } else { + /* Use JSON as fallback */ + header('Content-Type: application/json'); + echo "{" . PHP_EOL . + "\"status\": \"success\"," . PHP_EOL . + "\"message\": \"" . $GLOBALS["i18n_note_written_to"] . " " . $path . + "\"" . PHP_EOL . + "}" . PHP_EOL; + } // if-state +} + +?> diff --git a/lib/api_new.php b/lib/api_new.php @@ -1,101 +0,0 @@ -<?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 -} - -?>