-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSeoController.php
More file actions
105 lines (89 loc) · 3.99 KB
/
SeoController.php
File metadata and controls
105 lines (89 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
declare(strict_types=1);
namespace App\Controllers;
use App\Support\HtmlHelper;
use App\Support\RouteTranslator;
use App\Support\SitemapGenerator;
use mysqli;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
class SeoController
{
public function sitemap(Request $request, Response $response, mysqli $db): Response
{
$baseUrl = self::resolveBaseUrl($request);
$generator = new SitemapGenerator($db, $baseUrl);
$response->getBody()->write($generator->generate());
return $response->withHeader('Content-Type', 'application/xml; charset=UTF-8');
}
public function robots(Request $request, Response $response): Response
{
$baseUrl = self::resolveBaseUrl($request);
$basePath = HtmlHelper::getBasePath();
$lines = [
'User-agent: *',
'Disallow: ' . $basePath . '/admin/',
'Disallow: ' . $basePath . RouteTranslator::route('login'),
'Disallow: ' . $basePath . RouteTranslator::route('register'),
'',
'Sitemap: ' . $baseUrl . '/sitemap.xml',
'Feed: ' . $baseUrl . '/feed.xml',
];
$response->getBody()->write(implode("\n", $lines) . "\n");
return $response->withHeader('Content-Type', 'text/plain; charset=UTF-8');
}
public static function resolveBaseUrl(?Request $request = null): string
{
$envUrl = getenv('APP_CANONICAL_URL') ?: ($_ENV['APP_CANONICAL_URL'] ?? '');
if (is_string($envUrl) && $envUrl !== '') {
// NOTE: APP_CANONICAL_URL must include the subfolder if installed in one
// (e.g. https://example.com/pinakes), otherwise canonical URLs will be incorrect.
$envUrl = rtrim($envUrl, '/');
return $envUrl;
}
$scheme = 'https';
$host = 'localhost';
$port = null;
if ($request !== null) {
$uri = $request->getUri();
$scheme = $uri->getScheme() !== '' ? $uri->getScheme() : 'https';
$host = $uri->getHost() !== '' ? $uri->getHost() : $host;
$port = $uri->getPort();
} else {
if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
$forwardedProto = explode(',', (string)$_SERVER['HTTP_X_FORWARDED_PROTO'])[0];
$scheme = strtolower($forwardedProto) === 'https' ? 'https' : 'http';
} elseif (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') {
$scheme = 'https';
} elseif (!empty($_SERVER['REQUEST_SCHEME'])) {
$scheme = strtolower((string)$_SERVER['REQUEST_SCHEME']) === 'https' ? 'https' : 'http';
} elseif (isset($_SERVER['SERVER_PORT']) && (int)$_SERVER['SERVER_PORT'] === 443) {
$scheme = 'https';
} else {
$scheme = 'http';
}
if (!empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
$host = explode(',', (string)$_SERVER['HTTP_X_FORWARDED_HOST'])[0];
} elseif (!empty($_SERVER['HTTP_HOST'])) {
$host = (string)$_SERVER['HTTP_HOST'];
} elseif (!empty($_SERVER['SERVER_NAME'])) {
$host = (string)$_SERVER['SERVER_NAME'];
}
if (str_contains($host, ':')) {
[$hostOnly, $portPart] = explode(':', $host, 2);
$host = $hostOnly;
$port = is_numeric($portPart) ? (int)$portPart : null;
} elseif (isset($_SERVER['SERVER_PORT']) && is_numeric((string)$_SERVER['SERVER_PORT'])) {
$port = (int)$_SERVER['SERVER_PORT'];
}
}
$base = $scheme . '://' . $host;
$defaultPorts = ['http' => 80, 'https' => 443];
if ($port !== null && ($defaultPorts[$scheme] ?? null) !== $port) {
$base .= ':' . $port;
}
// Include basePath for subfolder installations (e.g. /pinakes).
$base .= HtmlHelper::getBasePath();
return rtrim($base, '/');
}
}