PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Validate.php

https://bitbucket.org/yhjohn/ayanapure.com
PHP | 1046 lines | 449 code | 98 blank | 499 comment | 84 complexity | c3e044d268e28f46936c0d8b41bef2e5 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2012 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2012 PrestaShop SA
  23. * @version Release: $Revision: 7040 $
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * International Registered Trademark & Property of PrestaShop SA
  26. */
  27. class ValidateCore
  28. {
  29. public static function isIp2Long($ip)
  30. {
  31. return preg_match('#^-?[0-9]+$#', (string)$ip);
  32. }
  33. public static function isAnything()
  34. {
  35. return true;
  36. }
  37. /**
  38. * Check for e-mail validity
  39. *
  40. * @param string $email e-mail address to validate
  41. * @return boolean Validity is ok or not
  42. */
  43. public static function isEmail($email)
  44. {
  45. return !empty($email) && preg_match('/^[a-z\p{L}0-9!#$%&\'*+\/=?^`{}|~_-]+[.a-z\p{L}0-9!#$%&\'*+\/=?^`{}|~_-]*@[a-z\p{L}0-9]+[._a-z\p{L}0-9-]*\.[a-z0-9]+$/ui', $email);
  46. }
  47. /**
  48. * Check for module URL validity
  49. *
  50. * @param string $url module URL to validate
  51. * @param array $errors Reference array for catching errors
  52. * @return boolean Validity is ok or not
  53. */
  54. public static function isModuleUrl($url, &$errors)
  55. {
  56. if (!$url || $url == 'http://')
  57. $errors[] = Tools::displayError('Please specify module URL');
  58. elseif (substr($url, -4) != '.tar' && substr($url, -4) != '.zip' && substr($url, -4) != '.tgz' && substr($url, -7) != '.tar.gz')
  59. $errors[] = Tools::displayError('Unknown archive type');
  60. else
  61. {
  62. if ((strpos($url, 'http')) === false)
  63. $url = 'http://'.$url;
  64. if (!is_array(@get_headers($url)))
  65. $errors[] = Tools::displayError('Invalid URL');
  66. }
  67. if (!count($errors))
  68. return true;
  69. return false;
  70. }
  71. /**
  72. * Check for MD5 string validity
  73. *
  74. * @param string $md5 MD5 string to validate
  75. * @return boolean Validity is ok or not
  76. */
  77. public static function isMd5($md5)
  78. {
  79. return preg_match('/^[a-f0-9A-F]{32}$/', $md5);
  80. }
  81. /**
  82. * Check for SHA1 string validity
  83. *
  84. * @param string $sha1 SHA1 string to validate
  85. * @return boolean Validity is ok or not
  86. */
  87. public static function isSha1($sha1)
  88. {
  89. return preg_match('/^[a-fA-F0-9]{40}$/', $sha1);
  90. }
  91. /**
  92. * Check for a float number validity
  93. *
  94. * @param float $float Float number to validate
  95. * @return boolean Validity is ok or not
  96. */
  97. public static function isFloat($float)
  98. {
  99. return strval((float)$float) == strval($float);
  100. }
  101. public static function isUnsignedFloat($float)
  102. {
  103. return strval((float)$float) == strval($float) && $float >= 0;
  104. }
  105. /**
  106. * Check for a float number validity
  107. *
  108. * @param float $float Float number to validate
  109. * @return boolean Validity is ok or not
  110. */
  111. public static function isOptFloat($float)
  112. {
  113. return empty($float) || Validate::isFloat($float);
  114. }
  115. /**
  116. * Check for a carrier name validity
  117. *
  118. * @param string $name Carrier name to validate
  119. * @return boolean Validity is ok or not
  120. */
  121. public static function isCarrierName($name)
  122. {
  123. return empty($name) || preg_match('/^[^<>;=#{}]*$/u', $name);
  124. }
  125. /**
  126. * Check for an image size validity
  127. *
  128. * @param string $size Image size to validate
  129. * @return boolean Validity is ok or not
  130. */
  131. public static function isImageSize($size)
  132. {
  133. return preg_match('/^[0-9]{1,4}$/', $size);
  134. }
  135. /**
  136. * Check for name validity
  137. *
  138. * @param string $name Name to validate
  139. * @return boolean Validity is ok or not
  140. */
  141. public static function isName($name)
  142. {
  143. return preg_match('/^[^0-9!<>,;?=+()@#"?{}_$%:]*$/u', stripslashes($name));
  144. }
  145. /**
  146. * Check for hook name validity
  147. *
  148. * @param string $hook Hook name to validate
  149. * @return boolean Validity is ok or not
  150. */
  151. public static function isHookName($hook)
  152. {
  153. return preg_match('/^[a-zA-Z0-9_-]+$/', $hook);
  154. }
  155. /**
  156. * Check for sender name validity
  157. *
  158. * @param string $mail_name Sender name to validate
  159. * @return boolean Validity is ok or not
  160. */
  161. public static function isMailName($mail_name)
  162. {
  163. return preg_match('/^[^<>;=#{}]*$/u', $mail_name);
  164. }
  165. /**
  166. * Check for e-mail subject validity
  167. *
  168. * @param string $mail_subject e-mail subject to validate
  169. * @return boolean Validity is ok or not
  170. */
  171. public static function isMailSubject($mail_subject)
  172. {
  173. return preg_match('/^[^<>]*$/u', $mail_subject);
  174. }
  175. /**
  176. * Check for module name validity
  177. *
  178. * @param string $module_name Module name to validate
  179. * @return boolean Validity is ok or not
  180. */
  181. public static function isModuleName($module_name)
  182. {
  183. return (is_string($module_name) && preg_match('/^[a-zA-Z0-9_-]+$/', $module_name));
  184. }
  185. /**
  186. * Check for template name validity
  187. *
  188. * @param string $tpl_name Template name to validate
  189. * @return boolean Validity is ok or not
  190. */
  191. public static function isTplName($tpl_name)
  192. {
  193. return preg_match('/^[a-zA-Z0-9_-]+$/', $tpl_name);
  194. }
  195. /**
  196. * Check for image type name validity
  197. *
  198. * @param string $type Image type name to validate
  199. * @return boolean Validity is ok or not
  200. */
  201. public static function isImageTypeName($type)
  202. {
  203. return preg_match('/^[a-zA-Z0-9_ -]+$/', $type);
  204. }
  205. /**
  206. * Check for price validity
  207. *
  208. * @param string $price Price to validate
  209. * @return boolean Validity is ok or not
  210. */
  211. public static function isPrice($price)
  212. {
  213. return preg_match('/^[0-9]{1,10}(\.[0-9]{1,9})?$/', $price);
  214. }
  215. /**
  216. * Check for price validity (including negative price)
  217. *
  218. * @param string $price Price to validate
  219. * @return boolean Validity is ok or not
  220. */
  221. public static function isNegativePrice($price)
  222. {
  223. return preg_match('/^[-]?[0-9]{1,10}(\.[0-9]{1,9})?$/', $price);
  224. }
  225. /**
  226. * Check for language code (ISO) validity
  227. *
  228. * @param string $iso_code Language code (ISO) to validate
  229. * @return boolean Validity is ok or not
  230. */
  231. public static function isLanguageIsoCode($iso_code)
  232. {
  233. return preg_match('/^[a-zA-Z]{2,3}$/', $iso_code);
  234. }
  235. public static function isLanguageCode($s)
  236. {
  237. return preg_match('/^[a-zA-Z]{2}(-[a-zA-Z]{2})?$/', $s);
  238. }
  239. public static function isStateIsoCode($iso_code)
  240. {
  241. return preg_match('/^[a-zA-Z0-9]{1,4}((-)[a-zA-Z0-9]{1,4})?$/', $iso_code);
  242. }
  243. public static function isNumericIsoCode($iso_code)
  244. {
  245. return preg_match('/^[0-9]{2,3}$/', $iso_code);
  246. }
  247. /**
  248. * Check for voucher name validity
  249. *
  250. * @param string $voucher voucher to validate
  251. * @return boolean Validity is ok or not
  252. */
  253. public static function isDiscountName($voucher)
  254. {
  255. return preg_match('/^[^!<>,;?=+()@"?{}_$%:]{3,32}$/u', $voucher);
  256. }
  257. /**
  258. * Check for product or category name validity
  259. *
  260. * @param string $name Product or category name to validate
  261. * @return boolean Validity is ok or not
  262. */
  263. public static function isCatalogName($name)
  264. {
  265. return preg_match('/^[^<>;=#{}]*$/u', $name);
  266. }
  267. /**
  268. * Check for a message validity
  269. *
  270. * @param string $message Message to validate
  271. * @return boolean Validity is ok or not
  272. */
  273. public static function isMessage($message)
  274. {
  275. return !preg_match('/[<>{}]/i', $message);
  276. }
  277. /**
  278. * Check for a country name validity
  279. *
  280. * @param string $name Country name to validate
  281. * @return boolean Validity is ok or not
  282. */
  283. public static function isCountryName($name)
  284. {
  285. return preg_match('/^[a-zA-Z -]+$/', $name);
  286. }
  287. /**
  288. * Check for a link (url-rewriting only) validity
  289. *
  290. * @param string $link Link to validate
  291. * @return boolean Validity is ok or not
  292. */
  293. public static function isLinkRewrite($link)
  294. {
  295. return preg_match('/^[_a-zA-Z0-9\-\pL]+$/u', $link);
  296. }
  297. /**
  298. * Check for a postal address validity
  299. *
  300. * @param string $address Address to validate
  301. * @return boolean Validity is ok or not
  302. */
  303. public static function isAddress($address)
  304. {
  305. return empty($address) || preg_match('/^[^!<>?=+@{}_$%]*$/u', $address);
  306. }
  307. /**
  308. * Check for city name validity
  309. *
  310. * @param string $city City name to validate
  311. * @return boolean Validity is ok or not
  312. */
  313. public static function isCityName($city)
  314. {
  315. return preg_match('/^[^!<>;?=+@#"?{}_$%]*$/u', $city);
  316. }
  317. /**
  318. * Check for search query validity
  319. *
  320. * @param string $search Query to validate
  321. * @return boolean Validity is ok or not
  322. */
  323. public static function isValidSearch($search)
  324. {
  325. return preg_match('/^[^<>;=#{}]{0,64}$/u', $search);
  326. }
  327. /**
  328. * Check for standard name validity
  329. *
  330. * @param string $name Name to validate
  331. * @return boolean Validity is ok or not
  332. */
  333. public static function isGenericName($name)
  334. {
  335. return empty($name) || preg_match('/^[^<>=#{}]*$/u', $name);
  336. }
  337. /**
  338. * Check for HTML field validity (no XSS please !)
  339. *
  340. * @param string $html HTML field to validate
  341. * @return boolean Validity is ok or not
  342. */
  343. public static function isCleanHtml($html)
  344. {
  345. $events = 'onmousedown|onmousemove|onmmouseup|onmouseover|onmouseout|onload|onunload|onfocus|onblur|onchange';
  346. $events .= '|onsubmit|ondblclick|onclick|onkeydown|onkeyup|onkeypress|onmouseenter|onmouseleave|onerror|onselect|onreset|onabort|ondragdrop|onresize|onactivate|onafterprint|onmoveend';
  347. $events .= '|onafterupdate|onbeforeactivate|onbeforecopy|onbeforecut|onbeforedeactivate|onbeforeeditfocus|onbeforepaste|onbeforeprint|onbeforeunload|onbeforeupdate|onmove';
  348. $events .= '|onbounce|oncellchange|oncontextmenu|oncontrolselect|oncopy|oncut|ondataavailable|ondatasetchanged|ondatasetcomplete|ondeactivate|ondrag|ondragend|ondragenter|onmousewheel';
  349. $events .= '|ondragleave|ondragover|ondragstart|ondrop|onerrorupdate|onfilterchange|onfinish|onfocusin|onfocusout|onhashchange|onhelp|oninput|onlosecapture|onmessage|onmouseup|onmovestart';
  350. $events .= '|onoffline|ononline|onpaste|onpropertychange|onreadystatechange|onresizeend|onresizestart|onrowenter|onrowexit|onrowsdelete|onrowsinserted|onscroll|onsearch|onselectionchange';
  351. $events .= '|onselectstart|onstart|onstop';
  352. return (!preg_match('/<[ \t\n]*script/ims', $html) && !preg_match('/('.$events.')[ \t\n]*=/ims', $html) && !preg_match('/.*script\:/ims', $html) && !preg_match('/<[ \t\n]*i?frame/ims', $html));
  353. }
  354. /**
  355. * Check for product reference validity
  356. *
  357. * @param string $reference Product reference to validate
  358. * @return boolean Validity is ok or not
  359. */
  360. public static function isReference($reference)
  361. {
  362. return preg_match('/^[^<>;={}]*$/u', $reference);
  363. }
  364. /**
  365. * Check for password validity
  366. *
  367. * @param string $passwd Password to validate
  368. * @param int $size
  369. * @return boolean Validity is ok or not
  370. */
  371. public static function isPasswd($passwd, $size = 5)
  372. {
  373. return (Tools::strlen($passwd) >= $size && Tools::strlen($passwd) < 255);
  374. }
  375. public static function isPasswdAdmin($passwd)
  376. {
  377. return Validate::isPasswd($passwd, 8);
  378. }
  379. /**
  380. * Check for configuration key validity
  381. *
  382. * @param string $config_name Configuration key to validate
  383. * @return boolean Validity is ok or not
  384. */
  385. public static function isConfigName($config_name)
  386. {
  387. return preg_match('/^[a-zA-Z_0-9-]+$/', $config_name);
  388. }
  389. /**
  390. * Check date formats like http://php.net/manual/en/function.date.php
  391. *
  392. * @param string $date_format date format to check
  393. * @return boolean Validity is ok or not
  394. */
  395. public static function isPhpDateFormat($date_format)
  396. {
  397. // We can't really check if this is valid or not, because this is a string and you can write whatever you want in it.
  398. // That's why only < et > are forbidden (HTML)
  399. return preg_match('/^[^<>]+$/', $date_format);
  400. }
  401. /**
  402. * Check for date format
  403. *
  404. * @param string $date Date to validate
  405. * @return boolean Validity is ok or not
  406. */
  407. public static function isDateFormat($date)
  408. {
  409. return (bool)preg_match('/^([0-9]{4})-((0?[0-9])|(1[0-2]))-((0?[0-9])|([1-2][0-9])|(3[01]))( [0-9]{2}:[0-9]{2}:[0-9]{2})?$/', $date);
  410. }
  411. /**
  412. * Check for date validity
  413. *
  414. * @param string $date Date to validate
  415. * @return boolean Validity is ok or not
  416. */
  417. public static function isDate($date)
  418. {
  419. if (!preg_match('/^([0-9]{4})-((0?[0-9])|(1[0-2]))-((0?[0-9])|([1-2][0-9])|(3[01]))( [0-9]{2}:[0-9]{2}:[0-9]{2})?$/', $date, $matches))
  420. return false;
  421. return checkdate((int)$matches[2], (int)$matches[5], (int)$matches[0]);
  422. }
  423. /**
  424. * Check for birthDate validity
  425. *
  426. * @param string $date birthdate to validate
  427. * @return boolean Validity is ok or not
  428. */
  429. public static function isBirthDate($date)
  430. {
  431. if (empty($date) || $date == '0000-00-00')
  432. return true;
  433. if (preg_match('/^([0-9]{4})-((0?[1-9])|(1[0-2]))-((0?[1-9])|([1-2][0-9])|(3[01]))( [0-9]{2}:[0-9]{2}:[0-9]{2})?$/', $date, $birth_date))
  434. {
  435. if ($birth_date[1] >= date('Y') - 9)
  436. return false;
  437. return true;
  438. }
  439. return false;
  440. }
  441. /**
  442. * Check for boolean validity
  443. *
  444. * @param boolean $bool Boolean to validate
  445. * @return boolean Validity is ok or not
  446. */
  447. public static function isBool($bool)
  448. {
  449. return $bool === null || is_bool($bool) || preg_match('/^0|1$/', $bool);
  450. }
  451. /**
  452. * Check for phone number validity
  453. *
  454. * @param string $number Phone number to validate
  455. * @return boolean Validity is ok or not
  456. */
  457. public static function isPhoneNumber($number)
  458. {
  459. return preg_match('/^[+0-9. ()-]*$/', $number);
  460. }
  461. /**
  462. * Check for barcode validity (EAN-13)
  463. *
  464. * @param string $ean13 Barcode to validate
  465. * @return boolean Validity is ok or not
  466. */
  467. public static function isEan13($ean13)
  468. {
  469. return !$ean13 || preg_match('/^[0-9]{0,13}$/', $ean13);
  470. }
  471. /**
  472. * Check for barcode validity (UPC)
  473. *
  474. * @param string $upc Barcode to validate
  475. * @return boolean Validity is ok or not
  476. */
  477. public static function isUpc($upc)
  478. {
  479. return !$upc || preg_match('/^[0-9]{0,12}$/', $upc);
  480. }
  481. /**
  482. * Check for postal code validity
  483. *
  484. * @param string $postcode Postal code to validate
  485. * @return boolean Validity is ok or not
  486. */
  487. public static function isPostCode($postcode)
  488. {
  489. return empty($postcode) || preg_match('/^[a-zA-Z 0-9-]+$/', $postcode);
  490. }
  491. /**
  492. * Check for zip code format validity
  493. *
  494. * @param string $zip_code zip code format to validate
  495. * @return boolean Validity is ok or not
  496. */
  497. public static function isZipCodeFormat($zip_code)
  498. {
  499. if (!empty($zip_code))
  500. return preg_match('/^[NLCnlc 0-9-]+$/', $zip_code);
  501. return true;
  502. }
  503. /**
  504. * Check for table or identifier validity
  505. * Mostly used in database for ordering : ASC / DESC
  506. *
  507. * @param string $way Keyword to validate
  508. * @return boolean Validity is ok or not
  509. */
  510. public static function isOrderWay($way)
  511. {
  512. return ($way === 'ASC' | $way === 'DESC' | $way === 'asc' | $way === 'desc');
  513. }
  514. /**
  515. * Check for table or identifier validity
  516. * Mostly used in database for ordering : ORDER BY field
  517. *
  518. * @param string $order Field to validate
  519. * @return boolean Validity is ok or not
  520. */
  521. public static function isOrderBy($order)
  522. {
  523. return preg_match('/^[a-zA-Z0-9._-]+$/', $order);
  524. }
  525. /**
  526. * Check for table or identifier validity
  527. * Mostly used in database for table names and id_table
  528. *
  529. * @param string $table Table/identifier to validate
  530. * @return boolean Validity is ok or not
  531. */
  532. public static function isTableOrIdentifier($table)
  533. {
  534. return preg_match('/^[a-zA-Z0-9_-]+$/', $table);
  535. }
  536. /**
  537. * @deprecated 1.5.0 You should not use list like this, please use an array when you build a SQL query
  538. */
  539. public static function isValuesList()
  540. {
  541. Tools::displayAsDeprecated();
  542. return true;
  543. /* For history reason, we keep this line */
  544. // return preg_match('/^[0-9,\'(). NULL]+$/', $list);
  545. }
  546. /**
  547. * Check for tags list validity
  548. *
  549. * @param string $list List to validate
  550. * @return boolean Validity is ok or not
  551. */
  552. public static function isTagsList($list)
  553. {
  554. return preg_match('/^[^!<>;?=+#"?{}_$%]*$/u', $list);
  555. }
  556. /**
  557. * Check for product visibility
  558. *
  559. * @param string $s visibility to check
  560. * @return boolean Validity is ok or not
  561. */
  562. public static function isProductVisibility($s)
  563. {
  564. return preg_match('/^both|catalog|search|none$/i', $s);
  565. }
  566. /**
  567. * Check for an integer validity
  568. *
  569. * @param integer $value Integer to validate
  570. * @return boolean Validity is ok or not
  571. */
  572. public static function isInt($value)
  573. {
  574. return ((string)(int)$value === (string)$value || $value === false);
  575. }
  576. /**
  577. * Check for an integer validity (unsigned)
  578. *
  579. * @param integer $value Integer to validate
  580. * @return boolean Validity is ok or not
  581. */
  582. public static function isUnsignedInt($value)
  583. {
  584. return (preg_match('#^[0-9]+$#', (string)$value) && $value < 4294967296 && $value >= 0);
  585. }
  586. /**
  587. * Check for an percentage validity (between 0 and 100)
  588. *
  589. * @param float $value Float to validate
  590. * @return boolean Validity is ok or not
  591. */
  592. public static function isPercentage($value)
  593. {
  594. return (Validate::isFloat($value) && $value >= 0 && $value <= 100);
  595. }
  596. /**
  597. * Check for an integer validity (unsigned)
  598. * Mostly used in database for auto-increment
  599. *
  600. * @param integer $id Integer to validate
  601. * @return boolean Validity is ok or not
  602. */
  603. public static function isUnsignedId($id)
  604. {
  605. return Validate::isUnsignedInt($id); /* Because an id could be equal to zero when there is no association */
  606. }
  607. public static function isNullOrUnsignedId($id)
  608. {
  609. return $id === null || Validate::isUnsignedId($id);
  610. }
  611. /**
  612. * Check object validity
  613. *
  614. * @param object $object Object to validate
  615. * @return boolean Validity is ok or not
  616. */
  617. public static function isLoadedObject($object)
  618. {
  619. return is_object($object) && $object->id;
  620. }
  621. /**
  622. * Check object validity
  623. *
  624. * @param integer $object Object to validate
  625. * @return boolean Validity is ok or not
  626. */
  627. public static function isColor($color)
  628. {
  629. return preg_match('/^(#[0-9a-fA-F]{6}|[a-zA-Z0-9-]*)$/', $color);
  630. }
  631. /**
  632. * Check url validity (disallowed empty string)
  633. *
  634. * @param string $url Url to validate
  635. * @return boolean Validity is ok or not
  636. */
  637. public static function isUrl($url)
  638. {
  639. return preg_match('/^[~:#,%&_=\(\)\.\? \+\-@\/a-zA-Z0-9]+$/', $url);
  640. }
  641. /**
  642. * Check tracking number validity (disallowed empty string)
  643. *
  644. * @param string $tracking_number Tracking number to validate
  645. * @return boolean Validity is ok or not
  646. */
  647. public static function isTrackingNumber($tracking_number)
  648. {
  649. return preg_match('/^[~:#,%&_=\(\)\.\? \+\-@\/a-zA-Z0-9]+$/', $tracking_number);
  650. }
  651. /**
  652. * Check url validity (allowed empty string)
  653. *
  654. * @param string $url Url to validate
  655. * @return boolean Validity is ok or not
  656. */
  657. public static function isUrlOrEmpty($url)
  658. {
  659. return empty($url) || Validate::isUrl($url);
  660. }
  661. /**
  662. * Check if URL is absolute
  663. *
  664. * @param string $url URL to validate
  665. * @return boolean Validity is ok or not
  666. */
  667. public static function isAbsoluteUrl($url)
  668. {
  669. if (!empty($url))
  670. return preg_match('/^https?:\/\/[,:#%&_=\(\)\.\? \+\-@\/a-zA-Z0-9]+$/', $url);
  671. return true;
  672. }
  673. public static function isMySQLEngine($engine)
  674. {
  675. return (in_array($engine, array('InnoDB', 'MyISAM')));
  676. }
  677. public static function isUnixName($data)
  678. {
  679. return preg_match('/^[a-z0-9\._-]+$/ui', $data);
  680. }
  681. public static function isTablePrefix($data)
  682. {
  683. // Even if "-" is theorically allowed, it will be considered a syntax error if you do not add backquotes (`) around the table name
  684. return preg_match('/^[a-z0-9_]+$/ui', $data);
  685. }
  686. /**
  687. * Check for standard name file validity
  688. *
  689. * @param string $name Name to validate
  690. * @return boolean Validity is ok or not
  691. */
  692. public static function isFileName($name)
  693. {
  694. return preg_match('/^[a-zA-Z0-9_.-]+$/', $name);
  695. }
  696. /**
  697. * Check for standard name directory validity
  698. *
  699. * @param string $dir Directory to validate
  700. * @return boolean Validity is ok or not
  701. */
  702. public static function isDirName($dir)
  703. {
  704. return (bool)preg_match('/^[a-zA-Z0-9_.-]*$/', $dir);
  705. }
  706. /**
  707. * Check for admin panel tab name validity
  708. *
  709. * @param string $name Name to validate
  710. * @return boolean Validity is ok or not
  711. */
  712. public static function isTabName($name)
  713. {
  714. return preg_match('/^[a-zA-Z0-9_-]*$/', $name);
  715. }
  716. public static function isWeightUnit($unit)
  717. {
  718. return preg_match('/^[a-zA-Z]{1,3}$/', $unit);
  719. }
  720. public static function isDistanceUnit($unit)
  721. {
  722. return preg_match('/^[a-zA-Z]{1,2}$/', $unit);
  723. }
  724. public static function isSubDomainName($domain)
  725. {
  726. return preg_match('/^[a-zA-Z0-9-_]*$/', $domain);
  727. }
  728. public static function isVoucherDescription($text)
  729. {
  730. return preg_match('/^([^<>{}]|<br \/>)*$/i', $text);
  731. }
  732. /**
  733. * Check if the value is a sort direction value (DESC/ASC)
  734. *
  735. * @param char $value
  736. * @return boolean Validity is ok or not
  737. */
  738. public static function isSortDirection($value)
  739. {
  740. return ($value !== null && ($value === 'ASC' || $value === 'DESC'));
  741. }
  742. /**
  743. * Customization fields' label validity
  744. *
  745. * @param string $label
  746. * @return boolean Validity is ok or not
  747. */
  748. public static function isLabel($label)
  749. {
  750. return (preg_match('/^[^{}<>]*$/u', $label));
  751. }
  752. /**
  753. * Price display method validity
  754. *
  755. * @param integer $data Data to validate
  756. * @return boolean Validity is ok or not
  757. */
  758. public static function isPriceDisplayMethod($data)
  759. {
  760. return ($data == PS_TAX_EXC || $data == PS_TAX_INC);
  761. }
  762. /**
  763. * @param string $dni to validate
  764. * @return bool
  765. */
  766. public static function isDniLite($dni)
  767. {
  768. return empty($dni) || (bool)preg_match('/^[0-9A-Za-z-.]{1,16}$/U', $dni);
  769. }
  770. /**
  771. * Check if $data is a PrestaShop cookie object
  772. *
  773. * @param mixed $data to validate
  774. * @return bool
  775. */
  776. public static function isCookie($data)
  777. {
  778. return (is_object($data) && get_class($data) == 'Cookie');
  779. }
  780. /**
  781. * Price display method validity
  782. *
  783. * @param string $data Data to validate
  784. * @return boolean Validity is ok or not
  785. */
  786. public static function isString($data)
  787. {
  788. return is_string($data);
  789. }
  790. /**
  791. * Check if the data is a reduction type (amout or percentage)
  792. *
  793. * @param string $data Data to validate
  794. * @return boolean Validity is ok or not
  795. */
  796. public static function isReductionType($data)
  797. {
  798. return ($data === 'amount' || $data === 'percentage');
  799. }
  800. /**
  801. * Check for bool_id
  802. *
  803. * @param string $ids
  804. * @return boolean Validity is ok or not
  805. */
  806. public static function isBoolId($ids)
  807. {
  808. return (bool)preg_match('#^[01]_[0-9]+$#', $ids);
  809. }
  810. /**
  811. * @deprecated 1.5.0 Use Validate::isBoolId()
  812. */
  813. public static function isBool_Id($ids)
  814. {
  815. Tools::displayAsDeprecated();
  816. return Validate::isBoolId($ids);
  817. }
  818. /**
  819. * Check the localization pack part selected
  820. *
  821. * @param string $data Localization pack to check
  822. * @return boolean Validity is ok or not
  823. */
  824. public static function isLocalizationPackSelection($data)
  825. {
  826. return ($data === 'states' || $data === 'taxes' || $data === 'currencies' || $data === 'languages' || $data === 'units');
  827. }
  828. /**
  829. * Check for PHP serialized data
  830. *
  831. * @param string $data Serialized data to validate
  832. * @return boolean Validity is ok or not
  833. */
  834. public static function isSerializedArray($data)
  835. {
  836. return $data === null || (is_string($data) && preg_match('/^a:[0-9]+:{.*;}$/s', $data));
  837. }
  838. /**
  839. * Check for Latitude/Longitude
  840. *
  841. * @param string $data Coordinate to validate
  842. * @return boolean Validity is ok or not
  843. */
  844. public static function isCoordinate($data)
  845. {
  846. return $data === null || preg_match('/^\-?[0-9]{1,8}\.[0-9]{1,8}$/s', $data);
  847. }
  848. /**
  849. * Check for Language Iso Code
  850. *
  851. * @param string $iso_code
  852. * @return boolean Validity is ok or not
  853. */
  854. public static function isLangIsoCode($iso_code)
  855. {
  856. return (bool)preg_match('/^[a-zA-Z]{2,3}$/s', $iso_code);
  857. }
  858. /**
  859. * Check for Language File Name
  860. *
  861. * @param string $file_name
  862. * @return boolean Validity is ok or not
  863. */
  864. public static function isLanguageFileName($file_name)
  865. {
  866. return (bool)preg_match('/^[a-zA-Z]{2,3}\.gzip$/s', $file_name);
  867. }
  868. /**
  869. *
  870. * @param array $ids
  871. * @return boolean return true if the array contain only unsigned int value
  872. */
  873. public static function isArrayWithIds($ids)
  874. {
  875. if (count($ids))
  876. foreach ($ids as $id)
  877. if ($id == 0 || !Validate::isUnsignedInt($id))
  878. return false;
  879. return true;
  880. }
  881. /**
  882. *
  883. * @param array $zones
  884. * @return boolean return true if array contain all value required for an image map zone
  885. */
  886. public static function isSceneZones($zones)
  887. {
  888. foreach ($zones as $zone)
  889. {
  890. if (!isset($zone['x1']) || !Validate::isUnsignedInt($zone['x1']))
  891. return false;
  892. if (!isset($zone['y1']) || !Validate::isUnsignedInt($zone['y1']))
  893. return false;
  894. if (!isset($zone['width']) || !Validate::isUnsignedInt($zone['width']))
  895. return false;
  896. if (!isset($zone['height']) || !Validate::isUnsignedInt($zone['height']))
  897. return false;
  898. if (!isset($zone['id_product']) || !Validate::isUnsignedInt($zone['id_product']))
  899. return false;
  900. }
  901. return true;
  902. }
  903. /**
  904. *
  905. * @param array $stock_management
  906. * @return boolean return true if is a valide stock management
  907. */
  908. public static function isStockManagement($stock_management)
  909. {
  910. if (!in_array($stock_management, array('WA', 'FIFO', 'LIFO')))
  911. return false;
  912. return true;
  913. }
  914. /**
  915. * Validate SIRET Code
  916. * @static
  917. * @param $siret SIRET Code
  918. * @return boolean Return true if is valid
  919. */
  920. public static function isSiret($siret)
  921. {
  922. if (Tools::strlen($siret) != 14)
  923. return false;
  924. $sum = 0;
  925. for ($i = 0; $i != 14; $i++)
  926. {
  927. $tmp = ((($i + 1) % 2) + 1) * intval($siret[$i]);
  928. if ($tmp >= 10)
  929. $tmp -= 9;
  930. $sum += $tmp;
  931. }
  932. return ($sum % 10 === 0);
  933. }
  934. /**
  935. * Validate APE Code
  936. * @static
  937. * @param $ape APE Code
  938. * @return boolean Return true if is valid
  939. */
  940. public static function isApe($ape)
  941. {
  942. return (bool)preg_match('/^[0-9]{3,4}[a-zA-Z]{1}$/s', $ape);
  943. }
  944. public static function isControllerName($name)
  945. {
  946. return (bool)(is_string($name) && preg_match('/^[0-9a-zA-Z-_]*$/u', $name));
  947. }
  948. }