You are here

function _filefield_paths_check_directory in File (Field) Paths 6

Same name and namespace in other branches
  1. 5 filefield_paths.module \_filefield_paths_check_directory()
1 call to _filefield_paths_check_directory()
filefield_paths_file_move in ./filefield_paths.module
Move file and update its database record.

File

./filefield_paths.module, line 763
Contains core functions for the FileField Paths module.

Code

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

  // error if dir is a file.
  if (is_file($directory)) {
    watchdog('file system', 'check_directory: The path  %directory is a file.', array(
      '%directory' => $directory,
    ), WATCHDOG_ERROR);
    if ($form_item) {
      form_set_error($form_item, t('The directory %directory is a file!', array(
        '%directory' => $directory,
      )));
    }
    return FALSE;
  }

  // create the directory if it is missing.
  if (!is_dir($directory) && $mode & FILE_CREATE_DIRECTORY && !@mkdir($directory, 0775, TRUE)) {
    watchdog('file system', 'The directory %directory does not exist.', array(
      '%directory' => $directory,
    ), WATCHDOG_ERROR);
    if ($form_item) {
      form_set_error($form_item, t('The directory %directory does not exist.', array(
        '%directory' => $directory,
      )));
    }
    return FALSE;
  }

  // Check to see if the directory is writable.
  if (!is_writable($directory) && $mode & FILE_MODIFY_PERMISSIONS && !@chmod($directory, 0775)) {
    watchdog('file system', 'The directory %directory is not writable, because it does not have the correct permissions set.', array(
      '%directory' => $directory,
    ), WATCHDOG_ERROR);
    if ($form_item) {
      form_set_error($form_item, t('The directory %directory is not writable', array(
        '%directory' => $directory,
      )));
    }
    return FALSE;
  }
  if ((file_directory_path() == $directory || file_directory_temp() == $directory) && !is_file("{$directory}/.htaccess")) {
    $htaccess_lines = "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\nOptions None\nOptions +FollowSymLinks";
    if (($fp = fopen("{$directory}/.htaccess", 'w')) && fputs($fp, $htaccess_lines)) {
      fclose($fp);
      chmod("{$directory}/.htaccess", 0664);
    }
    else {
      $repl = array(
        '%directory' => $directory,
        '!htaccess' => nl2br(check_plain($htaccess_lines)),
      );
      form_set_error($form_item, t("Security warning: Couldn't write .htaccess file. Please create a .htaccess file in your %directory directory which contains the following lines:<br /><code>!htaccess</code>", $repl));
      watchdog('security', "Security warning: Couldn't write .htaccess file.  Please create a .htaccess file in your %directory directory which contains the following lines:<br /><code>!htaccess</code>", $repl, WATCHDOG_ERROR);
    }
  }
  return TRUE;
}