Example #1
0
def check_directory(directory, mode=0, form_item=None):
    """
   Check that the directory exists and is writable + Directories need to
   have execute permissions to be considered a directory by FTP servers, etc.
  
   @param directory A string containing the name of a directory path.
   @param mode A Boolean value to indicate if the directory should be created
     if it does not exist or made writable if it is read-only.
   @param form_item An optional string containing the name of a form item that
     any errors will be attached to + This is useful for settings forms that
     require the user to specify a writable directory + If it can't be made to
     work, a form error will be set preventing them from saving the settings.
   @return False when directory not found, or True when directory exists.
  """
    php.Reference.check(directory)
    directory._ = php.rtrim(directory._, '/\\')
    # Check if directory exists.
    if (not php.is_dir(directory._)):
        if ((mode & FILE_CREATE_DIRECTORY) and mkdir(directory._) != False):
            chmod(directory._, 0775)
            # Necessary for non-webserver users.
        else:
            if (form_item):
                form_set_error(form_item, \
                  t('The directory %directory does not exist.', \
                  {'%directory' : directory._}))
                watchdog('file system', 'The directory %directory does not exist.', \
                  {'%directory' : directory}, WATCHDOG_ERROR)
            return False
    # Check to see if the directory is writable.
    if (not php.is_writable(directory._)):
        if ((mode & FILE_MODIFY_PERMISSIONS)
                and not php.chmod(directory, 0775)):
            form_set_error(form_item, t('The directory %directory is not writable', \
              {'%directory' : directory._}))
            watchdog('file system', 'The directory %directory is not writable, ' + \
              'because it does not have the correct permissions set.', \
              {'%directory' : directory._}, WATCHDOG_ERROR)
            return False
Example #2
0
def check_directory(directory, mode = 0, form_item = None):
  """
   Check that the directory exists and is writable + Directories need to
   have execute permissions to be considered a directory by FTP servers, etc.
  
   @param directory A string containing the name of a directory path.
   @param mode A Boolean value to indicate if the directory should be created
     if it does not exist or made writable if it is read-only.
   @param form_item An optional string containing the name of a form item that
     any errors will be attached to + This is useful for settings forms that
     require the user to specify a writable directory + If it can't be made to
     work, a form error will be set preventing them from saving the settings.
   @return False when directory not found, or True when directory exists.
  """
  php.Reference.check(directory);
  directory._ = php.rtrim(directory._, '/\\')
  # Check if directory exists.
  if (not php.is_dir(directory._)):
    if ((mode & FILE_CREATE_DIRECTORY) and mkdir(directory._) != False):
      chmod(directory._, 0775); # Necessary for non-webserver users.
    else:
      if (form_item):
        form_set_error(form_item, \
          t('The directory %directory does not exist.', \
          {'%directory' : directory._}))
        watchdog('file system', 'The directory %directory does not exist.', \
          {'%directory' : directory}, WATCHDOG_ERROR);
      return False
  # Check to see if the directory is writable.
  if (not php.is_writable(directory._)):
    if ((mode & FILE_MODIFY_PERMISSIONS) and not php.chmod(directory, 0775)):
      form_set_error(form_item, t('The directory %directory is not writable', \
        {'%directory' : directory._}))
      watchdog('file system', 'The directory %directory is not writable, ' + \
        'because it does not have the correct permissions set.', \
        {'%directory' : directory._}, WATCHDOG_ERROR)
      return False
  if ((file_directory_path() == directory._ or \
      file_directory_temp() == directory._) and \
      not php.is_file("directory/.htaccess")):
    htaccess_lines = \
      "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\n" + \
      "Options None\nOptions +FollowSymLinks"
    fp = fopen("directory/.htaccess", 'w')
    if (fp and fputs(fp, htaccess_lines)):
      fclose(fp)
      chmod(directory._ + '/.htaccess', 0664)
    else:
      variables = {'%directory' : directory._, \
        '!htaccess' : '<br />' + php.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: " + \
        "<code>!htaccess</code>", variables))
      watchdog('security', "Security warning: Couldn't write " + \
        ".htaccess file. Please create a .htaccess file in " + \
        "your %directory directory which contains the " + \
        "following lines: <code>not htaccess</code>", \
        variables, WATCHDOG_ERROR)
  return True