function sign_current_request($override = array())
{
if (isset($override['scheme'])) {
$scheme = $override['scheme'];
if (!in_array($scheme, array('http', 'https'))) {
return new Jetpack_Error('invalid_sheme', 'Invalid URL scheme');
}
} else {
if (is_ssl()) {
$scheme = 'https';
} else {
$scheme = 'http';
}
}
$host_port = isset($_SERVER['HTTP_X_FORWARDED_PORT']) ? $_SERVER['HTTP_X_FORWARDED_PORT'] : $_SERVER['SERVER_PORT'];
if (is_ssl()) {
// 443: Standard Port
// 80: Assume we're behind a proxy without X-Forwarded-Port. Hardcoding "80" here means most sites
// with SSL termination proxies (self-served, Cloudflare, etc.) don't need to fiddle with
// the JETPACK_SIGNATURE__HTTPS_PORT constant. The code also implies we can't talk to a
// site at https://example.com:80/ (which would be a strange configuration).
// JETPACK_SIGNATURE__HTTPS_PORT: Set this constant in wp-config.php to the back end webserver's port
// if the site is behind a proxy running on port 443 without
// X-Forwarded-Port and the back end's port is *not* 80. It's better,
// though, to configure the proxy to send X-Forwarded-Port.
$port = in_array($host_port, array(443, 80, JETPACK_SIGNATURE__HTTPS_PORT)) ? '' : $host_port;
} else {
// 80: Standard Port
// JETPACK_SIGNATURE__HTTPS_PORT: Set this constant in wp-config.php to the back end webserver's port
// if the site is behind a proxy running on port 80 without
// X-Forwarded-Port. It's better, though, to configure the proxy to
// send X-Forwarded-Port.
$port = in_array($host_port, array(80, JETPACK_SIGNATURE__HTTP_PORT)) ? '' : $host_port;
}
$url = "{$scheme}://{$_SERVER['HTTP_HOST']}:{$port}" . stripslashes($_SERVER['REQUEST_URI']);
if (array_key_exists('body', $override) && !is_null($override['body'])) {
$body = $override['body'];
} else {
if ('POST' == strtoupper($_SERVER['REQUEST_METHOD'])) {
$body = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : null;
// Convert the $_POST to the body, if the body was empty. This is how arrays are hashed
// and encoded on the Jetpack side.
if (defined('IS_WPCOM') && IS_WPCOM) {
if (empty($body) && is_array($_POST) && count($_POST) > 0) {
$body = $_POST;
}
}
} else {
$body = null;
}
}
$a = array();
foreach (array('token', 'timestamp', 'nonce', 'body-hash') as $parameter) {
if (isset($override[$parameter])) {
$a[$parameter] = $override[$parameter];
} else {
$a[$parameter] = isset($_GET[$parameter]) ? stripslashes($_GET[$parameter]) : '';
}
}
$method = isset($override['method']) ? $override['method'] : $_SERVER['REQUEST_METHOD'];
return $this->sign_request($a['token'], $a['timestamp'], $a['nonce'], $a['body-hash'], $method, $url, $body, true);
}