title: sys_get_temp_dir in shared hosting environment
body: <p><em>Note: This could also fit in superuser.</em></p>
  
  <p>I am setting up PHP 5.3.10 on a shared host with apache2 mpm itk and open_basedir in a way, that each user may not see or change the files of another user. In the apache2 vhost settings, I add the appropriate entries to restrict the user:</p>
  
  <pre><code>    AssignUserId     userA userA
      php_admin_value  open_basedir      /home/userA/www/
      php_admin_value  upload_tmp_dir    /home/userA/www/tmp/
      php_admin_value  session.save_path /home/userA/www/tmp/
      SetEnv           TMPDIR            /home/userA/www/tmp/
  </code></pre>
  
  <p>Now, the first line sets the linux user to use for apache2, the next three lines define the basedir, upload directory and session savepath to be in the user directory. I'll get back to the last line in a sec.</p>
  
  <p>Now for the problem: <code>sys_get_temp_dir()</code> should give back the temporary directory for php, which is /tmp be default on a linux system. For security reasons, this directory should reside in the open_basedir of userA. According to the php-source of 5.3.10, the <code>sys_get_temp_dir()</code>-function uses the environment variable TMPDIR to get this directory:</p>
  
  <pre><code>     // php-src/main/php_open_temporary_file.c:217-219
       /* On Unix use the (usual) TMPDIR environment variable. */
       {
               char* s = getenv("TMPDIR");
  </code></pre>
  
  <p>This is what the fifth line in the configuration above should do. However, <code>sys_get_temp_dir()</code> simply returns the global system directory, ignoring the environmental variable (which is perfectly set in $_SERVER, also viewable via <code>phpinfo()</code>).</p>
  
  <p>This results in some nasty bugs with various software relying on <code>sys_get_temp_dir()</code>, as that directory is outside of the <code>open_basedir</code> setting. I've tried to set the variable directly into $_ENV and $_SERVER without a change in behaviour. I've tried a <code>putenv('TMPDIR=/home/userA/www/tmp')</code> without change. </p>
  
  <p>However, I am able to change the output by defining the variable into /etc/apache2/envvars - which is useless for me, as I want each VHOST to have its own temporary folder.</p>
  
  <p>The only solution I have found so far is overwriting the internal <code>sys_get_temp_dir()</code> through an extension like <a href="http://no2.php.net/manual/en/function.runkit-function-redefine.php">runkit</a> and enforcing its inclusion via <a href="http://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file">auto_prepend_file</a>. But that solution is so dirty, I simply can't believe, that there is no better solution around.</p>
  
  <p><strong>So, my question: Is there any way to change the result of <code>sys_get_temp_dir()</code> to be set in an apache2 vhost setting, without reimplementing the function with runkit?</strong></p>
  
  <p><strong>Edit:</strong> The apache version is 2.2.22, and I currently use mod_php. As I will have to add all users manually, an fcgi or similar setup would also be possible.</p>