PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/broken-link-checker/modules/parsers/image.php

https://bitbucket.org/lgorence/quickpress
PHP | 175 lines | 84 code | 27 blank | 64 comment | 15 complexity | 772481c72a1a6ea1da71aebccde56b1b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /*
  3. Plugin Name: HTML images
  4. Description: e.g. <code>&lt;img src="http://example.com/fluffy.jpg"&gt;</code>
  5. Version: 1.0
  6. Author: Janis Elsts
  7. ModuleID: image
  8. ModuleCategory: parser
  9. ModuleClassName: blcHTMLImage
  10. ModuleContext: on-demand
  11. ModuleLazyInit: true
  12. ModulePriority: 900
  13. */
  14. //TODO: Update image parser to use the same HTML tag parsing routine as the HTML link parser.
  15. class blcHTMLImage extends blcParser {
  16. var $supported_formats = array('html');
  17. // \1 \2 \3 URL \4
  18. var $img_pattern = '/(<img[\s]+[^>]*src\s*=\s*)([\"\'])([^>]+?)\2([^<>]*>)/i';
  19. /**
  20. * Parse a string for HTML images - <img src="URL">
  21. *
  22. * @param string $content The text to parse.
  23. * @param string $base_url The base URL to use for normalizing relative URLs. If ommitted, the blog's root URL will be used.
  24. * @param string $default_link_text
  25. * @return array An array of new blcLinkInstance objects. The objects will include info about the links found, but not about the corresponding container entity.
  26. */
  27. function parse($content, $base_url = '', $default_link_text = ''){
  28. $instances = array();
  29. //remove all <code></code> blocks first
  30. $content = preg_replace('/<code[^>]*>.+?<\/code>/si', ' ', $content);
  31. //Find images
  32. if(preg_match_all($this->img_pattern, $content, $matches, PREG_SET_ORDER)){
  33. foreach($matches as $link){
  34. $url = $raw_url = $link[3];
  35. //FB::log($url, "Found image");
  36. //Decode &amp; and other entities
  37. $url = html_entity_decode($url);
  38. $url = trim($url);
  39. //Allow shortcodes in image URLs.
  40. $url = do_shortcode($url);
  41. //Attempt to parse the URL
  42. $parts = @parse_url($url);
  43. if(!$parts) {
  44. continue; //Skip invalid URLs
  45. };
  46. if ( !isset($parts['scheme']) ){
  47. //No sheme - likely a relative URL. Turn it into an absolute one.
  48. $url = $this->relative2absolute($url, $base_url);
  49. }
  50. //Skip invalid URLs (again)
  51. if ( !$url || (strlen($url)<6) ) {
  52. continue;
  53. }
  54. //The URL is okay, create and populate a new link instance.
  55. $instance = new blcLinkInstance();
  56. $instance->set_parser($this);
  57. $instance->raw_url = $raw_url;
  58. $instance->link_text = '';
  59. $link_obj = new blcLink($url); //Creates or loads the link
  60. $instance->set_link($link_obj);
  61. $instances[] = $instance;
  62. }
  63. };
  64. return $instances;
  65. }
  66. /**
  67. * Change all images that have a certain source URL to a new URL.
  68. *
  69. * @param string $content Look for images in this string.
  70. * @param string $new_url Change the images to this URL.
  71. * @param string $old_url The URL to look for.
  72. * @param string $old_raw_url The raw, not-normalized URL of the links to look for. Optional.
  73. *
  74. * @return array|WP_Error If successful, the return value will be an associative array with two
  75. * keys : 'content' - the modified content, and 'raw_url' - the new raw, non-normalized URL used
  76. * for the modified images. In most cases, the returned raw_url will be equal to the new_url.
  77. */
  78. function edit($content, $new_url, $old_url, $old_raw_url){
  79. if ( empty($old_raw_url) ){
  80. $old_raw_url = $old_url;
  81. }
  82. //Save the old & new URLs for use in the regex callback.
  83. $this->old_url = $old_raw_url;
  84. $this->new_url = htmlentities($new_url);
  85. //Find all images and replace those that match $old_url.
  86. $content = preg_replace_callback($this->img_pattern, array(&$this, 'edit_callback'), $content);
  87. return array(
  88. 'content' => $content,
  89. 'raw_url' => $this->new_url,
  90. );
  91. }
  92. function edit_callback($matches){
  93. $url = $matches[3];
  94. if ($url == $this->old_url){
  95. return $matches[1].'"'.$this->new_url.'"'.$matches[4];
  96. } else {
  97. return $matches[0];
  98. }
  99. }
  100. /**
  101. * Remove all images that have a certain URL.
  102. *
  103. * @param string $content Look for images in this string.
  104. * @param string $url The URL to look for.
  105. * @param string $raw_url The raw, non-normalized version of the URL to look for. Optional.
  106. * @return string Input string with all matching images removed.
  107. */
  108. function unlink($content, $url, $raw_url){
  109. if ( empty($raw_url) ){
  110. $raw_url = $url;
  111. }
  112. $this->old_url = $raw_url; //used by the callback
  113. $content = preg_replace_callback($this->img_pattern, array(&$this, 'unlink_callback'), $content);
  114. return $content;
  115. }
  116. function unlink_callback($matches){
  117. $url = $matches[3];
  118. //Does the URL match?
  119. if ($url == $this->old_url){
  120. return ''; //Completely remove the IMG tag
  121. } else {
  122. return $matches[0]; //return the image unchanged
  123. }
  124. }
  125. /**
  126. * Get the link text for printing in the "Broken Links" table.
  127. * Sub-classes should override this method and display the link text in a way appropriate for the link type.
  128. *
  129. * @param blcLinkInstance $instance
  130. * @param string $context
  131. * @return string HTML
  132. */
  133. function ui_get_link_text($instance, $context = 'display'){
  134. $text = __('Image', 'broken-link-checker');
  135. $image = sprintf(
  136. '<img src="%s" class="blc-small-image" alt="%2$s" title="%2$s"> ',
  137. esc_attr(plugins_url('/images/image.png', BLC_PLUGIN_FILE)),
  138. esc_attr($text)
  139. );
  140. if ( $context != 'email' ){
  141. $text = $image . $text;
  142. }
  143. return $text;
  144. }
  145. }
  146. ?>