Files
faveo/app/Http/Controllers/Utility/LibraryController.php
2016-06-13 20:41:55 +05:30

130 lines
4.2 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers\Utility;
use App\Http\Controllers\Controller;
use Config;
use Exception;
use Schema;
class LibraryController extends Controller
{
public static function getFileVersion()
{
try {
$app = Config::get('app.version');
if ($app) {
return preg_replace('/[^0-9,.]/', '', $app);
} else {
return 0;
}
} catch (Exception $ex) {
return 0;
}
}
public static function getDatabaseVersion()
{
try {
$database = self::isDatabaseSetup();
if ($database == true) {
if (Schema::hasColumn('settings_system', 'version')) {
return \DB::table('settings_system')->where('id', '=', '1')->first()->version;
} else {
return 0;
}
} else {
return 0;
}
} catch (Exception $ex) {
return 0;
}
}
public static function isDatabaseSetup()
{
try {
if (Schema::hasTable('settings_system')) {
return true;
}
} catch (Exception $e) {
return false;
}
}
public static function encryptByFaveoPublicKey($plaintext)
{
try {
// Compress the data to be sent
$plaintext = gzcompress($plaintext);
// Get the public Key of the recipient
$path = storage_path('app'.DIRECTORY_SEPARATOR.'faveo-public.key');
$key_content = file_get_contents($path);
//dd($path);
$publicKey = openssl_pkey_get_public($key_content);
//dd($publicKey);
$a_key = openssl_pkey_get_details($publicKey);
// Encrypt the data in small chunks and then combine and send it.
$chunkSize = ceil($a_key['bits'] / 8) - 11;
$output = '';
while ($plaintext) {
$chunk = substr($plaintext, 0, $chunkSize);
$plaintext = substr($plaintext, $chunkSize);
$encrypted = '';
if (!openssl_public_encrypt($chunk, $encrypted, $publicKey)) {
throw new Exception('Failed to encrypt data');
}
$output .= $encrypted;
}
openssl_free_key($publicKey);
// This is the final encrypted data to be sent to the recipient
$encrypted = $output;
return $encrypted;
} catch (Exception $ex) {
dd($ex);
}
}
public static function decryptByFaveoPrivateKey($encrypted)
{
try {
//$encrypted = p¥Ùn¿olÓ¥9)OÞݸÔvh§=Ìtt1rkC‰É§%YœfÐS\BâkHW€mùÌØg¹+VŠ¥²?áÙ{/<¶¡£e¡ˆr°(V)Öíàr„Ž]K9¤ÿÖ¡Åmž”üÈoò״µºŽ06¼e €rœ['4çhH¾ö:¨œS„œ¦,|¤ÇqÂrÈŸd+ml‡ uötφûóŽ&áyÙ(ÆŒÁ$¥±Zj*îàÒöLˆD†aɐö_§è¶°·V„Þú]%ÅR*B=žéršæñ*i+፭±èç|c¹Ñߟ­F$;
// Get the private Key
$path = storage_path('app'.DIRECTORY_SEPARATOR.'faveo-private.key');
$key_content = file_get_contents($path);
if (!$privateKey = openssl_pkey_get_private($key_content)) {
die('Private Key failed');
}
$a_key = openssl_pkey_get_details($privateKey);
// Decrypt the data in the small chunks
$chunkSize = ceil($a_key['bits'] / 8);
$output = '';
while ($encrypted) {
$chunk = substr($encrypted, 0, $chunkSize);
$encrypted = substr($encrypted, $chunkSize);
$decrypted = '';
if (!openssl_private_decrypt($chunk, $decrypted, $privateKey)) {
die('Failed to decrypt data');
}
$output .= $decrypted;
}
openssl_free_key($privateKey);
// Uncompress the unencrypted data.
$output = gzuncompress($output);
echo '<br /><br /> Unencrypted Data: '.$output;
} catch (Exception $ex) {
dd($ex);
}
}
}