PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Zikula-1.2.3/includes/FormUtil.class.php

#
PHP | 384 lines | 219 code | 41 blank | 124 comment | 81 complexity | 0c81f99645fc9d563fc75c1dd72ebb8f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * Zikula Application Framework
  4. *
  5. * @copyright Zikula Development Team
  6. * @link http://www.zikula.org
  7. * @version $Id: FormUtil.class.php 27233 2009-10-27 20:07:15Z mateo $
  8. * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9. * @author Robert Gasch rgasch@gmail.com
  10. * @package Zikula_Core
  11. * @subpackage FormUtil
  12. */
  13. /**
  14. * FormUtil
  15. *
  16. * @package Zikula_Core
  17. * @subpackage FormUtil
  18. */
  19. class FormUtil
  20. {
  21. /**
  22. * Return the requested key from input in a safe way. This function
  23. * is safe to use for recursive arrays and either returns a non-empty
  24. * string or the (optional) default.
  25. *
  26. * This method is based on pnVarCleanFromInput but array-safe.
  27. *
  28. * @param key The field to return
  29. * @param default The value to return if the requested field is not found (optional) (default=false)
  30. * @param source The sourc field to get a parameter from
  31. *
  32. * @return The requested input key or the specified default
  33. */
  34. function getPassedValue($key, $default = null, $source = null)
  35. {
  36. if (!$key) {
  37. return pn_exit(__f('Empty %1$s passed to %2$s.', array('key', 'FormUtil::getPassedValueSafe')));
  38. }
  39. $source = strtoupper($source);
  40. $src = ($source ? $source : 'REQUEST') . '_' . ($default != null ? $default : 'null');
  41. $doClean = false;
  42. switch (true)
  43. {
  44. case (isset($_REQUEST[$key]) && !isset($_FILES[$key]) && (!$source || $source == 'R' || $source == 'REQUEST')):
  45. $value = $_REQUEST[$key];
  46. $doClean = true;
  47. break;
  48. case isset($_GET[$key]) && (!$source || $source == 'G' || $source == 'GET'):
  49. $value = $_GET[$key];
  50. $doClean = true;
  51. break;
  52. case isset($_POST[$key]) && (!$source || $source == 'P' || $source == 'POST'):
  53. $value = $_POST[$key];
  54. $doClean = true;
  55. break;
  56. case isset($_COOKIE[$key]) && (!$source || $source == 'C' || $source == 'COOKIE'):
  57. $value = $_COOKIE[$key];
  58. $doClean = true;
  59. break;
  60. case isset($_FILES[$key]) && ($source == 'F' || $source == 'FILES'):
  61. $value = $_FILES[$key];
  62. break;
  63. case (isset($_GET[$key]) || isset($_POST[$key])) && ($source == 'GP' || $source == 'GETPOST'):
  64. if (isset($_GET[$key])) {
  65. $value = $_GET[$key];
  66. }
  67. if (isset($_POST[$key])) {
  68. $value = $_POST[$key];
  69. }
  70. $doClean = true;
  71. break;
  72. default:
  73. if ($source) {
  74. static $valid = array('R', 'REQUEST', 'G', 'GET', 'P', 'POST', 'C', 'COOKIE', 'F', 'FILES', 'GP', 'GETPOST');
  75. if (!in_array($source, $valid)) {
  76. pn_exit(__f('Invalid input source [%s] received.', DataUtil::formatForDisplay($source)));
  77. return $default;
  78. }
  79. }
  80. }
  81. if (isset($value) && !is_null($value)) {
  82. if (is_array($value)) {
  83. FormUtil::cleanArray($value);
  84. } else {
  85. static $alwaysclean = array('name', 'module', 'type', 'file', 'authid');
  86. if (in_array($key, $alwaysclean)) {
  87. $doClean = true;
  88. }
  89. if ($doClean && !defined('_PNINSTALLVER')) {
  90. FormUtil::cleanValue($value);
  91. }
  92. }
  93. return $value;
  94. }
  95. return $default;
  96. }
  97. /**
  98. * Clean an array acquired from input. This method is safe to use for recursive arrays
  99. * and cleans the array in place as well as returning it.
  100. *
  101. * @param array The array to clean up
  102. *
  103. * @return The the altered/cleaned data array.
  104. */
  105. function cleanArray(&$array)
  106. {
  107. if (!is_array($array)) {
  108. return pn_exit(__f('Non-array passed to %s.', 'FormUtil::cleanArray'));
  109. }
  110. $ak = array_keys($array);
  111. $kc = count($ak);
  112. for ($i = 0; $i < $kc; $i++) {
  113. $key = $ak[$i];
  114. if (is_array($array[$key])) {
  115. FormUtil::cleanArray($array[$key]);
  116. } else {
  117. FormUtil::cleanValue($array[$key]);
  118. }
  119. }
  120. return $array;
  121. }
  122. /**
  123. * Clean an individual data element in place; cleans the array in place
  124. * as well as returning it.
  125. *
  126. * @param value The value to clean
  127. *
  128. * @return A reference to the original (altered/cleaned) data array
  129. */
  130. function cleanValue(&$value)
  131. {
  132. static $isAdmin = null;
  133. if ($isAdmin === null)
  134. $isAdmin = SecurityUtil::checkPermission('.*', '.*', ACCESS_ADMIN);
  135. if (!$value) {
  136. return $value;
  137. }
  138. if (get_magic_quotes_gpc()) {
  139. pnStripslashes($value);
  140. }
  141. if (!$isAdmin) {
  142. static $replace = array();
  143. static $search = array('|</?\s*SCRIPT.*?>|si', '|</?\s*FRAME.*?>|si', '|</?\s*OBJECT.*?>|si', '|</?\s*META.*?>|si', '|</?\s*APPLET.*?>|si', '|</?\s*LINK.*?>|si', '|</?\s*IFRAME.*?>|si', '|STYLE\s*=\s*"[^"]*"|si');
  144. $value = preg_replace($search, $replace, $value);
  145. }
  146. $value = trim($value);
  147. return $value;
  148. }
  149. /**
  150. * Return a boolean indicating whether the specified field is required
  151. *
  152. * @param validationInfo The plain (non-structured) validation array
  153. * @param field The fieldname
  154. *
  155. * @return A boolean indicating whether or not the specified field is required
  156. */
  157. function isRequiredField($validationInfo, $field)
  158. {
  159. if (!$validationInfo) {
  160. return pn_exit(__f('Empty %1$s passed to %2$s.', array('validationInfo', 'FormUtil::isRequiredField')));
  161. }
  162. if (!$field) {
  163. return pn_exit(__f('Empty %1$s passed to %2$s.', array('fieldname', 'FormUtil::isRequiredField')));
  164. }
  165. $rec = isset($validationInfo[$field]) ? $validationInfo[$field] : null;
  166. if (!$rec) {
  167. return false;
  168. }
  169. return $rec[1];
  170. }
  171. /**
  172. * Get the required field marker (or nothing) for the specified field
  173. *
  174. * @param validationInfo The plain (non-structured) validation array
  175. * @param field The fieldname
  176. *
  177. * @return The required field marker or an empty string
  178. */
  179. function getRequiredFieldMarker($validationInfo, $field)
  180. {
  181. if (FormUtil::isRequiredField($validationInfo, $field)) {
  182. return _REQUIRED_MARKER;
  183. }
  184. return _MARKER_NONE;
  185. }
  186. /**
  187. * Clear the validation error array
  188. *
  189. * @param objectType The (string) object type
  190. *
  191. * @return nothing
  192. */
  193. function clearValidationErrors($objectType = null)
  194. {
  195. if ($objectType) {
  196. if (isset($_SESSION['validationErrors'][$objectType])) {
  197. unset($_SESSION['validationErrors'][$objectType]);
  198. }
  199. } else {
  200. if (isset($_SESSION['validationErrors'])) {
  201. unset($_SESSION['validationErrors']);
  202. }
  203. }
  204. }
  205. /**
  206. * Clear the objects which failed validation
  207. *
  208. * @param objectType The (string) object type
  209. *
  210. * @return nothing
  211. */
  212. function clearValidationFailedObjects($objectType = null)
  213. {
  214. if ($objectType) {
  215. if (isset($_SESSION['validationFailedObjects'][$objectType])) {
  216. unset($_SESSION['validationFailedObjects'][$objectType]);
  217. }
  218. } else {
  219. if (isset($_SESSION['validationFailedObjects'])) {
  220. unset($_SESSION['validationFailedObjects']);
  221. }
  222. }
  223. }
  224. /**
  225. * Get the validation errors
  226. *
  227. * @return The validation error array or null
  228. */
  229. function getValidationErrors()
  230. {
  231. static $ve = null;
  232. if (!$ve) {
  233. if (isset($_SESSION['validationErrors']) && is_array($_SESSION['validationErrors'])) {
  234. $ve = $_SESSION['validationErrors'];
  235. unset($_SESSION['validationErrors']);
  236. }
  237. }
  238. return $ve;
  239. }
  240. /**
  241. * Return the objects which failed validation
  242. *
  243. * @return The validation error array or null
  244. */
  245. function getFailedValidationObjects($objectType = null)
  246. {
  247. static $objects = null;
  248. if (!$objects) {
  249. if (isset($_SESSION['validationFailedObjects']) && is_array($_SESSION['validationFailedObjects'])) {
  250. if ($objectType && isset($_SESSION['validationFailedObjects'][$objectType])) {
  251. $objects = $_SESSION['validationFailedObjects'][$objectType];
  252. unset($_SESSION['validationFailedObjects'][$objectType]);
  253. } else {
  254. $objects = $_SESSION['validationFailedObjects'];
  255. unset($_SESSION['validationFailedObjects']);
  256. }
  257. }
  258. }
  259. return $objects;
  260. }
  261. /**
  262. * Return a boolean indicating whether or not the specified field failed validation
  263. *
  264. * @param objectType The (string) object type
  265. * @param field The fieldname
  266. *
  267. * @return A boolean indicating whether or not the specified field failed validation
  268. */
  269. function hasValidationErrors($objectType, $field = null)
  270. {
  271. if (!$objectType) {
  272. return pn_exit(__f('Empty %1$s passed to %2$s.', array('objectType', 'FormUtil::hasValidationErrors')));
  273. }
  274. if (!$field) {
  275. return pn_exit(__f('Empty %1$s passed to %2$s.', array('field', 'FormUtil::hasValidationErrors')));
  276. }
  277. $ve = FormUtil::getValidationErrors();
  278. return (boolean) ($ve[$objectType][$field]);
  279. }
  280. /**
  281. * Get the required field marker (or nothing) for the specified field
  282. *
  283. * @param objectType The (string) object type
  284. * @param field The fieldname
  285. *
  286. * @return The validation error marker or an empty string
  287. */
  288. function getValidationFieldMarker($objectType, $field)
  289. {
  290. if (FormUtil::hasValidationErrors($objectType, $field)) {
  291. return _VALIDATION_MARKER;
  292. }
  293. return _MARKER_NONE;
  294. }
  295. /**
  296. * Get the validation error for the specified field
  297. *
  298. * @param objectType The (string) object type
  299. * @param field The fieldname to get the error for
  300. *
  301. * @return The validation error or an empty string
  302. */
  303. function getValidationError($objectType, $field)
  304. {
  305. if (!FormUtil::hasValidationErrors($objectType, $field)) {
  306. return '';
  307. }
  308. $ve = FormUtil::getValidationErrors();
  309. $error = $ve[$objectType][$field];
  310. if ($error) {
  311. $error = '&nbsp;' . $error;
  312. }
  313. return $error;
  314. }
  315. /**
  316. * Get the appropriate field marker
  317. *
  318. * @param objectType The (string) object type
  319. * @param validationInfo The plain (non-structured) validation array
  320. * @param field The fieldname
  321. *
  322. * @return The a marker string or an 'nbsp';
  323. */
  324. function getFieldMarker($objectType, $validationInfo, $field)
  325. {
  326. if (FormUtil::hasValidationErrors($objectType, $field)) {
  327. return _VALIDATION_MARKER;
  328. } else if (FormUtil::isRequiredField($validationInfo, $field)) {
  329. return _REQUIRED_MARKER;
  330. }
  331. return _MARKER_NONE;
  332. }
  333. /**
  334. * Return a newly created pnFormRender instance with the given name
  335. *
  336. * @return The newly created pnFormRender instance.
  337. */
  338. function newPNForm($name)
  339. {
  340. Loader::requireOnce('includes/pnForm.php');
  341. return new pnFormRender($name);
  342. }
  343. }