getResponse();
$response->content = Yii::t('install', "Has been installed, if you want to reinstall please remove {lock_file} and try it again", ['lock_file'=>self::$installLockFile]);
$response->send();
exit(0);
}
}
public static function getIsInstalled()
{
return file_exists(self::$installLockFile);
}
public function actions()
{
return [
'error' => [
'class' => ErrorAction::className(),
],
];
}
public function actionError()
{
return $this->render('error');
}
public function actionIndex()
{
return $this->redirect(['choose-language']);
}
public function actionChooseLanguage()
{
return $this->render('choose-language');
}
public function actionAccept()
{
return $this->render('accept');
}
public function actionCheckEnvironment()
{
if (!isset($_SESSION['_install_env_passed']) || $_SESSION['_install_env_passed'] != 1) {
if (!in_array(Yii::$app->getRequest()->headers['referer'], [
Url::to(['accept'], true),
Url::to(['check-environment'], true)
])
) {
return $this->redirect(['accept']);
};
}
$data = array();
$data['phpversion'] = @phpversion();
$data['os'] = PHP_OS;
$tmp = function_exists('gd_info') ? gd_info() : array();
$max_execution_time = ini_get('max_execution_time');
$allow_reference = (ini_get('allow_call_time_pass_reference') ? '[√]On' : '[×]Off');
$allow_url_fopen = (ini_get('allow_url_fopen') ? '[√]On' : '[×]Off');
$safe_mode = (ini_get('safe_mode') ? '[×]On' : '[√]Off');
$err = 0;
if (!version_compare($data['phpversion'], '5.4', '<')) {
$data['phpversion'] = ' ' . Yii::t('install', 'Yes') . ' ' . $data['phpversion'];
} else {
$data['phpversion'] = ' ' . Yii::t('install', 'No') . ' ' . $data['phpversion'];
$err++;
}
if (empty($tmp['GD Version'])) {
$gd = '[×]Off';
if (!extension_loaded('imagick')){
$err++;
}
} else {
$gd = '[√]On ' . $tmp['GD Version'];
}
if (class_exists('pdo')) {
$data['pdo'] = ' ' . Yii::t('install', 'Yes');
} else {
$data['pdo'] = ' ' . Yii::t('install', 'No');
$err++;
}
/*if (extension_loaded('pdo_mysql')) {
$data['pdo_mysql'] = ' ' . Yii::t('install', 'Yes');
} else {
$data['pdo_mysql'] = ' ' . Yii::t('install', 'No');
$err++;
}*/
if (extension_loaded('curl')) {
$data['curl'] = ' ' . Yii::t('install', 'Yes');
} else {
$data['curl'] = ' ' . Yii::t('install', 'No');
$err++;
}
if (extension_loaded('gd')) {
$data['gd'] = ' ' . Yii::t('install', 'Yes');
} else {
$data['gd'] = ' ' . Yii::t('install', 'No');
if (function_exists('imagettftext')) {
$data['gd'] .= '
FreeType Support ' . Yii::t('install', 'No');
}
$err++;
}
if (extension_loaded('json')) {
$data['json'] = ' ' . Yii::t('install', 'Yes');
} else {
$data['json'] = ' ' . Yii::t('install', 'No');
$err++;
}
if (extension_loaded('mbstring')) {
$data['mbstring'] = ' ' . Yii::t('install', 'Yes');
} else {
$data['mbstring'] = ' ' . Yii::t('install', 'No');
$err++;
}
if (ini_get('file_uploads')) {
$data['upload_size'] = ' ' . ini_get('upload_max_filesize');
} else {
$data['upload_size'] = ' ' . Yii::t('install', 'Forbidden');
}
if (function_exists('session_start')) {
$data['session'] = ' ' . Yii::t('install', 'Yes');
} else {
$data['session'] = ' ' . Yii::t('install', 'No');
$err++;
}
$folders = array(
'@frontend/runtime',
'@frontend/web/assets',
'@backend/runtime',
'@frontend/web/admin/assets',
);
$newFolders = array();
foreach ($folders as &$dir) {
$dir = Yii::getAlias($dir);
if (is_writable($dir)) {
$newFolders[$dir]['w'] = true;
} else {
$newFolders[$dir]['w'] = false;
$err++;
}
if (is_readable($dir)) {
$newFolders[$dir]['r'] = true;
} else {
$newFolders[$dir]['r'] = false;
$err++;
}
}
$data['folders'] = $newFolders;
$_SESSION['_install_env_passed'] = 0;
if ($err == 0) {
$_SESSION['_install_env_passed'] = 1;
}
$data['err'] = $err;
return $this->render('check-environment', $data);
}
public function actionSetinfo()
{
set_time_limit(300);
if (! isset($_SESSION['_install_env_passed']) || $_SESSION['_install_env_passed'] != 1) {
$url = Url::to(['check-environment']);
echo "";
exit(0);
}
if (Yii::$app->getRequest()->getIsPost()) {
$this->on(self::EVENT_AFTER_ACTION, function () {
$request = Yii::$app->getRequest();
Yii::$app->getResponse()->format = Response::FORMAT_JSON;
try {
$db = $this->_getDbConnection();
Yii::$app->set("db", $db);
$tables = new Tables(['db' => Yii::$app->db]);
$tables->importDatabase();
//更新配置信息
$data = [
'username' => $request->post('manager', 'admin'),
'password_hash' => Yii::$app->security->generatePasswordHash($request->post('manager_pwd')),
'email' => $request->post('manager_email'),
];
Yii::$app->getDb()->createCommand()->update(AdminUser::tableName(), $data, 'id = 1')->execute();
$model = Options::findOne(['name' => 'website_title']);
$model->value = $request->post('sitename', 'Feehi CMS');
$model->save(false);
$model = Options::findOne(['name' => 'website_url']);
$model->value = $request->post('website_url', '');
$model->save(false);
$model = Options::findOne(['name' => 'website_url']);
$model->value = $request->post('siteurl', '');
$model->save(false);
$model = Options::findOne(['name' => 'seo_keywords']);
$model->value = $request->post('sitekeywords', '');
$model->save(false);
$model = Options::findOne(['name' => 'seo_description']);
$model->value = $request->post('siteinfo', '');
$model->save(false);
}catch (Exception $e){
echo str_repeat(" ", 1024 * 64 * 99);
$msg = str_replace(array("\r\n", "\r", "\n"), "", $e->getMessage());
$msg = str_replace('"', '', $msg);
$msg = str_replace("'", '', $msg);
echo "";
ob_flush();
flush();
exit(0);
}
$_SESSION["_install_setinfo"] = 1;
$configFile = yii::getAlias("@common/config/main-local.php");
$array = require $configFile;
$array['components']['db'] = [
'class' => yii\db\Connection::className(),
'dsn' => $db->dsn,
'username' => $db->username,
'password' => $db->password,
'charset' => 'utf8',
'tablePrefix' => $db->tablePrefix,
];
$str = "alert('{$message}');location.href='" . Url::to(['success']) . "';";
}else {
sleep(1);
echo "";
}
exit(0);
});
$html = $this->render('installing');
echo $html;
flush();
if(ob_get_level() > 0){
ob_flush();
}
} else {
return $this->render('setinfo');
}
}
public function actionCreateDatabase()
{
Yii::$app->getResponse()->format = Response::FORMAT_JSON;
try {
$this->_getDbConnection();
return ['message' => ''];
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
private function _getDbConnection()
{
$request = Yii::$app->getRequest();
$dbtype = $request->post('dbtype', 'mysql');
$dbhost = $request->post('dbhost', 'dbhost');
$dbuser = $request->post('dbuser', 'root');
$dbpassword = $request->post('dbpw', '');
$dbport = $request->post('dbport', '3306');
$dbname = $request->post('dbname', '');
$tablePrefix = $request->post("dbprefix", '');
$dsn = '';
switch ($dbtype) {
case "pgsql":
case "mysql":
$dsn = $dbtype . ":host=" . $dbhost . ';port=' . $dbport;
if( !empty($dbname) ) $dsn .= ";dbname=" . $dbname;
break;
case "sqlite":
$path = Yii::getAlias($dbhost);
$dsn = "sqlite:$path";
break;
}
$db = new Connection([
'dsn' => $dsn,
'username' => $dbuser,
'password' => $dbpassword,
'charset' => 'utf8',
'tablePrefix' => $tablePrefix,
]);
$this->checkAccountPermission($db, $dbtype, $dbname);
return $db;
}
public function actionSuccess()
{
if (isset($_SESSION["_install_setinfo"]) && $_SESSION["_install_setinfo"] == 1) {
if( !touch(Yii::getAlias(self::$installLockFile)) ){
$message = Yii::t("install", "Touch install lock file {lock_file} failed,please touch file handled", ['lock_file'=>self::$installLockFile]);
echo "";
}
session_destroy();
return $this->render("success");
} else {
return $this->redirect(['setinfo']);
}
}
public function actionLanguage()
{
$language = Yii::$app->getRequest()->get('lang');
if (isset($language)) {
Yii::$app->session['language'] = $language;
}
return $this->redirect(['accept']);
}
private function checkAccountPermission(Connection $db, $dbtype, $dbname)
{
if($dbtype != "sqlite") {
try {
if($dbtype === "pgsql"){
}else {
$db->createCommand("use $dbname")->execute();
}
}catch (\yii\db\Exception $e){
if( $e->getCode() == 1049 ) {
$request = Yii::$app->getRequest();
$dbhost = $request->post('dbhost', 'dbhost');
$dbport = $request->post('dbport', '3306');
$dsn = $dbtype . ":host=" . $dbhost . ';port=' . $dbport;
$db->dsn = $dsn;
$db->pdo = null;
$db->open();
$result = $db->createCommand("CREATE DATABASE IF NOT EXISTS `{$dbname}` DEFAULT CHARACTER SET utf8")->execute();
if ($result == 1) {
$this->checkAccountPermission($db, $dbtype, $dbname);
return;
} else {
throw new Exception(Yii::t('install', 'Create database error, please create yourself and retry'));
}
}else{
throw new Exception($e->getMessage());
}
}
$db->createCommand("create table test(id integer)")->execute();
$db->createCommand("insert into test values(1)")->execute();
$result = $db->createCommand("select * from test where id=1")->queryOne();
if ($result === false) {
throw new Exception(Yii::t('install', 'Access to database `{database}` error. Maybe permission denied', ['database' => $dbname]));
}
try {
$db->createCommand("drop table test")->execute();
} catch (Exception $exception) {
Yii::error("after install feehicms delete test database table `test` error:" . $exception->getFile() . "(" . $exception->getLine() . ")" . $exception->getMessage());
}
}
}
}