-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.php
232 lines (189 loc) · 5.73 KB
/
request.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
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
<?php
/**********************************************************
* Lidiun PHP Framework 6.0 - (http://www.lidiun.com)
*
* @Created in 26/08/2013
* @Author Dyon Enedi <dyonenedi@hotmail.com>
* @By Dyon Enedi <dyonenedi@hotmail.com>
* @Contributor Gabriela A. Ayres Garcia <gabriela.ayres.garcia@gmail.com>
* @License: free
*
**********************************************************/
namespace Lidiun_Framework_v6;
class Request
{
private static $_parameter;
private static $_post;
private static $_get;
private static $_files;
private static $_controller;
private static $_entity;
private static $_ajax;
private static $_webHeader;
private static $_ajaxHeader;
public static function load() {
// Set Path
Path::load();
// Set Link
Link::load();
// Set URL
Url::load();
// Set Language
Language::load();
// Set parameters
self::_setParameters();
// Set Controller
self::_setController();
// Set Headers
self::_setDefaultHeader();
}
#########################################################################
############################ PRIVATE METHODS ############################
#########################################################################
private static function _setParameters(){
// Treat parameters
if (!empty(Url::$_url['uri']) && is_array(Url::$_url['uri'])) {
foreach (Url::$_url['uri'] as $value) {
self::$_parameter[] = $value;
}
}
if (!empty($_POST) && is_array($_POST)) {
foreach ($_POST as $key => $value) {
self::$_post[$key] = $value;
}
}
if (!empty($_GET) && is_array($_GET)) {
foreach ($_GET as $key => $value) {
self::$_get[$key] = $value;
}
}
if (!empty($_FILES) && is_array($_FILES)) {
foreach ($_FILES as $key => $value) {
self::$_files[$key] = self::organizeArray($value);
}
}
if (empty(self::$_parameter)) {
self::$_parameter[0] = Conf::$_conf['preset']['controller_default'];
}
}
private static function _setController(){
if (empty(self::$_controller)) {
// If is an ajax request
if ((!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) {
$pathController = Path::$_path['ajax'];
self::$_ajax = true;
} else {
$pathController = Path::$_path['web'];
}
if (file_exists($pathController.self::$_parameter[0].'_controller.php')) {
// If exists a controller with first parameter
self::$_controller = strtolower(self::$_parameter[0]);
self::unsetParameter(0);
} else if (!empty(self::$_parameter[1]) && file_exists($pathController.self::$_parameter[1].'_controller.php')) {
// If exists a controller with second parameter
self::$_entity = strtolower(self::$_parameter[0]);
self::$_controller = strtolower(self::$_parameter[1]);
self::unsetParameter(0);
self::unsetParameter(1);
} else {
self::redirect('notfound');
}
}
self::orderParameter();
}
private static function _setDefaultHeader(){
self::$_webHeader = [
'Content-Type: text/html; charset=UTF-8',
'Access-Control-Allow-Methods: GET, POST',
'Access-Control-Allow-Headers: *',
];
self::$_ajaxHeader = [
'Content-Type: application/json; charset=UTF-8',
'Access-Control-Allow-Methods: GET, POST',
'Access-Control-Allow-Headers: *',
];
}
#########################################################################
########################### AUX METHODS ##############################
#########################################################################
private static function organizeArray($file){
if (is_array($file['name'])) {
foreach ($file as $key => $value) {
foreach ($value as $k => $val) {
$file[$k][$key] = $val;
unset($file[$key][$k]);
}
unset($file[$key]);
}
} else {
$file[0] = $file;
}
return $file;
}
private static function orderParameter(){
$aux = self::$_parameter;
self::$_parameter = [];
foreach ($aux as $value) {
array_push(self::$_parameter, $value);
}
}
#########################################################################
########################### PUBLIC METHODS ##############################
#########################################################################
public static function setController($controller){
self::$_controller = $controller;
}
public static function isAjax(){
return self::$_ajax;
}
public static function responseHeader() {
$header = (self::$_ajax) ? self::$_ajaxHeader: self::$_webHeader;
foreach ($header as $headerLine) {
header($headerLine);
}
}
/*
* Usefull for developer
*
*/
public static function setAjaxResponseHeader($header){
self::$_ajaxHeader = $header;
}
public static function setWebResponseHeader($header){
self::$_webHeader = $header;
}
public static function setParameter($parameter=false){
if ($parameter) {
array_push($_parameter, $parameter);
}
}
public static function unsetParameter($key){
unset(self::$_parameter[$key]);
}
public static function getParameter(){
return self::$_parameter;
}
public static function getGet(){
return self::$_get;
}
public static function getPost(){
return self::$_post;
}
public static function getFiles(){
return self::$_files;
}
public static function getEntity(){
return self::$_entity;
}
public static function getController(){
return self::$_controller;
}
public static function redirect($content){
Redirect_controller::$_content = $content;
self::setController("Lidiun_Framework_v6\Redirect");
}
public static function redirectTo($url){
header('Location: ' . Url::$_url['base'] . $url);
exit;
}
}