forked from MindscapeHQ/raygun4php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RaygunClient.php
executable file
·484 lines (421 loc) · 16.1 KB
/
RaygunClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<?php
namespace Raygun4php {
require_once realpath(__DIR__ . '/RaygunMessage.php');
require_once realpath(__DIR__ . '/RaygunIdentifier.php');
require_once realpath(__DIR__ . '/Raygun4PhpException.php');
require_once realpath(__DIR__ . '/Uuid.php');
use Raygun4Php\Rhumsaa\Uuid\Uuid;
class RaygunClient
{
protected $apiKey;
protected $version;
protected $tags;
protected $user;
protected $firstName;
protected $fullName;
protected $email;
protected $isAnonymous;
protected $uuid;
protected $httpData;
protected $useAsyncSending;
protected $debugSending;
protected $disableUserTracking;
protected $proxy;
/**
* @var array Parameter names to filter out of logged form data. Case insensitive.
* Accepts regular expressions when the name starts with a forward slash.
* Maps either to TRUE, or to a callable with $key and $value arguments.
*/
protected $filterParams = array();
private $host = 'api.raygun.io';
private $path = '/entries';
private $transport = 'ssl';
private $port = 443;
/**
* Creates a new RaygunClient instance.
*
* @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 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)
{
$this->apiKey = $key;
$this->useAsyncSending = $useAsyncSending;
$this->debugSending = $debugSending;
if (!$disableUserTracking) {
$this->SetUser();
}
$this->disableUserTracking = $disableUserTracking;
}
/**
* Transmits an error to the Raygun.io API
*
* @param int $errno The error number
* @param string $errstr The error string
* @param string $errfile The file the error occurred in
* @param int $errline The line the error occurred on
* @param array $tags An optional array of string tags used to provide metadata for the message
* @param array $userCustomData An optional associative array that can be used to place custom key-value
* @param int $timestamp Current Unix timestamp in the local timezone, used to set when an error occurred.
* data in the message payload
* @return The HTTP status code of the result when transmitting the message to Raygun.io
*/
public function SendError($errno, $errstr, $errfile, $errline, $tags = null, $userCustomData = null, $timestamp = null)
{
$message = $this->BuildMessage(new \ErrorException($errstr, $errno, 0, $errfile, $errline), $timestamp);
if ($tags != null)
{
$this->AddTags($message, $tags);
}
if ($userCustomData != null)
{
$this->AddUserCustomData($message, $userCustomData);
}
return $this->Send($message);
}
/**
* Transmits an exception to the Raygun.io API
*
* @param \Exception $exception An exception object to transmit
* @param array $tags An optional array of string tags used to provide metadata for the message
* @param array $userCustomData An optional associative array that can be used to place custom key-value
* data in the message payload
* @param int $timestamp Current Unix timestamp in the local timezone, used to set when an exception
* occurred.
* @return The HTTP status code of the result when transmitting the message to Raygun.io
*/
public function SendException($exception, $tags = null, $userCustomData = null, $timestamp = null)
{
$message = $this->BuildMessage($exception, $timestamp);
if ($tags != null)
{
$this->AddTags($message, $tags);
}
if ($userCustomData != null)
{
$this->AddUserCustomData($message, $userCustomData);
}
return $this->Send($message);
}
/**
* Sets the version number of your project that will be transmitted
* to Raygun.io.
*
* @param string $version The version number in the form of x.x.x.x,
* where x is a positive integer.
*/
public function SetVersion($version)
{
$this->version = $version;
}
/**
* Stores the current user of the calling application. This will be added to any messages sent
* by this provider. It is used in the dashboard to provide unique user tracking.
* If it is an email address, the user's Gravatar can be displayed. This method is optional,
* if it is not used a random identifier will be assigned to the current user.
*
* @param string $user A username, email address or other identifier for the current user
* of the calling application.
*/
public function SetUser($user = null, $firstName = null, $fullName = null, $email = null, $isAnonymous = null, $uuid = null)
{
$timestamp = time() + 60 * 60 * 24 * 30;
$this->firstName = $this->StoreOrRetrieveUserCookie('rgfirstname', $firstName);
$this->fullName = $this->StoreOrRetrieveUserCookie('rgfullname', $fullName);
$this->email = $this->StoreOrRetrieveUserCookie('rgemail', $email);
$this->uuid = $this->StoreOrRetrieveUserCookie('rguuidvalue', $uuid);
$this->isAnonymous = $this->StoreOrRetrieveUserCookie('rgisanonymous', $isAnonymous ? 'true' : 'false') == 'true' ? true : false;
if (is_string($user))
{
$this->user = $user;
if (php_sapi_name() != 'cli' && !headers_sent())
{
setcookie('rguserid', $user, $timestamp);
setcookie('rguuid', 'false', $timestamp);
}
}
else
{
if (!array_key_exists('rguuid', $_COOKIE))
{
$this->user = (string)Uuid::uuid4();
if (php_sapi_name() != 'cli' && !headers_sent())
{
setcookie('rguserid', $this->user, $timestamp);
setcookie('rguuid', 'true', $timestamp);
}
}
else if (array_key_exists('rguserid', $_COOKIE))
{
$this->user = $_COOKIE['rguserid'];
}
$this->isAnonymous = $this->StoreOrRetrieveUserCookie('rgisanonymous', 'true') == 'true';
}
}
private function StoreOrRetrieveUserCookie($key, $value)
{
$timestamp = time() + 60 * 60 * 24 * 30;
if (is_string($value))
{
if (php_sapi_name() != 'cli' && !headers_sent())
{
setcookie($key, $value, $timestamp);
}
return $value;
}
else
{
if (array_key_exists($key, $_COOKIE))
{
if ($_COOKIE[$key] != $value && php_sapi_name() != 'cli' && !headers_sent())
{
setcookie($key, $value, $timestamp);
}
return $_COOKIE[$key];
}
}
return null;
}
private function BuildMessage($errorException, $timestamp = null)
{
$message = new RaygunMessage($timestamp);
$message->Build($errorException);
$message->Details->Version = $this->version;
$message->Details->Context = new RaygunIdentifier(session_id());
if ($this->user != null)
{
$message->Details->User = new RaygunIdentifier($this->user, $this->firstName, $this->fullName, $this->email, $this->isAnonymous, $this->uuid);
}
else if (!$this->disableUserTracking)
{
$message->Details->User = new RaygunIdentifier($_COOKIE['rguserid']);
}
return $message;
}
/**
* Sets a string array of tags relating to the message, used for identification. These will be transmitted along
* with messages that are sent.
*
* @param RaygunMessage $message
* @param array $tags The tags relating to your project's version
* @throws Raygun4PhpException
*/
private function AddTags(&$message, $tags)
{
if (is_array($tags))
{
$message->Details->Tags = $tags;
}
else
{
throw new \Raygun4php\Raygun4PhpException("Tags must be an array");
}
}
private function AddUserCustomData(&$message, $userCustomData)
{
if ($this->is_assoc($userCustomData))
{
$message->Details->UserCustomData = $userCustomData;
}
else
{
throw new \Raygun4php\Raygun4PhpException("UserCustomData must be an associative array");
}
}
private function is_assoc($array)
{
return (bool)count(array_filter(array_keys($array), 'is_string'));
}
/**
* Transmits a RaygunMessage to the Raygun.io API. The default attempts to transmit asynchronously.
* To disable this and transmit sync (blocking), pass false in as the 2nd parameter in RaygunClient's
* constructor. This may be necessary on some Windows installations where the implementation is broken.
* This is a lower level function used by SendException and SendError and one of those should be used preferrably.
*
* @param \Raygun4php\RaygunMessage $message A populated message to be posted to the Raygun API
* @return int The HTTP status code of the result after transmitting the message to Raygun.io
* 202 if accepted, 403 if invalid JSON payload
* @throws Raygun4PhpException
*/
public function Send($message)
{
if (empty($this->apiKey))
{
throw new \Raygun4php\Raygun4PhpException("API not valid, cannot send message to Raygun");
}
$message = $this->filterParamsFromMessage($message);
$message = $this->toJsonRemoveUnicodeSequences($message);
return $this->post($message, realpath(__DIR__ . '/cacert.crt'));
}
private function post($data_to_send, $cert_path)
{
$headers = 0;
$remote = $this->transport . '://' . $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')
{
$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;
}
else
{
$fp = stream_socket_client($remote, $err, $errstr, 10, STREAM_CLIENT_CONNECT, $context);
if ($fp)
{
$req = '';
$req .= "POST $this->path HTTP/1.1\r\n";
$req .= "Host: $this->host\r\n";
$req .= "X-ApiKey: " . $this->apiKey . "\r\n";
$req .= 'Content-length: ' . strlen($data_to_send) . "\r\n";
$req .= "Content-type: application/json\r\n";
$req .= "Connection: close\r\n\r\n";
fwrite($fp, $req);
fwrite($fp, $data_to_send);
$response = "";
if ($this->debugSending)
{
while(!preg_match("/^HTTP\/[\d\.]* (\d{3})/", $response))
{
$response .= fgets($fp, 128);
}
fclose($fp);
return $response;
}
else
{
fclose($fp);
return 0;
}
}
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;
trigger_error('httpPost error: ' . $errstr);
return null;
}
}
}
function toJsonRemoveUnicodeSequences($struct) {
return preg_replace_callback("/\\\\u([a-f0-9]{4})/", function($matches){ return iconv('UCS-4LE','UTF-8',pack('V', hexdec("U$matches[1]"))); }, json_encode($struct));
}
/**
* Optionally applies a value transformation to every matching key, as defined by {@link FilterParams}.
* Replaces the value by default, but also supports custom transformations through
* anonymous functions. Applies to form data, environment data, HTTP headers.
* Does not apply to GET parameters in the request URI.
* Filters out raw HTTP data in case any filters are defined, since we can't accurately filter it.
*
* @param RaygunMessage $message
* @param string $replace Value to be inserted by default (unless specified otherwise by custom transformations).
* @return RaygunMessage
*/
function filterParamsFromMessage($message, $replace = '[filtered]') {
$filterParams = $this->getFilterParams();
// Skip checks if none are defined
if(!$filterParams) {
return $message;
}
// Ensure all filters are callable
foreach($filterParams as $filterKey => $filterFn) {
if(!is_callable($filterFn)) {
$filterParams[$filterKey] = function($key, $val) use ($replace) {return $replace;};
}
}
$walkFn = function(&$val, $key) use ($filterParams) {
foreach($filterParams as $filterKey => $filterFn) {
if(
(strpos($filterKey, '/') === 0 && preg_match($filterKey, $key))
|| (strpos($filterKey, '/') === FALSE && strtolower($filterKey) == strtolower($key))
) {
$val = $filterFn($key, $val);
}
}
};
if($message->Details->Request->Form) {
array_walk_recursive($message->Details->Request->Form, $walkFn);
}
if($message->Details->Request->Headers) {
array_walk_recursive($message->Details->Request->Headers, $walkFn);
}
if($message->Details->Request->Data) {
array_walk_recursive($message->Details->Request->Data, $walkFn);
}
if($message->Details->UserCustomData) {
array_walk_recursive($message->Details->UserCustomData, $walkFn);
}
// Unset raw HTTP data since we can't accurately filter it
if($message->Details->Request->RawData) {
$message->Details->Request->RawData = null;
}
return $message;
}
/**
* @param array $params
* @return self
*/
function setFilterParams($params) {
$this->filterParams = $params;
return $this;
}
/**
* @return array
*/
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 self
*/
function setProxy($proxy) {
$this->proxy = $proxy;
return $this;
}
/**
* @return String
*/
function getProxy() {
return $this->proxy;
}
public function __destruct()
{
if ($this->httpData)
{
curl_close($this->httpData);
}
}
}
}