tempnam() patch

Jon Parise jon@csh.rit.edu
Sun, 26 Nov 2000 18:09:55 -0500


---------------------- multipart/mixed attachment
Attached is a patch to imp/lib/mimetypes.php that replaces all of the
static occurences our homebrewed temporary filenames with a call to
tempnam().  Please look this over to make sure I haven't broken
anything; I don't use any of these viewers myself.

-- 
Jon Parise (jon@csh.rit.edu)  .  Rochester Inst. of Technology
http://www.csh.rit.edu/~jon/  :  Computer Science House Member

---------------------- multipart/mixed attachment
Index: mimetypes.php
===================================================================
RCS file: /cvs/horde/imp/lib/mimetypes.php,v
retrieving revision 1.34
diff -u -r1.34 mimetypes.php
--- mimetypes.php	2000/11/25 18:32:51	1.34
+++ mimetypes.php	2000/11/26 23:06:45
@@ -56,21 +56,21 @@
 	global $conf;
 	$content = mime_fetch_content($mime);
 	
-	$word_name = '/tmp/imp.word.' . date('Y-M-D_H:i:s') . '__' . md5($content);
-	horde_cleanup($word_name); // register this to get deleted at the end of the request.
+	$tmp_word = tempnam('/tmp', 'imp_word');
+	horde_cleanup($tmp_word);
 	
-	$fh = fopen($word_name, 'w');
+	$fh = fopen($tmp_word, 'w');
 	fwrite($fh, imap_base64($content));
 	fclose($fh);
 	
-	$pipe = popen($conf['utils']['wordviewer'] . " $word_name 2>&1", 'r');
+	$pipe = popen($conf['utils']['wordviewer'] . " $tmp_word 2>&1", 'r');
 	$data = '';
 	while (($rc = fgets($pipe, 8192))) {
 	    $data .= $rc;
     }
 	
     pclose($pipe);
-	unlink($word_name);
+	unlink($tmp_word);
 	
 	return $data;
 }
@@ -79,20 +79,20 @@
     global $conf;
     $content = mime_fetch_decoded_content($mime);
     
-    $xls_name = '/tmp/imp.xls.' . date('Y-M-D_H:i:s') . '__' . md5($content);
-    horde_cleanup($xls_name); // register this to get deleted at the end of the request.
+    $tmp_xls = tempnam('/tmp', 'imp_xls');
+    horde_cleanup($tmp_xls);
     
-    $fh = fopen($xls_name, 'w');
+    $fh = fopen($tmp_xls, 'w');
     fwrite($fh, $content);
     fclose($fh);
     
-    $pipe = popen($conf['utils']['excelviewer'] . " $xls_name 2>&1", 'r');
+    $pipe = popen($conf['utils']['excelviewer'] . " $tmp_xls 2>&1", 'r');
     while (($rc = fgets($pipe, 8192))) {
         $data .= $rc;
     }
     
     pclose($pipe);
-    unlink($xls_name);
+    unlink($tmp_xls);
     
     return $data;
 }
@@ -102,20 +102,21 @@
 	
 	$content = mime_fetch_content($mime);
 	
-	$rpm_name = '/tmp/imp.rpm.' . date('Y-M-D_H:i:s') . '__' . md5($content);
-	horde_cleanup($rpm_name); // register this to get deleted at the end of the request.
+	$tmp_rpm = tempnam('/tmp', 'imp_rpm');
+	horde_cleanup($tmp_rpm);
 	
-	$fh = fopen($rpm_name, 'w');
+	$fh = fopen($tmp_rpm, 'w');
 	fwrite($fh, imap_base64($content));
 	fclose($fh);
 	
-	$pipe = popen( $conf['utils']['rpm'] . " -qip $rpm_name 2>&1", 'r' );
+	$pipe = popen( $conf['utils']['rpm'] . " -qip $tmp_rpm 2>&1", 'r' );
 	$data = '<b><u>Query RPM Package</u></b><br><table><tr><td align=left><pre>';
 	
 	$re = '';
 	while (($rc = fgets($pipe, 10)))
 		$re .= $rc;
 	pclose($pipe);
+    unlink($tmp_rpm);
 	$data .= htmlentities($re);
 	$data .= '</pre></td></tr></table>';
 	
@@ -165,20 +166,21 @@
     
     $content = mime_fetch_content($mime);
     
-    $tgz_name = '/tmp/imp.' . date('s') . '__' . md5($content) . '.tgz';
-    horde_cleanup($tgz_name); // make sure this is deleted at the end of the request
+    $tmp_tgz = tempnam('/tmp', 'imp_tgz');
+    horde_cleanup($tmp_tgz);
     
-    $fh = fopen($tgz_name, 'w');
+    $fh = fopen($tmp_tgz, 'w');
     fwrite($fh, imap_base64($content));
     fclose($fh);
     
-    $pipe = popen($conf['utils']['tar'] . " tvzf $tgz_name 2>&1", 'r');
+    $pipe = popen($conf['utils']['tar'] . " tvzf $tmp_tgz 2>&1", 'r');
     $data = '<b><u>Query Gzipped Tarball</u></b><br><table><tr><td align=left><pre>';
     
     $re = '';
     while (($rc=fgets($pipe,10)))
       $re .= $rc;
     pclose($pipe);
+    unlink($tmp_tgz);
     
     $data .= htmlspecialchars($re);
     $data .= '</pre></td></tr></table>';
@@ -201,20 +203,21 @@
   
 	$content = mime_fetch_content($mime);
 	
-	$tar_name = '/tmp/imp.' . date('s') . '__' . md5($content) . '.tar';
-	horde_cleanup($tar_name); // make sure this is deleted at the end of the request
+	$tmp_tar = tempnam('/tmp', 'imp_tar');
+	horde_cleanup($tmp_tar);
 	
-	$fh = fopen($tar_name, 'w');
+	$fh = fopen($tmp_tar, 'w');
 	fwrite($fh, imap_base64($content));
 	fclose($fh);
   
-	$pipe = popen($conf['utils']['tar'] . " tvf $tar_name 2>&1", 'r');
+	$pipe = popen($conf['utils']['tar'] . " tvf $tmp_tar 2>&1", 'r');
 	$data = '<b><u>Query Tarball</u></b><br><table><tr><td align=left><pre>';
 	
 	$re = '';
 	while (($rc=fgets($pipe,10)))
 		$re .= $rc;
 	pclose($pipe);
+    unlink($tmp_tar);
 	
 	$data .= htmlspecialchars($re);
 	$data .= '</pre></td></tr></table>';
@@ -227,23 +230,24 @@
 	
 	$content = mime_fetch_content($mime);
 	
-	$zip_name = '/tmp/imp.zip.' . date('Y-M-D_H:i:s') . '__' . md5($content);
-	horde_cleanup($zip_name); // make sure this is deleted at the end of the request
+	$tmp_zip = tempnam('/tmp', 'imp_zip');
+	horde_cleanup($tmp_zip);
 	
-	$fh = fopen($zip_name, 'w');
+	$fh = fopen($tmp_zip, 'w');
 	fwrite($fh, imap_base64($content));
 	fclose($fh);
 	
-	$pipe = popen($conf['utils']['unzip'] . " -m $zip_name  2>&1 |grep -v Archive", 'r');
+	$pipe = popen($conf['utils']['unzip'] . " -m $tmp_zip  2>&1 |grep -v Archive", 'r');
 	$data = '<b><u>Contents of ZIP file</u></b><br><table><tr><td align=left><pre>';
 	while (($rc=fgets($pipe,10)))
 		$data.= $rc;
 	pclose($pipe);
 	$data .= '</pre></td></tr></table><br><b><u>Details of ZIP file</u></b><br><table><tr><td align="left"><pre>';
-	$pipe = popen($conf['utils']['unzip'] . " -mvh $zip_name  2>&1 |grep -v Archive", 'r');
+	$pipe = popen($conf['utils']['unzip'] . " -mvh $tmp_zip  2>&1 |grep -v Archive", 'r');
 	while (($rc=fgets($pipe,10)))
 		$data.= $rc;
 	pclose($pipe);
+    unlink($tmp_zip);
 	$data .= '</pre></td></tr></table>';
 	
 	return $data;
@@ -254,20 +258,21 @@
 	
 	$content = mime_fetch_content($mime);
 	
-	$deb_name = '/tmp/imp.' . date('Y-M-D_H:i:s') . '__' . md5($content) . ".deb";
-	horde_cleanup($deb_name); // make sure this is deleted at the end of the request
+	$tmp_deb = tempnam('/tmp', 'imp_deb');
+	horde_cleanup($tmp_deb);
 	
-	$fh = fopen($deb_name, 'w');
+	$fh = fopen($tmp_deb, 'w');
 	fwrite($fh, imap_base64($content));
 	fclose($fh);
 	
-	$pipe = popen( $conf['utils']['dpkg'] . " -f $deb_name 2>&1", 'r' );
+	$pipe = popen( $conf['utils']['dpkg'] . " -f $tmp_deb 2>&1", 'r' );
 	$data = '<b><u>Query Debian Package</u></b><br><table><tr><td align=left><pre>';
 	
 	$re = '';
 	while(($rc=fgets($pipe,10)))
 		$re.= $rc;
 	pclose($pipe);
+    unlink($tmp_deb);
 	$data .= htmlentities($re);
 	$data .="</pre></td></tr></table>";
 	
@@ -279,15 +284,15 @@
 	
 	$content = mime_fetch_content($mime);
 	
-	$gpg_name = '/tmp/imp.sig.' . date('Y-M-D_H:i:s') . '.txt.asc';
-	horde_cleanup($gpg_name);
+	$tmp_gpg = tempnam('/tmp', 'imp_gpg');
+	horde_cleanup($tmp_gpg);
 	
-	$fh = fopen($gpg_name, 'w');
+	$fh = fopen($tmp_gpg, 'w');
 	fwrite($fh, $content);
 	fclose($fh);
 	
-	$gpg_body = '/tmp/imp.sig.' . date('Y-M-D_H:i:s') . '.txt';
-	horde_cleanup($gpg_body);
+	$tmp_gpg_body = tempnam('/tmp', 'imp_gpg_body');
+	horde_cleanup($tmp_gpg_body);
 	
 	// the - 1 here seems pretty iffy...
 	$tmsg = @imap_fetchbody($imp['stream'], $index, $mime->imap_id - 1, FT_UID);
@@ -310,7 +315,7 @@
 	$rmsg .= $hmsg;
 	$rmsg .= "\n";
 	$rmsg .= $tmsg;
-	$fi = fopen($gpg_body, 'w');
+	$fi = fopen($tmp_gpg_body, 'w');
 	fwrite($fi, $rmsg);
 	fclose($fi);
 	
@@ -324,13 +329,15 @@
 		$gpg_flags = '';  // need pgp5 flags
 	}
 	
-	$pipe = popen('export HOME=' . $conf['pgp']['conf_dir'] . ';' . $conf['utils']['gpg'] . " $gpg_flags $gpg_name $gpg_body 2>&1", 'r');
+	$pipe = popen('export HOME=' . $conf['pgp']['conf_dir'] . ';' . $conf['utils']['gpg'] . " $gpg_flags $tmp_gpg $tmp_gpg_body 2>&1", 'r');
 	$data = '<b><u>Verification of GPG/PGP Signature</u></b><br><table><tr><td align=left><pre>';
 	
 	$re = '';
 	while (($rc = fgets($pipe,10)))
 		$re .= $rc;
 	pclose($pipe);
+    unlink($tmp_gpg);
+    unlink($tmp_gpg_body);
 	$data .= htmlentities($re);
 	$data .= '</pre></td></tr></table>';
 	

---------------------- multipart/mixed attachment--