Skip to content

Commit

Permalink
Merge pull request MindscapeHQ#99 from MindscapeHQ/only-output-with-d…
Browse files Browse the repository at this point in the history
…ebug-flags

Raygun4PHP: Remove echo when socket connection fails
  • Loading branch information
BenjaminHarding committed Feb 25, 2018
2 parents 6d28cbb + 33b57ef commit ce33cc6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ function error_handler($errno, $errstr, $errfile, $errline ) {
See the [Error Control Operators section on PHP.net](http:https://php.net/manual/en/language.operators.errorcontrol.php) for more information

## Changelog

- 1.8.1: Fix issue with error being raised with null bytes send with escapeshellarg method
- 1.8.2: No longer output warning when a socket connection fails
- 1.8.1: Fix issue with error being raised with null bytes send with escapeshellarg method
- 1.8.0: Bugfix with multiple cookies being set. Cookie options can be set via the setCookieOptions method
- 1.7.1: Fixed illegal string offset
- 1.7.0: Added custom error grouping
Expand Down
58 changes: 31 additions & 27 deletions src/Raygun4php/RaygunClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RaygunClient
protected $uuid;
protected $httpData;
protected $useAsyncSending;
protected $debugSending;
protected $debug;
protected $disableUserTracking;
protected $proxy;

Expand Down Expand Up @@ -52,14 +52,14 @@ class RaygunClient
* @param bool $useAsyncSending If true, attempts to post rapidly and asynchronously the script by forking a cURL process.
* RaygunClient cannot return the HTTP result when in async mode, however. If false, sends using a blocking socket connection.
* This is the only method available on Windows.
* @param bool $debugSending If true, and $useAsyncSending is true, this will output the HTTP response code from posting
* @param bool $debug If true, and $useAsyncSending is true, this will output the HTTP response code from posting. Will also emit errors if the socket connection fails to send through errors.
* error messages. See the GitHub documentation for code meaning. This param does nothing if useAsyncSending is set to true.
*/
public function __construct($key, $useAsyncSending = true, $debugSending = false, $disableUserTracking = false)
public function __construct($key, $useAsyncSending = true, $debug = false, $disableUserTracking = false)
{
$this->apiKey = $key;
$this->useAsyncSending = $useAsyncSending;
$this->debugSending = $debugSending;
$this->debug = $debug;

if (!$disableUserTracking) {
$this->SetUser();
Expand Down Expand Up @@ -333,25 +333,6 @@ public function Send($message)

private function post($data_to_send, $cert_path)
{
$headers = 0;
$remote = $this->transport . ':https://' . $this->host . ':' . $this->port;

$context = stream_context_create();
$result = stream_context_set_option($context, 'ssl', 'verify_host', true);

if (!empty($cert_path))
{
$result = stream_context_set_option($context, 'ssl', 'cafile', $cert_path);
$result = stream_context_set_option($context, 'ssl', 'verify_peer', true);
}
else
{
$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')
{
Expand All @@ -366,11 +347,31 @@ private function post($data_to_send, $cert_path)
$curlOpts[] = "--proxy '" . $this->proxy . "'";
}
$cmd = "curl " . implode(' ', $curlOpts) . " 'https://api.raygun.io:443/entries' > /dev/null 2>&1 &";
$output = array();
$exit;
exec($cmd, $output, $exit);
return $exit;
}
else
{
$remote = $this->transport . ':https://' . $this->host . ':' . $this->port;
$context = stream_context_create();
$result = stream_context_set_option($context, 'ssl', 'verify_host', true);

if (!empty($cert_path))
{
$result = stream_context_set_option($context, 'ssl', 'cafile', $cert_path);
$result = stream_context_set_option($context, 'ssl', 'verify_peer', true);
}
else
{
$result = stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
}

if ($this->proxy) {
$result = stream_context_set_option($context, 'http', 'proxy', $this->proxy);
}

$fp = stream_socket_client($remote, $err, $errstr, 10, STREAM_CLIENT_CONNECT, $context);

if ($fp)
Expand All @@ -386,7 +387,7 @@ private function post($data_to_send, $cert_path)
fwrite($fp, $data_to_send);

$response = "";
if ($this->debugSending)
if ($this->debug)
{
while(!preg_match("/^HTTP\/[\d\.]* (\d{3})/", $response))
{
Expand All @@ -406,9 +407,12 @@ private function post($data_to_send, $cert_path)
}
else
{
$errMsg = "<br/><br/>" . "<strong>Raygun Warning:</strong> Couldn't send asynchronously. ";
$errMsg .= "Try calling new RaygunClient('apikey', FALSE); to use an alternate sending method, or RaygunClient('key', FALSE, TRUE) to echo the HTTP response" . "<br/><br/>";
echo $errMsg;
if($this->debug) {
$errMsg = "<br/><br/>" . "<strong>Raygun Warning:</strong> Couldn't send error. ";
$errMsg .= "Error number: " . $errno . "<br/><br/>";
$errMsg .= "Error string: " . $errstr . "<br/><br/>";
echo $errMsg;
}
trigger_error('httpPost error: ' . $errstr);
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Raygun4php/RaygunClientMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class RaygunClientMessage
public function __construct()
{
$this->Name = "Raygun4php";
$this->Version = "1.8.1";
$this->Version = "1.8.2";
$this->ClientUrl = "https://github.com/MindscapeHQ/raygun4php";
}
}
Expand Down

0 comments on commit ce33cc6

Please sign in to comment.