Make WordPress Core

Changeset 2416


Ignore:
Timestamp:
03/07/2005 08:56:47 AM (22 years ago)
Author:
matt
Message:

Cleaned up enclosures, factored out header code, fixed validation -
​http://mosquito.wordpress.org/view.php?id=963

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/feed-functions.php

    r2389 r2416  
    147147                                        foreach($val as $enc) {
    148148                                                $enclosure = split( "\n", $enc );
    149                                                 print "<enclosure url='".trim( $enclosure[ 0 ] )."' length='".trim( $enclosure[ 1 ] )."' type='".trim( $enclosure[ 2 ] )."'/>\n";
     149                                                print "<enclosure url='".trim( htmlspecialchars($enclosure[ 0 ]) )."' length='".trim( $enclosure[ 1 ] )."' type='".trim( $enclosure[ 2 ] )."'/>\n";
    150150                                        }
    151151                                }
  • trunk/wp-includes/functions.php

    r2415 r2416  
    749749        endforeach;
    750750
    751         foreach ($post_links as $url){
    752                 if( $url != '' && in_array($url, $pung) == false ) {
    753                     set_time_limit( 60 );
    754                     $file = str_replace( "http://", "", $url );
    755                     $host = substr( $file, 0, strpos( $file, "/" ) );
    756                     $file = substr( $file, strpos( $file, "/" ) );
    757                     $headers = "HEAD $file HTTP/1.1\r\nHOST: $host\r\n\r\n";
    758                     $port    = 80;
    759                     $timeout = 3;
    760                     $fp = @fsockopen($host, $port, $err_num, $err_msg, $timeout);
    761                     if( $fp ) {
    762                         fputs($fp, $headers );
    763                         $response = '';
    764                         while ( !feof($fp) && strpos( $response, "\r\n\r\n" ) == false )
    765                             $response .= fgets($fp, 2048);
    766                         fclose( $fp );
    767                     } else {
    768                         $response = '';
    769                     }
    770                     if( $response != '' ) {
    771                         $len = substr( $response, strpos( $response, "Content-Length:" ) + 16 );
    772                         $len = substr( $len, 0, strpos( $len, "\n" ) );
    773                         $type = substr( $response, strpos( $response, "Content-Type:" ) + 14 );
    774                         $type = substr( $type, 0, strpos( $type, "\n" ) + 1 );
    775                         $allowed_types = array( 'video', 'audio' );
    776                         if( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) {
    777                             $meta_value = "$url\n$len\n$type\n";
    778                             $query = "INSERT INTO `".$wpdb->postmeta."` ( `meta_id` , `post_id` , `meta_key` , `meta_value` )
    779                                 VALUES ( NULL, '$post_ID', 'enclosure' , '".$meta_value."')";
    780                             $wpdb->query( $query );
    781                         }
    782                     }
    783                 }
    784         }
     751        foreach ($post_links as $url) :
     752                if ( $url != '' && in_array($url, $pung) == false ) {
     753                        if ( $headers = wp_get_http_headers( $url) ) {
     754                                $len  = $headers['content-length'];
     755                                $type = $headers['content-type'];
     756                                $allowed_types = array( 'video', 'audio' );
     757                                if( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) {
     758                                        $meta_value = "$url\n$len\n$type\n";
     759                                        $wpdb->query( "INSERT INTO `".$wpdb->postmeta."` ( `post_id` , `meta_key` , `meta_value` )
     760                                        VALUES ( '$post_ID', 'enclosure' , '".$meta_value."')" );
     761                                }
     762                        }
     763                }
     764        endforeach;
     765}
     766
     767function wp_get_http_headers( $url ) {
     768        set_time_limit( 60 );
     769        $parts = parse_url( $url );
     770        $file  = $parts['path'] . $parts['query'];
     771        $host  = $parts['host'];
     772        if ( !isset( $parts['port'] ) )
     773                $parts['port'] = 80;
     774
     775        $head = "HEAD $file HTTP/1.1\r\nHOST: $host\r\n\r\n";
     776
     777        $fp = @fsockopen($host, $parts['port'], $err_num, $err_msg, 3);
     778        if ( !$fp )
     779                return false;
     780
     781        $response = '';
     782        fputs( $fp, $head );
     783        while ( !feof( $fp ) && strpos( $response, "\r\n\r\n" ) == false )
     784                $response .= fgets( $fp, 2048 );
     785        fclose( $fp );
     786        preg_match_all('/(.*?): (.*)\r/', $response, $matches);
     787        $count = count($matches[1]);
     788        for ( $i = 0; $i < $count; $i++) {
     789                $key = strtolower($matches[1][$i]);
     790                $headers["$key"] = $matches[2][$i];
     791        }
     792
     793        preg_match('/.*([0-9]{3}).*/', $response, $return);
     794        $headers['response'] = $return[1]; // HTTP response code eg 204, 200, 404
     795        return $headers;
    785796}
    786797
    … …  
    864875
    865876        }
     877}
    866878
    867879        if (isset($wp_filter[$tag]))
Note: See TracChangeset for help on using the changeset viewer.