]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - inc/3rdparty/makefulltextfeed.php
update to 3.2 version of full-text-rss, issue #694
[github/wallabag/wallabag.git] / inc / 3rdparty / makefulltextfeed.php
index 4faad6d941904c5526bd4436d1793c4a52bb7b53..7a56be8c7c967cec629d0129feb3bb72d4ee8249 100755 (executable)
@@ -3,8 +3,8 @@
 // Author: Keyvan Minoukadeh\r
 // Copyright (c) 2013 Keyvan Minoukadeh\r
 // License: AGPLv3\r
-// Version: 3.1\r
-// Date: 2013-03-05\r
+// Version: 3.2\r
+// Date: 2013-05-13\r
 // More info: http://fivefilters.org/content-only/\r
 // Help: http://help.fivefilters.org\r
 \r
@@ -25,12 +25,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 \r
 // Usage\r
 // -----\r
-// Request this file passing it your feed in the querystring: makefulltextfeed.php?url=mysite.org\r
-// The following options can be passed in the querystring:\r
-// * URL: url=[feed or website url] (required, should be URL-encoded - in php: urlencode($url))\r
-// * URL points to HTML (not feed): html=true (optional, by default it's automatically detected)\r
-// * API key: key=[api key] (optional, refer to config.php)\r
-// * Max entries to process: max=[max number of items] (optional)\r
+// Request this file passing it a web page or feed URL in the querystring: makefulltextfeed.php?url=example.org/article\r
+// For more request parameters, see http://help.fivefilters.org/customer/portal/articles/226660-usage\r
 \r
 error_reporting(E_ALL ^ E_NOTICE);\r
 ini_set("display_errors", 1);\r
@@ -76,8 +72,8 @@ header('X-Robots-Tag: noindex, nofollow');
 ////////////////////////////////\r
 // Check if service is enabled\r
 ////////////////////////////////\r
-if (!$options->enabled) { \r
-       die('The full-text RSS service is currently disabled'); \r
+if (!$options->enabled) {\r
+       die('The full-text RSS service is currently disabled');\r
 }\r
 \r
 ////////////////////////////////\r
@@ -121,8 +117,8 @@ $options->smart_cache = $options->smart_cache && function_exists('apc_inc');
 ////////////////////////////////\r
 // Check for feed URL\r
 ////////////////////////////////\r
-if (!isset($_GET['url'])) { \r
-       die('No URL supplied'); \r
+if (!isset($_GET['url'])) {\r
+       die('No URL supplied');\r
 }\r
 $url = trim($_GET['url']);\r
 if (strtolower(substr($url, 0, 7)) == 'feed://') {\r
@@ -161,10 +157,12 @@ if (isset($_GET['key']) && ($key_index = array_search($_GET['key'], $options->ap
        if (isset($_GET['links'])) $redirect .= '&links='.urlencode($_GET['links']);\r
        if (isset($_GET['exc'])) $redirect .= '&exc='.urlencode($_GET['exc']);\r
        if (isset($_GET['format'])) $redirect .= '&format='.urlencode($_GET['format']);\r
-       if (isset($_GET['callback'])) $redirect .= '&callback='.urlencode($_GET['callback']);   \r
+       if (isset($_GET['callback'])) $redirect .= '&callback='.urlencode($_GET['callback']);\r
        if (isset($_GET['l'])) $redirect .= '&l='.urlencode($_GET['l']);\r
        if (isset($_GET['xss'])) $redirect .= '&xss';\r
        if (isset($_GET['use_extracted_title'])) $redirect .= '&use_extracted_title';\r
+       if (isset($_GET['content'])) $redirect .= '&content='.urlencode($_GET['content']);\r
+       if (isset($_GET['summary'])) $redirect .= '&summary='.urlencode($_GET['summary']);\r
        if (isset($_GET['debug'])) $redirect .= '&debug';\r
        if ($debug_mode) {\r
                debug('Redirecting to hide access key, follow URL below to continue');\r
@@ -177,7 +175,7 @@ if (isset($_GET['key']) && ($key_index = array_search($_GET['key'], $options->ap
 \r
 ///////////////////////////////////////////////\r
 // Set timezone.\r
-// Prevents warnings, but needs more testing - \r
+// Prevents warnings, but needs more testing -\r
 // perhaps if timezone is set in php.ini we\r
 // don't need to set it at all...\r
 ///////////////////////////////////////////////\r
@@ -199,7 +197,7 @@ if (isset($_GET['key']) && isset($_GET['hash']) && isset($options->api_keys[(int
 }\r
 $key_index = ($valid_key) ? (int)$_GET['key'] : 0;\r
 if (!$valid_key && $options->key_required) {\r
-       die('A valid key must be supplied'); \r
+       die('A valid key must be supplied');\r
 }\r
 if (!$valid_key && isset($_GET['key']) && $_GET['key'] != '') {\r
        die('The entered key is invalid');\r
@@ -250,6 +248,28 @@ if ($options->favour_feed_titles == 'user') {
        $favour_feed_titles = $options->favour_feed_titles;\r
 }\r
 \r
+///////////////////////////////////////////////\r
+// Include full content in output?\r
+///////////////////////////////////////////////\r
+if ($options->content === 'user') {\r
+       if (isset($_GET['content']) && $_GET['content'] === '0') {\r
+               $options->content = false;\r
+       } else {\r
+               $options->content = true;\r
+       }\r
+}\r
+\r
+///////////////////////////////////////////////\r
+// Include summaries in output?\r
+///////////////////////////////////////////////\r
+if ($options->summary === 'user') {\r
+       if (isset($_GET['summary']) && $_GET['summary'] === '1') {\r
+               $options->summary = true;\r
+       } else {\r
+               $options->summary = false;\r
+       }\r
+}\r
+\r
 ///////////////////////////////////////////////\r
 // Exclude items if extraction fails\r
 ///////////////////////////////////////////////\r
@@ -272,15 +292,6 @@ if ($options->detect_language === 'user') {
        $detect_language = $options->detect_language;\r
 }\r
 \r
-if ($detect_language >= 2) {\r
-       $language_codes = array('albanian' => 'sq','arabic' => 'ar','azeri' => 'az','bengali' => 'bn','bulgarian' => 'bg',\r
-       'cebuano' => 'ceb', // ISO 639-2\r
-       'croatian' => 'hr','czech' => 'cs','danish' => 'da','dutch' => 'nl','english' => 'en','estonian' => 'et','farsi' => 'fa','finnish' => 'fi','french' => 'fr','german' => 'de','hausa' => 'ha',\r
-       'hawaiian' => 'haw', // ISO 639-2 \r
-       'hindi' => 'hi','hungarian' => 'hu','icelandic' => 'is','indonesian' => 'id','italian' => 'it','kazakh' => 'kk','kyrgyz' => 'ky','latin' => 'la','latvian' => 'lv','lithuanian' => 'lt','macedonian' => 'mk','mongolian' => 'mn','nepali' => 'ne','norwegian' => 'no','pashto' => 'ps',\r
-       'pidgin' => 'cpe', // ISO 639-2  \r
-       'polish' => 'pl','portuguese' => 'pt','romanian' => 'ro','russian' => 'ru','serbian' => 'sr','slovak' => 'sk','slovene' => 'sl','somali' => 'so','spanish' => 'es','swahili' => 'sw','swedish' => 'sv','tagalog' => 'tl','turkish' => 'tr','ukrainian' => 'uk','urdu' => 'ur','uzbek' => 'uz','vietnamese' => 'vi','welsh' => 'cy');\r
-}\r
 $use_cld = extension_loaded('cld') && (version_compare(PHP_VERSION, '5.3.0') >= 0);\r
 \r
 /////////////////////////////////////\r
@@ -330,7 +341,7 @@ if ($options->cors) header('Access-Control-Allow-Origin: *');
 //////////////////////////////////\r
 if ($options->caching) {\r
        debug('Caching is enabled...');\r
-       $cache_id = md5($max.$url.$valid_key.$links.$favour_feed_titles.$xss_filter.$exclude_on_fail.$format.$detect_language.(int)isset($_GET['pubsub']));\r
+       $cache_id = md5($max.$url.(int)$valid_key.$links.(int)$favour_feed_titles.(int)$options->content.(int)$options->summary.(int)$xss_filter.(int)$exclude_on_fail.$format.$detect_language.(int)isset($_GET['pubsub']));\r
        $check_cache = true;\r
        if ($options->apc && $options->smart_cache) {\r
                apc_add("cache.$cache_id", 0, 10*60);\r
@@ -468,7 +479,7 @@ if ($img_url = $feed->get_image_url()) {
 ////////////////////////////////////////////\r
 // Loop through feed items\r
 ////////////////////////////////////////////\r
-$items = $feed->get_items(0, $max);    \r
+$items = $feed->get_items(0, $max);\r
 // Request all feed items in parallel (if supported)\r
 $urls_sanitized = array();\r
 $urls = array();\r
@@ -550,24 +561,43 @@ foreach ($items as $key => $item) {
                        $is_single_page = false;\r
                        if ($single_page_response = getSinglePage($item, $html, $effective_url)) {\r
                                $is_single_page = true;\r
-                               $html = $single_page_response['body'];\r
-                               // remove strange things\r
-                               $html = str_replace('</[>', '', $html); \r
-                               $html = convert_to_utf8($html, $single_page_response['headers']);\r
                                $effective_url = $single_page_response['effective_url'];\r
-                               debug("Retrieved single-page view from $effective_url");\r
+                               // check if action defined for returned Content-Type\r
+                               $mime_info = get_mime_action_info($single_page_response['headers']);\r
+                               if (isset($mime_info['action'])) {\r
+                                       if ($mime_info['action'] == 'exclude') {\r
+                                               continue; // skip this feed item entry\r
+                                       } elseif ($mime_info['action'] == 'link') {\r
+                                               if ($mime_info['type'] == 'image') {\r
+                                                       $html = "<a href=\"$effective_url\"><img src=\"$effective_url\" alt=\"{$mime_info['name']}\" /></a>";\r
+                                               } else {\r
+                                                       $html = "<a href=\"$effective_url\">Download {$mime_info['name']}</a>";\r
+                                               }\r
+                                               $extracted_title = $mime_info['name'];\r
+                                               $do_content_extraction = false;\r
+                                       }\r
+                               }\r
+                               if ($do_content_extraction) {\r
+                                       $html = $single_page_response['body'];\r
+                                       // remove strange things\r
+                                       $html = str_replace('</[>', '', $html);\r
+                                       $html = convert_to_utf8($html, $single_page_response['headers']);\r
+                                       debug("Retrieved single-page view from $effective_url");\r
+                               }\r
                                unset($single_page_response);\r
                        }\r
+               }\r
+               if ($do_content_extraction) {\r
                        debug('--------');\r
                        debug('Attempting to extract content');\r
                        $extract_result = $extractor->process($html, $effective_url);\r
                        $readability = $extractor->readability;\r
-                       $content_block = ($extract_result) ? $extractor->getContent() : null;                   \r
+                       $content_block = ($extract_result) ? $extractor->getContent() : null;\r
                        $extracted_title = ($extract_result) ? $extractor->getTitle() : '';\r
                        // Deal with multi-page articles\r
                        //die('Next: '.$extractor->getNextPageUrl());\r
                        $is_multi_page = (!$is_single_page && $extract_result && $extractor->getNextPageUrl());\r
-                       if ($options->multipage && $is_multi_page) {\r
+                       if ($options->multipage && $is_multi_page && $options->content) {\r
                                debug('--------');\r
                                debug('Attempting to process multi-page article');\r
                                $multi_page_urls = array();\r
@@ -580,7 +610,7 @@ foreach ($items as $key => $item) {
                                                // check it's not what we have already!\r
                                                if (!in_array($next_page_url, $multi_page_urls)) {\r
                                                        // it's not, so let's attempt to fetch it\r
-                                                       $multi_page_urls[] = $next_page_url;                                            \r
+                                                       $multi_page_urls[] = $next_page_url;\r
                                                        $_prev_ref = $http->referer;\r
                                                        if (($response = $http->get($next_page_url, true)) && $response['status_code'] < 300) {\r
                                                                // make sure mime type is not something with a different action associated\r
@@ -605,13 +635,15 @@ foreach ($items as $key => $item) {
                                // did we successfully deal with this multi-page article?\r
                                if (empty($multi_page_content)) {\r
                                        debug('Failed to extract all parts of multi-page article, so not going to include them');\r
-                                       $multi_page_content[] = $readability->dom->createElement('p')->innerHTML = '<em>This article appears to continue on subsequent pages which we could not extract</em>';\r
+                                       $_page = $readability->dom->createElement('p');\r
+                                       $_page->innerHTML = '<em>This article appears to continue on subsequent pages which we could not extract</em>';\r
+                                       $multi_page_content[] = $_page;\r
                                }\r
                                foreach ($multi_page_content as $_page) {\r
                                        $_page = $content_block->ownerDocument->importNode($_page, true);\r
                                        $content_block->appendChild($_page);\r
                                }\r
-                               unset($multi_page_urls, $multi_page_content, $page_mime_info, $next_page_url);\r
+                               unset($multi_page_urls, $multi_page_content, $page_mime_info, $next_page_url, $_page);\r
                        }\r
                }\r
                // use extracted title for both feed and item title if we're using single-item dummy feed\r
@@ -658,7 +690,7 @@ foreach ($items as $key => $item) {
                        } else {\r
                                $html = $content_block->ownerDocument->saveXML($content_block); // essentially outerHTML\r
                        }\r
-                       unset($content_block);\r
+                       //unset($content_block);\r
                        // post-processing cleanup\r
                        $html = preg_replace('!<p>[\s\h\v]*</p>!u', '', $html);\r
                        if ($links == 'remove') {\r
@@ -671,130 +703,155 @@ foreach ($items as $key => $item) {
                }\r
        }\r
 \r
-               if ($valid_key && isset($_GET['pubsub'])) { // used only on fivefilters.org at the moment\r
-                       $newitem->addElement('guid', 'http://fivefilters.org/content-only/redirect.php?url='.urlencode($item->get_permalink()), array('isPermaLink'=>'false'));\r
+       if ($valid_key && isset($_GET['pubsub'])) { // used only on fivefilters.org at the moment\r
+               $newitem->addElement('guid', 'http://fivefilters.org/content-only/redirect.php?url='.urlencode($item->get_permalink()), array('isPermaLink'=>'false'));\r
+       } else {\r
+               $newitem->addElement('guid', $item->get_permalink(), array('isPermaLink'=>'true'));\r
+       }\r
+       // filter xss?\r
+       if ($xss_filter) {\r
+               debug('Filtering HTML to remove XSS');\r
+               $html = htmLawed::hl($html, array('safe'=>1, 'deny_attribute'=>'style', 'comment'=>1, 'cdata'=>1));\r
+       }\r
+\r
+       // add content\r
+       if ($options->summary === true) {\r
+               // get summary\r
+               $summary = '';\r
+               if (!$do_content_extraction) {\r
+                       $summary = $html;\r
                } else {\r
-                       $newitem->addElement('guid', $item->get_permalink(), array('isPermaLink'=>'true'));\r
-               }\r
-               // filter xss?\r
-               if ($xss_filter) {\r
-                       debug('Filtering HTML to remove XSS');\r
-                       $html = htmLawed::hl($html, array('safe'=>1, 'deny_attribute'=>'style', 'comment'=>1, 'cdata'=>1));\r
-               }\r
-               $newitem->setDescription($html);\r
-               \r
-               // set date\r
-               if ((int)$item->get_date('U') > 0) {\r
-                       $newitem->setDate((int)$item->get_date('U'));\r
-               } elseif ($extractor->getDate()) {\r
-                       $newitem->setDate($extractor->getDate());\r
-               }\r
-               \r
-               // add authors\r
-               if ($authors = $item->get_authors()) {\r
-                       foreach ($authors as $author) {\r
-                               // for some feeds, SimplePie stores author's name as email, e.g. http://feeds.feedburner.com/nymag/intel\r
-                               if ($author->get_name() !== null) {\r
-                                       $newitem->addElement('dc:creator', $author->get_name());\r
-                               } elseif ($author->get_email() !== null) {\r
-                                       $newitem->addElement('dc:creator', $author->get_email());\r
+                       // Try to get first few paragraphs\r
+                       if (isset($content_block) && ($content_block instanceof DOMElement)) {\r
+                               $_paras = $content_block->getElementsByTagName('p');\r
+                               foreach ($_paras as $_para) {\r
+                                       $summary .= preg_replace("/[\n\r\t ]+/", ' ', $_para->textContent).' ';\r
+                                       if (strlen($summary) > 200) break;\r
                                }\r
+                       } else {\r
+                               $summary = $html;\r
                        }\r
-               } elseif ($authors = $extractor->getAuthors()) {\r
-                       //TODO: make sure the list size is reasonable\r
-                       foreach ($authors as $author) {\r
-                               // TODO: xpath often selects authors from other articles linked from the page.\r
-                               // for now choose first item\r
-                               $newitem->addElement('dc:creator', $author);\r
-                               break;\r
+               }\r
+               unset($_paras, $_para);\r
+               $summary = get_excerpt($summary);\r
+               $newitem->setDescription($summary);\r
+               if ($options->content) $newitem->setElement('content:encoded', $html);\r
+       } else {\r
+               if ($options->content) $newitem->setDescription($html);\r
+       }\r
+\r
+       // set date\r
+       if ((int)$item->get_date('U') > 0) {\r
+               $newitem->setDate((int)$item->get_date('U'));\r
+       } elseif ($extractor->getDate()) {\r
+               $newitem->setDate($extractor->getDate());\r
+       }\r
+\r
+       // add authors\r
+       if ($authors = $item->get_authors()) {\r
+               foreach ($authors as $author) {\r
+                       // for some feeds, SimplePie stores author's name as email, e.g. http://feeds.feedburner.com/nymag/intel\r
+                       if ($author->get_name() !== null) {\r
+                               $newitem->addElement('dc:creator', $author->get_name());\r
+                       } elseif ($author->get_email() !== null) {\r
+                               $newitem->addElement('dc:creator', $author->get_email());\r
                        }\r
                }\r
-               \r
-               // add language\r
-               if ($detect_language) {\r
-                       $language = $extractor->getLanguage();\r
-                       if (!$language) $language = $feed->get_language();\r
-                       if (($detect_language == 3 || (!$language && $detect_language == 2)) && $text_sample) {\r
-                               try {\r
-                                       if ($use_cld) {\r
-                                               // Use PHP-CLD extension\r
-                                               $php_cld = 'CLD\detect'; // in quotes to prevent PHP 5.2 parse error\r
-                                               $res = $php_cld($text_sample);\r
-                                               if (is_array($res) && count($res) > 0) {\r
-                                                       $language = $res[0]['code'];\r
-                                               }       \r
-                                       } else {\r
-                                               //die('what');\r
-                                               // Use PEAR's Text_LanguageDetect\r
-                                               if (!isset($l)) {\r
-                                                       $l = new Text_LanguageDetect('libraries/language-detect/lang.dat', 'libraries/language-detect/unicode_blocks.dat');\r
-                                               }\r
-                                               $l_result = $l->detect($text_sample, 1);\r
-                                               if (count($l_result) > 0) {\r
-                                                       $language = $language_codes[key($l_result)];\r
-                                               }\r
+       } elseif ($authors = $extractor->getAuthors()) {\r
+               //TODO: make sure the list size is reasonable\r
+               foreach ($authors as $author) {\r
+                       // TODO: xpath often selects authors from other articles linked from the page.\r
+                       // for now choose first item\r
+                       $newitem->addElement('dc:creator', $author);\r
+                       break;\r
+               }\r
+       }\r
+\r
+       // add language\r
+       if ($detect_language) {\r
+               $language = $extractor->getLanguage();\r
+               if (!$language) $language = $feed->get_language();\r
+               if (($detect_language == 3 || (!$language && $detect_language == 2)) && $text_sample) {\r
+                       try {\r
+                               if ($use_cld) {\r
+                                       // Use PHP-CLD extension\r
+                                       $php_cld = 'CLD\detect'; // in quotes to prevent PHP 5.2 parse error\r
+                                       $res = $php_cld($text_sample);\r
+                                       if (is_array($res) && count($res) > 0) {\r
+                                               $language = $res[0]['code'];\r
+                                       }\r
+                               } else {\r
+                                       //die('what');\r
+                                       // Use PEAR's Text_LanguageDetect\r
+                                       if (!isset($l)) {\r
+                                         $l = new Text_LanguageDetect();\r
+                                         $l->setNameMode(2); // return ISO 639-1 codes (e.g. "en")\r
+                                       }\r
+                                       $l_result = $l->detect($text_sample, 1);\r
+                                       if (count($l_result) > 0) {\r
+                                               $language = key($l_result);\r
                                        }\r
-                               } catch (Exception $e) {\r
-                                       //die('error: '.$e);    \r
-                                       // do nothing\r
                                }\r
-                       }\r
-                       if ($language && (strlen($language) < 7)) {     \r
-                               $newitem->addElement('dc:language', $language);\r
+                       } catch (Exception $e) {\r
+                               //die('error: '.$e);\r
+                               // do nothing\r
                        }\r
                }\r
-               \r
-               // add MIME type (if it appeared in our exclusions lists)\r
-               if (isset($mime_info['mime'])) $newitem->addElement('dc:format', $mime_info['mime']);\r
-               // add effective URL (URL after redirects)\r
-               if (isset($effective_url)) {\r
-                       //TODO: ensure $effective_url is valid witout - sometimes it causes problems, e.g.\r
-                       //http://www.siasat.pk/forum/showthread.php?108883-Pakistan-Chowk-by-Rana-Mubashir-�-25th-March-2012-Special-Program-from-Liari-(Karachi)\r
-                       //temporary measure: use utf8_encode()\r
-                       $newitem->addElement('dc:identifier', remove_url_cruft(utf8_encode($effective_url)));\r
-               } else {\r
-                       $newitem->addElement('dc:identifier', remove_url_cruft($item->get_permalink()));\r
+               if ($language && (strlen($language) < 7)) {\r
+                       $newitem->addElement('dc:language', $language);\r
                }\r
-               \r
-               // add categories\r
-               if ($categories = $item->get_categories()) {\r
-                       foreach ($categories as $category) {\r
-                               if ($category->get_label() !== null) {\r
-                                       $newitem->addElement('category', $category->get_label());\r
-                               }\r
+       }\r
+\r
+       // add MIME type (if it appeared in our exclusions lists)\r
+       if (isset($mime_info['mime'])) $newitem->addElement('dc:format', $mime_info['mime']);\r
+       // add effective URL (URL after redirects)\r
+       if (isset($effective_url)) {\r
+               //TODO: ensure $effective_url is valid witout - sometimes it causes problems, e.g.\r
+               //http://www.siasat.pk/forum/showthread.php?108883-Pakistan-Chowk-by-Rana-Mubashir-�-25th-March-2012-Special-Program-from-Liari-(Karachi)\r
+               //temporary measure: use utf8_encode()\r
+               $newitem->addElement('dc:identifier', remove_url_cruft(utf8_encode($effective_url)));\r
+       } else {\r
+               $newitem->addElement('dc:identifier', remove_url_cruft($item->get_permalink()));\r
+       }\r
+\r
+       // add categories\r
+       if ($categories = $item->get_categories()) {\r
+               foreach ($categories as $category) {\r
+                       if ($category->get_label() !== null) {\r
+                               $newitem->addElement('category', $category->get_label());\r
                        }\r
                }\r
-               \r
-               // check for enclosures\r
-               if ($options->keep_enclosures) {\r
-                       if ($enclosures = $item->get_enclosures()) {\r
-                               foreach ($enclosures as $enclosure) {\r
-                                       // thumbnails\r
-                                       foreach ((array)$enclosure->get_thumbnails() as $thumbnail) {\r
-                                               $newitem->addElement('media:thumbnail', '', array('url'=>$thumbnail));\r
-                                       }\r
-                                       if (!$enclosure->get_link()) continue;\r
-                                       $enc = array();\r
-                                       // Media RSS spec ($enc): http://search.yahoo.com/mrss\r
-                                       // SimplePie methods ($enclosure): http://simplepie.org/wiki/reference/start#methods4\r
-                                       $enc['url'] = $enclosure->get_link();\r
-                                       if ($enclosure->get_length()) $enc['fileSize'] = $enclosure->get_length();\r
-                                       if ($enclosure->get_type()) $enc['type'] = $enclosure->get_type();\r
-                                       if ($enclosure->get_medium()) $enc['medium'] = $enclosure->get_medium();\r
-                                       if ($enclosure->get_expression()) $enc['expression'] = $enclosure->get_expression();\r
-                                       if ($enclosure->get_bitrate()) $enc['bitrate'] = $enclosure->get_bitrate();\r
-                                       if ($enclosure->get_framerate()) $enc['framerate'] = $enclosure->get_framerate();\r
-                                       if ($enclosure->get_sampling_rate()) $enc['samplingrate'] = $enclosure->get_sampling_rate();\r
-                                       if ($enclosure->get_channels()) $enc['channels'] = $enclosure->get_channels();\r
-                                       if ($enclosure->get_duration()) $enc['duration'] = $enclosure->get_duration();\r
-                                       if ($enclosure->get_height()) $enc['height'] = $enclosure->get_height();\r
-                                       if ($enclosure->get_width()) $enc['width'] = $enclosure->get_width();\r
-                                       if ($enclosure->get_language()) $enc['lang'] = $enclosure->get_language();\r
-                                       $newitem->addElement('media:content', '', $enc);\r
+       }\r
+\r
+       // check for enclosures\r
+       if ($options->keep_enclosures) {\r
+               if ($enclosures = $item->get_enclosures()) {\r
+                       foreach ($enclosures as $enclosure) {\r
+                               // thumbnails\r
+                               foreach ((array)$enclosure->get_thumbnails() as $thumbnail) {\r
+                                       $newitem->addElement('media:thumbnail', '', array('url'=>$thumbnail));\r
                                }\r
+                               if (!$enclosure->get_link()) continue;\r
+                               $enc = array();\r
+                               // Media RSS spec ($enc): http://search.yahoo.com/mrss\r
+                               // SimplePie methods ($enclosure): http://simplepie.org/wiki/reference/start#methods4\r
+                               $enc['url'] = $enclosure->get_link();\r
+                               if ($enclosure->get_length()) $enc['fileSize'] = $enclosure->get_length();\r
+                               if ($enclosure->get_type()) $enc['type'] = $enclosure->get_type();\r
+                               if ($enclosure->get_medium()) $enc['medium'] = $enclosure->get_medium();\r
+                               if ($enclosure->get_expression()) $enc['expression'] = $enclosure->get_expression();\r
+                               if ($enclosure->get_bitrate()) $enc['bitrate'] = $enclosure->get_bitrate();\r
+                               if ($enclosure->get_framerate()) $enc['framerate'] = $enclosure->get_framerate();\r
+                               if ($enclosure->get_sampling_rate()) $enc['samplingrate'] = $enclosure->get_sampling_rate();\r
+                               if ($enclosure->get_channels()) $enc['channels'] = $enclosure->get_channels();\r
+                               if ($enclosure->get_duration()) $enc['duration'] = $enclosure->get_duration();\r
+                               if ($enclosure->get_height()) $enc['height'] = $enclosure->get_height();\r
+                               if ($enclosure->get_width()) $enc['width'] = $enclosure->get_width();\r
+                               if ($enclosure->get_language()) $enc['lang'] = $enclosure->get_language();\r
+                               $newitem->addElement('media:content', '', $enc);\r
                        }\r
                }\r
-       /* } */\r
+       }\r
        $output->addItem($newitem);\r
        unset($html);\r
        $item_count++;\r