Guest User

Image.php

a guest
Jan 4th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. <?php
  2.  
  3. // +----------------------------------------------------------------------+
  4. // | Copyright Incsub (http://incsub.com/) |
  5. // +----------------------------------------------------------------------+
  6. // | This program is free software; you can redistribute it and/or modify |
  7. // | it under the terms of the GNU General Public License, version 2, as |
  8. // | published by the Free Software Foundation. |
  9. // | |
  10. // | This program is distributed in the hope that it will be useful, |
  11. // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  12. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  13. // | GNU General Public License for more details. |
  14. // | |
  15. // | You should have received a copy of the GNU General Public License |
  16. // | along with this program; if not, write to the Free Software |
  17. // | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
  18. // | MA 02110-1301 USA |
  19. // +----------------------------------------------------------------------+
  20.  
  21. require_once ABSPATH . 'wp-admin/includes/media.php';
  22. require_once ABSPATH . 'wp-admin/includes/file.php';
  23. require_once ABSPATH . 'wp-admin/includes/image.php';
  24.  
  25. /**
  26. * Base class for autoblog images related addon.
  27. *
  28. * @category Autoblog
  29. * @package Addon
  30. *
  31. * @since 4.0.0
  32. */
  33. class Autoblog_Addon_Image extends Autoblog_Addon {
  34.  
  35. /**
  36. * Returns remote images from content.
  37. *
  38. * @since 4.0.0
  39. *
  40. * @access protected
  41. *
  42. * @param string $content The feed item content.
  43. *
  44. * @return array The array of remote images.
  45. */
  46. protected function _get_remote_images_from_content( $content ) {
  47. $images = $matches = array();
  48. $siteurl = parse_url( get_option( 'siteurl' ) );
  49.  
  50. if ( preg_match_all( '#(?:<img.*?src=[\'"](.*?)[\'"].*?>)|(http(?:[^\s]+)\.(?:jpe?g|gif|png))#is', $content, $matches ) ) {
  51. $matchKeep = [];
  52. if (!empty($matches[1][0]) && empty($matches[2][0])) {
  53. $matchKeep = $matches[1];
  54. }elseif (!empty($matches[2][0]) && empty($matches[1][0])) {
  55. $matchKeep = $matches[2];
  56. }
  57.  
  58. foreach ( $matchKeep as $url ) {
  59. $url = str_replace( ' ', '%20', current( explode( '?', $url, 2 ) ) );
  60. $purl = autoblog_parse_mb_url( $url );
  61. if ( ! isset( $purl['host'] ) || $purl['host'] != $siteurl['host'] && preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)/i', $url ) ) {
  62. // we seem to have an external images
  63. $images[] = $url;
  64. }
  65. }
  66. }
  67.  
  68. return $images;
  69. }
  70.  
  71. /**
  72. * @param $content
  73. *
  74. * @return array
  75. */
  76. protected function _get_remote_images_from_post_content( $content ) {
  77. $images = $matches = array();
  78. $siteurl = parse_url( get_option( 'siteurl' ) );
  79. //look up images by DOM
  80. $doc = new DOMDocument();
  81. $can_use_dom = @$doc->loadHTML( $content );
  82. if ( $can_use_dom == true ) {
  83. $tags = $doc->getElementsByTagName( 'img' );
  84. foreach ( $tags as $tag ) {
  85. $url = $tag->getAttribute( 'src' );
  86. $url = trim( $url );
  87. if ( ! empty( $url ) ) {
  88. $url = html_entity_decode( rawurldecode( $url ) );
  89. //no resouce url please
  90. if ( strpos( $url, '//' ) === 0 ) {
  91. $url = 'http:' . $url;
  92. }
  93. $purl = autoblog_parse_mb_url( $url );
  94. if ( ! isset( $purl['host'] ) || $purl['host'] != $siteurl['host'] ) {
  95. $images[] = $url;
  96. }
  97. }
  98. }
  99. } else {
  100. //can not parse DOM, use regex
  101. $images = $this->_get_remote_images_from_content( $content );
  102. }
  103.  
  104. return $images;
  105. }
  106.  
  107. /**
  108. * @param $url
  109. *
  110. * @return bool
  111. */
  112. protected function validate_images( $url ) {
  113. //validate images
  114. $sizes = @getimagesize( $url );
  115. if ( is_array( $sizes ) ) {
  116. $type = current( explode( '/', $sizes['mime'], 2 ) );
  117. if ( in_array( $sizes['mime'], get_allowed_mime_types() ) && $type == 'image' ) {
  118. return $url;
  119. }
  120. }
  121.  
  122. return false;
  123. }
  124.  
  125. /**
  126. * Grabs remote image.
  127. *
  128. * @since 4.0.0
  129. *
  130. * @access protected
  131. *
  132. * @param string $image The image URL.
  133. * @param int $post_id The post id to attach the image to.
  134. *
  135. * @return string|boolean Local image URL on success, otherwise FALSE.
  136. */
  137. protected function _download_image( $image, $post_id ) {
  138. $query = new WP_Query( array(
  139. 'post_type' => 'attachment',
  140. 'post_status' => 'inherit',
  141. 'meta_query' => array(
  142. array(
  143. 'key' => 'autoblog_orig_image',
  144. 'value' => $image,
  145. 'compare' => '='
  146. )
  147. )
  148. ) );
  149.  
  150. if ( $query->have_posts() ) {
  151. return $query->next_post()->ID;
  152. }
  153.  
  154. // Download file to temp location
  155. $tmp = download_url( $image );
  156. if ( is_wp_error( $tmp ) ) {
  157. return false;
  158. }
  159.  
  160. // Set variables for storage, fix file filename for query strings
  161. $matches = array();
  162. preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $image, $matches );
  163. $file_array['name'] = $this->_generate_image_name($image);
  164. $file_array['tmp_name'] = $tmp;
  165.  
  166. // do the validation and storage stuff
  167. $post = get_post( $post_id );
  168. $image_id = media_handle_sideload( $file_array, $post_id, null, array( 'post_author' => $post->post_author ) );
  169. if ( is_wp_error( $image_id ) ) {
  170. @unlink( $file_array['tmp_name'] );
  171. } else {
  172. add_post_meta( $image_id, 'autoblog_orig_image', $image );
  173. }
  174.  
  175. return $image_id;
  176. }
  177.  
  178. protected function _generate_image_name( $image ) {
  179. $matches = array();
  180. $file_name = '';
  181. preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $image, $matches );
  182. if ( is_array( $matches ) && count( $matches ) ) {
  183. $file_name = str_replace( '%20', '-', basename( $matches[0] ) );
  184. } else {
  185. $sizes = getimagesize( $image );
  186. if ( is_array( $sizes ) && isset( $sizes['mime'] ) && ! empty( $sizes['mime'] ) ) {
  187. $ext = image_type_to_extension( $sizes[2] );
  188.  
  189. $file_name = substr( sanitize_title( pathinfo( $image, PATHINFO_FILENAME ) ), 0, 254 ) . $ext;
  190. }
  191. }
  192.  
  193. return $file_name;
  194.  
  195. }
  196.  
  197. }
Add Comment
Please, Sign In to add comment