-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.php
150 lines (138 loc) · 4.59 KB
/
App.php
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
<?php
/**
* This file is part of the Sunhill Framework package.
*
* (c) Mehmet Selcuk Batal, Sunhill Technology <batalms@gmail.com>
*
* Licensed under The GNU Lesser General Public License, version 3.0
* Redistributions of files must retain the above copyright notice.
*/
/**
* Namespace: use Core directory
*/
namespace Core;
/**
* Main application class
* Don't change the class name
*/
class App
{
/**
* URL parameters
* @var array
*/
private $routes = [];
public function __construct() {
if (SYS_SYSERR === true) { // if system error printing set true
set_exception_handler(function($exception) {
echo '<b>[Sunhill] Exception:</b> '.$exception->getMessage();
});
}
}
/**
* Catch system errors
*
* @param string $message
* @throws exception
*/
public function catchError($message = null) {
if (SYS_SYSERR === true) { // if system error printing set true
throw new \Exception($message);
} else { // if set false
if (!empty(SYS_ERRPAGE) && file_exists(SYS_BASEPATH . '/App/Controllers/' . ucfirst(SYS_ERRPAGE) . '.php')) {
header('Location: ' . SYS_BASEURL . '/' . SYS_ERRPAGE); // redirect to error page
} else {
header('Location: ' . SYS_BASEURL); // redirect to home page
}
}
}
/**
* Parse url
*
* @param string $url
* @return array
*/
public function parseUrl($url = null) {
if (empty($url)) {$url = ucfirst(SYS_HOMEPAGE);} // set url as home page
$url = explode('/', str_replace('.php', '', rtrim($url, '/'))); // parse url
foreach ($url as $key => $value) {
$this->routes[$key] = ucfirst($value); // get parameters from url
}
// if page name is empty
if (!isset($this->routes[0]) || is_null($this->routes[0]) || $this->routes[0] == 'Index') {
$this->routes[0] = ucfirst(SYS_HOMEPAGE); // home page
}
// if page name includes php extension
if (strstr($this->routes[0], '.php')) {
$this->routes[0] = ucfirst(SYS_ERRPAGE); // error page
}
return $this->routes;
}
/**
* Filter sanitize the inputs
*
* @param string $input
* @param string $content
* @param string $type
* @return boolean
*/
public function secureInput($input = null, $content = null, $type = null) {
$result = null;
$content = trim($content);
$check = filter_has_var(INPUT_POST | INPUT_GET, $content);
if (!in_array($input, ['get', 'post']) || !in_array($type, ['string', 'integer', 'float', 'email', 'url']) || $check === false) {
return $result;
}
switch ($type) {
case 'string' :
$result = filter_input(INPUT_POST | INPUT_GET, $content, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
break;
case 'integer' :
$result = filter_input(INPUT_POST | INPUT_GET, $content, FILTER_SANITIZE_NUMBER_INT);
break;
case 'float' :
$result = filter_input(INPUT_POST | INPUT_GET, $content, FILTER_SANITIZE_NUMBER_FLOAT);
break;
case 'email' :
$result = filter_input(INPUT_POST | INPUT_GET, $content, FILTER_SANITIZE_EMAIL);
break;
case 'url' :
$result = filter_input(INPUT_POST | INPUT_GET, $content, FILTER_SANITIZE_URL);
break;
}
return $result;
}
/**
* Filter sanitize the variables
*
* @param string $content
* @param string $type
* @return boolean
*/
public function secureVar($content = null, $type = null) {
$result = null;
$content = strip_tags(trim($content));
if (!in_array($type, ['string', 'integer', 'float', 'email', 'url'])) {
return $result;
}
switch ($type) {
case 'string' :
$result = filter_var($content, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
break;
case 'integer' :
$result = filter_var($content, FILTER_SANITIZE_NUMBER_INT);
break;
case 'float' :
$result = filter_var($content, FILTER_SANITIZE_NUMBER_FLOAT);
break;
case 'email' :
$result = filter_var($content, FILTER_SANITIZE_EMAIL);
break;
case 'url' :
$result = filter_var($content, FILTER_SANITIZE_URL);
break;
}
return $result;
}
}
?>