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 (#286)
by Dane
03:00
created

PackController::uploadForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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\Admin;
26
27
use DB;
28
use Log;
29
use Alert;
30
use Storage;
31
use Pterodactyl\Models;
32
use Illuminate\Http\Request;
33
use Pterodactyl\Exceptions\DisplayException;
34
use Pterodactyl\Http\Controllers\Controller;
35
use Pterodactyl\Repositories\ServiceRepository\Pack;
36
use Pterodactyl\Exceptions\DisplayValidationException;
37
38
class PackController extends Controller
39
{
40
    public function __construct()
41
    {
42
        //
43
    }
44
45
    public function listAll(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...
46
    {
47
        return view('admin.services.packs.index', ['services' => Models\Service::all()]);
48
    }
49
50
    public function listByOption(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...
51
    {
52
        return view('admin.services.packs.byoption', [
53
            'option' => Models\ServiceOptions::with('service', 'packs')->findOrFail($id)
0 ignored issues
show
Bug introduced by
The method findOrFail does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
54
        ]);
55
    }
56
57
    public function listByService(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...
58
    {
59
        return view('admin.services.packs.byservice', [
60
            'service' => Models\Service::with('options', 'options.packs')->findOrFail($id),
0 ignored issues
show
Bug introduced by
The method findOrFail does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
61
        ]);
62
    }
63
64
    public function new(Request $request, $opt = null)
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...
Unused Code introduced by
The parameter $opt 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...
65
    {
66
        return view('admin.services.packs.new', [
67
            'services' => Models\Service::with('options')->get(),
0 ignored issues
show
Bug introduced by
The method get does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
68
        ]);
69
    }
70
71
    public function create(Request $request)
72
    {
73
        try {
74
            $repo = new Pack;
75
            $pack = $repo->create($request->only([
76
                'name',
77
                'version',
78
                'description',
79
                'option',
80
                'selectable',
81
                'visible',
82
                'file_upload',
83
            ]));
84
            Alert::success('Successfully created new service!')->flash();
85
86
            return redirect()->route('admin.services.packs.edit', $pack->id)->withInput();
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Pterodactyl\Models\ServicePack>. 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...
87
        } catch (DisplayValidationException $ex) {
88
            return redirect()->route('admin.services.packs.new', $request->input('option'))->withErrors(json_decode($ex->getMessage()))->withInput();
0 ignored issues
show
Bug introduced by
It seems like $request->input('option') targeting Illuminate\Http\Request::input() can also be of type string; however, Illuminate\Routing\Redirector::route() does only seem to accept array, 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...
89
        } catch (DisplayException $ex) {
90
            Alert::danger($ex->getMessage())->flash();
91
        } catch (\Exception $ex) {
92
            Log::error($ex);
93
            Alert::danger('An error occured while attempting to add a new service pack.')->flash();
94
        }
95
96
        return redirect()->route('admin.services.packs.new', $request->input('option'))->withInput();
0 ignored issues
show
Bug introduced by
It seems like $request->input('option') targeting Illuminate\Http\Request::input() can also be of type string; however, Illuminate\Routing\Redirector::route() does only seem to accept array, 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...
97
    }
98
99
    public function edit(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...
100
    {
101
        $pack = Models\ServicePack::with('option.service')->findOrFail($id);
0 ignored issues
show
Bug introduced by
The method findOrFail does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
102
103
        return view('admin.services.packs.edit', [
104
            'pack' => $pack,
105
            'services' => Models\Service::all()->load('options'),
106
            'files' => Storage::files('packs/' . $pack->uuid),
0 ignored issues
show
Bug introduced by
The method files() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
        ]);
108
    }
109
110
    public function update(Request $request, $id)
111
    {
112
        if (! is_null($request->input('action_delete'))) {
113
            try {
114
                $repo = new Pack;
115
                $repo->delete($id);
116
                Alert::success('The requested service pack has been deleted from the system.')->flash();
117
118
                return redirect()->route('admin.services.packs');
119
            } catch (DisplayException $ex) {
120
                Alert::danger($ex->getMessage())->flash();
121
            } catch (\Exception $ex) {
122
                Log::error($ex);
123
                Alert::danger('An error occured while attempting to delete this pack.')->flash();
124
            }
125
126
            return redirect()->route('admin.services.packs.edit', $id);
127
        } else {
128
            try {
129
                $repo = new Pack;
130
                $repo->update($id, $request->only([
131
                    'name',
132
                    'version',
133
                    'description',
134
                    'option',
135
                    'selectable',
136
                    'visible',
137
                ]));
138
                Alert::success('Service pack has been successfully updated.')->flash();
139
            } catch (DisplayValidationException $ex) {
140
                return redirect()->route('admin.services.packs.edit', $id)->withErrors(json_decode($ex->getMessage()))->withInput();
141
            } catch (\Exception $ex) {
142
                Log::error($ex);
143
                Alert::danger('An error occured while attempting to add edit this pack.')->flash();
144
            }
145
146
            return redirect()->route('admin.services.packs.edit', $id);
147
        }
148
    }
149
150
    public function export(Request $request, $id, $files = false)
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...
151
    {
152
        $pack = Models\ServicePack::findOrFail($id);
153
        $json = [
154
            'name' => $pack->name,
155
            'version' => $pack->version,
156
            'description' => $pack->dscription,
157
            'selectable' => (bool) $pack->selectable,
158
            'visible' => (bool) $pack->visible,
159
        ];
160
161
        $filename = tempnam(sys_get_temp_dir(), 'pterodactyl_');
162
        if ((bool) $files) {
163
            $zip = new \ZipArchive;
164
            if (! $zip->open($filename, \ZipArchive::CREATE)) {
165
                abort(503, 'Unable to open file for writing.');
166
            }
167
168
            $files = Storage::files('packs/' . $pack->uuid);
0 ignored issues
show
Bug introduced by
The method files() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
169
            foreach ($files as $file) {
170
                $zip->addFile(storage_path('app/' . $file), basename(storage_path('app/' . $file)));
171
            }
172
173
            $zip->addFromString('import.json', json_encode($json, JSON_PRETTY_PRINT));
174
            $zip->close();
175
176
            return response()->download($filename, 'pack-' . $pack->name . '.zip')->deleteFileAfterSend(true);
177
        } else {
178
            $fp = fopen($filename, 'a+');
179
            fwrite($fp, json_encode($json, JSON_PRETTY_PRINT));
180
            fclose($fp);
181
182
            return response()->download($filename, 'pack-' . $pack->name . '.json', [
183
                'Content-Type' => 'application/json',
184
            ])->deleteFileAfterSend(true);
185
        }
186
    }
187
188
    public function uploadForm(Request $request, $for = null)
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...
Unused Code introduced by
The parameter $for 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
        return view('admin.services.packs.upload', [
191
            'services' => Models\Service::all()->load('options'),
192
        ]);
193
    }
194
195
    public function postUpload(Request $request)
196
    {
197
        try {
198
            $repo = new Pack;
199
            $pack = $repo->createWithTemplate($request->only(['option', 'file_upload']));
200
            Alert::success('Successfully created new service!')->flash();
201
202
            return redirect()->route('admin.services.packs.edit', $pack->id)->withInput();
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Pterodactyl\Models\ServicePack>. 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...
203
        } catch (DisplayValidationException $ex) {
204
            return redirect()->back()->withErrors(json_decode($ex->getMessage()))->withInput();
205
        } catch (DisplayException $ex) {
206
            Alert::danger($ex->getMessage())->flash();
207
        } catch (\Exception $ex) {
208
            Log::error($ex);
209
            Alert::danger('An error occured while attempting to add a new service pack.')->flash();
210
        }
211
212
        return redirect()->back();
213
    }
214
}
215