View source
<?php
define('ACQUIA_HTTP_REQUEST_TIMEOUT', -1);
function _acquia_agent_xmlrpc() {
require_once './includes/xmlrpc.inc';
$args = func_get_args();
$context = array_shift($args);
$url = array_shift($args);
if (is_array($args[0])) {
$method = 'system.multicall';
$multicall_args = array();
foreach ($args[0] as $call) {
$multicall_args[] = array(
'methodName' => array_shift($call),
'params' => $call,
);
}
$args = array(
$multicall_args,
);
}
else {
$method = array_shift($args);
}
$xmlrpc_request = xmlrpc_request($method, $args);
$result = acquia_agent_http_request($context, $url, array(
"Content-Type" => "text/xml",
), 'POST', $xmlrpc_request->xml);
if ($result->code != 200) {
xmlrpc_error($result->code, $result->error);
return FALSE;
}
$message = xmlrpc_message($result->data);
if (!xmlrpc_message_parse($message)) {
xmlrpc_error(-32700, t('Parse error. Not well formed'));
return FALSE;
}
if ($message->messagetype == 'fault') {
xmlrpc_error($message->fault_code, $message->fault_string);
return FALSE;
}
return $message->params[0];
}
function acquia_agent_stream_context_create($url, $module = 'acquia_agent') {
$opts = array();
$uri = parse_url($url);
if (isset($uri['scheme']) && $uri['scheme'] == 'https' && variable_get('acquia_agent_verify_peer', 0)) {
$pem_file = drupal_get_path('module', $module) . '/' . $uri['host'] . '.pem';
if (file_exists($pem_file)) {
$opts['ssl'] = array(
'verify_peer' => TRUE,
'cafile' => $pem_file,
'allow_self_signed' => FALSE,
'CN_match' => $uri['host'],
);
}
}
return stream_context_create($opts);
}
function acquia_agent_http_request($context, $url, $headers = array(), $method = 'GET', $data = NULL, $retry = 3, $timeout = 30.0, $connect_timeout = 1.0) {
$result = new stdClass();
$uri = parse_url($url);
timer_start(__FUNCTION__);
switch ($uri['scheme']) {
case 'http':
$port = isset($uri['port']) ? $uri['port'] : 80;
$host = $uri['host'] . ($port != 80 ? ':' . $port : '');
$fp = @fsockopen($uri['host'], $port, $errno, $errstr, $connect_timeout);
break;
case 'https':
$port = isset($uri['port']) ? $uri['port'] : 443;
$host = $uri['host'] . ($port != 443 ? ':' . $port : '');
if (!isset($context)) {
$fp = @fsockopen('ssl://' . $uri['host'], $port, $errno, $errstr, $connect_timeout);
}
else {
$fp = @stream_socket_client('ssl://' . $uri['host'] . ':' . $port, $errno, $errstr, $connect_timeout, STREAM_CLIENT_CONNECT, $context);
if (!$fp && $errno == 0) {
$errno = 999;
$errstr = t('SSL error creating socket');
}
}
break;
default:
$result->error = 'invalid schema ' . $uri['scheme'];
return $result;
}
if (!$fp) {
$result->code = -$errno;
$result->error = trim($errstr);
return $result;
}
$path = isset($uri['path']) ? $uri['path'] : '/';
if (isset($uri['query'])) {
$path .= '?' . $uri['query'];
}
$defaults = array(
'Host' => "Host: {$host}",
'User-Agent' => 'User-Agent: Drupal (+http://drupal.org/)',
);
if (!empty($data) || $method == 'POST' || $method == 'PUT') {
$defaults['Content-Length'] = 'Content-Length: ' . strlen($data);
}
if (isset($uri['user'])) {
$defaults['Authorization'] = 'Authorization: Basic ' . base64_encode($uri['user'] . (!empty($uri['pass']) ? ":" . $uri['pass'] : ''));
}
foreach ($headers as $header => $value) {
$defaults[$header] = $header . ': ' . $value;
}
$request = $method . ' ' . $path . " HTTP/1.0\r\n";
$request .= implode("\r\n", $defaults);
$request .= "\r\n\r\n";
if ($data) {
$request .= $data . "\r\n";
}
$result->request = $request;
$time_left = $timeout - timer_read(__FUNCTION__) / 1000;
if ($time_left > 0) {
stream_set_timeout($fp, floor($time_left), floor(1000000 * fmod($time_left, 1)));
fwrite($fp, $request);
}
$response = '';
while (!feof($fp)) {
$time_left = $timeout - timer_read(__FUNCTION__) / 1000;
if ($time_left <= 0) {
$result->code = ACQUIA_HTTP_REQUEST_TIMEOUT;
$result->error = 'request timed out';
return $result;
}
stream_set_timeout($fp, floor($time_left), floor(1000000 * fmod($time_left, 1)));
$chunk = fread($fp, 1024);
$response .= $chunk;
}
fclose($fp);
list($split, $result->data) = explode("\r\n\r\n", $response, 2);
$split = preg_split("/\r\n|\n|\r/", $split);
list($protocol, $code, $text) = explode(' ', trim(array_shift($split)), 3);
$result->headers = array();
while ($line = trim(array_shift($split))) {
list($header, $value) = explode(':', $line, 2);
if (isset($result->headers[$header]) && $header == 'Set-Cookie') {
$result->headers[$header] .= ',' . trim($value);
}
else {
$result->headers[$header] = trim($value);
}
}
$responses = array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Time-out',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Large',
415 => 'Unsupported Media Type',
416 => 'Requested range not satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Time-out',
505 => 'HTTP Version not supported',
);
if (!isset($responses[$code])) {
$code = floor($code / 100) * 100;
}
switch ($code) {
case 200:
case 304:
break;
case 301:
case 302:
case 307:
$location = $result->headers['Location'];
$timeout -= timer_read(__FUNCTION__) / 1000;
if ($timeout <= 0) {
$result->code = ACQUIA_HTTP_REQUEST_TIMEOUT;
$result->error = 'request timed out';
}
elseif ($retry) {
$result = acquia_agent_http_request($context, $result->headers['Location'], $headers, $method, $data, --$retry, $timeout, $connect_timeout);
$result->redirect_code = $result->code;
}
$result->redirect_url = $location;
break;
default:
$result->error = $text;
}
$result->code = $code;
return $result;
}
function acquia_agent_valid_response($data) {
$authenticator = $data['authenticator'];
$result = $data['result'];
$result_auth = $result['authenticator'];
$valid = $authenticator['nonce'] == $result_auth['nonce'];
$valid = $valid && $authenticator['time'] < $result_auth['time'];
$key = acquia_agent_settings('acquia_key');
$hash = _acquia_agent_hmac($key, $result_auth['time'], $result_auth['nonce'], $result['body']);
return $valid && $hash == $result_auth['hash'];
}
function _acquia_agent_request($url, $method, $data) {
$ctx = acquia_agent_stream_context_create($url);
if (!$ctx) {
xmlrpc_error(-1, t('SSL is not supported or setup failed'));
$result = FALSE;
}
else {
$result = _acquia_agent_xmlrpc($ctx, $url, $method, $data);
}
if ($errno = xmlrpc_errno()) {
watchdog('acquia agent', '@message (@errno): %server - %method - <pre>@data</pre>', array(
'@message' => xmlrpc_error_msg(),
'@errno' => xmlrpc_errno(),
'%server' => $url,
'%method' => $method,
'@data' => print_r($data, TRUE),
), WATCHDOG_ERROR);
}
return $result;
}
function _acquia_agent_authenticator($params = array(), $identifier = NULL, $key = NULL) {
if (empty($identifier)) {
$identifier = acquia_agent_settings('acquia_identifier');
}
if (empty($key)) {
$key = acquia_agent_settings('acquia_key');
}
$time = time();
$nonce = md5(acquia_agent_random_bytes(55));
$authenticator['identifier'] = $identifier;
$authenticator['time'] = $time;
$authenticator['hash'] = _acquia_agent_hmac($key, $time, $nonce, $params);
$authenticator['nonce'] = $nonce;
return $authenticator;
}
function _acquia_agent_hmac($key, $time, $nonce, $params) {
return base64_encode(pack("H*", sha1((str_pad($key, 64, chr(0x0)) ^ str_repeat(chr(0x5c), 64)) . pack("H*", sha1((str_pad($key, 64, chr(0x0)) ^ str_repeat(chr(0x36), 64)) . $time . ':' . $nonce . ':' . $key . ':' . serialize($params))))));
}