No Description

class-jetpack-podcast-feed-locator.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Extension of the SimplePie_Locator class, to detect podcast feeds
  4. *
  5. * @package automattic/jetpack
  6. */
  7. /**
  8. * Class Jetpack_Podcast_Feed_Locator
  9. */
  10. class Jetpack_Podcast_Feed_Locator extends SimplePie_Locator {
  11. /**
  12. * Overrides the locator is_feed function to check for
  13. * appropriate podcast elements.
  14. *
  15. * @param SimplePie_File $file The file being checked.
  16. * @param boolean $check_html Adds text/html to the mimetypes checked.
  17. */
  18. public function is_feed( $file, $check_html = false ) {
  19. return parent::is_feed( $file, $check_html ) && $this->is_podcast_feed( $file );
  20. }
  21. /**
  22. * Checks the contents of the file for elements that make
  23. * it a podcast feed.
  24. *
  25. * @param SimplePie_File $file The file being checked.
  26. */
  27. private function is_podcast_feed( $file ) {
  28. // If we can't read the DOM assume it's a podcast feed, we'll work
  29. // it out later.
  30. if ( ! class_exists( 'DOMDocument' ) ) {
  31. return true;
  32. }
  33. $feed_dom = $this->safely_load_xml( $file->body );
  34. // Do this as either/or but prioritise the itunes namespace. It's pretty likely
  35. // that it's a podcast feed we've found if that namespace is present.
  36. return $feed_dom && $this->has_itunes_ns( $feed_dom ) && $this->has_audio_enclosures( $feed_dom );
  37. }
  38. /**
  39. * Safely loads an XML file
  40. *
  41. * @param string $xml A string of XML to load.
  42. * @return DOMDocument|false A restulting DOM document or `false` if there is an error.
  43. */
  44. private function safely_load_xml( $xml ) {
  45. $disable_entity_loader = PHP_VERSION_ID < 80000;
  46. if ( $disable_entity_loader && ! function_exists( 'libxml_disable_entity_loader' ) ) {
  47. return false;
  48. }
  49. if ( $disable_entity_loader ) {
  50. // This function has been deprecated in PHP 8.0 because in libxml 2.9.0, external entity loading
  51. // is disabled by default, so this function is no longer needed to protect against XXE attacks.
  52. // phpcs:ignore Generic.PHP.DeprecatedFunctions.Deprecated, PHPCompatibility.FunctionUse.RemovedFunctions.libxml_disable_entity_loaderDeprecated
  53. $loader = libxml_disable_entity_loader( true );
  54. }
  55. $errors = libxml_use_internal_errors( true );
  56. $return = new DOMDocument();
  57. if ( ! $return->loadXML( $xml ) ) {
  58. return false;
  59. }
  60. libxml_use_internal_errors( $errors );
  61. if ( $disable_entity_loader && isset( $loader ) ) {
  62. // phpcs:ignore Generic.PHP.DeprecatedFunctions.Deprecated, PHPCompatibility.FunctionUse.RemovedFunctions.libxml_disable_entity_loaderDeprecated
  63. libxml_disable_entity_loader( $loader );
  64. }
  65. return $return;
  66. }
  67. /**
  68. * Checks the RSS feed for the presence of the itunes podcast namespace.
  69. * It's pretty loose and just checks the URI for itunes.com
  70. *
  71. * @param DOMDocument $dom The XML document to check.
  72. * @return boolean Whether the itunes namespace is defined.
  73. */
  74. private function has_itunes_ns( $dom ) {
  75. $xpath = new DOMXPath( $dom );
  76. foreach ( $xpath->query( 'namespace::*' ) as $node ) {
  77. // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
  78. // nodeValue is not valid, but it's part of the DOM API that we don't control.
  79. if ( strstr( $node->nodeValue, 'itunes.com' ) ) {
  80. return true;
  81. }
  82. // phpcs:enable
  83. }
  84. return false;
  85. }
  86. /**
  87. * Checks the RSS feed for the presence of enclosures with an audio mimetype.
  88. *
  89. * @param DOMDocument $dom The XML document to check.
  90. * @return boolean Whether enclosures were found.
  91. */
  92. private function has_audio_enclosures( $dom ) {
  93. $xpath = new DOMXPath( $dom );
  94. $enclosures = $xpath->query( "//enclosure[starts-with(@type,'audio/')]" );
  95. return ! $enclosures ? false : $enclosures->length > 0;
  96. }
  97. }