Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * --------------------------------------------------------------------------------
- * - Minecraft Online-Status Image-Generator
- * --------------------------------------------------------------------------------
- * This code is based on Revi's initial idea and work
- * http://forums.bukkit.org/threads/web-beautiful-monitoring.122457
- *
- * Features
- * - supports png, jpg and gif as file_type
- * - per image cache files
- * - keep the cache clean (default is 30 missed unlink actions)
- *
- */
- /**
- * --------------------------------------------------------------------------------
- * - Configuration Section
- * --------------------------------------------------------------------------------
- */
- error_reporting(0); # set to E_ALL when debugging otherwise you can leave this alone
- $config['host'] = '127.0.0.1'; # IP or hostname
- $config['port'] = 25565; # port
- $config['folder_name'] = 'cache'; # folder used for caching; Make sure the folder is created and has the correct access rights
- $config['time_update_cache'] = 60; # update cache every x second, decrease this value if you want a faster response time
- $config['offline_message'] = 'offline'; # this message will be shown, when the server is offline or in case of connection problems
- $config['file_type'] = @$_GET['t']; # supported file_types (png|jpg|gif)
- $config['keep_files'] = 30; # keep n files before rebuilding the cache
- $config['debug'] = false; # set to true for debugging
- /**
- * You normally don't have to edit below this line.
- * Only edit below this line if you know what you are doing.
- */
- class MCMonitor {
- protected $host;
- protected $port;
- protected $offline_message;
- protected $file_type;
- protected $is_connected = false;
- protected $is_online = false;
- protected $online = 0;
- protected $max_online = 0;
- protected $data;
- protected static $cache;
- protected static $image;
- protected static $instance;
- public function __construct($host = '127.0.0.1', $port = 25565, $offline_message = 'offline', $file_type = 'png') {
- $this->setHost($host);
- $this->setPort($port);
- $this->setOfflineMessage($offline_message);
- $this->setFileType($file_type);
- // try to connect and set online status
- $this->getData(true);
- self::$instance = $this;
- }
- public function getInstance() {
- if (self::$instance == null) {
- global $config;
- self::$instance = new MCMonitor($config['host'], $config['port'], $config['offline_message'], $config['file_type']);
- }
- return self::$instance;
- }
- public function setOnlineCount($num = 0) {
- $this->online = $num;
- return $this;
- }
- public function getOnlineCount() {
- return $this->online;
- }
- public function setMaxOnlineCount($num = 0) {
- $this->max_online = $num;
- return $this;
- }
- public function getMaxOnlineCount() {
- return $this->max_online;
- }
- public function getData($refresh = false) {
- if (($this->data == null) || ($refresh))
- return $this->fetchData();
- return $this->data;
- }
- private function fetchData() {
- $socket = @fsockopen($this->getHost(), $this->getPort());
- if ($socket !== false) {
- $this->is_connected = true;
- // connect to host
- fwrite($socket, "\xFE\x01");
- $data = fread($socket, 1024);
- fclose($socket);
- $this->is_connected = false;
- // is the host online?
- if (($data !== false) && (substr($data, 0, 1) == "\xFF")) {
- $this->is_online = true;
- //$info = explode("\xA7", mb_convert_encoding(substr($data, 1), "auto", "UCS-2"));
- $this->data = explode("\x00", mb_convert_encoding(substr($data, 1), 'auto', 'UCS-2'));
- //var_dump($info); # uncomment to debug the parsed online packet
- return $this->data;
- }
- }
- $this->data = $this->getOfflineMessage();
- $this->is_online = false;
- return $this->data;
- }
- public function setHost($host = '127.0.0.1') {
- $this->host = $host;
- return $this;
- }
- public function getHost() {
- return $this->host;
- }
- public function setPort($port = 25565) {
- $this->port = $port;
- return $this;
- }
- public function getPort() {
- return $this->port;
- }
- public function setOfflineMessage($offline_message = 'offline') {
- $this->offline_message = $offline_message;
- return $this;
- }
- public function getOfflineMessage() {
- return $this->offline_message;
- }
- public function setFileType($file_type = 'png') {
- $this->file_type = ($this->is_supported_fileType($file_type)) ? $file_type : 'png'; # always fallback to png;
- return $this;
- }
- public function getFileType() {
- return $this->file_type;
- }
- public function is_supported_fileType($file_type) {
- switch ($file_type) {
- case 'png':
- case 'jpg':
- case 'gif':
- return true;
- }
- return false;
- }
- public function isOnline() {
- return $this->is_online;
- }
- public function isConnected() {
- return $this->is_connected;
- }
- public function getCacheInstance() {
- if (self::$cache == null) {
- global $config;
- self::$cache = new MCMonitor_Cache($config['folder_name'], $config['keep_files'], $config['time_update_cache']);
- }
- return self::$cache;
- }
- public function getImageInstance() {
- if (self::$image == null) {
- self::$image = new MCMonitor_Image();
- }
- return self::$image;
- }
- function send_raw() {
- return $this->getImageInstance()->send_raw();
- }
- }
- class MCMonitor_Cache extends MCMonitor {
- protected $folder_name;
- protected $time_update_cache;
- protected $file_list;
- protected $keep_files;
- protected $new_cache_time;
- protected $old_cache_time;
- public function __construct($folder_name = 'cache', $keep_files = 30, $time_update_cache = 60) {
- $this->folder_name = $folder_name;
- $this->keep_files = $keep_files;
- $this->time_update_cache = $time_update_cache;
- $this->fetch_fileList();
- }
- public function setFolderName($folder_name = 'cache') {
- $this->folder_name = $folder_name;
- return $this;
- }
- public function setFileList($list) {
- $this->file_list = $list;
- return $this;
- }
- public function getFileList() {
- return $this->file_list;
- }
- public function getFileListCount() {
- return count($this->file_list);
- }
- public function getFileType() {
- return parent::getInstance()->getFileType();
- }
- public function getNewCacheTime() {
- if ($this->new_cache_time == null)
- $this->fetch_fileList();
- return $this->new_cache_time;
- }
- public function getOldCacheTime() {
- if ($this->old_cache_time == null)
- $this->fetch_fileList();
- return $this->old_cache_time;
- }
- public function fetch_fileList() {
- $folder = $this->getFolder();
- if (!file_exists($folder))
- mkdir($folder);
- $this->new_cache_time = time();
- $this->setFileList(glob('./' . $folder . '/*.' . $this->getFileType())); # get all image files in $folder_name
- if ($this->getFileListCount() >= $this->getKeepFilesCount())
- $this->clear_cache();
- if ($this->getFileListCount() > 0)
- $this->old_cache_time = str_replace('.' . $this->getFileType(), '', str_replace('./' . $folder . '/', '', $this->getFileList()[0])); # get the oldest cache file time from filename
- else
- $this->old_cache_time = $this->new_cache_time;
- return $this;
- }
- public function getFolderName() {
- return $this->folder_name;
- }
- public function getFolder() {
- return escapeshellcmd($this->folder_name);
- }
- public function setTimeUpdateCache($time = 60) {
- $this->time_update_cache = $time;
- return $this;
- }
- public function getTimeUpdateCache() {
- return $this->time_update_cache;
- }
- public function setKeepFilesCount($file_count = 30) {
- $this->keep_files = $file_count;
- return $this;
- }
- public function getKeepFilesCount() {
- return $this->keep_files;
- }
- public function clear_cache() {
- foreach ($this->getFileList() as $file) {
- unlink($file);
- }
- return $this;
- }
- }
- class MCMonitor_Image extends MCMonitor {
- protected $x_start = 0;
- protected $y_start = 0;
- protected $y_end = 20;
- protected $height = 20;
- protected $width = 200;
- protected $img;
- protected $image_path = '';
- public function __construct($img = NULL) {
- $this->img = $img;
- }
- public function getOfflineMessage() {
- return parent::getInstance()->getOfflineMessage();
- }
- public function getOnlineCount() {
- return parent::getInstance()->getOnlineCount();
- }
- public function getMaxOnlineCount() {
- return parent::getInstance()->getMaxOnlineCount();
- }
- public function setMaxOnlineCount($num = 0) {
- parent::getInstance()->setMaxOnlineCount($num);
- return $this;
- }
- public function setOnlineCount($num = 0) {
- parent::getInstance()->setOnlineCount($num);
- return $this;
- }
- public function getImageRess() {
- return $this->img;
- }
- public function getData($refresh = false) {
- return parent::getInstance()->getData();
- }
- public function get_path() {
- return $this->image_path;
- }
- public function set_path($path) {
- $this->image_path = escapeshellcmd($path);
- return $this;
- }
- public function store() {
- switch ($this->getFileType()) {
- case 'jpg':
- imagejpeg($this->img, $this->image_path, 0);
- break;
- case 'gif':
- imagegif($this->img, $this->image_path, 0);
- break;
- default:
- imagepng($this->img, $this->image_path, 0);
- break;
- }
- return $this;
- }
- public function create() {
- $this->img = imagecreatetruecolor($this->width, $this->height);
- return $this;
- }
- public function open() {
- switch ($this->getFileType()) {
- case 'jpg':
- $this->img = imagecreatefromjpeg($this->get_path());
- case 'gif':
- $this->img = imagecreatefromgif($this->get_path());
- }
- $this->img = imagecreatefrompng($this->get_path());
- return $this;
- }
- public function send_raw() {
- switch ($this->getFileType()) {
- case 'jpg':
- return imagejpeg($this->getImageRess());
- case 'gif':
- return imagegif($this->getImageRess());
- }
- return imagepng($this->getImageRess());
- }
- public function free_mem() {
- if ($this->img != null)
- imagedestroy($this->img);
- return $this;
- }
- public function generate() {
- $d = imagecolorexact($this->img ,0, 0, 0 );
- $online_data = $this->getData();
- // if we get the offline message here we got no answer from the server
- if (is_string($online_data)) {
- imagefill($this->img, 0, 0, ($blue = imagecolorallocate($this->img, 255, 0, 0)));
- imagestring($this->img, 5, 70, 2, $this->getOfflineMessage(), $d);
- return $this;
- }
- $this->setMaxOnlineCount(end($online_data));
- $this->setOnlineCount(prev($online_data));
- reset($online_data);
- $this->x_end = $this->getOnlineCount() * ($this->width / $this->getMaxOnlineCount());
- imagefill($this->img, 0, 0, imagecolorallocate($this->img, 230, 220, 230));
- $s=0; # saturation (increase red and blue)
- $red = imagecolorallocate($this->img, 200-$s, 255, 150-$s); # allocate the imagecolor
- // set pixels for y and x
- for ($i = $this->y_start; $i < $this->y_end; $i++){ // y
- for ($j = $this->x_start; $j < $this->x_end; $j++){ // x
- imagesetpixel($this->img, $j, $i, $red);
- $red = imagecolorallocate($this->img, 180-$s, 255, 150-$s);
- $s = $s + 0.03;
- }
- }
- imagestring($this->img, 5, 80, 2, $this->getOnlineCount() . '/' . $this->getMaxOnlineCount(), $d);
- return $this;
- }
- }
- $monitor = new MCMonitor($config['host'], $config['port'], $config['offline_message'], $config['file_type']);
- $cache = $monitor->getCacheInstance();
- $image = $monitor->getImageInstance();
- if (!($config['debug']))
- header("Content-type: image/" . $cache->getFileType());
- if (count($cache->getFileList()) > 0) {
- if ($cache->getNewCacheTime() - $cache->getOldCacheTime() >= $cache->getTimeUpdateCache()) {
- if ($config['debug'])
- echo "update cache\n";
- $image->create()->generate()->set_path('./' . $cache->getFolder() . '/' . $cache->getNewCacheTime() . '.' . $cache->getFileType())->store();
- $image->send_raw();
- $image->free_mem();
- // unlink old file after the new one has been created
- unlink('./' . $cache->getFolder() . '/' . $cache->getOldCacheTime() . '.' . $cache->getFileType());
- } else {
- if ($config['debug'])
- echo "load image from cache\n";
- $image->set_path('./' . $cache->getFolderName() . '/' . $cache->getOldCacheTime() . '.' . $cache->getFileType())->open()->send_raw();
- $image->free_mem();
- }
- } else {
- if ($config['debug'])
- echo "empty cache, create image\n";
- $image->create()->generate()->set_path('./' . $cache->getFolder() . '/' . $cache->getNewCacheTime() . '.' . $cache->getFileType())->store();
- $image->send_raw();
- $image->free_mem();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement