Skip to content

Commit cfd5e0e

Browse files
committed
Implement base service file modification through panel
1 parent b8a6a15 commit cfd5e0e

File tree

19 files changed

+855
-1
lines changed

19 files changed

+855
-1
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
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+
namespace Pterodactyl\Console\Commands;
25+
26+
use Carbon;
27+
use Storage;
28+
use Illuminate\Console\Command;
29+
30+
class CleanServiceBackup extends Command
31+
{
32+
/**
33+
* The name and signature of the console command.
34+
*
35+
* @var string
36+
*/
37+
protected $signature = 'pterodactyl:cleanservices';
38+
39+
/**
40+
* The console command description.
41+
*
42+
* @var string
43+
*/
44+
protected $description = 'Cleans .bak files assocaited with service backups whene editing files through the panel.';
45+
46+
/**
47+
* Create a new command instance.
48+
*
49+
* @return void
50+
*/
51+
public function __construct()
52+
{
53+
parent::__construct();
54+
}
55+
56+
/**
57+
* Execute the console command.
58+
*
59+
* @return mixed
60+
*/
61+
public function handle()
62+
{
63+
$files = Storage::files('services/.bak');
64+
65+
foreach($files as $file) {
66+
$lastModified = Carbon::createFromTimestamp(Storage::lastModified($file));
67+
if ($lastModified->diffInMinutes(Carbon::now()) > 5) {
68+
$this->info('Deleting ' . $file);
69+
Storage::delete($file);
70+
}
71+
}
72+
}
73+
}

app/Console/Kernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Kernel extends ConsoleKernel
2121
\Pterodactyl\Console\Commands\ClearTasks::class,
2222
\Pterodactyl\Console\Commands\ClearServices::class,
2323
\Pterodactyl\Console\Commands\UpdateEmailSettings::class,
24+
\Pterodactyl\Console\Commands\CleanServiceBackup::class,
2425
];
2526

2627
/**

app/Http/Controllers/Admin/ServiceController.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use DB;
2828
use Log;
2929
use Validator;
30+
use Storage;
3031

3132
use Pterodactyl\Models;
3233
use Pterodactyl\Repositories\ServiceRepository;
@@ -274,4 +275,36 @@ public function deleteVariable(Request $request, $service, $option, $variable)
274275
return redirect()->route('admin.services.option', [$service, $option]);
275276
}
276277

278+
public function getConfiguration(Request $request, $serviceId)
279+
{
280+
$service = Models\Service::findOrFail($serviceId);
281+
return view('admin.services.config', [
282+
'service' => $service,
283+
'contents' => [
284+
'json' => Storage::get('services/' . $service->file . '/main.json'),
285+
'index' => Storage::get('services/' . $service->file . '/index.js')
286+
]
287+
]);
288+
}
289+
290+
public function postConfiguration(Request $request, $serviceId)
291+
{
292+
try {
293+
$repo = new ServiceRepository\Service;
294+
$repo->updateFile($serviceId, $request->except([
295+
'_token'
296+
]));
297+
return response('', 204);
298+
} catch (DisplayException $ex) {
299+
return response()->json([
300+
'error' => $ex->getMessage()
301+
], 503);
302+
} catch (\Exception $ex) {
303+
Log::error($ex);
304+
return response()->json([
305+
'error' => 'An error occured while attempting to save the file.'
306+
], 503);
307+
}
308+
}
309+
277310
}

app/Http/Routes/AdminRoutes.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,15 @@ public function map(Router $router) {
379379
'uses' => 'Admin\ServiceController@deleteService'
380380
]);
381381

382+
$router->get('/service/{id}/configuration', [
383+
'as' => 'admin.services.service.config',
384+
'uses' => 'Admin\ServiceController@getConfiguration'
385+
]);
386+
387+
$router->post('/service/{id}/configuration', [
388+
'uses' => 'Admin\ServiceController@postConfiguration'
389+
]);
390+
382391
$router->get('/service/{service}/option/new', [
383392
'as' => 'admin.services.option.new',
384393
'uses' => 'Admin\ServiceController@newOption'

app/Models/Checksum.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
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+
namespace Pterodactyl\Models;
25+
26+
use Illuminate\Database\Eloquent\Model;
27+
28+
class Checksum extends Model
29+
{
30+
31+
/**
32+
* The table associated with the model.
33+
*
34+
* @var string
35+
*/
36+
protected $table = 'checksums';
37+
38+
/**
39+
* Fields that are not mass assignable.
40+
*
41+
* @var array
42+
*/
43+
protected $guarded = ['id', 'created_at', 'updated_at'];
44+
45+
/**
46+
* Cast values to correct type.
47+
*
48+
* @var array
49+
*/
50+
protected $casts = [
51+
'service' => 'integer'
52+
];
53+
54+
}

app/Repositories/ServiceRepository/Service.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use DB;
2727
use Validator;
2828
use Uuid;
29+
use Storage;
2930

3031
use Pterodactyl\Models;
3132
use Pterodactyl\Services\UuidService;
@@ -110,4 +111,44 @@ public function delete($id)
110111
}
111112
}
112113

114+
public function updateFile($id, array $data)
115+
{
116+
$service = Models\Service::findOrFail($id);
117+
118+
$validator = Validator::make($data, [
119+
'file' => 'required|in:index,main',
120+
'contents' => 'required|string'
121+
]);
122+
123+
if ($validator->fails()) {
124+
throw new DisplayValidationException($validator->errors());
125+
}
126+
127+
$filename = ($data['file'] === 'main') ? 'main.json' : 'index.js';
128+
$filepath = 'services/' . $service->file . '/' . $filename;
129+
$backup = 'services/.bak/' . str_random(12) . '.bak';
130+
131+
DB::beginTransaction();
132+
133+
try {
134+
Storage::move($filepath, $backup);
135+
Storage::put($filepath, $data['contents']);
136+
137+
$checksum = Models\Checksum::firstOrNew([
138+
'service' => $service->id,
139+
'filename' => $filename
140+
]);
141+
142+
$checksum->checksum = sha1_file(storage_path('app/' . $filepath));
143+
$checksum->save();
144+
145+
DB::commit();
146+
} catch(\Exception $ex) {
147+
DB::rollback();
148+
Storage::move($backup, $filepath);
149+
throw $ex;
150+
}
151+
152+
}
153+
113154
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddChecksumsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('checksums', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->integer('service')->unsigned();
19+
$table->string('filename');
20+
$table->char('checksum', 40);
21+
$table->timestamps();
22+
23+
$table->foreign('service')->references('id')->on('services');
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::drop('checksums');
35+
}
36+
}

0 commit comments

Comments
 (0)