Example #1
0
/**
 * Add new domain alias
 *
 * @return bool TRUE on success, FALSE on failure
 */
function addDomainAlias()
{
    global $mainDmnProps;
    // Basic check
    if (empty($_POST['domain_alias_name'])) {
        set_page_message(tr('You must enter a domain alias name.'), 'error');
        return false;
    }
    $domainAliasName = clean_input(strtolower($_POST['domain_alias_name']));
    // Check for domain alias name syntax
    global $dmnNameValidationErrMsg;
    if (!isValidDomainName($domainAliasName)) {
        set_page_message($dmnNameValidationErrMsg, 'error');
        return false;
    }
    // www is considered as an alias of the domain alias
    while (strpos($domainAliasName, 'www.') !== false) {
        $domainAliasName = substr($domainAliasName, 4);
    }
    // Check for domain alias existence
    if (imscp_domain_exists($domainAliasName, $_SESSION['user_created_by'])) {
        set_page_message(tr('Domain %s is unavailable.', "<strong>{$domainAliasName}</strong>"), 'error');
        return false;
    }
    $domainAliasNameAscii = encode_idna($domainAliasName);
    // Set default mount point
    $mountPoint = "/{$domainAliasNameAscii}";
    // Check for shared mount point option
    if (isset($_POST['shared_mount_point']) && $_POST['shared_mount_point'] == 'yes') {
        // We are safe here
        if (!isset($_POST['shared_mount_point_domain'])) {
            showBadRequestErrorPage();
        }
        $sharedMountPointDomain = clean_input($_POST['shared_mount_point_domain']);
        $domainList = _client_getDomainsList();
        // Get shared mount point
        foreach ($domainList as $domain) {
            if ($domain['name'] == $sharedMountPointDomain) {
                $mountPoint = $domain['mount_point'];
            }
        }
    }
    // Check for URL forwarding option
    $forwardUrl = 'no';
    if (isset($_POST['url_forwarding']) && $_POST['url_forwarding'] == 'yes') {
        if (!isset($_POST['forward_url_scheme']) || isset($_POST['forward_url'])) {
            showBadRequestErrorPage();
        }
        $forwardUrl = clean_input($_POST['forward_url_scheme']) . clean_input($_POST['forward_url']);
        try {
            try {
                $uri = iMSCP_Uri_Redirect::fromString($forwardUrl);
            } catch (Zend_Uri_Exception $e) {
                throw new iMSCP_Exception(tr('Forward URL %s is not valid.', "<strong>{$forwardUrl}</strong>"));
            }
            $uri->setHost(encode_idna($uri->getHost()));
            if ($uri->getHost() == $domainAliasNameAscii && $uri->getPath() == '/') {
                throw new iMSCP_Exception(tr('Forward URL %s is not valid.', "<strong>{$forwardUrl}</strong>") . ' ' . tr('Domain alias %s cannot be forwarded on itself.', "<strong>{$domainAliasName}</strong>"));
            }
            $forwardUrl = $uri->getUri();
        } catch (Exception $e) {
            set_page_message($e->getMessage(), 'error');
            return false;
        }
    }
    $isSuUser = isset($_SESSION['logged_from_type']);
    # See http://youtrack.i-mscp.net/issue/IP-1486
    $userEmail = isset($_SESSION['user_email']) ? $_SESSION['user_email'] : '';
    $db = iMSCP_Database::getInstance();
    try {
        $db->beginTransaction();
        iMSCP_Events_Aggregator::getInstance()->dispatch(iMSCP_Events::onBeforeAddDomainAlias, array('domainId' => $mainDmnProps['domain_id'], 'domainAliasName' => $domainAliasNameAscii));
        exec_query('
                INSERT INTO domain_aliasses (
                    domain_id, alias_name, alias_mount, alias_status, alias_ip_id, url_forward
                ) VALUES (
                    ?, ?, ?, ?, ?, ?
                )
            ', array($mainDmnProps['domain_id'], $domainAliasNameAscii, $mountPoint, $isSuUser ? 'toadd' : 'ordered', $mainDmnProps['domain_ip_id'], $forwardUrl));
        $id = $db->insertId();
        // Create the phpini entry for that domain alias
        $phpini = iMSCP_PHPini::getInstance();
        $phpini->loadResellerPermissions($_SESSION['user_created_by']);
        // Load reseller PHP permissions
        $phpini->loadClientPermissions($_SESSION['user_id']);
        // Load client PHP permissions
        $phpini->loadDomainIni($_SESSION['user_id'], $mainDmnProps['domain_id'], 'dmn');
        // Load main domain PHP configuration options
        $phpini->saveDomainIni($_SESSION['user_id'], $id, 'als');
        if ($isSuUser) {
            $cfg = iMSCP_Registry::get('config');
            if ($cfg['CREATE_DEFAULT_EMAIL_ADDRESSES'] && $userEmail !== '') {
                client_mail_add_default_accounts($mainDmnProps['domain_id'], $userEmail, $domainAliasNameAscii, 'alias', $id);
            }
        }
        $db->commit();
        iMSCP_Events_Aggregator::getInstance()->dispatch(iMSCP_Events::onAfterAddDomainAlias, array('domainId' => $mainDmnProps['domain_id'], 'domainAliasName' => $domainAliasNameAscii, 'domainAliasId' => $id));
        if ($isSuUser) {
            send_request();
            write_log(sprintf('A new `%s` domain alias has been created by: %s', $domainAliasName, $_SESSION['user_logged']), E_USER_NOTICE);
            set_page_message(tr('Domain alias successfully created.'), 'success');
        } else {
            send_alias_order_email($domainAliasName);
            write_log(sprintf('A new `%s` domain alias has been ordered by: %s', $domainAliasName, decode_idna($_SESSION['user_logged'])), E_USER_NOTICE);
            set_page_message(tr('Domain alias successfully ordered.'), 'success');
        }
    } catch (iMSCP_Exception_Database $e) {
        $db->rollBack();
        write_log(sprintf('System was unable to create the `%s` domain alias: %s', $domainAliasName, $e->getMessage()), E_USER_ERROR);
        set_page_message(tr('Could not create domain alias. An unexpected error occurred.'), 'error');
        return false;
    }
    return true;
}
Example #2
0
/**
 *
 * @global <type> $cr_user_id
 * @global <type> $alias_name
 * @global <type> $domain_ip
 * @global <type> $forward
 * @global <type> $forward_prefix
 * @global <type> $mount_point
 * @global <type> $validation_err_msg
 * @param <type> $err_al
 * @return <type>
 */
function add_domain_alias(&$err_al)
{
    global $cr_user_id, $alias_name, $domain_ip, $forward, $forward_prefix, $mount_point, $validation_err_msg;
    $cfg = EasySCP_Registry::get('Config');
    $sql = EasySCP_Registry::get('Db');
    $cr_user_id = get_user_domain_id($_SESSION['user_id']);
    $alias_name = strtolower($_POST['ndomain_name']);
    //	$mount_point = array_encode_idna(strtolower($_POST['ndomain_mpoint']), true);
    if ($_POST['status'] == 1) {
        $forward = encode_idna(strtolower(clean_input($_POST['forward'])));
        $forward_prefix = clean_input($_POST['forward_prefix']);
    } else {
        $forward = 'no';
        $forward_prefix = '';
    }
    $query = "\n\t\tSELECT\n\t\t\t`domain_ip_id`\n\t\tFROM\n\t\t\t`domain`\n\t\tWHERE\n\t\t\t`domain_id` = ?\n\t";
    $rs = exec_query($sql, $query, $cr_user_id);
    $domain_ip = $rs->fields['domain_ip_id'];
    // First check if input string is a valid domain names
    if (!validates_dname($alias_name)) {
        $err_al = $validation_err_msg;
        return;
    }
    // Should be perfomed after domain names syntax validation now
    $alias_name = encode_idna($alias_name);
    if (easyscp_domain_exists($alias_name, 0)) {
        $err_al = tr('Domain with that name already exists on the system!');
        //	} else if (!validates_mpoint($mount_point) && $mount_point != '/') {
        //		$err_al = tr("Incorrect mount point syntax");
    } else {
        if ($alias_name == $cfg->BASE_SERVER_VHOST) {
            $err_al = tr('Master domain cannot be used!');
        } else {
            if ($_POST['status'] == 1) {
                $aurl = @parse_url($forward_prefix . decode_idna($forward));
                if ($aurl === false) {
                    $err_al = tr("Wrong address in forward URL!");
                } else {
                    $domain = $aurl['host'];
                    if (substr_count($domain, '.') <= 2) {
                        $ret = validates_dname($domain);
                    } else {
                        $ret = validates_dname($domain, true);
                    }
                    if (!$ret) {
                        $err_al = tr("Wrong domain part in forward URL!");
                    } else {
                        $domain = encode_idna($aurl['host']);
                        $forward = $aurl['scheme'] . '://';
                        if (isset($aurl['user'])) {
                            $forward .= $aurl['user'] . (isset($aurl['pass']) ? ':' . $aurl['pass'] : '') . '@';
                        }
                        $forward .= $domain;
                        if (isset($aurl['port'])) {
                            $forward .= ':' . $aurl['port'];
                        }
                        if (isset($aurl['path'])) {
                            $forward .= $aurl['path'];
                        } else {
                            $forward .= '/';
                        }
                        if (isset($aurl['query'])) {
                            $forward .= '?' . $aurl['query'];
                        }
                        if (isset($aurl['fragment'])) {
                            $forward .= '#' . $aurl['fragment'];
                        }
                    }
                }
            } else {
                $query = "\n\t\t\tSELECT\n\t\t\t\t`domain_id`\n\t\t\tFROM\n\t\t\t\t`domain_aliasses`\n\t\t\tWHERE\n\t\t\t\t`alias_name` = ?\n\t\t;";
                $res = exec_query($sql, $query, $alias_name);
                $query = "\n\t\t\tSELECT\n\t\t\t\t`domain_id`\n\t\t\tFROM\n\t\t\t\t`domain`\n\t\t\tWHERE\n\t\t\t\t`domain_name` = ?\n\t\t;";
                $res2 = exec_query($sql, $query, $alias_name);
                if ($res->rowCount() > 0 || $res2->rowCount() > 0) {
                    // we already have domain with this name
                    $err_al = tr("Domain with this name already exist");
                }
                //		$query = "
                //			SELECT
                //				COUNT(`subdomain_id`) AS cnt
                //			FROM
                //				`subdomain`
                //			WHERE
                //					`domain_id` = ?
                //				AND
                //					`subdomain_mount` = ?
                //		;";
                //		$subdomres = exec_query($sql, $query, array($cr_user_id, $mount_point));
                //		$subdomdata = $subdomres->fetchRow();
                //
                //		$query = "
                //			SELECT
                //				COUNT(`subdomain_alias_id`) AS alscnt
                //			FROM
                //				`subdomain_alias`
                //			WHERE
                //					`alias_id`
                //				IN (
                //					SELECT
                //						`alias_id`
                //					FROM
                //						`domain_aliasses`
                //					WHERE
                //						`domain_id` = ?
                //				)
                //				AND
                //					`subdomain_alias_mount` = ?
                //		;";
                //		$alssubdomres = exec_query($sql, $query, array($cr_user_id, $mount_point));
                //		$alssubdomdata = $alssubdomres->fetchRow();
                //
                //		if ($subdomdata['cnt'] > 0 || $alssubdomdata['alscnt'] > 0) {
                //			$err_al = tr("There is a subdomain with the same mount point!");
                //		}
            }
        }
    }
    if ('_off_' !== $err_al) {
        return;
    }
    // Begin add new alias domain
    $status = $cfg->ITEM_ORDERED_STATUS;
    $query = "\n\t\tINSERT INTO\n\t\t\t`domain_aliasses` (\n\t\t\t\t`domain_id`, `alias_name`, `alias_mount`, `status`,\n\t\t\t\t`alias_ip_id`, `url_forward`\n\t\t\t)\n\t\tVALUES\n\t\t\t(?, ?, ?, ?, ?, ?)\n\t;";
    exec_query($sql, $query, array($cr_user_id, $alias_name, $mount_point, $status, $domain_ip, $forward));
    $dmn_id = $sql->insertId();
    AddDefaultDNSEntries(0, $dmn_id, $alias_name, $domain_ip);
    update_reseller_c_props(get_reseller_id($cr_user_id));
    $admin_login = $_SESSION['user_logged'];
    if ($status == $cfg->ITEM_ORDERED_STATUS) {
        // notify the reseller:
        send_alias_order_email($alias_name);
        write_log("{$admin_login}: add domain alias for activation: {$alias_name}.");
        set_page_message(tr('Alias scheduled for activation!'), 'success');
    } else {
        // TODO: Check
        //		send_request('110 DOMAIN alias '.$dmn_id);
        write_log("{$admin_login}: domain alias scheduled for addition: {$alias_name}.");
        set_page_message(tr('Alias scheduled for addition!'), 'success');
    }
    user_goto('domains_manage.php');
}