- <?php
- class server_status {
- public $servers = array();
- public $server;
- public $settings = array();
- /**
- * Starts the Process of display the server status
- *
- * Gets the parameters passed and assignes them to variables
- *
- * @param void
- * @return NULL
- */
- public function start()
- {
- $_GET['server'] = strtolower($_GET['server']);
- $this->server = array_key_exists($_GET['server'], $this->servers) ? $this->servers[$_GET['server']] : die('SERVER NOT FOUND');
- $this->server['name'] = $_GET['server']; //Just so it's a little easier giving it a name :)
- if($_GET['text']) $this->settings['text'] = $_GET['text'];
- $this->display();
- }
- /**
- * Makes a connection to the server to see if it is online and responding
- *
- * @param void
- * @return boolean 1 or 0
- */
- public function checkServer()
- {
- if(!$this->server['offline']){
- $check = @fsockopen($this->server['ip'], $this->server['port'], $errnum, $errstr, 2) ? 1 : 0;
- }else{
- $check = 0;
- }
- return $check;
- }
- /**
- * Gets the status of the server from the cache, or if expired re-makes connection and cache's again
- *
- * @param void
- * @return boolean 1 or 0
- */
- private function getStatus()
- {
- $fullpath = $this->settings['fullpath'];
- $cachefile = $this->server['offline'] == FALSE ? $fullpath."cache/".$this->server['name'].".txt" : $fullpath.'cache/offline.txt';
- $cachetime = 5 * 60; // 5 minutes
- // Serve from the cache if it is younger than $cachetime
- if ((file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) or $this->server['offline'])
- {
- $this->logger('Loading from cache');
- $this->logger('Loading File', $cachefile);
- $file = fopen($cachefile, 'r');
- $contents = fread($file, filesize($cachefile));
- $this->logger('File contents', $contents);
- $json = json_decode($contents, 1);
- $online = $json['online'] ? 1 : 0;
- $this->logger('JSON Array', print_r($json, true));
- $this->logger('Online Status', $online);
- $this->logger("Cached",date('jS F Y H:i', filemtime($cachefile)));
- }else{
- $this->logger("Writing Cache File");
- $arr = array(); //ARRRGH PIRATES
- $check = $this->checkServer();
- $online = $arr['online'] = $check;
- $this->logger('Online Status', $online);
- $this->logger("Array List", print_r($arr, true));
- $this->logger("Json encoded", json_encode($arr));
- if (is_writable($cachefile) or true) {
- if (!$handle = fopen($cachefile, 'w+')) {
- $this->logger("Cannot open file ($cachefile)");
- exit;
- }
- if (fwrite($handle, json_encode($arr)) === FALSE) {
- $this->logger("Cannot write to file ($cachefile)");
- exit;
- }
- $this->logger("Success, wrote ($cachefile) to file ($cachefile)");
- fclose($handle);
- } else {
- $this->logger("The file $cachefile is not writable");
- }
- }
- return $online;
- }
- /**
- * Displays the image
- *
- * $param void
- * @output image png or text
- * @return NULL
- */
- private function display()
- {
- $online = $this->getStatus();
- $this->logger('Display Online status', $online);
- //OUTPUT TIME BABY!!
- header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
- $time = date('r', time() + 300); //Time in 5 minutes from now
- header("Expires: $time");
- //header("Expires: Tue, 23 Jul 1994 08:00:00 GMT"); // Date in the past
- if(!$this->settings['text']){
- header("Content-type: image/png");
- if($online){
- readfile($this->settings['fullpath'].$this->server['name'].'-on.png');
- }else{
- readfile($this->settings['fullpath'].$this->server['name'].'-off.png');
- }
- }else{
- if($online){
- $color = "green";
- }else{
- $color = "red";
- }
- $text = (!$online) ? "OFFLINE": "ONLINE";
- echo "<font style='color:$color'>$text</font>";
- }
- }
- /**
- * When the debug parameter is true when loading the &text=true variable, it will output extra information
- *
- * @param string $item
- * @param string $text
- * @return NULL
- */
- private function logger($item, $text = '')
- {
- if($this->settings['debug'] == TRUE)
- {
- echo "$item : $text <br /><br />\n\n";
- }
- }
- }
- $status = new server_status();
- $status->settings['debug'] = FALSE; //Returns extra informations only viewable if &text=true
- $status->settings['fullpath'] = ""; //full path to this script including /
- /**
- * List servers in an array
- *
- * Format is: 'SERVER NAME' => array('ip' => '', 'port' => ''),
- * There is an additional " 'offline' => true " if you would like to be able to display a server that is not online or you want to force it to show offline
- *
- * NOTE: you will need images corresponding to <server_name>-on.png and <server_name>-off.png in the script path.
- */
- $status->servers = array(
- 'server_name' => array('ip' => '', 'port' => ''),
- 'server_2' => array('ip' => '', 'port' => '')
- );
- $status->start();
- ?>