Skip to content

Commit

Permalink
Bug: 14326 Fix decoding custom_attributes.
Browse files Browse the repository at this point in the history
These can be json_encoded (which may contain large numeric strings
such as phone numbers), or bare scalar values.
  • Loading branch information
mrubinsk committed Apr 10, 2016
1 parent 6c7a7ae commit 3472b95
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 46 deletions.
50 changes: 39 additions & 11 deletions whups/lib/Driver.php
Expand Up @@ -145,17 +145,7 @@ public function getHistory($ticket_id, Horde_Form $form = null)

default:
if (strpos($type, 'attribute_') === 0) {
if (is_string($value) && defined('JSON_BIGINT_AS_STRING')) {
$value = json_decode(
$value, true, 512, constant('JSON_BIGINT_AS_STRING')
);
} else {
try {
$value = Horde_Serialize::unserialize(
$value, Horde_Serialize::JSON);
} catch (Horde_Serialize_Exception $e) {
}
}
$value = $this->_json_decode($value);
$attribute = substr($type, 10);
if (isset($attributes[$attribute])) {
$label = $attributes[$attribute];
Expand Down Expand Up @@ -200,6 +190,44 @@ public function getHistory($ticket_id, Horde_Form $form = null)
return $history;
}

/**
* Helper to decode attribute value, which may be a bare scalar value, or
* a json encoded structure that may contain large numeric strings that
* should not be taken as integers.
*
* @param string The value to decode.
*
* @return mixed The decoded value.
*/
protected function _json_decode($value)
{
if (is_string($value) && defined('JSON_BIGINT_AS_STRING')) {
$result = json_decode(
$value, true, 512, constant('JSON_BIGINT_AS_STRING')
);
// If we failed above, see if it was a bare scalar value. Note we
// need to encode it first to be sure we escape any characters that
// need escaping and we properly quote strings.
if (!isset($result)) {
$result = json_decode(
json_encode($value),
true,
512,
constant('JSON_BIGINT_AS_STRING')
);
}
} else {
try {
$result = Horde_Serialize::unserialize(
$value, Horde_Serialize::JSON);
} catch (Horde_Serialize_Exception $e) {
$result = $value;
}
}

return $result;
}

/**
*/
public function getQueue($queueId)
Expand Down
38 changes: 3 additions & 35 deletions whups/lib/Driver/Sql.php
Expand Up @@ -1129,19 +1129,7 @@ public function getTicketsByProperties(
$attributes = $this->getTicketAttributesWithNames(array_keys($tickets));
foreach ($attributes as $row) {
$attribute_id = 'attribute_' . $row['attribute_id'];
if (is_string($row['attribute_value']) && defined('JSON_BIGINT_AS_STRING')) {
$tickets[$row['id']][$attribute_id] = json_decode(
$row['attribute_value'], true, 512, constant('JSON_BIGINT_AS_STRING')
);
} else {
try {
$tickets[$row['id']][$attribute_id] =
Horde_Serialize::unserialize($row['attribute_value'],
Horde_Serialize::JSON);
} catch (Horde_Serialize_Exception $e) {
$tickets[$row['id']][$attribute_id] = $row['attribute_value'];
}
}
$tickets[$row['id']][$attribute_id] = $this->_json_decode($row['attribute_value']);
$tickets[$row['id']][$attribute_id . '_name'] = $row['attribute_name'];
}

Expand Down Expand Up @@ -3137,16 +3125,7 @@ public function getTicketAttributesWithNames($ticket_id)
$attributes = $this->_fromBackend($attributes);

foreach ($attributes as &$ticket) {
if (is_string($ticket['attribute_value']) && defined('JSON_BIGINT_AS_STRING')) {
$ticket['attribute_value'] = json_decode($ticket['attribute_value'], true, 512, constant('JSON_BIGINT_AS_STRING'));
} else {
try {
$ticket['attribute_value'] = Horde_Serialize::unserialize(
$ticket['attribute_value'],
Horde_Serialize::JSON);
} catch (Horde_Serialize_Exception $e) {
}
}
$ticket['attribute_value'] = $this->_json_decode($ticket['attribute_value']);
}
} else {
try {
Expand Down Expand Up @@ -3214,18 +3193,7 @@ protected function _getAllTicketAttributesWithNames($ticket_id)
$this->_fromBackend(@unserialize($attribute['attribute_params']));
$attribute['attribute_required'] =
(bool)$attribute['attribute_required'];
if (is_string($attribute['attribute_value']) && defined('JSON_BIGINT_AS_STRING')) {
$attribute['attribute_value'] = json_decode(
$attribute['attribute_value'], true, 512, constant('JSON_BIGINT_AS_STRING')
);
} else {
try {
$attribute['attribute_value'] = Horde_Serialize::unserialize(
$attribute['attribute_value'],
Horde_Serialize::JSON);
} catch (Horde_Serialize_Exception $e) {
}
}
$attribute['attribute_value'] = $this->_json_decode($attribute['attribute_value']);
}

return $attributes;
Expand Down

0 comments on commit 3472b95

Please sign in to comment.