-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
201 lines (163 loc) · 5.56 KB
/
index.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/*\
|*| MATIBOUX STATUS API
|*|
|*| The API that powers https://matistat.us/
|*|
|*| Author: Matiboux <matiboux@gmail.com>
|*| → Website: https://matiboux.com/
|*| → Github: https://github.com/matiboux/
|*|
|*| License: MIT License
|*| Copyright: 2018 Matiboux
|*|
|*| Open Source project published on Github:
|*| https://github.com/matiboux/matistat.us
\*/
/** Allow all requests */
header('Access-Control-Allow-Origin: *');
/** --- -- --- */
/** Cache Support? */
$cacheSupport = true;
/** Cache File Lifetime (seconds) */
$cacheLifetime = 20;
// Is there non-expired cache?
if($cacheSupport AND file_exists('cache.json') AND filemtime('cache.json') + $cacheLifetime > time()) {
$cache = file_get_contents('cache.json');
// Valid Cache? Okay, then return that
if(json_decode($cache) AND json_last_error() == JSON_ERROR_NONE) {
echo $cache;
exit;
}
}
/** The API base URL
Change if your website isn't hosted on the (sub)domain root directory.
Format: "matiboux.com/status/" (no protocol, and note the ending slash) */
$baseurl = null; // Like $baseurl = 'matiboux.com/';
// $baseurl = 'matiboux.com/status/';
/** Timeout for a request on service */
$timeout = 10;
/** List of supported services, and the url to ping */
$supportedServices = array(
'matiboux.com' => 'https://matiboux.com/',
'matiboux.me' => 'https://matiboux.me/',
'matistat.us' => 'https://matistat.us/',
'imgshot.eu' => 'https://imgshot.eu/',
'urwebs.it' => 'https://urwebs.it/',
'olifw.net' => 'https://olifw.net/',
'ljv.fr' => 'https://ljv.fr/'
);
/** Aliases, and Groups */
$aliases = array(
'domains' => [
'matiboux.com',
'matistat.us',
'imgshot.eu',
'urwebs.it',
'olifw.net',
'ljv.fr'
],
'apps' => [
]
);
/** --- -- --- */
/** Is there a script error? */
$success = false;
/** The timestamp after getting all services service */
$timestamp = null;
/** The services specifically requested
Values: "all" or list of services (array). */
$services = null;
/** Selected all Services? */
$allServices = null;
/** The resulted status for all services */
$statuses = [];
/** The JSON response of the API */
$response = array(
'success' => &$success, // Might change if an error occurs.
'timestamp' => &$timestamp, // Might change if an error occurs.
'services' => &$services, // $services value
'statuses' => &$statuses // All services status
);
$responseJSON = null;
/** The raw URL parameter for specific services request */
$urlParam = null;
// If $baseurl isn't empty, it must match and start with HTTP_HOST.
if(!empty($baseurl) AND strpos($baseurl, $_SERVER['HTTP_HOST']) === 0) {
// Fetch the parameter from the URL.
$urlParam = substr($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], strlen($baseurl));
// If an error occurred, unset $urlParam.
// (most likely $baseurl didn't match the actual URL)
if($urlParam === false) $urlParam = null;
}
// If $urlParam is null, rely on the (sub)domain root directory and remove the starting "/".
if($urlParam === null) $urlParam = substr($_SERVER['REQUEST_URI'], 1) ?: null;
if(empty($urlParam) OR $urlParam == 'all') {
$services = array_keys($supportedServices);
$allServices = true;
} else {
// Set the requested services.
$services = json_decode($urlParam, true) ?: $urlParam;
$allServices = false;
// Convert into an array, if not already.
if(!is_array($services)) {
if(!empty($aliases[$services])) $services = $aliases[$services];
else $services = [$services];
}
}
/** cURL Request Options */
$options = array(
CURLOPT_FRESH_CONNECT => true, // New connection, no cache.
CURLOPT_FOLLOWLOCATION => true, // Follow Redirects
CURLOPT_USERAGENT => 'MatibouxStatus', // Agent Identity
CURLOPT_CONNECTTIMEOUT => $timeout, // Connection Timeout
CURLOPT_TIMEOUT => $timeout, // Response Timeout
CURLOPT_MAXREDIRS => 5, // Stop after 5 redirection
CURLOPT_RETURNTRANSFER => true, // Return Request Content
CURLOPT_NOBODY => true, // Ignore Page Content
CURLOPT_SSL_VERIFYPEER => false, // Disable SSL certification checks
);
// Fetch services to test, and their data
$listServices = array_intersect_key($supportedServices, array_flip($services));
if(!empty($listServices)) {
$success = true;
foreach($listServices as $name => $url) {
// Give PHP some more time to execute
set_time_limit($timeout <= 10 ? $timeout * 2 : $timeout + 10);
// Create the resource and Set options
$resource = curl_init($url);
curl_setopt_array($resource, $options);
$content = curl_exec($resource); // Request Returned Content
$errno = curl_errno($resource); // Request Error Code
// $errmsg = curl_error($resource); // Request Error Message
$header = curl_getinfo($resource); // Request Returned Headers
$statuses[$name] = array(
'success' => $errno === 0,
'url' => $header['url'],
'ip' => $header['primary_ip'],
'port' => $header['primary_port'],
'content' => $content,
'up' => $header['http_code'] >= 200 AND $header['http_code'] < 300,
'http_code' => $header['http_code'],
'time' => array(
'total' => $header['total_time'],
'namelookup' => $header['namelookup_time'],
'connect' => $header['connect_time'],
'pretransfer' => $header['pretransfer_time'],
'starttransfer' => $header['starttransfer_time'],
'redirect' => $header['redirect_time']
)
);
// Close the resource
curl_close($resource);
}
$timestamp = time();
$responseJSON = json_encode($response);
$handle = fopen('cache.json', 'w');
fwrite($handle, $responseJSON);
fclose($handle);
} else $success = false;
/** Return API Response */
echo $responseJSON;
exit;
?>