1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: floor12 |
5
|
|
|
* Date: 17.09.2018 |
6
|
|
|
* Time: 23:07 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace floor12\backup\controllers; |
10
|
|
|
|
11
|
|
|
use floor12\backup\logic\BackupCreate; |
12
|
|
|
use floor12\backup\logic\BackupRestore; |
13
|
|
|
use floor12\backup\models\Backup; |
14
|
|
|
use floor12\backup\models\BackupFilter; |
15
|
|
|
use Throwable; |
16
|
|
|
use Yii; |
17
|
|
|
use yii\base\InvalidConfigException; |
18
|
|
|
use yii\db\StaleObjectException; |
19
|
|
|
use yii\filters\ContentNegotiator; |
20
|
|
|
use yii\filters\VerbFilter; |
21
|
|
|
use yii\web\BadRequestHttpException; |
22
|
|
|
use yii\web\Controller; |
23
|
|
|
use yii\web\ForbiddenHttpException; |
24
|
|
|
use yii\web\NotFoundHttpException; |
25
|
|
|
use yii\web\Response; |
26
|
|
|
|
27
|
|
|
class ApiController extends Controller |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
const HEADER_NAME = 'Backup-Auth-Token'; |
31
|
|
|
/** |
32
|
|
|
* @var Backup |
33
|
|
|
*/ |
34
|
|
|
protected $model; |
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $successResponse = [ |
39
|
|
|
'result' => 'success' |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritDoc |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function behaviors() |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
'contentNegotiator' => [ |
50
|
|
|
'class' => ContentNegotiator::class, |
51
|
|
|
'formats' => [ |
52
|
|
|
'application/json' => Response::FORMAT_JSON, |
53
|
|
|
], |
54
|
|
|
], |
55
|
|
|
'verbs' => [ |
56
|
|
|
'class' => VerbFilter::class, |
57
|
|
|
'actions' => [ |
58
|
|
|
'index' => ['get'], |
59
|
|
|
'delete' => ['delete'], |
60
|
|
|
'backup' => ['post'], |
61
|
|
|
'restore' => ['post'], |
62
|
|
|
'download' => ['get'], |
63
|
|
|
], |
64
|
|
|
], |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $action |
70
|
|
|
* @return bool |
71
|
|
|
* @throws ForbiddenHttpException |
72
|
|
|
* @throws BadRequestHttpException |
73
|
|
|
*/ |
74
|
|
|
public function beforeAction($action) |
75
|
|
|
{ |
76
|
|
|
$this->checkPermission(); |
77
|
|
|
$this->enableCsrfValidation = false; |
78
|
|
|
return parent::beforeAction($action); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function actionIndex() |
86
|
|
|
{ |
87
|
|
|
$model = new BackupFilter(); |
88
|
|
|
return $model->dataProvider()->getModels(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $id |
93
|
|
|
* @return array |
94
|
|
|
* @throws NotFoundHttpException |
95
|
|
|
* @throws Throwable |
96
|
|
|
* @throws StaleObjectException |
97
|
|
|
*/ |
98
|
|
|
public function actionDelete($id) |
99
|
|
|
{ |
100
|
|
|
$this->getBackup((int)$id); |
101
|
|
|
$this->model->delete(); |
102
|
|
|
return $this->successResponse; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $config_id |
107
|
|
|
* @return array |
108
|
|
|
* @throws InvalidConfigException |
109
|
|
|
* @throws NotFoundHttpException |
110
|
|
|
* @throws Throwable |
111
|
|
|
* @throws StaleObjectException |
112
|
|
|
*/ |
113
|
|
|
public function actionBackup($config_id) |
114
|
|
|
{ |
115
|
|
|
if (!Yii::$app->getModule('backup')->checkConfig($config_id)) |
116
|
|
|
throw new NotFoundHttpException(Yii::t('app.f12.backup', 'Backup config is not found.')); |
117
|
|
|
|
118
|
|
|
Yii::createObject(BackupCreate::class, [$config_id])->run(); |
119
|
|
|
|
120
|
|
|
return $this->successResponse; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $id |
125
|
|
|
* @return array |
126
|
|
|
* @throws InvalidConfigException |
127
|
|
|
* @throws NotFoundHttpException |
128
|
|
|
*/ |
129
|
|
|
public function actionRestore($id) |
130
|
|
|
{ |
131
|
|
|
$this->getBackup((int)$id); |
132
|
|
|
Yii::createObject(BackupRestore::class, [$this->model])->run(); |
133
|
|
|
return $this->successResponse; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param $id |
138
|
|
|
* @throws NotFoundHttpException |
139
|
|
|
*/ |
140
|
|
|
public function actionGet($id) |
141
|
|
|
{ |
142
|
|
|
$this->getBackup((int)$id); |
143
|
|
|
Yii::$app->response->sendFile($this->model->getFullPath()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param int $id |
148
|
|
|
* @throws NotFoundHttpException |
149
|
|
|
*/ |
150
|
|
|
protected function getBackup(int $id) |
151
|
|
|
{ |
152
|
|
|
$this->model = Backup::findOne($id); |
153
|
|
|
if (!$this->model) |
154
|
|
|
throw new NotFoundHttpException(Yii::t('app.f12.backup', 'Backup is not found.')); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return bool |
159
|
|
|
* @throws ForbiddenHttpException |
160
|
|
|
*/ |
161
|
|
|
protected function checkPermission() |
162
|
|
|
{ |
163
|
|
|
$headers = Yii::$app->request->getHeaders(); |
164
|
|
|
$authTokens = Yii::$app->getModule('backup')->authTokens; |
165
|
|
|
if (!empty($headers[self::HEADER_NAME]) && in_array($headers[self::HEADER_NAME], $authTokens)) |
|
|
|
|
166
|
|
|
return true; |
167
|
|
|
throw new ForbiddenHttpException(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
} |