commit cd91bddd034f605b7871b02b037399eed39b5e39 Author: Stas Volozhanin Date: Mon Apr 13 09:05:00 2026 +0000 first commit diff --git a/404.php b/404.php new file mode 100644 index 0000000..29c75ab --- /dev/null +++ b/404.php @@ -0,0 +1,335 @@ + 0 ) { + + header("HTTP/1.1 200 OK", true); + header('Status: 200 OK', true); + + //Set the content-type + header('Content-Type: image/webp'); + + echo file_get_contents($newImagePath); + + + + } else { + + // There is a problem with the image + // We generate the webp by copying the original image + copy( + $imagePath, + $newImagePath + ); + + + // We take the original file and we save it as .webp + header("HTTP/1.1 200 OK", true); + header('Status: 200 OK', true); + + //Set the content-type + header('Content-Type: image/webp'); + + + + echo file_get_contents($newImagePath); + + //var_dump( file_exists($imagePath), $imagePath ); + + } + + die(); + + + } else { + + // The image not exists, we need to get + if ( $masterServer != "" && $masterHost != "" ) { + + $masterUrl = $masterServer.$_SERVER['REDIRECT_URL']; + + + // var_dump($masterUrl, $masterHost);die(); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $masterUrl); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: '.$masterHost)); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); + curl_setopt($ch, CURLOPT_TIMEOUT, 120); + curl_setopt($ch, CURLOPT_MAXREDIRS, 10); + + $result = curl_exec($ch); + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + + // var_dump( $httpcode, $result);die(); + + if ($httpcode != 200) { + + $seconds_to_cache = 5; + $ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT"; + header("Expires: $ts"); + header("Pragma: cache"); + header("Cache-Control: max-age=$seconds_to_cache"); + echo( $result ); + die(); + + } + + error_log("404.php: request of ".$_SERVER['REDIRECT_URL'], 0); + + header("HTTP/1.1 200 OK", true); + header('Status: 200 OK', true); + // header("Content-type: image/".(($path_parts["extension"]=="jpg")?"jpeg":$path_parts["extension"]), false); + header('Content-Type: image/webp'); + file_put_contents($newImagePath, $result); + + echo( $result ); + + } else { + $filenamePartsArr = explode("-", $path_parts["filename"]); + if ( count( $filenamePartsArr ) > 0 && preg_match('/\d+x\d+\.\w+/', $filenamePartsArr[ count( $filenamePartsArr ) - 1 ], $match ) ) { + $filenamePartsArr_extension = explode(".", $filenamePartsArr[ count($filenamePartsArr)-1 ]); + $imagePath_without_dimension = str_replace("-". $filenamePartsArr_extension[0], "", $imagePath); + + } + + $imagePath_without_scaled = str_replace("-scaled", "", $imagePath); + + if(file_exists($imagePath_without_scaled)){ + // Create the webP version of the image and return it + $newImagePath = $imagePath . ".webp"; + $im = imagecreatefromjpeg($imagePath_without_scaled); + + if ( $im == false ) { + + $im = imagecreatefrompng($imagePath_without_scaled); + + } + + //The path that we want to save our webp file to. + + + //Quality of the new webp image. 1-100. + //Reduce this to decrease the file size. + $quality = 70; + + header("HTTP/1.1 200 OK", true); + header('Status: 200 OK', true); + //Set the content-type + header('Content-Type: image/webp'); + + //Create the webp image and save it + imagewebp($im, $newImagePath, $quality); + + //Clean up / Destroy the image object. + imagedestroy($im); + + echo file_get_contents($newImagePath); + + die(); + }elseif ( file_exists($imagePath_without_dimension) ) { + // Create the webP version of the image and return it + $newImagePath = $imagePath . ".webp"; + $im = imagecreatefromjpeg($imagePath_without_dimension); + + if ( $im == false ) { + + $im = imagecreatefrompng($imagePath_without_dimension); + + } + + //The path that we want to save our webp file to. + + + //Quality of the new webp image. 1-100. + //Reduce this to decrease the file size. + $quality = 70; + + header("HTTP/1.1 200 OK", true); + header('Status: 200 OK', true); + //Set the content-type + header('Content-Type: image/webp'); + + //Create the webp image and save it + imagewebp($im, $newImagePath, $quality); + + //Clean up / Destroy the image object. + imagedestroy($im); + + echo file_get_contents($newImagePath); + + die(); + + } + } + + } + +} else if ($path_parts["extension"]=="jpg" || + $path_parts["extension"]=="gif" || + $path_parts["extension"]=="png" || + $path_parts["extension"]=="jpeg" + ) { + + //echo "curl to $masterUrl\n"; + + // Is an image, check if are present the resize dimensions + $filenamePartsArr = explode("-", $path_parts["filename"]); + + if ( count( $filenamePartsArr ) > 0 ) { + + $newFilename = sprintf("%s.%s", + str_replace("-". $filenamePartsArr[ count($filenamePartsArr)-1 ], "", $path_parts["filename"]), + $path_parts["extension"] + ); + + $newFilePath = "/tauser/www" . $path_parts["dirname"] . "/" . $newFilename; + + if ( file_exists($newFilePath) ) { + + // The file exists, return it in the response + $seconds_to_cache = 5; + $ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT"; + header("Expires: $ts"); + header("Pragma: cache"); + header("Cache-Control: max-age=$seconds_to_cache"); + header("HTTP/1.1 200 OK", true); + header('Status: 200 OK', true); + header("Content-type: ". mime_content_type($newFilePath) , false); + + echo( file_get_contents($newFilePath) ); + + die(); + + } + + } + + if ( $masterServer != "" && $masterHost != "" ) { + + $masterUrl = $masterServer.$_SERVER['REDIRECT_URL']; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $masterUrl); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: '.$masterHost)); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); + curl_setopt($ch, CURLOPT_TIMEOUT, 120); + curl_setopt($ch, CURLOPT_MAXREDIRS, 10); + + $result = curl_exec($ch); + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + + if ($httpcode != 200) { + $seconds_to_cache = 5; + $ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT"; + header("Expires: $ts"); + header("Pragma: cache"); + header("Cache-Control: max-age=$seconds_to_cache"); + } else { + + + + header("HTTP/1.1 200 OK", true); + header('Status: 200 OK', true); + + + } + + error_log("404.php: request of ".$_SERVER['REDIRECT_URL'], 0); + + + header("Content-type: image/".(($path_parts["extension"]=="jpg")?"jpeg":$path_parts["extension"]), false); + + echo( $result ); + + } + + } + + + // If te request image not exists, check + + + die(); \ No newline at end of file