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
Pull Request — develop (#334)
by Dane
05:31 queued 02:47
created

AjaxController   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 11
dl 0
loc 210
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A postSaveFile() 0 19 3
A __construct() 0 4 1
B getStatus() 0 27 6
C postDirectoryList() 0 43 7
B postSetPrimary() 0 41 6
A postResetDatabasePassword() 0 23 3
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\Server;
26
27
use Log;
28
use Pterodactyl\Models;
29
use Illuminate\Http\Request;
30
use Pterodactyl\Repositories;
31
use GuzzleHttp\Exception\RequestException;
32
use Pterodactyl\Exceptions\DisplayException;
33
use Pterodactyl\Http\Controllers\Controller;
34
use Pterodactyl\Exceptions\DisplayValidationException;
35
36
class AjaxController extends Controller
37
{
38
    /**
39
     * @var array
40
     */
41
    protected $files = [];
42
43
    /**
44
     * @var array
45
     */
46
    protected $folders = [];
47
48
    /**
49
     * @var string
50
     */
51
    protected $directory;
52
53
    /**
54
     * Controller Constructor.
55
     */
56
    public function __construct()
57
    {
58
        //
59
    }
60
61
    /**
62
     * Returns true or false depending on the power status of the requested server.
63
     *
64
     * @param  \Illuminate\Http\Request $request
65
     * @param  string $uuid
66
     * @return \Illuminate\Contracts\View\View
67
     */
68
    public function getStatus(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...
69
    {
70
        $server = Models\Server::byUuid($uuid);
71
72
        if (! $server) {
73
            return response()->json([], 404);
74
        }
75
76
        if (! $server->installed) {
0 ignored issues
show
Bug introduced by
The property installed 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...
77
            return response()->json(['status' => 20]);
78
        }
79
80
        if ($server->suspended) {
0 ignored issues
show
Bug introduced by
The property suspended 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...
81
            return response()->json(['status' => 30]);
82
        }
83
84
        try {
85
            $res = $server->guzzleClient()->request('GET', '/server');
86
            if ($res->getStatusCode() === 200) {
87
                return response()->json(json_decode($res->getBody()));
88
            }
89
        } catch (RequestException $e) {
90
            //
91
        }
92
93
        return response()->json([]);
94
    }
95
96
    /**
97
     * Returns a listing of files in a given directory for a server.
98
     *
99
     * @param  \Illuminate\Http\Request $request
100
     * @param  string $uuid`
0 ignored issues
show
Bug introduced by
There is no parameter named $uuid`. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
101
     * @return \Illuminate\Contracts\View\View
102
     */
103
    public function postDirectoryList(Request $request, $uuid)
104
    {
105
        $server = Models\Server::byUuid($uuid);
106
        $this->authorize('list-files', $server);
107
108
        $this->directory = '/' . trim(urldecode($request->input('directory', '/')), '/');
109
        $prevDir = [
110
            'header' => ($this->directory !== '/') ? $this->directory : '',
111
        ];
112
        if ($this->directory !== '/') {
113
            $prevDir['first'] = true;
114
        }
115
116
        // Determine if we should show back links in the file browser.
117
        // This code is strange, and could probably be rewritten much better.
118
        $goBack = explode('/', trim($this->directory, '/'));
119
        if (! empty(array_filter($goBack)) && count($goBack) >= 2) {
120
            $prevDir['show'] = true;
121
            array_pop($goBack);
122
            $prevDir['link'] = '/' . implode('/', $goBack);
123
            $prevDir['link_show'] = implode('/', $goBack) . '/';
124
        }
125
126
        $controller = new Repositories\Daemon\FileRepository($uuid);
127
128
        try {
129
            $directoryContents = $controller->returnDirectoryListing($this->directory);
130
        } catch (DisplayException $ex) {
131
            return response($ex->getMessage(), 500);
132
        } catch (\Exception $ex) {
133
            Log::error($ex);
134
135
            return response('An error occured while attempting to load the requested directory, please try again.', 500);
136
        }
137
138
        return view('server.files.list', [
139
            'server' => $server,
140
            'files' => $directoryContents->files,
141
            'folders' => $directoryContents->folders,
142
            'editableMime' => Repositories\HelperRepository::editableFiles(),
143
            'directory' => $prevDir,
144
        ]);
145
    }
146
147
    /**
148
     * Handles a POST request to save a file.
149
     *
150
     * @param  Request $request
151
     * @param  string  $uuid
152
     * @return \Illuminate\Http\Response
153
     */
154
    public function postSaveFile(Request $request, $uuid)
155
    {
156
        $server = Models\Server::byUuid($uuid);
157
        $this->authorize('save-files', $server);
158
159
        $controller = new Repositories\Daemon\FileRepository($uuid);
160
161
        try {
162
            $controller->saveFileContents($request->input('file'), $request->input('contents'));
0 ignored issues
show
Bug introduced by
It seems like $request->input('file') targeting Illuminate\Http\Request::input() can also be of type array; however, Pterodactyl\Repositories...ory::saveFileContents() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $request->input('contents') targeting Illuminate\Http\Request::input() can also be of type array; however, Pterodactyl\Repositories...ory::saveFileContents() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
163
164
            return response(null, 204);
165
        } catch (DisplayException $ex) {
166
            return response($ex->getMessage(), 500);
167
        } catch (\Exception $ex) {
168
            Log::error($ex);
169
170
            return response('An error occured while attempting to save this file, please try again.', 500);
171
        }
172
    }
173
174
    /**
175
     * [postSetPrimary description].
176
     * @param  Request $request
177
     * @param  string  $uuid
178
     * @return \Illuminate\Http\Response
179
     */
180
    public function postSetPrimary(Request $request, $uuid)
181
    {
182
        $server = Models\Server::byUuid($uuid)->load('allocations');
183
        $this->authorize('set-connection', $server);
184
185
        if ((int) $request->input('allocation') === $server->allocation_id) {
0 ignored issues
show
Bug introduced by
The property allocation_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...
186
            return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return response()->json(...lt connection.'), 409); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Pterodactyl\Http\Control...troller::postSetPrimary of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
187
                'error' => 'You are already using this as your default connection.',
188
            ], 409);
189
        }
190
191
        try {
192
            $allocation = $server->allocations->where('id', $request->input('allocation'))->where('server_id', $server->id)->first();
0 ignored issues
show
Bug introduced by
The property allocations 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...
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...
193
            if (! $allocation) {
194
                return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return response()->json(...in the system.'), 422); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Pterodactyl\Http\Control...troller::postSetPrimary of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
195
                    'error' => 'No allocation matching your request was found in the system.',
196
                ], 422);
197
            }
198
199
            $repo = new Repositories\ServerRepository;
200
            $repo->changeBuild($server->id, [
201
                'default' => $allocation->ip . ':' . $allocation->port,
202
            ]);
203
204
            return response('The default connection for this server has been updated. Please be aware that you will need to restart your server for this change to go into effect.');
205
        } catch (DisplayValidationException $ex) {
206
            return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return response()->json(...essage(), true)), 422); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Pterodactyl\Http\Control...troller::postSetPrimary of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
207
                'error' => json_decode($ex->getMessage(), true),
208
            ], 422);
209
        } catch (DisplayException $ex) {
210
            return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return response()->json(...x->getMessage()), 503); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Pterodactyl\Http\Control...troller::postSetPrimary of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
211
                'error' => $ex->getMessage(),
212
            ], 503);
213
        } catch (\Exception $ex) {
214
            Log::error($ex);
215
216
            return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return response()->json(...r this server.'), 503); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Pterodactyl\Http\Control...troller::postSetPrimary of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
217
                'error' => 'An unhandled exception occured while attemping to modify the default connection for this server.',
218
            ], 503);
219
        }
220
    }
221
222
    public function postResetDatabasePassword(Request $request, $uuid)
223
    {
224
        $server = Models\Server::byUuid($uuid);
225
        $this->authorize('reset-db-password', $server);
226
227
        $database = Models\Database::where('server_id', $server->id)->findOrFail($request->input('database'));
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...
228
        $repo = new Repositories\DatabaseRepository;
229
230
        try {
231
            $password = str_random(20);
232
            $repo->password($database->id, $password);
233
234
            return response($password);
235
        } catch (DisplayException $ex) {
236
            return response()->json(['error' => $ex->getMessage()], 503);
237
        } catch (\Exception $ex) {
238
            Log::error($ex);
239
240
            return response()->json([
241
                'error' => 'An unhandled error occured while attempting to modify this database\'s password.',
242
            ], 503);
243
        }
244
    }
245
}
246