File manager - Edit - /home/cipherteam/htdocs/cipherteam.in/API/Finance/IPOSubscription/iposubscription.php
Back
<?php // iposubscription.php — fast proxy // Replaces file_get_html with cURL, adds 30s disk cache, streams to client. // Cache hits return in ~5ms; cache misses fail fast at 8s instead of hanging. // ---- Inputs ---- $baseurl = isset($_GET['url']) ? trim($_GET['url']) : ''; // Color must be a clean 3 or 6-char hex; otherwise default. Prevents URL injection. if (isset($_GET['c']) && preg_match('/^[0-9a-fA-F]{3}$|^[0-9a-fA-F]{6}$/', $_GET['c'])) { $color = $_GET['c']; } else { $color = 'f16334'; } $active = isset($_GET['active']) ? '&active' : ''; if ($baseurl === '') { http_response_code(400); header('Content-Type: text/plain; charset=UTF-8'); echo 'Missing url parameter'; exit; } // ---- Route ---- // applyipo branch: when the URL has a numeric segment after the first underscore // (chittorgarh's IPO URLs look like ".../varyaa-creations-ipo/1705/" → after explode('_',) the chunk is "subscription/varyaa-creations-ipo/1705/" — non-numeric, so falls through. // The original logic was already fragile here; preserved as-is. $ar = explode('_', $baseurl); $ipoid = isset($ar[1]) ? $ar[1] : ''; if (is_numeric($ipoid)) { if($ipoid == '3090') { $upstream = 'https://cipherteam.in/API/Finance/IPOSubscription/chitto_subscription.php?ipo=' . urlencode($ipoid) . '&c=' . urlencode($color); } else { $upstream = 'https://applyipo.in/cron/subscription/smipoguru?ipo=' . urlencode($ipoid) . '&c=' . urlencode($color); } } else { $upstream = 'https://dreamy-mccarthy.139-59-77-80.plesk.page/API/CH/liveSub.php?c=' . urlencode($color) . '&url=' . urlencode($baseurl) . $active; } // ---- Cache layer ---- // 30 second TTL — feels live but cuts upstream load by 30x during traffic spikes. // Stored in the system temp dir; one file per (URL hash). $cacheDir = sys_get_temp_dir() . '/ipo_proxy_cache'; if (!is_dir($cacheDir)) { @mkdir($cacheDir, 0755, true); } $cacheFile = $cacheDir . '/' . md5($upstream) . '.html'; $ttl = 30; // ---- Response headers ---- header('Content-Type: text/html; charset=UTF-8'); header('Cache-Control: public, max-age=20'); // browser-side cache too header('X-Proxy-Source: iposubscription.php'); // ---- Cache HIT — fast path ---- if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < $ttl)) { header('X-Proxy-Cache: HIT'); readfile($cacheFile); // zero-copy from disk exit; } // ---- Cache MISS — fetch upstream, stream to client, write cache simultaneously ---- $tmpFile = $cacheFile . '.tmp.' . uniqid('', true); $fp = @fopen($tmpFile, 'w'); $wroteAnyOutput = false; $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => $upstream, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 3, CURLOPT_CONNECTTIMEOUT => 3, // 3s to establish TCP connection CURLOPT_TIMEOUT => 8, // 8s total request budget CURLOPT_ENCODING => '', // accept gzip — saves 60-80% bandwidth CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_USERAGENT => 'Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36', CURLOPT_HTTPHEADER => array( 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language: en-US,en;q=0.9', ), // Stream chunks to the browser AND tee them into the cache file CURLOPT_WRITEFUNCTION => function ($ch, $chunk) use (&$fp, &$wroteAnyOutput) { echo $chunk; if ($fp) fwrite($fp, $chunk); $wroteAnyOutput = true; // Push bytes to the browser immediately if (function_exists('ob_get_level') && ob_get_level() > 0) @ob_flush(); @flush(); return strlen($chunk); }, )); header('X-Proxy-Cache: MISS'); $ok = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curlErr = curl_error($ch); curl_close($ch); if ($fp) fclose($fp); $success = ($ok !== false && $httpCode >= 200 && $httpCode < 300 && $wroteAnyOutput); if ($success) { // Atomic move into final cache location — prevents concurrent readers from seeing partial files @rename($tmpFile, $cacheFile); } else { // Upstream failed. Clean up the partial. @unlink($tmpFile); // If we hadn't streamed anything yet AND we have a stale cache, fall back to it. // Better to show 60-second-old data than a blank page. if (!$wroteAnyOutput && file_exists($cacheFile)) { header('X-Proxy-Cache: STALE'); readfile($cacheFile); } elseif (!$wroteAnyOutput) { // No cache, no upstream. Fail with a friendly page. http_response_code(502); ?> <!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Coming back soon</title> <style>body{margin:0;padding:40px 20px;font-family:system-ui,-apple-system,sans-serif;text-align:center;background:#f5f5f5;color:#333;}h1{font-size:22px;margin:0 0 8px}p{font-size:14px;color:#666;margin:4px 0;}small{display:block;margin-top:20px;color:#999;font-size:11px}</style> </head><body> <h1>We'll be back in a moment</h1> <p>Live subscription data isn't reachable right now.</p> <p>Please try again in a few seconds.</p> <small>If this keeps happening, refresh the page or check your connection.</small> </body></html> <?php } } ?>
| ver. 1.4 |
Github
|
.
| PHP 8.2.9 | Generation time: 0.03 |
proxy
|
phpinfo
|
Settings