Merge branch 'encrypt' into php8

This commit is contained in:
xiaomlove
2021-06-01 23:34:57 +08:00
5 changed files with 52 additions and 32 deletions
+1 -29
View File
@@ -9,6 +9,7 @@ use App\Models\SearchBox;
use App\Models\User; use App\Models\User;
use App\Repositories\ExamRepository; use App\Repositories\ExamRepository;
use App\Repositories\SearchBoxRepository; use App\Repositories\SearchBoxRepository;
use App\Repositories\TorrentRepository;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Encryption\Encrypter; use Illuminate\Encryption\Encrypter;
@@ -49,36 +50,7 @@ class Test extends Command
*/ */
public function handle() public function handle()
{ {
$rep = new ExamRepository();
// $r = $rep->assignToUser(1, 1);
// $r = $rep->addProgress(1, 1, [
// 1 => 25*1024*1024*1024,
// 2 => 55*3600,
// 3 => 10*1024*1024*1024,
// 4 => 1252
// ]);
// dd($r);
// $rep->assignCronjob();
// $r = $rep->cronjobCheckout();
// $disk = Storage::disk('google_dirve');
// $r = $disk->put('/', base_path('composer.json'));
// $r = DB::table('users')->where('id', 1)->update(['modcomment' => DB::raw("concat_ws(',', 'ddddd', modcomment)")]);
// $r = format_description('[em4] [em27]');
// $rep = new SearchBoxRepository();
// $r = $rep->initSearchBoxField(4);
// $imdb = new \Nexus\Imdb\Imdb();
// $imdb_id = 5768840;
// $r = $imdb->getMovie($imdb_id)->photo(true);
$original = "aaa";
$key = base64_decode('WUbN2wa2kl3E1VDW4iKaH3RBHw3hKY7BK0hWEkBZmGg=');
$cipher = 'AES-256-CBC';
$encrypter = new Encrypter($key, $cipher);
$encrypted = $encrypter->encrypt($original);
$decrypted = $encrypter->decrypt($encrypted);
dd($original, $key, $cipher, $encrypted, $decrypted);
} }
} }
+5 -1
View File
@@ -2,11 +2,14 @@
namespace App\Repositories; namespace App\Repositories;
use Illuminate\Encryption\Encrypter;
use Illuminate\Support\Str; use Illuminate\Support\Str;
class BaseRepository class BaseRepository
{ {
protected function getSortFieldAndType(array $params) private static $enctyper;
protected function getSortFieldAndType(array $params): array
{ {
$field = $params['sort_field'] ?? 'id'; $field = $params['sort_field'] ?? 'id';
$type = 'desc'; $type = 'desc';
@@ -15,4 +18,5 @@ class BaseRepository
} }
return [$field, $type]; return [$field, $type];
} }
} }
+17
View File
@@ -2,11 +2,14 @@
namespace App\Repositories; namespace App\Repositories;
use App\Models\Setting; use App\Models\Setting;
use Illuminate\Encryption\Encrypter;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class ToolRepository extends BaseRepository class ToolRepository extends BaseRepository
{ {
private static $encrypter;
public function backupWeb(): array public function backupWeb(): array
{ {
@@ -142,4 +145,18 @@ class ToolRepository extends BaseRepository
do_log("Final result: " . json_encode($backupResult)); do_log("Final result: " . json_encode($backupResult));
return $backupResult; return $backupResult;
} }
public function getEncrypter(): Encrypter
{
if (!is_null(self::$encrypter)) {
return self::$encrypter;
}
$key = nexus_env('APP_KEY');
$prefix = 'base64:';
if (Str::startsWith($key,$prefix)) {
$key = substr($key, strlen($prefix));
$key = base64_decode($key);
}
return self::$encrypter = new Encrypter($key, 'AES-256-CBC');
}
} }
+19
View File
@@ -254,4 +254,23 @@ class TorrentRepository extends BaseRepository
return "$speed/s"; return "$speed/s";
} }
public function encryptDownHash($id, $uid): string
{
$toolRep = new ToolRepository();
$payload = [
'id' => $id,
'uid' => $uid,
'date' => date('Ymd'),
];
return $toolRep->getEncrypter()->encrypt($payload);
}
public function decryptDownHash($downHash)
{
$toolRep = new ToolRepository();
return $toolRep->getEncrypter()->decrypt($downHash);
}
} }
+9 -1
View File
@@ -424,9 +424,12 @@ function isHttps()
function getSchemeAndHttpHost() function getSchemeAndHttpHost()
{ {
global $BASEURL;
if (isRunningInConsole()) {
return $BASEURL;
}
$isHttps = isHttps(); $isHttps = isHttps();
$protocol = $isHttps ? 'https' : 'http'; $protocol = $isHttps ? 'https' : 'http';
$port = $_SERVER['SERVER_PORT'];
$result = "$protocol://" . $_SERVER['HTTP_HOST']; $result = "$protocol://" . $_SERVER['HTTP_HOST'];
return $result; return $result;
@@ -576,3 +579,8 @@ function nexus_trans($key, $replace = [], $locale = null)
do_log("key: $key, replace: " . nexus_json_encode($replace) . ", locale: $locale, getKey: $getKey, result: $result"); do_log("key: $key, replace: " . nexus_json_encode($replace) . ", locale: $locale, getKey: $getKey, result: $result");
return $result; return $result;
} }
function isRunningInConsole(): bool
{
return php_sapi_name() == 'cli';
}