示例#1
0
/**
 * Validates a service port and sets an appropriate message on error.
 *
 * @param string $name Service name
 * @param string $ip Ip address
 * @param int $port Port
 * @param string $protocol Protocle
 * @param bool $show Tell whether or not service must be show on status page
 * @param string $index Item index on update, empty value otherwise
 * @return bool TRUE if valid, FALSE otherwise
 */
function admin_validatesService($name, $ip, $port, $protocol, $show, $index = '')
{
    /** @var $dbConfig iMSCP_Config_Handler_Db */
    $dbConfig = iMSCP_Registry::get('dbConfig');
    // Get a reference to the array that contain all error fields ids
    $errorFieldsIds =& iMSCP_Registry::get('errorFieldsIds');
    $dbServiceName = "PORT_{$name}";
    $ip = $ip == 'localhost' ? '127.0.0.1' : $ip;
    // Check for service name syntax
    if (!is_basicString($name)) {
        set_page_message(tr("Error with '{$name}': Only letters, numbers, dash and underscore are allowed for services names."), 'error');
        $errorFieldsIds[] = "name{$index}";
    }
    // Check for IP syntax
    if (filter_var($ip, FILTER_VALIDATE_IP) === false) {
        set_page_message(tr(' Wrong IP address.'), 'error');
        $errorFieldsIds[] = "ip{$index}";
    }
    // Check for port syntax
    if (!is_number($port) || $port < 1 || $port > 65535) {
        set_page_message(tr('Only numbers in range from 0 to 65535 are allowed.'), 'error');
        $errorFieldsIds[] = "port{$index}";
    }
    // Check for service port existences
    if (!is_int($index) && isset($dbConfig[$dbServiceName])) {
        set_page_message(tr('Service name already exists.'), 'error');
        $errorFieldsIds[] = "name{$index}";
    }
    // Check for protocol and show option
    if ($protocol != 'tcp' && $protocol != 'udp' || $show != '0' && $show != '1') {
        showBadRequestErrorPage();
    }
    return Zend_Session::namespaceIsset('pageMessages') ? false : true;
}
示例#2
0
/**
 * Validates a service port and sets an appropriate message on error
 *
 * @since 1.0.7
 * @param string $name Service port name
 * @param string $ip Ip address
 * @param int $port Service port
 * @param string $proto Service port protocol
 * @param int $show
 * @param int $index Item index on uppdate, empty value otherwise
 * @return TRUE if valid, FALSE otherwise
 */
function validates_service($name, $ip, $port, $proto, $show, $index = '')
{
    // Get a reference to the EasySCP_ConfigHandler_Db instance
    $db_cfg = EasySCP_Registry::get('Db_Config');
    // Get a reference to the array that contain all errors messages
    $messages =& EasySCP_Registry::get('Page_Messages');
    // Get a reference to the array that contain all error fields ids
    $error_fields_ids =& EasySCP_Registry::get('Error_Fields_Ids');
    // Accounting for errors messages
    static $msg_cnt = 0;
    $db_sname = "PORT_{$name}";
    $ip = $ip == 'localhost' ? '127.0.0.1' : $ip;
    if (!is_basicString($name)) {
        $messages[] = tr('Only letters, numbers, dash and underscore are allowed for services names!');
        $error_fields_ids[] = "name{$index}";
    }
    if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
        $messages[] = tr('Wrong IP number!');
        $error_fields_ids[] = "ip{$index}";
    }
    if (!is_number($port) || $port <= 0) {
        $messages[] = tr('Only positive numbers are allowed for services ports!');
        $error_fields_ids[] = "port{$index}";
    }
    if (!is_int($index) && isset($db_cfg->{$db_sname})) {
        $messages[] = tr('Service port with same name already exists!');
        $error_fields_ids[] = "name{$index}";
    }
    if ($proto != 'tcp' && $proto != 'udp') {
        $messages[] = tr('Unallowed protocol!');
        $error_fields_ids[] = "port_type{$index}";
    }
    if ($show != '0' && $show != '1') {
        $messages[] = tr('Incorrect value for show entry!');
        $error_fields_ids[] = "show_val{$index}";
    }
    return ($msg_cnt = count($messages) != $msg_cnt) ? false : true;
}