Guest User

valid validate.php file

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