Cleaned up enclosures, factored out header code, fixed validation -

http://mosquito.wordpress.org/view.php?id=963


git-svn-id: http://svn.automattic.com/wordpress/trunk@2416 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
matt 2005-03-07 08:56:47 +00:00
parent 5d148a8d1b
commit 568d440bf4
2 changed files with 47 additions and 35 deletions

View File

@ -146,7 +146,7 @@ function rss_enclosure() {
if (is_array($val)) { if (is_array($val)) {
foreach($val as $enc) { foreach($val as $enc) {
$enclosure = split( "\n", $enc ); $enclosure = split( "\n", $enc );
print "<enclosure url='".trim( $enclosure[ 0 ] )."' length='".trim( $enclosure[ 1 ] )."' type='".trim( $enclosure[ 2 ] )."'/>\n"; print "<enclosure url='".trim( htmlspecialchars($enclosure[ 0 ]) )."' length='".trim( $enclosure[ 1 ] )."' type='".trim( $enclosure[ 2 ] )."'/>\n";
} }
} }
} }

View File

@ -748,40 +748,51 @@ function do_enclose( $content, $post_ID ) {
endif; endif;
endforeach; endforeach;
foreach ($post_links as $url){ foreach ($post_links as $url) :
if( $url != '' && in_array($url, $pung) == false ) { if ( $url != '' && in_array($url, $pung) == false ) {
set_time_limit( 60 ); if ( $headers = wp_get_http_headers( $url) ) {
$file = str_replace( "http://", "", $url ); $len = $headers['content-length'];
$host = substr( $file, 0, strpos( $file, "/" ) ); $type = $headers['content-type'];
$file = substr( $file, strpos( $file, "/" ) ); $allowed_types = array( 'video', 'audio' );
$headers = "HEAD $file HTTP/1.1\r\nHOST: $host\r\n\r\n"; if( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) {
$port = 80; $meta_value = "$url\n$len\n$type\n";
$timeout = 3; $wpdb->query( "INSERT INTO `".$wpdb->postmeta."` ( `post_id` , `meta_key` , `meta_value` )
$fp = @fsockopen($host, $port, $err_num, $err_msg, $timeout); VALUES ( '$post_ID', 'enclosure' , '".$meta_value."')" );
if( $fp ) { }
fputs($fp, $headers ); }
$response = ''; }
while ( !feof($fp) && strpos( $response, "\r\n\r\n" ) == false ) endforeach;
$response .= fgets($fp, 2048); }
fclose( $fp );
} else { function wp_get_http_headers( $url ) {
$response = ''; set_time_limit( 60 );
} $parts = parse_url( $url );
if( $response != '' ) { $file = $parts['path'] . $parts['query'];
$len = substr( $response, strpos( $response, "Content-Length:" ) + 16 ); $host = $parts['host'];
$len = substr( $len, 0, strpos( $len, "\n" ) ); if ( !isset( $parts['port'] ) )
$type = substr( $response, strpos( $response, "Content-Type:" ) + 14 ); $parts['port'] = 80;
$type = substr( $type, 0, strpos( $type, "\n" ) + 1 );
$allowed_types = array( 'video', 'audio' ); $head = "HEAD $file HTTP/1.1\r\nHOST: $host\r\n\r\n";
if( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) {
$meta_value = "$url\n$len\n$type\n"; $fp = @fsockopen($host, $parts['port'], $err_num, $err_msg, 3);
$query = "INSERT INTO `".$wpdb->postmeta."` ( `meta_id` , `post_id` , `meta_key` , `meta_value` ) if ( !$fp )
VALUES ( NULL, '$post_ID', 'enclosure' , '".$meta_value."')"; return false;
$wpdb->query( $query );
} $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. // Deprecated. Use the new post loop.
@ -863,6 +874,7 @@ function merge_filters($tag) {
} }
} }
}
if (isset($wp_filter[$tag])) if (isset($wp_filter[$tag]))
ksort($wp_filter[$tag]); ksort($wp_filter[$tag]);