Beispiel #1
0
def scan_directory(dir, mask, nomask = ['.', '..', 'CVS'], \
    callback = 0, recurse = True, key = 'filename', min_depth = 0, depth = 0):
  """
   Finds all files that match a given mask in a given directory.
   Directories and files beginning with a period are excluded; this
   prevents hidden files and directories (such as SVN working directories)
   from being scanned.
  
   @param dir
     The base directory for the scan, without trailing slash.
   @param mask
     The regular expression of the files to find.
   @param nomask
     An array of files/directories to ignore.
   @param callback
     The callback function to call for each match.
   @param recurse
     When True, the directory scan will recurse the entire tree
     starting at the provided directory.
   @param key
     The key to be used for the returned array of files + Possible
     values are "filename", for the path starting with dir,
     "basename", for the basename of the file, and "name" for the name
     of the file without an extension.
   @param min_depth
     Minimum depth of directories to return files from.
   @param depth
     Current depth of recursion + This parameter is only used
     internally and should not be passed.
  
   @return
     An associative array (keyed on the provided key) of objects with
     "path", "basename", and "name" members corresponding to the
     matching files.
  """
  key = (key if php.in_array(key, \
    ('filename', 'basename', 'name')) else 'filename')
  files = []
  if php.is_dir(dir):
    dir_files = php.scandir(dir)
    for file in dir_files:
      if (not php.in_array(file, nomask) and file[0] != '.'):
        if (php.is_dir("%s/%s" % (dir, file)) and recurse):
          # Give priority to files in this folder by
          # merging them in after any subdirectory files.
          files = php.array_merge(file_scan_directory("%s/%s" % (dir, file), \
            mask, nomask, callback, recurse, key, min_depth, depth + 1), files)
        elif (depth >= min_depth and ereg(mask, file)):
          # Always use this match over anything already
          # set in files with the same $key.
          filename = "%s/%s" % (dir, file)
          basename_ = php.basename(file)
          name = php.substr(basename_, 0, php.strrpos(basename_, '.'))
          files[key] = php.stdClass()
          files[key].filename = filename
          files[key].basename = basename_
          files[key].name = name
          if (callback):
            callback(filename)
  return files
Beispiel #2
0
def directory_temp():
    """
   Determine the default temporary directory.
  
   @return A string containing a temp directory.
  """
    temporary_directory = variable_get('file_directory_temp', None)
    if (is_None(temporary_directory)):
        directories = []
        # Has PHP been set with an upload_tmp_dir?
        if (ini_get('upload_tmp_dir')):
            directories.append(ini_get('upload_tmp_dir'))
        # Operating system specific dirs.
        if (php.substr(PHP_OS, 0, 3) == 'WIN'):
            directories.append('c:\\windows\\temp')
            directories.append('c:\\winnt\\temp')
            path_delimiter = '\\'
        else:
            directories.append('/tmp')
            path_delimiter = '/'
        for directory in directories:
            if (not temporary_directory and php.is_dir(directory)):
                temporary_directory = directory
        # if a directory has been found, use it,
        # otherwise default to 'files/tmp' or 'files\\tmp'
        temporary_directory = (temporary_directory if \
          (temporary_directory != None) else \
          (file_directory_path() +  path_delimiter + 'tmp'))
        variable_set('file_directory_temp', temporary_directory)
    return temporary_directory
Beispiel #3
0
def directory_temp():
  """
   Determine the default temporary directory.
  
   @return A string containing a temp directory.
  """
  temporary_directory = variable_get('file_directory_temp', None)
  if (is_None(temporary_directory)):
    directories = []
    # Has PHP been set with an upload_tmp_dir?
    if (ini_get('upload_tmp_dir')):
      directories.append( ini_get('upload_tmp_dir') )
    # Operating system specific dirs.
    if (php.substr(PHP_OS, 0, 3) == 'WIN'):
      directories.append( 'c:\\windows\\temp' )
      directories.append( 'c:\\winnt\\temp' )
      path_delimiter = '\\'
    else:
      directories.append( '/tmp' )
      path_delimiter = '/'
    for directory in directories:
      if (not temporary_directory and php.is_dir(directory)):
        temporary_directory = directory
    # if a directory has been found, use it,
    # otherwise default to 'files/tmp' or 'files\\tmp'
    temporary_directory = (temporary_directory if \
      (temporary_directory != None) else \
      (file_directory_path() +  path_delimiter + 'tmp'))
    variable_set('file_directory_temp', temporary_directory)
  return temporary_directory
Beispiel #4
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
Beispiel #5
0
def scan_directory(dir, mask, nomask = ['.', '..', 'CVS'], \
    callback = 0, recurse = True, key = 'filename', min_depth = 0, depth = 0):
    """
   Finds all files that match a given mask in a given directory.
   Directories and files beginning with a period are excluded; this
   prevents hidden files and directories (such as SVN working directories)
   from being scanned.
  
   @param dir
     The base directory for the scan, without trailing slash.
   @param mask
     The regular expression of the files to find.
   @param nomask
     An array of files/directories to ignore.
   @param callback
     The callback function to call for each match.
   @param recurse
     When True, the directory scan will recurse the entire tree
     starting at the provided directory.
   @param key
     The key to be used for the returned array of files + Possible
     values are "filename", for the path starting with dir,
     "basename", for the basename of the file, and "name" for the name
     of the file without an extension.
   @param min_depth
     Minimum depth of directories to return files from.
   @param depth
     Current depth of recursion + This parameter is only used
     internally and should not be passed.
  
   @return
     An associative array (keyed on the provided key) of objects with
     "path", "basename", and "name" members corresponding to the
     matching files.
  """
    key = (key if php.in_array(key, \
      ('filename', 'basename', 'name')) else 'filename')
    files = []
    if php.is_dir(dir):
        dir_files = php.scandir(dir)
        for file in dir_files:
            if (not php.in_array(file, nomask) and file[0] != '.'):
                if (php.is_dir("%s/%s" % (dir, file)) and recurse):
                    # Give priority to files in this folder by
                    # merging them in after any subdirectory files.
                    files = php.array_merge(file_scan_directory("%s/%s" % (dir, file), \
                      mask, nomask, callback, recurse, key, min_depth, depth + 1), files)
                elif (depth >= min_depth and ereg(mask, file)):
                    # Always use this match over anything already
                    # set in files with the same $key.
                    filename = "%s/%s" % (dir, file)
                    basename_ = php.basename(file)
                    name = php.substr(basename_, 0,
                                      php.strrpos(basename_, '.'))
                    files[key] = php.stdClass()
                    files[key].filename = filename
                    files[key].basename = basename_
                    files[key].name = name
                    if (callback):
                        callback(filename)
    return files
Beispiel #6
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