1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace SakuraVpsMaintenance; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
|
7
|
|
|
require_once(__DIR__ . '/vendor/autoload.php'); |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author bootjp |
11
|
|
|
*/ |
12
|
|
|
class Checker |
13
|
|
|
{ |
14
|
|
|
protected $client; |
15
|
|
|
|
16
|
|
|
protected static $ipAddress; |
17
|
|
|
|
18
|
|
|
protected $error = ''; |
19
|
|
|
|
20
|
|
|
public function __construct($iniFilePath = null) |
21
|
|
|
{ |
22
|
|
|
$this->client = new Client([ |
23
|
|
|
'defaults' => [ |
24
|
|
|
'headers' => [ |
25
|
|
|
'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' |
26
|
|
|
] |
27
|
|
|
] |
28
|
|
|
] |
29
|
|
|
); |
30
|
|
|
if (!is_null($iniFilePath)) { |
31
|
|
|
if (!file_exists($iniFilePath)) { |
32
|
|
|
throw new \InvalidArgumentException('ini file not found'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
self::$ipAddress = parse_ini_file($iniFilePath, true); |
36
|
|
|
} else { |
37
|
|
|
self::$ipAddress = parse_ini_file(__DIR__ . '/ipaddress.ini', true); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $url [optional] |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function fetch($url = 'http://support.sakura.ad.jp/mainte/mainteindex.php?service=vps') |
46
|
|
|
{ |
47
|
|
|
$existsIpAddress = []; |
48
|
|
|
|
49
|
|
|
try { |
50
|
|
|
preg_match_all('{/mainte/mainteentry.php\?id=(?<page>.+?)"}s', $this->client->get($url)->getBody()->getContents(), $matches); |
51
|
|
|
foreach ($matches['page'] as $pageId) { |
52
|
|
|
$pageUrl = "http://support.sakura.ad.jp/mainte/mainteentry.php?id={$pageId}"; |
53
|
|
|
if (count($result = $this->parse($this->client->get($pageUrl)->getBody()->getContents())) !== 0) { |
54
|
|
|
$existsIpAddress = $result; |
55
|
|
|
} |
56
|
|
|
usleep(500000); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
} catch (\Exception $e) { |
60
|
|
|
$this->error .= "{$e->getMessage()}\n"; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $existsIpAddress; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $contents |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
private function parse($contents) |
71
|
|
|
{ |
72
|
|
|
preg_match_all('{(?<ipaddress>[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)}s', $contents, $matches); |
73
|
|
|
$onPageIpAddress = $matches['ipaddress']; |
74
|
|
|
|
75
|
|
|
return array_reduce(array_keys(self::$ipAddress), function($carry, $checkType) use($onPageIpAddress) { |
76
|
|
|
$result = array_filter(self::$ipAddress[$checkType], function($identifier) use($onPageIpAddress, $checkType) { |
77
|
|
|
switch ($checkType) { |
78
|
|
|
case 'static': |
79
|
|
|
return in_array($identifier, $onPageIpAddress); |
80
|
|
|
|
81
|
|
|
case 'regexp': |
82
|
|
|
foreach ($onPageIpAddress as $ipAddress) { |
83
|
|
|
if (preg_match($identifier, $ipAddress) === 1) { |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return false; |
89
|
|
|
default: |
90
|
|
|
throw new \RuntimeException("invalid type {$checkType}"); |
91
|
|
|
} |
92
|
|
|
}); |
93
|
|
|
|
94
|
|
|
if (count($result) !== 0) { |
95
|
|
|
$carry[$checkType] = $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $carry; |
99
|
|
|
}, []); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getException() |
103
|
|
|
{ |
104
|
|
|
if ($this->error !== '') { |
105
|
|
|
throw new \RuntimeException($this->error); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.