serialisation.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. if (!class_exists('Services_JSON', false)) {
  3. require_once dirname(__FILE__).'/services_json.php';
  4. }
  5. function CS_REST_SERIALISATION_get_available($log) {
  6. $log->log_message('Getting serialiser', __FUNCTION__, CS_REST_LOG_VERBOSE);
  7. if(function_exists('json_decode') && function_exists('json_encode')) {
  8. return new CS_REST_NativeJsonSerialiser($log);
  9. } else {
  10. return new CS_REST_ServicesJsonSerialiser($log);
  11. }
  12. }
  13. class CS_REST_BaseSerialiser {
  14. var $_log;
  15. function __construct($log) {
  16. $this->_log = $log;
  17. }
  18. /**
  19. * Recursively ensures that all data values are utf-8 encoded.
  20. * @param array $data All values of this array are checked for utf-8 encoding.
  21. */
  22. function check_encoding($data) {
  23. foreach($data as $k => $v) {
  24. // If the element is a sub-array then recusively encode the array
  25. if(is_array($v)) {
  26. $data[$k] = $this->check_encoding($v);
  27. // Otherwise if the element is a string then we need to check the encoding
  28. } else if(is_string($v)) {
  29. if((function_exists('mb_detect_encoding') && mb_detect_encoding($v) !== 'UTF-8') ||
  30. (function_exists('mb_check_encoding') && !mb_check_encoding($v, 'UTF-8'))) {
  31. // The string is using some other encoding, make sure we utf-8 encode
  32. $v = utf8_encode($v);
  33. }
  34. $data[$k] = $v;
  35. }
  36. }
  37. return $data;
  38. }
  39. }
  40. class CS_REST_DoNothingSerialiser extends CS_REST_BaseSerialiser {
  41. function get_type() { return 'do_nothing'; }
  42. function serialise($data) { return $data; }
  43. function deserialise($text) {
  44. $data = json_decode($text);
  45. return is_null($data) ? $text : $data;
  46. }
  47. function check_encoding($data) { return $data; }
  48. }
  49. class CS_REST_NativeJsonSerialiser extends CS_REST_BaseSerialiser {
  50. function get_format() {
  51. return 'json';
  52. }
  53. function get_type() {
  54. return 'native';
  55. }
  56. function serialise($data) {
  57. if(is_null($data) || $data == '') return '';
  58. return json_encode($this->check_encoding($data));
  59. }
  60. function deserialise($text) {
  61. $data = json_decode($text);
  62. return $this->strip_surrounding_quotes(is_null($data) ? $text : $data);
  63. }
  64. /**
  65. * We've had sporadic reports of people getting ID's from create routes with the surrounding quotes present.
  66. * There is no case where these should be present. Just get rid of it.
  67. */
  68. function strip_surrounding_quotes($data) {
  69. if(is_string($data)) {
  70. return trim($data, '"');
  71. }
  72. return $data;
  73. }
  74. }
  75. class CS_REST_ServicesJsonSerialiser extends CS_REST_BaseSerialiser {
  76. var $_serialiser;
  77. function __construct($log) {
  78. parent::__construct($log);
  79. $this->_serialiser = new Services_JSON();
  80. }
  81. function get_content_type() {
  82. return 'application/json';
  83. }
  84. function get_format() {
  85. return 'json';
  86. }
  87. function get_type() {
  88. return 'services_json';
  89. }
  90. function serialise($data) {
  91. if(is_null($data) || $data == '') return '';
  92. return $this->_serialiser->encode($this->check_encoding($data));
  93. }
  94. function deserialise($text) {
  95. $data = $this->_serialiser->decode($text);
  96. return is_null($data) ? $text : $data;
  97. }
  98. }