Passed
Push — master ( fabd45...a8694e )
by Ron
03:26 queued 11s
created

SettingsController::getBackups()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 19
ccs 0
cts 10
cp 0
crap 12
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Installer;
4
5
use App\User;
6
use App\Settings;
7
use Carbon\Carbon;
8
use App\Mail\TestEmail;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Log;
11
use Illuminate\Support\Facades\Auth;
12
use Illuminate\Support\Facades\Mail;
13
use App\Http\Controllers\Controller;
14
use Illuminate\Support\Facades\Route;
15
use Illuminate\Support\Facades\Config;
16
use Illuminate\Support\Facades\Storage;
17
use Illuminate\Support\Facades\Artisan;
18
19
class SettingsController extends Controller
20
{
21 24
    public function __construct()
22
    {
23 24
        $this->middleware(['auth', 'can:is_installer']);
24 24
    }
25
26
    //  Bring up the change logo form
27 2
    public function logoSettings()
28
    {
29 2
        return view('installer.logoSettings');
30
    }
31
    //  Submit the new company logo
32 2
    public function submitLogo(Request $request)
33
    {
34 2
        $request->validate([
35 2
            'file' => 'mimes:jpeg,bmp,png,jpg,gif'
36
        ]);
37
38 2
        $file     = $request->file;
39 2
        $fileName = $file->getClientOriginalName();
40 2
        $file->storeAs('img', $fileName, 'public');
41
42 2
        Settings::firstOrCreate(
43 2
            ['key'   => 'app.logo'],
44 2
            ['key'   => 'app.logo', 'value' => '/storage/img/' . $fileName]
45 2
        )->update(['value' => '/storage/img/' . $fileName]);
46
47 2
        Log::debug('Route ' . Route::currentRouteName() . ' visited by User ID-' . Auth::user()->user_id);
48 2
        Log::debug('Submitted Data - ', $request->toArray());
49 2
        Log::notice('A new company logo has been uploaded by User ID-' . Auth::user()->user_id);
50
51 2
        return response()->json(['url' => '/storage/img/' . $fileName]);
52
    }
53
54 2
    public function configuration()
55
    {
56 2
        $settings = collect([
57 2
            'url'      => config('app.url'),
58 2
            'timezone' => config('app.timezone'),
59 2
            'filesize' => config('filesystems.paths.max_size'),
60
        ]);
61
62 2
        return view('installer.configuration', [
63 2
            'timzone_list' => \Timezonelist::toArray(),
1 ignored issue
show
Bug introduced by
The type Timezonelist was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
64 2
            'settings'     => $settings,
65
        ]);
66
    }
67
68 2
    public function submitConfiguration(Request $request)
69
    {
70 2
        $request->validate([
71 2
            'url'      => 'required',
72
            'timezone' => 'required',
73
            'filesize' => 'required',
74
            //  TODO - add additinal validation to make sure proper information is passed in
75
        ]);
76
77
        //  Update the site URL
78 2
        if(config('app.url') !== $request->url)
79
        {
80 2
            Settings::firstOrCreate(
81 2
                ['key'   => 'app.url'],
82 2
                ['key'   => 'app.url', 'value' => $request->url]
83 2
            )->update(['value' => $request->url]);
84
        }
85
        //  Update the site timezone
86 2
        if (config('app.timezone') !== $request->timezone) {
87 2
            Settings::firstOrCreate(
88 2
                ['key'   => 'app.timezone'],
89 2
                ['key'   => 'app.timezone', 'value' => $request->timezone]
90 2
            )->update(['value' => $request->timezone]);
91
        }
92
        //  Update the maximum file upload size
93 2
        if (config('filesystems.paths.max_size') !== $request->filesize) {
94 2
            Settings::firstOrCreate(
95 2
                ['key'   => 'filesystems.paths.max_size'],
96 2
                ['key'   => 'filesystems.paths.max_size', 'value' => $request->filesize]
97 2
            )->update(['value' => $request->filesize]);
98
        }
99 2
        $request->session()->flash('success', 'Configuration Updated');
100
101 2
        return response()->json(['success' => true]);
102
    }
103
104
    //  System backups index page
105
    public function backupsIndex()
106
    {
107
        return view('installer.backupsIndex');
108
    }
109
110
    //  Retrieve the list of backups
111
    public function getBackups()
112
    {
113
        $backups = Storage::disk('backup')->files();
114
        $buArray = [];
115
116
        //  Filter list to include timestamp
117
        foreach($backups as $backup)
118
        {
119
            $parts = pathinfo($backup);
120
            if($parts['extension'] === 'zip')
121
            {
122
                $buArray[] = [
123
                    'name' => $backup,
124
                    'date' => Carbon::createFromTimestamp(Storage::disk('backup')->lastModified($backup))->format('M d, Y h:m a'),
125
                ];
126
            }
127
        }
128
129
        return $buArray;
130
    }
131
132
    //  Delete a backup
133
    public function delBackup($name)
134
    {
135
        Storage::disk('backup')->delete($name);
136
137
        return response()->json(['success' => true]);
138
    }
139
140
    //  Download a backup
141
    public function downloadBackup($name)
142
    {
143
        if(Storage::disk('backup')->exists($name))
144
        {
145
            return Storage::disk('backup')->download($name);
146
        }
147
148
        return view('err.badFile');
149
150
    }
151
152
    //  Create a new backup
153
    public function runBackup()
154
    {
155
        Artisan::call('tb-backup:run');
156
157
        return response()->json(['success' => true]);
158
    }
159
160
161
162
163
164
165
    //  Email Settings form
166
    public function emailSettings()
167
    {
168
        $settings = collect([
169
            'host'       => config('mail.host'),
170
            'port'       => config('mail.port'),
171
            'encryption' => config('mail.encryption'),
172
            'username'   => config('mail.username'),
173
            // 'password'   => config('mail.password'),
174
            'fromEmail'  =>config('mail.from.address'),
175
            'fromName'   => config('mail.from.name'),
176
        ]);
177
178
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
179
        return view('installer.emailSettings', [
180
            'settings' => $settings,
181
        ]);
182
    }
183
184
    //  Send a test email
185
    public function sendTestEmail(Request $request)
186
    {
187
        //  TODO - fix this and make it work
188
        return response()->json(['success' => false]);
189
        // $request->validate([
190
        //     'host'       => 'required',
191
        //     'port'       => 'required|numeric',
192
        //     'encryption' => 'required',
193
        //     'username'   => 'required',
194
        //     'fromEmail'  => 'required',
195
        //     'fromName'   => 'required',
196
        // ]);
197
198
        // //  Update each setting
199
        // Settings::firstOrCreate(
200
        //     ['key'   => 'mail.host'],
201
        //     ['key'   => 'mail.host', 'value' => $request->host]
202
        // )->update(['value' => $request->host]);
203
        // Settings::firstOrCreate(
204
        //     ['key'   => 'mail.port'],
205
        //     ['key'   => 'mail.port', 'value' => $request->port]
206
        // )->update(['value' => $request->port]);
207
        // Settings::firstOrCreate(
208
        //     ['key'   => 'mail.encryption'],
209
        //     ['key'   => 'mail.encryption', 'value' => $request->encryption]
210
        // )->update(['value' => $request->encryption]);
211
        // Settings::firstOrCreate(
212
        //     ['key'   => 'mail.username'],
213
        //     ['key'   => 'mail.username', 'value' => $request->username]
214
        // )->update(['value' => $request->username]);
215
        // Settings::firstOrCreate(
216
        //     ['key'   => 'mail.from.address'],
217
        //     ['key'   => 'mail.from.address', 'value' => $request->fromEmail]
218
        // )->update(['value' => $request->fromEmail]);
219
        // Settings::firstOrCreate(
220
        //     ['key'   => 'mail.from.name'],
221
        //     ['key'   => 'mail.from.name', 'value' => $request->fromName]
222
        // )->update(['value' => $request->fromName]);
223
        // //  Only update the password if it has changed
224
        // if (!empty($request->password) && $request->password !== 'NULL') {
225
        //     // Settings::where('key', 'mail.password')->update(['value' => $request->password]);
226
        //     Settings::firstOrCreate(
227
        //         ['key'   => 'mail.password'],
228
        //         ['key'   => 'mail.password', 'value' => $request->password]
229
        //     )->update(['value' => $request->password]);
230
        // }
231
232
        // try
233
        // {
234
        //     Mail::to(Auth::user())->send(new TestEmail($request));
235
        // }
236
        // catch (\Exception $e)
237
        // {
238
        //     // return $e->getMessage();
239
        //     return response()->json(['success' => false, 'message' => $e->getMessage()]);
240
        // }
241
242
        // return response()->json(['success' => true, 'message' => 'Email successfully sent to '.Auth::user()->email]);
243
    }
244
245
    //  Submit the test email form
246
    public function submitEmailSettings(Request $request)
247
    {
248
        $request->validate([
249
            'host'       => 'required',
250
            'port'       => 'required|numeric',
251
            'encryption' => 'required',
252
            'username'   => 'required',
253
            'fromEmail'  => 'required',
254
            'fromName'   => 'required',
255
        ]);
256
257
        //  Update each setting
258
        Settings::firstOrCreate(
259
            ['key'   => 'mail.host'],
260
            ['key'   => 'mail.host', 'value' => $request->host]
261
        )->update(['value' => $request->host]);
262
        Settings::firstOrCreate(
263
            ['key'   => 'mail.port'],
264
            ['key'   => 'mail.port', 'value' => $request->port]
265
        )->update(['value' => $request->port]);
266
        Settings::firstOrCreate(
267
            ['key'   => 'mail.encryption'],
268
            ['key'   => 'mail.encryption', 'value' => $request->encryption]
269
        )->update(['value' => $request->encryption]);
270
        Settings::firstOrCreate(
271
            ['key'   => 'mail.username'],
272
            ['key'   => 'mail.username', 'value' => $request->username]
273
        )->update(['value' => $request->username]);
274
        Settings::firstOrCreate(
275
            ['key'   => 'mail.from.address'],
276
            ['key'   => 'mail.from.address', 'value' => $request->fromEmail]
277
        )->update(['value' => $request->fromEmail]);
278
        Settings::firstOrCreate(
279
            ['key'   => 'mail.from.name'],
280
            ['key'   => 'mail.from.name', 'value' => $request->fromName]
281
        )->update(['value' => $request->fromName]);
282
        //  Only update the password if it has changed
283
        if(!empty($request->password) && $request->password !== 'NULL')
284
        {
285
            // Settings::where('key', 'mail.password')->update(['value' => $request->password]);
286
            Settings::firstOrCreate(
287
                ['key'   => 'mail.password'],
288
                ['key'   => 'mail.password', 'value' => $request->password]
289
            )->update(['value' => $request->password]);
290
        }
291
292
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
293
        Log::debug('Submitted Data - ', $request->toArray());
294
        Log::notice('Email Settings have been changed by User ID-'.Auth::user()->user_id);
295
        return response()->json(['success' => true]);
296
    }
297
298
    // protected function updateEmailSettings($data)
299
    // {
300
    //     //  Update each setting
301
    //     Settings::firstOrCreate(
302
    //         ['key'   => 'mail.host'],
303
    //         ['key'   => 'mail.host', 'value' => $data->host]
304
    //     )->update(['value' => $data->host]);
305
    //     Settings::firstOrCreate(
306
    //         ['key'   => 'mail.port'],
307
    //         ['key'   => 'mail.port', 'value' => $data->port]
308
    //     )->update(['value' => $data->port]);
309
    //     Settings::firstOrCreate(
310
    //         ['key'   => 'mail.encryption'],
311
    //         ['key'   => 'mail.encryption', 'value' => $data->encryption]
312
    //     )->update(['value' => $data->encryption]);
313
    //     Settings::firstOrCreate(
314
    //         ['key'   => 'mail.username'],
315
    //         ['key'   => 'mail.username', 'value' => $data->username]
316
    //     )->update(['value' => $data->username]);
317
    //     Settings::firstOrCreate(
318
    //         ['key'   => 'mail.from.address'],
319
    //         ['key'   => 'mail.from.address', 'value' => $data->fromEmail]
320
    //     )->update(['value' => $data->fromEmail]);
321
    //     Settings::firstOrCreate(
322
    //         ['key'   => 'mail.from.name'],
323
    //         ['key'   => 'mail.from.name', 'value' => $data->fromName]
324
    //     )->update(['value' => $data->fromName]);
325
    //     //  Only update the password if it has changed
326
    //     if (!empty($data->password) && $data->password !== 'NULL') {
327
    //         // Settings::where('key', 'mail.password')->update(['value' => $request->password]);
328
    //         Settings::firstOrCreate(
329
    //             ['key'   => 'mail.password'],
330
    //             ['key'   => 'mail.password', 'value' => $data->password]
331
    //         )->update(['value' => $data->password]);
332
    //     }
333
334
    //     return true;
335
    // }
336
337
338
}
339