2012-01-20 18:10:48 +01:00
|
|
|
<?php
|
2015-09-10 23:13:48 +02:00
|
|
|
|
2012-01-20 18:10:48 +01:00
|
|
|
/**
|
|
|
|
* Check that a given URL is functioning, by default, the homepage.
|
|
|
|
*
|
|
|
|
* Note that Director::test() will be used rather than a CURL check.
|
|
|
|
*/
|
|
|
|
class URLCheck implements EnvironmentCheck {
|
2015-09-10 23:13:48 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-01-20 18:10:48 +01:00
|
|
|
protected $url;
|
2015-09-10 23:13:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-01-20 18:10:48 +01:00
|
|
|
protected $testString;
|
|
|
|
|
|
|
|
/*
|
2015-09-10 23:13:48 +02:00
|
|
|
* @param string $url The URL to check, relative to the site (homepage is '').
|
|
|
|
* @param string $testString An optional piece of text to search for on the homepage.
|
2012-01-20 18:10:48 +01:00
|
|
|
*/
|
2015-09-10 23:13:48 +02:00
|
|
|
function __construct($url = '', $testString = '') {
|
2012-01-20 18:10:48 +01:00
|
|
|
$this->url = $url;
|
|
|
|
$this->testString = $testString;
|
|
|
|
}
|
2015-09-10 23:13:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws SS_HTTPResponse_Exception
|
|
|
|
*/
|
2012-01-20 18:10:48 +01:00
|
|
|
function check() {
|
|
|
|
$response = Director::test($this->url);
|
|
|
|
|
|
|
|
if($response->getStatusCode() != 200) {
|
|
|
|
return array(
|
|
|
|
EnvironmentCheck::ERROR,
|
|
|
|
sprintf('Error retrieving "%s" (Code: %d)', $this->url, $response->getStatusCode())
|
|
|
|
);
|
|
|
|
|
|
|
|
} else if($this->testString && (strpos($response->getBody(), $this->testString) === false)) {
|
|
|
|
return array(
|
|
|
|
EnvironmentCheck::WARNING,
|
2012-12-17 05:16:10 +01:00
|
|
|
sprintf('Success retrieving "%s", but string "%s" not found', $this->url, $this->testString)
|
2012-01-20 18:10:48 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return array(
|
|
|
|
EnvironmentCheck::OK,
|
|
|
|
sprintf('Success retrieving "%s"', $this->url)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2015-09-10 23:13:48 +02:00
|
|
|
}
|