PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/types/embedded/classes/validate.php

https://bitbucket.org/cisash/fananeen
PHP | 422 lines | 257 code | 30 blank | 135 comment | 36 complexity | 9c7f7c03ac2e808d96a3bfb80f15671b MD5 | raw file
  1. <?php
  2. require_once dirname(__FILE__) . '/validation-cakephp.php';
  3. /**
  4. * Validation class
  5. *
  6. * @version 1.0
  7. */
  8. class Wpcf_Validate
  9. {
  10. /**
  11. * Holds generic messages.
  12. * @var type
  13. */
  14. public static $messages = null;
  15. /**
  16. * Holds function names.
  17. * @var type
  18. */
  19. private static $_cake_aliases = array(
  20. 'digits' => 'numeric',
  21. 'number' => 'numeric',
  22. 'alphanumeric' => 'alphaNumericWhitespaces',
  23. 'nospecialchars' => 'noSpecialChars',
  24. );
  25. /**
  26. * Current validation has 'required' method.
  27. * @var type
  28. */
  29. private static $_is_required = false;
  30. private static $_validation_object = null;
  31. /**
  32. * Sets calls.
  33. *
  34. * @param type $args
  35. * @param type $value
  36. * @return type
  37. */
  38. public static function check($args, $value)
  39. {
  40. // Init validation object
  41. if (is_null(self::$_validation_object)) {
  42. self::$_validation_object = new Wpcf_Cake_Validation();
  43. }
  44. // Init messages
  45. if (is_null(self::$messages)) {
  46. self::_set_messages();
  47. }
  48. // Check if there is 'required' method
  49. if (array_key_exists('required', $args)) {
  50. self::$_is_required = true;
  51. }
  52. // Loop over validation array
  53. foreach ($args as $method => $v) {
  54. // Use this class method
  55. if (is_callable(array('Wpcf_Validate', $method))) {
  56. $check = call_user_func_array(array('Wpcf_Validate', $method),
  57. array($v, $value));
  58. // Use CakePHP method
  59. } else if ((isset(self::$_cake_aliases[$method])
  60. && is_callable(array('Wpcf_Cake_Validation', self::$_cake_aliases[$method])))
  61. || is_callable(array('Wpcf_Cake_Validation', $method))) {
  62. // Check if validation pattern is set
  63. if (isset($v['pattern'])) {
  64. $pattern = array_flip(explode('.', $v['pattern']));
  65. foreach ($pattern as $arg_key => $arg_value) {
  66. if (isset($v[$arg_key])) {
  67. $pattern[$arg_key] = $v[$arg_key];
  68. }
  69. }
  70. $pattern['check'] = $value;
  71. $v = $pattern;
  72. // Apply simple pattern (check, value)
  73. } else {
  74. unset($v['active'], $v['message']);
  75. $v = array($value) + $v;
  76. }
  77. // Validate
  78. if (isset(self::$_cake_aliases[$method]) && is_callable(array('Wpcf_Cake_Validation', self::$_cake_aliases[$method]))) {
  79. // $check = @call_user_func_array(array('Wpcf_Cake_Validation', self::$_cake_aliases[$method]),
  80. // array_values($v));
  81. $check = @call_user_func_array(array(self::$_validation_object, self::$_cake_aliases[$method]),
  82. array_values($v));
  83. } else {
  84. // $check = @call_user_func_array(array('Wpcf_Cake_Validation', $method),
  85. // array_values($v));
  86. $check = @call_user_func_array(array(self::$_validation_object, $method),
  87. array_values($v));
  88. }
  89. if (!$check) {
  90. $check = array();
  91. $check['error'] = 1;
  92. }
  93. // No method available
  94. } else {
  95. return array('error' => 1, 'message' => 'No validation method');
  96. }
  97. // Set error
  98. if (isset($check['error'])) {
  99. // Don't return error if it's empty but not required
  100. if ((!empty($value) && $method != 'required' && self::$_is_required)
  101. || (empty($value) && $method == 'required')) {
  102. $check['message'] = !empty($v['message']) ? $v['message'] : self::$messages[$method];
  103. return $check;
  104. }
  105. }
  106. }
  107. return true;
  108. }
  109. /**
  110. * Checks if method is available.
  111. *
  112. * @param type $method
  113. * @return type
  114. */
  115. public static function canValidate($method)
  116. {
  117. return (is_callable(array('Wpcf_Validate', $method))
  118. || (isset(self::$_cake_aliases[$method])
  119. && is_callable(array('Wpcf_Cake_Validation', self::$_cake_aliases[$method])))
  120. || is_callable(array('Wpcf_Cake_Validation', $method)));
  121. }
  122. /**
  123. * Checks if method has form data.
  124. *
  125. * @param type $method
  126. * @return type
  127. */
  128. public static function hasForm($method)
  129. {
  130. return is_callable(array('Wpcf_Validate', $method . '_form'));
  131. }
  132. /**
  133. * Inits messages.
  134. */
  135. private static function _set_messages()
  136. {
  137. // Set outside in /admin.php
  138. self::$messages = wpcf_admin_validation_messages();
  139. }
  140. /**
  141. * Return method invalid message.
  142. *
  143. * @param type $method
  144. * @return type
  145. */
  146. public static function get_message($method)
  147. {
  148. if (is_null(self::$messages)) {
  149. self::_set_messages();
  150. }
  151. if (isset(self::$messages[$method])) {
  152. return self::$messages[$method];
  153. }
  154. return null;
  155. }
  156. /**
  157. * Checks 'required'.
  158. *
  159. * @param type $args
  160. * @param type $value
  161. * @return type
  162. */
  163. public static function required($args, $value)
  164. {
  165. if (empty($value) && $value !== 0 && $value !== '0') {
  166. return array(
  167. 'error' => 1,
  168. );
  169. }
  170. return true;
  171. }
  172. /**
  173. * Returns form data.
  174. *
  175. * @param type $field
  176. * @param type $data
  177. * @return array
  178. */
  179. public static function required_form($field, $data = array())
  180. {
  181. $form = array();
  182. $form['required-checkbox'] = array(
  183. '#type' => 'checkbox',
  184. '#title' => __('Required', 'wpcf'),
  185. '#name' => $field['#name'] . '[active]',
  186. '#default_value' => isset($data['active']) ? 1 : 0,
  187. '#inline' => true,
  188. '#suffix' => '<br />',
  189. );
  190. $form['required-value'] = array(
  191. '#type' => 'hidden',
  192. '#value' => 'true',
  193. '#name' => $field['#name'] . '[value]',
  194. );
  195. $form['required-message'] = self::get_custom_message($field,
  196. self::get_message('required'), $data);
  197. return $form;
  198. }
  199. /**
  200. * Checks 'email'.
  201. *
  202. * @param type $args
  203. * @param type $value
  204. * @return type
  205. */
  206. public static function email($args, $value)
  207. {
  208. if (!is_email($value)) {
  209. return array(
  210. 'error' => 1,
  211. );
  212. }
  213. return true;
  214. }
  215. /**
  216. * Checks 'rewriteslug'.
  217. *
  218. * @param type $args
  219. * @param type $value
  220. * @return type
  221. */
  222. public static function rewriteslug($args, $value)
  223. {
  224. if (preg_match('#[^a-zA-Z0-9\/\_\-\%]#', $value) === false) {
  225. return array(
  226. 'error' => 1,
  227. );
  228. }
  229. return true;
  230. }
  231. /**
  232. * Returns form data.
  233. *
  234. * @param type $field
  235. * @param type $data
  236. * @return array
  237. */
  238. public static function email_form($field, $data = array())
  239. {
  240. $form = array();
  241. $form['email-checkbox'] = array(
  242. '#type' => 'checkbox',
  243. '#title' => __('Email', 'wpcf'),
  244. '#name' => $field['#name'] . '[active]',
  245. '#default_value' => isset($data['active']) ? 1 : 0,
  246. '#inline' => true,
  247. '#suffix' => '<br />',
  248. );
  249. $form['email-message'] = self::get_custom_message($field,
  250. self::get_message('email'), $data);
  251. return $form;
  252. }
  253. /**
  254. * Returns form data.
  255. *
  256. * @param type $field
  257. * @param type $data
  258. * @return array
  259. */
  260. public static function url_form($field, $data = array())
  261. {
  262. $form = array();
  263. $form['url-checkbox'] = array(
  264. '#type' => 'checkbox',
  265. '#title' => 'URL',
  266. '#name' => $field['#name'] . '[active]',
  267. '#default_value' => isset($data['active']) ? 1 : 0,
  268. '#inline' => true,
  269. '#suffix' => '<br />',
  270. );
  271. $form['url-message'] = self::get_custom_message($field,
  272. self::get_message('url'), $data);
  273. return $form;
  274. }
  275. /**
  276. * Returns form data.
  277. *
  278. * @param type $field
  279. * @param type $data
  280. * @return array
  281. */
  282. public static function date_form($field, $data = array())
  283. {
  284. $form = array();
  285. $form['date-checkbox'] = array(
  286. '#type' => 'checkbox',
  287. '#title' => __('Date', 'wpcf'),
  288. '#name' => $field['#name'] . '[active]',
  289. '#default_value' => isset($data['active']) ? 1 : 0,
  290. '#inline' => true,
  291. '#suffix' => '<br />',
  292. );
  293. $form['date-format'] = array(
  294. '#type' => 'hidden',
  295. '#value' => 'mdy',
  296. '#name' => $field['#name'] . '[format]',
  297. );
  298. $form['date-pattern'] = array(
  299. '#type' => 'hidden',
  300. '#value' => 'check.format',
  301. '#name' => $field['#name'] . '[pattern]',
  302. );
  303. $form['url-message'] = self::get_custom_message($field,
  304. self::get_message('date'), $data);
  305. return $form;
  306. }
  307. /**
  308. * Returns form data.
  309. *
  310. * @param type $field
  311. * @param type $data
  312. * @return array
  313. */
  314. public static function digits_form($field, $data = array())
  315. {
  316. $form = array();
  317. $attributes = array();
  318. $default_value = isset($data['active']) ? 1 : 0;
  319. $form['digits-checkbox'] = array(
  320. '#type' => 'checkbox',
  321. '#title' => __('Digits', 'wpcf'),
  322. '#name' => $field['#name'] . '[active]',
  323. '#default_value' => $default_value,
  324. '#inline' => true,
  325. '#suffix' => '<br />',
  326. '#attributes' => $attributes,
  327. );
  328. $form['digits-checkbox'] = self::setForced($form['digits-checkbox'], $field, $data);
  329. $form['digits-message'] = self::get_custom_message($field,
  330. self::get_message('digits'), $data);
  331. return $form;
  332. }
  333. /**
  334. * Returns form data.
  335. *
  336. * @param type $field
  337. * @param type $data
  338. * @return array
  339. */
  340. public static function number_form($field, $data = array())
  341. {
  342. $form = array();
  343. $attributes = array();
  344. $default_value = isset($data['active']) ? 1 : 0;
  345. $form['number-checkbox'] = array(
  346. '#type' => 'checkbox',
  347. '#title' => __('Numeric', 'wpcf'),
  348. '#name' => $field['#name'] . '[active]',
  349. '#default_value' => $default_value,
  350. '#inline' => true,
  351. '#suffix' => '<br />',
  352. '#attributes' => $attributes,
  353. );
  354. $form['number-checkbox'] = self::setForced($form['number-checkbox'], $field, $data);
  355. $form['number-message'] = self::get_custom_message($field,
  356. self::get_message('number'), $data);
  357. return $form;
  358. }
  359. public static function setForced($element, $field, $data = array())
  360. {
  361. $attributes = array();
  362. $default_value = isset($data['active']) ? 1 : 0;
  363. if (!empty($data['method_data']['forced'])) {
  364. if (!isset($element['#attributes'])) {
  365. $element['#attributes'] = array();
  366. }
  367. $element['#attributes']['readonly'] = 'readonly';
  368. $element['#attributes']['onclick'] = 'jQuery(this).attr(\'checked\', \'checked\');';
  369. $element['#default_value'] = 1;
  370. }
  371. return $element;
  372. }
  373. /**
  374. * Returns 'custom message' field.
  375. *
  376. * @param type $field
  377. * @param type $default
  378. * @param type $data
  379. * @return type
  380. */
  381. public static function get_custom_message($field, $default, $data)
  382. {
  383. return array(
  384. '#type' => 'textfield',
  385. // '#title' => __('Custom message', 'wpcf'),
  386. '#name' => $field['#name'] . '[message]',
  387. '#value' => !empty($data['message']) ? $data['message'] : $default,
  388. '#inline' => true,
  389. // '#suffix' => '<br /><br />',
  390. );
  391. }
  392. }