-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitecheck.php
85 lines (74 loc) · 2.16 KB
/
sitecheck.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
<?php
class sitecheck{
var $domain = ''; // 'http://www.carbonsilk.com'
var $expected_title = ''; // 'Carbon Silk'
var $http_response_code = null;
var $http_response_title = null;
function sitecheck($domain, $expected_title = null){
if(isset($domain)){
$this->domain = $domain;
}
if(isset($expected_title)){
$this->expected_title = $expected_title;
}
$this->get_domain_response();
}
function get_domain_response(){
$ch = curl_init($this->domain);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$this->http_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->http_response_title = self::get_title_text($output);
curl_close($ch);
// If we got a response we are done here
if($output){
return true;
} else {
return false;
}
}
function get_title_text($html){
preg_match('/<title>(.*)<\/title>/Us', $html, $matches);
$website_title = $matches[1];
return $website_title;
}
function validate_title_text(){
if(strstr($this->http_response_title, $this->expected_title)){
return true;
} else {
return false;
}
}
function send_notification($message){
if(is_string($this->notify_email) && is_string($message)){
$Name = "Siteup Mailer"; //senders name
$email = "noreply@" . $_SERVER['SERVER_NAME']; //senders e-mail adress
$recipient = $this->notify_email; //recipient
$mail_body = $message; //mail body
$subject = "Siteup Status Update"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
mail($recipient, $subject, $mail_body, $header); //mail command :)
}
}
function status(){
if(200 == $this->http_response_code && $this->validate_title_text()){
return true;
} else {
return false;
}
}
function status_response(){
if($this->status()){
$response = $this->domain . ' is up';
} else {
if(200 == $this->http_response_code){
$response = $this->domain . " can be reached but the expected title '{$this->expected_title}' does not match";
} else {
$response = $this->domain . ' is down';
}
$this->send_notification($response);
}
return $response;
}
}