This repository was archived by the owner on Aug 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunctions.php
More file actions
265 lines (242 loc) · 6.25 KB
/
functions.php
File metadata and controls
265 lines (242 loc) · 6.25 KB
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
<?php
/**
* @author: KentProjects <developer@kentprojects.com>
* @license: Copyright KentProjects
* @link: http://kentprojects.com
*/
define("APPLICATION_PATH", __DIR__);
date_default_timezone_set("Etc/UTC");
setlocale(LC_ALL, "en_GB.UTF8");
$_SERVER["PATH_INFO"] = empty($_SERVER["PATH_INFO"]) ? "/" : $_SERVER["PATH_INFO"];
require_once __DIR__ . "/exceptions.php";
/**
* Register the autoloader so we can call on classes when we feel like it!
*/
spl_autoload_register(
/**
* @param string $class
* @return bool
*/
function ($class)
{
$file = str_replace("_", "/", strtolower($class)) . ".php";
$filename = null;
/**
* If the word "Controller_" exists at the beginning of this class, handle it.
*/
if (strpos($class, "Controller_") === 0)
{
$filename = APPLICATION_PATH . "/controllers/" . str_replace("controller/", "", $file);
}
/**
* If the word "Model_" exists at the beginning of this class, handle it.
*/
elseif (strpos($class, "Model_") === 0)
{
$filename = APPLICATION_PATH . "/models/" . str_replace("model/", "", $file);
}
/**
* If the word "Intent_" exists at the beginning of this class, handle it.
*/
elseif (strpos($class, "Intent_") === 0)
{
$filename = APPLICATION_PATH . "/intents/" . str_replace("intent_", "", strtolower($class)) . ".php";
}
/**
* If the word "_Map" exists in this class, handle it.
* Checking to see if "Map" exists in general will make this function quicker for other classes!
*/
elseif (($class != "ModelMap") && (strpos($class, "Map") !== false) && (strpos($class, "Map") === (strlen($class) - 3)))
{
$filename = APPLICATION_PATH . "/models/maps/" . $file;
}
/**
* If the word "Permissions" exists in this class, handle it.
* Checking to see if "Permissions" exists in general will make this function quicker for other classes!
*/
elseif ((strpos($class, "Permissions") !== false) && (strpos($class, "Permissions") === (strlen($class) - 11)))
{
$filename = APPLICATION_PATH . "/models/permissions/" . $file;
}
/**
* Else this is a generic class in a folder, so go find it!
*/
else
{
$folders = array(
APPLICATION_PATH . "/classes",
APPLICATION_PATH . "/classes/traits",
APPLICATION_PATH . "/system",
APPLICATION_PATH . "/vendor"
);
foreach ($folders as $folder)
{
if (file_exists($folder . "/" . $file))
{
$filename = $folder . "/" . $file;
break;
}
}
}
if (empty($filename) || !file_exists($filename))
{
return false;
}
/** @noinspection PhpIncludeInspection */
require_once $filename;
return class_exists($class, false);
}
);
/**
* Sets up the Error Handler
* Wraps it up into a lovely catchable Exception
*/
set_error_handler(
function ($error_no, $error_string, $error_file, $error_line, $error_context)
{
Log::log_error($error_no, "{$error_string} in {$error_file}:{$error_line}");
throw new PHPException($error_no, $error_string, $error_file, $error_line, $error_context);
}
);
/**
* @param string $key
* @param string $value
* @return void
*/
function addStaticHeader($key, $value)
{
class_exists("Response") && call_user_func_array(array("Response", "addStaticHeader"), func_get_args());
}
/**
* Returns the first non-empty argument or NULL.
*
* @param mixed
* @param mixed
* [ @param mixed ] ...
* @return mixed|null
*/
function coalesce($a, $b)
{
foreach (func_get_args() as $a)
{
if (!empty($a))
{
return $a;
}
}
return null;
}
/**
* @param string|null $section
* @param string|null $key
* @throws InvalidArgumentException
* @return array|string
*/
function config($section = null, $key = null)
{
if (empty($GLOBALS["config.ini"]))
{
if (file_exists(__DIR__ . "/config.production.ini"))
{
$configFile = __DIR__ . "/config.production.ini";
}
elseif (file_exists(__DIR__ . "/config.ini"))
{
$configFile = __DIR__ . "/config.ini";
}
else
{
trigger_error("No config file found.", E_USER_ERROR);
return null;
}
$GLOBALS["config.ini"] = parse_ini_file($configFile, true);
}
switch (func_num_args())
{
case 2:
return $GLOBALS["config.ini"][$section][$key];
case 1:
return $GLOBALS["config.ini"][$section];
default:
throw new InvalidArgumentException("Invalid number of arguments for function config.");
}
}
/**
* A lovely long list of status codes that a response or exception could use.
*
* @param int $code
* @return string
*/
function getHttpStatusForCode($code)
{
$codes = array(
// Continuation
100 => "Continue",
101 => "Switching Protocols",
// Success
200 => "OK",
201 => "Created",
202 => "Accepted",
203 => "Non-Authoritative Information",
204 => "No Content",
205 => "Reset Content",
206 => "Partial Content",
// 30X Redirection
300 => "Multiple Choices",
301 => "Moved Permanently",
302 => "Found",
303 => "See Other",
304 => "Not Modified",
305 => "Use Proxy",
306 => "(Unused)",
307 => "Temporary Redirect",
// 4XX Client Error
400 => "Bad Request",
401 => "Unauthorized",
402 => "Payment Required",
403 => "Forbidden",
404 => "Not Found",
405 => "Method Not Allowed",
406 => "Not Acceptable",
407 => "Proxy Authentication Required",
408 => "Request Timeout",
409 => "Conflict",
410 => "Gone",
411 => "Length Required",
412 => "Precondition Failed",
413 => "Request Entity Too Large",
414 => "Request-URI Too Long",
415 => "Unsupported Media Type",
416 => "Requested Range Not Satisfiable",
417 => "Expectation Failed",
// 50X Server Error
500 => "Internal Server Error",
501 => "Not Implemented",
502 => "Bad Gateway",
503 => "Service Unavailable",
504 => "Gateway Timeout",
505 => "HTTP Version Not Supported"
);
return (isset($codes[$code])) ? $codes[$code] : "";
}
/**
* Creates a slug of a string.
* http://cubiq.org/the-perfect-php-clean-url-generator
*
* @param $str
* @param array $replace
* @param string $delimiter
* @return string
*/
function slugify($str, $replace = array(), $delimiter = '-')
{
if (!empty($replace))
{
$str = str_replace((array)$replace, ' ', $str);
}
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
return (string)$clean;
}