fix pargar bugs 2

This commit is contained in:
2026-07-21 13:25:18 +03:30
parent d43c5e1f25
commit 49e80a1b97
17 changed files with 228 additions and 106 deletions
+98
View File
@@ -756,4 +756,102 @@ class ConfigService
{
return $this->accountingProvider() === self::ACCOUNTING_PARGAR;
}
/**
* Hostname for WebOTP SMS (@domain #code) and other public links.
* Only from the current HTTP request — no settings, no .env.
*/
public function resolvePublicHost(?\Illuminate\Http\Request $request = null): string
{
foreach ($this->publicHostCandidates($request) as $host) {
if ($this->isUsableHost($host)) {
return $this->normalizeHost($host);
}
}
return '';
}
/** Full user-facing site URL, e.g. https://example.ir */
public function resolvePublicSiteUrl(?\Illuminate\Http\Request $request = null): string
{
if ($request !== null) {
$origin = rtrim(trim((string) $request->getSchemeAndHttpHost()), '/');
$host = $this->parseHostFromUrl($origin);
if ($this->isUsableHost($host)) {
return $origin;
}
}
if (! app()->runningInConsole() && request()) {
$origin = rtrim(trim((string) request()->getSchemeAndHttpHost()), '/');
$host = $this->parseHostFromUrl($origin);
if ($this->isUsableHost($host)) {
return $origin;
}
}
$host = $this->resolvePublicHost($request);
if ($host !== '') {
return 'https://' . $host;
}
return '';
}
/** @deprecated Use resolvePublicHost() — kept for backward compatibility */
public function resolveHostForOtp(string $urlSettingKey = 'app_url', ?\Illuminate\Http\Request $request = null): string
{
return $this->resolvePublicHost($request);
}
/** @return list<string> */
private function publicHostCandidates(?\Illuminate\Http\Request $request): array
{
$candidates = [];
if ($request !== null) {
$candidates[] = trim((string) $request->getHost());
}
if (! app()->runningInConsole() && request()) {
$candidates[] = trim((string) request()->getHost());
}
return $candidates;
}
private function isUsableHost(string $host): bool
{
$host = strtolower(trim($host));
return $host !== ''
&& ! in_array($host, ['localhost', '127.0.0.1', '0.0.0.0'], true);
}
private function parseHostFromUrl(string $url): string
{
$url = trim($url);
if ($url === '') {
return '';
}
if (! preg_match('#^https?://#i', $url)) {
$url = 'https://' . ltrim($url, '/');
}
$host = parse_url($url, PHP_URL_HOST);
if (! is_string($host) || $host === '') {
return '';
}
return $this->normalizeHost($host);
}
private function normalizeHost(string $host): string
{
$host = strtolower(trim($host));
return str_starts_with($host, 'www.') ? substr($host, 4) : $host;
}
}