Skip to content

Commit 18f2294

Browse files
authored
"set" for middleware methods; api content-type
1 parent 5129bbb commit 18f2294

File tree

2 files changed

+24
-30
lines changed

2 files changed

+24
-30
lines changed

Basic.php

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,13 @@ public static function apiCall($http_method, $url, $data=NULL, $user_token=NULL,
148148
/**
149149
* Handle HTTP API response
150150
*
151-
* @param integer $code - HTTP response code
152-
* @param string $data - Data to transmit
153-
* @param string $message - HTTP response message
151+
* @param integer $code - HTTP response code
152+
* @param string $data - Data to transmit
153+
* @param string $content_type - Header: Content-Type
154+
* @param string $message - HTTP response message
154155
*/
155156

156-
public static function apiResponse($code, $data=NULL, $message=NULL)
157+
public static function apiResponse($code, $data=NULL, $content_type='text/plain', $message=NULL)
157158
{
158159
// OK response
159160
if ($code > 199 && $code < 300) {
@@ -167,6 +168,7 @@ public static function apiResponse($code, $data=NULL, $message=NULL)
167168
header($_SERVER['SERVER_PROTOCOL'] . ' ' . $code . ' ' . $message); // Set HTTP response code and message
168169
}
169170

171+
header('Content-Type: ' . $content_type);
170172
exit($data); // Data in string format
171173
}
172174

@@ -195,14 +197,6 @@ public static function csrfToken()
195197
if (defined('VERIFY_CSRF_TOKEN') && VERIFY_CSRF_TOKEN === TRUE) {
196198
$_SESSION['csrf-token'] = bin2hex(random_bytes(32));
197199
return $_SESSION['csrf-token'];
198-
} else {
199-
?>
200-
" />
201-
<script>
202-
document.head.innerHTML = '';
203-
document.body.textContent = 'Please activate Basic::firewall() middleware. CSRF token verification will then be done by default.';
204-
</script>
205-
<?php
206200
}
207201
}
208202

@@ -357,14 +351,14 @@ function decrypt_v1($encrypted) {
357351
* @param boolean $boolean - TRUE or FALSE
358352
*/
359353

360-
public static function errorReporting($boolean)
354+
public static function setErrorReporting($boolean=TRUE)
361355
{
362356
if ($boolean === TRUE) {
363357
error_reporting(E_ALL);
364358
} elseif ($boolean === FALSE) {
365359
error_reporting(0);
366360
} else {
367-
exit('Boolean parameter for Basic::errorReporting() can only be TRUE or FALSE.');
361+
exit('Boolean parameter for Basic::setErrorReporting() can only be TRUE or FALSE.');
368362
}
369363
}
370364

@@ -377,7 +371,7 @@ public static function errorReporting($boolean)
377371
* @param string $uri_whitelist - Whitelisted URI RegEx characters
378372
*/
379373

380-
public static function firewall($ip_blacklist=[], $verify_csrf_token=TRUE, $post_auto_escape=TRUE, $uri_whitelist='\w\/\.\-\_\?\=\&')
374+
public static function setFirewall($ip_blacklist=[], $verify_csrf_token=TRUE, $post_auto_escape=TRUE, $uri_whitelist='\w\/\.\-\_\?\=\&')
381375
{
382376
// Deny access from blacklisted IP addresses
383377
if (isset($_SERVER['REMOTE_ADDR']) && in_array($_SERVER['REMOTE_ADDR'], $ip_blacklist)) {
@@ -430,7 +424,7 @@ public static function firewall($ip_blacklist=[], $verify_csrf_token=TRUE, $post
430424
* Force application to use TLS/HTTPS
431425
*/
432426

433-
public static function https()
427+
public static function setHttps()
434428
{
435429
if (! isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
436430
header('Location: https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
@@ -445,7 +439,7 @@ public static function https()
445439
* @param string $cipher_method - Only AES-256 GCM, CTR or CBC
446440
*/
447441

448-
public static function encryption($pass_phrase, $cipher_method='aes-256-gcm')
442+
public static function setEncryption($pass_phrase, $cipher_method='aes-256-gcm')
449443
{
450444
if (! defined('PASS_PHRASE')) {
451445
define('PASS_PHRASE', $pass_phrase);
@@ -473,7 +467,7 @@ public static function encryption($pass_phrase, $cipher_method='aes-256-gcm')
473467
* @param array $classes - Array of folders to autoload classes
474468
*/
475469

476-
public static function autoloadClass($classes)
470+
public static function setAutoloadClass($classes)
477471
{
478472
define('AUTOLOADED_FOLDERS', $classes);
479473
spl_autoload_register(function ($class_name) {
@@ -491,7 +485,7 @@ public static function autoloadClass($classes)
491485
* @param string $controller - 'HomeController@index' format
492486
*/
493487

494-
public static function homePage($controller)
488+
public static function setHomePage($controller)
495489
{
496490
if ( empty(self::segment(1)) ) {
497491
list($class, $method) = explode('@', $controller);
@@ -508,7 +502,7 @@ public static function homePage($controller)
508502
* 'index' as default method name
509503
*/
510504

511-
public static function autoRoute()
505+
public static function setAutoRoute()
512506
{
513507
if (self::segment(1) !== FALSE) { $class = ucfirst(strtolower(self::segment(1))) . 'Controller'; }
514508
if (self::segment(2) !== FALSE) { $method = strtolower(self::segment(2)); } else { $method = 'index'; }
@@ -530,7 +524,7 @@ public static function autoRoute()
530524
* 'Controller' as default controller suffix
531525
*/
532526

533-
public static function jsonRpc()
527+
public static function setJsonRpc()
534528
{
535529
// Check if there is request body
536530
if (file_get_contents('php://input') !== FALSE) {

sample-site/app.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
|--------------------------------------------------------------------------
1515
*/
1616

17-
Basic::errorReporting(TRUE); // Error reporting
18-
Basic::firewall(); // Enable firewall
19-
// Basic::https(); // Require TLS/HTTPS
20-
Basic::encryption('SecretPassPhrase123'); // Encryption cipher method and pass phrase
21-
Basic::autoloadClass(['classes', 'models', 'views', 'controllers']); // Autoload folders
22-
Basic::homePage('HomeController@index'); // Homepage
23-
Basic::autoRoute(); // Automatic '/class/method' routing
17+
Basic::setErrorReporting(); // Error reporting
18+
Basic::setFirewall(); // Enable firewall
19+
// Basic::setHttps(); // Require TLS/HTTPS
20+
Basic::setEncryption('SecretPassPhrase123'); // Encryption cipher method and pass phrase
21+
Basic::setAutoloadClass(['classes', 'models', 'views', 'controllers']); // Autoload folders
22+
Basic::setHomePage('HomeController@index'); // Homepage
23+
Basic::setAutoRoute(); // Automatic '/class/method' routing
2424

2525
/*
2626
|--------------------------------------------------------------------------
@@ -29,7 +29,7 @@
2929
*/
3030

3131
Basic::route('POST', '/jsonrpc', function() {
32-
Basic::jsonRpc(); // JSON-RPC endpoint
32+
Basic::setJsonRpc(); // JSON-RPC endpoint
3333
});
3434

3535
Basic::route('GET', '/posts', function() {
@@ -158,7 +158,7 @@
158158
}
159159

160160
if (! empty($data_output)) {
161-
Basic::apiResponse(200, json_encode($data_output));
161+
Basic::apiResponse(200, json_encode($data_output), 'application/json');
162162
} else {
163163
Basic::apiResponse(400, 'No Patient name found on search.');
164164
}

0 commit comments

Comments
 (0)