This repository has been archived by the owner on Jun 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Checker.php
108 lines (89 loc) · 3.19 KB
/
Checker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace SakuraVpsMaintenance;
use GuzzleHttp\Client;
require_once(__DIR__ . '/vendor/autoload.php');
/**
* @author bootjp
*/
class Checker
{
protected $client;
protected static $ipAddress;
protected $error = '';
public function __construct($iniFilePath = null)
{
$this->client = new Client([
'defaults' => [
'headers' => [
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36'
]
]
]
);
if (!is_null($iniFilePath)) {
if (!file_exists($iniFilePath)) {
throw new \InvalidArgumentException('ini file not found');
}
self::$ipAddress = parse_ini_file($iniFilePath, true);
} else {
self::$ipAddress = parse_ini_file(__DIR__ . '/ipaddress.ini', true);
}
}
/**
* @param string $url [optional]
* @return array
*/
public function fetch($url = 'http://support.sakura.ad.jp/mainte/mainteindex.php?service=vps')
{
$existsIpAddress = [];
try {
preg_match_all('{/mainte/mainteentry.php\?id=(?<page>.+?)"}s', $this->client->get($url)->getBody()->getContents(), $matches);
foreach ($matches['page'] as $pageId) {
$pageUrl = "http://support.sakura.ad.jp/mainte/mainteentry.php?id={$pageId}";
if (count($result = $this->parse($this->client->get($pageUrl)->getBody()->getContents())) !== 0) {
$existsIpAddress = $result;
}
usleep(500000);
}
} catch (\Exception $e) {
$this->error .= "{$e->getMessage()}\n";
}
return $existsIpAddress;
}
/**
* @param string $contents
* @return array
*/
private function parse($contents)
{
preg_match_all('{(?<ipaddress>[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)}s', $contents, $matches);
$onPageIpAddress = $matches['ipaddress'];
return array_reduce(array_keys(self::$ipAddress), function($carry, $checkType) use($onPageIpAddress) {
$result = array_filter(self::$ipAddress[$checkType], function($identifier) use($onPageIpAddress, $checkType) {
switch ($checkType) {
case 'static':
return in_array($identifier, $onPageIpAddress);
case 'regexp':
foreach ($onPageIpAddress as $ipAddress) {
if (preg_match($identifier, $ipAddress) === 1) {
return true;
}
}
return false;
default:
throw new \RuntimeException("invalid type {$checkType}");
}
});
if (count($result) !== 0) {
$carry[$checkType] = $result;
}
return $carry;
}, []);
}
public function getException()
{
if ($this->error !== '') {
throw new \RuntimeException($this->error);
}
}
}