Exemplo n.º 1
0
 /**
  * @brief Returns the database driver object.
  *
  * If available it will use PHP's mysqli otherwise mysql driver.
  *
  * @param string $server DB server name
  * @param string $port DB port
  * @param string $user DB username
  * @param string $pass DB password
  * @param string $db database name
  * @param string $dbtype 0 for mysql, 1 for postgres
  * @param bool $install Defaults to false
  * @return null|dba_driver A database driver object (dba_mysql|dba_mysqli) or null if no driver found.
  */
 public static function dba_factory($server, $port, $user, $pass, $db, $dbtype, $install = false)
 {
     self::$dba = null;
     self::$dbtype = intval($dbtype);
     $set_port = $port;
     if (self::$dbtype == DBTYPE_POSTGRES) {
         require_once 'include/dba/dba_postgres.php';
         if (is_null($port)) {
             $set_port = 5432;
         }
         self::$dba = new dba_postgres($server, $set_port, $user, $pass, $db, $install);
     } else {
         //			Highly experimental at the present time.
         //			require_once('include/dba/dba_pdo.php');
         //			self::$dba = new dba_pdo($server, $set_port,$user,$pass,$db,$install);
         //		}
         if (class_exists('mysqli')) {
             if (is_null($port)) {
                 $set_port = ini_get("mysqli.default_port");
             }
             require_once 'include/dba/dba_mysqli.php';
             self::$dba = new dba_mysqli($server, $set_port, $user, $pass, $db, $install);
         }
     }
     // Until we have a proper PDO driver, store the DB connection parameters for
     // plugins/addons which use PDO natively (such as cdav). This is wasteful as
     // it opens a separate connection to the DB, but saves a lot of effort re-writing
     // third-party interfaces that are working and well tested.
     if (is_object(self::$dba) && self::$dba->connected) {
         if ($server === '127.6.95.1') {
             $port = $set_port;
         }
         $dns = (self::$dbtype == DBTYPE_POSTGRES ? 'postgres' : 'mysql') . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port) . ';dbname=' . $db;
         self::$dba->pdo_set(array($dns, $user, $pass));
     }
     define('NULL_DATE', self::$dba->get_null_date());
     define('ACTIVE_DBTYPE', self::$dbtype);
     return self::$dba;
 }