|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the alphaz Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Muhammad Umer Farooq (Malik) <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @link https://github.com/alphazframework/framework |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
* @since 1.0.0 |
|
13
|
|
|
* |
|
14
|
|
|
* @license MIT |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace alphaz\Input; |
|
18
|
|
|
|
|
19
|
|
|
use alphaz\http\Request; |
|
20
|
|
|
|
|
21
|
|
|
class Input |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Wordwrap. |
|
25
|
|
|
* |
|
26
|
|
|
* @param (string) $str Str to be wordwraped |
|
27
|
|
|
* |
|
28
|
|
|
* @since 1.0.0 |
|
29
|
|
|
* |
|
30
|
|
|
* @return mixed |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function wordWrapEnable($str, $width) |
|
33
|
|
|
{ |
|
34
|
|
|
if (!empty($str) && !empty($width) && $width >= 1) { |
|
35
|
|
|
return wordwrap($str, $width, '<br />\n'); |
|
36
|
|
|
} else { |
|
37
|
|
|
return false; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Check form sbumit or not. |
|
43
|
|
|
* |
|
44
|
|
|
* @param (string) $name name of submit button/ field |
|
45
|
|
|
* |
|
46
|
|
|
* @since 1.0.0 |
|
47
|
|
|
* |
|
48
|
|
|
* @return bool |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function isFromSubmit($name) |
|
51
|
|
|
{ |
|
52
|
|
|
if (isset($_REQUEST[$name])) { |
|
53
|
|
|
return true; |
|
54
|
|
|
} else { |
|
55
|
|
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Accpet input |
|
61
|
|
|
* Support get.post,put,patch,delete,others. |
|
62
|
|
|
* |
|
63
|
|
|
* @param (string) $key name of filed (required in get,post request) |
|
64
|
|
|
* |
|
65
|
|
|
* @since 1.0.0 |
|
66
|
|
|
* |
|
67
|
|
|
* @return mixed |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function input($key) |
|
70
|
|
|
{ |
|
71
|
|
|
$request = new Request(); |
|
72
|
|
|
if ($request->isGet() || $request->isHead()) { |
|
73
|
|
|
$string = $request->getQuery($key); |
|
74
|
|
|
} elseif ($request->isPost()) { |
|
75
|
|
|
$string = $request->getPost($key); |
|
76
|
|
|
} elseif ($request->isPatch()) { |
|
77
|
|
|
$string = $request->getPatch($key); |
|
78
|
|
|
} elseif ($request->isPut()) { |
|
79
|
|
|
$string = $request->getPut($key); |
|
80
|
|
|
} elseif ($request->isDelete()) { |
|
81
|
|
|
$string = $request->getDelete($key); |
|
82
|
|
|
} elseif ($request->hasFiles()) { |
|
83
|
|
|
$string = $request->getFiles($key); |
|
84
|
|
|
} else { |
|
85
|
|
|
parse_str(file_get_contents('php://input'), $_STR); |
|
86
|
|
|
$string = $_STR[$key]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return (isset($string) && !empty($string)) ? $string : false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Accpet input |
|
94
|
|
|
* Support get.post,put,patch,delete,others. |
|
95
|
|
|
* |
|
96
|
|
|
* @since 1.0.0 |
|
97
|
|
|
* |
|
98
|
|
|
* @return mixed |
|
99
|
|
|
*/ |
|
100
|
|
|
public static function inputAll() |
|
101
|
|
|
{ |
|
102
|
|
|
$request = new Request(); |
|
103
|
|
|
if ($request->isGet() || $request->isHead()) { |
|
104
|
|
|
$string = $request->getQuery(); |
|
105
|
|
|
} elseif ($request->isPost()) { |
|
106
|
|
|
$string = $request->getPost(); |
|
107
|
|
|
} elseif ($request->isPatch()) { |
|
108
|
|
|
$string = $request->getPatch(); |
|
109
|
|
|
} elseif ($request->isPut()) { |
|
110
|
|
|
$string = $request->getPut(); |
|
111
|
|
|
} elseif ($request->isDelete()) { |
|
112
|
|
|
$string = $request->getDelete(); |
|
113
|
|
|
} elseif ($request->hasFiles()) { |
|
114
|
|
|
$string = $request->getFiles(); |
|
115
|
|
|
} else { |
|
116
|
|
|
parse_str(file_get_contents('php://input'), $_STR); |
|
117
|
|
|
$string = $_STR; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return (isset($string) && !empty($string)) ? $string : false; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Clean input. |
|
125
|
|
|
* |
|
126
|
|
|
* @param (string) $input string |
|
127
|
|
|
* @param (string) $type secured,root |
|
128
|
|
|
* |
|
129
|
|
|
* @since 1.0.0 |
|
130
|
|
|
* |
|
131
|
|
|
* @return mixed |
|
132
|
|
|
*/ |
|
133
|
|
|
public static function clean($input, $type) |
|
134
|
|
|
{ |
|
135
|
|
|
if (!empty($input)) { |
|
136
|
|
|
if (!empty($type)) { |
|
137
|
|
|
if ($type === 'secured') { |
|
138
|
|
|
return stripslashes(trim(htmlentities($input, ENT_QUOTES | ENT_HTML5, 'UTF-8'))); |
|
139
|
|
|
} elseif ($type === 'root') { |
|
140
|
|
|
return stripslashes(trim(htmlentities(htmlspecialchars(strip_tags($input), ENT_QUOTES | ENT_HTML5, 'UTF-8'), ENT_QUOTES | ENT_HTML5, 'UTF-8'))); |
|
141
|
|
|
} |
|
142
|
|
|
} else { |
|
143
|
|
|
return false; |
|
144
|
|
|
} |
|
145
|
|
|
} else { |
|
146
|
|
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Decode HTML entity. |
|
152
|
|
|
* |
|
153
|
|
|
* @param (string) $input string. |
|
154
|
|
|
* |
|
155
|
|
|
* @since 1.0.0 |
|
156
|
|
|
* |
|
157
|
|
|
* @return mixed |
|
158
|
|
|
*/ |
|
159
|
|
|
public function decodeHtmlEntity($input) |
|
160
|
|
|
{ |
|
161
|
|
|
return html_entity_decode($input, ENT_QUOTES | ENT_HTML5, 'UTF-8'); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Restore new lines. |
|
166
|
|
|
* |
|
167
|
|
|
* @param (string) $str string that tobe restored new lines |
|
168
|
|
|
* |
|
169
|
|
|
* @since 1.0.0 |
|
170
|
|
|
* |
|
171
|
|
|
* @return mixed |
|
172
|
|
|
*/ |
|
173
|
|
|
public static function restoreLineBreaks($str) |
|
174
|
|
|
{ |
|
175
|
|
|
if (isset($str) and strlen($str) !== 0) { |
|
176
|
|
|
$result = str_replace(PHP_EOL, "\n\r<br />\n\r", $str); |
|
177
|
|
|
|
|
178
|
|
|
return $result; |
|
179
|
|
|
} else { |
|
180
|
|
|
return false; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|