View file upload/library/XenForo/Helper/Email.php

File size: 2.36Kb
<?php

abstract class XenForo_Helper_Email
{
	/**
	 * Banned email cache for default banned emails option.
	 *
	 * @var array|null Null when not set up
	 */
	protected static $_bannedEmailCache = null;

	/**
	 * Determines if the specified email is banned. List of banned emails
	 * is simply an array of strings with * as wildcards.
	 *
	 * @param string $email
	 * @param array|null $bannedEmails List of banned emails; if null, uses the default value
	 *
	 * @return boolean
	 */
	public static function isEmailBanned($email, array $bannedEmails = null)
	{
		if ($bannedEmails === null)
		{
			if (self::$_bannedEmailCache === null)
			{
				$bannedEmails = XenForo_Model::create('XenForo_Model_DataRegistry')->get('bannedEmails');
				if (!is_array($bannedEmails))
				{
					$bannedEmails = XenForo_Model::create('XenForo_Model_Banning')->rebuildBannedEmailCache();
				}

				self::$_bannedEmailCache = $bannedEmails;
			}
			else
			{
				$bannedEmails = self::$_bannedEmailCache;
			}
		}

		foreach ($bannedEmails AS $bannedEmail)
		{
			$bannedEmail = str_replace('\\*', '(.*)', preg_quote($bannedEmail, '/'));
			if (preg_match('/^' . $bannedEmail . '$/i', $email))
			{
				return true;
			}
		}

		return false;
	}

	/**
	 * Determine if an email is valid (does not validate TLDs).
	 *
	 * @param string $email
	 *
	 * @return bool
	 */
	public static function isEmailValid($email)
	{
		if (preg_match('/["\'\s\\\\]/', $email))
		{
			// rejecting some odd emails as a sanity check
			return false;
		}

		$validator = new Zend_Validate_EmailAddress();
		$validator->getHostnameValidator()
			->setValidateTld(false)
			->setValidateIdn(false);
		return $validator->isValid($email);
	}

	public static function emailHasTypo($email)
	{
		// This is a very basic function and is really just trying to catch simple typos.
		// Most significantly, gamil.com since this can trigger an SFS action unexpectedly.
		$matches = array(
			'gamil.com',
			'gmial.com',
			'gmail.cmo',
			'gmail.co',
			'gmail.cm',
			'gnail.com',
			'hotnail.com',
			'hotmail.cmo',
			'hotmail.co',
			'hotmail.cm',
			'yahooo.com',
			'yaho.com',
			'yahoo.co',
			'yahoo.cmo',
			'yahoo.cm'
		);

		$regex = implode('|', array_map('preg_quote', $matches));
		return preg_match('/@(' . $regex . ')$/i', $email) ? true : false;
	}
}