Skip to content

Commit

Permalink
Import / Export function
Browse files Browse the repository at this point in the history
  • Loading branch information
andreapollastri committed May 9, 2020
1 parent b7beb25 commit 6692bc3
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 14 deletions.
1 change: 1 addition & 0 deletions app/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Alias extends Model
{

protected $fillable = [
'id',
'domain',
'application_id',
'aliascode'
Expand Down
1 change: 1 addition & 0 deletions app/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Application extends Model
{

protected $fillable = [
'id',
'server_id',
'username',
'password',
Expand Down
30 changes: 22 additions & 8 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,31 @@ public function exportCipi() {

public function importCipi(Request $request) {
$data = $request->input('cipikey');
$data = 'RFAzTFd2bE5YNHozcG1ZeHU3c0pCNVB0c2IvMDZUYkR4UFhvQy9Pd2dmVzFxU1gzU3ZmcHRlSVVlWTZzcDF0bW1iRGRmYkhjdWpBWlNqVW9CVi9wd2pqUUpEMHhRNGMwSElWZmVwYWdtUHY4V2NaVTNJdjBSd3AvZmVHNlVOcm13Mm1pYmdLQUl5TjBlVHhMYjdvV2xLZ0JUenBlVDQvMThPNURmbjFsMy9qTkJ5cHFiSTRydmRjMmhNTkt6S3Q2NTdvNEpYZlZRTHYxckNRM2RxVklmZGNWQVhsdVhCOS9DRW4rTG1RS0Jzek5pTmc3b0ZRR2dhdkMzMDAzdHpKR2xkQWdVT25INFJJWnJFVGNxYXhkMmxxSllQNGlkYWJheHltM0JwR0I2VlljQkdPYUZrTitLNG1abjRQc2ozNWJDR2lFcWpSY0lWMlJ1YUJXRnV0dVJDZVZVSjNnOXlOWmc1WDRwT1FXRldGc2swblhHNG9wcHkrdzBTd3hSWFhoaVZUdWtiZlhKSnc3UURVd2VyWGd6L0VYYStNZ2tZVFpKd25hUzhZUzlDendkUlNrRVJ2RlF1T0VEOFZ4TTUySS9YNDEwNHJpTGZpQWoweEVuUmhMdnRsekI4OVAzaWllNHF1T01sbHJwWEV6ajkyWTVlMVhmcnRnWXNnVERmcDI6Ohl9gSSY9a7hKz54a1+aLmA=';
$encryption_key = base64_decode('#CiPi-MiGrAtIoN@v2');
list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);
$data = openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);
$data = explode('###CIPIBR###', $data);
$servers = explode('###CIPISERVER###', $data[0]);
$applications = explode('###CIPIAPPLICATION###', $data[1]);
$aliases = explode('###CIPIALIAS###', $data[2]);
if($servers && $applications && $aliases) {
try {
list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);
} catch (\Throwable $th) {
$request->session()->flash('alert-error', 'Invalid Cipi migration key. Retry!');
return redirect('/settings');
}
try {
$data = openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);
} catch (\Throwable $th) {
$request->session()->flash('alert-error', 'Invalid Cipi migration key. Retry!');
return redirect('/settings');
}
if(strpos($data, '###CIPIBR###') === false) {
$request->session()->flash('alert-error', 'Invalid Cipi migration key. Retry!');
return redirect('/settings');
} else {
Alias::query()->delete();
Application::query()->delete();
Server::query()->delete();
}
$data = explode('###CIPIBR###', $data);
$servers = explode('###CIPISERVER###', $data[0]);
$applications = explode('###CIPIAPPLICATION###', $data[1]);
$aliases = explode('###CIPIALIAS###', $data[2]);
foreach ($servers as $server) {
if($server) {
$server = explode(',', $server);
Expand Down Expand Up @@ -148,6 +160,8 @@ public function importCipi(Request $request) {
]);
}
}
$request->session()->flash('alert-success', 'Your migration has been imported.');
return redirect('/settings');
}


Expand Down
1 change: 1 addition & 0 deletions app/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Server extends Model
{

protected $fillable = [
'id',
'name',
'provider',
'location',
Expand Down
115 changes: 110 additions & 5 deletions resources/views/settings.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
<a href="#" class="btn btn-sm btn-primary shadow-sm float-right" data-toggle="modal" data-target="#apikeyModal">
<i class="fas fa-key fa-sm text-white-50"></i> API KEY
</a>
<a href="#" class="btn btn-sm btn-primary shadow-sm float-right" style="margin-right:8px" data-toggle="modal" data-target="#apikeyModal">
<i class="fas fa-key fa-sm text-white-50"></i> EXPORT
<a href="#" class="btn btn-sm btn-primary shadow-sm float-right" style="margin-right:8px" data-toggle="modal" data-target="#exportModal">
<i class="fas fa-arrow-down text-white-50"></i> EXPORT
</a>
<a href="#" class="btn btn-sm btn-primary shadow-sm float-right" style="margin-right:8px" data-toggle="modal" data-target="#apikeyModal">
<i class="fas fa-key fa-sm text-white-50"></i> IMPORT
<a href="#" class="btn btn-sm btn-primary shadow-sm float-right" style="margin-right:8px" data-toggle="modal" data-target="#importModal">
<i class="fas fa-arrow-up fa-sm text-white-50"></i> IMPORT
</a>
</div>
</div>
Expand Down Expand Up @@ -218,7 +218,80 @@
<i class="fas fa-sync fa-sm text-white-50"></i> Renew App Secret (confirm required)
</a>
<a href="#" class="btn btn-danger shadow-sm" id="apikey-confirm">
<i class="fas fa-exclamation fa-sm text-white-50"></i> Are you really sure to renew App Secret?
Are you really sure to renew App Secret?
</a>
</div>
</div>
<div class="space"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- IMPORT -->
<div class="modal fade" id="importModal" tabindex="-1" role="dialog" aria-labelledby="importModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form action="/settings/import" method="POST">
@csrf
<div class="modal-header">
<h5 class="modal-title" id="importModalLabel">Import Cipi migration</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<div class="space"></div>
<div class="row">
<div class="col-sm-12 text-center">
Copy your migration key into this area:<br>
<textarea name="cipikey" class="form-group" style="width:100%;height:80px;"></textarea><br>
<b><i>All data in current Cipi db will be delete!</b></i>
<div class="space"></div>
</div>
</div>
<div class="row">
<div class="col-sm-12 text-center">
<a href="#" class="btn btn-sm btn-primary shadow-sm" id="import-confirm">
<i class="fas fa-arrow-up fa-sm text-white-50"></i> Import migration
</a>
<input type="submit" value="Click to confirm migration import" class="btn btn-danger shadow-sm" id="import-submit">
</div>
</div>
<div class="space"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
<!-- EXPORT -->
<div class="modal fade" id="exportModal" tabindex="-1" role="dialog" aria-labelledby="exportModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exportModalLabel">Export Cipi migration</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<div class="space"></div>
<div class="row">
<div class="col-sm-12 text-center">
This is your Cipi migration key:<br>
<textarea name="cipikey" class="form-group" style="width:100%;height:80px;" id="export-key" readonly>Hold on... Cipi is generating a new migration key...</textarea><br>
<div class="space"></div>
</div>
</div>
<div class="row">
<div class="col-sm-12 text-center">
<a href="#" class="btn btn-sm btn-primary shadow-sm" id="export-copy">
<i class="fas fa-arrow-down fa-sm text-white-50"></i> Copy migration key to clipboard
</a>
</div>
</div>
Expand Down Expand Up @@ -292,4 +365,36 @@
});
});
</script>
<script>
$('#importModal').on('show.bs.modal', function (event) {
$('#import-confirm').show();
$('#import-submit').hide();
});
$('#import-confirm').click(function() {
$('#import-confirm').hide();
$('#import-submit').show();
});
</script>
<script>
$('#exportModal').on('show.bs.modal', function (event) {
$.ajax({
url: "/settings/export",
type: "GET",
success: function(response){
$("#export-key").empty();
$("#export-key").val(response);
},
error: function(response) {
$("#export-key").empty();
$("#export-key").val('Error. Retry!');
}
});
})
</script>
<script>
$("#export-copy").click(function(){
$("#export-key").select();
document.execCommand('copy');
});
</script>
@endsection
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@
Route::post('/smtp', 'SettingsController@updateSmtp');
Route::get('/secret', 'SettingsController@updateSecret');
Route::get('/export', 'SettingsController@exportCipi');
Route::get('/import', 'SettingsController@importCipi');
Route::post('/import', 'SettingsController@importCipi');
});
});

0 comments on commit 6692bc3

Please sign in to comment.