-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
133 lines (112 loc) · 3.54 KB
/
index.php
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* TSF.fyi redirection interpreter.
* Copyright (C) 2019 Sybre Waaijer, The SEO Framework (https://theseoframework.com/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Discourage direct index file access.
if ( '/index.php' === $_SERVER['REQUEST_URI'] ) {
http_response_code( 503 );
exit;
}
header( 'Cache-Control: max-age=3600', true );
header( 'Pragma: public' );
if ( $_GET['error'] ?? false ) {
header( 'X-Robots-Tag: noindex, follow', true );
header( 'Cache-Control: max-age=60', true );
http_response_code( 503 );
require __DIR__ . DIRECTORY_SEPARATOR . '503.html';
exit;
}
function retry() {
$retry = (int) ( $_GET['r'] ?? 0 ) + 1;
if ( $retry > 3 ) {
$location = "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}/";
$error = json_last_error() ?: -1;
header( 'X-Redirect-By: tsf.fyi' );
header( 'X-Retry-Request: true' );
header( "Location: $location?error=$error", true, 302 );
exit;
}
// Redirect after 250ms.
usleep( 2.5e5 );
header( 'X-Redirect-By: tsf.fyi' );
header( 'X-Retry-Request: true' );
header( "Location: {$_SERVER['SCRIPT_URI']}?r=$retry", true, 302 );
exit;
}
$json = json_decode(
file_get_contents( __DIR__ . DIRECTORY_SEPARATOR . 'links.json', false, stream_context_create( [ 'http' => [ 'timeout' => 1 ] ] ) )
?: []
) or retry();
$request = preg_replace(
'/[^a-z0-9_\/%\-\.]+/',
'',
strtolower( substr_replace( $_SERVER['REQUEST_URI'], '', 0, strlen( dirname( $_SERVER['PHP_SELF'] ) ) ) )
) ?: 'links';
$r = array_values( array_filter( explode( '/', $request ) ) );
function find_endpoint( $json, $r ) {
$default = '';
$depth = 0;
while ( isset( $r[ $depth ] ) ) {
$next = $json->{$r[ $depth ]} ?? null;
if ( null === $next ) {
foreach ( $json as $_endpoint => $_json ) {
if ( isset( $_json->_alt ) && in_array( $r[ $depth ], $_json->_alt, true ) ) {
$next = $json->{$_endpoint};
break;
} elseif ( is_string( $_json ) ) {
if ( false !== strpos( $_endpoint, '$' ) ) {
if ( false !== strpos( $_endpoint, '$$' ) ) {
// Wildcard replacement.
$_items = array_slice( $r, $depth );
$next = str_replace( '$$', implode( '/', $_items ), $_json );
} else {
// Prudent replacement.
$next = str_replace( '$', $r[ $depth ], $_json );
}
}
}
}
}
if ( isset( $next->_deep ) ) {
$default = $next->_default ?? $default;
$json = $next->_deep;
++$depth;
} else {
if ( is_string( $next ) ) {
$endpoint = $next;
} elseif ( $next ) {
$endpoint = $next->_default ?? $default;
} else {
// Nothing found, always abort to prevent unwanted recursion and DDoS.
break;
}
// We're done here.
break;
}
}
return $endpoint ?? $default;
}
$location = find_endpoint( $json, $r ) ?: '';
if ( ! $location ) :
header( 'X-Robots-Tag: noindex, follow', true );
http_response_code( 404 );
require __DIR__ . DIRECTORY_SEPARATOR . '404.html';
exit;
endif;
http_response_code( 301 );
header( 'X-Redirect-By: tsf.fyi' );
header( "Location: $location", true, 301 );
exit;