|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace diecoding\flysystem; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\base\Action; |
|
7
|
|
|
use yii\helpers\Json; |
|
8
|
|
|
use yii\web\NotFoundHttpException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* ErrorAction displays application errors using a specified view. |
|
12
|
|
|
* |
|
13
|
|
|
* To use ErrorAction, you need to do the following steps: |
|
14
|
|
|
* |
|
15
|
|
|
* First, declare an action of ErrorAction type in the `actions()` method of your `SiteController` |
|
16
|
|
|
* class (or whatever controller you prefer), like the following: |
|
17
|
|
|
* |
|
18
|
|
|
* ```php |
|
19
|
|
|
* public function actions() |
|
20
|
|
|
* { |
|
21
|
|
|
* return [ |
|
22
|
|
|
* 'error' => ['class' => 'yii\web\ErrorAction'], |
|
23
|
|
|
* ]; |
|
24
|
|
|
* } |
|
25
|
|
|
* ``` |
|
26
|
|
|
* |
|
27
|
|
|
* Then, create a view file for this action. If the route of your error action is `site/error`, then |
|
28
|
|
|
* the view file should be `views/site/error.php`. In this view file, the following variables are available: |
|
29
|
|
|
* |
|
30
|
|
|
* - `$name`: the error name |
|
31
|
|
|
* - `$message`: the error message |
|
32
|
|
|
* - `$exception`: the exception being handled |
|
33
|
|
|
* |
|
34
|
|
|
* Finally, configure the "errorHandler" application component as follows, |
|
35
|
|
|
* |
|
36
|
|
|
* ```php |
|
37
|
|
|
* 'errorHandler' => [ |
|
38
|
|
|
* 'errorAction' => 'site/error', |
|
39
|
|
|
* ] |
|
40
|
|
|
* ``` |
|
41
|
|
|
* |
|
42
|
|
|
* @author Qiang Xue <[email protected]> |
|
43
|
|
|
* @author Dmitry Naumenko <[email protected]> |
|
44
|
|
|
* @since 2.0 |
|
45
|
|
|
*/ |
|
46
|
|
|
class LocalAction extends Action |
|
47
|
|
|
{ |
|
48
|
|
|
/** |
|
49
|
|
|
* @var string flysystem component |
|
50
|
|
|
*/ |
|
51
|
|
|
public $component = 'fs'; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var LocalComponent |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $filesystem; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function init() |
|
62
|
|
|
{ |
|
63
|
|
|
$this->filesystem = Yii::$app->get($this->component); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Runs the action. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $data |
|
70
|
|
|
* @return string result content |
|
71
|
|
|
* @throws NotFoundHttpException if data not valid |
|
72
|
|
|
*/ |
|
73
|
|
|
public function run($data) |
|
74
|
|
|
{ |
|
75
|
|
|
try { |
|
76
|
|
|
$params = Json::decode($this->filesystem->decrypt($data)); |
|
77
|
|
|
|
|
78
|
|
|
$now = time(); |
|
79
|
|
|
$filePath = (string) $params['filePath']; |
|
80
|
|
|
$expires = $params['expires']; |
|
81
|
|
|
|
|
82
|
|
|
if ($expires !== null && (int) $expires < $now) { |
|
83
|
|
|
$filePath = false; |
|
84
|
|
|
} |
|
85
|
|
|
} catch (\Throwable $th) { |
|
86
|
|
|
throw new NotFoundHttpException(Yii::t('yii', 'Page not found.')); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$normalizePath = '/' . $this->filesystem->normalizePath($filePath); |
|
|
|
|
|
|
90
|
|
|
if ($filePath === false || !$this->filesystem->fileExists($normalizePath)) { |
|
91
|
|
|
throw new NotFoundHttpException(Yii::t('yii', 'Page not found.')); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$filePath = $this->filesystem->path . $normalizePath; |
|
95
|
|
|
|
|
96
|
|
|
return Yii::$app->getResponse()->sendFile($filePath, $normalizePath, [ |
|
|
|
|
|
|
97
|
|
|
'inline' => true, |
|
98
|
|
|
]); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
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.