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\Models; |
26
|
|
|
|
27
|
|
|
use Auth; |
28
|
|
|
use Illuminate\Database\Eloquent\Model; |
29
|
|
|
use Illuminate\Notifications\Notifiable; |
30
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
31
|
|
|
|
32
|
|
|
class Server extends Model |
33
|
|
|
{ |
34
|
|
|
use Notifiable, SoftDeletes; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The table associated with the model. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $table = 'servers'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The attributes excluded from the model's JSON form. |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $hidden = ['daemonSecret', 'sftp_password']; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The attributes that should be mutated to dates. |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $dates = ['deleted_at']; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Fields that are not mass assignable. |
59
|
|
|
* |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
protected $guarded = ['id', 'installed', 'created_at', 'updated_at', 'deleted_at']; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Cast values to correct type. |
66
|
|
|
* |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
protected $casts = [ |
70
|
|
|
'node' => 'integer', |
71
|
|
|
'suspended' => 'integer', |
72
|
|
|
'owner' => 'integer', |
73
|
|
|
'memory' => 'integer', |
74
|
|
|
'swap' => 'integer', |
75
|
|
|
'disk' => 'integer', |
76
|
|
|
'io' => 'integer', |
77
|
|
|
'cpu' => 'integer', |
78
|
|
|
'oom_disabled' => 'integer', |
79
|
|
|
'port' => 'integer', |
80
|
|
|
'service' => 'integer', |
81
|
|
|
'option' => 'integer', |
82
|
|
|
'installed' => 'integer', |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var array |
87
|
|
|
*/ |
88
|
|
|
protected static $serverUUIDInstance = []; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var mixed |
92
|
|
|
*/ |
93
|
|
|
protected static $user; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Constructor. |
97
|
|
|
*/ |
98
|
|
|
public function __construct() |
99
|
|
|
{ |
100
|
|
|
parent::__construct(); |
101
|
|
|
self::$user = Auth::user(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Determine if we need to change the server's daemonSecret value to |
106
|
|
|
* match that of the user if they are a subuser. |
107
|
|
|
* |
108
|
|
|
* @param Illuminate\Database\Eloquent\Model\Server $server |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public static function getUserDaemonSecret(Server $server) |
112
|
|
|
{ |
113
|
|
|
if (self::$user->id === $server->owner || self::$user->root_admin === 1) { |
|
|
|
|
114
|
|
|
return $server->daemonSecret; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$subuser = Subuser::where('server_id', $server->id)->where('user_id', self::$user->id)->first(); |
|
|
|
|
118
|
|
|
|
119
|
|
|
if (is_null($subuser)) { |
120
|
|
|
return null; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $subuser->daemonSecret; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns array of all servers owned by the logged in user. |
128
|
|
|
* Returns all users servers if user is a root admin. |
129
|
|
|
* |
130
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
131
|
|
|
*/ |
132
|
|
|
public static function getUserServers($paginate = null) |
133
|
|
|
{ |
134
|
|
|
$query = self::select( |
135
|
|
|
'servers.*', |
136
|
|
|
'nodes.name as nodeName', |
137
|
|
|
'locations.short as a_locationShort', |
138
|
|
|
'allocations.ip', |
139
|
|
|
'allocations.ip_alias', |
140
|
|
|
'allocations.port', |
141
|
|
|
'services.name as a_serviceName', |
142
|
|
|
'service_options.name as a_serviceOptionName' |
143
|
|
|
)->join('nodes', 'servers.node', '=', 'nodes.id') |
144
|
|
|
->join('locations', 'nodes.location', '=', 'locations.id') |
145
|
|
|
->join('services', 'servers.service', '=', 'services.id') |
146
|
|
|
->join('service_options', 'servers.option', '=', 'service_options.id') |
147
|
|
|
->join('allocations', 'servers.allocation', '=', 'allocations.id'); |
148
|
|
|
|
149
|
|
|
if (self::$user->root_admin !== 1) { |
150
|
|
|
$query->whereIn('servers.id', Subuser::accessServers()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (is_numeric($paginate)) { |
154
|
|
|
return $query->paginate($paginate); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $query->get(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns a single server specified by UUID. |
162
|
|
|
* DO NOT USE THIS TO MODIFY SERVER DETAILS OR SAVE THOSE DETAILS. |
163
|
|
|
* YOU WILL OVERWRITE THE SECRET KEY AND BREAK THINGS. |
164
|
|
|
* |
165
|
|
|
* @param string $uuid The Short-UUID of the server to return an object about. |
166
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
167
|
|
|
*/ |
168
|
|
|
public static function getByUUID($uuid) |
169
|
|
|
{ |
170
|
|
|
if (array_key_exists($uuid, self::$serverUUIDInstance)) { |
171
|
|
|
return self::$serverUUIDInstance[$uuid]; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$query = self::select('servers.*', 'services.file as a_serviceFile') |
175
|
|
|
->join('services', 'services.id', '=', 'servers.service') |
176
|
|
|
->where('uuidShort', $uuid) |
177
|
|
|
->orWhere('uuid', $uuid); |
178
|
|
|
|
179
|
|
|
if (self::$user->root_admin !== 1) { |
180
|
|
|
$query->whereIn('servers.id', Subuser::accessServers()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$result = $query->first(); |
184
|
|
|
|
185
|
|
|
if (! is_null($result)) { |
186
|
|
|
$result->daemonSecret = self::getUserDaemonSecret($result); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
self::$serverUUIDInstance[$uuid] = $result; |
190
|
|
|
|
191
|
|
|
return self::$serverUUIDInstance[$uuid]; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Returns non-administrative headers for accessing a server on the daemon. |
196
|
|
|
* |
197
|
|
|
* @param string $uuid |
198
|
|
|
* @return array |
199
|
|
|
*/ |
200
|
|
|
public static function getGuzzleHeaders($uuid) |
201
|
|
|
{ |
202
|
|
|
if (array_key_exists($uuid, self::$serverUUIDInstance)) { |
203
|
|
|
return [ |
204
|
|
|
'X-Access-Server' => self::$serverUUIDInstance[$uuid]->uuid, |
205
|
|
|
'X-Access-Token' => self::$serverUUIDInstance[$uuid]->daemonSecret, |
206
|
|
|
]; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return []; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Gets all allocations associated with this server. |
214
|
|
|
* |
215
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
216
|
|
|
*/ |
217
|
|
|
public function allocations() |
218
|
|
|
{ |
219
|
|
|
return $this->hasMany(Allocation::class, 'assigned_to'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Gets information for the pack associated with this server. |
224
|
|
|
* |
225
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
226
|
|
|
*/ |
227
|
|
|
public function pack() |
228
|
|
|
{ |
229
|
|
|
return $this->hasOne(ServicePack::class, 'id', 'pack'); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Gets information for the service associated with this server. |
234
|
|
|
* |
235
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
236
|
|
|
*/ |
237
|
|
|
public function service() |
238
|
|
|
{ |
239
|
|
|
return $this->hasOne(Service::class, 'id', 'service'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Gets information for the service option associated with this server. |
244
|
|
|
* |
245
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
246
|
|
|
*/ |
247
|
|
|
public function option() |
248
|
|
|
{ |
249
|
|
|
return $this->hasOne(ServiceOptions::class, 'id', 'option'); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Gets information for the service variables associated with this server. |
254
|
|
|
* |
255
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
256
|
|
|
*/ |
257
|
|
|
public function variables() |
258
|
|
|
{ |
259
|
|
|
return $this->hasMany(ServerVariables::class); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.