/** * connects to the database * @return boolean|null true if connection can be established or false on error * * Connects to the database as specified in config.php */ public static function connect() { if (self::$connection) { return true; } $type = OC_Config::getValue('dbtype', 'sqlite'); $factory = new \OC\DB\ConnectionFactory(); if (!$factory->isValidType($type)) { return false; } $connectionParams = array('user' => OC_Config::getValue('dbuser', ''), 'password' => OC_Config::getValue('dbpassword', '')); $name = OC_Config::getValue('dbname', 'owncloud'); if ($factory->normalizeType($type) === 'sqlite3') { $datadir = OC_Config::getValue("datadirectory", OC::$SERVERROOT . '/data'); $connectionParams['path'] = $datadir . '/' . $name . '.db'; } else { $host = OC_Config::getValue('dbhost', ''); if (strpos($host, ':')) { // Host variable may carry a port or socket. list($host, $portOrSocket) = explode(':', $host, 2); if (ctype_digit($portOrSocket)) { $connectionParams['port'] = $portOrSocket; } else { $connectionParams['unix_socket'] = $portOrSocket; } } $connectionParams['host'] = $host; $connectionParams['dbname'] = $name; } $connectionParams['tablePrefix'] = OC_Config::getValue('dbtableprefix', 'oc_'); try { self::$connection = $factory->getConnection($type, $connectionParams); } catch (\Doctrine\DBAL\DBALException $e) { OC_Log::write('core', $e->getMessage(), OC_Log::FATAL); OC_User::setUserId(null); // send http status 503 header('HTTP/1.1 503 Service Temporarily Unavailable'); header('Status: 503 Service Temporarily Unavailable'); OC_Template::printErrorPage('Failed to connect to database'); die; } return true; }
/** * @brief Disconnect * @returns true/false * * This is good bye, good bye, yeah! */ public static function disconnect() { // Cut connection if required if (self::$connection) { if (self::$backend == self::BACKEND_MDB2) { self::$connection->disconnect(); } self::$connection = false; self::$MDB2 = false; self::$PDO = false; } return true; }
/** * @brief connects to the database * @return bool true if connection can be established or false on error * * Connects to the database as specified in config.php */ public static function connect() { if (self::$connection) { return true; } // The global data we need $name = OC_Config::getValue("dbname", "owncloud"); $host = OC_Config::getValue("dbhost", ""); $user = OC_Config::getValue("dbuser", ""); $pass = OC_Config::getValue("dbpassword", ""); $type = OC_Config::getValue("dbtype", "sqlite"); if (strpos($host, ':')) { list($host, $port) = explode(':', $host, 2); } else { $port = false; } // do nothing if the connection already has been established if (!self::$connection) { $config = new \Doctrine\DBAL\Configuration(); $eventManager = new \Doctrine\Common\EventManager(); switch ($type) { case 'sqlite': case 'sqlite3': $datadir = OC_Config::getValue("datadirectory", OC::$SERVERROOT . '/data'); $connectionParams = array('user' => $user, 'password' => $pass, 'path' => $datadir . '/' . $name . '.db', 'driver' => 'pdo_sqlite'); $connectionParams['adapter'] = '\\OC\\DB\\AdapterSqlite'; $connectionParams['wrapperClass'] = 'OC\\DB\\Connection'; break; case 'mysql': $connectionParams = array('user' => $user, 'password' => $pass, 'host' => $host, 'port' => $port, 'dbname' => $name, 'charset' => 'UTF8', 'driver' => 'pdo_mysql'); $connectionParams['adapter'] = '\\OC\\DB\\Adapter'; $connectionParams['wrapperClass'] = 'OC\\DB\\Connection'; // Send "SET NAMES utf8". Only required on PHP 5.3 below 5.3.6. // See http://stackoverflow.com/questions/4361459/php-pdo-charset-set-names#4361485 $eventManager->addEventSubscriber(new \Doctrine\DBAL\Event\Listeners\MysqlSessionInit()); break; case 'pgsql': $connectionParams = array('user' => $user, 'password' => $pass, 'host' => $host, 'port' => $port, 'dbname' => $name, 'driver' => 'pdo_pgsql'); $connectionParams['adapter'] = '\\OC\\DB\\AdapterPgSql'; $connectionParams['wrapperClass'] = 'OC\\DB\\Connection'; break; case 'oci': $connectionParams = array('user' => $user, 'password' => $pass, 'host' => $host, 'dbname' => $name, 'charset' => 'AL32UTF8', 'driver' => 'oci8'); if (!empty($port)) { $connectionParams['port'] = $port; } $connectionParams['adapter'] = '\\OC\\DB\\AdapterOCI8'; $connectionParams['wrapperClass'] = 'OC\\DB\\OracleConnection'; $eventManager->addEventSubscriber(new \Doctrine\DBAL\Event\Listeners\OracleSessionInit()); break; case 'mssql': $connectionParams = array('user' => $user, 'password' => $pass, 'host' => $host, 'port' => $port, 'dbname' => $name, 'charset' => 'UTF8', 'driver' => 'pdo_sqlsrv'); $connectionParams['adapter'] = '\\OC\\DB\\AdapterSQLSrv'; $connectionParams['wrapperClass'] = 'OC\\DB\\Connection'; break; default: return false; } $connectionParams['tablePrefix'] = OC_Config::getValue('dbtableprefix', 'oc_'); try { self::$connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config, $eventManager); if ($type === 'sqlite' || $type === 'sqlite3') { // Sqlite doesn't handle query caching and schema changes // TODO: find a better way to handle this self::$connection->disableQueryStatementCaching(); } } catch (\Doctrine\DBAL\DBALException $e) { OC_Log::write('core', $e->getMessage(), OC_Log::FATAL); OC_User::setUserId(null); // send http status 503 header('HTTP/1.1 503 Service Temporarily Unavailable'); header('Status: 503 Service Temporarily Unavailable'); OC_Template::printErrorPage('Failed to connect to database'); die; } } return true; }