Beispiel #1
0
 /**
  * This function connects to a DB2 database
  *
  * @param   string $host
  * @param   string $username
  * @param   string $password
  * @param   string $db_name
  * @return  boolean TRUE, if connected, otherwise FALSE
  * @access  public
  * @author  Thorsten Rinne <*****@*****.**>
  * @since   2005-04-16
  */
 function connect($host, $user, $passwd, $db)
 {
     $this->conn = db2_pconnect($db, $user, $passwd, $this->options);
     if (false == $this->conn) {
         PMF_Db::errorPage(db2_conn_errormsg());
         die;
     }
     return true;
 }
Beispiel #2
0
 public function __construct(array $params, $username, $password, $driverOptions = array())
 {
     $isPersistant = isset($params['persistent']) && $params['persistent'] == true;
     if ($isPersistant) {
         $this->_conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
     } else {
         $this->_conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
     }
     if (!$this->_conn) {
         throw new DB2Exception(db2_conn_errormsg());
     }
 }
Beispiel #3
0
function dbQuery($query, $show_errors = true, $all_results = true, $show_output = true)
{
    if ($show_errors) {
        error_reporting(E_ALL);
    } else {
        error_reporting(E_PARSE);
    }
    // Connect to the IBM DB2 database management system
    $link = db2_pconnect("testdb", "db2inst1", "testpass");
    if (!$link) {
        die(db2_conn_errormsg());
    }
    // Print results in HTML
    print "<html><body>\n";
    // Print SQL query to test sqlmap '--string' command line option
    //print "<b>SQL query:</b> " . $query . "<br>\n";
    // Perform SQL injection affected query
    $stmt = db2_prepare($link, $query);
    $result = db2_execute($stmt);
    if (!$result) {
        if ($show_errors) {
            print "<b>SQL error:</b> " . db2_stmt_errormsg($stmt) . "<br>\n";
        }
        exit(1);
    }
    if (!$show_output) {
        exit(1);
    }
    print "<b>SQL results:</b>\n";
    print "<table border=\"1\">\n";
    while ($line = db2_fetch_array($stmt)) {
        print "<tr>";
        foreach ($line as $col_value) {
            print "<td>" . $col_value . "</td>";
        }
        print "</tr>\n";
        if (!$all_results) {
            break;
        }
    }
    print "</table>\n";
    print "</body></html>";
}
Beispiel #4
0
 /**
  * connect()
  *
  * This function connects to a DB2 database
  *
  * @param   string $host
  * @param   string $username
  * @param   string $password
  * @param   string $db_name
  * @return  boolean TRUE, if connected, otherwise FALSE
  * @access  public
  * @author  Thorsten Rinne <*****@*****.**>
  * @since   2005-04-16
  */
 function connect($host, $user, $passwd, $db)
 {
     $this->conn = db2_pconnect($db, $user, $passwd, $this->options);
     if (false == $this->conn) {
         print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
         print "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n";
         print "<head>\n";
         print "    <title>phpMyFAQ Error</title>\n";
         print "    <meta http-equiv=\"content-type\" content=\"application/xhtml+xml; charset=utf-8\" />\n";
         print "</head>\n";
         print "<body>\n";
         print "<p align=\"center\">The connection to the DB2 server could not be established.</p>\n";
         print "<p align=\"center\">The error message of the DB2 server:<br />" . db2_conn_errormsg() . "</p>\n";
         print "</body>\n";
         print "</html>";
         return false;
     }
     return true;
 }
Beispiel #5
0
 /**
  * {@inheritdoc}
  */
 public function connect()
 {
     $config = $this->config;
     $config = array_merge($this->baseConfig, $config);
     $conn = "DATABASE='{$config['database']}';HOSTNAME='{$config['host']}';PORT={$config['port']};";
     $conn .= "PROTOCOL=TCPIP;UID={$config['username']};PWD={$config['password']};";
     if (!$config['persistent']) {
         $this->connection = db2_connect($conn, PGSQL_CONNECT_FORCE_NEW);
     } else {
         $this->connection = db2_pconnect($conn);
     }
     $this->connected = false;
     if ($this->connection) {
         $this->connected = true;
         $this->query('SET search_path TO ' . $config['schema']);
     }
     if (!empty($config['charset'])) {
         $this->setCharset($config['charset']);
     }
     return $this->connection;
 }
Beispiel #6
0
 public function open()
 {
     if (!empty($GLOBALS['phpopenfw_db2_conn']) && is_resource($GLOBALS['phpopenfw_db2_conn']) && !$this->handle) {
         $this->handle = $GLOBALS['phpopenfw_db2_conn'];
     } else {
         if (!$this->handle) {
             $conn_str = "\n\t\t\t\tDRIVER={IBM DB2 ODBC DRIVER};\n\t\t\t\tDATABASE={$this->source};\n\t\t\t\tHOSTNAME={$this->server};\n\t\t\t\tPORT={$this->port};\n\t\t\t\tPROTOCOL=TCPIP;\n\t\t\t\tUID={$this->user};\n\t\t\t\tPWD={$this->pass};\n\t\t\t";
             // Connection String
             if ($this->conn_str !== false) {
                 $db_params = (string) $this->conn_str;
                 if ($this->persistent) {
                     $this->handle = !empty($this->options) ? db2_pconnect($db_params, '', '', $this->options) : db2_pconnect($db_params, '', '');
                 } else {
                     $this->handle = !empty($this->options) ? db2_connect($db_params, '', '', $this->options) : db2_connect($db_params, '', '');
                 }
             } else {
                 if ($this->persistent) {
                     $this->handle = !empty($this->options) ? db2_pconnect($this->source, $this->user, $this->pass, $this->options) : db2_pconnect($this->source, $this->user, $this->pass);
                 } else {
                     $this->handle = !empty($this->options) ? db2_connect($this->source, $this->user, $this->pass, $this->options) : db2_connect($this->source, $this->user, $this->pass);
                 }
             }
             if (db2_conn_errormsg()) {
                 $this->connection_error(db2_conn_errormsg());
                 $this->handle = false;
                 return false;
             }
             // Keep track of the number of connections we create
             $this->increment_counters();
         }
     }
     // Flag Connection as Open
     $this->conn_open = true;
     // Start Transaction and Turn off Auto Commit?
     if (!$this->auto_commit && !$this->trans_started) {
         db2_autocommit($this->handle, DB2_AUTOCOMMIT_OFF);
         $this->start_trans();
     }
     return true;
 }
Beispiel #7
0
 /**
  * 
  * 
  * @todo Throw in your "transport/adapter" framework for a real OO look and feel ....
  * Throw new Exception("Fail execute ($sql) ".db2_stmt_errormsg(),db2_stmt_error());
  * ... and retrieve via try/catch + Exception methods.
  *
  * @param $database
  * @param $user
  * @param $password
  * @param null $options 'persistent' is one option
  * @return bool
  */
 public function connect($database, $user, $password, $options = null)
 {
     // Compensate for older ibm_db2 driver that may not do this check.
     if ($user && empty($password)) {
         $this->setErrorCode('08001');
         $this->setErrorMsg('Authorization failure on distributed database connection attempt. SQLCODE=-30082');
         return false;
     }
     if ($options) {
         if (isset($options['persistent']) && $options['persistent']) {
             $conn = db2_pconnect($database, $user, $password);
         } else {
             $conn = db2_connect($database, $user, $password);
         }
         if (is_resource($conn)) {
             return $conn;
         }
     }
     $this->setErrorCode(db2_conn_error());
     $this->setErrorMsg(db2_conn_errormsg());
     return false;
 }
Beispiel #8
0
 /**
  * This function opens a connection using the data source provided.
  *
  * @access public
  * @override
  * @throws Throwable_Database_Exception         indicates that there is problem with
  *                                              opening the connection
  *
  * @see http://www.php.net/manual/en/function.db2-connect.php
  * @see http://www.php.net/manual/en/function.db2-conn-error.php
  * @see http://www.zinox.com/node/132
  * @see http://www.ibm.com/developerworks/data/library/techarticle/dm-0505furlong/
  */
 public function open()
 {
     if (!$this->is_connected()) {
         $connection_string = 'DRIVER={IBM DB2 ODBC DRIVER};';
         $connection_string .= 'DATABASE=' . $this->data_source->database . ';';
         $connection_string .= 'HOSTNAME=' . $this->data_source->host . ';';
         $connection_string .= 'PORT=' . $this->data_source->port . ';';
         $connection_string .= 'PROTOCOL=TCPIP;';
         $connection_string .= 'UID=' . $this->data_source->username . ';';
         $connection_string .= 'PWD=' . $this->data_source->password . ';';
         $this->resource = $this->data_source->is_persistent() ? @db2_pconnect($connection_string, '', '') : @db2_connect($connection_string, '', '');
         if ($this->resource === FALSE) {
             throw new Throwable_Database_Exception('Message: Failed to establish connection. Reason: :reason', array(':reason' => @db2_conn_error()));
         }
         // "To use UTF-8 when talking to a DB2 instance, use the following command from the DB2 home at the command prompt: db2set DB2CODEPAGE=1208"
     }
 }
 /**
  * Opens an uncataloged database connection, sets mConn
  */
 protected function openUncataloged($dbName, $user, $password, $server, $port)
 {
     $dsn = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE={$dbName};CHARSET=UTF-8;HOSTNAME={$server};PORT={$port};PROTOCOL=TCPIP;UID={$user};PWD={$password};";
     wfSuppressWarnings();
     $this->mConn = db2_pconnect($dsn, "", "", array());
     wfRestoreWarnings();
 }
Beispiel #10
0
 /**
  * Connecto to database
  *
  */
 private function connect()
 {
     switch ($this->model) {
         case "MYSQLI":
             if (!class_exists('mysqli')) {
                 throw new DatabaseException('Unsupported database model - ' . $this->model);
             }
             $this->dbh = new \mysqli($this->host, $this->user, $this->pass, $this->name, $this->port);
             if ($this->dbh->connect_error) {
                 throw new DatabaseException($this->dbh->connect_error, $this->dbh->connect_errno);
             }
             break;
         case "MYSQL_PDO":
             if (!in_array('mysql', \PDO::getAvailableDrivers())) {
                 throw new DatabaseException('Unsupported database model - ' . $this->model);
             }
             $dsn = "mysql:host=" . $this->host . ";port=" . $this->port . ";dbname=" . $this->name;
             try {
                 $this->dbh = new \PDO($dsn, $this->user, $this->pass, array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
             } catch (\PDOException $e) {
                 throw new DatabaseException($e->getMessage(), $e->getCode());
             }
             break;
         case "ORACLE_PDO":
             if (!in_array('oci', \PDO::getAvailableDrivers())) {
                 throw new DatabaseException('Unsupported database model - ' . $this->model);
             }
             $dsn = "oci:dbname=" . $this->host . ":" . $this->port . "/" . $this->name;
             try {
                 $this->dbh = new \PDO($dsn, $this->user, $this->pass, array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
             } catch (\PDOException $e) {
                 throw new DatabaseException($e->getMessage(), $e->getCode());
             }
             break;
         case "SQLITE_PDO":
             if (!in_array('sqlite', \PDO::getAvailableDrivers())) {
                 throw new DatabaseException('Unsupported database model - ' . $this->model);
             }
             $dsn = "sqlite:" . $this->name;
             try {
                 $this->dbh = new \PDO($dsn, $this->user, $this->pass, array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
             } catch (\PDOException $e) {
                 throw new DatabaseException($e->getMessage(), $e->getCode());
             }
             break;
         case "DB2":
             if (!function_exists('db2_pconnect')) {
                 throw new DatabaseException('Unsupported database model - ' . $this->model);
             }
             $dsn = "ibm:DRIVER={IBM DB2 ODBC DRIVER};DATABASE=" . $this->name;
             $dsn .= ";HOSTNAME=" . $this->host . ";PORT=" . $this->port . ";PROTOCOL=TCPIP;UID=" . $this->user . ";PWD=" . $this->pass . ";";
             $this->dbh = db2_pconnect($dsn, $this->user, $this->pass);
             if (!$this->dbh) {
                 throw new DatabaseException(db2_conn_errormsg());
             }
             break;
         case "DBLIB_PDO":
             if (!in_array('dblib', \PDO::getAvailableDrivers())) {
                 throw new DatabaseException('Unsupported database model - ' . $this->model);
             }
             $dsn = "dblib:host=" . $this->host . ":" . $this->port . ";dbname=" . $this->name;
             try {
                 $this->dbh = new \PDO($dsn, $this->user, $this->pass, array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
             } catch (\PDOException $e) {
                 throw new DatabaseException($e->getMessage(), $e->getCode());
             }
             break;
         case "POSTGRESQL":
             if (!function_exists('pg_connect')) {
                 throw new DatabaseException('Unsupported database model - ' . $this->model);
             }
             $dsn = "host=" . $this->host . " port=" . $this->port . " dbname=" . $this->name . " user="******" password=" . $this->pass;
             $this->dbh = @pg_connect($dsn);
             if ($this->dbh == false) {
                 throw new DatabaseException(pg_last_error());
             }
             break;
     }
 }
Beispiel #11
0
 public function connect(array $configOptions = null, $dieOnError = false)
 {
     global $sugar_config;
     if (is_null($configOptions)) {
         $configOptions = $sugar_config['dbconfig'];
     }
     if ($this->getOption('persistent')) {
         $persistConnection = true;
     } else {
         $persistConnection = false;
     }
     // Creating the connection string dynamically so that we can accommodate all scenarios
     // Starting with user and password as we always need these.
     $dsn = "UID=" . $configOptions['db_user_name'] . ";PWD=" . $configOptions['db_password'] . ";";
     $this->schema = strtoupper($configOptions['db_user_name']);
     // Converting to upper since DB2 expects it that way
     if (isset($configOptions['db_name']) && $configOptions['db_name'] != '') {
         $dsn = $dsn . "DATABASE=" . $configOptions['db_name'] . ";";
     }
     if (!isset($configOptions['db_host_name']) || $configOptions['db_host_name'] == '') {
         $configOptions['db_host_name'] = 'localhost';
     }
     // Connect to localhost by default
     $dsn = $dsn . "HOSTNAME=" . $configOptions['db_host_name'] . ";";
     if (!isset($configOptions['db_protocol']) || $configOptions['db_protocol'] == '') {
         $configOptions['db_protocol'] = 'TCPIP';
     }
     // Use TCPIP as default protocol
     $dsn = $dsn . "PROTOCOL=" . $configOptions['db_protocol'] . ";";
     if (!isset($configOptions['db_port']) || $configOptions['db_port'] == '') {
         $configOptions['db_port'] = '50000';
     }
     // Use 50000 as the default port
     $dsn = $dsn . "PORT=" . $configOptions['db_port'] . ";";
     if (!isset($configOptions['db_options'])) {
         $configOptions['db_options'] = array();
     }
     if ($persistConnection) {
         $this->database = db2_pconnect($dsn, '', '', $configOptions['db_options']);
         if (!$this->database) {
             $this->log->fatal(__CLASS__ . ": Persistent connection specified, but failed. Error: " . db2_conn_error() . ": " . db2_conn_errormsg());
         }
     }
     if (!$this->database) {
         $this->database = db2_connect($dsn, '', '', $configOptions['db_options']);
         if ($this->database && $persistConnection) {
             $_SESSION['administrator_error'] = "<b>Severe Performance Degradation: Persistent Database Connections " . "not working.  Please set \$sugar_config['dbconfigoption']['persistent'] to false " . "in your config.php file</b>";
         }
         if (!$this->database) {
             $this->log->fatal(__CLASS__ . ": Could not connect to Database with non-persistent connection. Error " . db2_conn_error() . ": " . db2_conn_errormsg());
         }
     }
     // Skipping check for statement errors as there is a bug in the DB2 driver
     // http://pecl.php.net/bugs/bug.php?id=22854
     // TODO take this skip out when the DB2 bug is fixed
     $this->ignoreErrors = true;
     if (!$this->checkError('Could Not Connect:', $dieOnError) && $this->database != false) {
         $this->log->info("connected to db");
         if (db2_autocommit($this->database, DB2_AUTOCOMMIT_OFF)) {
             $this->log->info("turned autocommit off");
         } else {
             $this->log->error("failed to turn autocommit off!");
         }
     }
     $this->ignoreErrors = false;
     $this->log->info("Connect:" . $this->database);
     return !empty($this->database);
 }
Beispiel #12
0
 /**
  * Opens an uncataloged database connection, sets mConn
  */
 protected function openUncataloged($dbName, $user, $password, $server, $port)
 {
     $str = "DRIVER={IBM DB2 ODBC DRIVER};";
     $str .= "DATABASE={$dbName};";
     $str .= "HOSTNAME={$server};";
     // port was formerly validated to not be 0
     $str .= "PORT={$port};";
     $str .= "PROTOCOL=TCPIP;";
     $str .= "UID={$user};";
     $str .= "PWD={$password};";
     @($this->mConn = db2_pconnect($str, $user, $password));
 }
	function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
	{
		global $php_errormsg;

		if (!function_exists('db2_connect')) return null;

		// This needs to be set before the connect().
		// Replaces the odbc_binmode() call that was in Execute()
		ini_set('ibm_db2.binmode', $this->binmode);

		if (isset($php_errormsg)) $php_errormsg = '';
		$this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';

		if ($argDatabasename && empty($argDSN)) {

			if (stripos($argDatabasename,'UID=') && stripos($argDatabasename,'PWD=')) $this->_connectionID = db2_pconnect($argDatabasename,null,null);
			else $this->_connectionID = db2_pconnect($argDatabasename,$argUsername,$argPassword);
		} else {
			if ($argDatabasename) $schema = $argDatabasename;
			if (stripos($argDSN,'UID=') && stripos($argDSN,'PWD=')) $this->_connectionID = db2_pconnect($argDSN,null,null);
			else $this->_connectionID = db2_pconnect($argDSN,$argUsername,$argPassword);
		}
		if (isset($php_errormsg)) $php_errormsg = '';

		$this->_errorMsg = @db2_conn_errormsg();
		if ($this->_connectionID && $this->autoRollback) @db2_rollback($this->_connectionID);
		if (isset($this->connectStmt)) $this->Execute($this->connectStmt);

		if ($this->_connectionID && isset($schema)) $this->Execute("SET SCHEMA=$schema");
		return $this->_connectionID != false;
	}
Beispiel #14
0
function db2_operate($query, array $args = array(), array $options = array())
{
    global $db2_conn;
    if (!$db2_conn) {
        $conn_string = variable_get('simsauth_connect_string');
        $db2_conn = db2_pconnect($conn_string, '', '');
    }
    if (!empty($args)) {
        $stmt = db2_prepare($db2_conn, $query);
        if (!empty($options)) {
            $op_result = db2_set_option($stmt, $options, 2);
            if (!$op_result) {
                debug(db2_stmt_errormsg(), 'simsauth');
            }
        }
        $result = db2_execute($stmt, $args);
        if (!$result) {
            debug(db2_stmt_errormsg(), 'simsauth');
        } else {
            return TRUE;
        }
    } else {
        if (!empty($options)) {
            $op_result = db2_set_option($db2_conn, $options, 1);
            if (!$op_result) {
                debug(db2_conn_errormsg(), 'simsauth');
            }
        }
        $rs = db2_exec($db2_conn, $query);
        if (!$rs) {
            debug(db2_conn_errormsg(), 'simsauth');
        } else {
            return TRUE;
        }
    }
    return FALSE;
}
Beispiel #15
0
 /**
  * Connect
  *
  * @return ConnectionInterface
  */
 public function connect()
 {
     if (is_resource($this->resource)) {
         return $this;
     }
     // localize
     $p = $this->connectionParameters;
     // given a list of key names, test for existence in $p
     $findParameterValue = function (array $names) use($p) {
         foreach ($names as $name) {
             if (isset($p[$name])) {
                 return $p[$name];
             }
         }
         return null;
     };
     $database = $findParameterValue(array('database', 'db'));
     $username = $findParameterValue(array('username', 'uid', 'UID'));
     $password = $findParameterValue(array('password', 'pwd', 'PWD'));
     $isPersistent = $findParameterValue(array('persistent', 'PERSISTENT', 'Persistent'));
     $options = isset($p['driver_options']) ? $p['driver_options'] : array();
     if ($isPersistent) {
         $this->resource = db2_pconnect($database, $username, $password, $options);
     } else {
         $this->resource = db2_connect($database, $username, $password, $options);
     }
     if ($this->resource === false) {
         throw new Exception\RuntimeException(sprintf('%s: Unable to connect to database', __METHOD__));
     }
     return $this;
 }
Beispiel #16
0
 function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
 {
     global $php_errormsg;
     if (!function_exists('db2_connect')) {
         return null;
     }
     // This needs to be set before the connect().
     // Replaces the odbc_binmode() call that was in Execute()
     ini_set('ibm_db2.binmode', $this->binmode);
     if (isset($php_errormsg)) {
         $php_errormsg = '';
     }
     $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
     if ($argDatabasename) {
         $this->_connectionID = db2_pconnect($argDatabasename, $argUsername, $argPassword);
     } else {
         $this->_connectionID = db2_pconnect($argDSN, $argUsername, $argPassword);
     }
     if (isset($php_errormsg)) {
         $php_errormsg = '';
     }
     $this->_errorMsg = @db2_conn_errormsg();
     if ($this->_connectionID && $this->autoRollback) {
         @db2_rollback($this->_connectionID);
     }
     if (isset($this->connectStmt)) {
         $this->Execute($this->connectStmt);
     }
     return $this->_connectionID != false;
 }
Beispiel #17
0
/**
 * Opens up a database connection.
 *
 * Use a pooled connection if the db supports it and
 * the <var>db_persistent</var> setting is enabled.
 *
 * <b>Notes:</b>
 * - The database type is determined by the global variable
 *   <var>db_type</var>
 * - For ODBC, <var>$host</var> is ignored, <var>$database</var> = DSN
 * - For Oracle, <var>$database</var> = tnsnames name
 * - Use the {@link dbi_error()} function to get error information if the connection
 *   fails
 *
 * @param string $host     Hostname of database server
 * @param string $login    Database login
 * @param string $password Database login password
 * @param string $database Name of database
 * 
 * @return resource The connection
 */
function dbi_connect($host, $login, $password, $database)
{
    if (strcmp($GLOBALS["db_type"], "mysql") == 0) {
        if ($GLOBALS["db_persistent"]) {
            $c = mysql_pconnect($host, $login, $password);
        } else {
            $c = mysql_connect($host, $login, $password);
        }
        if ($c) {
            if (!mysql_select_db($database)) {
                return false;
            }
            return $c;
        } else {
            return false;
        }
    } else {
        if (strcmp($GLOBALS["db_type"], "mysqli") == 0) {
            if ($GLOBALS["db_persistent"]) {
                $c = @mysqli_connect($host, $login, $password, $database);
            } else {
                $c = @mysqli_connect($host, $login, $password, $database);
            }
            if ($c) {
                /*
                if ( ! mysqli_select_db ( $c, $database ) )
                  return false;
                */
                $GLOBALS["db_connection"] = $c;
                return $c;
            } else {
                return false;
            }
        } else {
            if (strcmp($GLOBALS["db_type"], "mssql") == 0) {
                if ($GLOBALS["db_persistent"]) {
                    $c = mssql_pconnect($host, $login, $password);
                } else {
                    $c = mssql_connect($host, $login, $password);
                }
                if ($c) {
                    if (!mssql_select_db($database)) {
                        return false;
                    }
                    return $c;
                } else {
                    return false;
                }
            } else {
                if (strcmp($GLOBALS["db_type"], "oracle") == 0) {
                    if (strlen($host) && strcmp($host, "localhost")) {
                        $c = OCIPLogon("{$login}@{$host}", $password, $database);
                    } else {
                        $c = OCIPLogon($login, $password, $database);
                    }
                    $GLOBALS["oracle_connection"] = $c;
                    return $c;
                } else {
                    if (strcmp($GLOBALS["db_type"], "postgresql") == 0) {
                        if (strlen($password)) {
                            if (strlen($host)) {
                                $dbargs = "host={$host} dbname={$database} user={$login} password={$password}";
                            } else {
                                $dbargs = "dbname={$database} user={$login} password={$password}";
                            }
                        } else {
                            if (strlen($host)) {
                                $dbargs = "host={$host} dbname={$database} user={$login}";
                            } else {
                                $dbargs = "dbname={$database} user={$login}";
                            }
                        }
                        if ($GLOBALS["db_persistent"]) {
                            $c = pg_pconnect($dbargs);
                        } else {
                            $c = pg_connect($dbargs);
                        }
                        $GLOBALS["postgresql_connection"] = $c;
                        if (!$c) {
                            echo "Error connecting to database\n";
                            exit;
                        }
                        return $c;
                    } else {
                        if (strcmp($GLOBALS["db_type"], "odbc") == 0) {
                            if ($GLOBALS["db_persistent"]) {
                                $c = odbc_pconnect($database, $login, $password);
                            } else {
                                $c = odbc_connect($database, $login, $password);
                            }
                            $GLOBALS["odbc_connection"] = $c;
                            return $c;
                        } else {
                            if (strcmp($GLOBALS["db_type"], "ibm_db2") == 0) {
                                if ($GLOBALS["db_persistent"]) {
                                    $c = db2_pconnect($database, $login, $password);
                                } else {
                                    $c = db2_connect($database, $login, $password);
                                }
                                $GLOBALS["ibm_db2_connection"] = $c;
                                return $c;
                            } else {
                                if (strcmp($GLOBALS["db_type"], "ibase") == 0) {
                                    $host = $host . ":" . $database;
                                    if ($GLOBALS["db_persistent"]) {
                                        $c = ibase_pconnect($host, $login, $password);
                                    } else {
                                        $c = ibase_connect($host, $login, $password);
                                    }
                                    return $c;
                                } else {
                                    if (empty($GLOBALS["db_type"])) {
                                        dbi_fatal_error("dbi_connect(): db_type not defined.");
                                    } else {
                                        dbi_fatal_error("dbi_connect(): invalid db_type '" . $GLOBALS["db_type"] . "'");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Beispiel #18
0
function dbi_connect($host, $login, $password, $database, $lazy = true)
{
    global $db_cache_count, $db_connection_info, $db_query_count, $old_textlimit, $old_textsize, $db_sqlite_error_str;
    $db_cache_count = $db_query_count = 0;
    if (!isset($db_connection_info)) {
        $db_connection_info = array();
    }
    $db_connection_info['connected'] = false;
    $db_connection_info['connection'] = 0;
    $db_connection_info['database'] = $database;
    $db_connection_info['host'] = $host;
    $db_connection_info['login'] = $login;
    $db_connection_info['password'] = $password;
    // mysqli requires $db_connection_info['connection'] to be set
    if (strcmp($GLOBALS['db_type'], 'mysqli') == 0) {
        $lazy == false;
    }
    // Lazy connections... do not connect until 1st call to dbi_query.
    if ($lazy) {
        // echo "<!-- Waiting on db connection made (lazy) -->\nRETURN!<br />";
        return true;
    }
    if (strcmp($GLOBALS['db_type'], 'mysql') == 0) {
        $c = $GLOBALS['db_persistent'] ? mysql_pconnect($host, $login, $password) : mysql_connect($host, $login, $password);
        if ($c) {
            if (!mysql_select_db($database)) {
                return false;
            }
            $db_connection_info['connected'] = true;
            $db_connection_info['connection'] = $c;
            return $c;
        } else {
            return false;
        }
    } elseif (strcmp($GLOBALS['db_type'], 'mysqli') == 0) {
        $c = new mysqli($host, $login, $password, $database);
        if ($c) {
            if (mysqli_connect_errno() && !empty($database)) {
                return false;
            }
            $db_connection_info['connected'] = true;
            $db_connection_info['connection'] = $GLOBALS['db_connection'] = $c;
            return $c;
        } else {
            return false;
        }
    } elseif (strcmp($GLOBALS['db_type'], 'mssql') == 0) {
        static $old_textlimit, $old_textsize;
        $old_textlimit = ini_get('mssql.textlimit');
        $old_textsize = ini_get('mssql.textsize');
        ini_set('mssql.textlimit', '2147483647');
        ini_set('mssql.textsize', '2147483647');
        $c = $GLOBALS['db_persistent'] ? mssql_pconnect($host, $login, $password) : mssql_connect($host, $login, $password);
        if ($c) {
            if (!mssql_select_db($database)) {
                return false;
            }
            $db_connection_info['connected'] = true;
            $db_connection_info['connection'] = $c;
            return $c;
        } else {
            return false;
        }
    } elseif (strcmp($GLOBALS['db_type'], 'oracle') == 0) {
        $_ora_conn_func = 'OCI' . ($GLOBALS['db_persistent'] ? 'P' : '') . 'Logon';
        $c = $_ora_conn_func($login, $password, strlen($host) && strcmp($host, 'localhost') ? '(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (HOST = ' . $host . ' ) (PORT = 1521))) (CONNECT_DATA = (SID = ' . $database . ' )))' : $database);
        unset($_ora_conn_func);
        $db_connection_info['connected'] = true;
        $db_connection_info['connection'] = $GLOBALS['oracle_connection'] = $c;
        return $c;
    } elseif (strcmp($GLOBALS['db_type'], 'postgresql') == 0) {
        $dbargs = (strlen($host) ? 'host=' . "{$host} " : '') . 'dbname=' . $database . ' user='******' password='******'');
        $c = $GLOBALS['db_persistent'] ? pg_pconnect($dbargs) : pg_connect($dbargs);
        $GLOBALS['postgresql_connection'] = $c;
        if (!$c) {
            return false;
        }
        $db_connection_info['connected'] = true;
        $db_connection_info['connection'] = $c;
        return $c;
    } elseif (strcmp($GLOBALS['db_type'], 'odbc') == 0) {
        $c = $GLOBALS['db_persistent'] ? odbc_pconnect($database, $login, $password) : odbc_connect($database, $login, $password);
        $db_connection_info['connected'] = true;
        $db_connection_info['connection'] = $GLOBALS['odbc_connection'] = $c;
        return $c;
    } elseif (strcmp($GLOBALS['db_type'], 'ibm_db2') == 0) {
        $c = $GLOBALS['db_persistent'] ? db2_pconnect($database, $login, $password) : db2_connect($database, $login, $password);
        $db_connection_info['connected'] = true;
        $db_connection_info['connection'] = $GLOBALS['ibm_db2_connection'] = $c;
        return $c;
    } elseif (strcmp($GLOBALS['db_type'], 'ibase') == 0) {
        $host = $host . ':' . $database;
        $c = $GLOBALS['db_persistent'] ? ibase_pconnect($host, $login, $password) : ibase_connect($host, $login, $password);
        $db_connection_info['connected'] = true;
        $db_connection_info['connection'] = $c;
        return $c;
    } elseif (strcmp($GLOBALS['db_type'], 'sqlite') == 0) {
        $c = $GLOBALS['db_persistent'] ? sqlite_popen($database, 0666, $db_sqlite_error_str) : sqlite_open($database, 0666, $db_sqlite_error_str);
        if (!$c) {
            echo translate('Error connecting to database') . ": " . $db_sqlite_error_str . ".\n";
            exit;
        }
        $db_connection_info['connected'] = true;
        $db_connection_info['connection'] = $GLOBALS['sqlite_c'] = $c;
        return $c;
    } else {
        dbi_fatal_error('dbi_connect (): ' . (empty($GLOBALS['db_type']) ? translate('db_type not defined.') : translate('invalid db_type') . ' ' . $GLOBALS['db_type'] . '.'));
    }
}