|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Pterodactyl - Panel |
|
4
|
|
|
* Copyright (c) 2015 - 2016 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\Repositories\ServiceRepository; |
|
26
|
|
|
|
|
27
|
|
|
use DB; |
|
28
|
|
|
use Uuid; |
|
29
|
|
|
use Storage; |
|
30
|
|
|
use Validator; |
|
31
|
|
|
use Pterodactyl\Models; |
|
32
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
|
33
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
|
34
|
|
|
|
|
35
|
|
|
class Service |
|
36
|
|
|
{ |
|
37
|
|
|
public function __construct() |
|
38
|
|
|
{ |
|
39
|
|
|
// |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function create(array $data) |
|
43
|
|
|
{ |
|
44
|
|
|
$validator = Validator::make($data, [ |
|
45
|
|
|
'name' => 'required|string|min:1|max:255', |
|
46
|
|
|
'description' => 'required|string', |
|
47
|
|
|
'file' => 'required|unique:services,file|regex:/^[\w.-]{1,50}$/', |
|
48
|
|
|
'executable' => 'max:255|regex:/^(.*)$/', |
|
49
|
|
|
'startup' => 'string', |
|
50
|
|
|
]); |
|
51
|
|
|
|
|
52
|
|
|
if ($validator->fails()) { |
|
53
|
|
|
throw new DisplayValidationException($validator->errors()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$data['author'] = env('SERVICE_AUTHOR', (string) Uuid::generate(4)); |
|
57
|
|
|
|
|
58
|
|
|
$service = new Models\Service; |
|
59
|
|
|
DB::beginTransaction(); |
|
60
|
|
|
|
|
61
|
|
|
try { |
|
62
|
|
|
$service->fill($data); |
|
63
|
|
|
$service->save(); |
|
64
|
|
|
|
|
65
|
|
|
Storage::put('services/' . $data['file'] . '/main.json', '{}'); |
|
|
|
|
|
|
66
|
|
|
Storage::copy('services/.templates/index.js', 'services/' . $data['file'] . '/index.js'); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
DB::commit(); |
|
69
|
|
|
} catch (\Exception $ex) { |
|
70
|
|
|
DB::rollBack(); |
|
71
|
|
|
throw $ex; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $service->id; |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function update($id, array $data) |
|
78
|
|
|
{ |
|
79
|
|
|
$service = Models\Service::findOrFail($id); |
|
80
|
|
|
|
|
81
|
|
|
$validator = Validator::make($data, [ |
|
82
|
|
|
'name' => 'sometimes|required|string|min:1|max:255', |
|
83
|
|
|
'description' => 'sometimes|required|string', |
|
84
|
|
|
'file' => 'sometimes|required|regex:/^[\w.-]{1,50}$/', |
|
85
|
|
|
'executable' => 'sometimes|max:255|regex:/^(.*)$/', |
|
86
|
|
|
'startup' => 'sometimes|string', |
|
87
|
|
|
]); |
|
88
|
|
|
|
|
89
|
|
|
if ($validator->fails()) { |
|
90
|
|
|
throw new DisplayValidationException($validator->errors()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$service->fill($data); |
|
94
|
|
|
|
|
95
|
|
|
return $service->save(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function delete($id) |
|
99
|
|
|
{ |
|
100
|
|
|
$service = Models\Service::findOrFail($id); |
|
101
|
|
|
$servers = Models\Server::where('service', $service->id)->get(); |
|
102
|
|
|
$options = Models\ServiceOptions::select('id')->where('parent_service', $service->id); |
|
103
|
|
|
|
|
104
|
|
|
if (count($servers) !== 0) { |
|
105
|
|
|
throw new DisplayException('You cannot delete a service that has servers associated with it.'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
DB::beginTransaction(); |
|
109
|
|
|
try { |
|
110
|
|
|
Models\ServiceVariables::whereIn('option_id', $options->get()->toArray())->delete(); |
|
111
|
|
|
$options->delete(); |
|
112
|
|
|
$service->delete(); |
|
113
|
|
|
|
|
114
|
|
|
Storage::deleteDirectory('services/' . $service->file); |
|
|
|
|
|
|
115
|
|
|
DB::commit(); |
|
116
|
|
|
} catch (\Exception $ex) { |
|
117
|
|
|
DB::rollBack(); |
|
118
|
|
|
throw $ex; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function updateFile($id, array $data) |
|
123
|
|
|
{ |
|
124
|
|
|
$service = Models\Service::findOrFail($id); |
|
125
|
|
|
|
|
126
|
|
|
$validator = Validator::make($data, [ |
|
127
|
|
|
'file' => 'required|in:index,main', |
|
128
|
|
|
'contents' => 'required|string', |
|
129
|
|
|
]); |
|
130
|
|
|
|
|
131
|
|
|
if ($validator->fails()) { |
|
132
|
|
|
throw new DisplayValidationException($validator->errors()); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$filename = ($data['file'] === 'main') ? 'main.json' : 'index.js'; |
|
136
|
|
|
$filepath = 'services/' . $service->file . '/' . $filename; |
|
137
|
|
|
$backup = 'services/.bak/' . str_random(12) . '.bak'; |
|
138
|
|
|
|
|
139
|
|
|
try { |
|
140
|
|
|
Storage::move($filepath, $backup); |
|
|
|
|
|
|
141
|
|
|
Storage::put($filepath, $data['contents']); |
|
|
|
|
|
|
142
|
|
|
} catch (\Exception $ex) { |
|
143
|
|
|
Storage::move($backup, $filepath); |
|
|
|
|
|
|
144
|
|
|
throw $ex; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.