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
|
|
|
|
21
|
|
|
public function before() |
22
|
|
|
{ |
23
|
|
|
$this->db['charset'] = 'utf8'; |
24
|
|
|
$this->db['collation'] = 'utf8_unicode_ci'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Labels for installation form |
29
|
|
|
*/ |
30
|
|
|
public function labels() |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
'db.driver' => __('Database type'), |
34
|
|
|
'db.host' => __('Host'), |
35
|
|
|
'db.username' => __('User name'), |
36
|
|
|
'db.password' => __('User password'), |
37
|
|
|
'db.database' => __('Database name'), |
38
|
|
|
'db.prefix' => __('Table prefix'), |
39
|
|
|
'email' => __('SendFrom email'), |
40
|
|
|
'singleLanguage' => __('Default language'), |
41
|
|
|
'multiLanguage' => __('Multi language'), |
42
|
|
|
'user.login' => __('Login'), |
43
|
|
|
'user.email' => __('Email'), |
44
|
|
|
'user.password' => __('Password') |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Installation post data validation |
50
|
|
|
*/ |
51
|
|
|
public function rules() |
52
|
|
|
{ |
53
|
|
|
return [ |
54
|
|
|
[['db.driver', 'db.host', 'db.username', 'db.password', 'db.database', 'db.prefix', 'email', 'singleLanguage'], 'required'], |
55
|
|
|
[['user.login', 'user.email', 'user.password'], 'required'], |
56
|
|
|
[['user.login', 'user.password'], 'length_min', 4], |
57
|
|
|
['user.email', 'email'], |
58
|
|
|
['multiLanguage', 'used'], |
59
|
|
|
['db.driver', 'in', ['mysql', 'pgsql', 'sqlite']], |
60
|
|
|
['email', 'email'], |
61
|
|
|
['singleLanguage', 'in', App::$Translate->getAvailableLangs()], |
62
|
|
|
['db', 'Apps\Model\Install\Main\FormInstall::filterCheckDb'] |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function make() |
67
|
|
|
{ |
68
|
|
|
// prepare configurations to save |
69
|
|
|
$cfg = App::$Properties->getAll('default'); |
70
|
|
|
$cfg['database'] = $this->db; |
71
|
|
|
$cfg['adminEmail'] = $this->email; |
72
|
|
|
$cfg['singleLanguage'] = $this->singleLanguage; |
73
|
|
|
$cfg['multiLanguage'] = (bool)$this->multiLanguage; |
74
|
|
|
$cfg['passwordSalt'] = '$2a$07$' . Str::randomLatinNumeric(mt_rand(21, 30)) . '$'; |
75
|
|
|
$cfg['debug']['cookie']['key'] = 'fdebug_' . Str::randomLatinNumeric(mt_rand(4, 16)); |
76
|
|
|
$cfg['debug']['cookie']['value'] = Str::randomLatinNumeric(mt_rand(32, 128)); |
77
|
|
|
|
78
|
|
|
// import database tables |
79
|
|
|
$connectName = 'install'; |
80
|
|
|
include(root . '/Private/Database/install.php'); |
81
|
|
|
|
82
|
|
|
// insert admin user |
83
|
|
|
$user = new User(); |
84
|
|
|
$user->setConnection('install'); |
85
|
|
|
$user->login = $this->user['login']; |
|
|
|
|
86
|
|
|
$user->email = $this->user['email']; |
|
|
|
|
87
|
|
|
$user->role_id = 3; |
|
|
|
|
88
|
|
|
$user->password = App::$Security->password_hash($this->user['password'], $cfg['passwordSalt']); |
|
|
|
|
89
|
|
|
$user->save(); |
90
|
|
|
|
91
|
|
|
$profile = new Profile(); |
92
|
|
|
$profile->setConnection('install'); |
93
|
|
|
$profile->user_id = $user->id; |
|
|
|
|
94
|
|
|
$profile->save(); |
95
|
|
|
|
96
|
|
|
// write config data |
97
|
|
|
App::$Properties->writeConfig('default', $cfg); |
|
|
|
|
98
|
|
|
File::write('/Private/Install/install.lock', 'Installation is locked!'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Check database connection filter |
104
|
|
|
* @param array $cfg |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function filterCheckDb($cfg = []) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
App::$Database->addConnection($this->db, 'install'); |
110
|
|
|
|
111
|
|
|
try { |
112
|
|
|
App::$Database->connection('install')->getDatabaseName(); |
113
|
|
|
} catch (\Exception $e) { |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
} |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.