<?php

/**
 * @class textVerification
 * @brief Check the text.
 * 
 * @author Benjamin Mercier
 * @data 28/03/2009
 * @modified Xavier Perrissoud
 * @date 13/10/2009
 * @version 0.1
 * @todo Add documentation for each method (and give explicitelly each format allowed)
 * @todo Add an optional param to verifUnixPath, to check if the folder exists in the user's tree
 * Some unixPath verification need that the checked folder is a valid one, and that it already exists.
 * For example, when adding a new virtualhost, the root directory must exists.
 * Also add a returned value other than true/false if the directory does not exists
 * @todo Check the "verifUnixPath" regexp
 */
class textVerification
{
    
    static public function verifAjaxID ($ajax_id)
    {
        $regex = '`^[a-z0-9_]{1,40}$`i';
        if (preg_match($regex, $ajax_id) == 1 ) return true;
        else return false;
    } // End of checkAjaxID
  
    static public function verifFirstName ($first_name)
    {
        $regex = '`^[a-zA-Z., -]{1,35}$`';
        if (preg_match($regex, $first_name) == 1 ) return true;
        else return false;
    } // End of verifFirstName
    
    static public function verifLastName ($last_name)
    {
        $regex = '`^[a-zA-Z., -]{1,35}$`';
        if (preg_match($regex, $last_name) == 1 ) return true;
        else return false;	
    } // End of verifLastName
    
    static public function verifCompany ($company)
    {
        $regex = '`^[a-zA-Z0-9.,& -]{0,35}$`';
        if (preg_match($regex, $company) == 1 ) return true;
        else return false;
    } // End of verifCompany
    
    static public function verifAddress ($address)
    {
        $regex = '`^[a-zA-Z0-9.,& -]{0,250}$`';
        if (preg_match($regex, $address) == 1 ) return true;
        else return false;
    } // End of verifAddress
    
    static public function verifCity ($city)
    {
        $regex = '`^[a-zA-Z -]{1,35}$`';
        if (preg_match($regex, $city) == 1 ) return true;
        else return false;
    } // End of verifCity
    
    static public function verifZipcode ($zipcode)
    {
        $regex = '`^[a-zA-Z0-9-]{1,12}$`';
        if (preg_match($regex, $zipcode) == 1 ) return true;
        else return false;
    } // End of verifZipcode
    
    static public function verifEmail ($email)
    {
        $regex = '`^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$`';
        if (preg_match($regex, $email) == 1 ) return true;
        else return false;
    } // End of verifEmail
    
    static public function verifPseudo ($pseudo)
    {
        $regex = '`^[a-z0-9]{1,9}$`';
        if (preg_match($regex, $pseudo) == 1 ) return true;
        else return false;
    } // End of verifPseudo
    
    static public function verifCountrieID ($countrie)
    {
        $regex = '`^[0-9]{1,11}$`';
        if (preg_match($regex, $countrie) == 1 ) return true;
        else return false;
    } // End of verifCountrieID
    
    static public function verifLangID ($lang)
    {
        $regex = '`^[0-9]{1,11}$`';
        if (preg_match($regex, $lang) == 1 ) return true;
        else return false;
    } // Endd of verifLangID
    
    static public function verifTemplate ($template)
    {
        $regex = '`^[a-zA-Z]{0,20}$`';
        if (preg_match($regex, $template) == 1 ) return true;
        else return false;
    }
    
    static public function verifHostingID ($hosting_id)
    {
        $regex = '`^[0-9]{1,11}$`';
        if (preg_match($regex, $hosting_id) == 1 ) return true;
        else return false;
    } // end of verifHostingID
    
    static public function verifID ($id)
    {
        $regex = '`^[1-9][0-9]{0,10}$`';
        if (preg_match($regex, $id) == 1 ) return true;
        else return false;
    } // end of verifID
    
    static public function verifSubject ($subject)
    {
        // 5 to 100 chars, begining with an alphanumeric one
        $regex='`^[a-z0-9éèàçù].{4,99}$`i';
        if (preg_match($regex, $subject) == 1 ) return true;
        else return false;
    } // End of verifSubject
    
    static public function verifMessage ($message)
    {
        // 5 to 500 chars, begining with an alphanumeric one
        $regex='`^[a-zA-Z0-9éèàçu].{4,499}$`mi';
        if (preg_match($regex, $message) == 1 ) return true;
        else return false;
    } // End of verifMessage
    
	static public function verifUrl ($url)
	{
		$regex='`^(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;/~\+#]*[\w\-\@?^=%&amp;/~\+#])?$`i';
		if (preg_match($regex, $url) == 1 ) return true;
		else return false;
	}
		
	static public function verifDatabaseName ($database)
	{
		$regex='`^[a-z0-9_]{1,16}$`';
		if (preg_match($regex, $database) == 1 ) return true;
		else return false;
	}
		
	static public function verifHost ($host)
	{
		$regex='`^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$`';
		if (preg_match($regex, $host) == 1 ) return true;
		else return false;
	}	
	
	static public function verifUnixPath ($dir)
	{
		//$regex='`^((?:\/[a-zA-Z0-9]+(?:_[a-zA-Z0-9]+)*(?:\-[a-zA-Z0-9]+)*)+)$`';
		$regex='`^\/{1}([a-z0-9_-]*\.?[a-z0-9_-]*\/{1})?$`i';
		if (preg_match($regex, $dir) == 1 ) return true;
		else return false;
	}
	
	static public function verifBoolean ($var)
	{
		$regex='`^([Ff]+(alse)?|[Tt]+(rue)?|0|[\+\-]?1)$`';
		if ( preg_match($regex, $var) == 1) return true;
		return is_bool($var);
	}
	
	static public function verifInteger ($value, $mini=null, $maxi = null)
	{
		// Check if the value is a numeric one
		if ( !is_numeric($value) ) return false;
		// Get the real value
		$value = intval($value);
		// Check with the minimal value if specified
		if ( !is_null($mini) ) {
			if ( $value < $mini ) return false;
		}
		// Check with the maxi mal value if specified
		if ( !is_null($maxi) ) {
			if ( $value > $maxi ) return false;
		}
		// If all is ok, return true;
		return true;
	}
	
	static public function verifPassword( $value )
	{
		$regex='`^[^\'"]{4,15}$`';
		if (preg_match($regex, $value == 1 )) return true;
		else return false;
	}
	
} // End of class

?>