<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[14016] trunk/wp-admin/includes: Replace use of tmpfile()
  with a safe get_temp_dir().</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/14016">14016</a></dd>
<dt>Author</dt> <dd>dd32</dd>
<dt>Date</dt> <dd>2010-04-06 11:20:51 +0000 (Tue, 06 Apr 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>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() &amp; better protect against bad server configs. Fixes <a href="http://trac.wordpress.org/ticket/12866">#12866</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminincludesclasswpfilesystemftpextphp">trunk/wp-admin/includes/class-wp-filesystem-ftpext.php</a></li>
<li><a href="#trunkwpadminincludesfilephp">trunk/wp-admin/includes/file.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminincludesclasswpfilesystemftpextphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/class-wp-filesystem-ftpext.php (14015 => 14016)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/class-wp-filesystem-ftpext.php        2010-04-06 11:20:47 UTC (rev 14015)
+++ trunk/wp-admin/includes/class-wp-filesystem-ftpext.php        2010-04-06 11:20:51 UTC (rev 14016)
</span><span class="lines">@@ -92,7 +92,9 @@
</span><span class="cx">                 if ( empty($type) )
</span><span class="cx">                         $type = FTP_BINARY;
</span><span class="cx"> 
</span><del>-                $temp = tmpfile();
</del><ins>+                $tempfile = wp_tempnam($file);
+                $temp = fopen($tempfile, 'w+');
+
</ins><span class="cx">                 if ( ! $temp )
</span><span class="cx">                         return false;
</span><span class="cx"> 
</span><span class="lines">@@ -106,6 +108,7 @@
</span><span class="cx">                         $contents .= fread($temp, 8192);
</span><span class="cx"> 
</span><span class="cx">                 fclose($temp);
</span><ins>+                unlink($tempfile);
</ins><span class="cx">                 return $contents;
</span><span class="cx">         }
</span><span class="cx">         function get_contents_array($file) {
</span><span class="lines">@@ -113,7 +116,8 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         function put_contents($file, $contents, $mode = false ) {
</span><del>-                $temp = tmpfile();
</del><ins>+                $tempfile = wp_tempnam($file);
+                $temp = fopen($tempfile, 'w+');
</ins><span class="cx">                 if ( ! $temp )
</span><span class="cx">                         return false;
</span><span class="cx"> 
</span><span class="lines">@@ -124,6 +128,7 @@
</span><span class="cx">                 $ret = @ftp_fput($this-&gt;link, $file, $temp, $type);
</span><span class="cx"> 
</span><span class="cx">                 fclose($temp);
</span><ins>+                unlink($tempfile);
</ins><span class="cx"> 
</span><span class="cx">                 $this-&gt;chmod($file, $mode);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpadminincludesfilephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/file.php (14015 => 14016)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/file.php        2010-04-06 11:20:47 UTC (rev 14015)
+++ trunk/wp-admin/includes/file.php        2010-04-06 11:20:51 UTC (rev 14016)
</span><span class="lines">@@ -149,21 +149,29 @@
</span><span class="cx">  * @return string Writable temporary directory
</span><span class="cx">  */
</span><span class="cx"> function get_temp_dir() {
</span><ins>+        static $temp;
</ins><span class="cx">         if ( defined('WP_TEMP_DIR') )
</span><span class="cx">                 return trailingslashit(WP_TEMP_DIR);
</span><span class="cx"> 
</span><ins>+        if ( $temp )
+                return trailingslashit($temp);
+
</ins><span class="cx">         $temp = WP_CONTENT_DIR . '/';
</span><span class="cx">         if ( is_dir($temp) &amp;&amp; is_writable($temp) )
</span><span class="cx">                 return $temp;
</span><span class="cx"> 
</span><del>-        if  ( function_exists('sys_get_temp_dir') )
-                return trailingslashit(sys_get_temp_dir());
</del><ins>+        if  ( function_exists('sys_get_temp_dir') ) {
+                $temp = sys_get_temp_dir();
+                if ( is_writable($temp) )
+                        return trailingslashit($temp);
+        }
</ins><span class="cx"> 
</span><span class="cx">         $temp = ini_get('upload_tmp_dir');
</span><del>-        if ( is_dir($temp) ) // always writable
</del><ins>+        if ( is_dir($temp) &amp;&amp; is_writable($temp) )
</ins><span class="cx">                 return trailingslashit($temp);
</span><span class="cx"> 
</span><del>-        return '/tmp/';
</del><ins>+        $temp = '/tmp/';
+        return $temp;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -179,7 +187,7 @@
</span><span class="cx">  * @param string $dir (optional) Directory to store the file in
</span><span class="cx">  * @return string a writable filename
</span><span class="cx">  */
</span><del>-function wp_tempnam($filename = '', $dir = ''){
</del><ins>+function wp_tempnam($filename = '', $dir = '') {
</ins><span class="cx">         if ( empty($dir) )
</span><span class="cx">                 $dir = get_temp_dir();
</span><span class="cx">         $filename = basename($filename);
</span><span class="lines">@@ -603,7 +611,7 @@
</span><span class="cx">                         return new WP_Error('extract_failed', __('Could not extract file from archive.'), $info['name']);
</span><span class="cx"> 
</span><span class="cx">                 if ( ! $wp_filesystem-&gt;put_contents( $to . $info['name'], $contents, FS_CHMOD_FILE) )
</span><del>-                        return new WP_Error('copy_failed', __('Could not copy file.'), $to . $file['filename']);
</del><ins>+                        return new WP_Error('copy_failed', __('Could not copy file.'), $to . $info['filename']);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         $z-&gt;close();
</span></span></pre>
</div>
</div>

</body>
</html>