Make WordPress Core


Ignore:
Timestamp:
12/16/2007 09:34:48 PM (16 years ago)
Author:
ryan
Message:

Import file attachments. Props tellyworth. fixes #5466

File:
1 edited

Legend:

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

    r6367 r6390  
    532532}
    533533
    534 
    535 function wp_get_http_headers( $url, $red = 1 ) {
     534// perform a HTTP HEAD or GET request
     535// if $file_path is a writable filename, this will do a GET request and write the file to that path
     536// returns a list of HTTP headers
     537function wp_get_http( $url, $file_path = false, $red = 1 ) {
    536538    global $wp_version;
    537539    @set_time_limit( 60 );
     
    546548        $parts['port'] = 80;
    547549
    548     $head = "HEAD $file HTTP/1.1\r\nHOST: $host\r\nUser-Agent: WordPress/" . $wp_version . "\r\n\r\n";
     550    if ( $file_path )
     551        $request_type = 'GET';
     552    else
     553        $request_type = 'HEAD';
     554       
     555    $head = "$request_type $file HTTP/1.1\r\nHOST: $host\r\nUser-Agent: WordPress/" . $wp_version . "\r\n\r\n";
    549556
    550557    $fp = @fsockopen( $host, $parts['port'], $err_num, $err_msg, 3 );
     
    556563    while ( !feof( $fp ) && strpos( $response, "\r\n\r\n" ) == false )
    557564        $response .= fgets( $fp, 2048 );
    558     fclose( $fp );
    559565    preg_match_all( '/(.*?): (.*)\r/', $response, $matches );
    560566    $count = count( $matches[1] );
     
    568574
    569575        $code = $headers['response'];
    570         if ( ( '302' == $code || '301' == $code ) && isset( $headers['location'] ) )
    571                 return wp_get_http_headers( $headers['location'], ++$red );
    572 
     576        if ( ( '302' == $code || '301' == $code ) && isset( $headers['location'] ) ) {
     577                fclose($fp);
     578                return wp_get_http_headers( $headers['location'], $get, ++$red );
     579        }
     580   
     581    // make a note of the final location, so the caller can tell if we were redirected or not
     582    $headers['x-final-location'] = $url;
     583
     584    // HEAD request only
     585    if ( !$file_path ) {
     586        fclose($fp);
     587        return $headers;
     588    }
     589   
     590    // GET request - fetch and write it to the supplied filename
     591    $content_length = $headers['content-length'];
     592    $got_bytes = 0;
     593    $out_fp = fopen($file_path, 'w');
     594    while ( !feof($fp) ) {
     595        $buf = fread( $fp, 4096 );
     596        fwrite( $out_fp, $buf );
     597        $got_bytes += strlen($buf);
     598        // don't read past the content-length
     599        if ($content_length and $got_bytes >= $content_length)
     600            break;
     601    }
     602   
     603    fclose($out_fp);
     604    fclose($fp);
    573605    return $headers;
     606}
     607
     608function wp_get_http_headers( $url ) {
     609    return wp_get_http( $url, false );
    574610}
    575611
     
    9931029
    9941030// Returns an array containing the current upload directory's path and url, or an error message.
    995 function wp_upload_dir() {
     1031function wp_upload_dir( $time = NULL ) {
    9961032    $siteurl = get_option( 'siteurl' );
    9971033    //prepend ABSPATH to $dir and $siteurl to $url if they're not already there
     
    10101046    if ( get_option( 'uploads_use_yearmonth_folders' ) ) {
    10111047        // Generate the yearly and monthly dirs
    1012         $time = current_time( 'mysql' );
     1048        if ( !$time )
     1049            $time = current_time( 'mysql' );
    10131050        $y = substr( $time, 0, 4 );
    10141051        $m = substr( $time, 5, 2 );
     
    10271064}
    10281065
    1029 function wp_upload_bits( $name, $deprecated, $bits ) {
     1066// return a filename that is sanitized and unique for the given directory
     1067function wp_unique_filename( $dir, $name, $ext, $unique_filename_callback = NULL ) {
     1068   
     1069    // Increment the file number until we have a unique file to save in $dir. Use $override['unique_filename_callback'] if supplied.
     1070    if ( $unique_filename_callback && function_exists( $unique_filename_callback ) ) {
     1071        $filename = $unique_filename_callback( $dir, $name );
     1072    } else {
     1073        $number = '';
     1074        $filename = str_replace( '#', '_', $name );
     1075        $filename = str_replace( array( '\\', "'" ), '', $filename );
     1076        if ( empty( $ext) )
     1077            $ext = '';
     1078        else
     1079            $ext = ".$ext";
     1080        $filename = $filename . $ext;
     1081        while ( file_exists( $dir . "/$filename" ) ) {
     1082            if ( '' == "$number$ext" )
     1083                $filename = $filename . ++$number . $ext;
     1084            else
     1085                $filename = str_replace( "$number$ext", ++$number . $ext, $filename );
     1086        }
     1087        $filename = str_replace( $ext, '', $filename );
     1088        $filename = sanitize_title_with_dashes( $filename ) . $ext;
     1089    }
     1090   
     1091    return $filename;
     1092}
     1093
     1094function wp_upload_bits( $name, $deprecated, $bits, $time = NULL ) {
    10301095    if ( empty( $name ) )
    10311096        return array( 'error' => __( "Empty filename" ) );
     
    10351100        return array( 'error' => __( "Invalid file type" ) );
    10361101
    1037     $upload = wp_upload_dir();
     1102    $upload = wp_upload_dir( $time );
    10381103
    10391104    if ( $upload['error'] !== false )
    10401105        return $upload;
    10411106
    1042     $number = '';
    10431107    $filename = $name;
    10441108    $path_parts = pathinfo( $filename );
    10451109    $ext = $path_parts['extension'];
    1046     if ( empty( $ext ) )
    1047         $ext = '';
    1048     else
    1049         $ext = ".$ext";
    1050     while ( file_exists( $upload['path'] . "/$filename" ) ) {
    1051         if ( '' == "$number$ext" )
    1052             $filename = $filename . ++$number . $ext;
    1053         else
    1054             $filename = str_replace( "$number$ext", ++$number . $ext, $filename );
    1055     }
     1110   
     1111    $filename = wp_unique_filename( $upload['path'], $path_parts['basename'], $ext );
    10561112
    10571113    $new_file = $upload['path'] . "/$filename";
Note: See TracChangeset for help on using the changeset viewer.