Ticket #10779: 10779.2.diff
File 10779.2.diff, 11.5 KB (added by , 15 years ago) |
---|
-
wp-admin/includes/file.php
99 99 } 100 100 101 101 /** 102 * {@internal Missing Short Description}} 102 * Returns a listing of all files in the specified folder and all subdirectories up to 100 levels deep. 103 * The depth of the recursiveness can be controlled by the $levels param. 103 104 * 104 * @since unknown105 * @since 2.6.0 105 106 * 106 * @param string $folder Optional.Full path to folder107 * @param int $levels Optional.Levels of folders to follow, Default: 100 (PHP Loop limit).108 * @return bool|array 107 * @param string $folder Full path to folder 108 * @param int $levels (optional) Levels of folders to follow, Default: 100 (PHP Loop limit). 109 * @return bool|array False on failure, Else array of files 109 110 */ 110 111 function list_files( $folder = '', $levels = 100 ) { 111 112 if( empty($folder) ) … … 135 136 } 136 137 137 138 /** 138 * {@internal Missing Short Description}} 139 * Determines a writable directory for temporary files. 140 * Function's preference is to WP_CONTENT_DIR followed by the return value of <code>sys_get_temp_dir()</code>, before finally defaulting to /tmp/ 139 141 * 140 * @since unknown142 * In the event that this function does not find a writable location, It may be overridden by the <code>WP_TEMP_DIR</code> constant in your <code>wp-config.php</code> file. 141 143 * 142 * @return unknown 144 * @since 2.5.0 145 * 146 * @return string Writable temporary directory 143 147 */ 144 148 function get_temp_dir() { 145 149 if ( defined('WP_TEMP_DIR') ) … … 156 160 } 157 161 158 162 /** 159 * {@internal Missing Short Description}} 163 * Returns a filename of a Temporary unique file. 164 * Please note that the calling function must unlink() this itself. 160 165 * 161 * @since unknown 166 * The filename is based off the passed parameter or defaults to the current unix timestamp, 167 * while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory. 162 168 * 163 * @param unknown_type $filename 164 * @param unknown_type $dir 165 * @return unknown 169 * @since 2.6.0 170 * 171 * @param string $filename (optional) Filename to base the Unique file off 172 * @param string $dir (optional) Directory to store the file in 173 * @return string a writable filename 166 174 */ 167 175 function wp_tempnam($filename = '', $dir = ''){ 168 176 if ( empty($dir) ) … … 425 433 } 426 434 427 435 /** 428 * Downloads a url to a local file using the Snoopy HTTP Class. 436 * Downloads a url to a local temporary file using the WordPress HTTP Class. 437 * Please note, That the calling function must unlink() the file. 429 438 * 430 * @since unknown 431 * @todo Transition over to using the new HTTP Request API (jacob). 439 * @since 2.5.0 432 440 * 433 441 * @param string $url the URL of the file to download 434 442 * @return mixed WP_Error on failure, string Filename on success. … … 467 475 } 468 476 469 477 /** 470 * {@internal Missing Short Description}} 478 * Unzip's a specified ZIP file to a location on the Filesystem via the WordPress Filesystem Abstraction. 479 * Assumes that WP_Filesystem() has already been called and set up. 471 480 * 472 * @since unknown 481 * Attempts to increase the PHP Memory limit to 256M before uncompressing, 482 * However, The most memory required shouldn't be much larger than the Archive itself. 473 483 * 474 * @param unknown_type $file 475 * @param unknown_type $to 476 * @return unknown 484 * @since 2.5.0 485 * 486 * @param string $file Full path and filename of zip archive 487 * @param string $to Full path on the filesystem to extract archive to 488 * @return mixed WP_Error on failure, True on success 477 489 */ 478 490 function unzip_file($file, $to) { 479 491 global $wp_filesystem; … … 481 493 if ( ! $wp_filesystem || !is_object($wp_filesystem) ) 482 494 return new WP_Error('fs_unavailable', __('Could not access filesystem.')); 483 495 484 // Unzip uses a lot of memory 496 // Unzip uses a lot of memory, But not this much hopefully 485 497 @ini_set('memory_limit', '256M'); 486 498 487 $fs =& $wp_filesystem;488 489 499 require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php'); 490 500 491 501 $archive = new PclZip($file); … … 497 507 if ( 0 == count($archive_files) ) 498 508 return new WP_Error('empty_archive', __('Empty archive')); 499 509 500 $path = explode('/', untrailingslashit($to)); 501 for ( $i = count($path); $i > 0; $i-- ) { //>0 = first element is empty allways for paths starting with '/' 502 $tmppath = implode('/', array_slice($path, 0, $i) ); 503 if ( $fs->is_dir($tmppath) ) { //Found the highest folder that exists, Create from here(ie +1) 504 for ( $i = $i + 1; $i <= count($path); $i++ ) { 505 $tmppath = implode('/', array_slice($path, 0, $i) ); 506 if ( ! $fs->mkdir($tmppath, FS_CHMOD_DIR) ) 507 return new WP_Error('mkdir_failed', __('Could not create directory'), $tmppath); 508 } 509 break; //Exit main for loop 510 } 511 } 510 $needed_dirs = array(); 511 $to = trailingslashit($to); 512 512 513 $to = trailingslashit($to); 514 foreach ($archive_files as $file) { 515 $path = $file['folder'] ? $file['filename'] : dirname($file['filename']); 516 $path = explode('/', $path); 517 for ( $i = count($path); $i >= 0; $i-- ) { //>=0 as the first element contains data 513 // Determine any parent dir's needed 514 if ( ! $wp_filesystem->is_dir($to) ) { //Only do parents if no children exist 515 $path = preg_split('![/\\\]!', untrailingslashit($to)); 516 for ( $i = count($path); $i >= 0; $i-- ) { 518 517 if ( empty($path[$i]) ) 519 518 continue; 520 $tmppath = $to . implode('/', array_slice($path, 0, $i) ); 521 if ( $fs->is_dir($tmppath) ) {//Found the highest folder that exists, Create from here 522 for ( $i = $i + 1; $i <= count($path); $i++ ) { //< count() no file component please. 523 $tmppath = $to . implode('/', array_slice($path, 0, $i) ); 524 if ( ! $fs->is_dir($tmppath) && ! $fs->mkdir($tmppath, FS_CHMOD_DIR) ) 525 return new WP_Error('mkdir_failed', __('Could not create directory'), $tmppath); 526 } 527 break; //Exit main for loop 528 } 519 $dir = implode('/', array_slice($path, 0, $i+1) ); 520 if ( preg_match('!^\w:$!i', $dir) ) //Win32 drive letter causes issues. 521 continue; 522 $needed_dirs[] = $dir; 529 523 } 524 } 530 525 531 // We've made sure the folders are there, so let's extract the file now: 532 if ( ! $file['folder'] ) { 533 if ( !$fs->put_contents( $to . $file['filename'], $file['content']) ) 534 return new WP_Error('copy_failed', __('Could not copy file'), $to . $file['filename']); 535 $fs->chmod($to . $file['filename'], FS_CHMOD_FILE); 536 } 526 // Determine any children directories needed 527 foreach ( $archive_files as $file ) 528 $needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname($file['filename']) ); 529 530 $needed_dirs = array_unique($needed_dirs); 531 asort($needed_dirs); 532 533 // Create those directories if need be: 534 foreach ( $needed_dirs as $_dir ) 535 if ( ! $wp_filesystem->is_dir($_dir) && ! $wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) ) 536 return new WP_Error('mkdir_failed', __('Could not create directory'), $_dir); 537 538 // Extract the files from the zip 539 foreach ( $archive_files as $file ) { 540 if ( $file['folder'] ) 541 continue; 542 if ( ! $wp_filesystem->put_contents( $to . $file['filename'], $file['content']) ) 543 return new WP_Error('copy_failed', __('Could not copy file'), $to . $file['filename']); 544 $wp_filesystem->chmod($to . $file['filename'], FS_CHMOD_FILE); 537 545 } 538 546 return true; 539 547 } 540 548 541 549 /** 542 * {@internal Missing Short Description}} 550 * Copies a directory from one location to another via the WordPress Filesystem Abstraction. 551 * Assumes that WP_Filesystem() has already been called and setup. 543 552 * 544 * @since unknown553 * @since 2.5.0 545 554 * 546 * @param unknown_type $from547 * @param unknown_type $to548 * @return unknown555 * @param string $from source directory 556 * @param string $to destination directory 557 * @return mixed WP_Error on failure, True on success. 549 558 */ 550 559 function copy_dir($from, $to) { 551 560 global $wp_filesystem; … … 574 583 return $result; 575 584 } 576 585 } 586 return true; 577 587 } 578 588 579 589 /** 580 * {@internal Missing Short Description}} 590 * Initialises and connects the WordPress Filesystem Abstraction classes. 591 * This function will include the chosen transport and attempt connecting. 581 592 * 582 * @since unknown593 * Plugins may add extra transports, And force WordPress to use them by returning the filename via the 'filesystem_method_file' filter. 583 594 * 584 * @param unknown_type $args 585 * @return unknown 595 * @since 2.5.0 596 * 597 * @param array $args (optional) Connection args, These are passed directly to the WP_Filesystem_*() classes. 598 * @param string $context (optional) Context for get_filesystem_method(), See function declaration for more information. 599 * @return boolean false on failure, true on success 586 600 */ 587 601 function WP_Filesystem( $args = false, $context = false ) { 588 602 global $wp_filesystem; … … 627 641 } 628 642 629 643 /** 630 * {@internal Missing Short Description}} 644 * Determines which Filesystem Method to use. 645 * The priority of the Transports are: Direct, SSH2, FTP PHP Extension, FTP Sockets (Via Sockets class, or fsoxkopen()) 631 646 * 632 * @since unknown 647 * Note that the return value of this function can be overridden in 2 ways 648 * - By defining FS_METHOD in your <code>wp-config.php</code> file 649 * - By using the filesystem_method filter 650 * Valid values for these are: 'direct', 'ssh', 'ftpext' or 'ftpsockets' 651 * Plugins may also define a custom transport handler, See the WP_Filesystem function for more information. 633 652 * 634 * @param unknown_type $args 653 * @since 2.5.0 654 * 655 * @param array $args Connection details. 635 656 * @param string $context Full path to the directory that is tested for being writable. 636 * @return unknown657 * @return string The transport to use, see description for valid return values. 637 658 */ 638 659 function get_filesystem_method($args = array(), $context = false) { 639 660 $method = defined('FS_METHOD') ? FS_METHOD : false; //Please ensure that this is either 'direct', 'ssh', 'ftpext' or 'ftpsockets' … … 659 680 } 660 681 661 682 /** 662 * {@internal Missing Short Description}} 683 * Displays a form to the user to request for their FTP/SSH details in order to connect to the filesystem. 684 * All chosen/entered details are saved, Excluding the Password. 663 685 * 664 * @since unknown686 * Hostnames may be in the form of hostname:portnumber (eg: wordpress.org:2467) to specify an alternate FTP/SSH port. 665 687 * 666 * @param unknown_type $form_post 667 * @param unknown_type $type 668 * @param unknown_type $error 669 * @return unknown 688 * Plugins may override this form by returning true|false via the <code>request_filesystem_credentials</code> filter. 689 * 690 * @since 2.5.0 691 * 692 * @param string $form_post the URL to post the form to 693 * @param string $type the chosen Filesystem method in use 694 * @param boolean $error if the current request has failed to connect 695 * @param string $context The directory which is needed access to, The write-test will be performed on this directory by get_filesystem_method() 696 * @return boolean False on failure. True on success. 670 697 */ 671 698 function request_filesystem_credentials($form_post, $type = '', $error = false, $context = false) { 672 699 $req_cred = apply_filters('request_filesystem_credentials', '', $form_post, $type, $error, $context); … … 693 720 //sanitize the hostname, Some people might pass in odd-data: 694 721 $credentials['hostname'] = preg_replace('|\w+://|', '', $credentials['hostname']); //Strip any schemes off 695 722 696 if ( strpos($credentials['hostname'], ':') ) 723 if ( strpos($credentials['hostname'], ':') ) { 697 724 list( $credentials['hostname'], $credentials['port'] ) = explode(':', $credentials['hostname'], 2); 698 else 725 if ( ! is_numeric($credentials['port']) ) 726 unset($credentials['port']); 727 } else { 699 728 unset($credentials['port']); 729 } 700 730 701 731 if ( defined('FTP_SSH') || (defined('FS_METHOD') && 'ssh' == FS_METHOD) ) 702 732 $credentials['connection_type'] = 'ssh';