* [FIX] Solved weird issue when connecting to MySQL DB after a successful installation. Solves #382

This commit is contained in:
nuxsmin
2017-01-23 23:50:06 +01:00
parent 957e6ddf08
commit a5358476ab
10 changed files with 87 additions and 77 deletions

View File

@@ -3,8 +3,8 @@
/**
* sysPass
*
* @author nuxsmin
* @link http://syspass.org
* @author nuxsmin
* @link http://syspass.org
* @copyright 2012-2017, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
@@ -40,6 +40,7 @@ use SP\Mgmt\Groups\Group;
use SP\Mgmt\Profiles\Profile;
use SP\Mgmt\Users\User;
use SP\Mgmt\Users\UserPass;
use SP\Storage\DBUtil;
use SP\Util\Util;
defined('APP_ROOT') || die();
@@ -104,6 +105,7 @@ class Installer
// Set DB connection info
$this->Config->setDbHost($this->InstallData->getDbHost());
$this->Config->setDbPort($this->InstallData->getDbPort());
$this->Config->setDbName($this->InstallData->getDbName());
// Set site config
@@ -111,6 +113,7 @@ class Installer
$this->connectDatabase();
$this->setupMySQLDatabase();
$this->checkConnection();
$this->createAdminAccount();
ConfigDB::setValue('version', implode(Util::getVersion(true)));
@@ -208,14 +211,15 @@ class Installer
$this->InstallData->setDbUser(substr('sp_' . $this->InstallData->getAdminLogin(), 0, 16));
// Comprobar si el usuario sumistrado existe
$query = sprintf(/** @lang SQL */
'SELECT COUNT(*) FROM mysql.user WHERE user=\'%s\' AND host=\'%s\'',
$this->InstallData->getDbUser(),
$this->InstallData->getDbAuthHost());
$query = /** @lang SQL */
'SELECT COUNT(*) FROM mysql.user WHERE user = ? AND host = ?';
try {
$sth = $this->DB->prepare($query);
$sth->execute([$this->InstallData->getDbUser(), $this->InstallData->getDbAuthHost()]);
// Si no existe el usuario, se intenta crear
if ((int)$this->DB->query($query)->fetchColumn() === 0
if ((int)$sth->fetchColumn() === 0
// Se comprueba si el nuevo usuario es distinto del creado en otra instalación
&& $this->InstallData->getDbUser() != $this->Config->getDbUser()
) {
@@ -257,21 +261,16 @@ class Installer
return;
}
$query = sprintf(/** @lang SQL */
'CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'',
$this->InstallData->getDbUser(),
$this->InstallData->getDbAuthHost(),
$this->InstallData->getDbPass());
$query = /** @lang SQL */
'CREATE USER `' . $this->InstallData->getDbUser() . '`@`' . $this->InstallData->getDbAuthHost() . '` IDENTIFIED BY \'' . $this->InstallData->getDbPass() . '\'';
$queryDns = sprintf(/** @lang SQL */
'CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'',
$this->InstallData->getDbUser(),
$this->InstallData->getDbAuthHostDns(),
$this->InstallData->getDbPass());
$queryDns = /** @lang SQL */
'CREATE USER `' . $this->InstallData->getDbUser() . '`@`' . $this->InstallData->getDbAuthHostDns() . '` IDENTIFIED BY \'' . $this->InstallData->getDbPass() . '\'';
try {
$this->DB->query($query);
$this->DB->query($queryDns);
$this->DB->exec($query);
$this->DB->exec($queryDns);
$this->DB->exec('FLUSH PRIVILEGES');
} catch (PDOException $e) {
throw new SPException(
SPException::SP_CRITICAL,
@@ -300,35 +299,31 @@ class Installer
}
if (!$this->InstallData->isHostingMode()) {
$query = sprintf(/** @lang SQL */
'CREATE SCHEMA `%s` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci', $this->InstallData->getDbName());
try {
$this->DB->query($query);
$this->DB->exec(/** @lang SQL */
'CREATE SCHEMA `' . $this->InstallData->getDbName() . '` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci');
} catch (PDOException $e) {
throw new SPException(SPException::SP_CRITICAL,
sprintf(__('Error al crear la BBDD', false) . ' (%s)', $e->getMessage()),
__('Verifique los permisos del usuario de la Base de Datos', false));
}
$query = sprintf(/** @lang SQL */
'GRANT ALL PRIVILEGES ON `%s`.* TO `%s`@`%s` IDENTIFIED BY \'%s\';',
$this->InstallData->getDbName(),
$this->InstallData->getDbUser(),
$this->InstallData->getDbAuthHost(),
$this->InstallData->getDbPass());
$query = /** @lang SQL */
'GRANT ALL PRIVILEGES ON `' . $this->InstallData->getDbName() . '`.*
TO `' . $this->InstallData->getDbUser() . '`@`' . $this->InstallData->getDbAuthHost() . '`';
$queryDns = sprintf(/** @lang SQL */
'GRANT ALL PRIVILEGES ON `%s`.* TO `%s`@`%s` IDENTIFIED BY \'%s\';',
$this->InstallData->getDbName(),
$this->InstallData->getDbUser(),
$this->InstallData->getDbAuthHostDns(),
$this->InstallData->getDbPass());
$queryDns = /** @lang SQL */
'GRANT ALL PRIVILEGES ON `' . $this->InstallData->getDbName() . '`.*
TO `' . $this->InstallData->getDbUser() . '`@`' . $this->InstallData->getDbAuthHostDns() . '`';
try {
$this->DB->query($query);
$this->DB->query($queryDns);
$this->DB->exec($query);
$this->DB->exec($queryDns);
$this->DB->exec('FLUSH PRIVILEGES');
} catch (PDOException $e) {
$this->rollback();
throw new SPException(SPException::SP_CRITICAL,
sprintf(__('Error al establecer permisos de la BBDD (\'%s\')'), $e->getMessage()),
__('Verifique los permisos del usuario de la Base de Datos', false));
@@ -343,12 +338,33 @@ class Installer
*/
private function checkDatabaseExist()
{
$query = sprintf(/** @lang SQL */
'SELECT COUNT(*)
FROM information_schema.schemata
WHERE schema_name = \'%s\' LIMIT 1', $this->InstallData->getDbName());
$query = /** @lang SQL */
'SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name = ? LIMIT 1';
return ((int)$this->DB->query($query)->fetchColumn() > 0);
$sth = $this->DB->prepare($query);
$sth->execute([$this->InstallData->getDbName()]);
return ((int)$sth->fetchColumn() > 0);
}
/**
* Deshacer la instalación en caso de fallo.
* Esta función elimina la base de datos y el usuario de sysPass
*/
private function rollback()
{
try {
$this->DB->exec('DROP DATABASE IF EXISTS `' . $this->InstallData->getDbName() . '`');
$this->DB->exec('DROP USER `' . $this->InstallData->getDbUser() . '`@`' . $this->InstallData->getDbAuthHost() . '`');
$this->DB->exec('DROP USER `' . $this->InstallData->getDbUser() . '`@`' . $this->InstallData->getDbAuthHostDns() . '`');
$this->DB->exec('DROP USER `' . $this->InstallData->getDbUser() . '`@`%`');
} catch (PDOException $e) {
debugLog($e->getMessage());
return false;
}
return true;
}
/**
@@ -369,7 +385,7 @@ class Installer
// Usar la base de datos de sysPass
try {
$this->DB->query('USE `' . $this->InstallData->getDbName() . '`');
$this->DB->exec('USE `' . $this->InstallData->getDbName() . '`');
} catch (PDOException $e) {
throw new SPException(SPException::SP_CRITICAL,
sprintf(__('Error al seleccionar la BBDD', false) . ' \'%s\' (%s)', $this->InstallData->getDbName(), $e->getMessage()),
@@ -387,8 +403,7 @@ class Installer
$query = str_replace("\n", '', $buffer);
$this->DB->query($query);
} catch (PDOException $e) {
// drop database on error
$this->DB->query('DROP DATABASE IF EXISTS `' . $this->InstallData->getDbName() . '`;');
$this->rollback();
throw new SPException(SPException::SP_CRITICAL,
sprintf(__('Error al crear la BBDD', false) . ' (%s)', $e->getMessage()),
@@ -399,6 +414,22 @@ class Installer
}
}
/**
* Comprobar la conexión a la BBDD
*
* @throws \SP\Core\Exceptions\SPException
*/
protected function checkConnection()
{
if (!DBUtil::checkDatabaseExist()) {
$this->rollback();
throw new SPException(SPException::SP_CRITICAL,
__('Error al comprobar la base de datos', false),
__('Intente de nuevo la instalación', false));
}
}
/**
* Crear el usuario admin de sysPass.
* Esta función crea el grupo, perfil y usuario 'admin' para utilizar sysPass.
@@ -466,23 +497,4 @@ class Installer
__('Informe al desarrollador', false));
}
}
/**
* Deshacer la instalación en caso de fallo.
* Esta función elimina la base de datos y el usuario de sysPass
*/
private function rollback()
{
try {
$this->DB->query('DROP DATABASE IF EXISTS `' . $this->InstallData->getDbName() . '`;');
$this->DB->query('DROP USER `' . $this->InstallData->getDbUser() . '`@`' . $this->InstallData->getDbAuthHost() . '`;');
$this->DB->query('DROP USER `' . $this->InstallData->getDbUser() . '`@`%`;');
} catch (PDOException $e) {
debugLog($e->getMessage());
return false;
}
return true;
}
}

View File

@@ -121,12 +121,12 @@ class DBUtil
try {
$db = DiFactory::getDBStorage()->getConnection();
$attributes = array(
$attributes = [
'SERVER_VERSION',
'CLIENT_VERSION',
'SERVER_INFO',
'CONNECTION_STATUS',
);
];
foreach ($attributes as $val) {
$dbinfo[$val] = $db->getAttribute(constant('PDO::ATTR_' . $val));

View File

@@ -134,9 +134,10 @@ class MySQLHandler implements DBStorageInterface
}
try {
$opts = [PDO::ATTR_EMULATE_PREPARES => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
$dsn = 'mysql:host=' . $this->dbHost . ';port=' . $this->dbPort . ';dbname=' . $this->dbName . ';charset=utf8';
// $this->db = new PDO($dsn, $dbuser, $dbpass, array(PDO::ATTR_PERSISTENT => true));
$this->db = new PDO($dsn, $this->dbUser, $this->dbPass);
$this->db = new PDO($dsn, $this->dbUser, $this->dbPass, $opts);
// $this->db = new PDO($dsn, $this->dbUser, $this->dbPass);
$this->dbStatus = 0;
} catch (\Exception $e) {
if ($isInstalled) {
@@ -153,9 +154,6 @@ class MySQLHandler implements DBStorageInterface
}
}
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); //FIXME
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $this->db;
}

View File

@@ -56,7 +56,7 @@ class Util
$alphabet = $charsLower . $charsUpper;
if ($useSpecial === true) {
$charsSpecial = '@#$%&/()=?¿!_-:.;,{}[]*^';
$charsSpecial = '@$%&/()!_:.;{}^';
$alphabet .= $charsSpecial;
}

View File

@@ -272,7 +272,7 @@ DROP TABLE IF EXISTS `config`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `config` (
`config_parameter` varchar(50) CHARACTER SET utf8 COLLATE utf8_spanish_ci NOT NULL,
`config_parameter` varchar(50) NOT NULL,
`config_value` varchar(2000) DEFAULT NULL,
UNIQUE KEY `vacParameter` (`config_parameter`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

File diff suppressed because one or more lines are too long

View File

@@ -114,7 +114,7 @@
<input id="dbhost" name="dbhost" type="text" required
class="mdl-textfield__input mdl-color-text--indigo-400"
value="localhost" autocomplete="off" autofocus
pattern="[0-9a-zA-Z._-]+|([0-9]{1,3}.)+">
pattern="([0-9a-zA-Z._-]+|([0-9]{1,3}.)+)(:[0-9]{1,5})?">
<label class="mdl-textfield__label"
for="dbhost"><?php echo __('Servidor BBDD para sysPass'); ?></label>
</div>

View File

@@ -1,5 +1,5 @@
var $jscomp={scope:{},findInternal:function(c,e,k){c instanceof String&&(c=String(c));for(var f=c.length,g=0;g<f;g++){var l=c[g];if(e.call(k,l,g,c))return{i:g,v:l}}return{i:-1,v:void 0}}};$jscomp.defineProperty="function"==typeof Object.defineProperties?Object.defineProperty:function(c,e,k){if(k.get||k.set)throw new TypeError("ES3 does not support getters and setters.");c!=Array.prototype&&c!=Object.prototype&&(c[e]=k.value)};
$jscomp.getGlobal=function(c){return"undefined"!=typeof window&&window===c?c:"undefined"!=typeof global&&null!=global?global:c};$jscomp.global=$jscomp.getGlobal(this);$jscomp.polyfill=function(c,e,k,f){if(e){k=$jscomp.global;c=c.split(".");for(f=0;f<c.length-1;f++){var g=c[f];g in k||(k[g]={});k=k[g]}c=c[c.length-1];f=k[c];e=e(f);e!=f&&null!=e&&$jscomp.defineProperty(k,c,{configurable:!0,writable:!0,value:e})}};
$jscomp.getGlobal=function(c){return"undefined"!=typeof window&&window===c?c:"undefined"!=typeof global?global:c};$jscomp.global=$jscomp.getGlobal(this);$jscomp.polyfill=function(c,e,k,f){if(e){k=$jscomp.global;c=c.split(".");for(f=0;f<c.length-1;f++){var g=c[f];g in k||(k[g]={});k=k[g]}c=c[c.length-1];f=k[c];e=e(f);e!=f&&null!=e&&$jscomp.defineProperty(k,c,{configurable:!0,writable:!0,value:e})}};
$jscomp.polyfill("Array.prototype.find",function(c){return c?c:function(c,k){return $jscomp.findInternal(this,c,k).v}},"es6-impl","es3");
sysPass.Actions=function(c){var e=c.log,k=0,f={doAction:"/ajax/ajax_getContent.php",updateItems:"/ajax/ajax_getItems.php",user:{savePreferences:"/ajax/ajax_userPrefsSave.php",password:"/ajax/ajax_usrpass.php",passreset:"/ajax/ajax_passReset.php"},main:{login:"/ajax/ajax_doLogin.php",install:"/ajax/ajax_install.php",getUpdates:"/ajax/ajax_checkUpds.php"},checks:"/ajax/ajax_checkConnection.php",config:{save:"/ajax/ajax_configSave.php","export":"/ajax/ajax_configSave.php","import":"/ajax/ajax_configSave.php"},
file:"/ajax/ajax_filesMgmt.php",link:"/ajax/ajax_itemSave.php",plugin:"/ajax/ajax_itemSave.php",account:{save:"/ajax/ajax_itemSave.php",saveFavorite:"/ajax/ajax_itemSave.php",request:"/ajax/ajax_itemSave.php",getFiles:"/ajax/ajax_accGetFiles.php",search:"/ajax/ajax_accSearch.php"},appMgmt:{show:"/ajax/ajax_itemShow.php",save:"/ajax/ajax_itemSave.php",search:"/ajax/ajax_itemSearch.php"},eventlog:"/ajax/ajax_eventlog.php",wiki:{show:"/ajax/ajax_wiki.php"},notice:{show:"/ajax/ajax_noticeShow.php",search:"/ajax/ajax_noticeSearch.php"}},

2
js/app-main.min.js vendored
View File

@@ -1,4 +1,4 @@
var $jscomp={scope:{}};$jscomp.defineProperty="function"==typeof Object.defineProperties?Object.defineProperty:function(a,f,c){if(c.get||c.set)throw new TypeError("ES3 does not support getters and setters.");a!=Array.prototype&&a!=Object.prototype&&(a[f]=c.value)};$jscomp.getGlobal=function(a){return"undefined"!=typeof window&&window===a?a:"undefined"!=typeof global&&null!=global?global:a};$jscomp.global=$jscomp.getGlobal(this);$jscomp.SYMBOL_PREFIX="jscomp_symbol_";
var $jscomp={scope:{}};$jscomp.defineProperty="function"==typeof Object.defineProperties?Object.defineProperty:function(a,f,c){if(c.get||c.set)throw new TypeError("ES3 does not support getters and setters.");a!=Array.prototype&&a!=Object.prototype&&(a[f]=c.value)};$jscomp.getGlobal=function(a){return"undefined"!=typeof window&&window===a?a:"undefined"!=typeof global?global:a};$jscomp.global=$jscomp.getGlobal(this);$jscomp.SYMBOL_PREFIX="jscomp_symbol_";
$jscomp.initSymbol=function(){$jscomp.initSymbol=function(){};$jscomp.global.Symbol||($jscomp.global.Symbol=$jscomp.Symbol)};$jscomp.symbolCounter_=0;$jscomp.Symbol=function(a){return $jscomp.SYMBOL_PREFIX+(a||"")+$jscomp.symbolCounter_++};
$jscomp.initSymbolIterator=function(){$jscomp.initSymbol();var a=$jscomp.global.Symbol.iterator;a||(a=$jscomp.global.Symbol.iterator=$jscomp.global.Symbol("iterator"));"function"!=typeof Array.prototype[a]&&$jscomp.defineProperty(Array.prototype,a,{configurable:!0,writable:!0,value:function(){return $jscomp.arrayIterator(this)}});$jscomp.initSymbolIterator=function(){}};$jscomp.arrayIterator=function(a){var f=0;return $jscomp.iteratorPrototype(function(){return f<a.length?{done:!1,value:a[f++]}:{done:!0}})};
$jscomp.iteratorPrototype=function(a){$jscomp.initSymbolIterator();a={next:a};a[$jscomp.global.Symbol.iterator]=function(){return this};return a};$jscomp.array=$jscomp.array||{};$jscomp.iteratorFromArray=function(a,f){$jscomp.initSymbolIterator();a instanceof String&&(a+="");var c=0,g={next:function(){if(c<a.length){var e=c++;return{value:f(e,a[e]),done:!1}}g.next=function(){return{done:!0,value:void 0}};return g.next()}};g[Symbol.iterator]=function(){return g};return g};

View File

@@ -1,5 +1,5 @@
var $jscomp={scope:{},findInternal:function(b,d,e){b instanceof String&&(b=String(b));for(var f=b.length,a=0;a<f;a++){var c=b[a];if(d.call(e,c,a,b))return{i:a,v:c}}return{i:-1,v:void 0}}};$jscomp.defineProperty="function"==typeof Object.defineProperties?Object.defineProperty:function(b,d,e){if(e.get||e.set)throw new TypeError("ES3 does not support getters and setters.");b!=Array.prototype&&b!=Object.prototype&&(b[d]=e.value)};
$jscomp.getGlobal=function(b){return"undefined"!=typeof window&&window===b?b:"undefined"!=typeof global&&null!=global?global:b};$jscomp.global=$jscomp.getGlobal(this);$jscomp.polyfill=function(b,d,e,f){if(d){e=$jscomp.global;b=b.split(".");for(f=0;f<b.length-1;f++){var a=b[f];a in e||(e[a]={});e=e[a]}b=b[b.length-1];f=e[b];d=d(f);d!=f&&null!=d&&$jscomp.defineProperty(e,b,{configurable:!0,writable:!0,value:d})}};
$jscomp.getGlobal=function(b){return"undefined"!=typeof window&&window===b?b:"undefined"!=typeof global?global:b};$jscomp.global=$jscomp.getGlobal(this);$jscomp.polyfill=function(b,d,e,f){if(d){e=$jscomp.global;b=b.split(".");for(f=0;f<b.length-1;f++){var a=b[f];a in e||(e[a]={});e=e[a]}b=b[b.length-1];f=e[b];d=d(f);d!=f&&null!=d&&$jscomp.defineProperty(e,b,{configurable:!0,writable:!0,value:d})}};
$jscomp.polyfill("Array.prototype.find",function(b){return b?b:function(b,e){return $jscomp.findInternal(this,b,e).v}},"es6-impl","es3");
sysPass.Triggers=function(b){var d=b.log,e=function(a){var c={valueField:"id",labelField:"name",searchField:["name"]};a.find(".select-box").each(function(a){var d=$(this);c.plugins=d.hasClass("select-box-deselect")?{clear_selection:{title:b.config().LANG[51]}}:{};if(d.data("onchange")){var g=d.data("onchange").split("/");c.onChange=function(a){if(0<a)if(2===g.length)sysPassApp.actions()[g[0]][g[1]](d);else sysPassApp.actions()[g[0]](d)}}d.selectize(c)});a.find("#allowed_exts").selectize({create:function(a){return{value:a.toUpperCase(),
text:a.toUpperCase()}},createFilter:/^[a-z0-9]{1,4}$/i,plugins:["remove_button"]});a.find("#wikifilter").selectize({create:!0,createFilter:/^[a-z0-9:._-]+$/i,plugins:["remove_button"]})},f=function(){d.info("updateFormHash");var a=$(".form-action[data-hash]");0<a.length&&a.each(function(){var a=$(this);a.attr("data-hash",SparkMD5.hash(a.serialize(),!1))})};return{views:{main:function(){d.info("views:main");$(".btn-menu").click(function(){var a=$(this);"1"===a.attr("data-history-reset")&&b.appRequests().history.reset();