diff --git a/whups/lib/Driver.php b/whups/lib/Driver.php index 5c3597622c6..c663085be61 100644 --- a/whups/lib/Driver.php +++ b/whups/lib/Driver.php @@ -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]; @@ -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) diff --git a/whups/lib/Driver/Sql.php b/whups/lib/Driver/Sql.php index 038d11d50ff..5d4f7fec16e 100644 --- a/whups/lib/Driver/Sql.php +++ b/whups/lib/Driver/Sql.php @@ -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']; } @@ -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 { @@ -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;