| 1 |
<?php |
| 2 |
/* |
| 3 |
* Copyright 2012 Facebook, Inc. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
/** |
| 19 |
* This helper is based on code from Facebook's Phabricator project |
| 20 |
* |
| 21 |
* https://github.com/facebook/phabricator |
| 22 |
* |
| 23 |
* Specifically, it is an adaptation of the PhutilReadableSerializer class. |
| 24 |
* |
| 25 |
* @package raven |
| 26 |
*/ |
| 27 |
class Raven_Serializer |
| 28 |
{ |
| 29 |
/* |
| 30 |
* The default mb detect order |
| 31 |
* |
| 32 |
* @see http://php.net/manual/en/function.mb-detect-encoding.php |
| 33 |
*/ |
| 34 |
const DEFAULT_MB_DETECT_ORDER = 'auto'; |
| 35 |
|
| 36 |
/* |
| 37 |
* Suggested detect order for western countries |
| 38 |
*/ |
| 39 |
const WESTERN_MB_DETECT_ORDER = 'UTF-8, ASCII, ISO-8859-1, ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6, ISO-8859-7, ISO-8859-8, ISO-8859-9, ISO-8859-10, ISO-8859-13, ISO-8859-14, ISO-8859-15, ISO-8859-16, Windows-1251, Windows-1252, Windows-1254'; |
| 40 |
|
| 41 |
/** |
| 42 |
* This is the default mb detect order for the detection of encoding |
| 43 |
* |
| 44 |
* @var string |
| 45 |
*/ |
| 46 |
protected $mb_detect_order = self::DEFAULT_MB_DETECT_ORDER; |
| 47 |
|
| 48 |
/** |
| 49 |
* @param null|string $mb_detect_order |
| 50 |
*/ |
| 51 |
public function __construct($mb_detect_order = null) |
| 52 |
{ |
| 53 |
if ($mb_detect_order != null) { |
| 54 |
$this->mb_detect_order = $mb_detect_order; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Serialize an object (recursively) into something safe for data |
| 60 |
* sanitization and encoding. |
| 61 |
* |
| 62 |
* @param mixed $value |
| 63 |
* @param int $max_depth |
| 64 |
* @param int $_depth |
| 65 |
* @return string|bool|double|int|null|object|array |
| 66 |
*/ |
| 67 |
public function serialize($value, $max_depth = 3, $_depth = 0) |
| 68 |
{ |
| 69 |
$className = is_object($value) ? get_class($value) : null; |
| 70 |
$toArray = is_array($value) || $className === 'stdClass'; |
| 71 |
if ($toArray && $_depth < $max_depth) { |
| 72 |
$new = array(); |
| 73 |
foreach ($value as $k => $v) { |
| 74 |
$new[$this->serializeValue($k)] = $this->serialize($v, $max_depth, $_depth + 1); |
| 75 |
} |
| 76 |
|
| 77 |
return $new; |
| 78 |
} |
| 79 |
return $this->serializeValue($value); |
| 80 |
} |
| 81 |
|
| 82 |
protected function serializeString($value) |
| 83 |
{ |
| 84 |
$value = (string) $value; |
| 85 |
if (function_exists('mb_detect_encoding') |
| 86 |
&& function_exists('mb_convert_encoding') |
| 87 |
) { |
| 88 |
// we always guarantee this is coerced, even if we can't detect encoding |
| 89 |
if ($currentEncoding = mb_detect_encoding($value, $this->mb_detect_order)) { |
| 90 |
$value = mb_convert_encoding($value, 'UTF-8', $currentEncoding); |
| 91 |
} else { |
| 92 |
$value = mb_convert_encoding($value, 'UTF-8'); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if (strlen($value) > 1024) { |
| 97 |
$value = substr($value, 0, 1014) . ' {clipped}'; |
| 98 |
} |
| 99 |
|
| 100 |
return $value; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @param mixed $value |
| 105 |
* @return string|bool|double|int|null |
| 106 |
*/ |
| 107 |
protected function serializeValue($value) |
| 108 |
{ |
| 109 |
if (is_null($value) || is_bool($value) || is_float($value) || is_integer($value)) { |
| 110 |
return $value; |
| 111 |
} elseif (is_object($value) || gettype($value) == 'object') { |
| 112 |
return 'Object '.get_class($value); |
| 113 |
} elseif (is_resource($value)) { |
| 114 |
return 'Resource '.get_resource_type($value); |
| 115 |
} elseif (is_array($value)) { |
| 116 |
return 'Array of length ' . count($value); |
| 117 |
} else { |
| 118 |
return $this->serializeString($value); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
/** |
| 124 |
* @return string |
| 125 |
* @codeCoverageIgnore |
| 126 |
*/ |
| 127 |
public function getMbDetectOrder() |
| 128 |
{ |
| 129 |
return $this->mb_detect_order; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @param string $mb_detect_order |
| 134 |
* |
| 135 |
* @return Raven_Serializer |
| 136 |
* @codeCoverageIgnore |
| 137 |
*/ |
| 138 |
public function setMbDetectOrder($mb_detect_order) |
| 139 |
{ |
| 140 |
$this->mb_detect_order = $mb_detect_order; |
| 141 |
|
| 142 |
return $this; |
| 143 |
} |
| 144 |
} |
| 145 |
|