Merge remote-tracking branch 'refs/remotes/origin/php8' into php8

This commit is contained in:
lgb
2024-03-20 14:24:52 +08:00
24 changed files with 437 additions and 21 deletions

View File

@@ -3,6 +3,9 @@
namespace App\Console\Commands;
use App\Events\TorrentCreated;
use App\Events\UserDestroyed;
use App\Events\UserDisabled;
use App\Events\UserEnabled;
use Illuminate\Console\Command;
class FireEvent extends Command
@@ -22,7 +25,10 @@ class FireEvent extends Command
protected $description = 'Fire a event, options: --name, --id';
protected array $eventMaps = [
"torrent_created" => TorrentCreated::class
"torrent_created" => TorrentCreated::class,
"user_destroyed" => UserDestroyed::class,
"user_disabled" => UserDisabled::class,
"user_enabled" => UserEnabled::class,
];
/**

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class UserDestroyed
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public int $id;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(int $id)
{
$this->id = $id;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class UserDisabled
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public int $id;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(int $id)
{
$this->id = $id;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class UserEnabled
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public int $id;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(int $id)
{
$this->id = $id;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}

View File

@@ -2,10 +2,11 @@
namespace App\Filament\Resources\Oauth;
use App\Filament\OptionsTrait;
use App\Filament\PageListSingle;
use App\Filament\Resources\Oauth\ClientResource\Pages;
use App\Filament\Resources\Oauth\ClientResource\RelationManagers;
use Laravel\Passport\Client;
use App\Models\OauthClient;
use Filament\Forms;
use Filament\Resources\Form;
use Filament\Resources\Resource;
@@ -16,7 +17,9 @@ use Illuminate\Database\Eloquent\SoftDeletingScope;
class ClientResource extends Resource
{
protected static ?string $model = Client::class;
use OptionsTrait;
protected static ?string $model = OauthClient::class;
protected static ?string $navigationIcon = 'heroicon-o-collection';
@@ -40,6 +43,11 @@ class ClientResource extends Resource
->schema([
Forms\Components\TextInput::make('name')->label(__('label.name')),
Forms\Components\TextInput::make('redirect')->label(__('oauth.redirect')),
Forms\Components\Radio::make('skips_authorization')
->options(self::getYesNoOptions())
->inline()
->default(0)
->label(__('oauth.skips_authorization')),
]);
}
@@ -52,6 +60,10 @@ class ClientResource extends Resource
Tables\Columns\TextColumn::make('name')->label(__('label.name')),
Tables\Columns\TextColumn::make('secret')->label(__('oauth.secret')),
Tables\Columns\TextColumn::make('redirect')->label(__('oauth.redirect')),
Tables\Columns\IconColumn::make('skips_authorization')
->boolean()
->label(__('oauth.skips_authorization'))
,
])
->filters([

View File

@@ -18,10 +18,10 @@ class BootNexus
public function handle(Request $request, Closure $next)
{
Nexus::boot();
do_log(sprintf(
"Nexus booted. request.server: %s, request.header: %s, request.query: %s, request.input: %s",
nexus_json_encode($request->server()), nexus_json_encode($request->header()), nexus_json_encode($request->query()), nexus_json_encode($request->input())
));
// do_log(sprintf(
// "Nexus booted. request.server: %s, request.header: %s, request.query: %s, request.input: %s",
// nexus_json_encode($request->server()), nexus_json_encode($request->header()), nexus_json_encode($request->query()), nexus_json_encode($request->input())
// ));
return $next($request);
}

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Listeners;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\InteractsWithQueue;
use Laravel\Passport\Passport;
class RemoveOauthTokens implements ShouldQueue
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
{
$uid = $event->id;
$modelNames = [
Passport::$authCodeModel,
Passport::$tokenModel,
];
foreach ($modelNames as $name) {
/**
* @var $model Model
*/
$model = new $name();
$model::query()->where("user_id", $uid)->forceDelete();
}
do_log(sprintf("success remove user: %d oauth tokens related.", $uid));
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Models;
use Illuminate\Support\Str;
use Laravel\Passport\Client;
class OauthClient extends Client
{
protected static function booted(): void
{
static::creating(function (OauthClient $model) {
$model->secret = Str::random(40);
});
}
public function skipsAuthorization(): bool
{
return (bool)$this->skips_authorization;
}
}

View File

@@ -9,6 +9,7 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Http\Resources\Json\JsonResource;
use Laravel\Passport\Passport;
use Nexus\Nexus;
use Filament\Facades\Filament;
use NexusPlugin\Menu\Filament\MenuItemResource;
@@ -22,6 +23,7 @@ class AppServiceProvider extends ServiceProvider
*/
public function register()
{
Passport::ignoreMigrations();
do_action('nexus_register');
}

View File

@@ -9,6 +9,7 @@ use App\Models\Category;
use App\Models\Codec;
use App\Models\Icon;
use App\Models\Media;
use App\Models\OauthClient;
use App\Models\Plugin;
use App\Models\Processing;
use App\Models\SearchBox;
@@ -21,6 +22,7 @@ use App\Policies\CodecPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Laravel\Passport\Passport;
class AuthServiceProvider extends ServiceProvider
{
@@ -54,6 +56,7 @@ class AuthServiceProvider extends ServiceProvider
public function boot()
{
$this->registerPolicies();
Passport::useClientModel(OauthClient::class);
Auth::viaRequest('nexus-cookie', function (Request $request) {
return $this->getUserByCookie($request->cookie());

View File

@@ -5,7 +5,10 @@ namespace App\Providers;
use App\Events\SeedBoxRecordUpdated;
use App\Events\TorrentCreated;
use App\Events\TorrentUpdated;
use App\Events\UserDestroyed;
use App\Events\UserDisabled;
use App\Listeners\FetchTorrentImdb;
use App\Listeners\RemoveOauthTokens;
use App\Listeners\RemoveSeedBoxRecordCache;
use App\Listeners\SyncTorrentToEs;
use Illuminate\Auth\Events\Registered;
@@ -33,6 +36,12 @@ class EventServiceProvider extends ServiceProvider
TorrentCreated::class => [
FetchTorrentImdb::class,
],
UserDestroyed::class => [
RemoveOauthTokens::class,
],
UserDisabled::class => [
RemoveOauthTokens::class,
],
];
/**

View File

@@ -198,6 +198,7 @@ class UserRepository extends BaseRepository
});
do_log("user: $uid, $modCommentText");
$this->clearCache($targetUser);
fire_event("user_disabled", $uid);
return true;
}
@@ -224,6 +225,7 @@ class UserRepository extends BaseRepository
$targetUser->updateWithModComment($update, $modCommentText);
do_log("user: $uid, $modCommentText, update: " . nexus_json_encode($update));
$this->clearCache($targetUser);
fire_event("user_enabled", $uid);
return true;
}
@@ -654,6 +656,7 @@ class UserRepository extends BaseRepository
}
UserBanLog::query()->insert($userBanLogs);
do_action("user_delete", $id);
fire_event("user_destroyed", $id);
return true;
}

View File

@@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('oauth_auth_codes', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->unsignedBigInteger('user_id')->index();
$table->unsignedBigInteger('client_id');
$table->text('scopes')->nullable();
$table->boolean('revoked');
$table->dateTime('expires_at')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('oauth_auth_codes');
}
};

View File

@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('oauth_access_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->unsignedBigInteger('user_id')->nullable()->index();
$table->unsignedBigInteger('client_id');
$table->string('name')->nullable();
$table->text('scopes')->nullable();
$table->boolean('revoked');
$table->timestamps();
$table->dateTime('expires_at')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('oauth_access_tokens');
}
};

View File

@@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('oauth_refresh_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->string('access_token_id', 100)->index();
$table->boolean('revoked');
$table->dateTime('expires_at')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('oauth_refresh_tokens');
}
};

View File

@@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('oauth_clients', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id')->nullable()->index();
$table->string('name');
$table->string('secret', 100)->nullable();
$table->string('provider')->nullable();
$table->text('redirect');
$table->boolean('personal_access_client');
$table->boolean('password_client');
$table->boolean('revoked');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('oauth_clients');
}
};

View File

@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('oauth_personal_access_clients', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('client_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('oauth_personal_access_clients');
}
};

View File

@@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('oauth_clients', function (Blueprint $table) {
$table->boolean("skips_authorization")->default(false);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('oauth_clients', function (Blueprint $table) {
$table->dropColumn("skips_authorization");
});
}
};

View File

@@ -1,6 +1,6 @@
<?php
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.9');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-03-15');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-03-18');
defined('IN_TRACKER') || define('IN_TRACKER', false);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");

View File

@@ -1230,3 +1230,8 @@ function get_snatch_info($torrentId, $userId)
{
return mysql_fetch_assoc(sql_query(sprintf('select * from snatched where torrentid = %s and userid = %s order by id desc limit 1', $torrentId, $userId)));
}
function fire_event(string $name, int $id): void
{
executeCommand("event:fire --name=$name --id=$id", "string", true, false);
}

View File

@@ -95,12 +95,20 @@ final class Nexus
return $this->script == 'announce';
}
public function incrementLogSequence()
public function incrementLogSequence(): void
{
$this->logSequence++;
}
public function getRequestSchema()
private function getFirst(string $result): string
{
if (str_contains($result, ",")) {
return strstr($result, ",", true);
}
return $result;
}
public function getRequestSchema(): string
{
$schema = $this->retrieveFromServer(['HTTP_X_FORWARDED_PROTO', 'REQUEST_SCHEME', 'HTTP_SCHEME']);
if (empty($schema)) {
@@ -109,18 +117,19 @@ final class Nexus
$schema = 'https';
}
}
return $schema;
return $this->getFirst($schema);
}
public function getRequestHost(): string
{
$host = $this->retrieveFromServer(['HTTP_HOST', 'host', ], true);
return (string)$host;
$host = $this->retrieveFromServer(['HTTP_X_FORWARDED_HOST', 'HTTP_HOST', 'host'], true);
return $this->getFirst(strval($host));
}
public function getRequestIp()
public function getRequestIp(): string
{
return $this->retrieveFromServer(['HTTP_CF_CONNECTING_IP', 'HTTP_X_FORWARDED_FOR', 'x-forwarded-for', 'HTTP_REMOTE_ADDR', 'REMOTE_ADDR'], true);
$ip = $this->retrieveFromServer(['HTTP_CF_CONNECTING_IP', 'HTTP_X_FORWARDED_FOR', 'x-forwarded-for', 'HTTP_REMOTE_ADDR', 'REMOTE_ADDR'], true);
return $this->getFirst($ip);
}
private function retrieveFromServer(array $fields, bool $includeHeader = false)
@@ -134,12 +143,6 @@ final class Nexus
}
foreach ($fields as $field) {
$result = $servers[$field] ?? null;
if ($result && in_array($field, ['HTTP_X_FORWARDED_FOR', 'x-forwarded-for'])) {
$result = preg_split('/[,\s]+/', $result);
}
if (is_array($result)) {
$result = Arr::first($result);
}
if ($result !== null && $result !== '') {
return $result;
}

View File

@@ -10,4 +10,5 @@ return [
'authorization_request_desc' => 'is requesting permission to access your account',
'btn_approve' => 'Authorize',
'btn_deny' => 'Cancel',
'skips_authorization' => 'Skips authorization',
];

View File

@@ -10,4 +10,5 @@ return [
'authorization_request_desc' => '正在请求获取您账号的访问权限',
'btn_approve' => '授权',
'btn_deny' => '取消',
'skips_authorization' => '跳过授权',
];

View File

@@ -6,4 +6,9 @@ return [
'revoked' => '有效',
'access_token' => '訪問令牌',
'refresh_token' => '刷新令牌',
'authorization_request_title' => '授權請求',
'authorization_request_desc' => '正在請求獲取您賬號的訪問權限',
'btn_approve' => '授權',
'btn_deny' => '取消',
'skips_authorization' => '跳過授權',
];