Esempio n. 1
0
function addDomain($domain)
{
    if (!isSiteAdmin()) {
        print json_encode(array('success' => FALSE, 'errors' => array('domain' => 'Permission denied')));
        return;
    }
    if (!$domain) {
        print json_encode(array('success' => FALSE, 'errors' => array('domain' => 'This field is required')));
        return;
    }
    $domain = strtolower($domain);
    if (!validDomain($domain)) {
        print json_encode(array('success' => FALSE, 'errors' => array('domain' => 'Invalid domain')));
        return;
    }
    if (domainExists($domain)) {
        print json_encode(array('success' => FALSE, 'errors' => array('domain' => 'Domain already exists')));
        return;
    }
    $user = $_SESSION['user'];
    if ($domain == $user['domain']) {
        print json_encode(array('success' => FALSE, 'errors' => array('domain' => 'Can not delete your own domain')));
        return;
    }
    $add = array('domain' => $domain);
    beginTransaction();
    $domain_id = db_insert('virtual_domains', $add, 'domain_id');
    if (!$domain_id) {
        cancelTransaction();
        print json_encode(array('success' => FALSE, 'errors' => array('domain' => 'Unknown error')));
        return;
    }
    $transport = array('subdomain' => 'autoreply', 'domain_id' => $domain_id, 'destination' => 'autoreply:', 'active' => 't');
    $transport_id = db_insert('transport_maps', $transport, 'transport_id');
    if (!$transport_id) {
        cancelTransaction();
        print json_encode(array('success' => FALSE, 'errors' => array('domain' => 'Unknown error')));
        return;
    }
    endTransaction();
    print json_encode(array('success' => true));
}
Esempio n. 2
0
	public function validate($for)
	{
		$email = trim($this->_value);
		if ($email == '' && $this->isRequired($for))
		{
			$this->error(t('%n is required'));
			return false;
		}
		else if ($email == '')
		{
			return true;
		}
		else
		{
			$isValid = true;
			$atIndex = strrpos($email, "@");
			if (is_bool($atIndex) && !$atIndex)
			{
				$isValid = false;
			}
			else
			{
				$domain = substr($email, $atIndex+1);
				$local = substr($email, 0, $atIndex);
				$localLen = strlen($local);
				$domainLen = strlen($domain);
				if ($localLen < 1 || $localLen > 64)
				{
					// local part length exceeded
					$isValid = false;
				}
				else if ($domainLen < 1 || $domainLen > 255)
				{
					// domain part length exceeded
					$isValid = false;
				}
				else if ($local[0] == '.' || $local[$localLen-1] == '.')
				{
					// local part starts or ends with '.'
					$isValid = false;
				}
				else if (preg_match('/\\.\\./', $local))
				{
					// local part has two consecutive dots
					$isValid = false;
				}
				else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
				{
					// character not valid in domain part
					$isValid = false;
				}
				else if (preg_match('/\\.\\./', $domain))
				{
					// domain part has two consecutive dots
					$isValid = false;
				}
				else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local)))
				{
					// character not valid in local part unless 
					// local part is quoted
					if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local)))
					{
						$isValid = false;
					}
				}
				if ($isValid && !(domainExists($domain)))
				{
					// domain not found in DNS
					$isValid = false;
				}
			}
			if (!$isValid)
			{
				$this->error(t('%n is not a valid emailadress'));
			}
			return $isValid;
		}
	}