2014-07-28 09:23:41 +03:00
|
|
|
<?php
|
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
class InstallStepInstallDb extends InstallStep
|
|
|
|
{
|
2014-07-28 09:23:41 +03:00
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
protected $sConfigPath;
|
2014-07-28 09:23:41 +03:00
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->sConfigPath = dirname(INSTALL_DIR) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.local.php';
|
|
|
|
InstallConfig::$sFileConfig = $this->sConfigPath;
|
|
|
|
}
|
2014-07-28 09:23:41 +03:00
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
/**
|
|
|
|
* Получаем данные для загрузки на форму
|
|
|
|
* Возможные источники: реквест, конфиг, дефолтные значения
|
|
|
|
*
|
|
|
|
* @param $sName
|
|
|
|
* @param null $mDefault
|
|
|
|
* @param bool $bUseHtmlspecialchars
|
|
|
|
*
|
|
|
|
* @return mixed|null|string
|
|
|
|
*/
|
|
|
|
public function getValue($sName, $mDefault = null, $bUseHtmlspecialchars = true)
|
|
|
|
{
|
|
|
|
$mResult = null;
|
|
|
|
$sNameRequest = str_replace('.', '_', $sName);
|
|
|
|
if (isset($_REQUEST[$sNameRequest])) {
|
|
|
|
$mResult = $_REQUEST[$sNameRequest];
|
|
|
|
} else {
|
|
|
|
$mResult = InstallConfig::get($sName, $mDefault);
|
|
|
|
}
|
|
|
|
return $bUseHtmlspecialchars ? htmlspecialchars($mResult) : $mResult;
|
|
|
|
}
|
2014-07-28 09:23:41 +03:00
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
/**
|
|
|
|
* Обработка отправки формы
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function process()
|
|
|
|
{
|
|
|
|
if (!$aRes = $this->processDbCheck()) {
|
|
|
|
return $aRes;
|
|
|
|
}
|
|
|
|
list($oDb, $sEngineDB) = $aRes;
|
|
|
|
/**
|
|
|
|
* Запускаем импорт дампов, сначала GEO DB
|
|
|
|
*/
|
|
|
|
list($bResult, $aErrors) = array_values($this->importDumpDB($oDb, InstallCore::getDataFilePath('sql/geo.sql'),
|
|
|
|
array(
|
|
|
|
'engine' => $sEngineDB,
|
|
|
|
'prefix' => InstallCore::getRequestStr('db.table.prefix'),
|
|
|
|
'check_table' => 'geo_city'
|
|
|
|
)));
|
|
|
|
if ($bResult) {
|
|
|
|
/**
|
|
|
|
* Запускаем основной дамп
|
|
|
|
*/
|
|
|
|
list($bResult, $aErrors) = array_values($this->importDumpDB($oDb,
|
|
|
|
InstallCore::getDataFilePath('sql/dump.sql'), array(
|
|
|
|
'engine' => $sEngineDB,
|
|
|
|
'prefix' => InstallCore::getRequestStr('db.table.prefix'),
|
|
|
|
'check_table' => 'topic'
|
|
|
|
)));
|
|
|
|
if ($bResult) {
|
2016-10-22 16:58:53 +03:00
|
|
|
return true;
|
2014-10-08 11:49:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->addError(join('<br/>', $aErrors));
|
|
|
|
}
|
2014-07-30 09:17:50 +03:00
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
protected function processDbCheck()
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Коннект к серверу БД
|
|
|
|
*/
|
|
|
|
if (!$oDb = $this->getDBConnection(InstallCore::getRequestStr('db.params.host'),
|
|
|
|
InstallCore::getRequestStr('db.params.port'), InstallCore::getRequestStr('db.params.user'),
|
|
|
|
InstallCore::getRequestStr('db.params.pass'))
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Выбор БД
|
|
|
|
*/
|
|
|
|
$sNameDb = InstallCore::getRequestStr('db.params.dbname');
|
|
|
|
if (!InstallCore::getRequest('db_create')) {
|
|
|
|
if (!@mysqli_select_db($oDb, $sNameDb)) {
|
|
|
|
return $this->addError(InstallCore::getLang('steps.installDb.errors.db_not_found'));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* Пытаемся создать БД
|
|
|
|
*/
|
|
|
|
@mysqli_query($oDb,
|
|
|
|
"CREATE DATABASE IF NOT EXISTS `{$sNameDb}` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");
|
|
|
|
if (!@mysqli_select_db($oDb, $sNameDb)) {
|
|
|
|
return $this->addError(InstallCore::getLang('steps.installDb.errors.db_not_create'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Проверяем корректность префикса таблиц
|
|
|
|
*/
|
|
|
|
if (!preg_match('#^[a-z0-9\_]*$#i', InstallCore::getRequestStr('db.table.prefix'))) {
|
|
|
|
return $this->addError(InstallCore::getLang('steps.installDb.errors.db_table_prefix'));
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Определяем поддержку InnoDB
|
|
|
|
*/
|
|
|
|
$sEngineDB = 'MyISAM';
|
|
|
|
if ($aRes = @mysqli_query($oDb, 'SHOW ENGINES')) {
|
|
|
|
while ($aRow = mysqli_fetch_assoc($aRes)) {
|
|
|
|
if (strtoupper($aRow['Engine']) == 'INNODB' and in_array(strtoupper($aRow['Support']),
|
|
|
|
array('DEFAULT', 'YES'))
|
|
|
|
) {
|
|
|
|
$sEngineDB = 'InnoDB';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-28 10:43:39 +03:00
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
$sPathRootWeb = $this->getPathRootWeb();
|
|
|
|
$aDirs = array();
|
|
|
|
$sDirs = trim(str_replace('http://' . $_SERVER['HTTP_HOST'], '', $sPathRootWeb), '/');
|
|
|
|
if ($sDirs != '') {
|
|
|
|
$aDirs = explode('/', $sDirs);
|
|
|
|
}
|
2014-07-28 10:43:39 +03:00
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
/**
|
|
|
|
* Прописываем параметры в конфиг
|
|
|
|
*/
|
|
|
|
$aSave = array(
|
|
|
|
'db.params.host' => InstallCore::getRequestStr('db.params.host'),
|
|
|
|
'db.params.port' => InstallCore::getRequestStr('db.params.port'),
|
|
|
|
'db.params.dbname' => InstallCore::getRequestStr('db.params.dbname'),
|
|
|
|
'db.params.user' => InstallCore::getRequestStr('db.params.user'),
|
|
|
|
'db.params.pass' => InstallCore::getRequestStr('db.params.pass'),
|
|
|
|
'db.table.prefix' => InstallCore::getRequestStr('db.table.prefix'),
|
|
|
|
'db.tables.engine' => $sEngineDB,
|
|
|
|
'path.root.web' => $sPathRootWeb,
|
|
|
|
'path.offset_request_url' => count($aDirs),
|
|
|
|
);
|
|
|
|
if (!InstallConfig::save($aSave)) {
|
|
|
|
return $this->addError(InstallConfig::$sLastError);
|
|
|
|
}
|
|
|
|
return array($oDb, $sEngineDB);
|
|
|
|
}
|
2014-07-28 09:23:41 +03:00
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
protected function getPathRootWeb()
|
|
|
|
{
|
|
|
|
$sPath = rtrim('http://' . $_SERVER['HTTP_HOST'], '/') . str_replace('/install/index.php', '',
|
|
|
|
$_SERVER['PHP_SELF']);
|
|
|
|
return preg_replace('#\/application$#', '', $sPath);
|
|
|
|
}
|
2014-07-28 09:23:41 +03:00
|
|
|
}
|