Passed
Push — master ( cf47da...53c71b )
by James
21:48 queued 09:49
created

InstallController::decrypt()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 20
rs 9.8333
c 0
b 0
f 0
cc 4
nc 8
nop 0
1
<?php
2
/**
3
 * InstallController.php
4
 * Copyright (c) 2018 [email protected]
5
 *
6
 * This file is part of Firefly III.
7
 *
8
 * Firefly III is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * Firefly III is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
declare(strict_types=1);
23
24
namespace FireflyIII\Http\Controllers\System;
25
26
27
use Artisan;
28
use Exception;
29
use FireflyIII\Http\Controllers\Controller;
30
use FireflyIII\Support\Http\Controllers\GetConfigurationData;
31
use Illuminate\Http\JsonResponse;
32
use Laravel\Passport\Passport;
33
use Log;
34
use phpseclib\Crypt\RSA;
35
36
/**
37
 * Class InstallController
38
 *
39
 * @codeCoverageIgnore
40
 */
41
class InstallController extends Controller
42
{
43
    use GetConfigurationData;
0 ignored issues
show
Bug introduced by James Cole
The trait FireflyIII\Support\Http\...rs\GetConfigurationData requires the property $data which is not provided by FireflyIII\Http\Controll...ystem\InstallController.
Loading history...
44
    /** @var string Forbidden error */
45
    public const FORBIDDEN_ERROR = 'Internal PHP function "proc_close" is disabled for your installation. Auto-migration is not possible.';
46
    /** @var string Basedir error */
47
    public const BASEDIR_ERROR = 'Firefly III cannot execute the upgrade commands. It is not allowed to because of an open_basedir restriction.';
48
    /** @var string Other errors */
49
    public const OTHER_ERROR = 'An unknown error prevented Firefly III from executing the upgrade commands. Sorry.';
50
    /** @noinspection MagicMethodsValidityInspection */
51
    /** @noinspection PhpMissingParentConstructorInspection */
52
    /**
53
     * InstallController constructor.
54
     */
55
    public function __construct()
56
    {
57
        // empty on purpose.
58
    }
59
60
    /**
61
     * Show index.
62
     *
63
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
64
     */
65
    public function index()
66
    {
67
        return view('install.index');
68
    }
69
70
    /**
71
     * Create specific RSA keys.
72
     *
73
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
74
     */
75
    public function keys()
76
    {
77
        if ($this->hasForbiddenFunctions()) {
78
            return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]);
79
        }
80
        // create keys manually because for some reason the passport namespace
81
        // does not exist
82
        $rsa  = new RSA();
83
        $keys = $rsa->createKey(4096);
84
85
        [$publicKey, $privateKey] = [
86
            Passport::keyPath('oauth-public.key'),
87
            Passport::keyPath('oauth-private.key'),
88
        ];
89
90
        if (file_exists($publicKey) || file_exists($privateKey)) {
91
            return response()->json(['error' => false, 'message' => 'OK']);
92
        }
93
94
        file_put_contents($publicKey, array_get($keys, 'publickey'));
95
        file_put_contents($privateKey, array_get($keys, 'privatekey'));
96
97
        return response()->json(['error' => false, 'message' => 'OK']);
98
    }
99
100
    /**
101
     * Run migration commands.
102
     *
103
     * @return JsonResponse
104
     */
105
    public function migrate(): JsonResponse
106
    {
107
        if ($this->hasForbiddenFunctions()) {
108
            return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]);
109
        }
110
111
        try {
112
            Log::debug('Am now calling migrate routine...');
113
            Artisan::call('migrate', ['--seed' => true, '--force' => true]);
114
            Log::debug(Artisan::output());
115
        } catch (Exception $e) {
116
            Log::error($e->getMessage());
117
            Log::error($e->getTraceAsString());
118
            if (strpos($e->getMessage(), 'open_basedir restriction in effect')) {
119
                return response()->json(['error' => true, 'message' => self::BASEDIR_ERROR]);
120
            }
121
122
            return response()->json(['error' => true, 'message' => self::OTHER_ERROR]);
123
        }
124
125
126
        return response()->json(['error' => false, 'message' => 'OK']);
127
    }
128
129
    /**
130
     * Do database upgrade.
131
     *
132
     * @return \Illuminate\Http\JsonResponse
133
     */
134
    public function upgrade(): JsonResponse
135
    {
136
        if ($this->hasForbiddenFunctions()) {
137
            return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]);
138
        }
139
        try {
140
            Log::debug('Am now calling upgrade database routine...');
141
            Artisan::call('firefly:upgrade-database');
142
            Log::debug(Artisan::output());
143
        } catch (Exception $e) {
144
            Log::error($e->getMessage());
145
            Log::error($e->getTraceAsString());
146
            if (strpos($e->getMessage(), 'open_basedir restriction in effect')) {
147
                return response()->json(['error' => true, 'message' => self::BASEDIR_ERROR]);
148
            }
149
150
            return response()->json(['error' => true, 'message' => self::OTHER_ERROR . ' ' . $e->getMessage()]);
151
        }
152
153
        return response()->json(['error' => false, 'message' => 'OK']);
154
    }
155
156
    /**
157
     * Do database decrypt.
158
     *
159
     * @return \Illuminate\Http\JsonResponse
160
     */
161
    public function decrypt(): JsonResponse
162
    {
163
        if ($this->hasForbiddenFunctions()) {
164
            return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]);
165
        }
166
        try {
167
            Log::debug('Am now calling decrypt database routine...');
168
            Artisan::call('firefly:decrypt-all');
169
            Log::debug(Artisan::output());
170
        } catch (Exception $e) {
171
            Log::error($e->getMessage());
172
            Log::error($e->getTraceAsString());
173
            if (strpos($e->getMessage(), 'open_basedir restriction in effect')) {
174
                return response()->json(['error' => true, 'message' => self::BASEDIR_ERROR]);
175
            }
176
177
            return response()->json(['error' => true, 'message' => self::OTHER_ERROR . ' ' . $e->getMessage()]);
178
        }
179
180
        return response()->json(['error' => false, 'message' => 'OK']);
181
    }
182
183
184
    /**
185
     * Do database verification.
186
     *
187
     * @return \Illuminate\Http\JsonResponse
188
     */
189
    public function verify(): JsonResponse
190
    {
191
        if ($this->hasForbiddenFunctions()) {
192
            return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]);
193
        }
194
        try {
195
            Log::debug('Am now calling verify database routine...');
196
            Artisan::call('firefly:verify');
197
            Log::debug(Artisan::output());
198
        } catch (Exception $e) {
199
            Log::error($e->getMessage());
200
            Log::error($e->getTraceAsString());
201
            if (strpos($e->getMessage(), 'open_basedir restriction in effect')) {
202
                return response()->json(['error' => true, 'message' => self::BASEDIR_ERROR]);
203
            }
204
205
            return response()->json(['error' => true, 'message' => self::OTHER_ERROR . ' ' . $e->getMessage()]);
206
        }
207
208
        return response()->json(['error' => false, 'message' => 'OK']);
209
    }
210
211
212
}
213