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
| require_once './vendor/autoload.php';
use RingCentral\Psr7\Response; use ServerlessFC\PhpCgiProxy;
define('ROOT_DIR', '/mnt/blog'); define('TEXT_REG', '#\.html.*|\.js.*|\.css.*|\.html.*#'); define('BINARY_REG', '#\.gif.*|\.jpg.*|\.png.*|\.jepg.*|\.swf.*|\.bmp.*|\.ico.*|\.svg.*#');
function debug($contents, $code = 200, $headers = ['Content-Type' => 'text/html']) { if (!is_string($contents)) { $contents = json_encode($contents, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); } return new Response( $code, $headers, $contents ); }
function fileHandler($filename) { $filename = rawurldecode($filename); $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); try { $type = $GLOBALS['fcPhpCgiProxy']->getMimeType($filename); } catch (\Exception $e) { $type = 'application/octet-stream'; } $headers = [ 'Content-Type' => $type, 'Accept-Ranges' => 'bytes', 'Cache-Control' => 'max-age=3600', ]; return new Response(200, $headers, $contents); }
function appHandler($request, $host, $scheme, $script = 'index.php') { $param = [ $request, ROOT_DIR, $script, [ 'HTTP_HOST' => $host, 'SERVER_NAME' => $host, 'SERVER_PORT' => '80', 'REQUEST_SCHEME' => $scheme, 'SCRIPT_FILENAME' => ROOT_DIR . '/' . $script, 'SCRIPT_NAME' => $script, ], [ 'debug_show_cgi_params' => false, 'readWriteTimeout' => 50000, ], ]; try { $resp = call_user_func_array([$GLOBALS['fcPhpCgiProxy'], 'requestPhpCgi'], $param); } catch (\Exception $e) { $GLOBALS['fcPhpCgiProxy'] = new PhpCgiProxy; $resp = call_user_func_array([$GLOBALS['fcPhpCgiProxy'], 'requestPhpCgi'], $param); } return $resp; }
function trace(string $log, string $level = 'info') { $logger = $GLOBALS['fcLogger']; call_user_func([$logger, $level], $log); }
function handler($request, $context): Response { $uri = $request->getAttribute('requestURI'); $host = $request->getHeaderLine('Host'); $method = $request->getMethod(); $scheme = $request->getHeaderLine('X-Forwarded-Proto'); $clientIP = $request->getAttribute('clientIP'); $userAgent = $request->getHeaderLine('user-agent');
// 访问日志 $log = sprintf('%s %s %s %s %s', $method, $host, $uri, $clientIP, $userAgent); // trace($log);
$urlInfo = parse_url($uri); $path = $urlInfo['path']; $query = $urlInfo['query'] ?? '';
$pathInfo = pathinfo($path); $extension = $pathInfo['extension'] ?? ''; $filePath = ROOT_DIR . $path; // 目录跳转 if (!preg_match('#/$#', $path) && is_dir($filePath . '/') && $method == 'GET') { return new Response(301, ['Location' => str_replace($path, $path . '/', $uri)], ''); } // 静态文件 if (file_exists($filePath) && (preg_match(TEXT_REG, $path) || preg_match(BINARY_REG, $path))) { return fileHandler($filePath); } // 直接访问 if (strpos($path, '.php')) { $script = $path; } // 伪静态 if (!strpos($path, '.php')) { // 优先访问index.php if (is_dir($filePath)) { $requestUri = $path . 'index.php' . '?' . $query; $request = $request->withAttribute('requestURI', $requestUri); $script = $path . 'index.php'; } else { $script = 'index.php'; } } return appHandler($request, $host, $scheme, $script); }
// 显示nas目录 // fun nas ls -a nas://
|