PageRenderTime 74ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/functions/inputparameter.inc.php

https://github.com/viglesiasce/testlink
PHP | 350 lines | 143 code | 38 blank | 169 comment | 7 complexity | 88b602b9b05a7b97599fe8a8a411f360 MD5 | raw file
  1. <?php
  2. /**
  3. * TestLink Open Source Project - http://testlink.sourceforge.net/
  4. * This script is distributed under the GNU General Public License 2 or later.
  5. *
  6. * Fetch and process input data
  7. *
  8. * Examples of using tlInputParameter related functions
  9. *
  10. * @interal revisions:
  11. * 20100109 - franciscom - fixed errors on documentation
  12. *
  13. * <code>
  14. *
  15. *
  16. * $params = array(
  17. *
  18. * // input from GET['HelloString3'],
  19. * // type: string, minLen: 1, maxLen: 15,
  20. * // regexp: null
  21. * // checkFunction: applys checks via checkFooOrBar() to ensure its either 'foo' or 'bar'
  22. * // normalization: done via normFunction() which replaces ',' with '.'
  23. * "HelloString3" => array("GET",tlInputParameter::STRING_N,1,15,null,'checkFooOrBar','normFunction'),
  24. *
  25. * // string, from POST['HelloString'], minLen 1, maxLen 15
  26. * "HelloString1" => array("POST",tlInputParameter::STRING_N,1,15),
  27. *
  28. * //non negative integer, from POST['HelloInt1']
  29. * "HelloInt1" => array("POST",tlInputParameter::INT_N),
  30. *
  31. * //string, from POST['HelloString2'], minLen 1, maxLen 15, checked with a regExp
  32. * "HelloString2" => array("POST",tlInputParameter::STRING_N,1,15,'/^aaaa$/'),
  33. *
  34. * // non negativ integer, from POST['HelloInt2'], minValue = 20, maxValue = 40. checked to
  35. * // ensure it's odd by using a chechkFunction
  36. * "HelloInt2" => array("POST",tlInputParameter::INT,20,40,'checkOdd'),
  37. * );
  38. *
  39. * $pageParams = I_PARAMS($params)
  40. *
  41. *
  42. * $params = array(
  43. * "HelloString1" => array(tlInputParameter::STRING_N,1,15),
  44. * "HelloInt1" => array(tlInputParameter::INT_N),
  45. * "HelloString2" => array(tlInputParameter::STRING_N,1,15,'/^aaaa$/'),
  46. * "HelloString3" => array(,tlInputParameter::STRING_N,1,15,null,'checkFunction','normFunction'),
  47. * "HelloInt2" => array(tlInputParameter::INT,20,40,'checkOdd'),
  48. * );
  49. *
  50. * $pageParams = P_PARAMS($params);
  51. * </code>
  52. *
  53. * @package TestLink
  54. * @copyright 2005-2009, TestLink community
  55. * @version CVS: $Id: inputparameter.inc.php,v 1.27 2010/01/11 19:16:30 franciscom Exp $
  56. * @link http://www.teamst.org/index.php
  57. *
  58. *
  59. **/
  60. /** include logic */
  61. require_once("object.class.php");
  62. require_once("inputparameter.class.php");
  63. /**
  64. * Fetches the input parameters from POST
  65. *
  66. * @param array $paramInfo generic array about the parameter see examples below of usage
  67. * @param object $args an optional object to which each parameter is added as a property
  68. *
  69. * @return array returns the array with the fetched parameter, keys are the same as in $paramInfo
  70. */
  71. function P_PARAMS($paramInfo,&$args = null)
  72. {
  73. return GPR_PARAMS("POST",$paramInfo,$args);
  74. }
  75. /**
  76. * Fetches the input parameters from GET
  77. *
  78. * @param array $paramInfo generic array about the parameter see examples below of usage
  79. * @param object $args an optional object to which each parameter is added as a property
  80. *
  81. * @return array returns the array with the fetched parameter, keys are the same as in $paramInfo
  82. */
  83. function G_PARAMS($paramInfo,&$args = null)
  84. {
  85. return GPR_PARAMS("GET",$paramInfo,$args);
  86. }
  87. /**
  88. * Fetches the input parameters from REQUEST
  89. *
  90. * @param array $paramInfo generic array about the parameter see examples below of usage
  91. * @param object $args an optional object to which each parameter is added as a property
  92. *
  93. * @return array returns the array with the fetched parameter, keys are the same as in $paramInfo
  94. */
  95. function R_PARAMS($paramInfo,&$args = null)
  96. {
  97. return GPR_PARAMS("REQUEST",$paramInfo,$args);
  98. }
  99. /**
  100. * Fetches the input parameters from POST
  101. *
  102. * @param string $source name of the source to fetch could be "POST", "GET", "REQUEST"
  103. * @param array $paramInfo generic array about the parameter see examples below of usage
  104. * @param object $args an optional object to which each parameter is added as a property
  105. *
  106. * @return array returns the array with the fetched parameter, keys are the same as in $paramInfo
  107. */
  108. function GPR_PARAMS($source,$paramInfo,&$args = null)
  109. {
  110. foreach($paramInfo as $pName => &$info)
  111. {
  112. array_unshift($info,$source);
  113. }
  114. return I_PARAMS($paramInfo,$args);
  115. }
  116. /**
  117. * Fetches the input parameters from the sources specified in $paramInfo
  118. *
  119. * @param array $paramInfo generic array about the parameter see examples below of usage
  120. * @param object $args an optional object to which each parameter is added as a property
  121. *
  122. * @return array returns the array with the fetched parameter, keys are the same as in $paramInfo
  123. */
  124. function I_PARAMS($paramInfo,&$args = null)
  125. {
  126. static $MAX_NUM_OF_PARAMS = 5;
  127. $params = null;
  128. foreach($paramInfo as $pName => $info)
  129. {
  130. $source = $info[0];
  131. $type = $info[1];
  132. for($i = 1;$i <= $MAX_NUM_OF_PARAMS;$i++)
  133. {
  134. $varName = "p{$i}";
  135. $value = isset($info[$i+1]) ? $info[$i+1] : null;
  136. $$varName = $value;
  137. }
  138. switch($type)
  139. {
  140. case tlInputParameter::ARRAY_INT:
  141. $pfnValidation = $p1;
  142. $value = GPR_PARAM_ARRAY_INT($source,$pName,$pfnValidation);
  143. break;
  144. case tlInputParameter::ARRAY_STRING_N:
  145. $pfnValidation = $p1;
  146. $value = GPR_PARAM_ARRAY_STRING_N($source,$pName,$pfnValidation);
  147. break;
  148. case tlInputParameter::INT_N:
  149. $maxVal = $p1;
  150. $pfnValidation = $p2;
  151. $value = GPR_PARAM_INT_N($source,$pName,$maxVal,$pfnValidation);
  152. break;
  153. case tlInputParameter::INT:
  154. $minVal = $p1;
  155. $maxVal = $p2;
  156. $pfnValidation = $p3;
  157. $value = GPR_PARAM_INT($source,$pName,$minVal,$maxVal,$pfnValidation);
  158. break;
  159. case tlInputParameter::STRING_N:
  160. $minLen = $p1;
  161. $maxLen = $p2;
  162. $regExp = $p3;
  163. $pfnValidation = $p4;
  164. $pfnNormalization = $p5;
  165. $value = GPR_PARAM_STRING_N($source,$pName,$minLen,$maxLen,$regExp,
  166. $pfnValidation,$pfnNormalization);
  167. break;
  168. case tlInputParameter::CB_BOOL:
  169. $value = GPR_PARAM_CB_BOOL($source,$pName);
  170. break;
  171. }
  172. $params[$pName] = $value;
  173. if ($args)
  174. {
  175. $args->$pName = $value;
  176. }
  177. }
  178. return $params;
  179. }
  180. /**
  181. * Process a string type value from GET/POST/REQUEST
  182. *
  183. * @param string $inputSource the name of the source, "GET","POST","REQUEST"
  184. * @param string $name the name of the parameter
  185. * @param integer $minLen the minimum length of the string
  186. * @param integer $maxLen the maximum length of the string
  187. * @param string $regExp a regular Expression for preg_ functions used the validate
  188. * @param string $pfnValidation a callback function used to validate
  189. * @param string $pfnNormalization a callback function used to normalize
  190. * @return string the value of the parameter
  191. */
  192. function GPR_PARAM_STRING_N($inputSource,$name,$minLen = null,$maxLen = null,$regExp = null,
  193. $pfnValidation = null,$pfnNormalization = null)
  194. {
  195. $vInfo = new tlStringValidationInfo();
  196. $vInfo->trim = tlStringValidationInfo::TRIM_BOTH;
  197. $vInfo->doStripSlashes = true;
  198. $parameters = array("minLen","maxLen","regExp","pfnValidation","pfnNormalization");
  199. foreach($parameters as $parameter)
  200. {
  201. if (!is_null($$parameter))
  202. $vInfo->$parameter = $$parameter;
  203. }
  204. $pInfo = new tlParameterInfo($inputSource,$name);
  205. $iParam = new tlInputParameter($pInfo,$vInfo);
  206. return $iParam->value();
  207. }
  208. /**
  209. * Process a integer type value from GET/POST/REQUEST
  210. *
  211. * @param string $inputSource the name of the source, "GET","POST","REQUEST"
  212. * @param string $name the name of the parameter
  213. * @param integer $minVal the minimum value
  214. * @param integer $maxVal the maximum value
  215. * @param string $pfnValidation a callback function used to validate
  216. * @return integer the value of the parameter
  217. */
  218. function GPR_PARAM_INT($inputSource,$name,$minVal = null,$maxVal = null,$pfnValidation = null)
  219. {
  220. $vInfo = new tlIntegerValidationInfo();
  221. $parameters = array("minVal","maxVal","pfnValidation");
  222. foreach($parameters as $parameter)
  223. {
  224. if (!is_null($$parameter))
  225. {
  226. $vInfo->$parameter = $$parameter;
  227. }
  228. }
  229. $pInfo = new tlParameterInfo($inputSource,$name);
  230. $iParam = new tlInputParameter($pInfo,$vInfo);
  231. return $iParam->value();
  232. }
  233. /**
  234. * Process a non-negative integer type value from GET/POST/REQUEST
  235. *
  236. * @param string $inputSource the name of the source, "GET","POST","REQUEST"
  237. * @param string $name the name of the parameter
  238. * @param integer $maxVal the maximum value
  239. * @param string $pfnValidation a callback function used to validate
  240. * @return integer the value of the parameter
  241. */
  242. function GPR_PARAM_INT_N($inputSource,$name,$maxVal = null,$pfnValidation = null)
  243. {
  244. return GPR_PARAM_INT($inputSource,$name,0,$maxVal,$pfnValidation);
  245. }
  246. /**
  247. * Process an array of integer type values from GET/POST/REQUEST
  248. *
  249. * @param string $inputSource the name of the source, "GET","POST","REQUEST"
  250. * @param string $name the name of the parameter
  251. * @param string $pfnValidation a callback function used to validate
  252. * @return array the array of integer values from the parameter
  253. */
  254. function GPR_PARAM_ARRAY_INT($inputSource,$name,$pfnValidation = null)
  255. {
  256. return GPR_PARAM_ARRAY($inputSource,tlInputParameter::INT,$name,$pfnValidation);
  257. }
  258. /**
  259. * Process an array of string_n type values from GET/POST/REQUEST
  260. *
  261. * @param string $inputSource the name of the source, "GET","POST","REQUEST"
  262. * @param string $name the name of the parameter
  263. * @param string $pfnValidation a callback function used to validate
  264. * @return array the array of string values from the parameter
  265. */
  266. function GPR_PARAM_ARRAY_STRING_N($inputSource,$name,$pfnValidation = null)
  267. {
  268. return GPR_PARAM_ARRAY($inputSource,tlInputParameter::STRING_N,$name,$pfnValidation);
  269. }
  270. /**
  271. * Process an array of string_n type values from GET/POST/REQUEST
  272. *
  273. * @param string $inputSource the name of the source, "GET","POST","REQUEST"
  274. * @param string $name the name of the parameter
  275. * @param string $pfnValidation a callback function used to validate
  276. * @return array the array of string values from the parameter
  277. */
  278. function GPR_PARAM_ARRAY($inputSource,$type,$name,$pfnValidation)
  279. {
  280. $vInfo = new tlArrayValidationInfo();
  281. if (!is_null($pfnValidation))
  282. $vInfo->pfnValidation = $pfnValidation;
  283. if ($type == tlInputParameter::STRING_N)
  284. $vInfo->validationInfo = new tlStringValidationInfo();
  285. else
  286. $vInfo->validationInfo = new tlIntegerValidationInfo();
  287. $pInfo = new tlParameterInfo($inputSource,$name);
  288. $iParam = new tlInputParameter($pInfo,$vInfo);
  289. return $iParam->value();
  290. }
  291. /**
  292. * Process an array of "checkbox" (string equal to "on") type values from GET/POST/REQUEST
  293. *
  294. * @param string $inputSource the name of the source, "GET","POST","REQUEST"
  295. * @param string $name the name of the parameter
  296. * @return array the array of boolean values from the parameter
  297. */
  298. function GPR_PARAM_CB_BOOL($inputSource,$name)
  299. {
  300. $vInfo = new tlCheckBoxValidationInfo();
  301. $pInfo = new tlParameterInfo($inputSource,$name);
  302. $iParam = new tlInputParameter($pInfo,$vInfo);
  303. return $iParam->value();
  304. }
  305. ?>