Make WordPress Core

Ticket #10779: 10779.diff

File 10779.diff, 11.6 KB (added by dd32, 15 years ago)
  • wp-admin/includes/file.php

     
    9999}
    100100
    101101/**
    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.
    103104 *
    104  * @since unknown
     105 * @since 2.6.0
    105106 *
    106  * @param string $folder Optional. Full path to folder
    107  * @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
    109110 */
    110111function list_files( $folder = '', $levels = 100 ) {
    111112        if( empty($folder) )
     
    135136}
    136137
    137138/**
    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/
    139141 *
    140  * @since unknown
     142 * 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.
    141143 *
    142  * @return unknown
     144 * @since 2.5.0
     145 *
     146 * @return string Writable temporary directory
    143147 */
    144148function get_temp_dir() {
    145149        if ( defined('WP_TEMP_DIR') )
     
    156160}
    157161
    158162/**
    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.
    160165 *
    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.
    162168 *
    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
    166174 */
    167175function wp_tempnam($filename = '', $dir = ''){
    168176        if ( empty($dir) )
     
    425433}
    426434
    427435/**
    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.
    429438 *
    430  * @since unknown
    431  * @todo Transition over to using the new HTTP Request API (jacob).
     439 * @since 2.5.0
    432440 *
    433441 * @param string $url the URL of the file to download
    434442 * @return mixed WP_Error on failure, string Filename on success.
     
    467475}
    468476
    469477/**
    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.
    471480 *
    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.
    473483 *
    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
    477489 */
    478490function unzip_file($file, $to) {
    479491        global $wp_filesystem;
     
    481493        if ( ! $wp_filesystem || !is_object($wp_filesystem) )
    482494                return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    483495
    484         // Unzip uses a lot of memory
     496        // Unzip uses a lot of memory, But not this much hopefully
    485497        @ini_set('memory_limit', '256M');
    486498
    487         $fs =& $wp_filesystem;
    488 
    489499        require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php');
    490500
    491501        $archive = new PclZip($file);
     
    497507        if ( 0 == count($archive_files) )
    498508                return new WP_Error('empty_archive', __('Empty archive'));
    499509
    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);
    512512
    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-- ) {
    518517                        if ( empty($path[$i]) )
    519518                                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;
    529523                }
     524        }
    530525
    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        for ( $i = 0; $i < count($needed_dirs); $i++ )
     535                if ( ! $wp_filesystem->is_dir($needed_dirs[$i]) && ! $wp_filesystem->mkdir($needed_dirs[$i], FS_CHMOD_DIR) )
     536                        return new WP_Error('mkdir_failed', __('Could not create directory'), $needed_dirs[$i]);
     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);
    537545        }
    538546        return true;
    539547}
    540548
    541549/**
    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.
    543552 *
    544  * @since unknown
     553 * @since 2.5.0
    545554 *
    546  * @param unknown_type $from
    547  * @param unknown_type $to
    548  * @return unknown
     555 * @param string $from source directory
     556 * @param string $to destination directory
     557 * @return mixed WP_Error on failure, True on success.
    549558 */
    550559function copy_dir($from, $to) {
    551560        global $wp_filesystem;
     
    574583                                return $result;
    575584                }
    576585        }
     586        return true;
    577587}
    578588
    579589/**
    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.
    581592 *
    582  * @since unknown
     593 * Plugins may add extra transports, And force WordPress to use them by returning the filename via the 'filesystem_method_file' filter.
    583594 *
    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
    586600 */
    587601function WP_Filesystem( $args = false, $context = false ) {
    588602        global $wp_filesystem;
     
    627641}
    628642
    629643/**
    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())
    631646 *
    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.
    633652 *
    634  * @param unknown_type $args
     653 * @since 2.5.0
     654 *
     655 * @param array $args Connection details.
    635656 * @param string $context Full path to the directory that is tested for being writable.
    636  * @return unknown
     657 * @return string The transport to use, see description for valid return values.
    637658 */
    638659function get_filesystem_method($args = array(), $context = false) {
    639660        $method = defined('FS_METHOD') ? FS_METHOD : false; //Please ensure that this is either 'direct', 'ssh', 'ftpext' or 'ftpsockets'
     
    659680}
    660681
    661682/**
    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.
    663685 *
    664  * @since unknown
     686 * Hostnames may be in the form of hostname:portnumber (eg: wordpress.org:2467) to specify an alternate FTP/SSH port.
    665687 *
    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.
    670697 */
    671698function request_filesystem_credentials($form_post, $type = '', $error = false, $context = false) {
    672699        $req_cred = apply_filters('request_filesystem_credentials', '', $form_post, $type, $error, $context);
     
    693720        //sanitize the hostname, Some people might pass in odd-data:
    694721        $credentials['hostname'] = preg_replace('|\w+://|', '', $credentials['hostname']); //Strip any schemes off
    695722
    696         if ( strpos($credentials['hostname'], ':') )
     723        if ( strpos($credentials['hostname'], ':') ) {
    697724                list( $credentials['hostname'], $credentials['port'] ) = explode(':', $credentials['hostname'], 2);
    698         else
     725                if ( ! is_numeric($credentials['port']) )
     726                        unset($credentials['port']);
     727        } else {
    699728                unset($credentials['port']);
     729        }
    700730
    701731        if ( defined('FTP_SSH') || (defined('FS_METHOD') && 'ssh' == FS_METHOD) )
    702732                $credentials['connection_type'] = 'ssh';