preg_match($pattern, $request_uri))) { $attempts = []; if (file_exists($attempts_file)) { foreach (file($attempts_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) { list($ip, $count) = explode(',', $line); $attempts[$ip] = (int)$count; } } $attempts[$visitor_ip] = ($attempts[$visitor_ip] ?? 0) + 1; if ($attempts[$visitor_ip] >= 15) { file_put_contents($blocklist_file, "$visitor_ip\n", FILE_APPEND | LOCK_EX); } $new_attempts = array_map(fn($ip, $count) => "$ip,$count", array_keys($attempts), $attempts); file_put_contents($attempts_file, implode("\n", $new_attempts) . "\n", LOCK_EX); http_response_code(200); exit; } // It is safe, so let's continue processing the script require_once(__DIR__.'/config.php'); // ๐Ÿš€ REDIRECT `/index.php` or `/index.html` to `/` if (preg_match('#/index\.(php|html?)$#', $request_uri)) { $clean_url = preg_replace('#/index\.(php|html?)$#', '/', $request_uri); header("Location: $clean_url", true, 301); exit; } // ๐Ÿš€ REDIRECT `.php` or `.html` URLs to clean versions (`/page.php` โ†’ `/page`) if (preg_match('#\.(php|html?)$#', $request_uri)) { $clean_url = preg_replace('#\.(php|html?)$#', '', $request_uri); header("Location: $clean_url", true, 301); exit; } // ๐Ÿ”ฅ SPECIAL CASE: The `/app/` and `/page/` folders must continue processing in `router.php` if (($request_uri === '/app' || $request_uri === '/page') || (strpos($request_uri, '/app/') === 0 || strpos($request_uri, '/page/') === 0)) { // Do nothing hereโ€”just let it continue processing the script below } else { $document_root = $_SERVER['DOCUMENT_ROOT']; $full_path = realpath($document_root . $request_uri); // ๐Ÿ“ Serve files normally IF they exist (EXCEPT `/app/`) with strong conditional caching if (is_file($full_path)) { streamFileWithConditionalCache($full_path); exit; } // ๐Ÿ”„ Alias: /uploads/profile_pictures/g-[.png] โ†’ stream sharded file directly (no redirect) // can be any safe token (letters, numbers, _ or -), not just 32-hex if (preg_match('#^/uploads/profile_pictures/(g-[A-Za-z0-9_-]+)(?:\.png)?$#', $request_uri, $m)) { $openaiId = $m[1]; $hex = substr($openaiId, 2); $shard = substr($hex, 0, 2) ?: '00'; $canonicalUrl = '/uploads/profile_pictures/_/' . $shard . '/' . $openaiId . '.png'; $canonicalPath = __DIR__ . $canonicalUrl; if (is_file($canonicalPath)) { // Stream with strong conditional caching so alias URL gets ETag/Last-Modified streamFileWithConditionalCache($canonicalPath); exit; } http_response_code(404); exit; } // (Legacy /uploads/g-.png intentionally not handled here; use api/test.php to migrate.) // ๐Ÿ“Œ Serve `index.php` or `index.html` for directories (EXCEPT `/app/`) if (is_dir($full_path)) { if (file_exists($full_path . '/index.php')) { require $full_path . '/index.php'; exit; } elseif (file_exists($full_path . '/index.html')) { require $full_path . '/index.html'; exit; } } // โœจ Pretty URLs: `/page` โ†’ `/page.php` (EXCEPT `/app/`) $php_file = $document_root . $request_uri . '.php'; if (file_exists($php_file)) { require $php_file; exit; } } // DEFAULT INDEX router('GET', '/', function () { header('Location: ' . (__DEFAULT__ ? __DEFAULT__ : 'https://hello.gpt-tools.co/')); }, true); router('GET', 'close', function ($params) { $_REQUEST = array_merge($_REQUEST, $params); // If a payment success flag is explicitly provided via query, keep old behavior if (isset($_REQUEST['success'])) { header('Location: ' . ($_REQUEST['success'] ? 'https://hello.gpt-tools.co/payment-successful' : 'https://hello.gpt-tools.co/payment-not-successful')); return; } // Otherwise render the local auto-close page require __DIR__ . '/close.php'; }, true); router('GET', '(e|error)', function ($params) { $m = $_REQUEST['m'] ?? $_REQUEST['message'] ?? 'Something went wrong.'; $o = strpos($m, ' ') !== false ? $m : (($d=base64_decode($m,true)) && base64_encode($d) === $m ? $d : $m); print htmlspecialchars($o); }, true); router('GET', 'api/(?{/uuid/})/integrate/openai/(?(action|privacy))', function ($params) { $_REQUEST = array_merge($_REQUEST, array_diff_key($params, array_flip(['route']))); renderAPI('user/integrate/openai/'. @$params['route']); }, true); router(['GET', 'POST'], 'api/(?{/uuid/})?/?(?{/uuid/})/chat/?(?(next|donate|auth|oauth|start))?', function ($params) { $_REQUEST = array_merge($_REQUEST, array_diff_key($params, array_flip(['route']))); renderAPI(@$params['route'] == 'oauth' ? 'user/auth/oauth' : 'chat/' . (@$params['route'] ?? 'next')); }, true); router(['GET', 'POST'], 'api/(?{/uuid/})?/?(?{/uuid/})/checkout/?(?(get|pay|go|check|back|oauth))?', function ($params) { $_REQUEST = array_merge($_REQUEST, array_diff_key($params, array_flip(['route']))); renderAPI(@$params['route'] == 'oauth' ? 'user/auth/oauth' : 'checkout/' . @$params['route'] ?? 'get'); }, true); router(['GET', 'POST'], 'api/(?{/uuid/})?/?(?{/uuid/})?/?checkout/(?{/uuid/})/(?(get|pay|go|manage|check|back))', function ($params) { $_REQUEST = array_merge($_REQUEST, array_diff_key($params, array_flip(['route']))); renderAPI(@$params['route'] == 'oauth' ? 'user/auth/oauth' : 'checkout/' . @$params['route'] ?? 'get'); }, true); router(['GET', 'POST'], 'api/(?{/uuid/})?/?tools/(?(mysql))', function ($params) { $_REQUEST = array_merge($_REQUEST, array_diff_key($params, array_flip(['route']))); renderAPI('tools/' . @$params['route']); }, true); router(['GET'], 'api/affiliate/(?(go))', function ($params) { $_REQUEST = array_merge($_REQUEST, array_diff_key($params, array_flip(['route']))); renderAPI('affiliate/'. @$params['route']); }, true); router(['GET', 'POST'], 'api/user/(?{/uuid/})/?(?.*)?', function ($params) { $_REQUEST = array_merge($_REQUEST, array_diff_key($params, array_flip(['route']))); renderAPI('user/'. @$params['route']); }, true); router('GET', 'app/?(?.*)', function ($params) { renderAPP('/'. @$params['route']); }, true); router('GET', 'page/?(?.*)', function ($params) { $_REQUEST = array_merge($_REQUEST, $params); renderPAGE('/'. @$params['route']); }, true); router('GET', 'sitemap.xml', function () { http_response_code(410); }, true); router('GET', 'favicon.ico', function () { header('Location: ' . __SITE__ . '/app/_/images/gpt.builders.jpg'); }, true); router('GET', 'db/?(?.*)', function ($params) { header('Location: /'); }, true); router('GET', '(?[^/]+)/?(?.*)', function ($params) { $_REQUEST = array_merge($_REQUEST, $params); $route = $_REQUEST['route'] ?? ''; $user_handle = $_REQUEST['user_handle']; if ($user_handle != "user" && $route == "affiliate") { $user = getUserWithUserHandle($user_handle); if ($user) { $_REQUEST["u"] = $user_handle; renderAPI('affiliate/go'); } } header('Location: /'); }, true); ?>