From 6470c3f14fabb0e655c24aca62e0b6ca5a58bed5 Mon Sep 17 00:00:00 2001 From: Matt Mullenweg Date: Mon, 7 Mar 2005 08:56:47 +0000 Subject: [PATCH] Cleaned up enclosures, factored out header code, fixed validation - http://mosquito.wordpress.org/view.php?id=963 git-svn-id: https://develop.svn.wordpress.org/trunk@2416 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/feed-functions.php | 2 +- wp-includes/functions.php | 80 +++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 35 deletions(-) diff --git a/wp-includes/feed-functions.php b/wp-includes/feed-functions.php index bd11301015..81cae60e00 100644 --- a/wp-includes/feed-functions.php +++ b/wp-includes/feed-functions.php @@ -146,7 +146,7 @@ function rss_enclosure() { if (is_array($val)) { foreach($val as $enc) { $enclosure = split( "\n", $enc ); - print "\n"; + print "\n"; } } } diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 557426a275..89acb7d584 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -748,40 +748,51 @@ function do_enclose( $content, $post_ID ) { endif; endforeach; - foreach ($post_links as $url){ - if( $url != '' && in_array($url, $pung) == false ) { - set_time_limit( 60 ); - $file = str_replace( "http://", "", $url ); - $host = substr( $file, 0, strpos( $file, "/" ) ); - $file = substr( $file, strpos( $file, "/" ) ); - $headers = "HEAD $file HTTP/1.1\r\nHOST: $host\r\n\r\n"; - $port = 80; - $timeout = 3; - $fp = @fsockopen($host, $port, $err_num, $err_msg, $timeout); - if( $fp ) { - fputs($fp, $headers ); - $response = ''; - while ( !feof($fp) && strpos( $response, "\r\n\r\n" ) == false ) - $response .= fgets($fp, 2048); - fclose( $fp ); - } else { - $response = ''; - } - if( $response != '' ) { - $len = substr( $response, strpos( $response, "Content-Length:" ) + 16 ); - $len = substr( $len, 0, strpos( $len, "\n" ) ); - $type = substr( $response, strpos( $response, "Content-Type:" ) + 14 ); - $type = substr( $type, 0, strpos( $type, "\n" ) + 1 ); - $allowed_types = array( 'video', 'audio' ); - if( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) { - $meta_value = "$url\n$len\n$type\n"; - $query = "INSERT INTO `".$wpdb->postmeta."` ( `meta_id` , `post_id` , `meta_key` , `meta_value` ) - VALUES ( NULL, '$post_ID', 'enclosure' , '".$meta_value."')"; - $wpdb->query( $query ); - } - } - } - } + foreach ($post_links as $url) : + if ( $url != '' && in_array($url, $pung) == false ) { + if ( $headers = wp_get_http_headers( $url) ) { + $len = $headers['content-length']; + $type = $headers['content-type']; + $allowed_types = array( 'video', 'audio' ); + if( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) { + $meta_value = "$url\n$len\n$type\n"; + $wpdb->query( "INSERT INTO `".$wpdb->postmeta."` ( `post_id` , `meta_key` , `meta_value` ) + VALUES ( '$post_ID', 'enclosure' , '".$meta_value."')" ); + } + } + } + endforeach; +} + +function wp_get_http_headers( $url ) { + set_time_limit( 60 ); + $parts = parse_url( $url ); + $file = $parts['path'] . $parts['query']; + $host = $parts['host']; + if ( !isset( $parts['port'] ) ) + $parts['port'] = 80; + + $head = "HEAD $file HTTP/1.1\r\nHOST: $host\r\n\r\n"; + + $fp = @fsockopen($host, $parts['port'], $err_num, $err_msg, 3); + if ( !$fp ) + return false; + + $response = ''; + fputs( $fp, $head ); + while ( !feof( $fp ) && strpos( $response, "\r\n\r\n" ) == false ) + $response .= fgets( $fp, 2048 ); + fclose( $fp ); + preg_match_all('/(.*?): (.*)\r/', $response, $matches); + $count = count($matches[1]); + for ( $i = 0; $i < $count; $i++) { + $key = strtolower($matches[1][$i]); + $headers["$key"] = $matches[2][$i]; + } + + preg_match('/.*([0-9]{3}).*/', $response, $return); + $headers['response'] = $return[1]; // HTTP response code eg 204, 200, 404 + return $headers; } // Deprecated. Use the new post loop. @@ -863,6 +874,7 @@ function merge_filters($tag) { } } +} if (isset($wp_filter[$tag])) ksort($wp_filter[$tag]);