forked from the-benchmarker/web-frameworks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
488 lines (405 loc) · 15.3 KB
/
functions.php
File metadata and controls
488 lines (405 loc) · 15.3 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<?php
/*
|--------------------------------------------------------------------------
| BasicPHP Functions Library
|--------------------------------------------------------------------------
|
| url_path() - retrieves the URL path substring separated by '/'
| homepage() - render hompage
| error404() - Handle Error 404 - Page Not Found - Invalid URI
| route_rpc() - JSON-RPC v2.0 compatibility layer
| route_auto() - automatic routing of URL path to Class and method
| route_class() - routes URL path request to Controllers
| view() - passes data and renders the View
| pdo_conn() - PHP Data Objects (PDO) database connection
| api_response() - handles API response
| api_call() - handles API call
| firewall() - web application firewall
| force_ssl() - force application to use SSL
| esc() - uses htmlspecialchars() to prevent XSS
| csrf_token() - uses sessions to create per request CSRF token
| encrypt() - encrypt data using AES-CBC-HMAC
| decrypt() - decrypt data using AES-CBC-HMAC
|
*/
/**
* Get URL path string value after the BASE_URL.
*
* @param integer $order - URL substring position from the BASE_URL
* - url_path(1) as first string after BASE_URL
*/
function url_path($order)
{
if (isset($_SERVER['REQUEST_URI'])) {
$url_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$url = explode('/', $url_path);
}
if (isset($url[$order+SUB_DIR]) || ! empty($url[$order+SUB_DIR])) {
return $url[$order+SUB_DIR];
} else {
return false;
}
}
/**
* Render Homepage
*/
function homepage()
{
if (empty(url_path(1))) {
list($class, $method) = explode('@', HOME_PAGE);
$object = new $class();
return $object->$method();
}
}
/**
* Handle Error 404 - Page Not Found - Invalid URI
* A valid page has $valid_page set to TRUE.
*/
function error404()
{
if (! isset($valid_page) || $valid_page !== true) {
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
exit();
}
}
/**
* JSON-RPC v2.0 Compatibility Layer with 'method' member as 'class.method'
*/
function route_rpc()
{
$valid_page = true; // Set page as valid
// Check if HTTP request method is 'POST', if there is POSTed data, and the POSTed data is in JSON format.
if ($_SERVER['REQUEST_METHOD'] == 'POST' && file_get_contents('php://input') !== false && json_decode(file_get_contents('php://input'), true) !== null) {
$json_rpc = json_decode(file_get_contents('php://input'), true);
// Requires the 'jsonrpc', 'method' and 'id' members of the request object
if (isset($json_rpc['jsonrpc']) && isset($json_rpc['method']) && isset($json_rpc['id'])) {
if (strstr($json_rpc['method'], '.') == false) {
exit(json_encode(['jsonrpc' => '2.0', 'error' => ['code' => 32600, 'message' => "The JSON-RPC 'method' member should have the format 'class.method'."], 'id' => $json_rpc['id']]));
}
list($class, $method) = explode('.', $json_rpc['method']);
$class = ucfirst($class) . CONTROLLER_SUFFIX;
$method = lcfirst($method);
if (class_exists($class)) {
$object = new $class();
if (method_exists($object, $method)) {
return $object->$method();
exit();
} else {
exit(json_encode(['jsonrpc' => '2.0', 'error' => ['code' => 32601, 'message' => "Method not found."], 'id' => $json_rpc['id']]));
}
} else {
exit(json_encode(['jsonrpc' => '2.0', 'error' => ['code' => 32601, 'message' => "Class not found."], 'id' => $json_rpc['id']]));
}
}
}
}
/**
* Automatic routing of url_path(1) and (2) as Class and method
*/
function route_auto()
{
$valid_page = true; // Set page as valid
if (url_path(1) !== false) {
$class = ucfirst(url_path(1)) . CONTROLLER_SUFFIX;
}
if (url_path(2) !== false) {
$method = lcfirst(url_path(2));
} else {
$method = METHOD_DEFAULT;
}
if (class_exists($class)) {
$object = new $class();
if (method_exists($object, $method)) {
return $object->$method();
} else {
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
exit();
}
}
}
/**
* Load Controller based on URL path string and HTTP method
*
* @param string $http_method - HTTP method (e.g. GET, POST, PUT, DELETE)
* @param string $string - URL path in the format '/url/string'
* - Wildcard convention from Codeigniter
* - (:num) for number and (:any) for string
* @param string $class_method - ClassController@method format
*/
function route_class($http_method, $path, $class_method)
{
$valid_page = true; // Set page as valid
if ($_SERVER['REQUEST_METHOD'] == $http_method) {
// Convert '/' and wilcards (:num) and (:any) to RegEx
$pattern = str_ireplace('/', '\/', $path);
$pattern = str_ireplace('(:num)', '[0-9]+', $pattern);
$pattern = str_ireplace('(:any)', '[^\/]+', $pattern);
// Check for subfolders from DocumentRoot and include in endpoint
$sub = explode('/', dirname($_SERVER['SCRIPT_NAME']));
if (! empty($sub[1])) {
$subfolder = implode('\/', $sub);
} else {
$subfolder = '';
}
$url_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if (preg_match('/^' . $subfolder . $pattern . '+$/i', $url_path)) {
list($class, $method) = explode('@', $class_method);
$object = new $class();
return $object->$method();
}
}
}
/**
* Passes data and renders the View
*
* @param string $view - View file, excluding .php extension
* @param array $data - Data as an array to pass to the View
*/
function view($view, $data=null)
{
// Convert array keys to variables
if (isset($data)) {
extract($data);
}
// Render Page View
return require_once '../views/' . $view . '.php';
}
/**
* PHP Data Objects (PDO) database connection
*
* @param string $database - Database (e.g. mysql)
* @param string $servername - Server Name (e.g localhost)
* @param string $dbname - Database Name
* @param string $username - Username
* @param string $password - Password
*/
function pdo_conn($database, $servername, $dbname, $username, $password)
{
$conn = new PDO("$database:host=$servername;dbname=$dbname", $username, $password, array(
PDO::ATTR_PERSISTENT => true
));
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
}
/**
* Handles the HTTP REST API Response
*
* @param array $data - Array to be encoded to JSON
* @param string $message - Message to send with response
*/
function api_response($data, $message=null)
{
// Define content type as JSON data through the header
header("Content-Type: application/json; charset=utf-8");
// Data and message as arrays to send with response
$response['data'] = $data;
$response['message'] = $message;
// Encode $response array to JSON
echo json_encode($response);
}
/**
* Handles the HTTP REST API Calls
*
* @param string $http_method - HTTP request method (e.g. 'GET', 'POST')
* @param string $url - URL of external server API
* @param string $data - POST fields in array
* @param string $username - Username
* @param string $password - Password
*/
function api_call($http_method, $url, $data=null, $username=null, $password=null)
{
// Initialize cURL
$ch = curl_init();
// Convert $data array parameter to JSON
$data_json = json_encode($data);
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $http_method);
// curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_json))
);
// Execute cURL
$result = curl_exec($ch);
// Close cURL connection
curl_close($ch);
// Convert JSON response from external server to an array
$data_output = json_decode($result, true);
return $data_output;
}
/**
* Web Application Firewall
*/
function firewall()
{
if (FIREWALL_ON == true) {
// Allow only access from whitelisted IP addresses
if (isset($_SERVER['REMOTE_ADDR']) && !in_array($_SERVER['REMOTE_ADDR'], ALLOWED_IP_ADDR)) {
header($_SERVER["SERVER_PROTOCOL"]." 403 Forbidden");
exit('<p>You are not allowed to access the application using your IP address.</p>');
}
// Allow only URI_WHITELISTED characters on the Request URI.
if (! empty(URI_WHITELISTED)) {
$regex_array = str_replace('w', 'alphanumeric', URI_WHITELISTED);
$regex_array = explode('\\', $regex_array);
if (isset($_SERVER['REQUEST_URI']) && preg_match('/[^' . URI_WHITELISTED . ']/i', $_SERVER['REQUEST_URI'])) {
header($_SERVER["SERVER_PROTOCOL"]." 400 Bad Request");
exit('<p>The URI should only contain alphanumeric and GET request characters:</p><p><ul>' . implode('<li>', $regex_array) . '</ul></p>');
}
}
// Deny POST_BLACKLISTED characters in $_POST and post body. '\' is blacklisted by default.
if (! empty(POST_BLACKLISTED)) {
$regex_array = explode('\\', POST_BLACKLISTED);
if (isset($_POST) && preg_match('/[' . POST_BLACKLISTED . '\\\]/i', implode('/', $_POST))) {
header($_SERVER["SERVER_PROTOCOL"]." 400 Bad Request");
exit('<p>Submitted data should NOT contain the following characters:</p><p><ul>' . implode('<li>', $regex_array) . '<li>\</ul></p>');
}
$post_data = file_get_contents('php://input');
if (isset($post_data) && preg_match('/[' . POST_BLACKLISTED . '\\\]/i', $post_data)) {
header($_SERVER["SERVER_PROTOCOL"]." 400 Bad Request");
exit('<p>Submitted data should NOT contain the following characters:</p><p><ul>' . implode('<li>', $regex_array) . '<li>\</ul></p>');
}
}
}
}
/**
* Force application to use SSL
*/
function force_ssl()
{
if (ENFORCE_SSL == true && (! isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on')) {
header('Location: https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
exit();
}
}
/**
* Helper function to prevent Cross-Site Scripting (XSS)
* Uses htmlspecialchars() to prevent XSS
*
* @param string $string - String to escape
*/
function esc($string)
{
return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
}
/**
* Helper function to prevent Cross-Site Request Forgery (CSRF)
* Creates a per request token to handle CSRF using sessions
*/
function csrf_token()
{
if (isset($_SESSION)) {
$_SESSION['csrf-token'] = bin2hex(random_bytes(32));
return $_SESSION['csrf-token'];
} else {
$error_message = 'Please initialize Sessions.';
$page_title = 'Sessions Error';
$data = compact('error_message', 'page_title');
view('error', $data);
}
}
/**
* Encrypt data using AES-CBC-HMAC
*
* @param string $plaintext - Plaintext to be encrypted
*/
function encrypt($plaintext)
{
function encrypt_v1($plaintext)
{
// Version
$version = 'enc-v1';
// Cipher method to AES with 256-bit key
$cipher = strtolower(CIPHER_METHOD);
// Salt for encryption key
$salt_key = random_bytes(16);
// Derive encryption key
$key = hash_pbkdf2('sha256', PASS_PHRASE, $salt_key, 10000);
// Initialization vector
$iv = random_bytes(16);
if ($cipher == 'aes-256-gcm') {
$ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv, $tag);
return $version . '::' . base64_encode($ciphertext) . '::' . base64_encode($iv) . '::' . base64_encode($tag) . '::' . base64_encode($salt_key);
} else {
// Salt for HMAC key
$salt_hmac = random_bytes(16);
// Derive HMAC key
$key_hmac = hash_pbkdf2('sha256', PASS_PHRASE, $salt_hmac, 10000);
$ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv);
$hash = hash_hmac('sha256', $ciphertext, $key_hmac);
return $version . '::' . base64_encode($ciphertext) . '::' . base64_encode($hash) . '::' . base64_encode($iv) . '::' . base64_encode($salt_key) . '::' . base64_encode($salt_hmac);
}
}
/** Version-based Encryption */
// Default encryption function
return encrypt_v1($plaintext);
}
/**
* Decrypt data using AES-CBC-HMAC
*
* @param string $encypted - base64_encoded ciphertext, hash,
* - iv, salt_key, and salt_hmac
*/
function decrypt($encrypted)
{
function decrypt_v1($encrypted)
{
// Return empty if $encrypted is not set or empty.
if (! isset($encrypted) || empty($encrypted)) {
return '';
}
// Cipher method to AES with 256-bit key
$cipher = strtolower(CIPHER_METHOD);
if ($cipher == 'aes-256-gcm') {
list($version, $ciphertext, $iv, $tag, $salt_key) = explode('::', $encrypted);
$ciphertext = base64_decode($ciphertext);
$iv = base64_decode($iv);
$tag = base64_decode($tag);
$salt_key = base64_decode($salt_key);
// Derive encryption key
$key = hash_pbkdf2('sha256', PASS_PHRASE, $salt_key, 10000);
$plaintext = openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv, $tag);
// GCM authentication
if ($plaintext !== false) {
return $plaintext;
} else {
exit('<strong>Warning: </strong>Please verify authenticity of ciphertext.');
}
} else {
list($version, $ciphertext, $hash, $iv, $salt_key, $salt_hmac) = explode('::', $encrypted);
$ciphertext = base64_decode($ciphertext);
$hash = base64_decode($hash);
$iv = base64_decode($iv);
$salt_key = base64_decode($salt_key);
$salt_hmac = base64_decode($salt_hmac);
// Derive encryption key
$key = hash_pbkdf2('sha256', PASS_PHRASE, $salt_key, 10000);
// Derive HMAC key
$key_hmac = hash_pbkdf2('sha256', PASS_PHRASE, $salt_hmac, 10000);
$digest = hash_hmac('sha256', $ciphertext, $key_hmac);
// HMAC authentication
if (hash_equals($hash, $digest)) {
return openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv);
} else {
exit('<strong>Warning: </strong>Please verify authenticity of ciphertext.');
}
}
}
$version = explode('::', $encrypted)[0];
/** Version-based Decryption */
// Return $encrypted if no encryption detected.
switch ($version) {
case 'enc-v1':
return decrypt_v1($encrypted);
break;
default:
return $encrypted;
}
}