1
1
<?php
2
2
3
+ namespace App ;
4
+
3
5
class Input {
6
+ /**
7
+ * Must take @bool to proceed
8
+ * refer to getMagicQuotes() method
9
+ *
10
+ * @var boolean
11
+ */
12
+ private static $ magic_quotes_exists ;
13
+
14
+
4
15
public static function exists ($ action = 'post ' ) {
5
16
switch ($ action ) {
6
17
case 'post ' :
7
18
return (!empty ($ _POST )) ? true : false ;
8
19
break ;
20
+
9
21
case 'get ' :
10
22
return (!empty ($ _GET )) ? true : false ;
11
23
break ;
24
+
12
25
default :
13
26
return false ;
14
27
}
28
+
15
29
}
16
30
17
- public static function get ($ name ) {
31
+
32
+ public static function get ($ name , $ purify = false ) {
33
+
18
34
if (isset ($ _POST [$ name ])) {
19
- return $ _POST [$ name ];
35
+ $ var = $ _POST [$ name ];
36
+
37
+ return self ::verify ($ var , $ purify );
20
38
}else if (isset ($ _GET [$ name ])) {
21
- return $ _GET [$ name ];
39
+ $ var = $ _GET [$ name ];
40
+
41
+ return self ::verify ($ var , $ purify );
42
+ }
43
+
44
+ return false ;
45
+ }
46
+
47
+
48
+ public static function verify ($ input , $ purify = false ) {
49
+ $ input = is_array ($ input ) ? array_map ('self::verify ' , $ input ) : static ::sanitize ($ input , $ purify );
50
+
51
+ return $ input ;
52
+ }
53
+
54
+ public static function check ($ name , $ in = '' ) {
55
+ $ in = strtolower ($ in );
56
+
57
+ switch ($ in ) {
58
+ case 'get ' :
59
+ if (isset ($ _GET [$ name ])) {
60
+ return true ;
61
+ }
62
+ break ;
63
+
64
+ case 'post ' :
65
+ if (isset ($ _POST [$ name ])) {
66
+ return true ;
67
+ }
68
+ break ;
69
+
70
+ default :
71
+ if (isset ($ _POST [$ name ])) {
72
+ return true ;
73
+ }else if (isset ($ _GET [$ name ])) {
74
+ return true ;
75
+ }
22
76
}
23
- return '' ;
77
+
78
+ return false ;
79
+ }
80
+
81
+
82
+ public function sanitize ($ string , $ purify = false )
83
+ {
84
+ $ string = trim ($ string );
85
+
86
+ if ($ purify ==true )
87
+ $ string = stripslashes (strip_tags ($ string ));
88
+
89
+ $ string = htmlentities ($ string , ENT_QUOTES , "UTF-8 " );
90
+
91
+ return $ string ;
24
92
}
25
- }
93
+
94
+ public function getMagicQuotes () {
95
+ if (function_exists ('get_magic_quotes_gpc ' )) {
96
+ self ::$ magic_quotes_exists = true ;
97
+ return self ::$ magic_quotes_exists ;
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Convert post request into url param string
103
+ *
104
+ * @param array $stream
105
+ * @param string $initParam
106
+ * @return string $params
107
+ */
108
+ public function splitStream ($ stream , $ initParam = '' ) {
109
+ $ params = !empty ($ initParam ) ? $ initParam : '' ;
110
+ $ magicQuotesExist = self ::getMagicQuotes ();
111
+
112
+ foreach ($ stream as $ key => $ value ) {
113
+ if ($ magicQuotesExist == true && get_magic_quotes_gpc () == 1 ) {
114
+ $ value = urlencode (stripslashes ($ value ));
115
+ }else {
116
+ $ value = urlencode ($ value );
117
+ }
118
+
119
+ $ params .= !empty ($ params ) ? '& ' : '' ;
120
+ $ params .= "$ key= $ value " ;
121
+ }
122
+
123
+ return $ params ;
124
+ }
125
+
126
+ }
0 commit comments