Guest User

Untitled

a guest
Nov 26th, 2013
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.01 KB | None | 0 0
  1.     /*end of all function and class*/
  2.    
  3.     /**
  4.     * callImage
  5.     * wrap for all above class with auto cache
  6.     * @Auther : mmis1000
  7.     * @license : GNU Public Licence - Version 3
  8.     * @Parameter :
  9.     * @  $ip : the server ip
  10.     * @  $cache_folder : the folder the store cached status image
  11.     * @  $font : the font file to use
  12.     * @  $templete : the templete image to use
  13.     * @  $configSet : the configSet to use in image, set it to false to use defult
  14.     * @  $cacheTime : the time before status image was expired, defult 300 seconds
  15.     * @  $status_handler : a function / function name that can modify the status array
  16.     * @    Parameter :
  17.     * @      &$status_array (pass by reference)
  18.     */
  19.     function callImage ($ip, $cache_folder, $font, $templete, $config_set = false, $cacheTime = 300, $status_handler = false) {
  20.         if (!preg_match('/\/$/' ,$cache_folder)) {
  21.             $cache_folder .= '/';
  22.         }
  23.         $file_name = 'last_' . base64_encode($ip) . '.png';
  24.         $file_name =  str_replace(array('+', '/'), array('-', '_'), $file_name);
  25.         $path = $cache_folder . $file_name;
  26.         $file_time = @filemtime($path);
  27.         if (!file_exists($cache_folder)) {
  28.             mkdir($cache_folder, 0777, true);
  29.         }
  30.         if (!$file_time || (time() - filemtime($path)) > $cacheTime) {//force refresh if file expired
  31.             $Server = new MinecraftServerStatus($ip, 25565, 3);
  32.             $state = $Server->Get();
  33.             if (is_callable($status_handler)) {//allow to modify the results without modify this function
  34.                 $status_handler($state);
  35.                 /*die( $state['hostname']);*/
  36.             }
  37.             $imagemaker = new status_image_factory($config_set);
  38.             $imagemaker -> getPng($state, $font, $templete, $path);//redirect output to a file
  39.            
  40.             $file_time = @filemtime($path);//reload file time
  41.             /*$last_modified_time = $file_time;//因為有時因不明原因導致傳輸不全,所以拿掉,但不知為啥還是被快取了
  42.             $etag = base64_encode($ip);
  43.             header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
  44.             header("Etag: $etag"); */
  45.         } else {//check if clinet side cache valid
  46.             $last_modified_time = $file_time;
  47.             $etag = base64_encode($ip);
  48.             header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
  49.             header("Etag: $etag");
  50.             if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time &&
  51.                 (isset($_SERVER['HTTP_IF_NONE_MATCH']) ? (@trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) : true)) {
  52.                 header("HTTP/1.1 304 Not Modified");
  53.                 return;
  54.             }
  55.         }
  56.         header('Content-Type: image/png');
  57.         header("Content-Disposition: inline; filename=\"{$file_name}\"");
  58.         $file=fopen($path,'rb');
  59.         echo fread($file,filesize($path));
  60.         fclose($file);
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment