Ejemplo n.º 1
0
function __cat_check_db_config()
{
    global $lang, $users, $config;
    $errors = array();
    $regexp = '/^[^\\x-\\x1F]+$/D';
    // Check if user has entered a database host
    if (!isset($config['database_host']) || $config['database_host'] == '') {
        $errors['installer_database_host'] = $lang->translate('Please enter a database host name');
    } else {
        if (preg_match('~(?:(?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]{0,61})?[a-zA-Z0-9])[.])*(?:[a-zA-Z][-a-zA-Z0-9]{0,61}[a-zA-Z0-9]|[a-zA-Z])[.]?)~', $config['database_host'], $match)) {
            $database_host = $match[0];
        } else {
            $errors['installer_database_host'] = $lang->translate('Invalid database hostname!');
        }
    }
    // check for valid port number
    if (!isset($config['database_port']) || $config['database_port'] == '') {
        $errors['installer_database_port'] = $lang->translate('Please enter a database port');
    } else {
        if (is_numeric($config['database_port'])) {
            $database_port = $config['database_port'];
        } else {
            $errors['installer_database_port'] = $lang->translate('Invalid port number!');
        }
    }
    // Check if user has entered a database username
    if (!isset($config['database_username']) || $config['database_username'] == '') {
        $errors['installer_database_username'] = $lang->translate('Please enter a database username');
    } else {
        if (preg_match($regexp, $config['database_username'], $match)) {
            $database_username = $match[0];
        } else {
            $errors['installer_database_username'] = $lang->translate('Invalid database username!');
        }
    }
    // Check if user has entered a database password
    if (!isset($config['database_password']) || $config['database_password'] == '') {
        $database_password = '';
        if (!isset($config['no_validate_db_password'])) {
            $errors['installer_database_password_empty'] = true;
        }
    } else {
        if (!isset($config['no_validate_db_password'])) {
            if (!$users->validatePassword($config['database_password'], false, true)) {
                $errors['installer_database_password'] = $lang->translate('Invalid database password!') . ' ' . $users->getPasswordError();
            } else {
                $database_password = $users->getLastValidatedPassword();
            }
        } else {
            $database_password = $config['database_password'];
        }
    }
    // Check if user has entered a database name
    if (!isset($config['database_name']) || $config['database_name'] == '') {
        $errors['installer_database_name'] = $lang->translate('Please enter a database name');
    } else {
        // make sure only allowed characters are specified; it is not allowed to
        // have a DB name with digits only!
        if (preg_match('/^[a-z0-9][a-z0-9_-]+$/i', $config['database_name']) && !is_numeric($config['database_name'])) {
            $database_name = $config['database_name'];
        } else {
            // contains invalid characters (only a-z, A-Z, 0-9 and _ allowed to avoid problems with table/field names)
            $errors['installer_database_name'] = $lang->translate('Only characters a-z, A-Z, 0-9, - and _ allowed in database name. Please note that a database name must not be composed of digits only.');
        }
    }
    // table prefix
    if (isset($config['table_prefix']) && $config['table_prefix'] != '' && !preg_match('/^[a-z0-9_]+$/i', $config['table_prefix'])) {
        $errors['installer_table_prefix'] = $lang->translate('Only characters a-z, A-Z, 0-9 and _ allowed in table_prefix.');
    }
    if (!count($errors)) {
        // check database connection
        $connectionParams = array('DB_NAME' => $database_name, 'DB_USERNAME' => $database_username, 'DB_PASSWORD' => $database_password, 'DB_HOST' => $database_host, 'DB_PORT' => $database_port);
        $conn = CAT_Helper_DB::getInstance($connectionParams);
        if (!$conn->check()) {
            $errors['global'] = $lang->translate('Unable to connect to the database! Please check your settings!');
        }
    }
    return $errors;
}
Ejemplo n.º 2
0
 public function __construct()
 {
     self::$obj = CAT_Helper_DB::getInstance();
     return self::$obj;
 }
Ejemplo n.º 3
0
 /**
  * returns a database connection handle
  *
  * This function must be used by all classes, as we plan to replace
  * the database class in later versions!
  *
  * @access public
  * @return object
  **/
 public function db()
 {
     if (!self::$db || !is_object(self::$db)) {
         if (!CAT_Registry::exists('CAT_PATH', false)) {
             CAT_Registry::define('CAT_PATH', dirname(__FILE__) . '/../..');
         }
         self::$db = CAT_Helper_DB::getInstance();
     }
     return self::$db;
 }