Advertisement
Guest User

Minecraft Online-Status Image-Generator (1.4.7)

a guest
Jan 30th, 2013
3,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.75 KB | None | 0 0
  1. <?php
  2. /**
  3. * --------------------------------------------------------------------------------
  4. * - Minecraft Online-Status Image-Generator
  5. * --------------------------------------------------------------------------------
  6. * This code is based on Revi's initial idea and work
  7. * http://forums.bukkit.org/threads/web-beautiful-monitoring.122457
  8. *
  9. * Features
  10. * - supports png, jpg and gif as file_type
  11. * - per image cache files
  12. * - keep the cache clean (default is 30 missed unlink actions)
  13. *
  14. */
  15.  
  16.  
  17. /**
  18. * --------------------------------------------------------------------------------
  19. * - Configuration Section
  20. * --------------------------------------------------------------------------------
  21. */
  22. error_reporting(0); # set to E_ALL when debugging otherwise you can leave this alone
  23.  
  24. $config['host'] = '127.0.0.1'; # IP or hostname
  25. $config['port'] = 25565; # port
  26. $config['folder_name'] = 'cache'; # folder used for caching; Make sure the folder is created and has the correct access rights
  27. $config['time_update_cache'] = 60; # update cache every x second, decrease this value if you want a faster response time
  28. $config['offline_message'] = 'offline'; # this message will be shown, when the server is offline or in case of connection problems
  29. $config['file_type'] = @$_GET['t']; # supported file_types (png|jpg|gif)
  30. $config['keep_files'] = 30; # keep n files before rebuilding the cache
  31. $config['debug'] = false; # set to true for debugging
  32.  
  33. /**
  34. * You normally don't have to edit below this line.
  35. * Only edit below this line if you know what you are doing.
  36. */
  37. class MCMonitor {
  38.  
  39.     protected $host;
  40.     protected $port;
  41.    
  42.     protected $offline_message;
  43.     protected $file_type;
  44.    
  45.     protected $is_connected = false;
  46.     protected $is_online = false;
  47.  
  48.     protected $online = 0;
  49.     protected $max_online = 0;
  50.    
  51.     protected $data;
  52.    
  53.     protected static $cache;
  54.     protected static $image;
  55.     protected static $instance;
  56.    
  57.     public function __construct($host = '127.0.0.1', $port = 25565, $offline_message = 'offline', $file_type = 'png') {    
  58.         $this->setHost($host);
  59.         $this->setPort($port);
  60.                
  61.         $this->setOfflineMessage($offline_message);
  62.         $this->setFileType($file_type);
  63.        
  64.         // try to connect and set online status
  65.         $this->getData(true);
  66.        
  67.         self::$instance = $this;
  68.     }
  69.    
  70.     public function getInstance() {
  71.         if (self::$instance == null) {
  72.             global $config;
  73.            
  74.             self::$instance = new MCMonitor($config['host'], $config['port'], $config['offline_message'], $config['file_type']);
  75.         }
  76.            
  77.         return self::$instance;
  78.     }
  79.    
  80.     public function setOnlineCount($num = 0) {
  81.         $this->online = $num;
  82.        
  83.         return $this;
  84.     }
  85.    
  86.     public function getOnlineCount() {
  87.         return $this->online;
  88.     }
  89.    
  90.     public function setMaxOnlineCount($num = 0) {
  91.         $this->max_online = $num;
  92.        
  93.         return $this;
  94.     }
  95.    
  96.     public function getMaxOnlineCount() {
  97.         return $this->max_online;
  98.     }  
  99.    
  100.     public function getData($refresh = false) {
  101.         if (($this->data == null) || ($refresh))
  102.             return $this->fetchData();
  103.        
  104.         return $this->data;
  105.     }
  106.    
  107.     private function fetchData() {
  108.         $socket = @fsockopen($this->getHost(), $this->getPort());
  109.    
  110.         if ($socket !== false) {
  111.             $this->is_connected = true;
  112.            
  113.             // connect to host
  114.             fwrite($socket, "\xFE\x01");
  115.             $data = fread($socket, 1024);
  116.             fclose($socket);
  117.            
  118.             $this->is_connected = false;
  119.  
  120.             // is the host online?
  121.             if (($data !== false) && (substr($data, 0, 1) == "\xFF")) {
  122.                 $this->is_online = true;
  123.                 //$info = explode("\xA7", mb_convert_encoding(substr($data, 1), "auto", "UCS-2"));
  124.                 $this->data = explode("\x00", mb_convert_encoding(substr($data, 1), 'auto', 'UCS-2'));
  125.                 //var_dump($info); # uncomment to debug the parsed online packet
  126.                 return $this->data;
  127.             }
  128.         }
  129.        
  130.         $this->data = $this->getOfflineMessage();
  131.         $this->is_online = false;
  132.        
  133.         return $this->data;
  134.     }
  135.    
  136.     public function setHost($host = '127.0.0.1') {
  137.         $this->host = $host;
  138.        
  139.         return $this;
  140.     }
  141.    
  142.     public function getHost() {
  143.         return $this->host;
  144.     }
  145.    
  146.     public function setPort($port = 25565) {
  147.         $this->port = $port;
  148.        
  149.         return $this;
  150.     }
  151.    
  152.     public function getPort() {
  153.         return $this->port;
  154.     }
  155.    
  156.     public function setOfflineMessage($offline_message = 'offline') {
  157.         $this->offline_message = $offline_message;
  158.        
  159.         return $this;
  160.     }
  161.    
  162.     public function getOfflineMessage() {
  163.         return $this->offline_message;
  164.     }
  165.    
  166.     public function setFileType($file_type = 'png') {
  167.         $this->file_type = ($this->is_supported_fileType($file_type)) ? $file_type : 'png'; # always fallback to png;
  168.        
  169.         return $this;
  170.     }
  171.    
  172.     public function getFileType() {
  173.         return $this->file_type;
  174.     }
  175.    
  176.     public function is_supported_fileType($file_type) {
  177.         switch ($file_type) {
  178.             case 'png':
  179.             case 'jpg':
  180.             case 'gif':
  181.                 return true;
  182.         }
  183.        
  184.         return false;
  185.     }  
  186.  
  187.     public function isOnline() {
  188.         return $this->is_online;
  189.     }
  190.    
  191.     public function isConnected() {
  192.         return $this->is_connected;
  193.     }
  194.    
  195.     public function getCacheInstance() {
  196.         if (self::$cache == null) {
  197.             global $config;
  198.            
  199.             self::$cache = new MCMonitor_Cache($config['folder_name'], $config['keep_files'], $config['time_update_cache']);
  200.         }
  201.        
  202.         return self::$cache;
  203.     }
  204.    
  205.     public function getImageInstance() {
  206.         if (self::$image == null) {
  207.             self::$image = new MCMonitor_Image();
  208.         }
  209.            
  210.         return self::$image;   
  211.     }
  212.    
  213.     function send_raw() {
  214.         return $this->getImageInstance()->send_raw();      
  215.     }
  216. }
  217.  
  218.  
  219. class MCMonitor_Cache extends MCMonitor {
  220.  
  221.     protected $folder_name;
  222.     protected $time_update_cache;
  223.    
  224.     protected $file_list;
  225.     protected $keep_files;
  226.    
  227.     protected $new_cache_time;
  228.     protected $old_cache_time;
  229.        
  230.     public function __construct($folder_name = 'cache', $keep_files = 30, $time_update_cache = 60) {
  231.         $this->folder_name = $folder_name;
  232.         $this->keep_files = $keep_files;
  233.         $this->time_update_cache = $time_update_cache;
  234.         $this->fetch_fileList();
  235.     }
  236.    
  237.     public function setFolderName($folder_name = 'cache') {
  238.         $this->folder_name = $folder_name;
  239.        
  240.         return $this;
  241.     }
  242.    
  243.     public function setFileList($list) {
  244.         $this->file_list = $list;
  245.        
  246.         return $this;
  247.     }
  248.    
  249.     public function getFileList() {
  250.         return $this->file_list;
  251.     }
  252.    
  253.     public function getFileListCount() {
  254.         return count($this->file_list);
  255.     }
  256.    
  257.     public function getFileType() {
  258.         return parent::getInstance()->getFileType();
  259.     }
  260.        
  261.     public function getNewCacheTime() {
  262.         if ($this->new_cache_time == null)
  263.             $this->fetch_fileList();
  264.        
  265.         return $this->new_cache_time;
  266.     }
  267.    
  268.     public function getOldCacheTime() {
  269.         if ($this->old_cache_time == null)
  270.             $this->fetch_fileList();   
  271.        
  272.         return $this->old_cache_time;
  273.     }
  274.        
  275.     public function fetch_fileList() {     
  276.         $folder = $this->getFolder();
  277.        
  278.         if (!file_exists($folder))
  279.             mkdir($folder);
  280.        
  281.         $this->new_cache_time = time();
  282.         $this->setFileList(glob('./' . $folder . '/*.' . $this->getFileType())); # get all image files in $folder_name 
  283.        
  284.         if ($this->getFileListCount() >= $this->getKeepFilesCount())
  285.             $this->clear_cache();
  286.        
  287.         if ($this->getFileListCount() > 0)
  288.             $this->old_cache_time = str_replace('.' . $this->getFileType(), '', str_replace('./' . $folder . '/', '', $this->getFileList()[0])); # get the oldest cache file time from filename    
  289.         else
  290.             $this->old_cache_time = $this->new_cache_time;
  291.        
  292.         return $this;
  293.     }
  294.    
  295.     public function getFolderName() {
  296.         return $this->folder_name;
  297.     }
  298.    
  299.     public function getFolder() {
  300.         return escapeshellcmd($this->folder_name);
  301.     }
  302.    
  303.     public function setTimeUpdateCache($time = 60) {
  304.         $this->time_update_cache = $time;
  305.        
  306.         return $this;
  307.     }
  308.    
  309.     public function getTimeUpdateCache() {
  310.         return $this->time_update_cache;
  311.     }
  312.    
  313.     public function setKeepFilesCount($file_count = 30) {
  314.         $this->keep_files = $file_count;
  315.        
  316.         return $this;
  317.     }
  318.    
  319.     public function getKeepFilesCount() {
  320.         return $this->keep_files;
  321.     }
  322.    
  323.     public function clear_cache() {
  324.         foreach ($this->getFileList() as $file) {
  325.             unlink($file);
  326.         }
  327.        
  328.         return $this;
  329.     }
  330. }
  331.  
  332. class MCMonitor_Image extends MCMonitor {
  333.  
  334.     protected $x_start = 0;
  335.     protected $y_start = 0;
  336.     protected $y_end = 20;
  337.     protected $height = 20;
  338.     protected $width = 200;
  339.    
  340.     protected $img;
  341.     protected $image_path = '';
  342.  
  343.     public function __construct($img = NULL) {
  344.         $this->img = $img;
  345.     }
  346.    
  347.     public function getOfflineMessage() {
  348.         return parent::getInstance()->getOfflineMessage();
  349.     }  
  350.    
  351.     public function getOnlineCount() {
  352.         return parent::getInstance()->getOnlineCount();
  353.     }
  354.    
  355.     public function getMaxOnlineCount() {
  356.         return parent::getInstance()->getMaxOnlineCount();
  357.     }
  358.    
  359.     public function setMaxOnlineCount($num = 0) {
  360.         parent::getInstance()->setMaxOnlineCount($num);
  361.        
  362.         return $this;
  363.     }  
  364.    
  365.     public function setOnlineCount($num = 0) {
  366.         parent::getInstance()->setOnlineCount($num);
  367.        
  368.         return $this;
  369.     }      
  370.    
  371.     public function getImageRess() {
  372.         return $this->img;
  373.     }
  374.    
  375.     public function getData($refresh = false) {
  376.         return parent::getInstance()->getData();
  377.     }
  378.    
  379.     public function get_path() {
  380.         return $this->image_path;
  381.     }
  382.    
  383.     public function set_path($path) {
  384.         $this->image_path = escapeshellcmd($path);
  385.        
  386.         return $this;
  387.     }
  388.    
  389.     public function store() {
  390.         switch ($this->getFileType()) {        
  391.             case 'jpg':
  392.                 imagejpeg($this->img, $this->image_path, 0);
  393.                 break;
  394.             case 'gif':
  395.                 imagegif($this->img, $this->image_path, 0);
  396.                 break;
  397.             default:
  398.                 imagepng($this->img, $this->image_path, 0);
  399.                 break;
  400.         }
  401.                
  402.         return $this;
  403.     }
  404.  
  405.     public function create() {
  406.         $this->img = imagecreatetruecolor($this->width, $this->height);
  407.        
  408.         return $this;
  409.     }
  410.    
  411.     public function open() {       
  412.         switch ($this->getFileType()) {
  413.             case 'jpg':
  414.                 $this->img = imagecreatefromjpeg($this->get_path());
  415.             case 'gif':
  416.                 $this->img = imagecreatefromgif($this->get_path());
  417.         }
  418.        
  419.         $this->img = imagecreatefrompng($this->get_path());
  420.        
  421.         return $this;
  422.     }
  423.    
  424.     public function send_raw() {
  425.         switch ($this->getFileType()) {        
  426.             case 'jpg':
  427.                 return imagejpeg($this->getImageRess());
  428.             case 'gif':
  429.                 return imagegif($this->getImageRess());
  430.         }
  431.    
  432.         return imagepng($this->getImageRess());
  433.     }
  434.    
  435.     public function free_mem() {
  436.         if ($this->img != null)
  437.             imagedestroy($this->img);
  438.            
  439.         return $this;
  440.     }
  441.    
  442.     public function generate() {
  443.         $d = imagecolorexact($this->img ,0, 0, 0 );
  444.         $online_data = $this->getData();
  445.                
  446.         // if we get the offline message here we got no answer from the server
  447.         if (is_string($online_data)) {
  448.             imagefill($this->img, 0, 0, ($blue = imagecolorallocate($this->img, 255, 0, 0)));
  449.             imagestring($this->img, 5, 70, 2, $this->getOfflineMessage(), $d);
  450.             return $this;
  451.         }
  452.        
  453.         $this->setMaxOnlineCount(end($online_data));
  454.         $this->setOnlineCount(prev($online_data));
  455.        
  456.         reset($online_data);
  457.            
  458.         $this->x_end = $this->getOnlineCount() * ($this->width / $this->getMaxOnlineCount());
  459.         imagefill($this->img, 0, 0, imagecolorallocate($this->img, 230, 220, 230));
  460.         $s=0; # saturation (increase red and blue)
  461.         $red = imagecolorallocate($this->img, 200-$s, 255, 150-$s); # allocate the imagecolor
  462.        
  463.         // set pixels for y and x
  464.         for ($i = $this->y_start; $i < $this->y_end; $i++){ // y
  465.             for ($j = $this->x_start; $j < $this->x_end; $j++){ // x
  466.                 imagesetpixel($this->img, $j, $i, $red);
  467.                 $red = imagecolorallocate($this->img, 180-$s, 255, 150-$s);
  468.                 $s = $s + 0.03;
  469.             }
  470.         }
  471.        
  472.         imagestring($this->img, 5, 80, 2, $this->getOnlineCount() . '/' . $this->getMaxOnlineCount(), $d); 
  473.        
  474.         return $this;
  475.     }  
  476. }
  477.  
  478. $monitor = new MCMonitor($config['host'], $config['port'], $config['offline_message'], $config['file_type']);
  479. $cache = $monitor->getCacheInstance();
  480. $image = $monitor->getImageInstance();
  481.  
  482. if (!($config['debug']))
  483.     header("Content-type: image/" . $cache->getFileType());
  484.  
  485. if (count($cache->getFileList()) > 0) {
  486.     if ($cache->getNewCacheTime() - $cache->getOldCacheTime() >= $cache->getTimeUpdateCache()) {
  487.         if ($config['debug'])
  488.             echo "update cache\n";
  489.  
  490.         $image->create()->generate()->set_path('./' . $cache->getFolder() . '/' . $cache->getNewCacheTime() . '.' . $cache->getFileType())->store();
  491.         $image->send_raw();
  492.         $image->free_mem();
  493.        
  494.         // unlink old file after the new one has been created
  495.         unlink('./' . $cache->getFolder() . '/' . $cache->getOldCacheTime() . '.' . $cache->getFileType());
  496.     } else {
  497.         if ($config['debug'])
  498.             echo "load image from cache\n";
  499.            
  500.         $image->set_path('./' . $cache->getFolderName() . '/' . $cache->getOldCacheTime() . '.' . $cache->getFileType())->open()->send_raw();
  501.         $image->free_mem();
  502.     }
  503. } else {
  504.     if ($config['debug'])
  505.         echo "empty cache, create image\n";
  506.        
  507.     $image->create()->generate()->set_path('./' . $cache->getFolder() . '/' . $cache->getNewCacheTime() . '.' . $cache->getFileType())->store();
  508.     $image->send_raw();
  509.     $image->free_mem();
  510. }
  511. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement