Odyssey
dms_imp_val.i
1 <?php
2  /**
3  *
4  *
5  *
6  */
7 
8  /**
9  * Used by HCU_FilterInput to search in POST/GET superglobals
10  */
11  define("HCUFILTER_REQUEST", "REQUEST");
12  /**
13  * Used by HCU_FilterInput to search in 'ENV' superglobal
14  */
15  define("HCUFILTER_ENV", "ENV");
16  /**
17  * Used by HCU_FilterInput to simply filter the value using filter_var
18  */
19  define("HCUFILTER_VAR", "VAR");
20  /**
21  * Used by HCU_FilterInput to search in 'COOKIE' superglobal
22  */
23  define("HCUFILTER_COOKIE", "COOKIE");
24  /**
25  * Used by HCU_FilterInput to search in 'SERVER' superglobal
26  */
27  define("HCUFILTER_SERVER", "SERVER");
28  /**
29  * HomeCU - Filter Input to be ONLY digits 0-9
30  */
31  define("HCUFILTER_INPUT_DIGITS", "digits");
32  /**
33  * HomeCU - Filter Input to be ONLY STRING (nothing is sanitized for this type to prevent lost characters)
34  * For legacy this alone doesn't specify the type of data being imported
35  */
36  define("HCUFILTER_INPUT_STRING", "string");
37  /**
38  * HomeCU - Filter Input to be an array. This will import all array elements.
39  * For legacy this alone doesn't specify the type of data being imported
40  */
41  define("HCUFILTER_INPUT_ARRAY", "array");
42 
43 
44 
45 function dms_import($dms_allowed) {
46  foreach ($dms_allowed as $key => $type) {
47  if (!isset($_REQUEST[$key]) && strpos($type,"prefix_") === false) {
48  ${$key} = NULL;
49  continue;
50  }
51  switch ($type) {
52  case "digits":
53  if (strlen(trim($_REQUEST[$key])) > 0 && ctype_digit((string)trim($_REQUEST[$key]))) {
54  global ${$key};
55  ${$key} = trim($_REQUEST[$key]);
56  }
57  break;
58  case "string":
59  if (is_string($_REQUEST[$key])) {
60  global ${$key};
61  ${$key} = trim($_REQUEST[$key]);
62  }
63  break;
64  case "array":
65  if (is_array($_REQUEST[$key]) && sizeof($_REQUEST[$key])) {
66  global ${$key};
67  ${$key} = $_REQUEST[$key];
68  }
69  break;
70  case "prefix_d":
71  foreach (array_keys($_REQUEST) as $rkey) {
72  if (strpos($rkey,$key) !== FALSE) {
73  if (strlen(trim($_REQUEST[$rkey])) > 0 && ctype_digit((string)trim($_REQUEST[$rkey]))) {
74  global ${$rkey};
75  ${$rkey} = trim($_REQUEST[$rkey]);
76  }
77  }
78  }
79  break;
80  case "prefix_s":
81  foreach (array_keys($_REQUEST) as $rkey) {
82  if (strpos($rkey,$key) !== FALSE) {
83  if (is_string($_REQUEST[$rkey])) {
84  global ${$rkey};
85  ${$rkey} = trim($_REQUEST[$rkey]);
86  }
87  }
88  }
89  break;
90  case "prefix_a":
91  foreach (array_keys($_REQUEST) as $rkey) {
92  if (strpos($rkey,$key) !== FALSE) {
93  if (is_array($_REQUEST[$rkey]) && sizeof($_REQUEST[$rkey])) {
94  global ${$rkey};
95  ${$rkey} = $_REQUEST[$rkey];
96  }
97  }
98  }
99  break;
100  }
101  }
102 }
103 /*
104  * Function: dms_import_v2
105  * Purpose: This function is the same code as dms_import_val, however, instead of
106  * putting the values in the form scope, it will add the values to the
107  * p_hb_env in the $p_hb_key value
108  * This function should be called AFTER cu_globals.i, to ensure the values aren't reset
109  * Parameters: p_hb_env -- This is a REFERENCE to the HB_ENV variable
110  * p_hb_key -- This is the name of the key:array where the values will be saved
111  * dms_allowed -- This is the list of fields to import
112  */
113 function dms_import_v2(&$p_hb_env, $p_hb_key, $dms_allowed) {
114  foreach ($dms_allowed as $key => $type) {
115  if (!isset($_REQUEST[$key]) && strpos($type, "prefix_") === false) {
116  $p_hb_env[$p_hb_key][$key] = NULL;
117  continue;
118  }
119  switch ($type) {
120  case "digits":
121  if (strlen(trim($_REQUEST[$key])) > 0 && ctype_digit((string)trim($_REQUEST[$key]))) {
122  $p_hb_env[$p_hb_key][$key] = $_REQUEST[$key];
123  }
124  break;
125  case "string":
126  if (is_string($_REQUEST[$key])) {
127  $p_hb_env[$p_hb_key][$key] = $_REQUEST[$key];
128  }
129  break;
130  case "array":
131  if (is_array($_REQUEST[$key]) && sizeof($_REQUEST[$key])) {
132  $p_hb_env[$p_hb_key][$key] = $_REQUEST[$key];
133  }
134  break;
135  case "prefix_d":
136  foreach (array_keys($_REQUEST) as $rkey) {
137  if (strpos($rkey,$key) !== FALSE) {
138  if (strlen(trim($_REQUEST[$rkey])) > 0 && ctype_digit((string)trim($_REQUEST[$rkey]))) {
139  $p_hb_env[$p_hb_key][$key] = trim($_REQUEST[$key]);
140  }
141  }
142  }
143  break;
144  case "prefix_s":
145  foreach (array_keys($_REQUEST) as $rkey) {
146  if (strpos($rkey,$key) !== FALSE) {
147  if (is_string($_REQUEST[$rkey])) {
148  $p_hb_env[$p_hb_key][$rkey] = trim($_REQUEST[$rkey]);
149  }
150  }
151  }
152  break;
153  case "prefix_a":
154  foreach (array_keys($_REQUEST) as $rkey) {
155  if (strpos($rkey,$key) !== FALSE) {
156  if (is_array($_REQUEST[$rkey]) && sizeof($_REQUEST[$rkey])) {
157  $p_hb_env[$p_hb_key][$key] = $_REQUEST[$rkey];
158  }
159  }
160  }
161  break;
162  }
163  }
164 }
165 
166 
167 /**
168  *
169  * Import Variables From the POST or GET and put into the HB_ENV array
170  * If a variable is NOT in the _POST or _GET, then be sure to set the value to NULL
171  *
172  * @param class $pHbEnv - (by reference)
173  * @param string $pHbKey -
174  * @param array[] $pVarAllowed - This is the colname=>datatype array list that defines the
175  * values to fetch.
176  * Allowed datatypes
177  * {digits, string, array, prefix_d, prefix_s, prefix_a}
178  *
179  * LEGACY FORMAT -- This should work by replacing dms_import_v2 with HCU_ImportVars
180  * $input_array = array("col1"=>"digits", "col2"=>"string", "col3"=>"array", "col4"=>"prefix_d");
181  * HCU_ImportVars($HB_ENV, "HCUPOST", $input_array)
182  * ** NOTE: THERE IS NOT prefix_d comparable option with the filter_input functionality
183  * PHP FILTER INPUT FORMAT
184  * layout
185  * the column name is the key of the associative array.
186  * the value MUST be an array with at least the filter option set.
187  * to use the raw value use FILTER_DEFAULT
188  * examples
189  * $input_array = array("col1"=>array('filter'=>FILTER_VALIDATE_INT),
190  * "col2"=>array('filter'=>FILTER_SANITIZE_FULL_SPECIAL_CHARS),
191  * "col3"=>array('filter'=>FILTER_VALIDATE_INT, 'options' => array('flags'=>FILTER_REQUIRE_ARRAY)),
192  * "col4"=>array('filter'=>FILTER_VALIDATE_REGEXP), 'options' => array('options'=>array('regexp'=>'/[0-9]* /')))
193  *
194  * @return true - Always returns true
195  *
196  */
197 function HCU_ImportVars (&$pHbEnv, $pHbKey, $pVarAllowed) {
198 
199  $newValues = '';
200 
201  try {
202  /*
203  *
204  * LOOP THROUGH THE dms_allowed
205  * When I determine the name to look for , then I can call the HCU_FilterInput
206  *
207  *
208  */
209  if (!is_array($pVarAllowed)) {
210  throw new exception('Not an Array', '900');
211  }
212 
213  // * It's an array -- Loop through the values
214  foreach ($pVarAllowed as $fieldName => $fieldType) {
215 
216  $fieldValue = false;
217  if (!is_array($fieldType)) {
218  /**
219  * LEGACY FILTERING
220  */
221  $prefixType = ''; // ** Reset prefix type
222  $prefixArray = false; // ** Reset prefix array flag
223  switch ($fieldType) {
224  case HCUFILTER_INPUT_DIGITS:
225  $fieldValue = HCU_FilterInput(HCUFILTER_REQUEST, $fieldName, FILTER_VALIDATE_INT);
226  // ** DO NOT ALLOW [+/-] - In Legacy it would Fail for this reason
227  $fieldValue = (strpos($fieldValue, "-") !== false ? NULL : $fieldValue);
228  $fieldValue = (strpos($fieldValue, "+") !== false ? NULL : $fieldValue);
229  break;
230  case HCUFILTER_INPUT_STRING:
231  /*
232  * LEGACY FILTER STRING should let values in with a similar fashion as in mammoth.
233  * Added the 'flags' array so single quotes would not be encoded
234  */
235  $fieldValue = HCU_FilterInput(HCUFILTER_REQUEST, $fieldName, FILTER_SANITIZE_STRING, false, array('flags' => FILTER_FLAG_NO_ENCODE_QUOTES));
236 
237  break;
238  case HCUFILTER_INPUT_ARRAY:
239  $fieldValue = HCU_FilterInput(HCUFILTER_REQUEST, $fieldName, FILTER_DEFAULT, true);
240  break;
241  case "prefix_a":
242  // if I use HCU_FilterInput $prefixType = ($prefixType == '' ? FILTER_VALIDATE_INT : $prefixType);
243  $prefixType = ($prefixType == '' ? HCUFILTER_INPUT_ARRAY: $prefixType);
244  $prefixArray = true;
245  case "prefix_d":
246  // if I use HCU_FilterInput $prefixType = ($prefixType == '' ? FILTER_VALIDATE_INT : $prefixType);
247  $prefixType = ($prefixType == '' ? HCUFILTER_INPUT_DIGITS : $prefixType);
248  case "prefix_s":
249  // if I use HCU_FilterInput $prefixType = ($prefixType == '' ? FILTER_SANITIZE_STRING : $prefixType);
250  $prefixType = ($prefixType == '' ? HCUFILTER_INPUT_STRING : $prefixType);
251  // ** FOR PREFIX TYPES, maybe I should use a recursive call...
252  // * to call this function again with correct parameters for the variables found...
253  foreach (array_keys($_REQUEST) as $reqKey) {
254  // ** Loop through each Value in request.
255  if (strpos($reqKey, $fieldName) !== FALSE) {
256  //$fieldValue = HCU_FilterInput(HCUFILTER_REQUEST, $fieldName, $prefixType, $prefixArray);
257  $prefixArray = array($reqKey => $prefixType);
258  $retPrefix = HCU_ImportVars($pHbEnv, $pHbKey, $prefixArray);
259  // In this instance the HB_ENV is going to be populated with the subsequent calls
260  }
261  }
262  break;
263  }
264 
265  } else {
266  /**
267  * NEW FILTER OPTIONS
268  * THEY can use the filter_options directly
269  *
270  * fieldType should be an array. With at least
271  * 'filter' required
272  * 'options' optional
273  */
274  // ** Return the results from filter_input_array and determine how to convert to our standard response
275  $fieldFilter = HCU_array_key_value('filter', $fieldType);
276  $fieldFilter = ($fieldFilter == 0 ? FILTER_DEFAULT : $fieldFilter);
277 
278  $fieldFlags = HCU_array_key_value('flags', $fieldType);
279  $fieldIsArray = ($fieldFlags == FILTER_REQUIRE_ARRAY ? true : false);
280 
281  $fieldOptions = HCU_array_key_value('options', $fieldType);
282  $fieldValue = HCU_FilterInput(HCUFILTER_REQUEST, $fieldName, $fieldFilter, $fieldIsArray, $fieldOptions);
283  }
284  if ($fieldValue !== false) {
285  // ** set with without the hb_env key
286  if ($pHbKey == '') {
287  $pHbEnv[$fieldName] = $fieldValue;
288  } else {
289  $pHbEnv[$pHbKey][$fieldName] = $fieldValue;
290  }
291  }
292  }
293 
294  /*
295  * SET DEFAULT KEY ARRAY
296  * If there will be an array key used and it is NOT already set
297  * Setting this prevents undefined variable references to the KEY when nothing was imported
298  */
299  if ($pHbKey != '' && !isset($pHbEnv[$pHbKey])) {
300  $pHbEnv[$pHbKey] = Array();
301  }
302 
303  } catch (exception $e) {
304  // * what to do on Error?
305  exit;
306  }
307 
308  return true;
309 }
310 
311 /**
312  *
313  * Get the value of a variable
314  * This routine will search for a variable in one of the superglobals
315  * If it is being sought with REQUEST then the order will be
316  * POST
317  * GET
318  *
319  * In this manner it should perform similar to use _REQUEST
320  *
321  * @param const $pInputType - These are custom made, to handle instances when we want to search for both POST/GET, but also want to search individual ENV
322  * HCUFILTER_REQUEST (search POST/GET) HCUFILTER_ENV (search ENV)
323  * @param mixed $pKeyName - This is the name of the variable for which we are searching the value (for HCUFILTER_REQUEST, HCUFILTER_ENV)
324  * @param int $pFilterType - This is the PHP validation {filter / sanitize} option to apply
325  *
326  *
327  * @return mixed - NULL - Returns NULL if values was
328  *
329  */
330 function HCU_FilterInput ($pInputType, $pKeyName, $pFilterType, $pIsArray=false, $pAddOptions=false) {
331 
332  // Start the value as false
333  $retVal = false;
334  $filterOptions = Array();
335  $filterType = 0;
336 
337 
338  if (is_array($pAddOptions)) {
339  /* Pass through the value in pAddOptions to the filter_input function */
340  $filterOptions = $pAddOptions;
341  }
342 
343  if ($pIsArray) {
344  // * Add the Require attribute to any flags value that is already set
345  $filterOptions['flags'] = intval(HCU_array_key_value('flags', $filterOptions)) + FILTER_REQUIRE_ARRAY;
346  }
347 
348  // -- OLD STYLE DEPRECATED $filterOptions = ($pAddOptions !== false ? $pAddOptions : NULL);
349  // ** For Arrays, it will override the pAddOptions?? Possible to combine later
350  // -- OLD STYLE DEPRECATED $filterOptions = ($pIsArray ? FILTER_REQUIRE_ARRAY : $filterOptions);
351 
352  switch ($pInputType) {
353  case HCUFILTER_REQUEST:
354  /*
355  * GET VALUE FROM EITHER _POST or _GET SUPERGLOBAL
356  */
357  /* First Look in the _POST SUPERGLOBAL */
358  if (HCU_array_key_exists($pKeyName, $_POST)) {
359  $retVal = filter_input(INPUT_POST, $pKeyName, $pFilterType, $filterOptions);
360  } elseif (HCU_array_key_exists($pKeyName, $_GET)) {
361  $retVal = filter_input(INPUT_GET, $pKeyName, $pFilterType, $filterOptions);
362  }
363 
364  break;
365  case HCUFILTER_ENV: /* ENV SUPERGLOBAL */
366  $filterType = INPUT_ENV;
367  case HCUFILTER_COOKIE: /* COOOKIE SUPERGLOBAL */
368  $filterType = ($filterType == 0 ? INPUT_COOKIE : $filterType);
369  case HCUFILTER_SERVER: /* SERVER SUPERGLOBAL */
370  $filterType = ($filterType == 0 ? INPUT_SERVER : $filterType);
371  /*
372  * GET VALUE FROM _ENV SUPERGLOBAL
373  */
374  $retVal = filter_input($filterType, $pKeyName, $pFilterType, $filterOptions);
375  break;
376  case HCUFILTER_VAR:
377  /*
378  * USE filter_var to filter the variable
379  */
380  $retVal = filter_var($pKeyName, $pFilterType, $filterOptions);
381  break;
382  default:
383  // ** DO NOTHING
384  }
385  return $retVal;
386 }
387 
388 /**
389  *
390  * Get the value of a variable using the php filters. The value is passed in.
391  *
392  *
393  * @param mixed $pValue - This is the value of the variable we want to filter
394  * @param int $pFilterType - This is the PHP validation {filter / sanitize} option to apply
395  *
396  *
397  * @return mixed - NULL - Returns NULL if values was
398  *
399  */
400 function HCU_FilterVar( $pValue, $pFilterType, $pIsArray=false, $pAddOptions=false ) {
401 
402  // Start the value as false
403  $retVal = false;
404  $filterOptions = Array();
405 
406  if (is_array($pAddOptions)) {
407  /* Pass through the value in pAddOptions to the filter_input function */
408  $filterOptions = $pAddOptions;
409  }
410 
411  if ($pIsArray) {
412  // * Add the Require attribute to any flags value that is already set
413  $filterOptions['flags'] = intval(HCU_array_key_value('flags', $filterOptions)) + FILTER_REQUIRE_ARRAY;
414  }
415 
416  // -- OLD STYLE DEPRECATED $filterOptions = ($pAddOptions !== false ? $pAddOptions : NULL);
417  // ** For Arrays, it will override the pAddOptions?? Possible to combine later
418  // -- OLD STYLE DEPRECATED $filterOptions = ($pIsArray ? FILTER_REQUIRE_ARRAY : $filterOptions);
419 
420  /*
421  * USE filter_var to filter the variable
422  */
423  $retVal = filter_var( $pValue, $pFilterType, $filterOptions );
424 
425  return $retVal;
426 }
427 
428 /**
429  * HCU_ImportArray
430  *
431  * Import Variables from the given input array to the given output array, using the fitler instructions
432  * in the Allowed array.
433  * If a variable is NOT in the input array, but is in the Allowed array, then be sure to set the value to NULL
434  *
435  * @param array $pOutput - (by reference) output array
436  * @param array $pInput - input array
437  * @param array[] $pVarAllowed - This is the colname=>datatype array list that defines the
438  * values to fetch.
439  * PHP FILTER INPUT FORMAT
440  * layout
441  * the column name is the key of the associative array.
442  * the value MUST be an array with at least the filter option set.
443  * to use the raw value use FILTER_DEFAULT
444  * examples
445  * $allowedArray = array("col1"=>array('filter'=>FILTER_VALIDATE_INT),
446  * "col2"=>array('filter'=>FILTER_SANITIZE_FULL_SPECIAL_CHARS),
447  * "col3"=>array('filter'=>FILTER_VALIDATE_INT, 'options' => array('flags'=>FILTER_REQUIRE_ARRAY)),
448  * "col4"=>array('filter'=>FILTER_VALIDATE_REGEXP), 'options' => array('options'=>array('regexp'=>'/[0-9]* /')))
449  *
450  * @return boolean - Returns true on success, false on error
451  *
452  */
453 function HCU_ImportArray( &$pOutput, $pInput, $pVarAllowed ) {
454 
455  try {
456  // make sure returning to an array
457  if ( !is_array($pOutput) ) {
458  $pOutput = array();
459  }
460 
461  // loop through the Allowed array
462  if ( !is_array($pVarAllowed) ) {
463  throw new exception('Not an Array', '901');
464  }
465 
466  // * It's an array -- Loop through the values
467  foreach ($pVarAllowed as $fieldName => $fieldType) {
468  /**
469  * NEW FILTER OPTIONS
470  * Callers can use the filter_options directly
471  *
472  * fieldType should be an array. With at least
473  * 'filter' required
474  * 'options' optional
475  */
476  // ** Return the results from filter_input_array and determine how to convert to our standard response
477  $fieldFilter = HCU_array_key_value('filter', $fieldType);
478  $fieldFilter = ($fieldFilter == 0 ? FILTER_DEFAULT : $fieldFilter);
479 
480  $fieldFlags = HCU_array_key_value('flags', $fieldType);
481  $fieldIsArray = ($fieldFlags == FILTER_REQUIRE_ARRAY ? true : false);
482 
483  $fieldOptions = HCU_array_key_value('options', $fieldType);
484 
485  $fieldValue = HCU_FilterVar($pInput[$fieldName], $fieldFilter, $fieldIsArray, $fieldOptions);
486 
487  if ($fieldValue !== false) {
488  // ** set with without the hb_env key
489  $pOutput[$fieldName] = $fieldValue;
490  } else {
491  // provide a null returned
492  $pOutput[$fieldName] = null;
493  }
494  }
495 
496  $returnVal = true;
497  } catch (exception $e) {
498  // * what to do on Error?
499  $returnVal = false;
500  }
501 
502  return $returnVal;
503 } // end HCU_ImportArray