summaryrefslogtreecommitdiffstats
path: root/app/class/globals.php
blob: 5a03da95b0c432166ca3252a8c42eb9f8376cfcb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php

/*
 * SCROTT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * For more information, please refer to UNLICENSE
 */

/*
 * This file defines various functions which exist in the global namespace.
 * These are utility functions and constants for the Scrott application.
 */

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
{
    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);
}

/*
 * 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());
}

/*
 * 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];
}

?>