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 ( a1d3bb...b96e5a )
by Dane
02:57
created

AjaxController::postSetPrimary()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 41
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.439
c 0
b 0
f 0
cc 6
eloc 30
nc 18
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 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::getByUUID($uuid);
71
72
        if (! $server) {
73
            return response()->json([], 404);
74
        }
75
76
        $client = Models\Node::guzzleRequest($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...
77
78
        try {
79
            $res = $client->request('GET', '/server', [
80
                'headers' => Models\Server::getGuzzleHeaders($uuid),
81
            ]);
82
            if ($res->getStatusCode() === 200) {
83
                return response()->json(json_decode($res->getBody()));
84
            }
85
        } catch (RequestException $e) {
86
            //
87
        }
88
89
        return response()->json([]);
90
    }
91
92
    /**
93
     * Returns a listing of files in a given directory for a server.
94
     *
95
     * @param  \Illuminate\Http\Request $request
96
     * @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...
97
     * @return \Illuminate\Contracts\View\View
98
     */
99
    public function postDirectoryList(Request $request, $uuid)
100
    {
101
        $server = Models\Server::getByUUID($uuid);
102
        $this->directory = '/' . trim(urldecode($request->input('directory', '/')), '/');
103
        $this->authorize('list-files', $server);
104
105
        $prevDir = [
106
            'header' => ($this->directory !== '/') ? $this->directory : '',
107
        ];
108
        if ($this->directory !== '/') {
109
            $prevDir['first'] = true;
110
        }
111
112
        // Determine if we should show back links in the file browser.
113
        // This code is strange, and could probably be rewritten much better.
114
        $goBack = explode('/', trim($this->directory, '/'));
115
        if (! empty(array_filter($goBack)) && count($goBack) >= 2) {
116
            $prevDir['show'] = true;
117
            array_pop($goBack);
118
            $prevDir['link'] = '/' . implode('/', $goBack);
119
            $prevDir['link_show'] = implode('/', $goBack) . '/';
120
        }
121
122
        $controller = new Repositories\Daemon\FileRepository($uuid);
123
124
        try {
125
            $directoryContents = $controller->returnDirectoryListing($this->directory);
126
        } catch (DisplayException $ex) {
127
            return response($ex->getMessage(), 500);
128
        } catch (\Exception $ex) {
129
            Log::error($ex);
130
131
            return response('An error occured while attempting to load the requested directory, please try again.', 500);
132
        }
133
134
        return view('server.files.list', [
135
            'server' => $server,
136
            'files' => $directoryContents->files,
137
            'folders' => $directoryContents->folders,
138
            'editableMime' => Repositories\HelperRepository::editableFiles(),
139
            'directory' => $prevDir,
140
        ]);
141
    }
142
143
    /**
144
     * Handles a POST request to save a file.
145
     *
146
     * @param  Request $request
147
     * @param  string  $uuid
148
     * @return \Illuminate\Http\Response
149
     */
150
    public function postSaveFile(Request $request, $uuid)
151
    {
152
        $server = Models\Server::getByUUID($uuid);
153
        $this->authorize('save-files', $server);
154
155
        $controller = new Repositories\Daemon\FileRepository($uuid);
156
157
        try {
158
            $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...
159
160
            return response(null, 204);
161
        } catch (DisplayException $ex) {
162
            return response($ex->getMessage(), 500);
163
        } catch (\Exception $ex) {
164
            Log::error($ex);
165
166
            return response('An error occured while attempting to save this file, please try again.', 500);
167
        }
168
    }
169
170
    /**
171
     * [postSetPrimary description].
172
     * @param  Request $request
173
     * @param  string  $uuid
174
     * @return \Illuminate\Http\Response
175
     */
176
    public function postSetPrimary(Request $request, $uuid)
177
    {
178
        $server = Models\Server::getByUUID($uuid);
179
        $this->authorize('set-connection', $server);
180
181
        if ((int) $request->input('allocation') === $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...
182
            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...
183
                'error' => 'You are already using this as your default connection.',
184
            ], 409);
185
        }
186
187
        try {
188
            $allocation = Models\Allocation::where('id', $request->input('allocation'))->where('assigned_to', $server->id)->first();
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...
189
            if (! $allocation) {
190
                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...
191
                    'error' => 'No allocation matching your request was found in the system.',
192
                ], 422);
193
            }
194
195
            $repo = new Repositories\ServerRepository;
196
            $repo->changeBuild($server->id, [
197
                'default' => $allocation->ip . ':' . $allocation->port,
198
            ]);
199
200
            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.');
201
        } catch (DisplayValidationException $ex) {
202
            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...
203
                'error' => json_decode($ex->getMessage(), true),
204
            ], 422);
205
        } catch (DisplayException $ex) {
206
            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...
207
                'error' => $ex->getMessage(),
208
            ], 503);
209
        } catch (\Exception $ex) {
210
            Log::error($ex);
211
212
            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...
213
                'error' => 'An unhandled exception occured while attemping to modify the default connection for this server.',
214
            ], 503);
215
        }
216
    }
217
218
    public function postResetDatabasePassword(Request $request, $uuid)
219
    {
220
        $server = Models\Server::getByUUID($uuid);
221
        $database = Models\Database::where('id', $request->input('database'))->where('server_id', $server->id)->firstOrFail();
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...
Unused Code introduced by
$database is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
222
223
        $this->authorize('reset-db-password', $server);
224
        try {
225
            $repo = new Repositories\DatabaseRepository;
226
            $password = str_random(16);
227
            $repo->modifyPassword($request->input('database'), $password);
0 ignored issues
show
Documentation introduced by
$request->input('database') is of type string|array, but the function expects a integer.

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...
228
229
            return response($password);
230
        } catch (\Pterodactyl\Exceptions\DisplayException $ex) {
231
            return response()->json([
232
                'error' => $ex->getMessage(),
233
            ], 503);
234
        } catch (\Exception $ex) {
235
            Log::error($ex);
236
237
            return response()->json([
238
                'error' => 'An unhandled error occured while attempting to modify this database\'s password.',
239
            ], 503);
240
        }
241
    }
242
}
243