Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

128 lines
3.9KB

  1. <?php
  2. /**
  3. * Implements data: URI for base64 encoded images supported by GD.
  4. */
  5. class HTMLPurifier_URIScheme_data extends HTMLPurifier_URIScheme
  6. {
  7. /**
  8. * @type bool
  9. */
  10. public $browsable = true;
  11. /**
  12. * @type array
  13. */
  14. public $allowed_types = array(
  15. // you better write validation code for other types if you
  16. // decide to allow them
  17. 'image/jpeg' => true,
  18. 'image/gif' => true,
  19. 'image/png' => true,
  20. );
  21. // this is actually irrelevant since we only write out the path
  22. // component
  23. /**
  24. * @type bool
  25. */
  26. public $may_omit_host = true;
  27. /**
  28. * @param HTMLPurifier_URI $uri
  29. * @param HTMLPurifier_Config $config
  30. * @param HTMLPurifier_Context $context
  31. * @return bool
  32. */
  33. public function doValidate(&$uri, $config, $context)
  34. {
  35. $result = explode(',', $uri->path, 2);
  36. $is_base64 = false;
  37. $charset = null;
  38. $content_type = null;
  39. if (count($result) == 2) {
  40. list($metadata, $data) = $result;
  41. // do some legwork on the metadata
  42. $metas = explode(';', $metadata);
  43. while (!empty($metas)) {
  44. $cur = array_shift($metas);
  45. if ($cur == 'base64') {
  46. $is_base64 = true;
  47. break;
  48. }
  49. if (substr($cur, 0, 8) == 'charset=') {
  50. // doesn't match if there are arbitrary spaces, but
  51. // whatever dude
  52. if ($charset !== null) {
  53. continue;
  54. } // garbage
  55. $charset = substr($cur, 8); // not used
  56. } else {
  57. if ($content_type !== null) {
  58. continue;
  59. } // garbage
  60. $content_type = $cur;
  61. }
  62. }
  63. } else {
  64. $data = $result[0];
  65. }
  66. if ($content_type !== null && empty($this->allowed_types[$content_type])) {
  67. return false;
  68. }
  69. if ($charset !== null) {
  70. // error; we don't allow plaintext stuff
  71. $charset = null;
  72. }
  73. $data = rawurldecode($data);
  74. if ($is_base64) {
  75. $raw_data = base64_decode($data);
  76. } else {
  77. $raw_data = $data;
  78. }
  79. // XXX probably want to refactor this into a general mechanism
  80. // for filtering arbitrary content types
  81. $file = tempnam("/tmp", "");
  82. file_put_contents($file, $raw_data);
  83. if (function_exists('exif_imagetype')) {
  84. $image_code = exif_imagetype($file);
  85. unlink($file);
  86. } elseif (function_exists('getimagesize')) {
  87. set_error_handler(array($this, 'muteErrorHandler'));
  88. $info = getimagesize($file);
  89. restore_error_handler();
  90. unlink($file);
  91. if ($info == false) {
  92. return false;
  93. }
  94. $image_code = $info[2];
  95. } else {
  96. trigger_error("could not find exif_imagetype or getimagesize functions", E_USER_ERROR);
  97. }
  98. $real_content_type = image_type_to_mime_type($image_code);
  99. if ($real_content_type != $content_type) {
  100. // we're nice guys; if the content type is something else we
  101. // support, change it over
  102. if (empty($this->allowed_types[$real_content_type])) {
  103. return false;
  104. }
  105. $content_type = $real_content_type;
  106. }
  107. // ok, it's kosher, rewrite what we need
  108. $uri->userinfo = null;
  109. $uri->host = null;
  110. $uri->port = null;
  111. $uri->fragment = null;
  112. $uri->query = null;
  113. $uri->path = "$content_type;base64," . base64_encode($raw_data);
  114. return true;
  115. }
  116. /**
  117. * @param int $errno
  118. * @param string $errstr
  119. */
  120. public function muteErrorHandler($errno, $errstr)
  121. {
  122. }
  123. }