SHOW:
|
|
- or go back to the newest paste.
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; |
224 | + | protected $file_list = array(); |
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 |
282 | + | $file_list = glob('./' . $folder . '/*.' . $this->getFileType()); |
283 | if ((!is_array($file_list)) && ($file_list == false)) { | |
284 | $file_list = array(); | |
285 | } | |
286 | $this->setFileList($file_list); # get and set all image files in $folder_name | |
287 | - | if ($this->getFileListCount() > 0) |
287 | + | |
288 | - | $this->old_cache_time = str_replace('.' . $this->getFileType(), '', str_replace('./' . $folder . '/', '', $this->getFileList()[0])); # get the oldest cache file time from filename |
288 | + | |
289 | - | else |
289 | + | |
290 | ||
291 | if ($this->getFileListCount() > 0) { | |
292 | $file_list = $this->getFileList(); | |
293 | $this->old_cache_time = str_replace('.' . $this->getFileType(), '', str_replace('./' . $folder . '/', '', $file_list[0])); # get the oldest cache file time from filename | |
294 | } else { | |
295 | $this->old_cache_time = $this->new_cache_time; | |
296 | } | |
297 | ||
298 | return $this; | |
299 | } | |
300 | ||
301 | public function getFolderName() { | |
302 | return $this->folder_name; | |
303 | } | |
304 | ||
305 | public function getFolder() { | |
306 | return escapeshellcmd($this->folder_name); | |
307 | } | |
308 | ||
309 | public function setTimeUpdateCache($time = 60) { | |
310 | $this->time_update_cache = $time; | |
311 | ||
312 | return $this; | |
313 | } | |
314 | ||
315 | public function getTimeUpdateCache() { | |
316 | return $this->time_update_cache; | |
317 | } | |
318 | ||
319 | public function setKeepFilesCount($file_count = 30) { | |
320 | $this->keep_files = $file_count; | |
321 | ||
322 | return $this; | |
323 | } | |
324 | ||
325 | public function getKeepFilesCount() { | |
326 | return $this->keep_files; | |
327 | } | |
328 | ||
329 | public function clear_cache() { | |
330 | foreach ($this->getFileList() as $file) { | |
331 | unlink($file); | |
332 | } | |
333 | ||
334 | return $this; | |
335 | } | |
336 | } | |
337 | ||
338 | class MCMonitor_Image extends MCMonitor { | |
339 | ||
340 | protected $x_start = 0; | |
341 | protected $y_start = 0; | |
342 | protected $y_end = 20; | |
343 | protected $height = 20; | |
344 | protected $width = 200; | |
345 | ||
346 | protected $img; | |
347 | protected $image_path = ''; | |
348 | ||
349 | public function __construct($img = NULL) { | |
350 | $this->img = $img; | |
351 | } | |
352 | ||
353 | public function getOfflineMessage() { | |
354 | return parent::getInstance()->getOfflineMessage(); | |
355 | } | |
356 | ||
357 | public function getOnlineCount() { | |
358 | return parent::getInstance()->getOnlineCount(); | |
359 | } | |
360 | ||
361 | public function getMaxOnlineCount() { | |
362 | return parent::getInstance()->getMaxOnlineCount(); | |
363 | } | |
364 | ||
365 | public function setMaxOnlineCount($num = 0) { | |
366 | parent::getInstance()->setMaxOnlineCount($num); | |
367 | ||
368 | return $this; | |
369 | } | |
370 | ||
371 | public function setOnlineCount($num = 0) { | |
372 | parent::getInstance()->setOnlineCount($num); | |
373 | ||
374 | return $this; | |
375 | } | |
376 | ||
377 | public function getImageRess() { | |
378 | return $this->img; | |
379 | } | |
380 | ||
381 | public function getData($refresh = false) { | |
382 | return parent::getInstance()->getData(); | |
383 | } | |
384 | ||
385 | public function get_path() { | |
386 | return $this->image_path; | |
387 | } | |
388 | ||
389 | public function set_path($path) { | |
390 | $this->image_path = escapeshellcmd($path); | |
391 | ||
392 | return $this; | |
393 | } | |
394 | ||
395 | public function store() { | |
396 | switch ($this->getFileType()) { | |
397 | case 'jpg': | |
398 | imagejpeg($this->img, $this->image_path, 0); | |
399 | break; | |
400 | case 'gif': | |
401 | imagegif($this->img, $this->image_path, 0); | |
402 | break; | |
403 | default: | |
404 | imagepng($this->img, $this->image_path, 0); | |
405 | break; | |
406 | } | |
407 | ||
408 | return $this; | |
409 | } | |
410 | ||
411 | public function create() { | |
412 | $this->img = imagecreatetruecolor($this->width, $this->height); | |
413 | ||
414 | return $this; | |
415 | } | |
416 | ||
417 | public function open() { | |
418 | switch ($this->getFileType()) { | |
419 | case 'jpg': | |
420 | $this->img = imagecreatefromjpeg($this->get_path()); | |
421 | case 'gif': | |
422 | $this->img = imagecreatefromgif($this->get_path()); | |
423 | } | |
424 | ||
425 | $this->img = imagecreatefrompng($this->get_path()); | |
426 | ||
427 | return $this; | |
428 | } | |
429 | ||
430 | public function send_raw() { | |
431 | switch ($this->getFileType()) { | |
432 | case 'jpg': | |
433 | return imagejpeg($this->getImageRess()); | |
434 | case 'gif': | |
435 | return imagegif($this->getImageRess()); | |
436 | } | |
437 | ||
438 | return imagepng($this->getImageRess()); | |
439 | } | |
440 | ||
441 | public function free_mem() { | |
442 | if ($this->img != null) | |
443 | imagedestroy($this->img); | |
444 | ||
445 | return $this; | |
446 | } | |
447 | ||
448 | public function generate() { | |
449 | $d = imagecolorexact($this->img ,0, 0, 0 ); | |
450 | $online_data = $this->getData(); | |
451 | ||
452 | // if we get the offline message here we got no answer from the server | |
453 | if (is_string($online_data)) { | |
454 | imagefill($this->img, 0, 0, ($blue = imagecolorallocate($this->img, 255, 0, 0))); | |
455 | imagestring($this->img, 5, 70, 2, $this->getOfflineMessage(), $d); | |
456 | return $this; | |
457 | } | |
458 | ||
459 | $this->setMaxOnlineCount(end($online_data)); | |
460 | $this->setOnlineCount(prev($online_data)); | |
461 | ||
462 | reset($online_data); | |
463 | ||
464 | $this->x_end = $this->getOnlineCount() * ($this->width / $this->getMaxOnlineCount()); | |
465 | imagefill($this->img, 0, 0, imagecolorallocate($this->img, 230, 220, 230)); | |
466 | $s=0; # saturation (increase red and blue) | |
467 | $red = imagecolorallocate($this->img, 200-$s, 255, 150-$s); # allocate the imagecolor | |
468 | ||
469 | // set pixels for y and x | |
470 | for ($i = $this->y_start; $i < $this->y_end; $i++){ // y | |
471 | for ($j = $this->x_start; $j < $this->x_end; $j++){ // x | |
472 | imagesetpixel($this->img, $j, $i, $red); | |
473 | $red = imagecolorallocate($this->img, 180-$s, 255, 150-$s); | |
474 | $s = $s + 0.03; | |
475 | } | |
476 | } | |
477 | ||
478 | imagestring($this->img, 5, 80, 2, $this->getOnlineCount() . '/' . $this->getMaxOnlineCount(), $d); | |
479 | ||
480 | return $this; | |
481 | } | |
482 | } | |
483 | ||
484 | $monitor = new MCMonitor($config['host'], $config['port'], $config['offline_message'], $config['file_type']); | |
485 | - | if (count($cache->getFileList()) > 0) { |
485 | + | |
486 | $image = $monitor->getImageInstance(); | |
487 | ||
488 | if (!($config['debug'])) | |
489 | header("Content-type: image/" . $cache->getFileType()); | |
490 | ||
491 | if ($cache->getFileListCount() > 0) { | |
492 | if ($cache->getNewCacheTime() - $cache->getOldCacheTime() >= $cache->getTimeUpdateCache()) { | |
493 | if ($config['debug']) | |
494 | echo "update cache\n"; | |
495 | ||
496 | $image->create()->generate()->set_path('./' . $cache->getFolder() . '/' . $cache->getNewCacheTime() . '.' . $cache->getFileType())->store(); | |
497 | $image->send_raw(); | |
498 | $image->free_mem(); | |
499 | ||
500 | // unlink old file after the new one has been created | |
501 | unlink('./' . $cache->getFolder() . '/' . $cache->getOldCacheTime() . '.' . $cache->getFileType()); | |
502 | } else { | |
503 | if ($config['debug']) | |
504 | echo "load image from cache\n"; | |
505 | ||
506 | $image->set_path('./' . $cache->getFolderName() . '/' . $cache->getOldCacheTime() . '.' . $cache->getFileType())->open()->send_raw(); | |
507 | $image->free_mem(); | |
508 | } | |
509 | } else { | |
510 | if ($config['debug']) | |
511 | echo "empty cache, create image\n"; | |
512 | ||
513 | $image->create()->generate()->set_path('./' . $cache->getFolder() . '/' . $cache->getNewCacheTime() . '.' . $cache->getFileType())->store(); | |
514 | $image->send_raw(); | |
515 | $image->free_mem(); | |
516 | } | |
517 | ?> |