Files
sysPass/inc/DBConnectionFactory.class.php
nuxsmin 5f38a00900 * [ADD] New method to optimize/minimize CSS and JS files.
* [ADD] Temporary password can be viewed within session.
* [ADD] Old backup and export files are deleted when performing a new backup/export.
* [ADD] Added the ability to force the connection through HTTPS.
* [MOD] CSS and JS files are now minimized by default.
* [MOD] Updated translations.
* [MOD] UI tweaks.
* [FIX] Fixed issue on enabling secondary users/groups edit on accounts.
* [FIX] Fixed issue when sending email request to blank recipients.
2015-10-05 01:41:21 +02:00

111 lines
3.4 KiB
PHP

<?php
/**
* sysPass
*
* @author nuxsmin
* @link http://syspass.org
* @copyright 2012-2015 Rubén Domínguez nuxsmin@syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace SP;
use \PDO;
defined('APP_ROOT') || die(_('No es posible acceder directamente a este archivo'));
/**
* Class DBConnectionFactory
*
* Esta clase se encarga de crear las conexiones a la BD
*/
class DBConnectionFactory
{
/**
* @var DBConnectionFactory
*/
private static $_factory;
/**
* @var \PDO
*/
private $_db;
/**
* Obtener una instancia de la clase
*
* @return DBConnectionFactory
*/
public static function getFactory()
{
if (!self::$_factory) {
self::$_factory = new DBConnectionFactory();
}
return self::$_factory;
}
/**
* Realizar la conexión con la BBDD.
* Esta función utiliza PDO para conectar con la base de datos.
*
* @throws SPException
* @return \PDO
*/
public function getConnection()
{
if (!$this->_db) {
$isInstalled = Config::getValue('installed');
$dbhost = Config::getValue('dbhost');
$dbuser = Config::getValue('dbuser');
$dbpass = Config::getValue('dbpass');
$dbname = Config::getValue('dbname');
$dbport = Config::getValue('dbport', 3306);
if (empty($dbhost) || empty($dbuser) || empty($dbpass) || empty($dbname)) {
if ($isInstalled) {
Init::initError(_('No es posible conectar con la BD'), _('Compruebe los datos de conexión'));
} else {
throw new SPException(SPException::SP_CRITICAL, _('No es posible conectar con la BD'), _('Compruebe los datos de conexión'));
}
}
try {
$dsn = 'mysql:host=' . $dbhost . ';port=' . $dbport . ';dbname=' . $dbname . ';charset=utf8';
// $this->db = new PDO($dsn, $dbuser, $dbpass, array(PDO::ATTR_PERSISTENT => true));
$this->_db = new PDO($dsn, $dbuser, $dbpass);
} catch (\Exception $e) {
if ($isInstalled) {
if ($e->getCode() === 1049) {
Config::setValue('installed', '0');
}
Init::initError(_('No es posible conectar con la BD'), 'Error ' . $e->getCode() . ': ' . $e->getMessage());
} else {
throw new SPException(SPException::SP_CRITICAL, $e->getMessage(), $e->getCode());
}
}
}
$this->_db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $this->_db;
}
}