<?php
/*
  $Id: validations.php,v 1.3 02.09.2009 16:02:34 oscim Exp $
// kate: replace-trailing-space-save true; space-indent on; indent-width 2; mixedindent off; indent-mode cstyle; word-wrap-marker-color #41C2A4
+-----------------------------------------------------------------------+
|                  osCSS Open Source E-commerce                         |
+-----------------------------------------------------------------------+
// Arguments   : email   email address to be checked
//
// Return      : true  - valid email address
//               false - invalid email address
//
// Description : function for validating email address that conforms to RFC 822 specs
//
//               This function is converted from a JavaScript written by
//               Sandeep V. Tamhankar (stamhankar@hotmail.com). The original JavaScript
//               is available at http://javascript.internet.com
//
// Sample Valid Addresses:
//
//    first.last@host.com
//    firstlast@host.to
//    "first last"@host.com
//    "first@last"@host.com
//    first-last@host.com
//    first.last@[123.123.123.123]
//
// Invalid Addresses:
//
//    first last@host.com

	ENCODE UTF-8
*/

/**
* @package oscss-Rv
* @date 02.09.2009 ACCEPT RV_COMPATIBILITY a ON
    modifie pour RV_COMPATIBILITY

  ! NOTE: Require le fichier .tld.txt dans le repertoire include
*/
function tep_validate_email_inclus($email) {
  $valid_address = true;

  $mail_pat = '^(.+)@(.+)$';
  $valid_chars = "[^] \(\)<>@,;:\.\\\"\[]";
  $atom = "$valid_chars+";
  $quoted_user='(\"[^\"]*\")';
  $word = "($atom|$quoted_user)";
  $user_pat = "^$word(\.$word)*$";
  $ip_domain_pat='^\[([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\]$';
  $domain_pat = "^$atom(\.$atom)*$";

  if (preg_match('/'.$mail_pat.'/i', $email, $components)) {
    $user = $components[1];
    $domain = $components[2];
    // validate user
    if (preg_match('/'.$user_pat.'/i', $user)) {
      // validate domain
      if (preg_match('/'.$ip_domain_pat.'/i', $domain, $ip_components)) {
        // this is an IP address
        for ($i=1;$i<=4;$i++) {
          if ($ip_components[$i] > 255) {
            $valid_address = false;
            break;
          }
        }
      }
      else {
        // Domain is a name, not an IP
        if (preg_match('/'.$domain_pat.'/i', $domain)) {
          /* domain name seems valid, but now make sure that it ends in a valid TLD or ccTLD
              and that there's a hostname preceding the domain or country. */
          $domain_components = explode(".", $domain);
          // Make sure there's a host name preceding the domain.
          if (sizeof($domain_components) < 2) {
            $valid_address = false;
          } else {
            $top_level_domain = strtolower($domain_components[sizeof($domain_components)-1]);
            // Allow all 2-letter TLDs (ccTLDs)
            if (preg_match('/^[a-z][a-z]$/i', $top_level_domain) != 1) {
              $tld_pattern = '';
              // Get authorized TLDs from text file
              $tlds = file(DIR_WS_INCLUDES . 'tld.txt');
              while (list(,$line) = each($tlds)) {
                // Get rid of comments
                $words = explode('#', $line);
                $tld = trim($words[0]);
                // TLDs should be 3 letters or more
                if (preg_match('/^[a-z]{3,}$/i', $tld) == 1) {
                  $tld_pattern .= '^' . $tld . '$|';
                }
              }
              // Remove last '|'
              $tld_pattern = substr($tld_pattern, 0, -1);
              if (preg_match("/$tld_pattern/i", $top_level_domain) == 0) {
                  $valid_address = false;
              }
            }
          }
        }
        else {
          $valid_address = false;
        }
      }
    }
    else {
      $valid_address = false;
    }
  }
  else {
    $valid_address = false;
  }
  if ($valid_address && _cst_bool('ENTRY_EMAIL_ADDRESS_CHECK')) {
    if (!checkdnsrr($domain, "MX") && !checkdnsrr($domain, "A")) {
      $valid_address = false;
    }
  }
  return $valid_address;
}
?>