Example #1
0
/**
 * Add new subdomain
 *
 * @return bool TRUE on success, FALSE on failure
 */
function client_addSubdomain()
{
    global $mainDmnProps;
    // Basic check
    if (empty($_POST['subdomain_name'])) {
        set_page_message(tr('You must enter a subdomain name.'), 'error');
        return false;
    }
    if (empty($_POST['domain_name'])) {
        showBadRequestErrorPage();
    }
    // Check for parent domain
    $domainName = clean_input($_POST['domain_name']);
    $domainType = $domainId = null;
    $domainList = _client_getDomainsList();
    foreach ($domainList as $domain) {
        if (($domain['type'] == 'dmn' || $domain['type'] == 'als') && $domain['name'] == $domainName) {
            $domainType = $domain['type'];
            $domainId = $domain['id'];
        }
    }
    if (null === $domainType) {
        showBadRequestErrorPage();
    }
    $subLabel = clean_input(strtolower($_POST['subdomain_name']));
    if ($subLabel == 'www' || strpos($subLabel, 'www.') !== false) {
        set_page_message(tr('%s is not allowed as subdomain label.', "<strong>www</strong>"), 'error');
        return false;
    }
    $subdomainName = $subLabel . '.' . $domainName;
    // Check for subdomain syntax
    if (!isValidDomainName($subdomainName)) {
        set_page_message(tr('Subdomain name is not valid.'), 'error');
        return false;
    }
    // Ensure that this subdomain doesn't already exists as domain or domain alias
    $stmt = exec_query('
        SELECT domain_id FROM domain WHERE domain_name = :subdomain_name
        UNION ALL
        SELECT alias_id FROM domain_aliasses WHERE alias_name = :subdomain_name', array('subdomain_name' => $subdomainName));
    if ($stmt->rowCount()) {
        set_page_message(tr('Subdomain %s is unavailable.', "<strong>{$subdomainName}</strong>"), 'error');
        return false;
    }
    $subLabelAscii = clean_input(encode_idna(strtolower($_POST['subdomain_name'])));
    $subdomainNameAscii = encode_idna($subdomainName);
    // Check for sudomain existence
    foreach ($domainList as $domain) {
        if ($domain['name'] == $subdomainNameAscii) {
            set_page_message(tr('Subdomain %s already exist.', "<strong>{$subdomainName}</strong>"), 'error');
            return false;
        }
    }
    // Set default mount point
    if ($domainType == 'dmn') {
        if (in_array($subLabelAscii, array('backups', 'cgi-bin', 'errors', 'logs', 'phptmp'))) {
            $mountPoint = "/sub_{$subLabelAscii}";
        } else {
            $mountPoint = "/{$subLabelAscii}";
        }
    } else {
        if (in_array($subLabelAscii, array('cgi-bin', 'phptmp'))) {
            $mountPoint = "/{$domainName}/sub_{$subLabelAscii}";
        } else {
            $mountPoint = "/{$domainName}/{$subLabelAscii}";
        }
    }
    // 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']);
        // 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') {
        // We are safe here
        if (isset($_POST['forward_url_scheme']) && isset($_POST['forward_url'])) {
            $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() == $subdomainNameAscii && $uri->getPath() == '/') {
                    throw new iMSCP_Exception(tr('Forward URL %s is not valid.', "<strong>{$forwardUrl}</strong>") . ' ' . tr('Subdomain %s cannot be forwarded on itself.', "<strong>{$subdomainName}</strong>"));
                }
                $forwardUrl = $uri->getUri();
            } catch (Exception $e) {
                set_page_message($e->getMessage(), 'error');
                return false;
            }
        } else {
            showBadRequestErrorPage();
        }
    }
    $db = iMSCP_Database::getInstance();
    try {
        $db->beginTransaction();
        iMSCP_Events_Aggregator::getInstance()->dispatch(iMSCP_Events::onBeforeAddSubdomain, array('subdomainName' => $subdomainName, 'subdomainType' => $domainType, 'parentDomainId' => $domainId, 'mountPoint' => $mountPoint, 'forwardUrl' => $forwardUrl, 'customerId' => $_SESSION['user_id']));
        if ($domainType == 'als') {
            $query = "\n                INSERT INTO subdomain_alias (\n                    alias_id, subdomain_alias_name, subdomain_alias_mount, subdomain_alias_url_forward,\n                    subdomain_alias_status\n                ) VALUES (\n                    ?, ?, ?, ?, ?\n                )\n            ";
        } else {
            $query = "\n                INSERT INTO subdomain (\n                    domain_id, subdomain_name, subdomain_mount, subdomain_url_forward, subdomain_status\n                ) VALUES (\n                    ?, ?, ?, ?, ?\n                )\n            ";
        }
        exec_query($query, array($domainId, $subLabelAscii, $mountPoint, $forwardUrl, 'toadd'));
        $subdomainId = $db->insertId();
        // Create the phpini entry for that subdomain
        $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'], $subdomainId, $domainType == 'dmn' ? 'sub' : 'subals');
        iMSCP_Events_Aggregator::getInstance()->dispatch(iMSCP_Events::onAfterAddSubdomain, array('subdomainName' => $subdomainName, 'subdomainType' => $domainType, 'parentDomainId' => $domainId, 'mountPoint' => $mountPoint, 'forwardUrl' => $forwardUrl, 'customerId' => $_SESSION['user_id'], 'subdomainId' => $subdomainId));
        $db->commit();
        send_request();
        write_log(sprintf('A new `%s` subdomain has been created by %s', $subdomainName, decode_idna($_SESSION['user_logged'])), E_USER_NOTICE);
        return true;
    } catch (iMSCP_Exception_Database $e) {
        $db->rollBack();
        write_log(sprintf('System was unable to create the `%s` subdomain: %s', $subdomainName, $e->getMessage()), E_USER_ERROR);
        set_page_message('Could not create subdomain. An unexpected error occurred.', 'error');
        return false;
    }
}
Example #2
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 #3
0
/**
 * Generate page
 *
 * @param iMSCP_pTemplate $tpl
 */
function client_generatePage($tpl)
{
    $mainDmnProps = get_domain_default_props($_SESSION['user_id']);
    $stmt = exec_query('SELECT SUM(`quota`) AS `quota` FROM `mail_users` WHERE `domain_id` = ? AND `quota` IS NOT NULL', $mainDmnProps['domain_id']);
    $quota = $stmt->fields['quota'];
    if ($mainDmnProps['mail_quota'] != '0' && $quota >= $mainDmnProps['mail_quota']) {
        set_page_message(tr('You cannot add new email account. You have already assigned all your email quota to other mailboxes. Please first, review your quota assignments.'), 'warning');
        $tpl->assign('MAIL_ACCOUNT', '');
    } else {
        /** @var iMSCP_Config_Handler_File $cfg */
        $cfg = iMSCP_Registry::get('config');
        $checked = $cfg->HTML_CHECKED;
        $selected = $cfg->HTML_SELECTED;
        $mailType = isset($_POST['account_type']) && in_array($_POST['account_type'], array('1', '2', '3')) ? $_POST['account_type'] : '1';
        $tpl->assign(array('USERNAME' => isset($_POST['username']) ? tohtml($_POST['username']) : '', 'NORMAL_CHECKED' => $mailType == '1' ? $checked : '', 'FORWARD_CHECKED' => $mailType == '2' ? $checked : '', 'NORMAL_FORWARD_CHECKED' => $mailType == '3' ? $checked : '', 'TR_QUOTA' => $mainDmnProps['mail_quota'] == '0' ? tr('Quota in MiB (0 for unlimited)') : tr('Quota in MiB (Max: %s)', bytesHuman($mainDmnProps['mail_quota'] - $quota, 'MiB')), 'QUOTA' => isset($_POST['quota']) ? tohtml($_POST['quota']) : '', 'FORWARD_LIST' => isset($_POST['forward_list']) ? tohtml($_POST['forward_list']) : ''));
        foreach (_client_getDomainsList() as $domain) {
            $tpl->assign(array('DOMAIN_NAME' => tohtml($domain['name']), 'DOMAIN_NAME_UNICODE' => tohtml(decode_idna($domain['name'])), 'DOMAIN_NAME_SELECTED' => isset($_POST['domain_name']) && $_POST['domain_name'] == $domain['name'] ? $selected : ''));
            $tpl->parse('DOMAIN_NAME_ITEM', '.domain_name_item');
        }
    }
}