Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Foundation\Console;
4
 
5
use Exception;
6
use Illuminate\Console\Command;
7
use Illuminate\Encryption\Encrypter;
8
use Illuminate\Filesystem\Filesystem;
9
use Illuminate\Support\Env;
10
use Illuminate\Support\Str;
11
use Symfony\Component\Console\Attribute\AsCommand;
12
 
13
#[AsCommand(name: 'env:decrypt')]
14
class EnvironmentDecryptCommand extends Command
15
{
16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'env:decrypt
22
                    {--key= : The encryption key}
23
                    {--cipher= : The encryption cipher}
24
                    {--env= : The environment to be decrypted}
25
                    {--force : Overwrite the existing environment file}
26
                    {--path= : Path to write the decrypted file}
27
                    {--filename= : Filename of the decrypted file}';
28
 
29
    /**
30
     * The name of the console command.
31
     *
32
     * This name is used to identify the command during lazy loading.
33
     *
34
     * @var string|null
35
     *
36
     * @deprecated
37
     */
38
    protected static $defaultName = 'env:decrypt';
39
 
40
    /**
41
     * The console command description.
42
     *
43
     * @var string
44
     */
45
    protected $description = 'Decrypt an environment file';
46
 
47
    /**
48
     * The filesystem instance.
49
     *
50
     * @var \Illuminate\Filesystem\Filesystem
51
     */
52
    protected $files;
53
 
54
    /**
55
     * Create a new command instance.
56
     *
57
     * @param  \Illuminate\Filesystem\Filesystem  $files
58
     * @return void
59
     */
60
    public function __construct(Filesystem $files)
61
    {
62
        parent::__construct();
63
 
64
        $this->files = $files;
65
    }
66
 
67
    /**
68
     * Execute the console command.
69
     *
70
     * @return void
71
     */
72
    public function handle()
73
    {
74
        $key = $this->option('key') ?: Env::get('LARAVEL_ENV_ENCRYPTION_KEY');
75
 
76
        if (! $key) {
77
            $this->components->error('A decryption key is required.');
78
 
79
            return Command::FAILURE;
80
        }
81
 
82
        $cipher = $this->option('cipher') ?: 'AES-256-CBC';
83
 
84
        $key = $this->parseKey($key);
85
 
86
        $encryptedFile = ($this->option('env')
87
                    ? base_path('.env').'.'.$this->option('env')
88
                    : $this->laravel->environmentFilePath()).'.encrypted';
89
 
90
        $outputFile = $this->outputFilePath();
91
 
92
        if (Str::endsWith($outputFile, '.encrypted')) {
93
            $this->components->error('Invalid filename.');
94
 
95
            return Command::FAILURE;
96
        }
97
 
98
        if (! $this->files->exists($encryptedFile)) {
99
            $this->components->error('Encrypted environment file not found.');
100
 
101
            return Command::FAILURE;
102
        }
103
 
104
        if ($this->files->exists($outputFile) && ! $this->option('force')) {
105
            $this->components->error('Environment file already exists.');
106
 
107
            return Command::FAILURE;
108
        }
109
 
110
        try {
111
            $encrypter = new Encrypter($key, $cipher);
112
 
113
            $this->files->put(
114
                $outputFile,
115
                $encrypter->decrypt($this->files->get($encryptedFile))
116
            );
117
        } catch (Exception $e) {
118
            $this->components->error($e->getMessage());
119
 
120
            return Command::FAILURE;
121
        }
122
 
123
        $this->components->info('Environment successfully decrypted.');
124
 
125
        $this->components->twoColumnDetail('Decrypted file', $outputFile);
126
 
127
        $this->newLine();
128
    }
129
 
130
    /**
131
     * Parse the encryption key.
132
     *
133
     * @param  string  $key
134
     * @return string
135
     */
136
    protected function parseKey(string $key)
137
    {
138
        if (Str::startsWith($key, $prefix = 'base64:')) {
139
            $key = base64_decode(Str::after($key, $prefix));
140
        }
141
 
142
        return $key;
143
    }
144
 
145
    /**
146
     * Get the output file path that should be used for the command.
147
     *
148
     * @return string
149
     */
150
    protected function outputFilePath()
151
    {
152
        $path = Str::finish($this->option('path') ?: base_path(), DIRECTORY_SEPARATOR);
153
 
154
        $outputFile = $this->option('filename') ?: ('.env'.($this->option('env') ? '.'.$this->option('env') : ''));
155
        $outputFile = ltrim($outputFile, DIRECTORY_SEPARATOR);
156
 
157
        return $path.$outputFile;
158
    }
159
}