compose.php 1.7 KB

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