|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Pterodactyl - Panel |
|
4
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>. |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
14
|
|
|
* copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
22
|
|
|
* SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Pterodactyl\Http\Controllers\Admin; |
|
26
|
|
|
|
|
27
|
|
|
use Log; |
|
28
|
|
|
use Alert; |
|
29
|
|
|
use Illuminate\Http\Request; |
|
30
|
|
|
use Pterodactyl\Models\Database; |
|
31
|
|
|
use Pterodactyl\Models\Location; |
|
32
|
|
|
use Pterodactyl\Models\DatabaseHost; |
|
33
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
|
34
|
|
|
use Pterodactyl\Http\Controllers\Controller; |
|
35
|
|
|
use Pterodactyl\Repositories\DatabaseRepository; |
|
36
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
|
37
|
|
|
|
|
38
|
|
|
class DatabaseController extends Controller |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* Display database host index. |
|
42
|
|
|
* |
|
43
|
|
|
* @param Request $request |
|
44
|
|
|
* @return \Illuminate\View\View |
|
45
|
|
|
*/ |
|
46
|
|
|
public function index(Request $request) |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
return view('admin.databases.index', [ |
|
|
|
|
|
|
49
|
|
|
'locations' => Location::with('nodes')->get(), |
|
|
|
|
|
|
50
|
|
|
'hosts' => DatabaseHost::withCount('databases')->with('node')->get(), |
|
51
|
|
|
]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Display database host to user. |
|
56
|
|
|
* |
|
57
|
|
|
* @param Request $request |
|
58
|
|
|
* @param int $id |
|
59
|
|
|
* @return \Illuminate\View\View |
|
60
|
|
|
*/ |
|
61
|
|
|
public function view(Request $request, $id) |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
return view('admin.databases.view', [ |
|
|
|
|
|
|
64
|
|
|
'locations' => Location::with('nodes')->get(), |
|
|
|
|
|
|
65
|
|
|
'host' => DatabaseHost::with('databases.server')->findOrFail($id), |
|
|
|
|
|
|
66
|
|
|
]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Handle post request to create database host. |
|
71
|
|
|
* |
|
72
|
|
|
* @param Request $request |
|
73
|
|
|
* @return \Illuminate\Response\RedirectResponse |
|
74
|
|
|
*/ |
|
75
|
|
View Code Duplication |
public function create(Request $request) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
$repo = new DatabaseRepository; |
|
78
|
|
|
|
|
79
|
|
|
try { |
|
80
|
|
|
$host = $repo->add($request->intersect([ |
|
81
|
|
|
'name', 'username', 'password', |
|
82
|
|
|
'host', 'port', 'node_id', |
|
83
|
|
|
])); |
|
84
|
|
|
Alert::success('Successfully created new database host on the system.')->flash(); |
|
85
|
|
|
|
|
86
|
|
|
return redirect()->route('admin.databases.view', $host->id); |
|
|
|
|
|
|
87
|
|
|
} catch (\PDOException $ex) { |
|
88
|
|
|
Alert::danger($ex->getMessage())->flash(); |
|
89
|
|
|
} catch (DisplayValidationException $ex) { |
|
90
|
|
|
return redirect()->route('admin.databases')->withErrors(json_decode($ex->getMessage())); |
|
91
|
|
|
} catch (\Exception $ex) { |
|
92
|
|
|
Log::error($ex); |
|
93
|
|
|
Alert::danger('An error was encountered while trying to process this request. This error has been logged.')->flash(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return redirect()->route('admin.databases'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Handle post request to update a database host. |
|
101
|
|
|
* |
|
102
|
|
|
* @param Request $request |
|
103
|
|
|
* @param int $id |
|
104
|
|
|
* @return \Illuminate\Response\RedirectResponse |
|
105
|
|
|
*/ |
|
106
|
|
View Code Duplication |
public function update(Request $request, $id) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
$repo = new DatabaseRepository; |
|
109
|
|
|
|
|
110
|
|
|
try { |
|
111
|
|
|
if ($request->input('action') !== 'delete') { |
|
112
|
|
|
$host = $repo->update($id, $request->intersect([ |
|
|
|
|
|
|
113
|
|
|
'name', 'username', 'password', |
|
114
|
|
|
'host', 'port', 'node_id', |
|
115
|
|
|
])); |
|
116
|
|
|
Alert::success('Database host was updated successfully.')->flash(); |
|
117
|
|
|
} else { |
|
118
|
|
|
$repo->delete($id); |
|
119
|
|
|
|
|
120
|
|
|
return redirect()->route('admin.databases'); |
|
121
|
|
|
} |
|
122
|
|
|
} catch (\PDOException $ex) { |
|
123
|
|
|
Alert::danger($ex->getMessage())->flash(); |
|
124
|
|
|
} catch (DisplayException $ex) { |
|
125
|
|
|
Alert::danger($ex->getMessage())->flash(); |
|
126
|
|
|
} catch (DisplayValidationException $ex) { |
|
127
|
|
|
return redirect()->route('admin.databases.view', $id)->withErrors(json_decode($ex->getMessage())); |
|
|
|
|
|
|
128
|
|
|
} catch (\Exception $ex) { |
|
129
|
|
|
Log::error($ex); |
|
130
|
|
|
Alert::danger('An error was encountered while trying to process this request. This error has been logged.')->flash(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return redirect()->route('admin.databases.view', $id); |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.