예제 #1
0
/**  Connect to Database
 *
 * @return object $db Database object
 */
function dbConnect()
{
    // XXX: one day all globals will die, I promise
    global $db_type;
    global $db_user;
    global $db_pass;
    global $db_host;
    global $db_port;
    global $db_name;
    global $db_file;
    global $db_debug;
    global $db_ssl_ca;
    global $sql_regexp;
    if (!(isset($db_type) && $db_type == 'mysql' || $db_type == 'mysqli' || $db_type == 'pgsql' || $db_type == 'sqlite' || $db_type == 'sqlite3' || $db_type == 'oci8')) {
        include_once "header.inc.php";
        error(ERR_DB_NO_DB_TYPE);
        include_once "footer.inc.php";
        exit;
    }
    if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_user) && $db_user != "")) {
        include_once "header.inc.php";
        error(ERR_DB_NO_DB_USER);
        include_once "footer.inc.php";
        exit;
    }
    if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_pass) && $db_pass != '')) {
        include_once "header.inc.php";
        error(ERR_DB_NO_DB_PASS);
        include_once "footer.inc.php";
        exit;
    }
    if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_host) && $db_host != '')) {
        include_once "header.inc.php";
        error(ERR_DB_NO_DB_HOST);
        include_once "footer.inc.php";
        exit;
    }
    if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_name) && $db_name != '')) {
        include_once "header.inc.php";
        error(ERR_DB_NO_DB_NAME);
        include_once "footer.inc.php";
        exit;
    }
    if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !isset($db_port) || $db_port == '') {
        if ($db_type == "mysql" || $db_type == "mysqli") {
            $db_port = 3306;
        } else {
            if ($db_type == 'oci8') {
                $db_port = 1521;
            } else {
                $db_port = 5432;
            }
        }
    }
    if (($db_type == 'sqlite' || $db_type == 'sqlite3') && !(isset($db_file) && $db_file != '')) {
        include_once "header.inc.php";
        error(ERR_DB_NO_DB_FILE);
        include_once "footer.inc.php";
        exit;
    }
    if ($db_type == 'sqlite' || $db_type == 'sqlite3') {
        $dsn = "{$db_type}:{$db_file}";
    } else {
        if ($db_type == 'oci8') {
            $db_name = '?service=' . $db_name;
        }
        $dsn = "{$db_type}:host={$db_host};port={$db_port};dbname={$db_name}";
    }
    $db = new PDOLayer($dsn, $db_user, $db_pass);
    if (isset($db_debug) && $db_debug) {
        $db->setOption('debug', 1);
    }
    /* erase info */
    $dsn = '';
    // Add support for regular expressions in both MySQL and PostgreSQL
    if ($db_type == 'mysql' || $db_type == 'mysqli' || $db_type == 'sqlite' || $db_type == 'sqlite3') {
        $sql_regexp = "REGEXP";
    } elseif ($db_type == "oci8") {
        # TODO: what is regexp syntax in Oracle?
        $sql_regexp = "";
    } elseif ($db_type == "pgsql") {
        $sql_regexp = "~";
    } else {
        include_once "header.inc.php";
        error(ERR_DB_NO_DB_TYPE);
        include_once "footer.inc.php";
        exit;
    }
    return $db;
}
function dbConnect()
{
    // XXX: one day all globals will die, I promise
    global $db_type;
    global $db_user;
    global $db_pass;
    global $db_host;
    global $db_port;
    global $db_name;
    global $db_file;
    global $db_layer;
    global $db_debug;
    global $sql_regexp;
    if (!(isset($db_type) && $db_type == 'mysql' || $db_type == 'mysqli' || $db_type == 'pgsql' || $db_type == 'sqlite' || $db_type == 'sqlite3' || $db_type == 'oci8')) {
        include_once "header.inc.php";
        error(ERR_DB_NO_DB_TYPE);
        include_once "footer.inc.php";
        exit;
    }
    if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_user) && $db_user != "")) {
        include_once "header.inc.php";
        error(ERR_DB_NO_DB_USER);
        include_once "footer.inc.php";
        exit;
    }
    if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_pass) && $db_pass != '')) {
        include_once "header.inc.php";
        error(ERR_DB_NO_DB_PASS);
        include_once "footer.inc.php";
        exit;
    }
    if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_host) && $db_host != '')) {
        include_once "header.inc.php";
        error(ERR_DB_NO_DB_HOST);
        include_once "footer.inc.php";
        exit;
    }
    if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_name) && $db_name != '')) {
        include_once "header.inc.php";
        error(ERR_DB_NO_DB_NAME);
        include_once "footer.inc.php";
        exit;
    }
    if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !isset($db_port)) {
        if ($db_type == "mysql" || $db_type == "mysqli") {
            $db_port = 3306;
        } else {
            if ($db_type == 'oci8') {
                $db_port = 1521;
            } else {
                $db_port = 5432;
            }
        }
    }
    if (($db_type == 'sqlite' || $db_type == 'sqlite3') && !(isset($db_file) && $db_file != '')) {
        include_once "header.inc.php";
        error(ERR_DB_NO_DB_FILE);
        include_once "footer.inc.php";
        exit;
    }
    if ($db_layer == 'MDB2') {
        if ($db_type == 'sqlite') {
            $dsn = "{$db_type}:///{$db_file}";
        } else {
            if ($db_type == 'sqlite3') {
                $dsn = "pdoSqlite:///{$db_file}";
            } else {
                if ($db_type == 'oci8') {
                    $db_name = '?service=' . $db_name;
                }
                $dsn = "{$db_type}://{$db_user}:{$db_pass}@{$db_host}:{$db_port}/{$db_name}";
            }
        }
    }
    if ($db_layer == 'PDO') {
        if ($db_type == 'sqlite' || $db_type == 'sqlite3') {
            $dsn = "{$db_type}:{$db_file}";
        } else {
            if ($db_type == 'oci8') {
                $db_name = '?service=' . $db_name;
            }
            $dsn = "{$db_type}:host={$db_host};dbname={$db_name}";
        }
    }
    if ($db_layer == 'MDB2') {
        $options = array('portability' => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL);
        $db = MDB2::connect($dsn, $options);
    }
    if ($db_layer == 'PDO') {
        $db = new PDOLayer($dsn, $db_user, $db_pass);
    }
    if (isset($db_debug) && $db_debug) {
        $db->setOption('debug', 1);
    }
    // FIXME - it's strange, but this doesn't work, perhaps bug in MDB2 library
    if (PEAR::isError($db)) {
        // Error handling should be put.
        error(MYSQL_ERROR_FATAL, $db->getMessage());
    }
    // Do an ASSOC fetch. Gives us the ability to use ["id"] fields.
    if ($db_layer == 'MDB2') {
        $db->setFetchMode(MDB2_FETCHMODE_ASSOC);
    }
    /* erase info */
    $dsn = '';
    // Add support for regular expressions in both MySQL and PostgreSQL
    if ($db_type == 'mysql' || $db_type == 'mysqli' || $db_type == 'sqlite' || $db_type == 'sqlite3') {
        $sql_regexp = "REGEXP";
    } elseif ($db_type == "oci8") {
        # TODO: what is regexp syntax in Oracle?
        $sql_regexp = "";
    } elseif ($db_type == "pgsql") {
        $sql_regexp = "~";
    } else {
        include_once "header.inc.php";
        error(ERR_DB_NO_DB_TYPE);
        include_once "footer.inc.php";
        exit;
    }
    return $db;
}