You are here

Drupal and safe mode

Drupal 6.x with small modification can also be used on servers with Safe Mode ON (safe_mode=1). Standard distribution of Drupal doesn't support Safe Mode out of the box. Joomla has support already build-in. Safe Mode ON can be bypassed with FTP directory functions.

Add following function to includes/file.inc:

function mkdir_ftp($directory) {
  $directory = "your_drupal_ftp_base_folder/" . $directory;
  $ftpConn = ftp_connect("ftp.your.domain");
  if(false == ftp_login($ftpConn, "your_login", "your_password")) {
    watchdog('file system',
        'The directory cannot be created, ftp_login() failed.',
        array(), WATCHDOG_ERROR);
    return FALSE;
  }
  if(false == ftp_mkdir($ftpConn, $directory)) {
    watchdog('file system',
        'The directory %directory cannot be created, ftp_mkdir() failed.',
        array('%directory' => $directory), WATCHDOG_ERROR);
    return FALSE;
  }
  if(false == ftp_chmod($ftpConn, 0777, $directory)) {
    watchdog('file system',
        'The directory %directory cannot be created, ftp_chmod() failed.',
        array('%directory' => $directory), WATCHDOG_ERROR);
    return FALSE;
  }
  ftp_close($ftpConn);
  
  return TRUE;
}

and call it instead of PHP mkdir() in the same file, in function file_check_directory():

function file_check_directory(&$directory, $mode = 0, $form_item = NULL) {
  $directory = rtrim($directory, '/\\');

  // Check if directory exists.
  if (!is_dir($directory)) {
    if (($mode & FILE_CREATE_DIRECTORY) && @mkdir_ftp($directory)) {
      drupal_set_message(t('The directory %directory has been created.', array('%directory' => $directory)));
      @chmod($directory, 0775); // Necessary for non-webserver users.
    }
...

Comments

It will not work if recursion is needed
(I had to remove the watchdog stuff, because if was not able to retrieve the directory listing to do correct tests)
Thanks for the trick anyway ...

function mkdir_ftp($directory) {
global $ftpuser,$ftppass,$ftpmkdir,$ftpdir;
$directory = $ftpdir . $directory;
$ftpConn = ftp_connect($_SERVER['SERVER_NAME']);
if(false == ftp_login($ftpConn,$ftpuser , $ftppass)) {
watchdog('file system',
'The directory cannot be created, ftp_login() failed.',
array(), WATCHDOG_ERROR);
return FALSE;
}
$list=split("/",$directory);
$directory="";$first=true;
foreach($list as $ldir){
if ($first)
$directory.="".$ldir;
else
$directory.="/".$ldir;
$first=false;
ftp_mkdir($ftpConn, $directory));
ftp_chmod($ftpConn, 0777, $directory)
}
ftp_close($ftpConn);
return TRUE;
}

Thanks for pointing out the missing recursion and sharing your code.

only two mistakes on the code... one ; missing and one ) repeated

function mkdir_ftp($directory) {
global $ftpuser,$ftppass,$ftpmkdir,$ftpdir;
$directory = $ftpdir . $directory;
$ftpConn = ftp_connect($_SERVER['use your server name here']);
if(false == ftp_login($ftpConn,$ftpuser , $ftppass)) {
watchdog('file system',
'The directory cannot be created, ftp_login() failed.',
array(), WATCHDOG_ERROR);
return FALSE;
}
$list=split("/",$directory);
$directory="";$first=true;
foreach($list as $ldir){
if ($first)
$directory.="".$ldir;
else
$directory.="/".$ldir;
$first=false;
ftp_mkdir($ftpConn, $directory);
ftp_chmod($ftpConn, 0777, $directory);
}
ftp_close($ftpConn);
return TRUE;
}