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

DatabaseRepository::password()   B

Complexity

Conditions 2
Paths 6

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 22
nc 6
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\Repositories;
26
27
use DB;
28
use Crypt;
29
use Config;
30
use Validator;
31
use Pterodactyl\Models;
32
use Pterodactyl\Exceptions\DisplayException;
33
use Pterodactyl\Exceptions\DisplayValidationException;
34
35
class DatabaseRepository
36
{
37
    /**
38
     * Adds a new database to a specified database host server.
39
     *
40
     * @param int   $server   Id of the server to add a database for.
41
     * @param array $options  Array of options for creating that database.
0 ignored issues
show
Bug introduced by
There is no parameter named $options. 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...
42
     *
43
     * @throws \Pterodactyl\Exceptions\DisplayException
44
     * @throws \Pterodactyl\Exceptions\DisplayValidationException
45
     * @throws \Exception
46
     * @return void
47
     */
48
    public function create($server, $data)
49
    {
50
        $server = Models\Server::findOrFail($server);
51
52
        $validator = Validator::make($data, [
53
            'host' => 'required|exists:database_servers,id',
54
            'database' => 'required|regex:/^\w{1,100}$/',
55
            'connection' => 'required|regex:/^[0-9%.]{1,15}$/',
56
        ]);
57
58
        if ($validator->fails()) {
59
            throw new DisplayValidationException($validator->errors());
60
        }
61
62
        $host = Models\DatabaseServer::findOrFail($data['host']);
63
        DB::beginTransaction();
64
65
        try {
66
            $database = Models\Database::firstOrNew([
67
                'server_id' => $server->id,
68
                'db_server' => $data['host'],
69
                'database' => sprintf('s%d_%s', $server->id, $data['database']),
70
            ]);
71
72
            if ($database->exists) {
73
                throw new DisplayException('A database with those details already exists in the system.');
74
            }
75
76
            $database->username = sprintf('s%d_%s', $server->id, str_random(10));
77
            $database->remote = $data['connection'];
78
            $database->password = Crypt::encrypt(str_random(20));
79
80
            $database->save();
81
        } catch (\Exception $ex) {
82
            DB::rollBack();
83
            throw $ex;
84
        }
85
86
        Config::set('database.connections.dynamic', [
87
            'driver' => 'mysql',
88
            'host' => $host->host,
89
            'port' => $host->port,
90
            'database' => 'mysql',
91
            'username' => $host->username,
92
            'password' => Crypt::decrypt($host->password),
93
            'charset'   => 'utf8',
94
            'collation' => 'utf8_unicode_ci',
95
        ]);
96
97
        try {
98
            DB::connection('dynamic')->statement(sprintf('CREATE DATABASE IF NOT EXISTS `%s`', $database->database));
99
            DB::connection('dynamic')->statement(sprintf(
100
                'CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'',
101
                $database->username, $database->remote, Crypt::decrypt($database->password)
102
            ));
103
            DB::connection('dynamic')->statement(sprintf(
104
                'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX ON `%s`.* TO `%s`@`%s`',
105
                $database->database, $database->username, $database->remote
106
            ));
107
108
            DB::connection('dynamic')->statement('FLUSH PRIVILEGES');
109
110
            // Save Everything
111
            DB::commit();
112
        } catch (\Exception $ex) {
113
            try {
114
                DB::connection('dynamic')->statement(sprintf('DROP DATABASE IF EXISTS `%s`', $database->database));
115
                DB::connection('dynamic')->statement(sprintf('DROP USER IF EXISTS `%s`@`%s`', $database->username, $database->remote));
116
                DB::connection('dynamic')->statement('FLUSH PRIVILEGES');
117
            } catch (\Exception $ex) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
118
            }
119
120
            DB::rollBack();
121
            throw $ex;
122
        }
123
    }
124
125
    /**
126
     * Updates the password for a given database.
127
     * @param  int $id The ID of the database to modify.
128
     * @param  string $password The new password to use for the database.
129
     * @return bool
130
     */
131
    public function password($id, $password)
132
    {
133
        $database = Models\Database::with('host')->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...
134
135
        DB::beginTransaction();
136
        try {
137
            $database->password = Crypt::encrypt($password);
138
            $database->save();
139
140
            Config::set('database.connections.dynamic', [
141
                'driver' => 'mysql',
142
                'host' => $database->host->host,
143
                'port' => $database->host->port,
144
                'database' => 'mysql',
145
                'username' => $database->host->username,
146
                'password' => Crypt::decrypt($database->host->password),
147
                'charset'   => 'utf8',
148
                'collation' => 'utf8_unicode_ci',
149
            ]);
150
151
            DB::connection('dynamic')->statement(sprintf(
152
                'SET PASSWORD FOR `%s`@`%s` = PASSWORD(\'%s\')',
153
                $database->username, $database->remote, $password
154
            ));
155
156
            DB::commit();
157
        } catch (\Exception $ex) {
158
            DB::rollBack();
159
            throw $ex;
160
        }
161
    }
162
163
    /**
164
     * Drops a database from the associated MySQL Server.
165
     * @param  int $id The ID of the database to drop.
166
     * @return bool
167
     */
168
    public function drop($id)
169
    {
170
        $database = Models\Database::with('host')->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...
171
172
        DB::beginTransaction();
173
        try {
174
            Config::set('database.connections.dynamic', [
175
                'driver' => 'mysql',
176
                'host' => $database->host->host,
177
                'port' => $database->host->port,
178
                'database' => 'mysql',
179
                'username' => $database->host->username,
180
                'password' => Crypt::decrypt($database->host->password),
181
                'charset'   => 'utf8',
182
                'collation' => 'utf8_unicode_ci',
183
            ]);
184
185
            DB::connection('dynamic')->statement(sprintf('DROP DATABASE IF EXISTS `%s`', $database->database));
186
            DB::connection('dynamic')->statement(sprintf('DROP USER IF EXISTS `%s`@`%s`', $database->username, $database->remote));
187
            DB::connection('dynamic')->statement('FLUSH PRIVILEGES');
188
189
            $database->delete();
190
191
            DB::commit();
192
        } catch (\Exception $ex) {
193
            DB::rollback();
194
            throw $ex;
195
        }
196
    }
197
198
    /**
199
     * Deletes a database server from the system if it is empty.
200
     *
201
     * @param  int $server The ID of the Database Server.
202
     * @return
203
     */
204
    public function delete($server)
205
    {
206
        $host = Models\DatabaseServer::withCount('databases')->findOrFail($server);
207
208
        if ($host->databases_count > 0) {
209
            throw new DisplayException('You cannot delete a database server that has active databases attached to it.');
210
        }
211
212
        return $host->delete();
213
    }
214
215
    /**
216
     * Adds a new Database Server to the system.
217
     * @param array $data
218
     */
219
    public function add(array $data)
220
    {
221
        if (isset($data['host'])) {
222
            $data['host'] = gethostbyname($data['host']);
223
        }
224
225
        $validator = Validator::make($data, [
226
            'name' => 'required|string|max:255',
227
            'host' => 'required|ip|unique:database_servers,host',
228
            'port' => 'required|numeric|between:1,65535',
229
            'username' => 'required|string|max:32',
230
            'password' => 'required|string',
231
            'linked_node' => 'sometimes',
232
        ]);
233
234
        if ($validator->fails()) {
235
            throw new DisplayValidationException($validator->errors());
236
        }
237
238
        DB::beginTransaction();
239
        try {
240
            Config::set('database.connections.dynamic', [
241
                'driver' => 'mysql',
242
                'host' => $data['host'],
243
                'port' => $data['port'],
244
                'database' => 'mysql',
245
                'username' => $data['username'],
246
                'password' => $data['password'],
247
                'charset'   => 'utf8',
248
                'collation' => 'utf8_unicode_ci',
249
            ]);
250
251
            // Allows us to check that we can connect to things.
252
            DB::connection('dynamic')->select('SELECT 1 FROM dual');
253
254
            Models\DatabaseServer::create([
255
                'name' => $data['name'],
256
                'host' => $data['host'],
257
                'port' => $data['port'],
258
                'username' => $data['username'],
259
                'password' => Crypt::encrypt($data['password']),
260
                'max_databases' => null,
261
                'linked_node' => (! empty($data['linked_node']) && $data['linked_node'] > 0) ? $data['linked_node'] : null,
262
            ]);
263
264
            DB::commit();
265
        } catch (\Exception $ex) {
266
            DB::rollBack();
267
            throw $ex;
268
        }
269
    }
270
}
271