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 ( 2dbaca...c09170 )
by Dane
12s
created

NodesController::getConfigurationToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
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\Admin;
26
27
use DB;
28
use Log;
29
use Alert;
30
use Carbon;
31
use Validator;
32
use Pterodactyl\Models;
33
use Illuminate\Http\Request;
34
use Pterodactyl\Exceptions\DisplayException;
35
use Pterodactyl\Http\Controllers\Controller;
36
use Pterodactyl\Repositories\NodeRepository;
37
use Pterodactyl\Exceptions\DisplayValidationException;
38
39
class NodesController extends Controller
40
{
41
    /**
42
     * Controller Constructor.
43
     */
44
    public function __construct()
45
    {
46
        //
47
    }
48
49
    public function getScript(Request $request, $id)
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...
50
    {
51
        return response()->view('admin.nodes.remote.deploy', ['node' => Models\Node::findOrFail($id)])->header('Content-Type', 'text/plain');
52
    }
53
54
    public function getIndex(Request $request)
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...
55
    {
56
        return view('admin.nodes.index', [
57
            'nodes' => Models\Node::select(
58
                'nodes.*',
59
                'locations.long as a_locationName',
60
                DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node = nodes.id) as a_serverCount')
61
            )->join('locations', 'nodes.location', '=', 'locations.id')->paginate(20),
62
        ]);
63
    }
64
65
    public function getNew(Request $request)
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...
66
    {
67
        if (! Models\Location::all()->count()) {
68
            Alert::warning('You must add a location before you can add a new node.')->flash();
69
70
            return redirect()->route('admin.locations');
71
        }
72
73
        return view('admin.nodes.new', [
74
            'locations' => Models\Location::all(),
75
        ]);
76
    }
77
78
    public function postNew(Request $request)
79
    {
80
        try {
81
            $node = new NodeRepository;
82
            $new = $node->create($request->except([
83
                '_token',
84
            ]));
85
            Alert::success('Successfully created new node. <strong>Before you can add any servers you need to first assign some IP addresses and ports.</strong>')->flash();
86
            Alert::info('<strong>To simplify the node setup you can generate a token on the configuration tab.</strong>')->flash();
87
88
            return redirect()->route('admin.nodes.view', [
89
                'id' => $new,
90
                'tab' => 'tab_allocation',
91
            ]);
92
        } catch (DisplayValidationException $e) {
93
            return redirect()->route('admin.nodes.new')->withErrors(json_decode($e->getMessage()))->withInput();
94
        } catch (DisplayException $e) {
95
            Alert::danger($e->getMessage())->flash();
96
        } catch (\Exception $e) {
97
            Log::error($e);
98
            Alert::danger('An unhandled exception occured while attempting to add this node. Please try again.')->flash();
99
        }
100
101
        return redirect()->route('admin.nodes.new')->withInput();
102
    }
103
104
    public function getView(Request $request, $id)
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...
105
    {
106
        $node = Models\Node::findOrFail($id);
107
108
        return view('admin.nodes.view', [
109
            'node' => $node,
110
            'servers' => Models\Server::select('servers.*', 'users.email as a_ownerEmail', 'services.name as a_serviceName')
111
                ->join('users', 'users.id', '=', 'servers.owner')
112
                ->join('services', 'services.id', '=', 'servers.service')
113
                ->where('node', $id)->paginate(10, ['*'], 'servers'),
114
            'stats' => Models\Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node', $node->id)->first(),
115
            'locations' => Models\Location::all(),
116
            'allocations' => Models\Allocation::select('allocations.*', 'servers.name as assigned_to_name')
117
                ->where('allocations.node', $node->id)
118
                ->leftJoin('servers', 'servers.id', '=', 'allocations.assigned_to')
119
                ->orderBy('allocations.ip', 'asc')
120
                ->orderBy('allocations.port', 'asc')
121
                ->paginate(20, ['*'], 'allocations'),
122
            'allocation_ips' => Models\Allocation::select('id', 'ip')
123
                ->where('node', $node->id)
124
                ->groupBy('ip')
125
                ->get(),
126
        ]);
127
    }
128
129
    public function postView(Request $request, $id)
130
    {
131
        try {
132
            $node = new NodeRepository;
133
            $node->update($id, $request->except([
134
                '_token',
135
            ]));
136
            Alert::success('Successfully update this node\'s information. If you changed any daemon settings you will need to restart it now.')->flash();
137
138
            return redirect()->route('admin.nodes.view', [
139
                'id' => $id,
140
                'tab' => 'tab_settings',
141
            ]);
142
        } catch (DisplayValidationException $e) {
143
            return redirect()->route('admin.nodes.view', $id)->withErrors(json_decode($e->getMessage()))->withInput();
144
        } catch (DisplayException $e) {
145
            Alert::danger($e->getMessage())->flash();
146
        } catch (\Exception $e) {
147
            Log::error($e);
148
            Alert::danger('An unhandled exception occured while attempting to edit this node. Please try again.')->flash();
149
        }
150
151
        return redirect()->route('admin.nodes.view', [
152
            'id' => $id,
153
            'tab' => 'tab_settings',
154
        ])->withInput();
155
    }
156
157
    public function deallocateSingle(Request $request, $node, $allocation)
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...
158
    {
159
        $query = Models\Allocation::where('node', $node)->whereNull('assigned_to')->where('id', $allocation)->delete();
160
        if ((int) $query === 0) {
161
            return response()->json([
162
                'error' => 'Unable to find an allocation matching those details to delete.',
163
            ], 400);
164
        }
165
166
        return response('', 204);
167
    }
168
169
    public function deallocateBlock(Request $request, $node)
170
    {
171
        $query = Models\Allocation::where('node', $node)->whereNull('assigned_to')->where('ip', $request->input('ip'))->delete();
172
        if ((int) $query === 0) {
173
            Alert::danger('There was an error while attempting to delete allocations on that IP.')->flash();
174
175
            return redirect()->route('admin.nodes.view', [
176
                'id' => $node,
177
                'tab' => 'tab_allocations',
178
            ]);
179
        }
180
        Alert::success('Deleted all unallocated ports for <code>' . $request->input('ip') . '</code>.')->flash();
181
182
        return redirect()->route('admin.nodes.view', [
183
            'id' => $node,
184
            'tab' => 'tab_allocation',
185
        ]);
186
    }
187
188
    public function setAlias(Request $request, $node)
0 ignored issues
show
Unused Code introduced by
The parameter $node 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...
189
    {
190
        if (! $request->input('allocation')) {
191
            return response('Missing required parameters.', 422);
192
        }
193
194
        try {
195
            $update = Models\Allocation::findOrFail($request->input('allocation'));
196
            $update->ip_alias = (empty($request->input('alias'))) ? null : $request->input('alias');
197
            $update->save();
198
199
            return response('', 204);
200
        } catch (\Exception $ex) {
201
            throw $ex;
202
        }
203
    }
204
205
    public function getAllocationsJson(Request $request, $id)
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...
206
    {
207
        $allocations = Models\Allocation::select('ip')->where('node', $id)->groupBy('ip')->get();
208
209
        return response()->json($allocations);
210
    }
211
212
    public function postAllocations(Request $request, $id)
213
    {
214
        $validator = Validator::make($request->all(), [
215
            'allocate_ip.*' => 'required|string',
216
            'allocate_port.*' => 'required',
217
        ]);
218
219
        if ($validator->fails()) {
220
            return redirect()->route('admin.nodes.view', [
221
                'id' => $id,
222
                'tab' => 'tab_allocation',
223
            ])->withErrors($validator->errors())->withInput();
224
        }
225
226
        $processedData = [];
227
        foreach ($request->input('allocate_ip') as $ip) {
0 ignored issues
show
Bug introduced by
The expression $request->input('allocate_ip') of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
228
            if (! array_key_exists($ip, $processedData)) {
229
                $processedData[$ip] = [];
230
            }
231
        }
232
233
        foreach ($request->input('allocate_port') as $portid => $ports) {
0 ignored issues
show
Bug introduced by
The expression $request->input('allocate_port') of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
234
            if (array_key_exists($portid, $request->input('allocate_ip'))) {
235
                $json = json_decode($ports);
236
                if (json_last_error() === 0 && ! empty($json)) {
237
                    foreach ($json as &$parsed) {
238
                        array_push($processedData[$request->input('allocate_ip')[$portid]], $parsed->value);
239
                    }
240
                }
241
            }
242
        }
243
244
        try {
245
            $node = new NodeRepository;
246
            $node->addAllocations($id, $processedData);
247
            Alert::success('Successfully added new allocations to this node.')->flash();
248
        } catch (DisplayException $e) {
249
            Alert::danger($e->getMessage())->flash();
250
        } catch (\Exception $e) {
251
            Log::error($e);
252
            Alert::danger('An unhandled exception occured while attempting to add allocations this node. Please try again.')->flash();
253
        } finally {
254
            return redirect()->route('admin.nodes.view', [
255
                'id' => $id,
256
                'tab' => 'tab_allocation',
257
            ]);
258
        }
259
    }
260
261 View Code Duplication
    public function deleteNode(Request $request, $id)
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...
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...
262
    {
263
        try {
264
            $repo = new NodeRepository;
265
            $repo->delete($id);
266
            Alert::success('Successfully deleted the requested node from the panel.')->flash();
267
268
            return redirect()->route('admin.nodes');
269
        } catch (DisplayException $e) {
270
            Alert::danger($e->getMessage())->flash();
271
        } catch (\Exception $e) {
272
            Log::error($e);
273
            Alert::danger('An unhandled exception occured while attempting to delete this node. Please try again.')->flash();
274
        }
275
276
        return redirect()->route('admin.nodes.view', [
277
            'id' => $id,
278
            'tab' => 'tab_delete',
279
        ]);
280
    }
281
282
    public function getConfigurationToken(Request $request, $id)
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...
283
    {
284
        // Check if Node exists. Will lead to 404 if not.
285
        Models\Node::findOrFail($id);
286
287
        // Create a token
288
        $token = new Models\NodeConfigurationToken();
289
        $token->node = $id;
0 ignored issues
show
Documentation introduced by
The property node does not exist on object<Pterodactyl\Models\NodeConfigurationToken>. 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...
290
        $token->token = str_random(32);
0 ignored issues
show
Documentation introduced by
The property token does not exist on object<Pterodactyl\Models\NodeConfigurationToken>. 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...
291
        $token->expires_at = Carbon::now()->addMinutes(5); // Expire in 5 Minutes
0 ignored issues
show
Documentation introduced by
The property expires_at does not exist on object<Pterodactyl\Models\NodeConfigurationToken>. 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...
292
        $token->save();
293
294
        $token_response = [
295
            'token' => $token->token,
0 ignored issues
show
Documentation introduced by
The property token does not exist on object<Pterodactyl\Models\NodeConfigurationToken>. 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...
296
            'expires_at' => $token->expires_at->toDateTimeString(),
0 ignored issues
show
Documentation introduced by
The property expires_at does not exist on object<Pterodactyl\Models\NodeConfigurationToken>. 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...
297
        ];
298
299
        return response()->json($token_response, 200);
300
    }
301
}
302