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

115 lines
3.3 KiB
PHP
Raw Permalink Normal View History

2016-12-09 22:28:25 +00:00
<?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;
2016-12-10 22:36:24 +00:00
public function __construct(string $address, int $port, string $protocol = '', bool $populate = true, bool $history = true)
2016-12-09 22:28:25 +00:00
{
$this->address = $address;
$this->port = $port;
$this->protocol = $protocol;
if ($populate) {
2016-12-10 22:36:24 +00:00
$this->populate($history);
2016-12-09 22:28:25 +00:00
}
}
2016-12-10 22:36:24 +00:00
public function populate(bool $history = true): void
2016-12-09 22:28:25 +00:00
{
$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')
2016-12-10 22:36:24 +00:00
->limit($history ? 10 : 1)
2016-12-09 22:28:25 +00:00
->get();
$this->state = isset($this->history[0]) ? intval($this->history[0]->history_state) : null;
}
public function check(): int
{
$this->state = static::FAIL;
2016-12-10 22:36:24 +00:00
2016-12-11 14:49:03 +00:00
try {
$sock = Net::detectIPVersion(
$this->address[0] === '[' && $this->address[strlen($this->address) - 1] === ']'
? substr($this->address, 1, -1)
: $this->address
) !== 0 || dns_get_record($this->address, DNS_A | DNS_A6 | DNS_MX)
? fsockopen($this->address, $this->port, $errno, $errstr, 1)
: false;
} catch (\ErrorException $e) {
$sock = false;
}
2016-12-09 22:28:25 +00:00
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') {
2016-12-10 22:36:24 +00:00
[$protocol, $response, $text] = explode(' ', $header, 3);
2016-12-09 22:28:25 +00:00
if ($response >= 400 && $response < 500) {
$this->state = static::ERROR;
}
if ($response >= 500 && $response < 600) {
$this->state = static::FAIL;
}
}
}
}
return $this->state;
}
2016-12-10 19:43:17 +00:00
public function save(): void
2016-12-09 22:28:25 +00:00
{
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);
}
}