Changeset 6390 for trunk/wp-includes/functions.php
- Timestamp:
- 12/16/2007 09:34:48 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/functions.php
r6367 r6390 532 532 } 533 533 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 537 function wp_get_http( $url, $file_path = false, $red = 1 ) { 536 538 global $wp_version; 537 539 @set_time_limit( 60 ); … … 546 548 $parts['port'] = 80; 547 549 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"; 549 556 550 557 $fp = @fsockopen( $host, $parts['port'], $err_num, $err_msg, 3 ); … … 556 563 while ( !feof( $fp ) && strpos( $response, "\r\n\r\n" ) == false ) 557 564 $response .= fgets( $fp, 2048 ); 558 fclose( $fp );559 565 preg_match_all( '/(.*?): (.*)\r/', $response, $matches ); 560 566 $count = count( $matches[1] ); … … 568 574 569 575 $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); 573 605 return $headers; 606 } 607 608 function wp_get_http_headers( $url ) { 609 return wp_get_http( $url, false ); 574 610 } 575 611 … … 993 1029 994 1030 // Returns an array containing the current upload directory's path and url, or an error message. 995 function wp_upload_dir( ) {1031 function wp_upload_dir( $time = NULL ) { 996 1032 $siteurl = get_option( 'siteurl' ); 997 1033 //prepend ABSPATH to $dir and $siteurl to $url if they're not already there … … 1010 1046 if ( get_option( 'uploads_use_yearmonth_folders' ) ) { 1011 1047 // Generate the yearly and monthly dirs 1012 $time = current_time( 'mysql' ); 1048 if ( !$time ) 1049 $time = current_time( 'mysql' ); 1013 1050 $y = substr( $time, 0, 4 ); 1014 1051 $m = substr( $time, 5, 2 ); … … 1027 1064 } 1028 1065 1029 function wp_upload_bits( $name, $deprecated, $bits ) { 1066 // return a filename that is sanitized and unique for the given directory 1067 function 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 1094 function wp_upload_bits( $name, $deprecated, $bits, $time = NULL ) { 1030 1095 if ( empty( $name ) ) 1031 1096 return array( 'error' => __( "Empty filename" ) ); … … 1035 1100 return array( 'error' => __( "Invalid file type" ) ); 1036 1101 1037 $upload = wp_upload_dir( );1102 $upload = wp_upload_dir( $time ); 1038 1103 1039 1104 if ( $upload['error'] !== false ) 1040 1105 return $upload; 1041 1106 1042 $number = '';1043 1107 $filename = $name; 1044 1108 $path_parts = pathinfo( $filename ); 1045 1109 $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 ); 1056 1112 1057 1113 $new_file = $upload['path'] . "/$filename";
Note: See TracChangeset
for help on using the changeset viewer.