From 0746eb18ff79bb94e2b1f1dca866b5541d4340cb Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sun, 5 Feb 2017 18:02:00 -0500 Subject: Add global source code file --- app/class/globals.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 app/class/globals.php (limited to 'app/class/globals.php') diff --git a/app/class/globals.php b/app/class/globals.php new file mode 100644 index 0000000..3512961 --- /dev/null +++ b/app/class/globals.php @@ -0,0 +1,22 @@ + -- cgit v1.2.3 From 912b28d44b340f9c418f3714d38df40e06f6f6da Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Mon, 13 Feb 2017 03:59:41 -0500 Subject: Add app-root(), app-path(), and similar function to globals.php --- app/class/globals.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'app/class/globals.php') diff --git a/app/class/globals.php b/app/class/globals.php index 3512961..c936488 100644 --- a/app/class/globals.php +++ b/app/class/globals.php @@ -19,4 +19,38 @@ define("__VERSION__", "v0.0"); +/* + * Get the application root path. This is an absolute path on the server. + */ +function ar() : string +{ + return substr($_SERVER['SCRIPT_NAME'], 0, -10); // 10 = strlen of "/index.php" +} + +/* + * Get the current page's path. This is an absolute path on the server. + */ +function ap() : string +{ + return ar() . $_SERVER['PATH_INFO']; +} + +/* + * Redirect to the given URL and die. + */ +function redirect(string $url) : void +{ + header("Location: " . $url); + exit; +} + +/* + * Redirect to the given in-app URL and die. The given URL should be a path + * relative to the app root. + */ +function location(string $path) : void +{ + redirect(ar() . $path); +} + ?> -- cgit v1.2.3 From 093cca175e42c0d0040821b4687ebd5823e95a5f Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Tue, 14 Feb 2017 23:42:06 -0500 Subject: Add function require_https() --- app/class/globals.php | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'app/class/globals.php') diff --git a/app/class/globals.php b/app/class/globals.php index c936488..8bfa029 100644 --- a/app/class/globals.php +++ b/app/class/globals.php @@ -53,4 +53,14 @@ function location(string $path) : void redirect(ar() . $path); } +/* + * Assert that the current connection to the server is over HTTPS. Redirect + * if not. + */ +function require_https() : void +{ + if (!isset($_SERVER['HTTPS'])) + redirect("https://" . $_SERVER['SERVER_NAME'] . ap()); +} + ?> -- cgit v1.2.3 From 2c9af1c3afad3f3aedef073a7436d677283ea456 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sun, 16 Apr 2017 02:10:48 -0400 Subject: Add global state for page errors, warnings, etc. Added arrays to the global $_SCROTT variable to keep lists of page errors, warnings, and notices. Also added functions to globals.php for interacting with these arrays. --- app/class/globals.php | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'app/class/globals.php') diff --git a/app/class/globals.php b/app/class/globals.php index 8bfa029..5a03da9 100644 --- a/app/class/globals.php +++ b/app/class/globals.php @@ -19,6 +19,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. */ @@ -63,4 +76,31 @@ 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]; +} + ?> -- cgit v1.2.3 From bbdbc8cbd9142c4377197964d48b2db5dfe00fa3 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sun, 16 Apr 2017 03:17:09 -0400 Subject: Add global function saveFile() --- app/class/globals.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'app/class/globals.php') diff --git a/app/class/globals.php b/app/class/globals.php index 5a03da9..615efa6 100644 --- a/app/class/globals.php +++ b/app/class/globals.php @@ -103,4 +103,45 @@ function getErrors(string $level) : array 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; +} + ?> -- cgit v1.2.3 From a607d74c3080cff5a9a9f4362f6da491982e8a6e Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Tue, 27 Jun 2017 01:39:02 -0400 Subject: Add function isAction() --- app/class/globals.php | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'app/class/globals.php') diff --git a/app/class/globals.php b/app/class/globals.php index 615efa6..94df716 100644 --- a/app/class/globals.php +++ b/app/class/globals.php @@ -76,6 +76,17 @@ function require_https() : void redirect("https://" . $_SERVER['SERVER_NAME'] . ap()); } +/* + * Check whether an action string was submitted by the user agent + */ +function isAction(string $action) : bool +{ + if (!isset($_REQUEST['action'])) + return false; + + return $_REQUEST['action'] == $action; +} + /* * Check for errors, warnings, or notices */ -- cgit v1.2.3 From 09f403582c5de5dcc97b265e8ae42469c9c8ff3f Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Tue, 27 Jun 2017 01:43:16 -0400 Subject: Add function input() --- app/class/globals.php | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'app/class/globals.php') diff --git a/app/class/globals.php b/app/class/globals.php index 94df716..776fc35 100644 --- a/app/class/globals.php +++ b/app/class/globals.php @@ -87,6 +87,17 @@ function isAction(string $action) : bool return $_REQUEST['action'] == $action; } +/* + * Get an array of submitted form input + */ +function input() : array +{ + if (!isset($_REQUEST['input'])) + throw new Exception("Requested form input, but no input data was supplied"); + + return $_REQUEST['input']; +} + /* * Check for errors, warnings, or notices */ -- cgit v1.2.3 From 41edfa2f7c85d657ff41ba146bd45de84373281c Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sat, 21 Jul 2018 23:03:15 -0400 Subject: Add PAGE_OBJECT global mechanism This addresses a problem with most views. They need an object context to display in. IE what pad, group, etc. are we viewing? This variable is intended to be set by index.php and referenced by page models. --- app/class/globals.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'app/class/globals.php') diff --git a/app/class/globals.php b/app/class/globals.php index 776fc35..8d27ae4 100644 --- a/app/class/globals.php +++ b/app/class/globals.php @@ -12,6 +12,8 @@ * For more information, please refer to UNLICENSE */ +require_once "class/obj.class.php"; + /* * This file defines various functions which exist in the global namespace. * These are utility functions and constants for the Scrott application. @@ -32,6 +34,14 @@ $_SCROTT[ERROR] = array(); $_SCROTT[WARNING] = array(); $_SCROTT[NOTICE] = array(); +/* + * The page object is the object our current request is primarily + * interested in. For example, when navigating to a pad web page by + * its guid, that pad is the page object. When viewing the dashboard, + * the current logged-in user is the page object. + */ +$_SCROTT['PAGE_OBJECT'] = NULL; + /* * Get the application root path. This is an absolute path on the server. */ @@ -125,6 +135,29 @@ function getErrors(string $level) : array 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 -- cgit v1.2.3 From 72ba54e1224ef815f8f7dabdf0c7449b120cfc4c Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sat, 21 Jul 2018 23:10:05 -0400 Subject: Change errorlevel constants --- app/class/globals.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app/class/globals.php') diff --git a/app/class/globals.php b/app/class/globals.php index 8d27ae4..ab82edd 100644 --- a/app/class/globals.php +++ b/app/class/globals.php @@ -26,9 +26,9 @@ define("__VERSION__", "v0.0"); * 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"); +define("ERROR", "ERROR_LIST"); +define("WARNING", "WARNING_LIST"); +define("NOTICE", "NOTICE_LIST"); $_SCROTT[ERROR] = array(); $_SCROTT[WARNING] = array(); -- cgit v1.2.3 From 380187ee5aadf008c7a71a9015008c155c528ccc Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sun, 22 Jul 2018 03:31:24 -0400 Subject: Update function location() Passing no argument (or NULL) now causes this function to redirect to the 'app-path' (current request page). This is a way to reload the current page. --- app/class/globals.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'app/class/globals.php') diff --git a/app/class/globals.php b/app/class/globals.php index ab82edd..c81c9a2 100644 --- a/app/class/globals.php +++ b/app/class/globals.php @@ -69,11 +69,14 @@ function redirect(string $url) : void /* * Redirect to the given in-app URL and die. The given URL should be a path - * relative to the app root. + * relative to the app root. No argument reloads the current path. */ -function location(string $path) : void +function location(?string $path = NULL) : void { - redirect(ar() . $path); + if ($path) + redirect(ar() . $path); + else + redirect(ap()); } /* -- cgit v1.2.3 From fc45eabca659f9e9792ca4c5a82a3cef732cec10 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Tue, 24 Jul 2018 04:59:34 -0400 Subject: Add $_SCROTT['PAGE_NAME'] variable and getter/setter The intention is for index.php to set this variable. This is the text (HTML) displayed on the button for the pad select dropdown in the nav bar. Basically the canonical name of the page we're on. --- app/class/globals.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'app/class/globals.php') diff --git a/app/class/globals.php b/app/class/globals.php index c81c9a2..7120aaa 100644 --- a/app/class/globals.php +++ b/app/class/globals.php @@ -42,6 +42,13 @@ $_SCROTT[NOTICE] = array(); */ $_SCROTT['PAGE_OBJECT'] = NULL; +/* + * The page name variable is what displays in the pad select dropdown and + * is typically set to the "group / pad" of the current page or, "Dashboard", + * "Groups", "Pads", etc. on special pages. + */ +$_SCROTT['PAGE_NAME'] = ""; + /* * Get the application root path. This is an absolute path on the server. */ @@ -161,6 +168,29 @@ function getPageObj() : ?obj return $_SCROTT['PAGE_OBJECT']; } +/* + * Set the page name string. This can only be called once per + * runtime. + */ +function setPageName(string $name) : void +{ + global $_SCROTT; + + if ($_SCROTT['PAGE_NAME'] !== "") + throw new Exception("Tried to configure page name twice"); + + $_SCROTT['PAGE_NAME'] = $name; +} + +/* + * Get the page name string. + */ +function getPageName() : string +{ + global $_SCROTT; + return $_SCROTT['PAGE_NAME']; +} + /* * Save an uploaded file and impose some constraints on supplied * data. Caller can optionally pass some strings by reference to -- cgit v1.2.3 From 323576a594f20485e25c55dd52a5356cb209479c Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sat, 15 Sep 2018 21:51:23 -0400 Subject: Add function oneStr() The initial intended use case for this is applying the "active" and "in active" classes to the first tab to appear in the settings modal. --- app/class/globals.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'app/class/globals.php') diff --git a/app/class/globals.php b/app/class/globals.php index 7120aaa..8ac67e1 100644 --- a/app/class/globals.php +++ b/app/class/globals.php @@ -191,6 +191,21 @@ function getPageName() : string return $_SCROTT['PAGE_NAME']; } +/* + * Produce a string, but only once. This function is useful + * when dealing with some variable collection of markup and + * you want to affect only the first one with a modifier. + * $state should be initialized to false prior to first call. + */ +function oneStr(string $str, bool &$state) : string +{ + if ($state) + return ""; + + $state = true; + return $str; +} + /* * Save an uploaded file and impose some constraints on supplied * data. Caller can optionally pass some strings by reference to -- cgit v1.2.3 From 77cbdc84d952643163d7086f79a633e5837be76d Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Wed, 19 Sep 2018 15:30:44 -0400 Subject: globals: Add function saveIfFile() This is an alternative function to globals' saveFile(), which allows model code to just pass in the name of the expected uploaded file, rather than requiring them to look up the file themselves. This is in line with my preference to encapsulate PHP superglobals access away from most of the codebase. Note that even if the user opts not to upload optional files, the associated file field will still be present in $_FILES, with a special error code set (meaning 'no file uploaded') which setFile() ignores. It is only in the case of a malformed form submission that $_FILES will be missing the requested file field, prompting Scrott to throw an exception. --- app/class/globals.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'app/class/globals.php') diff --git a/app/class/globals.php b/app/class/globals.php index 8ac67e1..6e3b7fd 100644 --- a/app/class/globals.php +++ b/app/class/globals.php @@ -247,4 +247,19 @@ function saveFile(array $file, string $path, int $maxsize, ?array $allowedMime = return true; } +/* + * Similar to saveFile, but takes the uploaded file field name, + * rather than the array directly. The file is looked up in + * $_FILES. If it does not exist, an exception is thrown. + */ +function saveIfFile(string $file, string $path, int $maxsize, ?array $allowedMime = NULL, + ?string &$origName = NULL, ?string &$origMime = NULL) : bool +{ + if (!isset($_FILES[$file])) + throw new Exception("Requested file upload, but no data was supplied"); + + $f = $_FILES[$file]; + return saveFile($f, $path, $maxsize, $allowedMime, $origName, $origMime); +} + ?> -- cgit v1.2.3 From eef89807b15b0e38362d67b68a5becacc8838864 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Wed, 19 Sep 2018 16:39:31 -0400 Subject: globals: Remove single call assertions for setPage...() functions Removing these unnecessary checks. They are not protecting us from anything, only inconveniencing me in index.php. --- app/class/globals.php | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) (limited to 'app/class/globals.php') diff --git a/app/class/globals.php b/app/class/globals.php index 6e3b7fd..c1a6e32 100644 --- a/app/class/globals.php +++ b/app/class/globals.php @@ -146,16 +146,11 @@ function getErrors(string $level) : array } /* - * Set the page object for the current request. This can only - * be called once per runtime. + * Set the page object for the current request. */ 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; } @@ -169,16 +164,11 @@ function getPageObj() : ?obj } /* - * Set the page name string. This can only be called once per - * runtime. + * Set the page name string. */ function setPageName(string $name) : void { global $_SCROTT; - - if ($_SCROTT['PAGE_NAME'] !== "") - throw new Exception("Tried to configure page name twice"); - $_SCROTT['PAGE_NAME'] = $name; } -- cgit v1.2.3 From 2ec07236fed37e5e35b44449f39abc93e854132f Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sat, 27 Oct 2018 15:37:12 -0400 Subject: Bump version number Signed-off-by: Malf Furious --- app/class/globals.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/class/globals.php') diff --git a/app/class/globals.php b/app/class/globals.php index c1a6e32..74635d7 100644 --- a/app/class/globals.php +++ b/app/class/globals.php @@ -19,7 +19,7 @@ require_once "class/obj.class.php"; * These are utility functions and constants for the Scrott application. */ -define("__VERSION__", "v0.0"); +define("__VERSION__", "v0.1"); /* * These global variables are arrays of strings logged by Scrott business -- cgit v1.2.3