Jump to content



Latest News: (loading..)

Issue Information

  • #000382

  • 0 - None Assigned

  • New

  • 2.3.1

  • -

Issue Confirmations

  • Yes (0)No (0)
Photo

tep_validate_email correct function

Posted by faaliyet on 21 October 2011 - 07:34 AM

Dear osCommerce moderators;
I can not add the preg_match function correctly because forum says:


Errors found

You have posted a message with more emoticons than this community allows. Please reduce the number of emoticons you've added to the message

That sucks!
AND yes , i tried to post that as [code...
(Again bug, because i'm trying to post that in CODE tag)

So you can find preg_match at:
http://svn.php.net/viewvc/php/php-src/trunk/ext/filter/logical_filters.c?view=markup

and under the function :
void php_filter_validate_email(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
of part:
const char regexp[] = ..................


- Open the file catalog/includes/functions/validations.php

Check for this:
  function tep_validate_email($email) {
	$email = trim($email);
 
	if ( strlen($email) > 255 ) {
	  $valid_address = false;
	} elseif ( function_exists('filter_var') && defined('FILTER_VALIDATE_EMAIL') ) {
	 $valid_address = (bool)filter_var($email, FILTER_VALIDATE_EMAIL);
	} else {
	  if ( substr_count( $email, '@' ) > 1 ) {
		$valid_address = false;
	  }

Here $valid_address can be true in the next step if it's true;  Also the filter FILTER_VALIDATE_EMAIL should be after the  if ( substr_count( $email, '@' ) > 1 ) {
because user can send mail with '   "myaddress@domain.com"@domain.com   ' address.
Also the filter FILTER_VALIDATE_EMAIL  does not work correctly in PHP5.2, so I added code to compare php version and that's jumping to preg_match function if php version is lower then 5.3.

	  if ( preg_match("/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i", $email) ) {
		$valid_address = true;
	  } else {
		$valid_address = false;
	  }


Now here is the correct validation function.

  function tep_validate_email($email) {
	if(is_scalar($email) !== true)
	  return false;
 
	$email = trim($email);
	$valid_address = false;
 
	if ( strlen($email) > 255 ) {
	  return false;
 
	} else {
	  if ( substr_count( $email, '@' ) > 1 ) {
		//"email@domain.com"@domain.com: fix for lastest version of FILTER_VALIDATE_EMAIL
		return false; //return , becase $valid_address can be true in next check
	  }
	  if ( function_exists('filter_var') && defined('FILTER_VALIDATE_EMAIL') && version_compare(PHP_VERSION, '5.3', '>=' ) {
		$valid_address = (bool)filter_var($email, FILTER_VALIDATE_EMAIL);
		if ($valid_addres === false) return false;
	  } else {
		//https://bugs.php.net/bug.php?id=49576
		//lastest version of FILTER_VALIDATE_EMAIL
		//const char regexp[] =	http://svn.php.net/viewvc/php/php-src/trunk/ext/filter/logical_filters.c?view=markup
		if ( preg_match( , (string)$email) ) {
			$valid_address = true;
		} else {
			return false;
		}
	  }
	}
 
	if ($valid_address === true && ENTRY_EMAIL_ADDRESS_CHECK == 'true') {
	  $domain = explode('@', $email);
 
	  if ( !checkdnsrr($domain[1], "MX") && !checkdnsrr($domain[1], "A") ) {
		return false;
	  }
	}
 
	return $valid_address;
  }