summaryrefslogtreecommitdiffstats
path: root/app/class/globals.php
diff options
context:
space:
mode:
Diffstat (limited to 'app/class/globals.php')
-rw-r--r--app/class/globals.php81
1 files changed, 81 insertions, 0 deletions
diff --git a/app/class/globals.php b/app/class/globals.php
index 8bfa029..615efa6 100644
--- a/app/class/globals.php
+++ b/app/class/globals.php
@@ -20,6 +20,19 @@
define("__VERSION__", "v0.0");
/*
+ * These global variables are arrays of strings logged by Scrott business
+ * logic to report errors, warnings, or informational responses to the
+ * user in cases where an exception doesn't need to be thrown.
+ */
+define("ERROR", "errorlist");
+define("WARNING", "warninglist");
+define("NOTICE", "noticelist");
+
+$_SCROTT[ERROR] = array();
+$_SCROTT[WARNING] = array();
+$_SCROTT[NOTICE] = array();
+
+/*
* Get the application root path. This is an absolute path on the server.
*/
function ar() : string
@@ -63,4 +76,72 @@ function require_https() : void
redirect("https://" . $_SERVER['SERVER_NAME'] . ap());
}
+/*
+ * Check for errors, warnings, or notices
+ */
+function isError(string $level) : bool
+{
+ global $_SCROTT;
+ return count($_SCROTT[$level]) > 0;
+}
+
+/*
+ * Log an error, warning, or notice
+ */
+function logError(string $level, string $error) : void
+{
+ global $_SCROTT;
+ $_SCROTT[$level][] = $error;
+}
+
+/*
+ * Get an array of all errors, warnings, or notices
+ */
+function getErrors(string $level) : array
+{
+ global $_SCROTT;
+ return $_SCROTT[$level];
+}
+
+/*
+ * Save an uploaded file and impose some constraints on supplied
+ * data. Caller can optionally pass some strings by reference to
+ * be given the supplied file's original name and mime-type.
+ * Maxsize is in bytes. If this function returns false, the
+ * appropriate error will be logged.
+ */
+function saveFile(array $file, string $path, int $maxsize, ?array $allowedMime = NULL,
+ ?string &$origName = NULL, ?string &$origMime = NULL) : bool
+{
+ if ($file['error'] > 0)
+ {
+ if ($file['error'] != UPLOAD_ERR_NO_FILE)
+ logError(ERROR, "An unknown error occurred");
+
+ return false;
+ }
+
+ if ($file['size'] > $maxsize)
+ {
+ logError(ERROR, "File must be no larger than " . $maxsize . " bytes");
+ return false;
+ }
+
+ if (is_array($allowedMime) && array_search($file['type'], $allowedMime) === false)
+ {
+ logError(ERROR, "File type is not supported");
+ return false;
+ }
+
+ if (!move_uploaded_file($file['tmp_name'], $path))
+ {
+ logError(ERROR, "Error saving uploaded file");
+ return false;
+ }
+
+ $origName = $file['name'];
+ $origMime = $file['type'];
+ return true;
+}
+
?>