|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Apps\Model\Install\Main; |
|
4
|
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\Profile; |
|
6
|
|
|
use Apps\ActiveRecord\User; |
|
7
|
|
|
use Apps\Controller\Console\Db; |
|
8
|
|
|
use Ffcms\Core\App; |
|
9
|
|
|
use Ffcms\Core\Arch\Model; |
|
10
|
|
|
use Ffcms\Core\Helper\FileSystem\File; |
|
11
|
|
|
use Ffcms\Core\Helper\Type\Str; |
|
12
|
|
|
|
|
13
|
|
|
class FormInstall extends Model |
|
14
|
|
|
{ |
|
15
|
|
|
public $db = []; |
|
16
|
|
|
public $email; |
|
17
|
|
|
public $multiLanguage; |
|
18
|
|
|
public $singleLanguage; |
|
19
|
|
|
public $user = []; |
|
20
|
|
|
public $mainpage; |
|
21
|
|
|
|
|
22
|
|
|
public function before() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->db['charset'] = 'utf8'; |
|
25
|
|
|
$this->db['collation'] = 'utf8_unicode_ci'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Labels for installation form |
|
30
|
|
|
*/ |
|
31
|
|
|
public function labels() |
|
32
|
|
|
{ |
|
33
|
|
|
return [ |
|
34
|
|
|
'db.driver' => __('Database type'), |
|
35
|
|
|
'db.host' => __('Host'), |
|
36
|
|
|
'db.username' => __('User name'), |
|
37
|
|
|
'db.password' => __('User password'), |
|
38
|
|
|
'db.database' => __('Database name'), |
|
39
|
|
|
'db.prefix' => __('Table prefix'), |
|
40
|
|
|
'email' => __('SendFrom email'), |
|
41
|
|
|
'singleLanguage' => __('Default language'), |
|
42
|
|
|
'multiLanguage' => __('Multi language'), |
|
43
|
|
|
'user.login' => __('Login'), |
|
44
|
|
|
'user.email' => __('Email'), |
|
45
|
|
|
'user.password' => __('Password'), |
|
46
|
|
|
'user.repassword' => __('Repeat password'), |
|
47
|
|
|
'mainpage' => __('Main page') |
|
48
|
|
|
]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Installation post data validation |
|
53
|
|
|
*/ |
|
54
|
|
|
public function rules() |
|
55
|
|
|
{ |
|
56
|
|
|
return [ |
|
57
|
|
|
[['db.driver', 'db.host', 'db.username', 'db.password', 'db.database', 'db.prefix', 'email', 'singleLanguage', 'mainpage'], 'required'], |
|
58
|
|
|
[['user.login', 'user.email', 'user.password', 'user.repassword'], 'required'], |
|
59
|
|
|
['mainpage', 'in', ['none', 'news', 'about']], |
|
60
|
|
|
[['user.login', 'user.password'], 'length_min', 4], |
|
61
|
|
|
['user.repassword', 'equal', $this->getRequest('user.password', $this->getSubmitMethod())], |
|
62
|
|
|
['user.email', 'email'], |
|
63
|
|
|
['multiLanguage', 'used'], |
|
64
|
|
|
['db.driver', 'in', ['mysql', 'pgsql', 'sqlite']], |
|
65
|
|
|
['email', 'email'], |
|
66
|
|
|
['singleLanguage', 'in', App::$Translate->getAvailableLangs()], |
|
67
|
|
|
['db', 'Apps\Model\Install\Main\FormInstall::filterCheckDb'] |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Save configurations build by installer interface |
|
73
|
|
|
*/ |
|
74
|
|
|
public function make() |
|
75
|
|
|
{ |
|
76
|
|
|
// prepare configurations to save |
|
77
|
|
|
/** @var array $cfg */ |
|
78
|
|
|
$cfg = App::$Properties->getAll('default'); |
|
79
|
|
|
$this->before(); |
|
80
|
|
|
$cfg['database'] = $this->db; |
|
81
|
|
|
$cfg['adminEmail'] = $this->email; |
|
82
|
|
|
$cfg['singleLanguage'] = $this->singleLanguage; |
|
83
|
|
|
$cfg['multiLanguage'] = (bool)$this->multiLanguage; |
|
84
|
|
|
$cfg['passwordSalt'] = '$2a$07$' . Str::randomLatinNumeric(mt_rand(21, 30)) . '$'; |
|
85
|
|
|
$cfg['debug']['cookie']['key'] = 'fdebug_' . Str::randomLatinNumeric(mt_rand(4, 16)); |
|
86
|
|
|
$cfg['debug']['cookie']['value'] = Str::randomLatinNumeric(mt_rand(32, 128)); |
|
87
|
|
|
|
|
88
|
|
|
// import database tables |
|
89
|
|
|
$connectName = 'install'; |
|
90
|
|
|
include(root . '/Private/Database/install.php'); |
|
91
|
|
|
|
|
92
|
|
|
// insert admin user |
|
93
|
|
|
$user = new User(); |
|
94
|
|
|
$user->setConnection('install'); |
|
95
|
|
|
$user->login = $this->user['login']; |
|
96
|
|
|
$user->email = $this->user['email']; |
|
97
|
|
|
$user->role_id = 4; |
|
98
|
|
|
$user->password = App::$Security->password_hash($this->user['password'], $cfg['passwordSalt']); |
|
99
|
|
|
$user->save(); |
|
100
|
|
|
|
|
101
|
|
|
$profile = new Profile(); |
|
102
|
|
|
$profile->setConnection('install'); |
|
103
|
|
|
$profile->user_id = $user->id; |
|
104
|
|
|
$profile->save(); |
|
105
|
|
|
|
|
106
|
|
|
// write config data |
|
107
|
|
|
App::$Properties->writeConfig('default', $cfg); |
|
108
|
|
|
// make routing configs based on preset property |
|
109
|
|
|
$routing = []; |
|
110
|
|
|
switch ($this->mainpage) { |
|
111
|
|
|
case 'news': |
|
112
|
|
|
$routing = [ |
|
113
|
|
|
'Alias' => [ |
|
114
|
|
|
'Front' => [ |
|
115
|
|
|
'/' => '/content/list/news', |
|
116
|
|
|
'/about' => '/content/read/page/about-page' |
|
117
|
|
|
] |
|
118
|
|
|
] |
|
119
|
|
|
]; |
|
120
|
|
|
break; |
|
121
|
|
|
case 'about': |
|
122
|
|
|
$routing = [ |
|
123
|
|
|
'Alias' => [ |
|
124
|
|
|
'Front' => [ |
|
125
|
|
|
'/' => '/content/read/page/about-page' |
|
126
|
|
|
] |
|
127
|
|
|
] |
|
128
|
|
|
]; |
|
129
|
|
|
break; |
|
130
|
|
|
} |
|
131
|
|
|
// write routing configurations |
|
132
|
|
|
App::$Properties->writeConfig('routing', $routing); |
|
133
|
|
|
// write installer lock |
|
134
|
|
|
File::write('/Private/Install/install.lock', 'Installation is locked!'); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Check database connection filter |
|
140
|
|
|
* @param array $cfg |
|
141
|
|
|
* @return bool |
|
142
|
|
|
*/ |
|
143
|
|
|
public function filterCheckDb($cfg = []) |
|
|
|
|
|
|
144
|
|
|
{ |
|
145
|
|
|
App::$Database->addConnection($this->db, 'install'); |
|
146
|
|
|
|
|
147
|
|
|
try { |
|
148
|
|
|
App::$Database->getConnection('install')->getDatabaseName(); |
|
|
|
|
|
|
149
|
|
|
} catch (\Exception $e) { |
|
150
|
|
|
return false; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return true; |
|
154
|
|
|
} |
|
155
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.