noalyss Version-9
http_input.class.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of PhpCompta.
5 *
6 * PhpCompta is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * NOALYSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with PhpCompta; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20// Copyright (2016) Author Dany De Bontridder <dany@alchimerys.be>
21
22/**
23 * @file
24 * @brief manage the http input (get , post, request)
25 */
26
27
28/**
29 * @file
30 * @brief manage the http input (get , post, request)
31 */
32/**
33 * @class HttpInput
34 * @brief manage the http input (get , post, request) and extract from an array
35 */
36
38{
39
40 private $array;
41 private $empty; //!< if empty that replace by $empty
42
43 function _construct()
44 {
45 $this->array=null;
46 $this->empty="";
47 }
48 public function __toString(): string
49 {
50 return "http_input".var_export($this,true);
51 }
52 public function get_array()
53 {
54 return $this->array;
55 }
56
57 public function get_empty()
58 {
59 return $this->empty;
60 }
61
62 public function set_array($array)
63 {
64 $this->array=$array;
65 return $this;
66 }
67 /*!
68 * \brief $empty replace the empty value
69 *
70 */
71 public function set_empty($empty)
72 {
73 $this->empty=$empty;
74 return $this;
75 }
76
77 /**
78 * \brief Check the type of the value
79 * @param $p_name name of the variable
80 * @param $p_type type of the variable (number,string,text,date,array)
81 * @throws Exception if the variable doesn't exist or type incorrect
82 * @todo Add regex:pattern
83 */
84 function check_type($p_name, $p_type)
85 {
86 try
87 {
88 // no check on string
89 if ($p_type=="string" || $p_type=="text")
90 {
91 return;
92 }
93 // Check if number
94 else if ($p_type=="number")
95 {
96 if (trim($this->array[$p_name]) == "")
97 {
98 $this->array[$p_name]=$this->empty;
99 }
100
101 if ( isNumber($this->array[$p_name])==0 )
102 {
103 throw new Exception(_("Type invalide")."[ $p_name ] = {$this->array[$p_name]}"
105 }
106 $this->array[$p_name]=h($this->array[$p_name]);
107 }
108 // Check if date dd.mm.yyyy
109 else if ($p_type=="date")
110 {
111 if (trim($this->array[$p_name]) == "" )
112 {
113 $this->array[$p_name]=$this->empty;
114 }
115 if (isDate($this->array[$p_name]) <> $this->array[$p_name])
116 {
117 throw new Exception(_("Type invalide")."[ $p_name ] = {$this->array[$p_name]}"
119 }
120 $this->array[$p_name]=h($this->array[$p_name]);
121 }
122 else if ($p_type=="raw") {
123 /* NoOperation*/
124 }
125 else if ($p_type=="array")
126 {
127 if ( empty($this->array[$p_name]) )
128 {
129 $this->array[$p_name]=$this->empty;
130 }
131 if (!is_array($this->array[$p_name]) ) {
132 throw new Exception(_("Type invalide")."[ $p_name ] = {$this->array[$p_name]}"
134 }
135 if (is_string($this->array )) {
136 $this->array[$p_name]=h($this->array[$p_name]);
137 }
138 }else {
139 throw new Exception(_("Unknown type"));
140 }
141 }
142 catch (Exception $ex)
143 {
144 throw $ex;
145 }
146 }
147
148 /**
149 * @brief Retrieve from $this->array the variable
150 * @param $p_name name of the variable
151 * @param $p_type type of the variable (number,string or text,date('dd.mm.yyyy'),array)
152 * @param $p_default default value is variable
153 * @throws Exception if invalid
154 * @see check_type
155 */
156 function get_value($p_name, $p_type="string", $p_default="")
157 {
158 try
159 {
160 if (func_num_args()==3)
161 {
162 if (array_key_exists($p_name,$this->array) )
163 {
164 $this->check_type($p_name, $p_type);
165 if ($p_type != 'raw' && is_string($this->array[$p_name]) ) return preg_replace("/</","< ", $this->array[$p_name]);
166 return $this->array[$p_name];
167 }
168 else
169 {
170 return $p_default;
171 }
172 }
173 if (!array_key_exists($p_name,$this->array))
174 {
175 throw new Exception(_('Paramètre invalide')."[$p_name]",
177 }
178 $this->check_type($p_name, $p_type);
179 if ( is_string($this->array[$p_name]) ) return preg_replace("/</","< ", $this->array[$p_name]);
180 return $this->array[$p_name];
181 }
182 catch (Exception $e)
183 {
184 throw $e;
185 }
186 }
187
188 /**
189 * @brief Retrieve from $_GET
190 * @param $p_name name of the variable
191 * @param $p_type type of the variable , opt. default string
192 * @param $p_default default value is variable is not set
193 * @throws Exception if invalid
194 */
195 function get($p_name, $p_type="string", $p_default="")
196 {
197 try
198 {
199 $this->array=$_GET;
200 if (func_num_args()==1)
201 return $this->get_value($p_name);
202 if (func_num_args()==2)
203 return $this->get_value($p_name, $p_type);
204 if (func_num_args()==3)
205 return $this->get_value($p_name, $p_type, $p_default);
206 }
207 catch (Exception $exc)
208 {
209 throw $exc;
210 }
211 }
212
213 /**
214 * @brief Retrieve from $_POST
215 * @param string $p_name name of the variable
216 * @param $p_type type of the variable , opt. default string
217 * @param $p_default default value is variable is not set
218 * @throws Exception if invalid
219 */
220 function post($p_name, $p_type="string", $p_default="")
221 {
222 try
223 {
224 $this->array=$_POST;
225 if (func_num_args()==1)
226 return $this->get_value($p_name);
227 if (func_num_args()==2)
228 return $this->get_value($p_name, $p_type);
229 if (func_num_args()==3)
230 return $this->get_value($p_name, $p_type, $p_default);
231 }
232 catch (Exception $exc)
233 {
234 throw $exc;
235 }
236 }
237 /**
238 * @brief Retrieve from $_REQUEST
239 * @param $p_name string name of the variable
240 * @param $p_type string type of the variable , opt. default string
241 * @param $p_default string default value is variable is not set
242 * @throws Exception if invalid
243 */
244 function request($p_name, $p_type="string", $p_default="")
245 {
246 try
247 {
248 $this->array=$_REQUEST;
249 if (func_num_args()==1)
250 return $this->get_value($p_name);
251 if (func_num_args()==2)
252 return $this->get_value($p_name, $p_type);
253 if (func_num_args()==3)
254 return $this->get_value($p_name, $p_type, $p_default);
255 }
256 catch (Exception $exc)
257 {
258 throw $exc;
259 }
260 }
261 /**
262 * @brief Retrieve from $p_array,
263 * @param $p_name name of the variable
264 * @param $p_type type of the variable , opt. default string
265 * @param $p_default default value is variable is not set
266 * @throws Exception if invalid
267 */
268 function extract( $p_name, $p_type="string", $p_default="")
269 {
270 try
271 {
272 if ( $this->array === null ) {
273 throw new Exception( _("HTTP266:array not set")) ;
274 }
275 if (func_num_args()==1)
276 return $this->get_value($p_name);
277 if (func_num_args()==2)
278 return $this->get_value($p_name, $p_type);
279 if (func_num_args()==3)
280 return $this->get_value($p_name, $p_type, $p_default);
281 }
282 catch (Exception $exc)
283 {
284 throw $exc;
285 }
286 }
287
288 /**
289 * @brief Extract variable name from an exception message. If an exception is thrown
290 * then thanks this function it is possible to know what variable triggers
291 * the exception
292 * @param type $p_string
293 * @return string like "[variable]"
294 */
295 function extract_variable($p_string)
296 {
297 if (preg_match("/\[.*\]/", $p_string, $found)==1)
298 {
299 return $found[0];
300 }
301 }
302
303}
304
305?>
isNumber($p_int)
Definition: ac_common.php:215
isDate($p_date)
Definition: ac_common.php:236
h( $row[ 'oa_description'])
$_REQUEST['ac']
$ex
Definition: balance.inc.php:45
$_GET['qcode']
manage the http input (get , post, request) and extract from an array
extract_variable($p_string)
Extract variable name from an exception message.
extract( $p_name, $p_type="string", $p_default="")
Retrieve from $p_array,.
set_array($array)
request($p_name, $p_type="string", $p_default="")
Retrieve from $_REQUEST.
post($p_name, $p_type="string", $p_default="")
Retrieve from $_POST.
check_type($p_name, $p_type)
Check the type of the value.
set_empty($empty)
$empty replace the empty value
$empty
if empty that replace by $empty
get_value($p_name, $p_type="string", $p_default="")
Retrieve from $this->array the variable.
const EXC_PARAM_TYPE
Definition: constant.php:344
const EXC_PARAM_VALUE
Definition: constant.php:343
$_POST['ac']
Definition: do.php:310