PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-includes/class-oembed.php

https://bitbucket.org/dkrzos/phc
PHP | 321 lines | 170 code | 44 blank | 107 comment | 44 complexity | 5e9f6ef43e5e770cb0dc936dec6e88e8 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * API for fetching the HTML to embed remote content based on a provided URL.
  4. * Used internally by the {@link WP_Embed} class, but is designed to be generic.
  5. *
  6. * @link http://codex.wordpress.org/oEmbed oEmbed Codex Article
  7. * @link http://oembed.com/ oEmbed Homepage
  8. *
  9. * @package WordPress
  10. * @subpackage oEmbed
  11. */
  12. /**
  13. * oEmbed class.
  14. *
  15. * @package WordPress
  16. * @subpackage oEmbed
  17. * @since 2.9.0
  18. */
  19. class WP_oEmbed {
  20. var $providers = array();
  21. /**
  22. * Constructor
  23. *
  24. * @uses apply_filters() Filters a list of pre-defined oEmbed providers.
  25. */
  26. function __construct() {
  27. // List out some popular sites that support oEmbed.
  28. // The WP_Embed class disables discovery for non-unfiltered_html users, so only providers in this array will be used for them.
  29. // Add to this list using the wp_oembed_add_provider() function (see its PHPDoc for details).
  30. $this->providers = apply_filters( 'oembed_providers', array(
  31. '#https?://(www\.)?youtube.com/watch.*#i' => array( 'http://www.youtube.com/oembed', true ),
  32. 'http://youtu.be/*' => array( 'http://www.youtube.com/oembed', false ),
  33. 'http://blip.tv/*' => array( 'http://blip.tv/oembed/', false ),
  34. '#https?://(www\.)?vimeo\.com/.*#i' => array( 'http://vimeo.com/api/oembed.{format}', true ),
  35. '#https?://(www\.)?dailymotion\.com/.*#i' => array( 'http://www.dailymotion.com/services/oembed', true ),
  36. '#https?://(www\.)?flickr\.com/.*#i' => array( 'http://www.flickr.com/services/oembed/', true ),
  37. '#https?://(.+\.)?smugmug\.com/.*#i' => array( 'http://api.smugmug.com/services/oembed/', true ),
  38. '#https?://(www\.)?hulu\.com/watch/.*#i' => array( 'http://www.hulu.com/api/oembed.{format}', true ),
  39. '#https?://(www\.)?viddler\.com/.*#i' => array( 'http://lab.viddler.com/services/oembed/', true ),
  40. 'http://qik.com/*' => array( 'http://qik.com/api/oembed.{format}', false ),
  41. 'http://revision3.com/*' => array( 'http://revision3.com/api/oembed/', false ),
  42. 'http://i*.photobucket.com/albums/*' => array( 'http://photobucket.com/oembed', false ),
  43. 'http://gi*.photobucket.com/groups/*' => array( 'http://photobucket.com/oembed', false ),
  44. '#https?://(www\.)?scribd\.com/.*#i' => array( 'http://www.scribd.com/services/oembed', true ),
  45. 'http://wordpress.tv/*' => array( 'http://wordpress.tv/oembed/', false ),
  46. '#https?://(.+\.)?polldaddy\.com/.*#i' => array( 'http://polldaddy.com/oembed/', true ),
  47. '#https?://(www\.)?funnyordie\.com/videos/.*#i' => array( 'http://www.funnyordie.com/oembed', true ),
  48. '#https?://(www\.)?twitter.com/.+?/status(es)?/.*#i' => array( 'http://api.twitter.com/1/statuses/oembed.{format}', true ),
  49. '#https?://(www\.)?soundcloud\.com/.*#i' => array( 'http://soundcloud.com/oembed', true ),
  50. '#https?://(www\.)?slideshare.net/*#' => array( 'http://www.slideshare.net/api/oembed/2', true ),
  51. '#http://instagr(\.am|am\.com)/p/.*#i' => array( 'http://api.instagram.com/oembed', true ),
  52. ) );
  53. // Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop().
  54. add_filter( 'oembed_dataparse', array($this, '_strip_newlines'), 10, 3 );
  55. }
  56. /**
  57. * The do-it-all function that takes a URL and attempts to return the HTML.
  58. *
  59. * @see WP_oEmbed::discover()
  60. * @see WP_oEmbed::fetch()
  61. * @see WP_oEmbed::data2html()
  62. *
  63. * @param string $url The URL to the content that should be attempted to be embedded.
  64. * @param array $args Optional arguments. Usually passed from a shortcode.
  65. * @return bool|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
  66. */
  67. function get_html( $url, $args = '' ) {
  68. $provider = false;
  69. if ( !isset($args['discover']) )
  70. $args['discover'] = true;
  71. foreach ( $this->providers as $matchmask => $data ) {
  72. list( $providerurl, $regex ) = $data;
  73. // Turn the asterisk-type provider URLs into regex
  74. if ( !$regex ) {
  75. $matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i';
  76. $matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask );
  77. }
  78. if ( preg_match( $matchmask, $url ) ) {
  79. $provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML
  80. break;
  81. }
  82. }
  83. if ( !$provider && $args['discover'] )
  84. $provider = $this->discover( $url );
  85. if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) )
  86. return false;
  87. return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
  88. }
  89. /**
  90. * Attempts to find oEmbed provider discovery <link> tags at the given URL.
  91. *
  92. * @param string $url The URL that should be inspected for discovery <link> tags.
  93. * @return bool|string False on failure, otherwise the oEmbed provider URL.
  94. */
  95. function discover( $url ) {
  96. $providers = array();
  97. // Fetch URL content
  98. if ( $html = wp_remote_retrieve_body( wp_remote_get( $url, array( 'reject_unsafe_urls' => true ) ) ) ) {
  99. // <link> types that contain oEmbed provider URLs
  100. $linktypes = apply_filters( 'oembed_linktypes', array(
  101. 'application/json+oembed' => 'json',
  102. 'text/xml+oembed' => 'xml',
  103. 'application/xml+oembed' => 'xml', // Incorrect, but used by at least Vimeo
  104. ) );
  105. // Strip <body>
  106. $html = substr( $html, 0, stripos( $html, '</head>' ) );
  107. // Do a quick check
  108. $tagfound = false;
  109. foreach ( $linktypes as $linktype => $format ) {
  110. if ( stripos($html, $linktype) ) {
  111. $tagfound = true;
  112. break;
  113. }
  114. }
  115. if ( $tagfound && preg_match_all( '/<link([^<>]+)>/i', $html, $links ) ) {
  116. foreach ( $links[1] as $link ) {
  117. $atts = shortcode_parse_atts( $link );
  118. if ( !empty($atts['type']) && !empty($linktypes[$atts['type']]) && !empty($atts['href']) ) {
  119. $providers[$linktypes[$atts['type']]] = $atts['href'];
  120. // Stop here if it's JSON (that's all we need)
  121. if ( 'json' == $linktypes[$atts['type']] )
  122. break;
  123. }
  124. }
  125. }
  126. }
  127. // JSON is preferred to XML
  128. if ( !empty($providers['json']) )
  129. return $providers['json'];
  130. elseif ( !empty($providers['xml']) )
  131. return $providers['xml'];
  132. else
  133. return false;
  134. }
  135. /**
  136. * Connects to a oEmbed provider and returns the result.
  137. *
  138. * @param string $provider The URL to the oEmbed provider.
  139. * @param string $url The URL to the content that is desired to be embedded.
  140. * @param array $args Optional arguments. Usually passed from a shortcode.
  141. * @return bool|object False on failure, otherwise the result in the form of an object.
  142. */
  143. function fetch( $provider, $url, $args = '' ) {
  144. $args = wp_parse_args( $args, wp_embed_defaults() );
  145. $provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider );
  146. $provider = add_query_arg( 'maxheight', (int) $args['height'], $provider );
  147. $provider = add_query_arg( 'url', urlencode($url), $provider );
  148. $provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args );
  149. foreach( array( 'json', 'xml' ) as $format ) {
  150. $result = $this->_fetch_with_format( $provider, $format );
  151. if ( is_wp_error( $result ) && 'not-implemented' == $result->get_error_code() )
  152. continue;
  153. return ( $result && ! is_wp_error( $result ) ) ? $result : false;
  154. }
  155. return false;
  156. }
  157. /**
  158. * Fetches result from an oEmbed provider for a specific format and complete provider URL
  159. *
  160. * @since 3.0.0
  161. * @access private
  162. * @param string $provider_url_with_args URL to the provider with full arguments list (url, maxheight, etc.)
  163. * @param string $format Format to use
  164. * @return bool|object False on failure, otherwise the result in the form of an object.
  165. */
  166. function _fetch_with_format( $provider_url_with_args, $format ) {
  167. $provider_url_with_args = add_query_arg( 'format', $format, $provider_url_with_args );
  168. $response = wp_remote_get( $provider_url_with_args, array( 'reject_unsafe_urls' => true ) );
  169. if ( 501 == wp_remote_retrieve_response_code( $response ) )
  170. return new WP_Error( 'not-implemented' );
  171. if ( ! $body = wp_remote_retrieve_body( $response ) )
  172. return false;
  173. $parse_method = "_parse_$format";
  174. return $this->$parse_method( $body );
  175. }
  176. /**
  177. * Parses a json response body.
  178. *
  179. * @since 3.0.0
  180. * @access private
  181. */
  182. function _parse_json( $response_body ) {
  183. return ( ( $data = json_decode( trim( $response_body ) ) ) && is_object( $data ) ) ? $data : false;
  184. }
  185. /**
  186. * Parses an XML response body.
  187. *
  188. * @since 3.0.0
  189. * @access private
  190. */
  191. function _parse_xml( $response_body ) {
  192. if ( !function_exists('simplexml_load_string') ) {
  193. return false;
  194. }
  195. if ( ! function_exists( 'libxml_disable_entity_loader' ) )
  196. return false;
  197. $loader = libxml_disable_entity_loader( true );
  198. $errors = libxml_use_internal_errors( true );
  199. $data = simplexml_load_string( $response_body );
  200. libxml_use_internal_errors( $errors );
  201. $return = false;
  202. if ( is_object( $data ) ) {
  203. $return = new stdClass;
  204. foreach ( $data as $key => $value ) {
  205. $return->$key = (string) $value;
  206. }
  207. }
  208. libxml_disable_entity_loader( $loader );
  209. return $return;
  210. }
  211. /**
  212. * Converts a data object from {@link WP_oEmbed::fetch()} and returns the HTML.
  213. *
  214. * @param object $data A data object result from an oEmbed provider.
  215. * @param string $url The URL to the content that is desired to be embedded.
  216. * @return bool|string False on error, otherwise the HTML needed to embed.
  217. */
  218. function data2html( $data, $url ) {
  219. if ( ! is_object( $data ) || empty( $data->type ) )
  220. return false;
  221. $return = false;
  222. switch ( $data->type ) {
  223. case 'photo':
  224. if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) )
  225. break;
  226. if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) )
  227. break;
  228. $title = ! empty( $data->title ) && is_string( $data->title ) ? $data->title : '';
  229. $return = '<a href="' . esc_url( $url ) . '"><img src="' . esc_url( $data->url ) . '" alt="' . esc_attr($title) . '" width="' . esc_attr($data->width) . '" height="' . esc_attr($data->height) . '" /></a>';
  230. break;
  231. case 'video':
  232. case 'rich':
  233. if ( ! empty( $data->html ) && is_string( $data->html ) )
  234. $return = $data->html;
  235. break;
  236. case 'link':
  237. if ( ! empty( $data->title ) && is_string( $data->title ) )
  238. $return = '<a href="' . esc_url( $url ) . '">' . esc_html( $data->title ) . '</a>';
  239. break;
  240. default:
  241. $return = false;
  242. }
  243. // You can use this filter to add support for custom data types or to filter the result
  244. return apply_filters( 'oembed_dataparse', $return, $data, $url );
  245. }
  246. /**
  247. * Strip any new lines from the HTML.
  248. *
  249. * @access private
  250. * @param string $html Existing HTML.
  251. * @param object $data Data object from WP_oEmbed::data2html()
  252. * @param string $url The original URL passed to oEmbed.
  253. * @return string Possibly modified $html
  254. */
  255. function _strip_newlines( $html, $data, $url ) {
  256. if ( false !== strpos( $html, "\n" ) )
  257. $html = str_replace( array( "\r\n", "\n" ), '', $html );
  258. return $html;
  259. }
  260. }
  261. /**
  262. * Returns the initialized {@link WP_oEmbed} object
  263. *
  264. * @since 2.9.0
  265. * @access private
  266. *
  267. * @see WP_oEmbed
  268. * @uses WP_oEmbed
  269. *
  270. * @return WP_oEmbed object.
  271. */
  272. function _wp_oembed_get_object() {
  273. static $wp_oembed;
  274. if ( is_null($wp_oembed) )
  275. $wp_oembed = new WP_oEmbed();
  276. return $wp_oembed;
  277. }