Passed
Push — main ( 378ff3...fca707 )
by Sugeng
03:34
created

LocalComponent::decrypt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use DateTimeImmutable;
6
use DateTimeInterface;
7
use League\Flysystem\Local\LocalFilesystemAdapter;
8
use Yii;
9
use yii\base\InvalidConfigException;
10
use yii\helpers\Json;
11
use yii\helpers\StringHelper;
12
use yii\helpers\Url;
13
14
/**
15
 * Class LocalComponent
16
 * 
17
 * ```php
18
 * 'components' => [
19
 *     'fs' => [
20
 *         'class' => \diecoding\flysystem\LocalComponent::class,
21
 *         'path' => dirname(dirname(__DIR__)) . '/storage', // or you can use @alias
22
 *         'secret' => 'my-secret',
23
 *         'action' => '/site/file',
24
 *         'prefix' => '',
25
 *     ],
26
 * ],
27
 * ```
28
 * 
29
 * @package diecoding\flysystem
30
 * 
31
 * @link      https://sugengsulistiyawan.my.id/
32
 * @author    Sugeng Sulistiyawan <[email protected]>
33
 * @copyright Copyright (c) 2023
34
 */
35
class LocalComponent extends AbstractComponent
36
{
37
    /**
38
     * @var string
39
     */
40
    public $path;
41
42
    /**
43
     * @var string
44
     */
45
    public $action = '/site/file';
46
47
    /**
48
     * @var string
49
     */
50
    public $cipherAlgo = 'aes-128-cbc';
51
52
    /**
53
     * @var string
54
     */
55
    public $secret;
56
57
    /**
58
     * @var string
59
     */
60
    protected $_basePath;
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function init()
66
    {
67
        if (empty($this->path)) {
68
            throw new InvalidConfigException('The "path" property must be set.');
69
        }
70
71
        if (empty($this->secret)) {
72
            throw new InvalidConfigException('The "secret" property must be set.');
73
        }
74
75
        parent::init();
76
    }
77
78
    public function publicUrl(string $path, array $config = []): string
79
    {
80
        $config['attachmentName'] = pathinfo($path, PATHINFO_BASENAME);
81
82
        $params = [
83
            'path'    => $this->normalizePath($this->_basePath . '/' . $path),
84
            'expires' => false,
85
            'config'  => $config,
86
        ];
87
88
        return Url::toRoute([$this->action, 'data' => $this->encrypt(Json::encode($params))], true);
89
    }
90
91
    public function temporaryUrl(string $path, DateTimeInterface $expiresAt, array $config = []): string
92
    {
93
        $config['attachmentName'] = pathinfo($path, PATHINFO_BASENAME);
94
95
        $params = [
96
            'path'    => $this->normalizePath($this->_basePath . '/' . $path),
97
            'expires' => DateTimeImmutable::createFromInterface($expiresAt)->getTimestamp(),
98
            'config'  => $config,
99
        ];
100
101
        return Url::toRoute([$this->action, 'data' => $this->encrypt(Json::encode($params))], true);
102
    }
103
104
    /**
105
     * @return LocalFilesystemAdapter
106
     */
107
    protected function initAdapter()
108
    {
109
        $this->path      = (string) Yii::getAlias($this->path);
110
        $this->_basePath = $this->normalizePath($this->path . '/' . $this->prefix);
111
112
        return new LocalFilesystemAdapter($this->_basePath);
113
    }
114
115
    /**
116
     * Encrypts a string.
117
     * 
118
     * @param string $string the string to encrypt
119
     * @return string the encrypted string
120
     */
121
    protected function encrypt($string)
122
    {
123
        $encryptedString = openssl_encrypt($string, $this->cipherAlgo, $this->secret);
124
        $encryptedString = StringHelper::base64UrlEncode(base64_encode($encryptedString));
125
126
        return $encryptedString;
127
    }
128
129
    /**
130
     * Decrypts a string. 
131
     * False is returned in case it was not possible to decrypt it.
132
     * 
133
     * @param string $string the string to decrypt
134
     * @return string the decrypted string
135
     */
136
    protected function decrypt($string)
137
    {
138
        $decodedString = base64_decode(StringHelper::base64UrlDecode($string));
139
        $decodedString = openssl_decrypt($decodedString, $this->cipherAlgo, $this->secret);
140
141
        return $decodedString;
142
    }
143
}
144