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]; } /* * Set the page object for the current request. This can only * be called once per runtime. */ function setPageObj(obj $obj) : void { global $_SCROTT; if ($_SCROTT['PAGE_OBJECT'] !== NULL) throw new Exception("Tried to establish page object context twice"); $_SCROTT['PAGE_OBJECT'] = $obj; } /* * Get the page object for the current request. */ function getPageObj() : ?obj { global $_SCROTT; return $_SCROTT['PAGE_OBJECT']; } /* * 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; } ?>