Esempio n. 1
0
/**
 * Get data from previous step
 *
 * @return bool
 */
function getPreviousStepData()
{
    global $hpId, $dmnName, $adminName, $dmnExpire;
    if (isset($_SESSION['dmn_expire'])) {
        $dmnExpire = $_SESSION['dmn_expire'];
    }
    if (isset($_SESSION['step_one'])) {
        $stepTwo = $_SESSION['dmn_name'] . ';' . $_SESSION['dmn_tpl'];
        $hpId = $_SESSION['dmn_tpl'];
        unset($_SESSION['dmn_name']);
        unset($_SESSION['dmn_tpl']);
        unset($_SESSION['chtpl']);
        unset($_SESSION['step_one']);
    } elseif (isset($_SESSION['step_two_data'])) {
        $stepTwo = $_SESSION['step_two_data'];
        unset($_SESSION['step_two_data']);
    } elseif (isset($_SESSION['local_data'])) {
        $stepTwo = $_SESSION['local_data'];
        unset($_SESSION['local_data']);
    } else {
        $stepTwo = "'';0";
    }
    list($dmnName, $hpId) = explode(';', $stepTwo);
    $adminName = $dmnName;
    if (!isValidDomainName($dmnName) || $hpId == '') {
        return false;
    }
    return true;
}
Esempio n. 2
0
/**
 * Validate the given DNS MX record
 *
 * @access private
 * @param string $type MX type
 * @param int $priority MX preference
 * @param string $host Mail host
 * @param $verifiedData
 * @return bool TRUE if the given MX DNS record is valid, FALSE otherwise
 */
function _client_validateDnsMxRecord($type, $priority, $host, $verifiedData)
{
    $validator = iMSCP_Validate::getInstance();
    $mxTypes = array('domain', 'wildcard');
    if (customerHasFeature('mail')) {
        $mxTypes[] = 'filter';
    }
    // Should never occurs since we are using options stack in HTML form
    if (!$validator->assertContains($type, $mxTypes, true, tr('Invalid MX type.'))) {
        if ($type == 'filter') {
            showBadRequestErrorPage();
        } else {
            set_page_message($validator->getLastValidationMessages(), 'error');
            return false;
        }
    }
    // Should never occurs since we are using options stack in HTML form
    if (!$validator->assertContains($priority, range(5, 50, 5), false, tr('Invalid MX priority.'))) {
        set_page_message($validator->getLastValidationMessages(), 'error');
        return false;
    }
    // Mail host must not be equal to the domain for which it's added
    if (!$validator->assertNotEquals($verifiedData['item_name'], encode_idna($host), tr('Mailhost must not be equal to the domain name for which you add it.'))) {
        set_page_message($validator->getLastValidationMessages(), 'error');
        return false;
    }
    if ($host == '') {
        set_page_message(tr('Mailhost cannot be empty.'), 'error');
        return false;
    } elseif (!isValidDomainName($host)) {
        set_page_message(tr("Mailhost %s is not valid.", "<strong>{$host}</strong>"), 'error');
        return false;
    }
    return true;
}
Esempio n. 3
0
 /**
  * Returns true if and only if the host string passes validation. If no host is passed, then the host contained in
  * the instance variable is used.
  *
  * @param  string $host The HTTP host
  * @return boolean
  * @uses   Zend_Filter
  */
 public function validateHost($host = null)
 {
     if ($host === null) {
         $host = $this->_host;
     }
     // If the host is empty, then it is considered invalid
     if (strlen($host) === 0) {
         return false;
     }
     return isValidDomainName($host);
 }
Esempio n. 4
0
/**
 * Check input data
 *
 * @return void
 */
function reseller_checkData()
{
    $cfg = iMSCP_Registry::get('config');
    if (!isset($_POST['dmn_name']) || $_POST['dmn_name'] === '') {
        set_page_message(tr('Domain name cannot be empty.'), 'error');
        return;
    }
    $dmnName = clean_input($_POST['dmn_name']);
    global $dmnNameValidationErrMsg;
    if (!isValidDomainName($dmnName)) {
        set_page_message($dmnNameValidationErrMsg, 'error');
        return;
    }
    // www is considered as an alias of the domain
    while (strpos($dmnName, 'www.') !== false) {
        $dmnName = substr($dmnName, 4);
    }
    $asciiDmnName = encode_idna($dmnName);
    if (imscp_domain_exists($asciiDmnName, $_SESSION['user_id']) || $asciiDmnName == $cfg['BASE_SERVER_VHOST']) {
        set_page_message(tr('Domain %s is unavailable.', "<strong>{$dmnName}</strong>"), 'error');
        return;
    }
    if ((!isset($_POST['datepicker']) || $_POST['datepicker'] === '') && !isset($_POST['never_expire'])) {
        set_page_message(tr('Domain expiration date must be filled.'), 'error');
        return;
    }
    $dmnExpire = isset($_POST['datepicker']) ? @strtotime(clean_input($_POST['datepicker'])) : 0;
    if ($dmnExpire === false) {
        set_page_message('Invalid expiration date.', 'error');
        return;
    }
    $hpId = isset($_POST['dmn_tpl']) ? clean_input($_POST['dmn_tpl']) : 0;
    $customizeHp = $hpId > 0 && isset($_POST['chtpl']) ? $_POST['chtpl'] : '_no_';
    if ($hpId == 0 || $customizeHp == '_yes_') {
        $_SESSION['dmn_name'] = $asciiDmnName;
        $_SESSION['dmn_expire'] = $dmnExpire;
        $_SESSION['dmn_tpl'] = $hpId;
        $_SESSION['chtpl'] = '_yes_';
        $_SESSION['step_one'] = '_yes_';
        redirectTo('user_add2.php');
    }
    if (reseller_limits_check($_SESSION['user_id'], $hpId)) {
        $_SESSION['dmn_name'] = $asciiDmnName;
        $_SESSION['dmn_expire'] = $dmnExpire;
        $_SESSION['dmn_tpl'] = $hpId;
        $_SESSION['chtpl'] = $customizeHp;
        $_SESSION['step_one'] = '_yes_';
        redirectTo('user_add3.php');
    }
    set_page_message(tr('Hosting plan limits exceed reseller limits.'), 'error');
}
Esempio n. 5
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;
}
Esempio n. 6
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;
    }
}
Esempio n. 7
0
/**
 * Validate CNAME DNS record
 *
 * @param string $cname Cname
 * @param string $domainName Domain name
 * @param string &$errorString Error string
 * @return bool TRUE if cname is valid, FALSE otherwise
 */
function client_validate_CNAME($cname, $domainName, &$errorString)
{
    if (empty($cname) || $cname == '.') {
        $errorString .= tr('CNAME field is empty or invalid.');
        return false;
    }
    if (substr($cname, -1) == '.') {
        $cnameRecord = rtrim($cname, '.');
    } else {
        $cnameRecord = $cname;
    }
    if (strpos($cnameRecord, '.') === false && $cnameRecord != '@') {
        $cnameRecord .= $domainName;
    }
    if ($cnameRecord != '@' && !isValidDomainName($cnameRecord)) {
        $errorString .= tr('Invalid CNAME');
        return false;
    }
    return true;
}