GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 759ebd...aa6e73 )
by Dane
10:12
created

ServerController::getFiles()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 30
rs 8.8571
cc 1
eloc 21
nc 1
nop 2
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\Http\Controllers\Server;
26
27
use DB;
28
use Log;
29
use Uuid;
30
use Alert;
31
use Javascript;
32
use Pterodactyl\Models;
33
use Illuminate\Http\Request;
34
use InvalidArgumentException;
35
use Pterodactyl\Exceptions\DisplayException;
36
use Pterodactyl\Http\Controllers\Controller;
37
use Pterodactyl\Repositories\ServerRepository;
38
use Pterodactyl\Repositories\Daemon\FileRepository;
39
use Pterodactyl\Exceptions\DisplayValidationException;
40
41
class ServerController extends Controller
42
{
43
    /**
44
     * Controller Constructor.
45
     *
46
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
47
     */
48
    public function __construct()
49
    {
50
        //
51
    }
52
53
    /**
54
     * Renders server index page for specified server.
55
     *
56
     * @param  \Illuminate\Http\Request $request
57
     * @return \Illuminate\Contracts\View\View
58
     */
59
    public function getIndex(Request $request)
60
    {
61
        $server = Models\Server::getByUUID($request->route()->server);
62
63
        Javascript::put([
64
            'meta' => [
65
                'saveFile' => route('server.files.save', $server->uuidShort),
0 ignored issues
show
Bug introduced by
The property uuidShort does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
66
                'csrfToken' => csrf_token(),
67
            ],
68
        ]);
69
70
        return view('server.index', [
71
            'server' => $server,
72
            'allocations' => Models\Allocation::where('assigned_to', $server->id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(),
0 ignored issues
show
Bug introduced by
The property id does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
73
            'node' => Models\Node::find($server->node),
0 ignored issues
show
Bug introduced by
The property node does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
74
        ]);
75
    }
76
77
    /**
78
     * Renders file overview page.
79
     *
80
     * @param  Request $request
81
     * @return \Illuminate\Contracts\View\View
82
     */
83
    public function getFiles(Request $request, $uuid)
84
    {
85
        $server = Models\Server::getByUUID($uuid);
86
        $this->authorize('list-files', $server);
87
88
        $node = Models\Node::find($server->node);
0 ignored issues
show
Bug introduced by
The property node does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
89
90
        Javascript::put([
91
            'server' => collect($server->makeVisible('daemonSecret'))->only('uuid', 'uuidShort', 'daemonSecret'),
92
            'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'),
93
            'meta' => [
94
                'directoryList' => route('server.files.directory-list', $server->uuidShort),
0 ignored issues
show
Bug introduced by
The property uuidShort does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
95
                'csrftoken' => csrf_token(),
96
            ],
97
            'permissions' => [
98
                'moveFiles' => $request->user()->can('move-files', $server),
99
                'copyFiles' => $request->user()->can('copy-files', $server),
100
                'compressFiles' => $request->user()->can('compress-files', $server),
101
                'decompressFiles' => $request->user()->can('decompress-files', $server),
102
                'createFiles' => $request->user()->can('create-files', $server),
103
                'downloadFiles' => $request->user()->can('download-files', $server),
104
                'deleteFiles' => $request->user()->can('delete-files', $server),
105
            ],
106
        ]);
107
108
        return view('server.files.index', [
109
            'server' => $server,
110
            'node' => $node,
111
        ]);
112
    }
113
114
    /**
115
     * Renders add file page.
116
     *
117
     * @param  Request $request
118
     * @return \Illuminate\Contracts\View\View
119
     */
120
    public function getAddFile(Request $request, $uuid)
121
    {
122
        $server = Models\Server::getByUUID($uuid);
123
        $this->authorize('add-files', $server);
124
125
        return view('server.files.add', [
126
            'server' => $server,
127
            'node' => Models\Node::find($server->node),
0 ignored issues
show
Bug introduced by
The property node does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
128
            'directory' => (in_array($request->get('dir'), [null, '/', ''])) ? '' : trim($request->get('dir'), '/') . '/',
129
        ]);
130
    }
131
132
    /**
133
     * Renders edit file page for a given file.
134
     *
135
     * @param  Request $request
136
     * @param  string  $uuid
137
     * @param  string  $file
138
     * @return \Illuminate\Contracts\View\View
139
     */
140
    public function getEditFile(Request $request, $uuid, $file)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
141
    {
142
        $server = Models\Server::getByUUID($uuid);
143
        $this->authorize('edit-files', $server);
144
145
        $fileInfo = (object) pathinfo($file);
146
        $controller = new FileRepository($uuid);
147
148
        try {
149
            $fileContent = $controller->returnFileContents($file);
150
        } catch (DisplayException $ex) {
151
            Alert::danger($ex->getMessage())->flash();
152
153
            return redirect()->route('server.files.index', $uuid);
0 ignored issues
show
Documentation introduced by
$uuid is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
154
        } catch (\Exception $ex) {
155
            Log::error($ex);
156
            Alert::danger('An error occured while attempting to load the requested file for editing, please try again.')->flash();
157
158
            return redirect()->route('server.files.index', $uuid);
0 ignored issues
show
Documentation introduced by
$uuid is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
159
        }
160
161
        return view('server.files.edit', [
162
            'server' => $server,
163
            'node' => Models\Node::find($server->node),
0 ignored issues
show
Bug introduced by
The property node does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
164
            'file' => $file,
165
            'stat' => $fileContent['stat'],
166
            'contents' => $fileContent['file']->content,
167
            'directory' => (in_array($fileInfo->dirname, ['.', './', '/'])) ? '/' : trim($fileInfo->dirname, '/') . '/',
168
        ]);
169
    }
170
171
    /**
172
     * Handles downloading a file for the user.
173
     *
174
     * @param  Request $request
175
     * @param  string  $uuid
176
     * @param  string  $file
177
     * @return \Illuminate\Contracts\View\View
178
     */
179
    public function getDownloadFile(Request $request, $uuid, $file)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
180
    {
181
        $server = Models\Server::getByUUID($uuid);
182
        $node = Models\Node::find($server->node);
0 ignored issues
show
Bug introduced by
The property node does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
183
184
        $this->authorize('download-files', $server);
185
186
        $download = new Models\Download;
187
188
        $download->token = (string) Uuid::generate(4);
0 ignored issues
show
Documentation introduced by
The property token does not exist on object<Pterodactyl\Models\Download>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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.

Loading history...
189
        $download->server = $server->uuid;
0 ignored issues
show
Documentation introduced by
The property server does not exist on object<Pterodactyl\Models\Download>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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.

Loading history...
Bug introduced by
The property uuid does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
190
        $download->path = $file;
0 ignored issues
show
Documentation introduced by
The property path does not exist on object<Pterodactyl\Models\Download>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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.

Loading history...
191
192
        $download->save();
193
194
        return redirect($node->scheme . '://' . $node->fqdn . ':' . $node->daemonListen . '/server/file/download/' . $download->token);
0 ignored issues
show
Documentation introduced by
The property token does not exist on object<Pterodactyl\Models\Download>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
195
    }
196
197
    /**
198
     * Renders server settings page.
199
     *
200
     * @param  \Illuminate\Http\Request $request
201
     * @return \Illuminate\Contracts\View\View
202
     */
203
    public function getSettings(Request $request, $uuid)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
204
    {
205
        $server = Models\Server::getByUUID($uuid);
206
        $allocation = Models\Allocation::findOrFail($server->allocation);
0 ignored issues
show
Bug introduced by
The property allocation does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
207
208
        $variables = Models\ServiceVariables::select(
209
                'service_variables.*',
210
                DB::raw('COALESCE(server_variables.variable_value, service_variables.default_value) as a_serverValue')
211
            )->leftJoin('server_variables', 'server_variables.variable_id', '=', 'service_variables.id')
212
            ->where('service_variables.option_id', $server->option)
0 ignored issues
show
Bug introduced by
The property option does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
213
            ->where('server_variables.server_id', $server->id)
0 ignored issues
show
Bug introduced by
The property id does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
214
            ->get();
215
216
        $service = Models\Service::select(
217
                DB::raw('IFNULL(service_options.executable, services.executable) as executable')
218
            )->leftJoin('service_options', 'service_options.parent_service', '=', 'services.id')
219
            ->where('service_options.id', $server->option)
220
            ->where('services.id', $server->service)
0 ignored issues
show
Bug introduced by
The property service does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
221
            ->first();
222
223
        $serverVariables = [
224
            '{{SERVER_MEMORY}}' => $server->memory,
0 ignored issues
show
Bug introduced by
The property memory does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
225
            '{{SERVER_IP}}' => $allocation->ip,
226
            '{{SERVER_PORT}}' => $allocation->port,
227
        ];
228
229
        $processed = str_replace(array_keys($serverVariables), array_values($serverVariables), $server->startup);
0 ignored issues
show
Bug introduced by
The property startup does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
230
        foreach ($variables as &$variable) {
231
            $replace = ($variable->user_viewable === 1) ? $variable->a_serverValue : '**';
232
            $processed = str_replace('{{' . $variable->env_variable . '}}', $replace, $processed);
233
        }
234
235
        return view('server.settings', [
236
            'server' => $server,
237
            'databases' => Models\Database::select('databases.*', 'database_servers.host as a_host', 'database_servers.port as a_port')
238
                ->where('server_id', $server->id)
239
                ->join('database_servers', 'database_servers.id', '=', 'databases.db_server')
240
                ->get(),
241
            'node' => Models\Node::find($server->node),
0 ignored issues
show
Bug introduced by
The property node does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
242
            'variables' => $variables->where('user_viewable', 1),
243
            'service' => $service,
244
            'processedStartup' => $processed,
245
        ]);
246
    }
247
248 View Code Duplication
    public function postSettingsSFTP(Request $request, $uuid)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
249
    {
250
        $server = Models\Server::getByUUID($uuid);
251
        $this->authorize('reset-sftp', $server);
252
253
        try {
254
            $repo = new ServerRepository;
255
            $repo->updateSFTPPassword($server->id, $request->input('sftp_pass'));
0 ignored issues
show
Bug introduced by
The property id does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
256
            Alert::success('Successfully updated this servers SFTP password.')->flash();
257
        } catch (DisplayValidationException $ex) {
258
            return redirect()->route('server.settings', $uuid)->withErrors(json_decode($ex->getMessage()));
259
        } catch (DisplayException $ex) {
260
            Alert::danger($ex->getMessage())->flash();
261
        } catch (\Exception $ex) {
262
            Log::error($ex);
263
            Alert::danger('An unknown error occured while attempting to update this server\'s SFTP settings.')->flash();
264
        }
265
266
        return redirect()->route('server.settings', $uuid);
267
    }
268
269
    public function postSettingsStartup(Request $request, $uuid)
270
    {
271
        $server = Models\Server::getByUUID($uuid);
272
        $this->authorize('edit-startup', $server);
273
274
        try {
275
            $repo = new ServerRepository;
276
            $repo->updateStartup($server->id, $request->except([
0 ignored issues
show
Bug introduced by
The property id does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
277
                '_token',
278
            ]));
279
            Alert::success('Server startup variables were successfully updated.')->flash();
280
        } catch (DisplayException $ex) {
281
            Alert::danger($ex->getMessage())->flash();
282
        } catch (\Exception $ex) {
283
            Log::error($ex);
284
            Alert::danger('An unhandled exception occured while attemping to update startup variables for this server. Please try again.')->flash();
285
        }
286
287
        return redirect()->route('server.settings', [
288
            'uuid' => $uuid,
289
            'tab' => 'tab_startup',
290
        ]);
291
    }
292
}
293