Advertisement
Guest User

Validate.php

a guest
Nov 26th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 27.75 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 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-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-z0-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.     {
  394.         $events = 'onmousedown|onmousemove|onmmouseup|onmouseover|onmouseout|onload|onunload|onfocus|onblur|onchange';
  395.         $events .= '|onsubmit|ondblclick|onclick|onkeydown|onkeyup|onkeypress|onmouseenter|onmouseleave|onerror|onselect|onreset|onabort|ondragdrop|onresize|onactivate|onafterprint|onmoveend';
  396.         $events .= '|onafterupdate|onbeforeactivate|onbeforecopy|onbeforecut|onbeforedeactivate|onbeforeeditfocus|onbeforepaste|onbeforeprint|onbeforeunload|onbeforeupdate|onmove';
  397.         $events .= '|onbounce|oncellchange|oncontextmenu|oncontrolselect|oncopy|oncut|ondataavailable|ondatasetchanged|ondatasetcomplete|ondeactivate|ondrag|ondragend|ondragenter|onmousewheel';
  398.         $events .= '|ondragleave|ondragover|ondragstart|ondrop|onerrorupdate|onfilterchange|onfinish|onfocusin|onfocusout|onhashchange|onhelp|oninput|onlosecapture|onmessage|onmouseup|onmovestart';
  399.         $events .= '|onoffline|ononline|onpaste|onpropertychange|onreadystatechange|onresizeend|onresizestart|onrowenter|onrowexit|onrowsdelete|onrowsinserted|onscroll|onsearch|onselectionchange';
  400.         $events .= '|onselectstart|onstart|onstop';
  401.  
  402.         if (preg_match('/<[\s]*script/ims', $html) || preg_match('/('.$events.')[\s]*=/ims', $html) || preg_match('/.*script\:/ims', $html))
  403.             return false;
  404.  
  405.         if (!$allow_iframe && preg_match('/<[\s]*(i?frame|form|input|embed|object)/ims', $html))
  406.             return false;
  407.  
  408.         return true;
  409.     }
  410.  
  411.     /**
  412.      * Check for product reference validity
  413.      *
  414.      * @param string $reference Product reference to validate
  415.      * @return boolean Validity is ok or not
  416.      */
  417.     public static function isReference($reference)
  418.     {
  419.         return preg_match('/^[^<>;={}]*$/u', $reference);
  420.     }
  421.  
  422.     /**
  423.      * Check for password validity
  424.      *
  425.      * @param string $passwd Password to validate
  426.      * @param int $size
  427.      * @return boolean Validity is ok or not
  428.      */
  429.     public static function isPasswd($passwd, $size = 5)
  430.     {
  431.         return (Tools::strlen($passwd) >= $size && Tools::strlen($passwd) < 255);
  432.     }
  433.  
  434.     public static function isPasswdAdmin($passwd)
  435.     {
  436.         return Validate::isPasswd($passwd, 8);
  437.     }
  438.  
  439.     /**
  440.      * Check for configuration key validity
  441.      *
  442.      * @param string $config_name Configuration key to validate
  443.      * @return boolean Validity is ok or not
  444.      */
  445.     public static function isConfigName($config_name)
  446.     {
  447.         return preg_match('/^[a-zA-Z_0-9-]+$/', $config_name);
  448.     }
  449.  
  450.     /**
  451.      * Check date formats like http://php.net/manual/en/function.date.php
  452.      *
  453.      * @param string $date_format date format to check
  454.      * @return boolean Validity is ok or not
  455.      */
  456.     public static function isPhpDateFormat($date_format)
  457.     {
  458.         // 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.
  459.         // That's why only < et > are forbidden (HTML)
  460.         return preg_match('/^[^<>]+$/', $date_format);
  461.     }
  462.  
  463.     /**
  464.      * Check for date format
  465.      *
  466.      * @param string $date Date to validate
  467.      * @return boolean Validity is ok or not
  468.      */
  469.     public static function isDateFormat($date)
  470.     {
  471.         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);
  472.     }
  473.  
  474.     /**
  475.      * Check for date validity
  476.      *
  477.      * @param string $date Date to validate
  478.      * @return boolean Validity is ok or not
  479.      */
  480.     public static function isDate($date)
  481.     {
  482.         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))
  483.             return false;
  484.         return checkdate((int)$matches[2], (int)$matches[3], (int)$matches[1]);
  485.     }
  486.  
  487.     /**
  488.      * Check for birthDate validity
  489.      *
  490.      * @param string $date birthdate to validate
  491.      * @return boolean Validity is ok or not
  492.      */
  493.     public static function isBirthDate($date)
  494.     {
  495.         if (empty($date) || $date == '0000-00-00')
  496.             return true;
  497.         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))
  498.         {
  499.             if ($birth_date[1] > date('Y') && $birth_date[2] > date('m') && $birth_date[3] > date('d'))
  500.                 return false;
  501.             return true;
  502.         }
  503.         return false;
  504.     }
  505.  
  506.     /**
  507.      * Check for boolean validity
  508.      *
  509.      * @param boolean $bool Boolean to validate
  510.      * @return boolean Validity is ok or not
  511.      */
  512.     public static function isBool($bool)
  513.     {
  514.         return $bool === null || is_bool($bool) || preg_match('/^0|1$/', $bool);
  515.     }
  516.  
  517.     /**
  518.      * Check for phone number validity
  519.      *
  520.      * @param string $number Phone number to validate
  521.      * @return boolean Validity is ok or not
  522.      */
  523.     public static function isPhoneNumber($number)
  524.     {
  525.         return preg_match('/^[+0-9. ()-]*$/', $number);
  526.     }
  527.  
  528.     /**
  529.      * Check for barcode validity (EAN-13)
  530.      *
  531.      * @param string $ean13 Barcode to validate
  532.      * @return boolean Validity is ok or not
  533.      */
  534.     public static function isEan13($ean13)
  535.     {
  536.         return !$ean13 || preg_match('/^[0-9]{0,13}$/', $ean13);
  537.     }
  538.  
  539.     /**
  540.      * Check for barcode validity (UPC)
  541.      *
  542.      * @param string $upc Barcode to validate
  543.      * @return boolean Validity is ok or not
  544.      */
  545.     public static function isUpc($upc)
  546.     {
  547.         return !$upc || preg_match('/^[0-9]{0,12}$/', $upc);
  548.     }
  549.  
  550.     /**
  551.      * Check for postal code validity
  552.      *
  553.      * @param string $postcode Postal code to validate
  554.      * @return boolean Validity is ok or not
  555.      */
  556.     public static function isPostCode($postcode)
  557.     {
  558.         return empty($postcode) || preg_match('/^[a-zA-Z 0-9-]+$/', $postcode);
  559.     }
  560.  
  561.     /**
  562.      * Check for zip code format validity
  563.      *
  564.      * @param string $zip_code zip code format to validate
  565.      * @return boolean Validity is ok or not
  566.      */
  567.     public static function isZipCodeFormat($zip_code)
  568.     {
  569.         if (!empty($zip_code))
  570.             return preg_match('/^[NLCnlc 0-9-]+$/', $zip_code);
  571.         return true;
  572.     }
  573.  
  574.     /**
  575.      * Check for table or identifier validity
  576.      * Mostly used in database for ordering : ASC / DESC
  577.      *
  578.      * @param string $way Keyword to validate
  579.      * @return boolean Validity is ok or not
  580.      */
  581.     public static function isOrderWay($way)
  582.     {
  583.         return ($way === 'ASC' | $way === 'DESC' | $way === 'asc' | $way === 'desc');
  584.     }
  585.  
  586.     /**
  587.      * Check for table or identifier validity
  588.      * Mostly used in database for ordering : ORDER BY field
  589.      *
  590.      * @param string $order Field to validate
  591.      * @return boolean Validity is ok or not
  592.      */
  593.     public static function isOrderBy($order)
  594.     {
  595.         return preg_match('/^[a-zA-Z0-9.!_-]+$/', $order);
  596.     }
  597.  
  598.     /**
  599.      * Check for table or identifier validity
  600.      * Mostly used in database for table names and id_table
  601.      *
  602.      * @param string $table Table/identifier to validate
  603.      * @return boolean Validity is ok or not
  604.      */
  605.     public static function isTableOrIdentifier($table)
  606.     {
  607.         return preg_match('/^[a-zA-Z0-9_-]+$/', $table);
  608.     }
  609.  
  610.     /**
  611.      * @deprecated 1.5.0 You should not use list like this, please use an array when you build a SQL query
  612.      */
  613.     public static function isValuesList()
  614.     {
  615.         Tools::displayAsDeprecated();
  616.         return true;
  617.         /* For history reason, we keep this line */
  618.         // return preg_match('/^[0-9,\'(). NULL]+$/', $list);
  619.     }
  620.  
  621.     /**
  622.      * Check for tags list validity
  623.      *
  624.      * @param string $list List to validate
  625.      * @return boolean Validity is ok or not
  626.      */
  627.     public static function isTagsList($list)
  628.     {
  629.         return preg_match('/^[^!<>;?=+#"°{}_$%]*$/u', $list);
  630.     }
  631.  
  632.     /**
  633.      * Check for product visibility
  634.      *
  635.      * @param string $s visibility to check
  636.      * @return boolean Validity is ok or not
  637.      */
  638.     public static function isProductVisibility($s)
  639.     {
  640.         return preg_match('/^both|catalog|search|none$/i', $s);
  641.     }
  642.  
  643.     /**
  644.      * Check for an integer validity
  645.      *
  646.      * @param integer $value Integer to validate
  647.      * @return boolean Validity is ok or not
  648.      */
  649.     public static function isInt($value)
  650.     {
  651.         return ((string)(int)$value === (string)$value || $value === false);
  652.     }
  653.  
  654.     /**
  655.      * Check for an integer validity (unsigned)
  656.      *
  657.      * @param integer $value Integer to validate
  658.      * @return boolean Validity is ok or not
  659.      */
  660.     public static function isUnsignedInt($value)
  661.     {
  662.         return (preg_match('#^[0-9]+$#', (string)$value) && $value < 4294967296 && $value >= 0);
  663.     }
  664.    
  665.     /**
  666.      * Check for an percentage validity (between 0 and 100)
  667.      *
  668.      * @param float $value Float to validate
  669.      * @return boolean Validity is ok or not
  670.      */
  671.     public static function isPercentage($value)
  672.     {
  673.         return (Validate::isFloat($value) && $value >= 0 && $value <= 100);
  674.     }
  675.  
  676.     /**
  677.      * Check for an integer validity (unsigned)
  678.      * Mostly used in database for auto-increment
  679.      *
  680.      * @param integer $id Integer to validate
  681.      * @return boolean Validity is ok or not
  682.      */
  683.     public static function isUnsignedId($id)
  684.     {
  685.         return Validate::isUnsignedInt($id); /* Because an id could be equal to zero when there is no association */
  686.     }
  687.  
  688.     public static function isNullOrUnsignedId($id)
  689.     {
  690.         return $id === null || Validate::isUnsignedId($id);
  691.     }
  692.  
  693.     /**
  694.      * Check object validity
  695.      *
  696.      * @param object $object Object to validate
  697.      * @return boolean Validity is ok or not
  698.      */
  699.     public static function isLoadedObject($object)
  700.     {
  701.         return is_object($object) && $object->id;
  702.     }
  703.  
  704.     /**
  705.      * Check object validity
  706.      *
  707.      * @param integer $object Object to validate
  708.      * @return boolean Validity is ok or not
  709.      */
  710.     public static function isColor($color)
  711.     {
  712.         return preg_match('/^(#[0-9a-fA-F]{6}|[a-zA-Z0-9-]*)$/', $color);
  713.     }
  714.  
  715.     /**
  716.      * Check url validity (disallowed empty string)
  717.      *
  718.      * @param string $url Url to validate
  719.      * @return boolean Validity is ok or not
  720.      */
  721.     public static function isUrl($url)
  722.     {
  723.         return preg_match('/^[~:#,%&_=\(\)\.\? \+\-@\/a-zA-Z0-9]+$/', $url);
  724.     }
  725.  
  726.     /**
  727.      * Check tracking number validity (disallowed empty string)
  728.      *
  729.      * @param string $tracking_number Tracking number to validate
  730.      * @return boolean Validity is ok or not
  731.      */
  732.     public static function isTrackingNumber($tracking_number)
  733.     {
  734.         return preg_match('/^[~:#,%&_=\(\)\[\]\.\? \+\-@\/a-zA-Z0-9]+$/', $tracking_number);
  735.     }
  736.  
  737.     /**
  738.      * Check url validity (allowed empty string)
  739.      *
  740.      * @param string $url Url to validate
  741.      * @return boolean Validity is ok or not
  742.      */
  743.     public static function isUrlOrEmpty($url)
  744.     {
  745.         return empty($url) || Validate::isUrl($url);
  746.     }
  747.  
  748.     /**
  749.      * Check if URL is absolute
  750.      *
  751.      * @param string $url URL to validate
  752.      * @return boolean Validity is ok or not
  753.      */
  754.     public static function isAbsoluteUrl($url)
  755.     {
  756.         if (!empty($url))
  757.             return preg_match('/^https?:\/\/[~:#,%&_=\(\)\[\]\.\? \+\-@\/a-zA-Z0-9]+$/', $url);
  758.         return true;
  759.     }
  760.  
  761.     public static function isMySQLEngine($engine)
  762.     {
  763.         return (in_array($engine, array('InnoDB', 'MyISAM')));
  764.     }
  765.  
  766.     public static function isUnixName($data)
  767.     {
  768.         return preg_match('/^[a-z0-9\._-]+$/ui', $data);
  769.     }
  770.  
  771.     public static function isTablePrefix($data)
  772.     {
  773.         // Even if "-" is theorically allowed, it will be considered a syntax error if you do not add backquotes (`) around the table name
  774.         return preg_match('/^[a-z0-9_]+$/ui', $data);
  775.     }
  776.  
  777.     /**
  778.      * Check for standard name file validity
  779.      *
  780.      * @param string $name Name to validate
  781.      * @return boolean Validity is ok or not
  782.      */
  783.     public static function isFileName($name)
  784.     {
  785.         return preg_match('/^[a-zA-Z0-9_.-]+$/', $name);
  786.     }
  787.  
  788.     /**
  789.      * Check for standard name directory validity
  790.      *
  791.      * @param string $dir Directory to validate
  792.      * @return boolean Validity is ok or not
  793.      */
  794.     public static function isDirName($dir)
  795.     {
  796.         return (bool)preg_match('/^[a-zA-Z0-9_.-]*$/', $dir);
  797.     }
  798.  
  799.     /**
  800.      * Check for admin panel tab name validity
  801.      *
  802.      * @param string $name Name to validate
  803.      * @return boolean Validity is ok or not
  804.      */
  805.     public static function isTabName($name)
  806.     {
  807.         return preg_match('/^[a-zA-Z0-9_-]*$/', $name);
  808.     }
  809.  
  810.     public static function isWeightUnit($unit)
  811.     {
  812.         return (Validate::isGenericName($unit) & (Tools::strlen($unit) < 5));
  813.     }
  814.  
  815.     public static function isDistanceUnit($unit)
  816.     {
  817.         return (Validate::isGenericName($unit) & (Tools::strlen($unit) < 5));
  818.     }
  819.  
  820.     public static function isSubDomainName($domain)
  821.     {
  822.         return preg_match('/^[a-zA-Z0-9-_]*$/', $domain);
  823.     }
  824.  
  825.     public static function isVoucherDescription($text)
  826.     {
  827.         return preg_match('/^([^<>{}]|<br \/>)*$/i', $text);
  828.     }
  829.  
  830.     /**
  831.      * Check if the value is a sort direction value (DESC/ASC)
  832.      *
  833.      * @param char $value
  834.      * @return boolean Validity is ok or not
  835.      */
  836.     public static function isSortDirection($value)
  837.     {
  838.         return ($value !== null && ($value === 'ASC' || $value === 'DESC'));
  839.     }
  840.  
  841.     /**
  842.      * Customization fields' label validity
  843.      *
  844.      * @param string $label
  845.      * @return boolean Validity is ok or not
  846.      */
  847.     public static function isLabel($label)
  848.     {
  849.         return (preg_match('/^[^{}<>]*$/u', $label));
  850.     }
  851.  
  852.     /**
  853.      * Price display method validity
  854.      *
  855.      * @param integer $data Data to validate
  856.      * @return boolean Validity is ok or not
  857.      */
  858.     public static function isPriceDisplayMethod($data)
  859.     {
  860.         return ($data == PS_TAX_EXC || $data == PS_TAX_INC);
  861.     }
  862.  
  863.     /**
  864.      * @param string $dni to validate
  865.      * @return bool
  866.      */
  867.     public static function isDniLite($dni)
  868.     {
  869.         return empty($dni) || (bool)preg_match('/^[0-9A-Za-z-.]{1,16}$/U', $dni);
  870.     }
  871.  
  872.     /**
  873.      * Check if $data is a PrestaShop cookie object
  874.      *
  875.      * @param mixed $data to validate
  876.      * @return bool
  877.      */
  878.     public static function isCookie($data)
  879.     {
  880.         return (is_object($data) && get_class($data) == 'Cookie');
  881.     }
  882.  
  883.     /**
  884.      * Price display method validity
  885.      *
  886.      * @param string $data Data to validate
  887.      * @return boolean Validity is ok or not
  888.      */
  889.     public static function isString($data)
  890.     {
  891.         return is_string($data);
  892.     }
  893.  
  894.     /**
  895.      * Check if the data is a reduction type (amout or percentage)
  896.      *
  897.      * @param string $data Data to validate
  898.      * @return boolean Validity is ok or not
  899.      */
  900.     public static function isReductionType($data)
  901.     {
  902.         return ($data === 'amount' || $data === 'percentage');
  903.     }
  904.  
  905.     /**
  906.      * Check for bool_id
  907.      *
  908.      * @param string $ids
  909.      * @return boolean Validity is ok or not
  910.      */
  911.     public static function isBoolId($ids)
  912.     {
  913.         return (bool)preg_match('#^[01]_[0-9]+$#', $ids);
  914.     }
  915.  
  916.     /**
  917.      * @deprecated 1.5.0 Use Validate::isBoolId()
  918.      */
  919.     public static function isBool_Id($ids)
  920.     {
  921.         Tools::displayAsDeprecated();
  922.         return Validate::isBoolId($ids);
  923.     }
  924.  
  925.     /**
  926.      * Check the localization pack part selected
  927.      *
  928.      * @param string $data Localization pack to check
  929.      * @return boolean Validity is ok or not
  930.      */
  931.     public static function isLocalizationPackSelection($data)
  932.     {
  933.         return ($data === 'states' || $data === 'taxes' || $data === 'currencies' || $data === 'languages' || $data === 'units');
  934.     }
  935.  
  936.     /**
  937.      * Check for PHP serialized data
  938.      *
  939.      * @param string $data Serialized data to validate
  940.      * @return boolean Validity is ok or not
  941.      */
  942.     public static function isSerializedArray($data)
  943.     {
  944.         return $data === null || (is_string($data) && preg_match('/^a:[0-9]+:{.*;}$/s', $data));
  945.     }
  946.  
  947.     /**
  948.      * Check for Latitude/Longitude
  949.      *
  950.      * @param string $data Coordinate to validate
  951.      * @return boolean Validity is ok or not
  952.      */
  953.     public static function isCoordinate($data)
  954.     {
  955.         return $data === null || preg_match('/^\-?[0-9]{1,8}\.[0-9]{1,8}$/s', $data);
  956.     }
  957.  
  958.     /**
  959.      * Check for Language Iso Code
  960.      *
  961.      * @param string $iso_code
  962.      * @return boolean Validity is ok or not
  963.      */
  964.     public static function isLangIsoCode($iso_code)
  965.     {
  966.         return (bool)preg_match('/^[a-zA-Z]{2,3}$/s', $iso_code);
  967.     }
  968.  
  969.     /**
  970.      * Check for Language File Name
  971.      *
  972.      * @param string $file_name
  973.      * @return boolean Validity is ok or not
  974.      */
  975.     public static function isLanguageFileName($file_name)
  976.     {
  977.         return (bool)preg_match('/^[a-zA-Z]{2,3}\.gzip$/s', $file_name);
  978.     }
  979.  
  980.     /**
  981.      *
  982.      * @param array $ids
  983.      * @return boolean return true if the array contain only unsigned int value
  984.      */
  985.     public static function isArrayWithIds($ids)
  986.     {
  987.         if (count($ids))
  988.             foreach ($ids as $id)
  989.                 if ($id == 0 || !Validate::isUnsignedInt($id))
  990.                     return false;
  991.         return true;
  992.     }
  993.  
  994.     /**
  995.      *
  996.      * @param array $zones
  997.      * @return boolean return true if array contain all value required for an image map zone
  998.      */
  999.     public static function isSceneZones($zones)
  1000.     {
  1001.         foreach ($zones as $zone)
  1002.         {
  1003.             if (!isset($zone['x1']) || !Validate::isUnsignedInt($zone['x1']))
  1004.                 return false;
  1005.             if (!isset($zone['y1']) || !Validate::isUnsignedInt($zone['y1']))
  1006.                 return false;
  1007.             if (!isset($zone['width']) || !Validate::isUnsignedInt($zone['width']))
  1008.                 return false;
  1009.             if (!isset($zone['height']) || !Validate::isUnsignedInt($zone['height']))
  1010.                 return false;
  1011.             if (!isset($zone['id_product']) || !Validate::isUnsignedInt($zone['id_product']))
  1012.                 return false;
  1013.         }
  1014.         return true;
  1015.     }
  1016.  
  1017.     /**
  1018.      *
  1019.      * @param array $stock_management
  1020.      * @return boolean return true if is a valide stock management
  1021.      */
  1022.     public static function isStockManagement($stock_management)
  1023.     {
  1024.         if (!in_array($stock_management, array('WA', 'FIFO', 'LIFO')))
  1025.             return false;
  1026.         return true;
  1027.     }
  1028.  
  1029.     /**
  1030.      * Validate SIRET Code
  1031.      * @static
  1032.      * @param $siret SIRET Code
  1033.      * @return boolean Return true if is valid
  1034.      */
  1035.     public static function isSiret($siret)
  1036.     {
  1037.         if (Tools::strlen($siret) != 14)
  1038.             return false;
  1039.         $sum = 0;
  1040.         for ($i = 0; $i != 14; $i++)
  1041.         {
  1042.             $tmp = ((($i + 1) % 2) + 1) * intval($siret[$i]);
  1043.             if ($tmp >= 10)
  1044.                 $tmp -= 9;
  1045.             $sum += $tmp;
  1046.         }
  1047.         return ($sum % 10 === 0);
  1048.     }
  1049.  
  1050.     /**
  1051.      * Validate APE Code
  1052.      * @static
  1053.      * @param $ape APE Code
  1054.      * @return boolean Return true if is valid
  1055.      */
  1056.     public static function isApe($ape)
  1057.     {
  1058.         return (bool)preg_match('/^[0-9]{3,4}[a-zA-Z]{1}$/s', $ape);
  1059.     }
  1060.    
  1061.     public static function isControllerName($name)
  1062.     {
  1063.         return (bool)(is_string($name) && preg_match('/^[0-9a-zA-Z-_]*$/u', $name));
  1064.     }
  1065.    
  1066.     public static function isPrestaShopVersion($version)
  1067.     {
  1068.         return (preg_match('/^[0-1]\.[0-9]{1,2}(\.[0-9]{1,2}){0,2}$/', $version) && ip2long($version));
  1069.     }
  1070. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement