Skip to content

Commit

Permalink
Merge pull request #49 from MindscapeHQ/enhanced-user-info
Browse files Browse the repository at this point in the history
Enhanced user info
  • Loading branch information
fundead committed Jul 2, 2014
2 parents 988b038 + f735c24 commit af96ad3
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 24 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,21 @@ You can transmit the version number of your PHP project along with the message b

### User tracking

You can call $client->SetUser($user), passing in a string representing the username or email address of the current user of the calling application. This will be attached to the message and visible in the dashboard. This method is optional - if it is not called, a random identifier will be used. If you use this, and the user changes (log in/out), be sure to call it again passing in the new user (or just call $client->SetUser() to assign a new random identifier).
**New in 1.5: additional data support**

You can call $client->SetUser, passing in some or all of the following data, which will be used to provide an affected user count and reports:

```php
SetUser($user = null, $firstName = null, $fullName = null, $email = null, $isAnonymous = null, $uuid = null)
```

`$user` should be a unique identifier which is used to identify your users. If you set this to their email address, be sure to also set the $email parameter too.

This feature and values are optional if you wish to disable it for privacy concerns.

Note that this data is stored as a cookie. If you do not call SetUser the default is to store a random UUID to represent the user.

This feature can be used in CLI mode by calling SetUser(string) at the start of your session.
This feature can be used in CLI mode by calling SetUser() at the start of your session.

### Filtering Sensitive Data

Expand Down Expand Up @@ -193,6 +203,7 @@ If, when running a PHP script from the command line on *nix operating systems, y

## Changelog

- 1.5.0: Add enhanced user data support; fix null backtrace frames that could occur in 1.4
- 1.4.0: Added Sensitive Data Filtering; improved Error backtraces; Travis CI enabled
- 1.3.6: Move included Rhumsaa\Uuid lib into this namespace to prevent collisions if already included
- 1.3.5: Fixed possible bug in async curl logic
Expand Down
56 changes: 50 additions & 6 deletions src/Raygun4php/RaygunClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class RaygunClient
protected $version;
protected $tags;
protected $user;
protected $firstName;
protected $fullName;
protected $email;
protected $isAnonymous;
protected $uuid;
protected $httpData;
protected $useAsyncSending;
protected $debugSending;
Expand Down Expand Up @@ -121,16 +126,25 @@ public function SetVersion($version)
* of the calling application.
*
*/
public function SetUser($user = null)
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, time() + 60 * 60 * 24 * 30);
setcookie('rguuid', 'false', time() + 60 * 60 * 24 * 30);
setcookie('rguserid', $user, $timestamp);
setcookie('rguuid', 'false', $timestamp);
}
}
else
Expand All @@ -141,15 +155,45 @@ public function SetUser($user = null)

if (php_sapi_name() != 'cli' && !headers_sent())
{
setcookie('rguserid', $this->user, time() + 60 * 60 * 24 * 30);
setcookie('rguuid', 'true', time() + 60 * 60 * 24 * 30);
setcookie('rguserid', $this->user, $timestamp);
setcookie('rguuid', 'true', $timestamp);
}
}
else
{
$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;
}

/*
Expand All @@ -167,7 +211,7 @@ private function BuildMessage($errorException, $timestamp = null)

if ($this->user != null)
{
$message->Details->User = new RaygunIdentifier($this->user);
$message->Details->User = new RaygunIdentifier($this->user, $this->firstName, $this->fullName, $this->email, $this->isAnonymous, $this->uuid);
}
else
{
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.4.0";
$this->version = "1.5.0";
$this->clientUrl = "https://github.com/MindscapeHQ/raygun4php";
}
}
Expand Down
37 changes: 23 additions & 14 deletions src/Raygun4php/RaygunExceptionMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public function __construct($exception)
{
$exceptionClass = get_class($exception);

if ($exceptionClass != "ErrorException")
if ($exceptionClass != 'ErrorException')
{
$this->Message = $exceptionClass.": ".$exception->getMessage();
$this->Message = $exceptionClass.': '.$exception->getMessage();
$this->BuildStackTrace($exception);
}
else
{
$this->Message = "Error: ".$exception->getMessage();
$this->Message = 'Error: '.$exception->getMessage();
$this->BuildErrorTrace($exception);
}

Expand All @@ -37,8 +37,17 @@ private function BuildErrorTrace($error)
foreach ($traces as $trace) {
$line = new RaygunExceptionTraceLineMessage();

$argCount = count($trace['args']);
if (array_key_exists('args', $trace) && $argCount != 5) {
$fromManualSendError = false;
if (array_key_exists('function', $trace) &&
array_key_exists('class', $trace))
{
if ($trace['function'] == 'SendError' && $trace['class'] == 'Raygun4php\RaygunClient')
{
$fromManualSendError = true;
}
}

if (array_key_exists('args', $trace) && $fromManualSendError == true) {
$errorData = $trace['args'];

if (count($errorData) >= 2) {
Expand Down Expand Up @@ -79,21 +88,21 @@ private function BuildLine($trace)
{
$line = new RaygunExceptionTraceLineMessage();

if (array_key_exists("file", $trace))
if (array_key_exists('file', $trace))
{
$line->FileName = $trace["file"];
$line->FileName = $trace['file'];
}
if (array_key_exists("class", $trace))
if (array_key_exists('class', $trace))
{
$line->ClassName = $trace["class"];
$line->ClassName = $trace['class'];
}
if (array_key_exists("function", $trace))
if (array_key_exists('function', $trace))
{
$line->MethodName = $trace["function"];
$line->MethodName = $trace['function'];
}
if (array_key_exists("line", $trace))
if (array_key_exists('line', $trace))
{
$line->LineNumber = $trace["line"];
$line->LineNumber = $trace['line'];
}

return $line;
Expand Down Expand Up @@ -132,7 +141,7 @@ private function GetClassName()
}
}
}
if ($class != "")
if ($class != '')
{
return $class;
}
Expand Down
17 changes: 16 additions & 1 deletion src/Raygun4php/RaygunIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,24 @@ class RaygunIdentifier
{
public $Identifier;

public function __construct($id)
public $FirstName;

public $FullName;

public $Email;

public $IsAnonymous;

public $Uuid;

public function __construct($id, $firstName = null, $fullName = null, $email = null, $isAnonymous = null, $uuid = null)
{
$this->Identifier = $id;
$this->FirstName = $firstName;
$this->FullName = $fullName;
$this->Email = $email;
$this->IsAnonymous = $isAnonymous;
$this->Uuid = $uuid;
}
}
}

0 comments on commit af96ad3

Please sign in to comment.