Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*end of all function and class*/
- /**
- * callImage
- * wrap for all above class with auto cache
- * @Auther : mmis1000
- * @license : GNU Public Licence - Version 3
- * @Parameter :
- * @ $ip : the server ip
- * @ $cache_folder : the folder the store cached status image
- * @ $font : the font file to use
- * @ $templete : the templete image to use
- * @ $configSet : the configSet to use in image, set it to false to use defult
- * @ $cacheTime : the time before status image was expired, defult 300 seconds
- * @ $status_handler : a function / function name that can modify the status array
- * @ Parameter :
- * @ &$status_array (pass by reference)
- */
- function callImage ($ip, $cache_folder, $font, $templete, $config_set = false, $cacheTime = 300, $status_handler = false) {
- if (!preg_match('/\/$/' ,$cache_folder)) {
- $cache_folder .= '/';
- }
- $file_name = 'last_' . base64_encode($ip) . '.png';
- $file_name = str_replace(array('+', '/'), array('-', '_'), $file_name);
- $path = $cache_folder . $file_name;
- $file_time = @filemtime($path);
- if (!file_exists($cache_folder)) {
- mkdir($cache_folder, 0777, true);
- }
- if (!$file_time || (time() - filemtime($path)) > $cacheTime) {//force refresh if file expired
- $Server = new MinecraftServerStatus($ip, 25565, 3);
- $state = $Server->Get();
- if (is_callable($status_handler)) {//allow to modify the results without modify this function
- $status_handler($state);
- /*die( $state['hostname']);*/
- }
- $imagemaker = new status_image_factory($config_set);
- $imagemaker -> getPng($state, $font, $templete, $path);//redirect output to a file
- $file_time = @filemtime($path);//reload file time
- /*$last_modified_time = $file_time;//因為有時因不明原因導致傳輸不全,所以拿掉,但不知為啥還是被快取了
- $etag = base64_encode($ip);
- header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
- header("Etag: $etag"); */
- } else {//check if clinet side cache valid
- $last_modified_time = $file_time;
- $etag = base64_encode($ip);
- header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
- header("Etag: $etag");
- if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time &&
- (isset($_SERVER['HTTP_IF_NONE_MATCH']) ? (@trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) : true)) {
- header("HTTP/1.1 304 Not Modified");
- return;
- }
- }
- header('Content-Type: image/png');
- header("Content-Disposition: inline; filename=\"{$file_name}\"");
- $file=fopen($path,'rb');
- echo fread($file,filesize($path));
- fclose($file);
- }
Advertisement
Add Comment
Please, Sign In to add comment