-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_form_class.php
More file actions
323 lines (232 loc) · 8.62 KB
/
Copy pathjson_form_class.php
File metadata and controls
323 lines (232 loc) · 8.62 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/*BismillahHiraManNiraheem
Ya Allah, Ya Hayyo Ya Qayoom, Ya Wawhaabo
Hazaaa min fazli Rabi.
Allah Hu Akbar,
Hasbunallah wa Namal wakeel,
Salat wa salam ala sayeidnaa Muhammad wa aalihi wa ashabil kiraam wa ambiyaa kiram wa mashakinaa wa malaikate kar muqarbeen wa sidiqeen na wa shodai wa soleheen
Madatt Ya Rasool Allah (SAW), Madatt Ya Gousul Azam, Madatt YA Faiz Ad Destani, Madatt Ya Khaliq Gudjwani, Madatt Ya Sheikh Mahmood Bukhari, Madatt Ya Sheikh NAzim Adil Al Haqqani, Madatt Ya Sahibul saif, Madatt Ya Sheikh Lokman Efenndi, Madatt ya Siqiqeen wa sohdai Bi iznillah
Allah Huma Baarik lana fir rizq wal umer e wal hasanaat...
Saqaqallah Hul azeem, wa sadqa e Rasool e Kareem (SAW)*/
// Author: Abrar Ajaz Wani
// Project Name: Json-PHP Form Handler & Data-validator
class json_form
{
protected $json_string = null;
protected $json_res = null;
protected $json_form_data = [];
protected $json_form_secure = null;
public $debug = false;
public $json_upload_dir = null;
public function __construct($json = '[]') {
$this->json_string($json);
}
// function acceptes the json string and update it at $this->josn_String
public function json_string($string) {
if ($this->isJson($string)) {
$this->json_string = $string;
if ($this->debug == true) var_dump($this->json_array());
} else {
throw new Exception("Invalid JSON format", 1);
end();
}
}
public function json_resultant($string) {
if ($this->isJson($string)) {
$this->json_res = $string;
if ($this->debug == true) var_dump($this->json_array());
} else {
throw new Exception("Invalid JSON format", 1);
end();
}
}
//drops form in html format given in the function $fn
public function new_form($fn, $var = false) {
ob_start();
$this->json_disturbute_v1($this->json_array(),$fn);
$result = ob_get_clean();
if ($var == true) {return $result;} else {echo $result;}
// var_dump($key,gettype($value));
}
//drops update form in html format given in the function $fn with values defined in $res
public function update_form($fn,$res,$var = false) {
ob_start();
$this->json_disturbute_v2($this->json_array(),$res,$fn);
$result = ob_get_clean();
if ($var == true) {return $result;} else {echo $result;}
}
private function json_disturbute_v2($array,$res,$fn) {
foreach ($array as $key => $value) {
if (gettype($value) == 'array' && $this->isAssoc($value) == true) {
call_user_func($fn,$key,'group_caption',null);
//go ahead!
$this->json_disturbute_v2($value,$res[$key],$fn);
} else if (gettype($value) == 'string') {
if (!isset($res[$key])) {continue;}
call_user_func($fn,$key,$value,null,$res[$key]);
} else if (!$this->isAssoc($value)) {
call_user_func($fn,$key,'option',$value,$res[$key]);
}
}
}
private function json_disturbute_v1($array,$fn) {
foreach ($array as $key => $value) {
if (gettype($value) == 'array' && $this->isAssoc($value) == true) {
call_user_func($fn,$key,'group_caption',null);
//go ahead!
$this->json_disturbute_v1($value,$fn);
} else if (gettype($value) == 'string') {
call_user_func($fn,$key,$value,$value);
} else if (!$this->isAssoc($value)) {
call_user_func($fn,$key,'option',$value);
}
}
}
private function json_disturbute_v3($array) {
if (!isset($return)) $return = [];
foreach ($array as $key => $value) {
if (gettype($value) == 'array' && $this->isAssoc($value) == true) {
//go ahead!
$return[$key] = $this->json_disturbute_v3($value);
} else if ($value == 'img') {
$ext = array('image/jpg','image/jpe','image/jpeg','image/jfif','image/png','image/bmp','image/dib','image/gif');
$fileN = $this->save_file($key, $ext );
$return[$key] = $this->json_data_check($key,$fileN);
} else if ($value == 'docs') {
$ext = array('text/pdf');
$fileN = $this->save_file($key, $ext );
$return[$key] = $this->json_data_check($key,$fileN);
} else {
$return[$key] = $this->json_data_check($key,$_REQUEST[str_replace(" ", "_", $key)]);
}
}
return $return;
}
public function json_handle_request($type = "POST")
{
if ($_SERVER['REQUEST_METHOD'] == $type) {
$this->json_form_data = json_encode($this->json_disturbute_v3($this->json_array()));
return $this->get_json_request();
}
}
//returrns $json_form_data
public function get_json_request(bool $is_array = true) {
if ($is_array == true) {
return json_decode($this->json_form_data,true);
} else {
return $this->json_form_data;
}
}
//returns the $this->res ; returns array or object
public function get_json_res( bool $is_array = true){
if ($is_array == true) {
return json_decode($this->json_res,$is_array);
} else {
return $this->json_res;
}
}
//returns the json_string ; returns array or object
public function json_array( bool $is_array = true) {
if ($is_array == true) {
return json_decode($this->json_string,$is_array);
} else {
return $this->json_string;
}
}
// Evlauate if the given $string is a valid JSON doc
public function isJson($string) {
$decoded = json_decode($string);
if ( !is_object($decoded) && !is_array($decoded) ) {
return false;
}
return (json_last_error() == JSON_ERROR_NONE);
}
// Evlauate if the given $string is a Associative array
private function isAssoc($array)
{
return !isset($array[0]);//(count($array) != count($array, 1));
}
//secures data
public function json_secureRule($string) {
if (!$this->isJson($string)) {
throw new Exception("Invalid JSON DATA", 1);
}
$this->json_form_secure = $string;
}
//returns the $this->res ; returns array or object
public function get_json_secureRule( bool $is_array = true){
if ($is_array == true) {
return json_decode($this->json_form_secure,$is_array);
} else {
return $this->json_form_secure;
}
}
public function json_data_check($key,$data) {
if (!is_array($this->get_json_secureRule())) {return $data;}
if (array_key_exists($key, $this->get_json_secureRule()) || array_key_exists('main_rule', $this->get_json_secureRule())) {
if (array_key_exists($key, $this->get_json_secureRule())) {$rule = $this->get_json_secureRule()[$key];}
if (array_key_exists('main_rule', $this->get_json_secureRule())) { foreach ($this->get_json_secureRule()['main_rule'] as $k) {$rule[] = $k;}}
foreach ($rule as $rule_index) {
# code...
$rule_p = explode(":", $rule_index);
if ($rule_p[0] == "max") {
if(strlen($data) > $rule_p[1]) {
throw new Exception("$key: MAX Invalid input length", 1);
return false;
}
} else if ($rule_p[0] == "min") {
if(strlen($data) < $rule_p[1]) {
throw new Exception("$key: MIN Invalid input length", 1);
return false;
}
} else if ($rule_p[0] == "filter") {
# code...
if (!filter_var($data, $rule_p[1])) {
throw new Exception("$key: Filter validation: Invalid Data found", 1);
return false;
}
} else if ($rule_p[0] == "func") {
$data = call_user_func($rule_p[1],$data);
} else if ($rule_p[0] == 'preg') {
if (!preg_match($rule_p[1], $data)) {
throw new Exception("$key Preg validation: Invalid Data found", 1);
return false;
}
}
}
}
return $data;
}
public function save_file($Key, $exts )
{
global $_FILES;
$dir = $this->json_upload_dir;
if ($_FILES) {
$fileformat = $_FILES[$Key]["type"];
if (!in_array($fileformat, $exts)) {
return null;
}
}
$rand = md5(rand());
if (strlen($_FILES[$Key]["name"]) != 0) {
$name = ($_FILES[$Key]["name"]);
$fileN = $rand . ($name);
$fileformat = $_FILES[$Key]["type"];
$target_dir = __dir__ . $dir;
$path_parts = pathinfo($fileN);
$ext = $path_parts['extension'];
$fileN = $rand .'.'.$ext;
$target_file = $target_dir . (basename($fileN));
$move = move_uploaded_file($_FILES[$Key]["tmp_name"], $target_file);
} else {
$fileN = null;
return null;
}
if ($move) {
return $fileN;
} else {
return null;
}
}
}
?>