fix exam exit

This commit is contained in:
xiaomlove
2023-04-29 03:46:14 +08:00
parent 10cd69852c
commit bc926b798f
7 changed files with 114 additions and 32 deletions
+3
View File
@@ -87,3 +87,6 @@ MEILISEARCH_HOST=127.0.0.1
MEILISEARCH_PORT=7700 MEILISEARCH_PORT=7700
MEILISEARCH_MASTER_KEY= MEILISEARCH_MASTER_KEY=
CACHE_KEY_AGENT_ALLOW=all_agent_allows
CACHE_KEY_AGENT_DENY=all_agent_denies
+3 -2
View File
@@ -97,8 +97,9 @@ class Test extends Command
*/ */
public function handle() public function handle()
{ {
$rep = new MeiliSearchRepository(); $rep = new ExamRepository();
$rep->import(); $r = $rep->listMatchExam(1);
dd($r);
} }
} }
+15 -13
View File
@@ -12,7 +12,7 @@ class Exam extends NexusModel
public $timestamps = true; public $timestamps = true;
protected $casts = [ protected $casts = [
'filters' => 'object', 'filters' => 'array',
'indexes' => 'array', 'indexes' => 'array',
]; ];
@@ -116,25 +116,27 @@ class Exam extends NexusModel
$currentFilters = $this->filters; $currentFilters = $this->filters;
$arr = []; $arr = [];
$filter = self::FILTER_USER_CLASS; $filter = self::FILTER_USER_CLASS;
if (!empty($currentFilters->{$filter})) { if (!empty($currentFilters[$filter])) {
$classes = collect(User::$classes)->only($currentFilters->{$filter}); $classes = collect(User::$classes)->only($currentFilters[$filter]);
$arr[] = sprintf('%s: %s', nexus_trans("exam.filters.$filter"), $classes->pluck('text')->implode(', ')); $arr[] = sprintf('%s: %s', nexus_trans("exam.filters.$filter"), $classes->pluck('text')->implode(', '));
} }
$filter = self::FILTER_USER_REGISTER_TIME_RANGE; $filter = self::FILTER_USER_REGISTER_TIME_RANGE;
if (!empty($currentFilters->{$filter})) { if (!empty($currentFilters[$filter])) {
$range = $currentFilters->{$filter}; $range = $currentFilters[$filter];
$arr[] = sprintf( if (!empty($range[0]) || !empty($range[1])) {
"%s: <br/>%s ~ %s", $arr[] = sprintf(
nexus_trans("exam.filters.$filter"), "%s: <br/>%s ~ %s",
$range[0] ? Carbon::parse($range[0])->toDateTimeString() : '--', nexus_trans("exam.filters.$filter"),
$range[1] ? Carbon::parse($range[1])->toDateTimeString() : '--' $range[0] ? Carbon::parse($range[0])->toDateTimeString() : '--',
); $range[1] ? Carbon::parse($range[1])->toDateTimeString() : '--'
);
}
} }
$filter = self::FILTER_USER_DONATE; $filter = self::FILTER_USER_DONATE;
if (!empty($currentFilters->{$filter})) { if (!empty($currentFilters[$filter])) {
$donateStatus = $classes = collect(User::$donateStatus)->only($currentFilters->{$filter}); $donateStatus = collect(User::$donateStatus)->only($currentFilters[$filter]);
$arr[] = sprintf('%s: %s', nexus_trans("exam.filters.$filter"), $donateStatus->pluck('text')->implode(', ')); $arr[] = sprintf('%s: %s', nexus_trans("exam.filters.$filter"), $donateStatus->pluck('text')->implode(', '));
} }
+71 -2
View File
@@ -74,7 +74,8 @@ class AgentAllowRepository extends BaseRepository
public function checkClient($peerId, $agent, $debug = false) public function checkClient($peerId, $agent, $debug = false)
{ {
//check from high version to low version, if high version allow, stop! //check from high version to low version, if high version allow, stop!
$allows = NexusDB::remember("all_agent_allows", 3600, function () { $cacheKey = nexus_env("CACHE_KEY_AGENT_ALLOW", "all_agent_allows") . ":php";
$allows = NexusDB::remember($cacheKey, 3600, function () {
return AgentAllow::query() return AgentAllow::query()
->orderBy('peer_id_start', 'desc') ->orderBy('peer_id_start', 'desc')
->orderBy('agent_start', 'desc') ->orderBy('agent_start', 'desc')
@@ -190,8 +191,9 @@ class AgentAllowRepository extends BaseRepository
private function checkIsDenied($peerId, $agent, $familyId) private function checkIsDenied($peerId, $agent, $familyId)
{ {
$cacheKey = nexus_env("CACHE_KEY_AGENT_DENY", "all_agent_denies") . ":php";
/** @var Collection $allDenies */ /** @var Collection $allDenies */
$allDenies = NexusDB::remember("all_agent_denies", 3600, function () { $allDenies = NexusDB::remember($cacheKey, 3600, function () {
return AgentDeny::query()->get()->groupBy('family_id'); return AgentDeny::query()->get()->groupBy('family_id');
}); });
$agentDenies = $allDenies->get($familyId, []); $agentDenies = $allDenies->get($familyId, []);
@@ -264,4 +266,71 @@ class AgentAllowRepository extends BaseRepository
} }
public function checkClientSimple($peerId, $agent, $debug = false)
{
//check from high version to low version, if high version allow, stop!
$cacheKey = nexus_env("CACHE_KEY_AGENT_ALLOW", "all_agent_allows") . ":php";
$allows = NexusDB::remember($cacheKey, 3600, function () {
return AgentAllow::query()
->orderBy('peer_id_start', 'desc')
->orderBy('agent_start', 'desc')
->get();
});
$agentAllowPassed = null;
foreach ($allows as $agentAllow) {
$agentAllowId = $agentAllow->id;
$agentAllowLogPrefix = "[ID: $agentAllowId], peerId: $peerId";
$pattern = $agentAllow->peer_id_pattern;
//check peer_id, when handle scrape request, no peer_id, so let it pass
$isPeerIdAllowed = empty($pattern) || preg_match($pattern, $peerId);
$agentAllowLogPrefix .= ", peer_id pattern: $pattern, isPeerIdAllowed: $isPeerIdAllowed";
//check agent, agent must have both announce + scrape
$pattern = $agentAllow->agent_pattern;
$isAgentAllowed = !empty($pattern) && preg_match($pattern, $agent);
$agentAllowLogPrefix .= ", agent pattern: $pattern, isAgentAllowed: $isAgentAllowed";
//both OK, passed, client is allowed
if ($isPeerIdAllowed && $isAgentAllowed) {
$agentAllowPassed = $agentAllow;
do_log("$agentAllowLogPrefix, PASSED", 'debug');
break;
}
if ($debug) {
do_log("$agentAllowLogPrefix, NOT PASSED", 'debug');
}
}
if (!$agentAllowPassed) {
throw new ClientNotAllowedException("Banned Client, Please goto " . getSchemeAndHttpHost() . "/faq.php#id29 for a list of acceptable clients");
}
if ($debug) {
do_log("agentAllowPassed: " . $agentAllowPassed->toJson(), 'debug');
}
// check if exclude
if ($agentAllowPassed->exception == 'yes') {
$agentDeny = $this->checkIsDenied($peerId, $agent, $agentAllowPassed->id);
if ($agentDeny) {
if ($debug) {
do_log("agentDeny: " . $agentDeny->toJson());
}
throw new ClientNotAllowedException(sprintf(
"[%s-%s]Client: %s is banned due to: %s",
$agentAllowPassed->id, $agentDeny->id, $agentDeny->name, $agentDeny->comment
));
}
}
if (isHttps() && $agentAllowPassed->allowhttps != 'yes') {
throw new ClientNotAllowedException(sprintf(
"[%s]This client does not support https well, Please goto %s/faq.php#id29 for a list of proper clients",
$agentAllowPassed->id, getSchemeAndHttpHost()
));
}
return $agentAllowPassed;
}
} }
+14 -11
View File
@@ -251,21 +251,24 @@ class ExamRepository extends BaseRepository
$filters = $exam->filters; $filters = $exam->filters;
$filter = Exam::FILTER_USER_CLASS; $filter = Exam::FILTER_USER_CLASS;
if (!empty($filters->{$filter}) && !in_array($user->class, $filters->{$filter})) { $filterValues = $filters[$filter];
do_log("$logPrefix, user class: {$user->class} not in: " . json_encode($filters->{$filter})); if (!empty($filterValues) && !in_array($user->class, $filterValues)) {
do_log("$logPrefix, user class: {$user->class} not in: " . json_encode($filterValues));
return false; return false;
} }
$filter = Exam::FILTER_USER_DONATE; $filter = Exam::FILTER_USER_DONATE;
if (!empty($filters->{$filter}) && !in_array($user->donate_status, $filters->{$filter})) { $filterValues = $filters[$filter];
do_log("$logPrefix, user donate status: {$user->donate_status} not in: " . json_encode($filters->{$filter})); if (!empty($filterValues) && !in_array($user->donate_status, $filterValues)) {
do_log("$logPrefix, user donate status: {$user->donate_status} not in: " . json_encode($filterValues));
return false; return false;
} }
$filter = Exam::FILTER_USER_REGISTER_TIME_RANGE; $filter = Exam::FILTER_USER_REGISTER_TIME_RANGE;
$filterValues = $filters[$filter];
$added = $user->added->toDateTimeString(); $added = $user->added->toDateTimeString();
$registerTimeBegin = isset($filters->{$filter}[0]) ? Carbon::parse($filters->{$filter}[0])->toDateTimeString() : ''; $registerTimeBegin = isset($filterValues[0]) ? Carbon::parse($filterValues[0])->toDateTimeString() : '';
$registerTimeEnd = isset($filters->{$filter}[1]) ? Carbon::parse($filters->{$filter}[1])->toDateTimeString() : ''; $registerTimeEnd = isset($filterValues[1]) ? Carbon::parse($filterValues[1])->toDateTimeString() : '';
if (!empty($registerTimeBegin) && $added < $registerTimeBegin) { if (!empty($registerTimeBegin) && $added < $registerTimeBegin) {
do_log("$logPrefix, user added: $added not bigger than begin: " . $registerTimeBegin); do_log("$logPrefix, user added: $added not bigger than begin: " . $registerTimeBegin);
return false; return false;
@@ -839,13 +842,13 @@ class ExamRepository extends BaseRepository
->orderBy("$userTable.id", "asc"); ->orderBy("$userTable.id", "asc");
$filter = Exam::FILTER_USER_CLASS; $filter = Exam::FILTER_USER_CLASS;
if (!empty($filters->$filter)) { if (!empty($filters[$filter])) {
$baseQuery->whereIn("$userTable.class", $filters->$filter); $baseQuery->whereIn("$userTable.class", $filters[$filter]);
} }
$filter = Exam::FILTER_USER_DONATE; $filter = Exam::FILTER_USER_DONATE;
if (!empty($filters->$filter) && count($filters->$filter) == 1) { if (!empty($filters[$filter]) && count($filters[$filter]) == 1) {
$donateStatus = $filters->$filter[0]; $donateStatus = $filters[$filter][0];
if ($donateStatus == User::DONATE_YES) { if ($donateStatus == User::DONATE_YES) {
$baseQuery->where(function (Builder $query) { $baseQuery->where(function (Builder $query) {
$query->where('donor', 'yes')->where(function (Builder $query) { $query->where('donor', 'yes')->where(function (Builder $query) {
@@ -865,7 +868,7 @@ class ExamRepository extends BaseRepository
} }
$filter = Exam::FILTER_USER_REGISTER_TIME_RANGE; $filter = Exam::FILTER_USER_REGISTER_TIME_RANGE;
$range = $filters->$filter; $range = $filters[$filter];
if (!empty($range)) { if (!empty($range)) {
if (!empty($range[0])) { if (!empty($range[0])) {
$baseQuery->where("$userTable.added", ">=", Carbon::parse($range[0])->toDateTimeString()); $baseQuery->where("$userTable.added", ">=", Carbon::parse($range[0])->toDateTimeString());
+2 -2
View File
@@ -1,6 +1,6 @@
<?php <?php
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.1'); defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.2');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2023-04-21'); defined('RELEASE_DATE') || define('RELEASE_DATE', '2023-04-29');
defined('IN_TRACKER') || define('IN_TRACKER', false); defined('IN_TRACKER') || define('IN_TRACKER', false);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP"); defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org"); defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
+6 -2
View File
@@ -1070,8 +1070,12 @@ function clear_inbox_count_cache($uid)
function clear_agent_allow_deny_cache() function clear_agent_allow_deny_cache()
{ {
do_log("clear_agent_allow_deny_cache"); do_log("clear_agent_allow_deny_cache");
\Nexus\Database\NexusDB::cache_del("all_agent_allows"); $allowCacheKey = nexus_env("CACHE_KEY_AGENT_ALLOW", "all_agent_allows");
\Nexus\Database\NexusDB::cache_del("all_agent_denies"); $denyCacheKey = nexus_env("CACHE_KEY_AGENT_DENY", "all_agent_denies");
foreach (["", ":php", ":go"] as $suffix) {
\Nexus\Database\NexusDB::cache_del($allowCacheKey . $suffix);
\Nexus\Database\NexusDB::cache_del($denyCacheKey . $suffix);
}
} }