Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Apps/Model/Install/Main/FormInstall.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Apps\Model\Install\Main;
4
5
use Apps\ActiveRecord\Profile;
6
use Apps\ActiveRecord\System;
7
use Apps\ActiveRecord\User;
8
use Extend\Version;
9
use Ffcms\Core\App;
10
use Ffcms\Core\Arch\Model;
11
use Ffcms\Core\Helper\Crypt;
12
use Ffcms\Core\Helper\FileSystem\File;
13
use Ffcms\Core\Managers\MigrationsManager;
14
15
/**
16
 * Class FormInstall. System installation business logic model
17
 * @package Apps\Model\Install\Main
18
 */
19
class FormInstall extends Model
20
{
21
    public $db = [];
22
    public $mail = [];
23
    public $multiLanguage;
24
    public $singleLanguage;
25
    public $user = [];
26
    public $mainpage;
27
28
    public $baseDomain;
29
30
    public $_name = 'formInstall';
31
32
    /**
33
     * Set default data
34
     */
35
    public function before()
36
    {
37
        $this->db['charset'] = 'utf8';
38
        $this->db['collation'] = 'utf8_unicode_ci';
39
40
        $this->baseDomain = App::$Request->getHttpHost();
41
    }
42
43
    /**
44
     * Labels for installation form
45
     * @return array
46
     */
47
    public function labels(): array
48
    {
49
        return [
50
            'db.driver' => __('Database type'),
51
            'db.host' => __('Host'),
52
            'db.username' => __('User name'),
53
            'db.password' => __('User password'),
54
            'db.database' => __('Database name'),
55
            'db.prefix' => __('Table prefix'),
56
            'mail.enable' => __('Use mail features'),
57
            'mail.host' => __('Host'),
58
            'mail.port' => __('Port'),
59
            'mail.encrypt' => __('Encryption'),
60
            'mail.user' => __('User'),
61
            'mail.password' => __('Password'),
62
            'singleLanguage' => __('Default language'),
63
            'multiLanguage' => __('Multi language'),
64
            'user.login' => __('Login'),
65
            'user.email' => __('Email'),
66
            'user.password' => __('Password'),
67
            'user.repassword' => __('Repeat password'),
68
            'mainpage' => __('Main page')
69
        ];
70
    }
71
72
    /**
73
     * Installation post data validation rules
74
     * @return array
75
     */
76
    public function rules(): array
77
    {
78
        return [
79
            [['db.driver', 'db.host', 'db.username', 'db.password', 'db.database', 'db.prefix', 'singleLanguage', 'mainpage'], 'required'],
80
            [['user.login', 'user.email', 'user.password', 'user.repassword'], 'required'],
81
            ['mail.enable', 'required'],
82
            ['mail.enable', 'int'],
83
            [['mail.host', 'mail.port', 'mail.user', 'main.encrypt', 'mail.password'], 'used'],
84
            ['mail.user', 'email'],
85
            ['mail.port', 'int'],
86
            ['mail.encrypt', 'in', ['ssl', 'tls', 'none']],
87
            ['mainpage', 'in', ['none', 'news', 'about']],
88
            [['user.login', 'user.password'], 'length_min', 4],
89
            ['user.repassword', 'equal', $this->getRequest('user.password', $this->getSubmitMethod())],
90
            ['user.email', 'email'],
91
            ['multiLanguage', 'used'],
92
            ['db.driver', 'in', ['mysql', 'pgsql', 'sqlite']],
93
            ['singleLanguage', 'in', App::$Translate->getAvailableLangs()],
94
            ['db', 'Apps\Model\Install\Main\FormInstall::filterCheckDb']
95
        ];
96
    }
97
98
    /**
99
     * Save configurations build by installer interface
100
     */
101
    public function make()
102
    {
103
        // prepare configurations to save
104
        /** @var array $cfg */
105
        $cfg = App::$Properties->getAll('default');
106
        $this->before();
107
        $cfg['baseDomain'] = $this->baseDomain;
108
        $cfg['database'] = $this->db;
109
        $cfg['singleLanguage'] = $this->singleLanguage;
110
        $cfg['multiLanguage'] = (bool)$this->multiLanguage;
111
        $cfg['debug']['cookie']['key'] = 'fdebug_' . Crypt::randomString(mt_rand(4, 16));
112
        $cfg['debug']['cookie']['value'] = Crypt::randomString(mt_rand(32, 128));
113
        $cfg['mail'] = $this->mail;
114
115
        // initialize migrations table
116
        App::$Database->getConnection('install')->getSchemaBuilder()->create('migrations', function ($table) {
117
            $table->increments('id');
118
            $table->string('migration', 128)->unique();
119
            $table->timestamps();
120
        });
121
122
        // import migrations
123
        $manager = new MigrationsManager(null, 'install');
124
        $search = $manager->search(null, false);
125
        $manager->makeUp($search);
0 ignored issues
show
$search of type false is incompatible with the type array|string expected by parameter $file of Ffcms\Core\Managers\MigrationsManager::makeUp(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
        $manager->makeUp(/** @scrutinizer ignore-type */ $search);
Loading history...
126
127
        // insert admin user
128
        $user = new User();
129
        $user->setConnection('install');
130
        $user->login = $this->user['login'];
131
        $user->email = $this->user['email'];
132
        $user->role_id = 4;
133
        $user->password = Crypt::passwordHash($this->user['password']);
134
        $user->save();
135
136
        $profile = new Profile();
137
        $profile->setConnection('install');
138
        $profile->user_id = $user->id;
139
        $profile->save();
140
141
        // set installation version
142
        $system = new System();
143
        $system->setConnection('install');
144
        $system->var = 'version';
145
        $system->data = Version::VERSION;
146
        $system->save();
147
148
        // write config data
149
        App::$Properties->writeConfig('default', $cfg);
150
        // make routing configs based on preset property
151
        $routing = [];
152
        switch ($this->mainpage) {
153
            case 'news':
154
                $routing = [
155
                    'Alias' => [
156
                        'Front' => [
157
                            '/' => '/content/list/news',
158
                            '/about' => '/content/read/page/about-page'
159
                        ]
160
                    ]
161
                ];
162
                break;
163
            case 'about':
164
                $routing = [
165
                    'Alias' => [
166
                        'Front' => [
167
                            '/' => '/content/read/page/about-page'
168
                        ]
169
                    ]
170
                ];
171
                break;
172
        }
173
        // write routing configurations
174
        App::$Properties->writeConfig('routing', $routing);
175
        // write installer lock
176
        File::write('/Private/Install/install.lock', 'Installation is locked!');
177
    }
178
179
180
    /**
181
     * Check database connection filter
182
     * @return bool
183
     */
184
    public function filterCheckDb()
185
    {
186
        App::$Database->addConnection($this->db, 'install');
187
188
        try {
189
            App::$Database->getConnection('install')->getDatabaseName();
190
        } catch (\Exception $e) {
191
            return false;
192
        }
193
194
        return true;
195
    }
196
}
197