Make WordPress Core

Changeset 14016


Ignore:
Timestamp:
04/06/2010 11:20:51 AM (15 years ago)
Author:
dd32
Message:

Replace use of tmpfile() with a safe get_temp_dir(). tmpfile() may use a temporary directly which is not writable. Add static caching to get_temp_dir() & better protect against bad server configs. Fixes #12866

Location:
trunk/wp-admin/includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/class-wp-filesystem-ftpext.php

    r13869 r14016  
    9393            $type = FTP_BINARY;
    9494
    95         $temp = tmpfile();
     95        $tempfile = wp_tempnam($file);
     96        $temp = fopen($tempfile, 'w+');
     97
    9698        if ( ! $temp )
    9799            return false;
     
    107109
    108110        fclose($temp);
     111        unlink($tempfile);
    109112        return $contents;
    110113    }
     
    114117
    115118    function put_contents($file, $contents, $mode = false ) {
    116         $temp = tmpfile();
     119        $tempfile = wp_tempnam($file);
     120        $temp = fopen($tempfile, 'w+');
    117121        if ( ! $temp )
    118122            return false;
     
    125129
    126130        fclose($temp);
     131        unlink($tempfile);
    127132
    128133        $this->chmod($file, $mode);
  • trunk/wp-admin/includes/file.php

    r13994 r14016  
    150150 */
    151151function get_temp_dir() {
     152    static $temp;
    152153    if ( defined('WP_TEMP_DIR') )
    153154        return trailingslashit(WP_TEMP_DIR);
     155
     156    if ( $temp )
     157        return trailingslashit($temp);
    154158
    155159    $temp = WP_CONTENT_DIR . '/';
     
    157161        return $temp;
    158162
    159     if  ( function_exists('sys_get_temp_dir') )
    160         return trailingslashit(sys_get_temp_dir());
     163    if  ( function_exists('sys_get_temp_dir') ) {
     164        $temp = sys_get_temp_dir();
     165        if ( is_writable($temp) )
     166            return trailingslashit($temp);
     167    }
    161168
    162169    $temp = ini_get('upload_tmp_dir');
    163     if ( is_dir($temp) ) // always writable
     170    if ( is_dir($temp) && is_writable($temp) )
    164171        return trailingslashit($temp);
    165172
    166     return '/tmp/';
     173    $temp = '/tmp/';
     174    return $temp;
    167175}
    168176
     
    180188 * @return string a writable filename
    181189 */
    182 function wp_tempnam($filename = '', $dir = ''){
     190function wp_tempnam($filename = '', $dir = '') {
    183191    if ( empty($dir) )
    184192        $dir = get_temp_dir();
     
    604612
    605613        if ( ! $wp_filesystem->put_contents( $to . $info['name'], $contents, FS_CHMOD_FILE) )
    606             return new WP_Error('copy_failed', __('Could not copy file.'), $to . $file['filename']);
     614            return new WP_Error('copy_failed', __('Could not copy file.'), $to . $info['filename']);
    607615    }
    608616
Note: See TracChangeset for help on using the changeset viewer.