diff --git a/README.md b/README.md index 7101038..5fdd3d2 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,15 @@ Windows default: *false* *false* is the only effective option on Windows due to platform and library limitations within the supported versions. +### Proxies + +A HTTP proxy can be set if your environment can't connect out through PHP or the `curl` binrary natively: + +```php +$client = new \Raygun4php\RaygunClient("apiKey"); +$client->setProxy('http://someproxy:8080'); +``` + ### Debug mode The client offers a debug mode in which the HTTP response code can be returned after a POST attempt. This can be useful when adding Raygun to your site. This is accessed by passing in *true* as the third parameter in the client constructor: @@ -203,6 +212,7 @@ If, when running a PHP script from the command line on *nix operating systems, y ## Changelog +- 1.6.0: Added HTTP proxy support, support X-Forwarded-For, null server var guards - 1.5.3: Unify property casing (internal change) - 1.5.2: Prevent error when query_string isn't present in $_SERVER - 1.5.1: Guard against intermittent user id cookie being null; overload for disabling user tracking diff --git a/src/Raygun4php/RaygunClient.php b/src/Raygun4php/RaygunClient.php index 2ae42c6..1d3a4ac 100755 --- a/src/Raygun4php/RaygunClient.php +++ b/src/Raygun4php/RaygunClient.php @@ -22,6 +22,7 @@ class RaygunClient protected $useAsyncSending; protected $debugSending; protected $disableUserTracking; + protected $proxy; /** * @var Array Parameter names to filter out of logged form data. Case insensitive. @@ -296,12 +297,23 @@ private function post($data_to_send, $cert_path) $result = stream_context_set_option($context, 'ssl', 'allow_self_signed', true); } + if ($this->proxy) { + $result = stream_context_set_option($context, 'http', 'proxy', $this->proxy); + } + if ($this->useAsyncSending && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { - $cmd = "curl -X POST -H 'Content-Type: application/json' -H 'X-ApiKey: " . $this->apiKey . "'"; - $cmd .= " -d " . escapeshellarg($data_to_send) . " --cacert '" . realpath(__DIR__ . '/cacert.crt') - . "' 'https://api.raygun.io:443/entries' > /dev/null 2>&1 &"; - + $curlOpts = array( + "-X POST", + "-H 'Content-Type: application/json'", + "-H 'X-ApiKey: " . $this->apiKey . "'", + "-d " . escapeshellarg($data_to_send), + "--cacert '" . realpath(__DIR__ . '/cacert.crt') . "'" + ); + if ($this->proxy) { + $curlOpts[] = "--proxy '" . $this->proxy . "'"; + } + $cmd = "curl " . implode(' ', $curlOpts) . " 'https://api.raygun.io:443/entries' > /dev/null 2>&1 &"; exec($cmd, $output, $exit); return $exit; } @@ -432,6 +444,24 @@ function getFilterParams() { return $this->filterParams; } + /** + * Use a proxy for sending HTTP requests to Raygun. + * + * @param String $url URL including protocol and an optional port, e.g. http://myproxy:8080 + * @return Raygun4php\RaygunClient + */ + function setProxy($proxy) { + $this->proxy = $proxy; + return $this; + } + + /** + * @return String + */ + function getProxy() { + return $this->proxy; + } + public function __destruct() { if ($this->httpData) diff --git a/src/Raygun4php/RaygunClientMessage.php b/src/Raygun4php/RaygunClientMessage.php index 977e2e6..9b63e38 100644 --- a/src/Raygun4php/RaygunClientMessage.php +++ b/src/Raygun4php/RaygunClientMessage.php @@ -10,7 +10,7 @@ class RaygunClientMessage public function __construct() { $this->Name = "Raygun4php"; - $this->Version = "1.5.3"; + $this->Version = "1.6.0"; $this->ClientUrl = "https://github.com/MindscapeHQ/raygun4php"; } } diff --git a/src/Raygun4php/RaygunRequestMessage.php b/src/Raygun4php/RaygunRequestMessage.php index 7f3e61c..c2c30f7 100644 --- a/src/Raygun4php/RaygunRequestMessage.php +++ b/src/Raygun4php/RaygunRequestMessage.php @@ -16,10 +16,10 @@ class RaygunRequestMessage public function __construct() { if (php_sapi_name() !== 'cli') { - $this->HostName = $_SERVER['HTTP_HOST']; - $this->HttpMethod = $_SERVER['REQUEST_METHOD']; - $this->Url = $_SERVER['REQUEST_URI']; - $this->IpAddress = $_SERVER['REMOTE_ADDR']; + $this->HostName = (isset($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : null; + $this->HttpMethod = (isset($_SERVER['REQUEST_METHOD'])) ? $_SERVER['REQUEST_METHOD'] : null; + $this->Url = (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : null; + $this->IpAddress = $this->getRemoteAddr(); if (array_key_exists('QUERY_STRING', $_SERVER)) { @@ -99,5 +99,21 @@ private function emu_getAllHeaders() return getallheaders(); } } + + private function getRemoteAddr() + { + $ip = null; + + if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) + { + $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; + } + else if (!empty($_SERVER['REMOTE_ADDR'])) + { + $ip = $_SERVER['REMOTE_ADDR']; + } + + return $ip; + } } } \ No newline at end of file