Skip to content

Commit 4465ca1

Browse files
authored
Updated functions and app files
Added homepage( ) and error404( ) Set $valid_page = TRUE for validly rendered pages
1 parent 0f3e7f6 commit 4465ca1

File tree

3 files changed

+79
-77
lines changed

3 files changed

+79
-77
lines changed

app.php

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,35 @@
22

33
/*
44
|--------------------------------------------------------------------------
5-
| Firewall
5+
| Load BasicPHP Functions Library and Configuration File
66
|--------------------------------------------------------------------------
77
*/
88

9-
firewall();
9+
require_once 'functions.php';
10+
require_once 'config.php';
1011

1112
/*
1213
|--------------------------------------------------------------------------
13-
| SSL/HTTPS
14+
| Security
1415
|--------------------------------------------------------------------------
1516
*/
1617

17-
force_ssl();
18+
firewall(); // Firewall
19+
force_ssl(); // SSL/HTTPS
1820

1921
/*
2022
|--------------------------------------------------------------------------
21-
| JSON-RPC v2.0 Compatibility Layer with 'method' member as 'class.method'
23+
| Routing
2224
|--------------------------------------------------------------------------
2325
*/
2426

25-
route_rpc();
27+
route_rpc(); // JSON-RPC v2.0
28+
route_auto(); // Automatic '/class/method' routing
29+
homepage(); // Render homepage
2630

2731
/*
2832
|--------------------------------------------------------------------------
29-
| Render Homepage with JSON-RPC v2.0 Compatibility Layer
30-
|--------------------------------------------------------------------------
31-
*/
32-
33-
if ( empty(url_path(1)) && ! isset($json_rpc['method']) ) {
34-
35-
list($class, $method) = explode('@', HOME_PAGE);
36-
$object = new $class();
37-
return $object->$method();
38-
39-
}
40-
41-
/*
42-
|--------------------------------------------------------------------------
43-
| Automatic Routing of url_path(1) and (2) as '/class/method' path
44-
|--------------------------------------------------------------------------
45-
*/
46-
47-
route_auto();
48-
49-
/*
50-
|--------------------------------------------------------------------------
51-
| Manual Routing Using Endpoints and Wildcards to Controllers
33+
| Endpoint Routing
5234
|--------------------------------------------------------------------------
5335
*/
5436

@@ -60,15 +42,6 @@
6042
|--------------------------------------------------------------------------
6143
| Handle Error 404 - Page Not Found - Invalid URI
6244
|--------------------------------------------------------------------------
63-
|
64-
| Invalid page includes only four (4) files: the front controller (index.php),
65-
| config.php, functions.php and routes.php.
66-
|
6745
*/
6846

69-
if ( count(get_included_files()) == 4 ) {
70-
71-
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
72-
exit();
73-
74-
}
47+
error404(); // Handle Error 404

functions.php

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@
44
|--------------------------------------------------------------------------
55
| BasicPHP Functions Library
66
|--------------------------------------------------------------------------
7-
|
8-
| These are core functions necessary to run the nano-framework:
97
|
10-
| 1. url_path() - retrieves the URL path substring separated by '/'
11-
| 2. route_rpc() - JSON-RPC v2.0 compatibility layer
12-
| 3. route_auto() - automatic routing of URL path to Class and method
13-
| 4. route_class() - routes URL path request to Controllers
14-
| 5. view() - passes data and renders the View
15-
| 6. pdo_conn() - PHP Data Objects (PDO) database connection
16-
| 7. api_response() - handles API response
17-
| 8. api_call() - handles API call
18-
| 9. firewall() - web application firewall
19-
| 10. force_ssl() - force application to use SSL
20-
| 11. esc() - uses htmlspecialchars() to prevent XSS
21-
| 12. csrf_token() - uses sessions to create per request CSRF token
22-
| 13. encrypt() - encrypt data using AES-CBC-HMAC
23-
| 14. decrypt() - decrypt data using AES-CBC-HMAC
8+
| url_path() - retrieves the URL path substring separated by '/'
9+
| homepage() - render hompage
10+
| error404() - Handle Error 404 - Page Not Found - Invalid URI
11+
| route_rpc() - JSON-RPC v2.0 compatibility layer
12+
| route_auto() - automatic routing of URL path to Class and method
13+
| route_class() - routes URL path request to Controllers
14+
| view() - passes data and renders the View
15+
| pdo_conn() - PHP Data Objects (PDO) database connection
16+
| api_response() - handles API response
17+
| api_call() - handles API call
18+
| firewall() - web application firewall
19+
| force_ssl() - force application to use SSL
20+
| esc() - uses htmlspecialchars() to prevent XSS
21+
| csrf_token() - uses sessions to create per request CSRF token
22+
| encrypt() - encrypt data using AES-CBC-HMAC
23+
| decrypt() - decrypt data using AES-CBC-HMAC
2424
|
2525
*/
2626

@@ -47,13 +47,45 @@ function url_path($order)
4747

4848
}
4949

50+
/**
51+
* Render Homepage
52+
*/
53+
54+
function homepage()
55+
{
56+
57+
if ( empty(url_path(1)) ) {
58+
list($class, $method) = explode('@', HOME_PAGE);
59+
$object = new $class();
60+
return $object->$method();
61+
}
62+
63+
}
64+
65+
/**
66+
* Handle Error 404 - Page Not Found - Invalid URI
67+
* A valid page has $valid_page set to TRUE.
68+
*/
69+
70+
function error404()
71+
{
72+
73+
if ( ! isset($valid_page) || $valid_page !== TRUE ) {
74+
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
75+
exit();
76+
}
77+
78+
}
79+
5080
/**
5181
* JSON-RPC v2.0 Compatibility Layer with 'method' member as 'class.method'
5282
*/
5383

5484
function route_rpc()
5585
{
5686

87+
$valid_page = TRUE; // Set page as valid
88+
5789
// Check if HTTP request method is 'POST', if there is POSTed data, and the POSTed data is in JSON format.
5890
if ($_SERVER['REQUEST_METHOD'] == 'POST' && file_get_contents('php://input') !== FALSE && json_decode( file_get_contents('php://input'), TRUE ) !== NULL) {
5991

@@ -88,6 +120,8 @@ function route_rpc()
88120
function route_auto()
89121
{
90122

123+
$valid_page = TRUE; // Set page as valid
124+
91125
if (url_path(1) !== FALSE) { $class = ucfirst(url_path(1)) . CONTROLLER_SUFFIX; }
92126
if (url_path(2) !== FALSE) { $method = lcfirst(url_path(2)); } else { $method = METHOD_DEFAULT; }
93127

@@ -116,6 +150,8 @@ function route_auto()
116150
function route_class($http_method, $path, $class_method)
117151
{
118152

153+
$valid_page = TRUE; // Set page as valid
154+
119155
if ($_SERVER['REQUEST_METHOD'] == $http_method) {
120156

121157
// Convert '/' and wilcards (:num) and (:any) to RegEx

public/index.php

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* BasicPHP - A frameworkless library-based approach for building web applications
55
* - and application programming interfaces or API's.
66
* - The aim of the project is for developers to build applications that are
7-
* - framework-independent using raw PHP, and native functions and API's.
7+
* - framework-independent using vanilla PHP, and native functions and API's.
88
* -
99
* - To embed the application to any framework, copy BasicPHP's configuration
1010
* - file (config.php), functions library (functions.php), and the 'classes',
@@ -17,18 +17,12 @@
1717
* @license MIT License
1818
*/
1919

20-
// Register the start time/memory as a float value
21-
$time_start = microtime(TRUE);
22-
$memory_start = memory_get_usage();
20+
// // Register the start time/memory as a float value
21+
// $time_start = microtime(TRUE);
22+
// $memory_start = memory_get_usage();
2323

24-
// Bootstrap configuration
25-
require_once '../config.php';
26-
27-
// Functions library
28-
require_once '../functions.php';
29-
30-
// Routing configuration
31-
require_once '../routes.php';
24+
// Load application
25+
require_once '../app.php';
3226

3327
// // Register the end time/memory as a float value
3428
// $time_end = microtime(TRUE);
@@ -38,13 +32,12 @@
3832
// $memory_used = $memory_end - $memory_start;
3933
// echo 'Lapse Time: ' . $time_lapse . ' seconds<br />';
4034
// echo 'Memory Usage: ' . $memory_used . ' bytes<br />';
41-
/*
42-
// Compute average load speed. Set $_SESSION['speed'] as an array.
43-
if (! isset($_SESSION['speed'])) { $_SESSION['speed'] = []; }
44-
$_SESSION['speed'][] = $time_lapse;
45-
// Average load speed
46-
echo 'The average load speed is: ' . (array_sum($_SESSION['speed'])/count($_SESSION['speed']));
47-
var_dump($_SESSION['speed']);
48-
// Place a comment on session_destroy() to start computing average load speed.
49-
session_destroy();
50-
*/
35+
36+
// // Compute average load speed. Set $_SESSION['speed'] as an array.
37+
// if (! isset($_SESSION['speed'])) { $_SESSION['speed'] = []; }
38+
// $_SESSION['speed'][] = $time_lapse;
39+
// // Average load speed
40+
// echo 'The average load speed is: ' . (array_sum($_SESSION['speed'])/count($_SESSION['speed']));
41+
// var_dump($_SESSION['speed']);
42+
// // Place a comment on session_destroy() to start computing average load speed.
43+
// session_destroy();

0 commit comments

Comments
 (0)