This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/Status.php
2016-12-09 23:28:25 +01:00

102 lines
2.8 KiB
PHP

<?php
/**
* Status checking.
* @package Sakura
*/
namespace Sakura;
/**
* Status checking.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class Status
{
private $address = '';
private $port = 0;
private $protocol = '';
public $state = null;
public $history = [];
public const OK = 2;
public const ERROR = 1;
public const FAIL = 0;
public function __construct(string $address, int $port, string $protocol = '', bool $populate = true)
{
$this->address = $address;
$this->port = $port;
$this->protocol = $protocol;
if ($populate) {
$this->populate();
}
}
public function populate(): void
{
$this->history = DB::table('status_history')
->where('history_name', $this->address)
->where('history_port', $this->port)
->where('history_protocol', $this->protocol)
->orderBy('history_date', 'desc')
->get();
$this->state = isset($this->history[0]) ? intval($this->history[0]->history_state) : null;
}
public function check(): int
{
$this->state = static::FAIL;
$sock = checkdnsrr($this->address) ? fsockopen($this->address, $this->port, $errno, $errstr, 1) : false;
if ($sock !== false) {
fclose($sock);
$this->state = static::OK;
if ($this->protocol === 'http' || $this->protocol === 'https') {
$header = strstr(
Net::request("{$this->protocol}://{$this->address}:{$this->port}/", 'head', null, 1),
"\r\n",
true
);
if ($header !== false && strtolower(substr($header, 0, 4)) == 'http') {
list($protocol, $response, $text) = explode(' ', $header, 3);
if ($response >= 400 && $response < 500) {
$this->state = static::ERROR;
}
if ($response >= 500 && $response < 600) {
$this->state = static::FAIL;
}
}
}
}
return $this->state;
}
public function save()
{
DB::table('status_history')
->insert([
'history_name' => $this->address,
'history_port' => $this->port,
'history_protocol' => $this->protocol,
'history_state' => $this->state
]);
$this->history = array_merge(DB::table('status_history')
->where('history_name', $this->address)
->where('history_port', $this->port)
->where('history_protocol', $this->protocol)
->orderBy('history_date', 'desc')
->limit(1)
->get(), $this->history);
}
}