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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
<?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
*/
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.
*/
define("__VERSION__", "v0.4");
/*
* 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", "ERROR_LIST");
define("WARNING", "WARNING_LIST");
define("NOTICE", "NOTICE_LIST");
$_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;
/*
* 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'] = "";
/*
* The auto modal variable holds the name (DOM ID) of a modal to automatically
* open on page load. The `stdpage` footer will reference this.
*/
$_SCROTT['AUTO_MODAL'] = "";
/*
* 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. No argument reloads the current path.
*/
function location(?string $path = NULL) : void
{
if ($path)
redirect(ar() . $path);
else
redirect(ap());
}
/*
* 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 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;
}
/*
* 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
*/
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];
}
/*
* Set the page object for the current request.
*/
function setPageObj(obj $obj) : void
{
global $_SCROTT;
$_SCROTT['PAGE_OBJECT'] = $obj;
}
/*
* Get the page object for the current request.
*/
function getPageObj() : ?obj
{
global $_SCROTT;
return $_SCROTT['PAGE_OBJECT'];
}
/*
* Set the page name string.
*/
function setPageName(string $name) : void
{
global $_SCROTT;
$_SCROTT['PAGE_NAME'] = $name;
}
/*
* Get the page name string.
*/
function getPageName() : string
{
global $_SCROTT;
return $_SCROTT['PAGE_NAME'];
}
/*
* Set the auto modal.
*/
function setAutoModal(string $autoModal) : void
{
global $_SCROTT;
$_SCROTT['AUTO_MODAL'] = $autoModal;
}
/*
* Get the auto modal.
*/
function getAutoModal() : string
{
global $_SCROTT;
return $_SCROTT['AUTO_MODAL'];
}
/*
* 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
* 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;
}
/*
* 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);
}
?>
|