safe-svg.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. /*
  3. Plugin Name: Safe SVG
  4. Plugin URI: https://wpsvg.com/
  5. Description: Allows SVG uploads into WordPress and sanitizes the SVG before saving it
  6. Version: 1.6.1
  7. Author: Daryll Doyle
  8. Author URI: http://enshrined.co.uk
  9. Text Domain: safe-svg
  10. Domain Path: /languages
  11. */
  12. defined( 'ABSPATH' ) or die( 'Really?' );
  13. require 'lib/vendor/autoload.php';
  14. if ( ! class_exists( 'safe_svg' ) ) {
  15. /**
  16. * Class safe_svg
  17. */
  18. Class safe_svg {
  19. /**
  20. * The sanitizer
  21. *
  22. * @var \enshrined\svgSanitize\Sanitizer
  23. */
  24. protected $sanitizer;
  25. /**
  26. * Set up the class
  27. */
  28. function __construct() {
  29. $this->sanitizer = new enshrined\svgSanitize\Sanitizer();
  30. $this->sanitizer->minify( true );
  31. add_filter( 'upload_mimes', array( $this, 'allow_svg' ) );
  32. add_filter( 'wp_handle_upload_prefilter', array( $this, 'check_for_svg' ) );
  33. add_filter( 'wp_check_filetype_and_ext', array( $this, 'fix_mime_type_svg' ), 75, 4 );
  34. add_filter( 'wp_prepare_attachment_for_js', array( $this, 'fix_admin_preview' ), 10, 3 );
  35. add_filter( 'wp_get_attachment_image_src', array( $this, 'one_pixel_fix' ), 10, 4 );
  36. add_filter( 'admin_post_thumbnail_html', array( $this, 'featured_image_fix' ), 10, 3 );
  37. add_action( 'admin_enqueue_scripts', array( $this, 'load_custom_admin_style' ) );
  38. add_action( 'get_image_tag', array( $this, 'get_image_tag_override' ), 10, 6 );
  39. add_filter( 'wp_generate_attachment_metadata', array( $this, 'skip_svg_regeneration' ), 10, 2 );
  40. add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_upgrade_link' ) );
  41. add_filter( 'wp_get_attachment_metadata', array( $this, 'metadata_error_fix' ), 10, 2 );
  42. }
  43. /**
  44. * Allow SVG Uploads
  45. *
  46. * @param $mimes
  47. *
  48. * @return mixed
  49. */
  50. public function allow_svg( $mimes ) {
  51. $mimes['svg'] = 'image/svg+xml';
  52. $mimes['svgz'] = 'image/svg+xml';
  53. return $mimes;
  54. }
  55. /**
  56. * Fixes the issue in WordPress 4.7.1 being unable to correctly identify SVGs
  57. *
  58. * @thanks @lewiscowles
  59. *
  60. * @param null $data
  61. * @param null $file
  62. * @param null $filename
  63. * @param null $mimes
  64. *
  65. * @return null
  66. */
  67. public function fix_mime_type_svg( $data = null, $file = null, $filename = null, $mimes = null ) {
  68. $ext = isset( $data['ext'] ) ? $data['ext'] : '';
  69. if ( strlen( $ext ) < 1 ) {
  70. $exploded = explode( '.', $filename );
  71. $ext = strtolower( end( $exploded ) );
  72. }
  73. if ( $ext === 'svg' ) {
  74. $data['type'] = 'image/svg+xml';
  75. $data['ext'] = 'svg';
  76. } elseif ( $ext === 'svgz' ) {
  77. $data['type'] = 'image/svg+xml';
  78. $data['ext'] = 'svgz';
  79. }
  80. return $data;
  81. }
  82. /**
  83. * Check if the file is an SVG, if so handle appropriately
  84. *
  85. * @param $file
  86. *
  87. * @return mixed
  88. */
  89. public function check_for_svg( $file ) {
  90. if ( $file['type'] === 'image/svg+xml' ) {
  91. if ( ! $this->sanitize( $file['tmp_name'] ) ) {
  92. $file['error'] = __( "Sorry, this file couldn't be sanitized so for security reasons wasn't uploaded",
  93. 'safe-svg' );
  94. }
  95. }
  96. return $file;
  97. }
  98. /**
  99. * Sanitize the SVG
  100. *
  101. * @param $file
  102. *
  103. * @return bool|int
  104. */
  105. protected function sanitize( $file ) {
  106. $dirty = file_get_contents( $file );
  107. // Is the SVG gzipped? If so we try and decode the string
  108. if ( $is_zipped = $this->is_gzipped( $dirty ) ) {
  109. $dirty = gzdecode( $dirty );
  110. // If decoding fails, bail as we're not secure
  111. if ( $dirty === false ) {
  112. return false;
  113. }
  114. }
  115. $clean = $this->sanitizer->sanitize( $dirty );
  116. if ( $clean === false ) {
  117. return false;
  118. }
  119. // If we were gzipped, we need to re-zip
  120. if ( $is_zipped ) {
  121. $clean = gzencode( $clean );
  122. }
  123. file_put_contents( $file, $clean );
  124. return true;
  125. }
  126. /**
  127. * Check if the contents are gzipped
  128. *
  129. * @see http://www.gzip.org/zlib/rfc-gzip.html#member-format
  130. *
  131. * @param $contents
  132. *
  133. * @return bool
  134. */
  135. protected function is_gzipped( $contents ) {
  136. if ( function_exists( 'mb_strpos' ) ) {
  137. return 0 === mb_strpos( $contents, "\x1f" . "\x8b" . "\x08" );
  138. } else {
  139. return 0 === strpos( $contents, "\x1f" . "\x8b" . "\x08" );
  140. }
  141. }
  142. /**
  143. * Filters the attachment data prepared for JavaScript to add the sizes array to the response
  144. *
  145. * @param array $response Array of prepared attachment data.
  146. * @param int|object $attachment Attachment ID or object.
  147. * @param array $meta Array of attachment meta data.
  148. *
  149. * @return array
  150. */
  151. public function fix_admin_preview( $response, $attachment, $meta ) {
  152. if ( $response['mime'] == 'image/svg+xml' ) {
  153. $possible_sizes = apply_filters( 'image_size_names_choose', array(
  154. 'thumbnail' => __( 'Thumbnail' ),
  155. 'medium' => __( 'Medium' ),
  156. 'large' => __( 'Large' ),
  157. 'full' => __( 'Full Size' ),
  158. ) );
  159. $sizes = array();
  160. foreach ( $possible_sizes as $size => $label ) {
  161. $sizes[ $size ] = array(
  162. 'height' => get_option( "{$size}_size_w", 2000 ),
  163. 'width' => get_option( "{$size}_size_h", 2000 ),
  164. 'url' => $response['url'],
  165. 'orientation' => 'portrait',
  166. );
  167. }
  168. $response['sizes'] = $sizes;
  169. $response['icon'] = $response['url'];
  170. }
  171. return $response;
  172. }
  173. /**
  174. * Filters the image src result.
  175. * Here we're gonna spoof the image size and set it to 100 width and height
  176. *
  177. * @param array|false $image Either array with src, width & height, icon src, or false.
  178. * @param int $attachment_id Image attachment ID.
  179. * @param string|array $size Size of image. Image size or array of width and height values
  180. * (in that order). Default 'thumbnail'.
  181. * @param bool $icon Whether the image should be treated as an icon. Default false.
  182. *
  183. * @return array
  184. */
  185. public function one_pixel_fix( $image, $attachment_id, $size, $icon ) {
  186. if ( get_post_mime_type( $attachment_id ) == 'image/svg+xml' ) {
  187. $image['1'] = false;
  188. $image['2'] = false;
  189. }
  190. return $image;
  191. }
  192. /**
  193. * If the featured image is an SVG we wrap it in an SVG class so we can apply our CSS fix.
  194. *
  195. * @param string $content Admin post thumbnail HTML markup.
  196. * @param int $post_id Post ID.
  197. * @param int $thumbnail_id Thumbnail ID.
  198. *
  199. * @return string
  200. */
  201. public function featured_image_fix( $content, $post_id, $thumbnail_id ) {
  202. $mime = get_post_mime_type( $thumbnail_id );
  203. if ( 'image/svg+xml' === $mime ) {
  204. $content = sprintf( '<span class="svg">%s</span>', $content );
  205. }
  206. return $content;
  207. }
  208. /**
  209. * Load our custom CSS sheet.
  210. */
  211. function load_custom_admin_style() {
  212. wp_enqueue_style( 'safe-svg-css', plugins_url( 'assets/safe-svg.css', __FILE__ ), array() );
  213. }
  214. /**
  215. * Override the default height and width string on an SVG
  216. *
  217. * @param string $html HTML content for the image.
  218. * @param int $id Attachment ID.
  219. * @param string $alt Alternate text.
  220. * @param string $title Attachment title.
  221. * @param string $align Part of the class name for aligning the image.
  222. * @param string|array $size Size of image. Image size or array of width and height values (in that order).
  223. * Default 'medium'.
  224. *
  225. * @return mixed
  226. */
  227. function get_image_tag_override( $html, $id, $alt, $title, $align, $size ) {
  228. $mime = get_post_mime_type( $id );
  229. if ( 'image/svg+xml' === $mime ) {
  230. if( is_array( $size ) ) {
  231. $width = $size[0];
  232. $height = $size[1];
  233. } else {
  234. $width = get_option( "{$size}_size_w", false );
  235. $height = get_option( "{$size}_size_h", false );
  236. }
  237. if( $height && $width ) {
  238. $html = str_replace( 'width="1" ', sprintf( 'width="%s" ', $width ), $html );
  239. $html = str_replace( 'height="1" ', sprintf( 'height="%s" ', $height ), $html );
  240. } else {
  241. $html = str_replace( 'width="1" ', '', $html );
  242. $html = str_replace( 'height="1" ', '', $html );
  243. }
  244. }
  245. return $html;
  246. }
  247. /**
  248. * Skip regenerating SVGs
  249. *
  250. * @param int $attachment_id Attachment Id to process.
  251. * @param string $file Filepath of the Attached image.
  252. *
  253. * @return mixed Metadata for attachment.
  254. */
  255. function skip_svg_regeneration( $metadata, $attachment_id ) {
  256. if ( 'image/svg+xml' === get_post_mime_type( $attachment_id ) ) {
  257. // return new WP_Error( 'skip_svg_generate', __( 'Skipping SVG file.', 'safe-svg' ) );
  258. }
  259. return $metadata;
  260. }
  261. /**
  262. * Add in an upgrade link for Safe SVG
  263. *
  264. * @param $links
  265. *
  266. * @return array
  267. */
  268. function add_upgrade_link( $links ) {
  269. $mylinks = array(
  270. '<a target="_blank" style="color:#3db634;" href="https://wpsvg.com/?utm_source=plugin-list&utm_medium=upgrade-link&utm_campaign=plugin-list&utm_content=action-link">Upgrade</a>',
  271. );
  272. return array_merge( $links, $mylinks );
  273. }
  274. /**
  275. * Filters the attachment meta data.
  276. *
  277. * @param array|bool $data Array of meta data for the given attachment, or false
  278. * if the object does not exist.
  279. * @param int $post_id Attachment ID.
  280. */
  281. function metadata_error_fix( $data, $post_id ) {
  282. // If it's a WP_Error regenerate metadata and save it
  283. if ( is_wp_error( $data ) ) {
  284. $data = wp_generate_attachment_metadata( $post_id, get_attached_file( $post_id ) );
  285. wp_update_attachment_metadata( $post_id, $data );
  286. }
  287. return $data;
  288. }
  289. }
  290. }
  291. $safe_svg = new safe_svg();