6 * Functions for message compositon: writing a message, attaching files etc.
8 * @author Thijs Kinkhorst <kink at squirrelmail.org>
9 * @copyright © 1999-2007 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @package squirrelmail
17 * Get a new file to write an attachment to.
18 * This function makes sure it doesn't overwrite other attachments,
19 * preventing collisions and race conditions.
24 function sq_get_attach_tempfile()
26 global $username, $attachment_dir;
28 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
30 // using PHP >= 4.3.2 we can be truly atomic here
31 $filemods = check_php_version ( 4,3,2 ) ?
'x' : 'w';
33 // give up after 1000 tries
35 for ($try=0; $try<$TMP_MAX; ++
$try) {
37 $localfilename = GenerateRandomString(32, '', 7);
38 $full_localfilename = "$hashed_attachment_dir/$localfilename";
40 // filename collision. try again
41 if ( file_exists($full_localfilename) ) {
45 // try to open for (binary) writing
46 $fp = @fopen
( $full_localfilename, $filemods);
48 if ( $fp !== FALSE ) {
49 // success! make sure it's not readable, close and return filename
50 chmod($full_localfilename, 0600);
52 return $full_localfilename;
56 // we tried 1000 times but didn't succeed.
57 error_box( _("Could not open temporary file to store attachment. Contact your system administrator to resolve this issue.") );