<?php

//This script copywrite 2006 William "Jump" Whittington.
//It may not be copied, used, altered, or transfered in
//any way other than to be used in code applications
//provided by me in their original form.
define('NO_REGISTER_GLOBALS', 1);
define('SKIP_SESSIONCREATE', 1);
define('NOCOOKIES', 1);
define('DIE_QUIETLY', 1);
define('DIR', dirname(__FILE__));
require_once './global.php';
$vbulletin->input->clean_array_gpc('r', array('email' => TYPE_STR));
if($vbulletin->GPC['email'] != '')
{
	$email = $vbulletin->GPC['email'];
	$verify = (!ini_get('safe_mode')) ? verifyMailAddy($email,$vbulletin->options['lfsemailtrue']) : 0;
	header('Content-Type: text/plain;');
	require_once(DIR . '/includes/functions_user.php');
	switch(true)
	{
		case $verify != 0;
			echo $verify;
			break;
		case is_banned_email($email):
			echo 2;
			break;
		case $vbulletin->options['requireuniqueemail'] &&
			$existingemail = $db->query_first("
				SELECT
					email
				FROM
					" . TABLE_PREFIX . "user
				WHERE
					email = '" . $db->escape_string($email) . "'
				")
			:
			echo 1;
			break;
		default;
			echo 0;
	}
}

function verifyMailAddy($email,$lfsemailtrue=true)
{
	if(!$lfsemailtrue)
	{
	return 0;
	}
	list($user_name, $domain) = split('@',$email);
	if(function_exists('getmxrr'))
	{
		$mail_exchanger = (getmxrr($domain, $mx)) ? $mx[0] : $domain;
	}
	else
	{
		$mx = windoz_getmxrr($domain);
		$mail_exchanger = (!$mx) ? $domain : $mx;
	}
	if($connect = @fsockopen($mail_exchanger, 25, $errno, $errstr, 15))
	{
		$receive = fgets($connect, 128);
		if(substr($receive, 0, 3) == '220')
		{
			fputs($connect, "HELO ".$_SERVER['HTTP_HOST']."\r\n");
			$receive = fgets($connect, 128);
			if(substr($receive, 0, 3) == '554')
			{
				//Server rejected query.
				fputs($connect, "QUIT\r\n");
				fclose($connect);
				return 3;
			}
			fputs($connect, "MAIL FROM: <{$email}>\r\n");
			$mail_from = fgets( $connect, 128 );
			fputs($connect, "RCPT TO: <{$email}>\r\n");
			$mail_to = fgets($connect, 128);
			fputs($connect, "QUIT\r\n");
			fclose($connect);
			if(substr($mail_to, 0, 3) != '250')
			{
				//Server rejected address
				return 5;
			}
		}
		else
		{
			//No response from server
			return 4;
		}
	}
	else
	{
		//Can not connect E-Mail server.
		return 3;
	}
	//Good, but no guarantee that they don't have an accept all email addy.
	return 0;
}

function windoz_getmxrr($domain)
{
	if(!empty($domain))
	{
		$domain = escapeshellcmd($domain); 
		exec("nslookup -type=MX $domain",$receive_data);
		foreach($receive_data as $data_line)
		{
			if(preg_match("#^$domain#", $data_line))
			{
				if(preg_match('#^.*MX preference = (\d{1,10}), mail exchanger = (.*)$#siU', $data_line, $match))
				{
					return $match[2];
				}
			}
		}
		return false;
	}
	return false;
}


?>
