noalyss Version-10
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
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 (string) type of the variable (number,string,text,date,array,raw)
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]) ) {
166 return preg_replace("/</","<.", $this->array[$p_name]);
167 }elseif ($p_type == 'raw') {
168 $a=preg_replace("/<script/","<.", $this->array[$p_name]);
169 $a=preg_replace("/<iframe/i","<.", $this->array[$p_name]);
170 return $a;
171
172 }
173 return $this->array[$p_name];
174 }
175 else
176 {
177 return $p_default;
178 }
179 }
180 if (!array_key_exists($p_name,$this->array))
181 {
182 throw new Exception("C175."._('Paramètre invalide')."[$p_name]",
184 }
185 $this->check_type($p_name, $p_type);
186 if ( $p_type == 'string' && is_string($this->array[$p_name]) ) return preg_replace("/</","<.", $this->array[$p_name]);
187 if ( $p_type == 'raw' ) {
188 $a=preg_replace("/<script/","<.", $this->array[$p_name]);
189 $a=preg_replace("/<iframe/i","<.", $this->array[$p_name]);
190 $a=preg_replace("/<frame/i","<.", $this->array[$p_name]);
191 return $a;
192 }
193 return $this->array[$p_name];
194 }
195 catch (Exception $e)
196 {
197 throw $e;
198 }
199 }
200
201 /**
202 * @brief Retrieve from $_GET
203 * @param $p_name string of the variable
204 * @param $p_type string of the variable , opt. default string
205 * @param $p_default mixed default value is variable is not set
206 * @throws Exception if invalid
207 */
208 function get($p_name, $p_type="string", $p_default="")
209 {
210 try
211 {
212 $this->array=$_GET;
213 if (func_num_args()==1)
214 return $this->get_value($p_name);
215 if (func_num_args()==2)
216 return $this->get_value($p_name, $p_type);
217 if (func_num_args()==3)
218 return $this->get_value($p_name, $p_type, $p_default);
219 }
220 catch (Exception $exc)
221 {
222 throw $exc;
223 }
224 }
225
226 /**
227 * @brief Retrieve from $_POST
228 * @param string $p_name name of the variable
229 * @param $p_type type of the variable , opt. default string
230 * @param $p_default default value is variable is not set
231 * @throws Exception if invalid
232 */
233 function post($p_name, $p_type="string", $p_default="")
234 {
235 try
236 {
237 $this->array=$_POST;
238 if (func_num_args()==1)
239 return $this->get_value($p_name);
240 if (func_num_args()==2)
241 return $this->get_value($p_name, $p_type);
242 if (func_num_args()==3)
243 return $this->get_value($p_name, $p_type, $p_default);
244 }
245 catch (Exception $exc)
246 {
247 throw $exc;
248 }
249 }
250 /**
251 * @brief Retrieve from $_REQUEST
252 * @param $p_name string name of the variable
253 * @param $p_type string type of the variable , opt. default string
254 * @param $p_default string default value is variable is not set
255 * @throws Exception if invalid
256 */
257 function request($p_name, $p_type="string", $p_default="")
258 {
259 try
260 {
261 $this->array=$_REQUEST;
262 if (func_num_args()==1)
263 return $this->get_value($p_name);
264 if (func_num_args()==2)
265 return $this->get_value($p_name, $p_type);
266 if (func_num_args()==3)
267 return $this->get_value($p_name, $p_type, $p_default);
268 }
269 catch (Exception $exc)
270 {
271 throw $exc;
272 }
273 }
274 /**
275 * @brief Retrieve from $p_array,
276 * @param $p_name name of the variable
277 * @param $p_type type of the variable , opt. default string
278 * @param $p_default default value is variable is not set
279 * @throws Exception if invalid
280 */
281 function extract( $p_name, $p_type="string", $p_default="")
282 {
283 try
284 {
285 if ( $this->array === null ) {
286 throw new Exception( _("HTTP266:array not set")) ;
287 }
288 if (func_num_args()==1)
289 return $this->get_value($p_name);
290 if (func_num_args()==2)
291 return $this->get_value($p_name, $p_type);
292 if (func_num_args()==3)
293 return $this->get_value($p_name, $p_type, $p_default);
294 }
295 catch (Exception $exc)
296 {
297 throw $exc;
298 }
299 }
300
301 /**
302 * @brief Extract variable name from an exception message. If an exception is thrown
303 * then thanks this function it is possible to know what variable triggers
304 * the exception
305 * @param type $p_string
306 * @return string like "[variable]"
307 */
308 function extract_variable($p_string)
309 {
310 if (preg_match("/\[.*\]/", $p_string, $found)==1)
311 {
312 return $found[0];
313 }
314 }
315
316}
317
318?>
isNumber($p_int)
isDate($p_date)
Verifie qu'une date est bien formaté en d.m.y et est valable.
h( $row[ 'oa_description'])
$_REQUEST['ac']
_("actif, passif,charge,...")
$ex
$_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:356
const EXC_PARAM_VALUE
Definition constant.php:355
$_POST['ac']
Definition do.php:323
if( $delta< 0) elseif( $delta==0)