silverstripe-environmentcheck/code/EnvironmentChecker.php

275 lines
6.9 KiB
PHP
Raw Normal View History

<?php
/**
* Provides an interface for checking the given EnvironmentCheckSuite.
*/
class EnvironmentChecker extends RequestHandler {
2015-09-10 23:13:48 +02:00
/**
* @var array
*/
private static $url_handlers = array(
'' => 'index',
);
2015-09-10 23:13:48 +02:00
/**
* @var string
*/
protected $checkSuiteName;
2015-09-10 23:13:48 +02:00
/**
* @var string
*/
protected $title;
2015-09-10 23:13:48 +02:00
/**
* @var int
*/
protected $errorCode = 500;
2015-09-10 23:13:48 +02:00
/**
* @var null|string
*/
2015-11-20 00:07:07 +01:00
private static $to_email_address = null;
2015-09-10 23:13:48 +02:00
/**
* @var null|string
*/
2015-11-20 00:07:07 +01:00
private static $from_email_address = null;
2015-09-10 23:13:48 +02:00
/**
* @var bool
*/
2015-11-20 00:07:07 +01:00
private static $email_results = false;
2015-09-10 23:13:48 +02:00
/**
* @var bool Log results via {@link SS_Log}
*/
private static $log_results_warning = false;
/**
* @var int Maps to {@link Zend_Log} levels. Defaults to Zend_Log::WARN
*/
private static $log_results_warning_level = 4;
/**
* @var bool Log results via {@link SS_Log}
*/
private static $log_results_error = false;
/**
* @var int Maps to {@link Zend_Log} levels. Defaults to Zend_Log::ALERT
*/
private static $log_results_error_level = 1;
2015-09-10 23:13:48 +02:00
/**
* @param string $checkSuiteName
* @param string $title
*/
function __construct($checkSuiteName, $title) {
parent::__construct();
$this->checkSuiteName = $checkSuiteName;
$this->title = $title;
}
2015-09-10 23:13:48 +02:00
/**
* @param string $permission
*
* @throws SS_HTTPResponse_Exception
*/
2013-06-21 05:36:45 +02:00
function init($permission = 'ADMIN') {
// if the environment supports it, provide a basic auth challenge and see if it matches configured credentials
if(defined('ENVCHECK_BASICAUTH_USERNAME') && defined('ENVCHECK_BASICAUTH_PASSWORD')) {
if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
// authenticate the input user/pass with the configured credentials
if(
!(
$_SERVER['PHP_AUTH_USER'] == ENVCHECK_BASICAUTH_USERNAME
&& $_SERVER['PHP_AUTH_PW'] == ENVCHECK_BASICAUTH_PASSWORD
)
) {
$response = new SS_HTTPResponse(null, 401);
$response->addHeader('WWW-Authenticate', "Basic realm=\"Environment check\"");
// Exception is caught by RequestHandler->handleRequest() and will halt further execution
$e = new SS_HTTPResponse_Exception(null, 401);
$e->setResponse($response);
throw $e;
}
} else {
$response = new SS_HTTPResponse(null, 401);
$response->addHeader('WWW-Authenticate', "Basic realm=\"Environment check\"");
// Exception is caught by RequestHandler->handleRequest() and will halt further execution
$e = new SS_HTTPResponse_Exception(null, 401);
$e->setResponse($response);
throw $e;
}
} else {
if(!$this->canAccess(null, $permission)) return $this->httpError(403);
}
}
2015-09-10 23:13:48 +02:00
/**
* @param null|int|Member $member
* @param string $permission
*
* @return bool
*
* @throws SS_HTTPResponse_Exception
*/
2013-06-21 05:36:45 +02:00
function canAccess($member = null, $permission = "ADMIN") {
if(!$member) {
$member = Member::currentUser();
}
if(!$member) {
$member = BasicAuth::requireLogin('Environment Checker', $permission, false);
}
// We allow access to this controller regardless of live-status or ADMIN permission only
// if on CLI. Access to this controller is always allowed in "dev-mode", or of the user is ADMIN.
if(
Director::isDev()
2013-06-21 05:36:45 +02:00
|| Director::is_cli()
|| empty($permission)
|| Permission::checkMember($member, $permission)
) {
return true;
}
// Extended access checks.
// "Veto" style, return NULL to abstain vote.
$canExtended = null;
$results = $this->extend('canAccess', $member);
if($results && is_array($results)) {
if(!min($results)) return false;
else return true;
}
return false;
}
2015-09-10 23:13:48 +02:00
/**
* @return SS_HTTPResponse
*/
function index() {
$response = new SS_HTTPResponse;
$result = EnvironmentCheckSuite::inst($this->checkSuiteName)->run();
if(!$result->ShouldPass()) {
$response->setStatusCode($this->errorCode);
}
$resultText = $result->customise(array(
2013-06-21 05:36:45 +02:00
"URL" => Director::absoluteBaseURL(),
"Title" => $this->title,
"Name" => $this->checkSuiteName,
"ErrorCode" => $this->errorCode,
))->renderWith("EnvironmentChecker");
2015-11-20 00:07:07 +01:00
if ($this->config()->email_results && !$result->ShouldPass()) {
$email = new Email($this->config()->from_email_address, $this->config()->to_email_address, $this->title, $resultText);
$email->send();
}
// Optionally log errors and warnings individually
foreach($result->Details() as $detail) {
if($this->config()->log_results_warning && $detail->StatusCode == EnvironmentCheck::WARNING) {
$this->log(
sprintf('EnvironmentChecker warning at "%s" check. Message: %s', $detail->Check, $detail->Message),
$this->config()->log_results_warning_level
);
} elseif($this->config()->log_results_error && $detail->StatusCode == EnvironmentCheck::ERROR) {
$this->log(
sprintf('EnvironmentChecker error at "%s" check. Message: %s', $detail->Check, $detail->Message),
$this->config()->log_results_error_level
);
}
}
// output the result as JSON if requested
if(
$this->getRequest()->getExtension() == 'json'
|| strpos($this->getRequest()->getHeader('Accept'), 'application/json') !== false
) {
$response->setBody($result->toJSON());
$response->addHeader('Content-Type', 'application/json');
return $response;
}
$response->setBody($resultText);
return $response;
}
/**
* @param string $message
* @param int $level
*/
public function log($message, $level) {
SS_Log::log($message, $level);
}
/**
* Set the HTTP status code that should be returned when there's an error.
2015-09-10 23:13:48 +02:00
*
* @param int $errorCode
*/
function setErrorCode($errorCode) {
$this->errorCode = $errorCode;
}
2015-09-10 23:13:48 +02:00
/**
2015-11-20 00:07:07 +01:00
* @deprecated
2015-09-10 23:13:48 +02:00
* @param string $from
*/
public static function set_from_email_address($from) {
2015-11-20 00:07:07 +01:00
Deprecation::notice('2.0', 'Use config API instead');
Config::inst()->update('EnvironmentChecker', 'from_email_address', $from);
}
2015-09-10 23:13:48 +02:00
/**
2015-11-20 00:07:07 +01:00
* @deprecated
2015-09-10 23:13:48 +02:00
* @return null|string
*/
public static function get_from_email_address() {
2015-11-20 00:07:07 +01:00
Deprecation::notice('2.0', 'Use config API instead');
return Config::inst()->get('EnvironmentChecker', 'from_email_address');
}
2015-09-10 23:13:48 +02:00
/**
2015-11-20 00:07:07 +01:00
* @deprecated
2015-09-10 23:13:48 +02:00
* @param string $to
*/
public static function set_to_email_address($to) {
2015-11-20 00:07:07 +01:00
Deprecation::notice('2.0', 'Use config API instead');
Config::inst()->update('EnvironmentChecker', 'to_email_address', $to);
}
2015-09-10 23:13:48 +02:00
/**
2015-11-20 00:07:07 +01:00
* @deprecated
2015-09-10 23:13:48 +02:00
* @return null|string
*/
public static function get_to_email_address() {
2015-11-20 00:07:07 +01:00
Deprecation::notice('2.0', 'Use config API instead');
return Config::inst()->get('EnvironmentChecker', 'to_email_address');
}
2015-09-10 23:13:48 +02:00
/**
2015-11-20 00:07:07 +01:00
* @deprecated
2015-09-10 23:13:48 +02:00
* @param bool $results
*/
public static function set_email_results($results) {
2015-11-20 00:07:07 +01:00
Deprecation::notice('2.0', 'Use config API instead');
Config::inst()->update('EnvironmentChecker', 'email_results', $results);
}
2015-09-10 23:13:48 +02:00
/**
2015-11-20 00:07:07 +01:00
* @deprecated
2015-09-10 23:13:48 +02:00
* @return bool
*/
public static function get_email_results() {
2015-11-20 00:07:07 +01:00
Deprecation::notice('2.0', 'Use config API instead');
return Config::inst()->get('EnvironmentChecker', 'email_results');
}
}