diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index 49927b8c..e1cba4cd 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -7,6 +7,7 @@ use App\Models\ExamProgress; use App\Models\ExamUser; use App\Models\User; use App\Repositories\ExamRepository; +use App\Repositories\TorrentRepository; use Carbon\Carbon; use Illuminate\Console\Command; use Illuminate\Support\Facades\DB; @@ -61,8 +62,10 @@ class Test extends Command // $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]'); - dd($r); + $rep = new TorrentRepository(); + $r1 = $rep->encryptDownHash(1, 1); + $r2 = $rep->decryptDownHash($r1); + dd($r1, $r2); } } diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 976a843f..f32e1c22 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -2,11 +2,14 @@ namespace App\Repositories; +use Illuminate\Encryption\Encrypter; use Illuminate\Support\Str; class BaseRepository { - protected function getSortFieldAndType(array $params) + private static $enctyper; + + protected function getSortFieldAndType(array $params): array { $field = $params['sort_field'] ?? 'id'; $type = 'desc'; @@ -15,4 +18,5 @@ class BaseRepository } return [$field, $type]; } + } diff --git a/app/Repositories/ToolRepository.php b/app/Repositories/ToolRepository.php index 41aee68a..7588bc79 100644 --- a/app/Repositories/ToolRepository.php +++ b/app/Repositories/ToolRepository.php @@ -2,11 +2,14 @@ namespace App\Repositories; use App\Models\Setting; +use Illuminate\Encryption\Encrypter; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; class ToolRepository extends BaseRepository { + private static $encrypter; public function backupWeb(): array { @@ -142,4 +145,18 @@ class ToolRepository extends BaseRepository do_log("Final result: " . json_encode($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'); + } } diff --git a/app/Repositories/TorrentRepository.php b/app/Repositories/TorrentRepository.php index 6620142a..5209298a 100644 --- a/app/Repositories/TorrentRepository.php +++ b/app/Repositories/TorrentRepository.php @@ -254,4 +254,23 @@ class TorrentRepository extends BaseRepository 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); + } + + + } diff --git a/include/globalfunctions.php b/include/globalfunctions.php index 4cb6db02..6b09e071 100644 --- a/include/globalfunctions.php +++ b/include/globalfunctions.php @@ -423,9 +423,12 @@ function isHttps() function getSchemeAndHttpHost() { + global $BASEURL; + if (isRunningInConsole()) { + return $BASEURL; + } $isHttps = isHttps(); $protocol = $isHttps ? 'https' : 'http'; - $port = $_SERVER['SERVER_PORT']; $result = "$protocol://" . $_SERVER['HTTP_HOST']; return $result; @@ -574,3 +577,8 @@ function nexus_trans($key, $replace = [], $locale = null) } return $result; } + +function isRunningInConsole(): bool +{ + return php_sapi_name() == 'cli'; +}