00621: Safe_mode prevents sub-directories creation

Summary: Safe_mode prevents sub-directories creation
Created: 2005-12-15 13:44
Status: Open
Category: Feature
From: JFH
Assigned:
Priority: 33
Version: 2.0.12
OS: Linux/Apache/PHP 4.x

Description: When PHP safe_mode is enabled, everything works fine, except sub-directories creation.

I use per-group subdirectories and my webhost has enabled safe_mode, so creating a new group doesn't work (I get 'PmWiki needs to have a writable ...').

I found the following solution, for function mkdirp($dir):

function mkdirp($dir) {
  global $ScriptUrl, $FtpServer, $FtpLogin, $FtpPassword;
  if (file_exists($dir)) return;
  if (!file_exists(dirname($dir))) mkdirp(dirname($dir));

  /*BEGIN CUSTOM CODE*/

  $safemode = ini_get('safe_mode');
  if($safemode) {
	$conn_id = ftp_connect($FtpServer);
  	$login_result = ftp_login($conn_id, $FtpLogin, $FtpPassword);

  	if(ftp_mkdir($conn_id, $dir)) {
  		ftp_close($conn_id);
  		fixperms($dir);
    	if (@touch("$dir/xxx")) { unlink("$dir/xxx"); return; }
    	rmdir($dir);
 	 }

  	else ftp_close($conn_id);
  }

  else {
  	if (mkdir($dir, 0777)) {
    	fixperms($dir);
    	if (@touch("$dir/xxx")) { unlink("$dir/xxx"); return; }
    	rmdir($dir);
  	}
  }

  /*END CUSTOM CODE*/

  $parent = realpath(dirname($dir)); 
  $perms = decoct(fileperms($parent) & 03777);
  $msg = "PmWiki needs to have a writable <tt>$dir/</tt> directory 
    before it can continue.  You can create the directory manually 
    by executing the following commands on your server:
    <pre>    mkdir $parent/$dir\n    chmod 777 $parent/$dir</pre>
    Then, <a href='$ScriptUrl'>reload this page</a>.";
  $safemode = ini_get('safe_mode');
  if (!$safemode) $msg .= "<br /><br />Or, for a slightly more 
    secure installation, try executing <pre>    chmod 2777 $parent</pre> 
    on your server and following <a target='_blank' href='$ScriptUrl'>
    this link</a>.  Afterwards you can restore the permissions to 
    their current setting by executing <pre>    chmod $perms $parent</pre>.";
  Abort($msg);
}

You'll have to define $FtpServer, $FtpLogin, $FtpPassword in local/config.php. Now, sub-directories creation works fine. --JFH