|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: floor12 |
|
5
|
|
|
* Date: 31.12.2017 |
|
6
|
|
|
* Time: 14:45 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace floor12\backup; |
|
10
|
|
|
|
|
11
|
|
|
use Yii; |
|
12
|
|
|
use yii\base\ErrorException; |
|
13
|
|
|
use yii\base\NotSupportedException; |
|
14
|
|
|
use yii\db\Connection; |
|
15
|
|
|
use yii\db\Exception; |
|
16
|
|
|
|
|
17
|
|
|
class Module extends \yii\base\Module |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
public $administratorRoleName = 'admin'; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
public $backupFolder = '@app/backups'; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
public $chmod; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
public $authTokens = []; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var string[] |
|
38
|
|
|
*/ |
|
39
|
|
|
public $binaries = [ |
|
40
|
|
|
'mysql' => '/usr/bin/mysql', |
|
41
|
|
|
'mysqldump' => '/usr/bin/mysqldump', |
|
42
|
|
|
'pg_dump' => '/usr/bin/pg_dump', |
|
43
|
|
|
'pg_restore' => '/usr/bin/pg_restore', |
|
44
|
|
|
'gzip' => '/bin/gzip', |
|
45
|
|
|
'zcat' => '/bin/zcat', |
|
46
|
|
|
'ionice' => '/usr/bin/ionice', |
|
47
|
|
|
'zip' => '/usr/bin/zip', |
|
48
|
|
|
'unzip' => '/usr/bin/unzip', |
|
49
|
|
|
'chmod' => '/bin/chmod', |
|
50
|
|
|
]; |
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
|
|
public $controllerNamespace = 'floor12\backup\controllers'; |
|
55
|
|
|
/** |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
public $backupRootPath; |
|
59
|
|
|
/** |
|
60
|
|
|
* @var string |
|
61
|
|
|
*/ |
|
62
|
|
|
public $connection; |
|
63
|
|
|
/** |
|
64
|
|
|
* @var string |
|
65
|
|
|
*/ |
|
66
|
|
|
public $ionice; |
|
67
|
|
|
/** |
|
68
|
|
|
* @var array |
|
69
|
|
|
*/ |
|
70
|
|
|
public $configs = []; |
|
71
|
|
|
/** |
|
72
|
|
|
* @var string |
|
73
|
|
|
*/ |
|
74
|
|
|
public $adminLayout = '@app/views/layouts/main'; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @inheritdoc |
|
78
|
|
|
*/ |
|
79
|
|
|
public function init() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->backupRootPath = Yii::getAlias($this->backupFolder); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
try { |
|
84
|
|
|
if (!file_exists($this->backupRootPath)) |
|
|
|
|
|
|
85
|
|
|
mkdir($this->backupRootPath); |
|
|
|
|
|
|
86
|
|
|
} catch (ErrorException $e) { |
|
87
|
|
|
throw new ErrorException("Backup folder not exists. Its impossible to create it because of permission error."); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if (!is_writable($this->backupRootPath)) |
|
|
|
|
|
|
91
|
|
|
throw new ErrorException("Backup folder is not writable."); |
|
92
|
|
|
|
|
93
|
|
|
$this->checkSqliteDb(); |
|
94
|
|
|
$this->registerTranslations(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @throws NotSupportedException |
|
99
|
|
|
* @throws Exception |
|
100
|
|
|
*/ |
|
101
|
|
|
public function checkSqliteDb() |
|
102
|
|
|
{ |
|
103
|
|
|
$dbFileName = $this->backupRootPath . '/sqlite.db'; |
|
104
|
|
|
$this->connection = new Connection(['dsn' => 'sqlite:' . $dbFileName]); |
|
|
|
|
|
|
105
|
|
|
$this->connection->getSchema(); |
|
106
|
|
|
$sql = file_get_contents(__DIR__ . '/migration/sqlite.backup.sql'); |
|
107
|
|
|
$this->connection->createCommand($sql)->execute(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Register some lang files |
|
112
|
|
|
* @return void |
|
113
|
|
|
*/ |
|
114
|
|
|
public function registerTranslations() |
|
115
|
|
|
{ |
|
116
|
|
|
Yii::$app->i18n->translations['app.f12.backup'] = [ |
|
117
|
|
|
'class' => 'yii\i18n\PhpMessageSource', |
|
118
|
|
|
'basePath' => '@vendor/floor12/yii2-module-backup/src/messages', |
|
119
|
|
|
'sourceLanguage' => 'en-US', |
|
120
|
|
|
'fileMap' => [ |
|
121
|
|
|
'app.f12.backup' => 'backup.php', |
|
122
|
|
|
], |
|
123
|
|
|
]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param string $config_id |
|
128
|
|
|
* @return bool |
|
129
|
|
|
*/ |
|
130
|
|
|
public function checkConfig(string $config_id) |
|
131
|
|
|
{ |
|
132
|
|
|
foreach (Yii::$app->getModule('backup')->configs as $config) |
|
133
|
|
|
if ($config['id'] == $config_id) |
|
134
|
|
|
return true; |
|
135
|
|
|
return false; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.