Source for file format.class.php

Documentation is available at format.class.php

  1. <?php   /*
  2.    *@=@=@=@ START LICENSE @=@=@=@*
  3.    Copyright or © or Copr. Stéphane TRICHET and Nicolas SOTRON
  4.    
  5.    stephane.t@simpliciweb.net and nicolas.s@simpliciweb.net
  6.    
  7.    This software is a computer program whose purpose is to make a
  8.    web hosting platform
  9.    
  10.    This software is governed by the CeCILL  license under French law and
  11.    abiding by the rules of distribution of free software.  You can  use, 
  12.    modify and/ or redistribute the software under the terms of the CeCILL
  13.    license as circulated by CEA, CNRS and INRIA at the following URL
  14.    "http://www.cecill.info". 
  15.    
  16.    As a counterpart to the access to the source code and  rights to copy,
  17.    modify and redistribute granted by the license, users are provided only
  18.    with a limited warranty  and the software's author,  the holder of the
  19.    economic rights,  and the successive licensors  have only  limited
  20.    liability. 
  21.    
  22.    In this respect, the user's attention is drawn to the risks associated
  23.    with loading,  using,  modifying and/or developing or reproducing the
  24.    software by the user in light of its specific status of free software,
  25.    that may mean  that it is complicated to manipulate,  and  that  also
  26.    therefore means  that it is reserved for developers  and  experienced
  27.    professionals having in-depth computer knowledge. Users are therefore
  28.    encouraged to load and test the software's suitability as regards their
  29.    requirements in conditions enabling the security of their systems and/or 
  30.    data to be ensured and,  more generally, to use and operate it in the 
  31.    same conditions as regards security. 
  32.    
  33.    The fact that you are presently reading this means that you have had
  34.    knowledge of the CeCILL license and that you accept its terms.
  35.    *@=@=@=@ END   LICENSE @=@=@=@*
  36.  
  37. */?><?php  
  38.  
  39.  
  40. /**
  41.  * Format Class
  42.  * 
  43.  * 
  44.  * @author SimpliciWeb
  45.  * @version $Id: format.class.php 910 2008-03-19 08:54:35Z nicolas_s $
  46.  * @package Common
  47.  * @subpackage FrameWork
  48.  */    
  49.  
  50.  
  51. /**********************/
  52. /* Méthodes publiques */
  53. /**********************/
  54.  
  55. //isEnum
  56. //isFileArray
  57. //isVarchar
  58. //isUnsignedInteger
  59. //isUnsignedFloat
  60. //isMediumText
  61. //isText
  62.  
  63. /**
  64.  * This class provid format validation
  65.  *
  66.  * @author SimpliciWeb
  67.  * @version $Id: format.class.php 910 2008-03-19 08:54:35Z nicolas_s $
  68.  * @package Common
  69.  * @subpackage FrameWork
  70.  */
  71. class Format {
  72.  
  73.  
  74.     /**
  75.      * Constructor, init the requests and objects
  76.      *
  77.      * @return Format 
  78.      */
  79.     function Format ({
  80.  
  81.     }
  82.  
  83.     /**
  84.      * Test if it is not a valid value in 'YES','NO'
  85.      *
  86.      * @param string $p_value 
  87.      * @return bool TRUE = is NOT a good value
  88.      */
  89.     function isNotYesNo(&$p_value{
  90.         return $this->isNotEnum($p_valuearray('YES''NO'));
  91.     }
  92.  
  93.     /**
  94.      * Test if it is not a date before today
  95.      *
  96.      * @param string $p_value 
  97.      * @return bool TRUE = is NOT a date before today
  98.      */
  99.     function isNotDateBeforeToday(&$p_value{
  100.  
  101.         //echo("isNotDateAfterToday : ".$p_value."<br />");
  102.  
  103.         if (!$this->isNotDate($p_value)) {
  104.  
  105.  
  106.             $date_exploded explode("-"$p_value);
  107.             //print_r($date_exploded);
  108.             $year  $date_exploded[0];
  109.             $month $date_exploded[1];
  110.             $day   $date_exploded[2];
  111.             
  112.             
  113.             
  114.             //echo($year." ".$month." ".$day);
  115.             $ts @mktime (0,0,0,$month,$day,$year);
  116.             //echo("mktime (0,0,0,$month,$day,$year)");
  117.             $ts_now time();
  118.  
  119.  
  120.         //echo("isNotDateAfterToday : ".($month*2)."<br />");
  121.         //echo("isNotDateAfterToday : ".$ts."<br />");
  122.         //echo("isNotDateAfterToday : ".$ts_now."<br />");
  123.  
  124.             return !($ts<$ts_now);
  125.  
  126.             
  127.         else {
  128.         //echo("isNotDateAfterToday : return true <br />");
  129.             return true;
  130.         }
  131.     }
  132.  
  133.  
  134.  
  135.     /**
  136.      * Test if it is not a date
  137.      *
  138.      * @param string $p_value 
  139.      * @return bool TRUE = is NOT a date
  140.      */
  141.     function isNotDate(&$p_value{
  142.         if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})"$p_value)) {
  143.             return false;
  144.         else {
  145.             return true;
  146.         }
  147.     }
  148.  
  149.     /**
  150.      * Test if it is not a domain
  151.      *
  152.      * @param string $p_value 
  153.      * @return bool TRUE = is NOT a domain
  154.      */
  155.     function isNotDomainName(&$p_value{
  156.         
  157.         $p_value trim($p_value);
  158.         if (strtolower($p_value== 'localhost' || $p_value == '127.0.0.1'{
  159.             return false;
  160.         }
  161.         
  162.         if (!eregi("^[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"$p_value)) // {2,3} => {2,4} pour les .info
  163.             return true;
  164.         else {
  165.             return false;
  166.         }
  167.     }
  168.  
  169.     /**
  170.      * Test if it is not an email
  171.      *
  172.      * @param string $p_value 
  173.      * @return bool TRUE = is NOT an email
  174.      */
  175.     function isNotEmail(&$p_value{
  176.         if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"$p_value)) {
  177.             return true;
  178.         else {
  179.             return false;
  180.         }
  181.     }
  182.  
  183.  
  184.     /**
  185.      * Return an empty $_FILES array
  186.      *
  187.      * @param array $r_result 
  188.      * @return bool 
  189.      */
  190.     function getEmptyFileArray(&$r_result{
  191.         $r_result array();
  192.  
  193.         $r_result['name'"";
  194.         $r_result['type'"";
  195.         $r_result['size'"";
  196.         $r_result['tmp_name'"";
  197.         $r_result['error'"-999";
  198.  
  199.         return false;
  200.     }
  201.  
  202.     /**
  203.      * Test if it is not a valid $_FILES array
  204.      *
  205.      * @param string $p_value 
  206.      * @return bool TRUE = is NOT a valid $_FILES array
  207.      */
  208.     function isNotFileArray($p_value{
  209.         //echo("isFileArray".$p_value);
  210.  
  211.  
  212.         if (!is_array($p_value)) {
  213.             return true;
  214.         }
  215.  
  216.         if (!isset($p_value['name'])) {
  217.             return true;
  218.         }
  219.         if (!isset($p_value['type'])) {
  220.             return true;
  221.         }
  222.         if (!isset($p_value['size'])) {
  223.             return true;
  224.         }
  225.         if (!isset($p_value['tmp_name'])) {
  226.             return true;
  227.         }
  228.         if (!isset($p_value['error'])) {
  229.             return true;
  230.         }
  231.  
  232.         return false;
  233.     }
  234.  
  235.     /**
  236.      * Test if it is not varchar
  237.      *
  238.      * @param string $p_value 
  239.      * @param integer $p_length 
  240.      * @param bool $is_good_if_len_zero 
  241.      * @return bool TRUE = is NOT a varchar
  242.      */
  243.     function isNotVarchar($p_value$p_length$is_good_if_len_zero true{
  244.  
  245.         if (strlen(trim($p_value)) <= $p_length{
  246.             
  247.             if ($is_good_if_len_zero)            
  248.                 return false;
  249.             else
  250.                 return (strlen($p_value== 0);
  251.         }
  252.  
  253.         return true;
  254.     }
  255.  
  256.     /**
  257.      * Test if it is not an unsigned integer
  258.      *
  259.      * @param string $p_value 
  260.      * @param integer $p_length 
  261.      * @return bool TRUE = is NOT a medium text
  262.      */
  263.     function isNotUnsignedInteger($p_value$p_length{
  264.         /*// tesster si c pas null en premier
  265.         * on teste pas si c null, car pour PHP un int = NULL et egal a int = 0 :(
  266.         *
  267.         */
  268.         //echo("isUnsignedInteger".$p_value);
  269.  
  270.         if (>= strlen($p_value))
  271.             return true;
  272.  
  273.         if (is_int($p_value)) {
  274.             // si c'est un chiffre on teste ca valeur maximale
  275.             $max_value 0;
  276.             for ($i=0;$i<$p_length;$i++{
  277.                 $max_value $max_value*10 9;
  278.             }
  279.             //echo("integerXXX$p_value<=**$max_value**");
  280.             if ($p_value <= $max_value{
  281.                 //echo("return true");
  282.                 return false;
  283.             }
  284.             //echo("return false");
  285.             return true;
  286.         else {
  287.             // si c'est une string
  288.             // on verifie qu'elle ne contient que des nombres
  289.             $for_test eregi_replace("[0-9]+"""$p_value);
  290.  
  291.             //echo("===$forTest===");
  292.             ////echo("char");
  293.             if ((strlen($p_value<= $p_length&& (strlen($for_test== 0)) {
  294.                 return false;
  295.             }
  296.  
  297.             return true;
  298.  
  299.         }
  300.     }
  301.  
  302.  
  303.     /**
  304.      * Test if it is not unsigned float
  305.      *
  306.      * @param string $p_value 
  307.      * @return bool TRUE = is NOT an unsigned float
  308.      */
  309.     function isNotUnsignedFloat($p_value{
  310.         /*// tesster si c pas null en premier
  311.         * on teste pas si c null, car pour PHP un int = NULL et egal a int = 0 :(
  312.         *
  313.         */
  314.         //echo("isUnsignedFloat<br />".$p_value);
  315.         if ($p_value == "")
  316.             return true;
  317.  
  318.         $length 38;
  319.         if (is_float($p_value)) {
  320.             // si c'est un chiffre on teste ca valeur maximale
  321.             //echo("is float $p_value");
  322.             $max_value 0;
  323.             for ($i=0;$i<$length;$i++{
  324.                 $max_value $max_value*10 9;
  325.             }
  326.             //echo("vraifloat<br />".$p_value);
  327.             return ($p_value $max_value);
  328.         else {
  329.             // si c'est une string
  330.             // on verifie qu'elle ne contient que des nombres
  331.             //echo("is string but float $p_value<br />");
  332.             $for_test eregi_replace("[0-9]+(\.)?[0-9]*"""$p_value);
  333.             //echo("string float<br />".$p_value);
  334.             //echo("string test<br />".$forTest);
  335.  
  336.             if ((strlen($p_value<= $length&& (strlen($for_test== 0)) {
  337.                 //echo("GOOD<br />");
  338.                 return false;
  339.             }
  340.  
  341.             //echo("BAD<br />");
  342.             return true;
  343.  
  344.         }
  345.     }
  346.  
  347.  
  348.     /**
  349.      * Test if it is not a medium text
  350.      *
  351.      * @param string $p_value 
  352.      * @return bool TRUE = is NOT a medium text
  353.      */
  354.     function isNotMediumText($p_value{
  355.         // tester si c pas null en premier
  356.         //echo("isMediumText".$p_value);
  357.         $length 16777215 ;
  358.  
  359.         return $this->isNotVarchar($p_value$length);
  360.     }
  361.  
  362.  
  363.  
  364.     /**
  365.      * Test if it is not a text
  366.      *
  367.      * @param string $p_value 
  368.      * @return bool TRUE = is NOT a text
  369.      */
  370.     function isNotText($p_value{
  371.         // tester si c pas null en premier
  372.         //echo("isText".$p_value);
  373.         $length 65535 ;
  374.  
  375.         return $this->isNotVarchar($p_value$length);
  376.     }
  377.  
  378.     /**
  379.      * Test if it is not a medium text
  380.      *
  381.      * @param string $p_value 
  382.      * @return bool TRUE = is NOT a medium text
  383.      */
  384.     function isNotMedText($p_value{
  385.         $length 16777215 ;
  386.  
  387.         return $this->isNotVarchar($p_value$length);
  388.     }
  389.  
  390.  
  391.     /**
  392.      * Test if it is not a valid enum value
  393.      *
  394.      * @param mixed $p_value 
  395.      * @param array $p_array_values 
  396.      * @param bool $i_make_to_upper 
  397.      * @return bool TRUE = is NOT a valid enum value
  398.      */
  399.     function isNotEnum(&$p_value$p_array_values$i_make_to_upper false{
  400.         //echo("isEnum".$value);
  401.         $finded false;
  402.  
  403.         if ($i_make_to_upper{
  404.             $p_value strtoupper($p_value);
  405.         }
  406.  
  407.         foreach($p_array_values as $key => $value{
  408.             if ($value == $p_value{
  409.                 $finded true;
  410.                 break;
  411.             }
  412.         }
  413. //        for ($i=0;$i<sizeof($p_array_values);$i++) {
  414. //            if ($p_array_values[$i] == $p_value) {
  415. //                $finded = true;
  416. //                break;
  417. //            }
  418. //        }
  419.  
  420.         return !$finded;
  421.  
  422.     }
  423.  
  424.  
  425.     /**
  426.      * Test if it is not valid email
  427.      *
  428.      * @param string $p_value 
  429.      * @return bool TRUE = is NOT valid email
  430.      */
  431.     function isNotValidCheckMXMail($Email{
  432.  
  433.         $HTTP_HOST $_SERVER['HTTP_HOST'];
  434.  
  435.         if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"$Email)) {  // test si le format de l'email est bon
  436.             return true;
  437.         }
  438.  
  439.         return false;
  440.  
  441.         /*
  442.          * 
  443.          * Ne sera jamais traité car si on fait un telnet sur
  444.          * wanadoo.com (et pas fr) ca fait un timeout et c pas
  445.          * bon
  446.          * 
  447.          */
  448.  
  449.         list($Username$Domainsplit("@"$Email);  // split le nom et le domaine
  450.  
  451.  
  452.         //echo "<br /><HR><br />";
  453.  
  454.         global $g_object_loader;
  455.         $res =$g_object_loader->getNet_DNS_Resolver();
  456.         $answer $res->search($Domain"MX");
  457.         //echo "<br /><HR><br /><PRE>";
  458.         //print_r($answer);
  459.         //echo "</PRE>";
  460.  
  461.         if ($answer//verifie existance serveur de mail sur ce domaine
  462.         $ConnectAddress $answer->additional[0]->address;
  463.         else {     // Si pas d'enregistrement MX, on met simplement le domaine comme adresse de connexion
  464.         $ConnectAddress $Domain;
  465.         }
  466.         
  467.         //echo($ConnectAddress);
  468.  
  469.         //if (!(($_SERVER["SERVER_NAME"]=="200.1.1.93") ||
  470.         //($_SERVER["SERVER_NAME"]=="200.1.1.108"))) 
  471.         //{
  472.  
  473.             $Connect @fsockopen($ConnectAddress25);
  474.  
  475.             if ($Connect// Si socket ouvert
  476.             //echo("connected<br />");
  477.             $Out fgets($Connect1024);
  478.             //echo("$Out<br />");
  479.             if (ereg("^220"$Out )) {
  480.                 fputs($Connect"HELO $HTTP_HOST\r\n");
  481.                 $Out fgets($Connect1024);
  482.                 //echo("$Out<br />");
  483.                 fputs($Connect"MAIL FROM: <{$Email}>\r\n");
  484.                 $From fgets($Connect1024);
  485.                 //echo("$From<br />");
  486.                 fputs($Connect"RCPT TO: <{$Email}>\r\n");
  487.                 $To fgets($Connect1024);
  488.                 //echo("$Connect<br />");
  489.                 fputs($Connect"QUIT\r\n");
  490.                 fclose($Connect);
  491.  
  492.                 if (!ereg ("^250"$From|| !ereg ("^250"$To)) // Si adresse n'existe pas
  493.                 return true;
  494.                 }
  495.             }
  496.             }
  497.              else // Si le telnet échoue, ce n'est pas une preuve
  498.             return false;
  499.             }
  500.         //}
  501.  
  502.         // Si tout est OK
  503.         return false;
  504.     }
  505.  
  506.  
  507.     /**
  508.      * Test if it is not valid postal code
  509.      *
  510.      * @param string $p_value 
  511.      * @return bool TRUE = is NOT a valid postal code
  512.      */
  513.     function isNotValidPostalCode($p_value{
  514.         if ($this->isNotVarchar($p_value10)) return true;
  515.         if (ereg ("^(\d{2}(( \d{3})|\d( )?\d{1,2}(-\d{4})?))|([a-zA-Z]([a-zA-Z])?\d(([a-zA-Z])|\d)? \d([a-zA-Z]){2})"$p_value)) return true;
  516.         return false;
  517.     }
  518.  
  519.     /**
  520.      * Test if it is not a phone number
  521.      *
  522.      * @param string $p_value 
  523.      * @param bool     $p_allow_empty  Allow empty value
  524.      * @return bool TRUE = is NOT a phone number
  525.      */
  526.     function isNotValidPhoneNumber($p_value$p_allow_empty false{
  527.         $p_value trim($p_value);
  528.         if ($p_value == "")
  529.           if ($p_allow_empty)  
  530.               return false;
  531.           else 
  532.               return true;
  533.         if ($this->isNotVarchar($p_value20)) return true;
  534.         if (ereg ("^[0-9 +.)(-]*$"$p_value)) return false;
  535.         return true;
  536.     }
  537.  
  538.  
  539.     
  540. }
  541.  
  542. ?>

Documentation generated on Tue, 25 Mar 2008 15:35:02 +0100 by phpDocumentor 1.3.2