diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2024-04-26 10:06:03 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2024-04-26 10:06:03 +0200 |
commit | f9b93c4bbb23b452551bd46ce86cc6e3602fd594 (patch) | |
tree | 3fd8f14afb8248fcd9a3092a3f8ee1755dc936c3 /web/include | |
parent | 975c0b735be0808883116616d934ee257a24fb08 (diff) | |
download | faktura-main.tar.gz faktura-main.tar.bz2 faktura-main.zip |
Begin web app frontend in PHP.main
While I'd like a command line client for retreiving information and
scripting stuff based on the db contents, a web app is convenient for
the less common stuff like adding and editing data.
The web app could also have been done in rust, however, I feel php is so
convenient for simple web stuff that I think it makes more sense this
way.
Diffstat (limited to 'web/include')
-rw-r--r-- | web/include/api.php | 31 | ||||
-rw-r--r-- | web/include/misc.php | 36 |
2 files changed, 67 insertions, 0 deletions
diff --git a/web/include/api.php b/web/include/api.php new file mode 100644 index 0000000..c13b15c --- /dev/null +++ b/web/include/api.php @@ -0,0 +1,31 @@ +<?php +/** + * Functions for intercting with the remote API. + * + * @package volse.faktura.web + */ + +namespace Faktura\API; + +define('API_URL', 'http://postgrest:3000'); + +function call(string $method, string $resource, mixed $data = null): string { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, API_URL . '/' . $resource); + curl_setopt($ch, CURLOPT_HEADER, 'Accept: application/json'); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); + curl_setopt($ch, CURLOPT_TIMEOUT, 5); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $result = curl_exec($ch); + + if ($result === false) { + echo curl_error($ch); + die(); + } + + curl_close($ch); + + return $result; +} + diff --git a/web/include/misc.php b/web/include/misc.php new file mode 100644 index 0000000..15b5cea --- /dev/null +++ b/web/include/misc.php @@ -0,0 +1,36 @@ +<?php +/** + * Misc utility functions + * + * @package volse.faktura.web + */ + +/** + * Escape an input string so that it's safe fr use in a html context. + * + * @param string $input The unescaped input string. + * + * @return A string that's safe for use in html tags. + */ +function esc_html(?string $input): string { + if (empty($input)) { + return ''; + } + + return htmlspecialchars($input, ENT_NOQUOTES | ENT_HTML5); +} + +/** + * Escape input so that it's safe for use in html attrubutes. + * + * @param string $input The unescaped input string. + * + * @return A string that's safe for use in html attibutes. + */ +function esc_attr(?string $input): string { + if (empty($input)) { + return ''; + } + + return htmlspecialchars($input, ENT_QUOTES | ENT_HTML5); +} |