Passed
Push — master ( 6084dc...68443e )
by Mihail
05:40
created

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

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
        $this->before();
71
        $cfg['database'] = $this->db;
72
        $cfg['adminEmail'] = $this->email;
73
        $cfg['singleLanguage'] = $this->singleLanguage;
74
        $cfg['multiLanguage'] = (bool)$this->multiLanguage;
75
        $cfg['passwordSalt'] = '$2a$07$' . Str::randomLatinNumeric(mt_rand(21, 30)) . '$';
76
        $cfg['debug']['cookie']['key'] = 'fdebug_' . Str::randomLatinNumeric(mt_rand(4, 16));
77
        $cfg['debug']['cookie']['value'] = Str::randomLatinNumeric(mt_rand(32, 128));
78
79
        // import database tables
80
        $connectName = 'install';
81
        include(root . '/Private/Database/install.php');
82
83
        // insert admin user
84
        $user = new User();
85
        $user->setConnection('install');
86
        $user->login = $this->user['login'];
87
        $user->email = $this->user['email'];
88
        $user->role_id = 4;
89
        $user->password = App::$Security->password_hash($this->user['password'], $cfg['passwordSalt']);
90
        $user->save();
91
92
        $profile = new Profile();
93
        $profile->setConnection('install');
94
        $profile->user_id = $user->id;
95
        $profile->save();
96
97
        // write config data
98
        App::$Properties->writeConfig('default', $cfg);
0 ignored issues
show
It seems like $cfg defined by \Ffcms\Core\App::$Properties->getAll('default') on line 69 can also be of type boolean; however, Ffcms\Core\Properties::writeConfig() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
99
        File::write('/Private/Install/install.lock', 'Installation is locked!');
100
    }
101
102
103
    /**
104
     * Check database connection filter
105
     * @param array $cfg
106
     * @return bool
107
     */
108
    public function filterCheckDb($cfg = [])
109
    {
110
        App::$Database->addConnection($this->db, 'install');
111
112
        try {
113
            App::$Database->getConnection('install')->getDatabaseName();
114
        } catch (\Exception $e) {
115
            return false;
116
        }
117
118
        return true;
119
    }
120
}