1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @link https://github.com/rkit/filemanager-yii2 |
5
|
|
|
* @copyright Copyright (c) 2015 Igor Romanov |
6
|
|
|
* @license [MIT](http://opensource.org/licenses/MIT) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace rkit\filemanager\models; |
10
|
|
|
|
11
|
|
|
use Yii; |
12
|
|
|
use yii\behaviors\TimestampBehavior; |
13
|
|
|
use yii\helpers\FileHelper; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ActiveRecord for table "file" |
17
|
|
|
* |
18
|
|
|
* @property integer $id |
19
|
|
|
* @property integer $user_id |
20
|
|
|
* @property integer $owner_id |
21
|
|
|
* @property integer $owner_type |
22
|
|
|
* @property string $title |
23
|
|
|
* @property string $name |
24
|
|
|
* @property integer $size |
25
|
|
|
* @property string $extension |
26
|
|
|
* @property string $mime |
27
|
|
|
* @property string $date_create |
28
|
|
|
* @property string $date_update |
29
|
|
|
* @property integer $ip |
30
|
|
|
* @property integer $tmp |
31
|
|
|
* @property integer $position |
32
|
|
|
*/ |
33
|
|
|
class File extends \yii\db\ActiveRecord |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
* @codeCoverageIgnore |
38
|
|
|
* @internal |
39
|
|
|
*/ |
40
|
|
|
public static function tableName() |
41
|
|
|
{ |
42
|
|
|
return 'file'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
* @codeCoverageIgnore |
48
|
|
|
* @internal |
49
|
|
|
*/ |
50
|
|
|
public function attributeLabels() |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
'id' => Yii::t('filemanager-yii2', 'ID'), |
54
|
|
|
'user_id' => Yii::t('filemanager-yii2', 'User'), |
55
|
|
|
'owner_id' => Yii::t('filemanager-yii2', 'Owner'), |
56
|
|
|
'owner_type' => Yii::t('filemanager-yii2', 'Owner type'), |
57
|
|
|
'title' => Yii::t('filemanager-yii2', 'Title'), |
58
|
|
|
'name' => Yii::t('filemanager-yii2', 'Name'), |
59
|
|
|
'size' => Yii::t('filemanager-yii2', 'Size'), |
60
|
|
|
'extension' => Yii::t('filemanager-yii2', 'Extension'), |
61
|
|
|
'mime' => Yii::t('filemanager-yii2', 'Mime'), |
62
|
|
|
'date_create' => Yii::t('filemanager-yii2', 'Date create'), |
63
|
|
|
'date_update' => Yii::t('filemanager-yii2', 'Date update'), |
64
|
|
|
'ip' => Yii::t('filemanager-yii2', 'IP'), |
65
|
|
|
'position' => Yii::t('filemanager-yii2', 'Position'), |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
* @internal |
72
|
|
|
*/ |
73
|
26 |
|
public function behaviors() |
74
|
|
|
{ |
75
|
|
|
return [ |
76
|
|
|
[ |
77
|
26 |
|
'class' => TimestampBehavior::className(), |
78
|
26 |
|
'createdAtAttribute' => 'date_create', |
79
|
26 |
|
'updatedAtAttribute' => 'date_update', |
80
|
26 |
|
'value' => new \yii\db\Expression('NOW()'), |
81
|
26 |
|
], |
82
|
26 |
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @internal |
87
|
|
|
*/ |
88
|
26 |
|
public function beforeSave($insert) |
89
|
|
|
{ |
90
|
26 |
|
if (parent::beforeSave($insert)) { |
91
|
26 |
|
if ($insert) { |
92
|
26 |
|
if (!Yii::$app instanceof \yii\console\Application) { |
93
|
|
|
$this->user_id = Yii::$app->user->isGuest ? 0 : Yii::$app->user->id; // @codeCoverageIgnore |
94
|
|
|
$this->ip = ip2long(Yii::$app->request->getUserIP()); // @codeCoverageIgnore |
95
|
|
|
} // @codeCoverageIgnore |
96
|
|
|
|
97
|
26 |
|
if ($this->owner_id === null) { |
98
|
25 |
|
$this->owner_id = 0; |
99
|
25 |
|
} |
100
|
26 |
|
} |
101
|
26 |
|
return true; |
102
|
|
|
} |
103
|
|
|
return false; // @codeCoverageIgnore |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Path to the file |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
111
|
|
|
*/ |
112
|
26 |
|
public function path() |
113
|
|
|
{ |
114
|
26 |
|
if ($this->isNewRecord || is_object($this->date_create)) { |
115
|
26 |
|
$dateCreate = date('Ym'); |
116
|
26 |
|
} else { |
117
|
15 |
|
$dateCreate = date_format(date_create($this->date_create), 'Ym'); |
118
|
|
|
} |
119
|
|
|
|
120
|
26 |
|
return '/' . $dateCreate . '/' . $this->id . '/' . $this->name; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Fill meta info |
125
|
|
|
* |
126
|
|
|
* @param string $path File path |
127
|
|
|
* @return void |
128
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
129
|
|
|
*/ |
130
|
26 |
|
public function fillMetaInfo($path) |
131
|
|
|
{ |
132
|
26 |
|
$pathInfo = pathinfo($path); |
133
|
|
|
|
134
|
26 |
|
if ($this->title === null) { |
135
|
5 |
|
$this->title = $pathInfo['filename']; |
136
|
5 |
|
} |
137
|
|
|
|
138
|
26 |
|
$this->size = filesize($path); |
139
|
26 |
|
$this->mime = FileHelper::getMimeType($path); |
140
|
26 |
|
$this->extension = $this->getExtensionByMimeType($path, $this->mime); |
141
|
26 |
|
$this->name = $this->generateName(); |
142
|
26 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get extension By MimeType |
146
|
|
|
* |
147
|
|
|
* $param string $path File path |
148
|
|
|
* @param string $mimeType MimeType of the file |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
26 |
|
private function getExtensionByMimeType($path, $mimeType) |
152
|
|
|
{ |
153
|
26 |
|
$extensions = FileHelper::getExtensionsByMimeType($mimeType); |
154
|
26 |
|
$pathExtension = pathinfo($path, PATHINFO_EXTENSION); |
155
|
26 |
|
$titleExtension = pathinfo($this->title, PATHINFO_EXTENSION); |
156
|
|
|
|
157
|
26 |
|
if (array_search($pathExtension, $extensions) !== false) { |
158
|
24 |
|
return $pathExtension; |
159
|
2 |
|
} elseif (array_search($titleExtension, $extensions) !== false) { |
160
|
1 |
|
return $titleExtension; |
161
|
|
|
} |
162
|
1 |
|
$extension = explode('/', $mimeType); |
163
|
1 |
|
$extension = end($extension); |
164
|
1 |
|
if (array_search($extension, $extensions) !== false) { |
165
|
1 |
|
return $extension; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return current($extensions); // @codeCoverageIgnore |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Generate a name |
173
|
|
|
* |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
26 |
|
public function generateName() |
177
|
|
|
{ |
178
|
26 |
|
$name = date('YmdHis') . substr(md5(microtime() . uniqid()), 0, 10); |
179
|
26 |
|
return $name . '.' . $this->extension; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Checks whether the file is temp |
184
|
|
|
* |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
9 |
|
public function isTmp() |
188
|
|
|
{ |
189
|
9 |
|
return (bool)$this->tmp; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Check access |
194
|
|
|
* |
195
|
|
|
* @param int $ownerId The id of the owner |
196
|
|
|
* @param int $ownerType The type of the owner |
197
|
|
|
* @return bool |
198
|
|
|
*/ |
199
|
11 |
|
public function checkAccess($ownerId, $ownerType) |
200
|
|
|
{ |
201
|
|
|
return |
202
|
11 |
|
$this->checkAccessToModel($ownerId, $ownerType) || |
203
|
11 |
|
$this->checkAccessToFile($ownerType); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Check access to model |
208
|
|
|
* |
209
|
|
|
* @param int $ownerId The id of the owner |
210
|
|
|
* @param int $ownerType The type of the owner |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
11 |
|
public function checkAccessToModel($ownerId, $ownerType) |
214
|
|
|
{ |
215
|
11 |
|
$equalOwnerType = $this->owner_type === $ownerType; |
216
|
11 |
|
$equalOwnerId = $this->owner_id === $ownerId; |
217
|
|
|
|
218
|
11 |
|
return !$this->tmp && $equalOwnerType && $equalOwnerId; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Check access to file |
223
|
|
|
* |
224
|
|
|
* @param int $ownerId The id of the owner |
|
|
|
|
225
|
|
|
* @param int $ownerType The type of the owner |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
11 |
|
public function checkAccessToFile($ownerType) |
229
|
|
|
{ |
230
|
11 |
|
$equalOwnerType = $this->owner_type === $ownerType; |
231
|
11 |
|
$user = $this->user_id === Yii::$app->user->id || $this->user_id === 0; |
232
|
|
|
|
233
|
11 |
|
return $this->tmp && $equalOwnerType && $user; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Find all by owner |
238
|
|
|
* |
239
|
|
|
* @param int $ownerId The id of the owner |
240
|
|
|
* @param int $ownerType The type of the owner |
241
|
|
|
* @return array |
242
|
|
|
*/ |
243
|
13 |
|
public static function findAllByOwner($ownerId, $ownerType) |
244
|
|
|
{ |
245
|
13 |
|
return static::find() |
246
|
13 |
|
->where(['owner_id' => $ownerId, 'owner_type' => $ownerType]) |
247
|
13 |
|
->orderBy('position ASC') |
248
|
13 |
|
->all(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Find one by owner |
253
|
|
|
* |
254
|
|
|
* @param int $ownerId The id of the owner |
255
|
|
|
* @param int $ownerType The type of the owner |
256
|
|
|
* @return File|null |
257
|
|
|
*/ |
258
|
8 |
|
public static function findOneByOwner($ownerId, $ownerType) |
259
|
|
|
{ |
260
|
8 |
|
return static::find() |
261
|
8 |
|
->where(['owner_id' => $ownerId, 'owner_type' => $ownerType]) |
262
|
8 |
|
->one(); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|