VEKIA

validate.php for prestashop 1.5.4.1

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