2021-04-17 19:01:33 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Arr;
|
2022-03-28 15:58:12 +08:00
|
|
|
use Nexus\Database\NexusDB;
|
2021-04-17 19:01:33 +08:00
|
|
|
|
|
|
|
|
class Setting extends NexusModel
|
|
|
|
|
{
|
2022-06-29 17:00:15 +08:00
|
|
|
protected $fillable = ['name', 'value', 'autoload'];
|
|
|
|
|
|
|
|
|
|
public $timestamps = true;
|
2021-04-17 19:01:33 +08:00
|
|
|
|
2022-08-20 19:11:28 +08:00
|
|
|
const PERMISSION_NO_CLASS = 100;
|
|
|
|
|
|
|
|
|
|
public static array $permissionMustHaveClass = ['defaultclass', 'staffmem'];
|
2022-08-19 15:30:16 +08:00
|
|
|
|
2022-08-24 13:36:14 +08:00
|
|
|
const DIRECT_PERMISSION_CACHE_KEY_PREFIX = 'nexus_direct_permissions_';
|
|
|
|
|
const ROLE_PERMISSION_CACHE_KEY_PREFIX = 'nexus_role_permissions_';
|
2022-08-24 00:19:19 +08:00
|
|
|
|
2022-08-26 17:35:49 +08:00
|
|
|
const TORRENT_GLOBAL_STATE_CACHE_KEY = 'global_promotion_state';
|
2025-05-02 23:27:16 +07:00
|
|
|
const USER_TOKEN_PERMISSION_ALLOWED_CACHE_KRY = 'user_token_permission_allowed';
|
2022-08-26 17:35:49 +08:00
|
|
|
|
2022-05-06 22:25:00 +08:00
|
|
|
/**
|
|
|
|
|
* get setting autoload = yes with cache
|
|
|
|
|
*
|
|
|
|
|
* @param null $name
|
|
|
|
|
* @param null $default
|
2022-05-07 02:47:21 +08:00
|
|
|
* @return mixed
|
2022-05-06 22:25:00 +08:00
|
|
|
*/
|
2022-05-07 02:47:21 +08:00
|
|
|
public static function get($name = null, $default = null): mixed
|
2021-04-17 19:01:33 +08:00
|
|
|
{
|
2022-12-19 19:17:22 +08:00
|
|
|
static $settings = null;
|
|
|
|
|
if (is_null($settings)) {
|
|
|
|
|
$settings = NexusDB::remember("nexus_settings_in_laravel", 600, function () {
|
|
|
|
|
return self::getFromDb();
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-04-17 19:01:33 +08:00
|
|
|
if (is_null($name)) {
|
|
|
|
|
return $settings;
|
|
|
|
|
}
|
2022-04-04 17:26:26 +08:00
|
|
|
return Arr::get($settings, $name, $default);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-06 22:25:00 +08:00
|
|
|
/**
|
|
|
|
|
* get setting autoload = yes without cache
|
|
|
|
|
*
|
|
|
|
|
* @param null $name
|
|
|
|
|
* @param null $default
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public static function getFromDb($name = null, $default = null): mixed
|
2022-04-04 17:26:26 +08:00
|
|
|
{
|
2022-05-06 22:25:00 +08:00
|
|
|
$rows = self::query()->where('autoload', 'yes')->get(['name', 'value']);
|
2022-04-04 17:26:26 +08:00
|
|
|
$result = [];
|
|
|
|
|
foreach ($rows as $row) {
|
2022-05-06 22:25:00 +08:00
|
|
|
$value = self::normalizeValue($row);
|
2022-04-04 17:26:26 +08:00
|
|
|
Arr::set($result, $row->name, $value);
|
|
|
|
|
}
|
|
|
|
|
if (is_null($name)) {
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
return Arr::get($result, $name, $default);
|
2021-04-17 19:01:33 +08:00
|
|
|
}
|
2021-05-15 01:24:44 +08:00
|
|
|
|
2022-05-06 22:25:00 +08:00
|
|
|
/**
|
|
|
|
|
* get from db by name, generally used for `autoload` = 'no'
|
|
|
|
|
*
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param null $default
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public static function getByName($name, $default = null): mixed
|
|
|
|
|
{
|
|
|
|
|
$result = self::query()->where('name', $name)->first();
|
|
|
|
|
if ($result) {
|
|
|
|
|
return self::normalizeValue($result);
|
|
|
|
|
}
|
|
|
|
|
return $default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getByWhereRaw($whereRaw): array
|
|
|
|
|
{
|
|
|
|
|
$result = [];
|
|
|
|
|
$list = self::query()->whereRaw($whereRaw)->get();
|
|
|
|
|
foreach ($list as $value) {
|
|
|
|
|
Arr::set($result, $value->name, self::normalizeValue($value));
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function normalizeValue(Setting $setting)
|
|
|
|
|
{
|
|
|
|
|
$value = $setting->value;
|
|
|
|
|
if (!is_null($value)) {
|
|
|
|
|
$arr = json_decode($value, true);
|
|
|
|
|
if (is_array($arr)) {
|
|
|
|
|
$value = $arr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 23:55:15 +07:00
|
|
|
public static function updateUserTokenPermissionAllowedCache(array $allowed = []): void
|
2025-05-02 23:27:16 +07:00
|
|
|
{
|
|
|
|
|
$redis = NexusDB::redis();
|
|
|
|
|
$key = self::USER_TOKEN_PERMISSION_ALLOWED_CACHE_KRY;
|
|
|
|
|
$redis->del($key);
|
|
|
|
|
//must not use cache
|
2025-05-02 23:55:15 +07:00
|
|
|
if (empty($allowed)) {
|
|
|
|
|
$allowed = self::getFromDb("permission.user_token_allowed");
|
|
|
|
|
}
|
2025-05-02 23:27:16 +07:00
|
|
|
if (!empty($allowed)) {
|
|
|
|
|
$redis->sAdd($key, ...$allowed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 01:39:40 +07:00
|
|
|
public static function getDefaultLang(): string
|
2025-02-15 03:15:45 +08:00
|
|
|
{
|
|
|
|
|
return self::get("main.defaultlang");
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-05 15:38:40 +07:00
|
|
|
public static function getIsUseChallengeResponseAuthentication(): bool
|
|
|
|
|
{
|
|
|
|
|
return self::get("security.use_challenge_response_authentication") == "yes";
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 01:39:40 +07:00
|
|
|
public static function getUploadTorrentMaxSize(): int
|
|
|
|
|
{
|
|
|
|
|
return intval(self::get("main.max_torrent_size"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getUploadTorrentMaxPrice(): int
|
|
|
|
|
{
|
|
|
|
|
return intval(self::get("torrent.max_price"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getIsPaidTorrentEnabled(): bool
|
|
|
|
|
{
|
|
|
|
|
return self::get("torrent.paid_torrent_enabled") == "yes";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getUploadDenyApprovalDenyCount(): int
|
|
|
|
|
{
|
|
|
|
|
return intval(self::get("main.upload_deny_approval_deny_count"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getOfferSkipApprovedCount(): int
|
|
|
|
|
{
|
|
|
|
|
return intval(self::get("main.offer_skip_approved_count"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getLargeTorrentSize(): int
|
|
|
|
|
{
|
|
|
|
|
return intval(self::get("torrent.largesize"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getLargeTorrentSpState(): int
|
|
|
|
|
{
|
|
|
|
|
return intval(self::get("torrent.largepro"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getUploadTorrentHalfDownProbability(): int
|
|
|
|
|
{
|
|
|
|
|
return intval(self::get("torrent.randomhalfleech"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getUploadTorrentFreeProbability(): int
|
|
|
|
|
{
|
|
|
|
|
return intval(self::get("torrent.randomfree"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getUploadTorrentTwoTimesUpProbability(): int
|
|
|
|
|
{
|
|
|
|
|
return intval(self::get("torrent.randomtwoup"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getUploadTorrentFreeTwoTimesUpProbability(): int
|
|
|
|
|
{
|
|
|
|
|
return intval(self::get("torrent.randomtwoupfree"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getUploadTorrentHalfDownTwoTimesUpProbability(): int
|
|
|
|
|
{
|
|
|
|
|
return intval(self::get("torrent.randomtwouphalfdown"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getUploadTorrentOneThirdDownProbability(): int
|
|
|
|
|
{
|
|
|
|
|
return intval(self::get("torrent.randomthirtypercentdown"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getUploadTorrentRewardBonus(): int
|
|
|
|
|
{
|
|
|
|
|
return intval(self::get("bonus.uploadtorrent"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function getIsUploadOpenAtWeekend(): bool
|
|
|
|
|
{
|
|
|
|
|
return self::get("main.sptime") == "yes";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getIsSpecialSectionEnabled(): bool
|
|
|
|
|
{
|
|
|
|
|
return self::get('main.spsct') == 'yes';
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 16:39:44 +07:00
|
|
|
public static function getIsComplainEnabled(): bool
|
|
|
|
|
{
|
|
|
|
|
return self::get('main.complain_enabled') == 'yes';
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 01:39:40 +07:00
|
|
|
public static function getIsAllowUserReceiveEmailNotification(): bool
|
|
|
|
|
{
|
|
|
|
|
return self::get('smtp.emailnotify') == 'yes';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getBaseUrl(): string
|
|
|
|
|
{
|
|
|
|
|
$result = self::get('basic.BASEURL', $_SERVER['HTTP_HOST'] ?? '');
|
|
|
|
|
return rtrim($result, '/');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getSiteName(): string
|
|
|
|
|
{
|
|
|
|
|
return self::get("basic.SITENAME");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getTorrentSaveDir(): string
|
|
|
|
|
{
|
|
|
|
|
return self::get("main.torrent_dir");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getSmtpType(): string
|
|
|
|
|
{
|
|
|
|
|
return self::get("smtp.smtptype");
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 23:27:16 +07:00
|
|
|
public static function getPermissionUserTokenAllowed(): array
|
|
|
|
|
{
|
|
|
|
|
return self::get("permission.user_token_allowed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-04-17 19:01:33 +08:00
|
|
|
}
|