Skip to content

Commit 7f97c9e

Browse files
author
Cyril Glapa
committed
Updating raven-php
1 parent 8013eb6 commit 7f97c9e

File tree

8 files changed

+514
-148
lines changed

8 files changed

+514
-148
lines changed

src/lib/Raven/Client.php

Lines changed: 273 additions & 95 deletions
Large diffs are not rendered by default.

src/lib/Raven/Compat.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
class Raven_Compat
1313
{
14-
1514
public static function gethostname()
1615
{
1716
if (function_exists('gethostname')) {

src/lib/Raven/CurlHandler.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public function enqueue($url, $data=null, $headers=array())
4545
foreach ($headers as $key => $value) {
4646
array_push($new_headers, $key .': '. $value);
4747
}
48+
// XXX(dcramer): Prevent 100-continue response form server (Fixes GH-216)
49+
$new_headers[] = 'Expect:';
4850

4951
curl_setopt($ch, CURLOPT_HTTPHEADER, $new_headers);
5052
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@@ -69,9 +71,9 @@ public function enqueue($url, $data=null, $headers=array())
6971

7072
public function join($timeout=null)
7173
{
72-
if (!isset($timeout)) {
73-
$timeout = $this->join_timeout;
74-
}
74+
if (!isset($timeout)) {
75+
$timeout = $this->join_timeout;
76+
}
7577
$start = time();
7678
do {
7779
$this->select();
@@ -94,8 +96,7 @@ private function select()
9496
do {
9597
$mrc = curl_multi_exec($this->multi_handle, $active);
9698
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
97-
}
98-
else {
99+
} else {
99100
return;
100101
}
101102
}

src/lib/Raven/ErrorHandler.php

Lines changed: 111 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
* @package raven
2222
*/
2323

24+
// TODO(dcramer): deprecate default error types in favor of runtime configuration
25+
// unless a reason can be determined that making them dynamic is better. They
26+
// currently are not used outside of the fatal handler.
2427
class Raven_ErrorHandler
2528
{
2629
private $old_exception_handler;
@@ -29,11 +32,60 @@ class Raven_ErrorHandler
2932
private $call_existing_error_handler = false;
3033
private $reservedMemory;
3134
private $send_errors_last = false;
32-
private $error_types = -1;
3335

34-
public function __construct($client, $send_errors_last = false)
36+
/**
37+
* @var array
38+
* Error types which should be processed by the handler.
39+
* A 'null' value implies "whatever error_reporting is at time of error".
40+
*/
41+
private $error_types = null;
42+
43+
/**
44+
* @deprecated
45+
* @var array
46+
* Error types that can be processed by the handler
47+
*/
48+
private $validErrorTypes = array(
49+
E_ERROR,
50+
E_WARNING,
51+
E_PARSE,
52+
E_NOTICE,
53+
E_CORE_ERROR,
54+
E_CORE_WARNING,
55+
E_COMPILE_ERROR,
56+
E_COMPILE_WARNING,
57+
E_USER_ERROR,
58+
E_USER_WARNING,
59+
E_USER_NOTICE,
60+
E_STRICT,
61+
E_RECOVERABLE_ERROR,
62+
E_DEPRECATED,
63+
E_USER_DEPRECATED,
64+
);
65+
66+
/**
67+
* @deprecated
68+
* @var array
69+
* The default Error types that are always processed by the handler. Can be set during construction.
70+
*/
71+
private $defaultErrorTypes = array(
72+
E_ERROR,
73+
E_PARSE,
74+
E_CORE_ERROR,
75+
E_CORE_WARNING,
76+
E_COMPILE_ERROR,
77+
E_COMPILE_WARNING,
78+
E_STRICT,
79+
);
80+
81+
public function __construct($client, $send_errors_last = false, $default_error_types = null,
82+
$error_types = null)
3583
{
3684
$this->client = $client;
85+
if ($default_error_types !== null) {
86+
$this->defaultErrorTypes = $default_error_types;
87+
}
88+
$this->error_types = $error_types;
3789
register_shutdown_function(array($this, 'detectShutdown'));
3890
if ($send_errors_last) {
3991
$this->send_errors_last = true;
@@ -53,16 +105,49 @@ public function handleException($e, $isError = false, $vars = null)
53105

54106
public function handleError($code, $message, $file = '', $line = 0, $context=array())
55107
{
56-
if ($this->error_types & $code & error_reporting()) {
57-
$e = new ErrorException($message, 0, $code, $file, $line);
58-
$this->handleException($e, true, $context);
108+
if (error_reporting() !== 0) {
109+
$error_types = $this->error_types;
110+
if ($error_types === null) {
111+
$error_types = error_reporting();
112+
}
113+
if ($error_types & $code) {
114+
$e = new ErrorException($message, 0, $code, $file, $line);
115+
$this->handleException($e, true, $context);
116+
}
59117
}
60-
61-
if ($this->call_existing_error_handler && $this->old_error_handler) {
62-
call_user_func($this->old_error_handler, $code, $message, $file, $line, $context);
118+
if ($this->call_existing_error_handler) {
119+
if ($this->old_error_handler !== null) {
120+
return call_user_func($this->old_error_handler, $code, $message, $file, $line, $context);
121+
} else {
122+
return false;
123+
}
63124
}
64125
}
65126

127+
/**
128+
* Nothing by default, use it in child classes for catching other types of errors
129+
* Only constants from $this->validErrorTypes can be used
130+
*
131+
* @deprecated
132+
* @return array
133+
*/
134+
135+
protected function getAdditionalErrorTypesToProcess()
136+
{
137+
return array();
138+
}
139+
140+
/**
141+
* @deprecated
142+
* @return array
143+
*/
144+
private function getErrorTypesToProcess()
145+
{
146+
$additionalErrorTypes = array_intersect($this->getAdditionalErrorTypesToProcess(), $this->validErrorTypes);
147+
// array_unique so bitwise "or" operation wouldn't fail if some error type gets repeated
148+
return array_unique(array_merge($this->defaultErrorTypes, $additionalErrorTypes));
149+
}
150+
66151
public function handleFatalError()
67152
{
68153
if (null === $lastError = error_get_last()) {
@@ -71,7 +156,10 @@ public function handleFatalError()
71156

72157
unset($this->reservedMemory);
73158

74-
$errors = E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING | E_STRICT;
159+
$errors = 0;
160+
foreach ($this->getErrorTypesToProcess() as $errorType) {
161+
$errors |= $errorType;
162+
}
75163

76164
if ($lastError['type'] & $errors) {
77165
$e = new ErrorException(
@@ -88,10 +176,19 @@ public function registerExceptionHandler($call_existing_exception_handler = true
88176
$this->call_existing_exception_handler = $call_existing_exception_handler;
89177
}
90178

91-
public function registerErrorHandler($call_existing_error_handler = true, $error_types = -1)
179+
/**
180+
* Register a handler which will intercept standard PHP errors and report them to the
181+
* associated Sentry client.
182+
*
183+
* @return array
184+
*/
185+
//
186+
public function registerErrorHandler($call_existing_error_handler = true, $error_types = null)
92187
{
93-
$this->error_types = $error_types;
94-
$this->old_error_handler = set_error_handler(array($this, 'handleError'), error_reporting());
188+
if ($error_types !== null) {
189+
$this->error_types = $error_types;
190+
}
191+
$this->old_error_handler = set_error_handler(array($this, 'handleError'), E_ALL);
95192
$this->call_existing_error_handler = $call_existing_error_handler;
96193
}
97194

@@ -102,7 +199,8 @@ public function registerShutdownFunction($reservedMemorySize = 10)
102199
$this->reservedMemory = str_repeat('x', 1024 * $reservedMemorySize);
103200
}
104201

105-
public function detectShutdown() {
202+
public function detectShutdown()
203+
{
106204
if (!defined('RAVEN_CLIENT_END_REACHED')) {
107205
define('RAVEN_CLIENT_END_REACHED', true);
108206
}

src/lib/Raven/Processor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* @package raven
66
*/
7-
class Raven_Processor
7+
abstract class Raven_Processor
88
{
99
public function __construct(Raven_Client $client)
1010
{
@@ -13,8 +13,8 @@ public function __construct(Raven_Client $client)
1313

1414
/**
1515
* Process and sanitize data, modifying the existing value if necessary.
16+
*
17+
* @param array $data Array of log data
1618
*/
17-
public function process(&$data)
18-
{
19-
}
19+
abstract public function process(&$data);
2020
}

src/lib/Raven/SanitizeDataProcessor.php

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,114 @@
77
*/
88
class Raven_SanitizeDataProcessor extends Raven_Processor
99
{
10-
static $mask = '********';
11-
static $fields_re = '/(authorization|password|passwd|secret|password_confirmation|card_number)/i';
12-
static $values_re = '/^(?:\d[ -]*?){13,16}$/';
10+
const MASK = '********';
11+
const FIELDS_RE = '/(authorization|password|passwd|secret|password_confirmation|card_number|auth_pw)/i';
12+
const VALUES_RE = '/^(?:\d[ -]*?){13,16}$/';
1313

14+
private $client;
15+
private $fields_re;
16+
private $values_re;
17+
18+
public function __construct(Raven_Client $client)
19+
{
20+
$this->client = $client;
21+
$this->fields_re = self::FIELDS_RE;
22+
$this->values_re = self::VALUES_RE;
23+
$this->session_cookie_name = ini_get('session.name');
24+
}
25+
26+
/**
27+
* Override the default processor options
28+
*
29+
* @param array $options Associative array of processor options
30+
*/
31+
public function setProcessorOptions(array $options)
32+
{
33+
if (isset($options['fields_re'])) {
34+
$this->fields_re = $options['fields_re'];
35+
}
36+
37+
if (isset($options['values_re'])) {
38+
$this->values_re = $options['values_re'];
39+
}
40+
}
41+
42+
/**
43+
* Replace any array values with our mask if the field name or the value matches a respective regex
44+
*
45+
* @param mixed $item Associative array value
46+
* @param string $key Associative array key
47+
*/
1448
public function sanitize(&$item, $key)
1549
{
1650
if (empty($item)) {
1751
return;
1852
}
1953

20-
if (preg_match(self::$values_re, $item)) {
21-
$item = self::$mask;
54+
if (preg_match($this->values_re, $item)) {
55+
$item = self::MASK;
2256
}
2357

2458
if (empty($key)) {
2559
return;
2660
}
2761

28-
if (preg_match(self::$fields_re, $key)) {
29-
$item = self::$mask;
62+
if (preg_match($this->fields_re, $key)) {
63+
$item = self::MASK;
64+
}
65+
}
66+
67+
public function sanitizeHttp(&$data)
68+
{
69+
if (empty($data['request'])) {
70+
return;
71+
}
72+
$http = &$data['request'];
73+
if (empty($http['cookies'])) {
74+
return;
75+
}
76+
77+
$cookies = &$http['cookies'];
78+
if (!empty($cookies[$this->session_cookie_name])) {
79+
$cookies[$this->session_cookie_name] = self::MASK;
3080
}
3181
}
3282

3383
public function process(&$data)
3484
{
3585
array_walk_recursive($data, array($this, 'sanitize'));
86+
$this->sanitizeHttp($data);
87+
}
88+
89+
/**
90+
* @return string
91+
*/
92+
public function getFieldsRe()
93+
{
94+
return $this->fields_re;
95+
}
96+
97+
/**
98+
* @param string $fields_re
99+
*/
100+
public function setFieldsRe($fields_re)
101+
{
102+
$this->fields_re = $fields_re;
103+
}
104+
105+
/**
106+
* @return string
107+
*/
108+
public function getValuesRe()
109+
{
110+
return $this->values_re;
111+
}
112+
113+
/**
114+
* @param string $values_re
115+
*/
116+
public function setValuesRe($values_re)
117+
{
118+
$this->values_re = $values_re;
36119
}
37120
}

src/lib/Raven/Serializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function serializeValue($value)
6868
$value = (string) $value;
6969

7070
if (function_exists('mb_convert_encoding')) {
71-
$value = mb_convert_encoding($value, 'UTF-8', 'UTF-8');
71+
$value = mb_convert_encoding($value, 'UTF-8', 'auto');
7272
}
7373

7474
return $value;

0 commit comments

Comments
 (0)