webmaster@1: TRUE));
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Make sure the destination is a complete path and resides in the file system
webmaster@1: * directory, if it is not prepend the file system directory.
webmaster@1: *
webmaster@1: * @param $dest A string containing the path to verify. If this value is
webmaster@1: * omitted, Drupal's 'files' directory will be used.
webmaster@1: * @return A string containing the path to file, with file system directory
webmaster@1: * appended if necessary, or FALSE if the path is invalid (i.e. outside the
webmaster@1: * configured 'files' or temp directories).
webmaster@1: */
webmaster@1: function file_create_path($dest = 0) {
webmaster@1: $file_path = file_directory_path();
webmaster@1: if (!$dest) {
webmaster@1: return $file_path;
webmaster@1: }
webmaster@1: // file_check_location() checks whether the destination is inside the Drupal files directory.
webmaster@1: if (file_check_location($dest, $file_path)) {
webmaster@1: return $dest;
webmaster@1: }
webmaster@1: // check if the destination is instead inside the Drupal temporary files directory.
webmaster@1: else if (file_check_location($dest, file_directory_temp())) {
webmaster@1: return $dest;
webmaster@1: }
webmaster@1: // Not found, try again with prefixed directory path.
webmaster@1: else if (file_check_location($file_path .'/'. $dest, $file_path)) {
webmaster@1: return $file_path .'/'. $dest;
webmaster@1: }
webmaster@1: // File not found.
webmaster@1: return FALSE;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Check that the directory exists and is writable. Directories need to
webmaster@1: * have execute permissions to be considered a directory by FTP servers, etc.
webmaster@1: *
webmaster@1: * @param $directory A string containing the name of a directory path.
webmaster@1: * @param $mode A Boolean value to indicate if the directory should be created
webmaster@1: * if it does not exist or made writable if it is read-only.
webmaster@1: * @param $form_item An optional string containing the name of a form item that
webmaster@1: * any errors will be attached to. This is useful for settings forms that
webmaster@1: * require the user to specify a writable directory. If it can't be made to
webmaster@1: * work, a form error will be set preventing them from saving the settings.
webmaster@1: * @return FALSE when directory not found, or TRUE when directory exists.
webmaster@1: */
webmaster@1: function file_check_directory(&$directory, $mode = 0, $form_item = NULL) {
webmaster@1: $directory = rtrim($directory, '/\\');
webmaster@1:
webmaster@1: // Check if directory exists.
webmaster@1: if (!is_dir($directory)) {
webmaster@1: if (($mode & FILE_CREATE_DIRECTORY) && @mkdir($directory)) {
webmaster@1: drupal_set_message(t('The directory %directory has been created.', array('%directory' => $directory)));
webmaster@1: @chmod($directory, 0775); // Necessary for non-webserver users.
webmaster@1: }
webmaster@1: else {
webmaster@1: if ($form_item) {
webmaster@1: form_set_error($form_item, t('The directory %directory does not exist.', array('%directory' => $directory)));
webmaster@1: }
webmaster@1: return FALSE;
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: // Check to see if the directory is writable.
webmaster@1: if (!is_writable($directory)) {
webmaster@1: if (($mode & FILE_MODIFY_PERMISSIONS) && @chmod($directory, 0775)) {
webmaster@1: drupal_set_message(t('The permissions of directory %directory have been changed to make it writable.', array('%directory' => $directory)));
webmaster@1: }
webmaster@1: else {
webmaster@1: form_set_error($form_item, t('The directory %directory is not writable', array('%directory' => $directory)));
webmaster@1: watchdog('file system', 'The directory %directory is not writable, because it does not have the correct permissions set.', array('%directory' => $directory), WATCHDOG_ERROR);
webmaster@1: return FALSE;
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: if ((file_directory_path() == $directory || file_directory_temp() == $directory) && !is_file("$directory/.htaccess")) {
webmaster@1: $htaccess_lines = "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\nOptions None\nOptions +FollowSymLinks";
webmaster@1: if (($fp = fopen("$directory/.htaccess", 'w')) && fputs($fp, $htaccess_lines)) {
webmaster@1: fclose($fp);
webmaster@1: chmod($directory .'/.htaccess', 0664);
webmaster@1: }
webmaster@1: else {
webmaster@1: $variables = array('%directory' => $directory, '!htaccess' => '
'. nl2br(check_plain($htaccess_lines)));
webmaster@1: 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: !htaccess
", $variables));
webmaster@1: watchdog('security', "Security warning: Couldn't write .htaccess file. Please create a .htaccess file in your %directory directory which contains the following lines: !htaccess
", $variables, WATCHDOG_ERROR);
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: return TRUE;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Checks path to see if it is a directory, or a dir/file.
webmaster@1: *
webmaster@1: * @param $path A string containing a file path. This will be set to the
webmaster@1: * directory's path.
webmaster@1: * @return If the directory is not in a Drupal writable directory, FALSE is
webmaster@1: * returned. Otherwise, the base name of the path is returned.
webmaster@1: */
webmaster@1: function file_check_path(&$path) {
webmaster@1: // Check if path is a directory.
webmaster@1: if (file_check_directory($path)) {
webmaster@1: return '';
webmaster@1: }
webmaster@1:
webmaster@1: // Check if path is a possible dir/file.
webmaster@1: $filename = basename($path);
webmaster@1: $path = dirname($path);
webmaster@1: if (file_check_directory($path)) {
webmaster@1: return $filename;
webmaster@1: }
webmaster@1:
webmaster@1: return FALSE;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Check if a file is really located inside $directory. Should be used to make
webmaster@1: * sure a file specified is really located within the directory to prevent
webmaster@1: * exploits.
webmaster@1: *
webmaster@1: * @code
webmaster@1: * // Returns FALSE:
webmaster@1: * file_check_location('/www/example.com/files/../../../etc/passwd', '/www/example.com/files');
webmaster@1: * @endcode
webmaster@1: *
webmaster@1: * @param $source A string set to the file to check.
webmaster@1: * @param $directory A string where the file should be located.
webmaster@1: * @return 0 for invalid path or the real path of the source.
webmaster@1: */
webmaster@1: function file_check_location($source, $directory = '') {
webmaster@1: $check = realpath($source);
webmaster@1: if ($check) {
webmaster@1: $source = $check;
webmaster@1: }
webmaster@1: else {
webmaster@1: // This file does not yet exist
webmaster@1: $source = realpath(dirname($source)) .'/'. basename($source);
webmaster@1: }
webmaster@1: $directory = realpath($directory);
webmaster@1: if ($directory && strpos($source, $directory) !== 0) {
webmaster@1: return 0;
webmaster@1: }
webmaster@1: return $source;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Copies a file to a new location. This is a powerful function that in many ways
webmaster@1: * performs like an advanced version of copy().
webmaster@1: * - Checks if $source and $dest are valid and readable/writable.
webmaster@1: * - Performs a file copy if $source is not equal to $dest.
webmaster@1: * - If file already exists in $dest either the call will error out, replace the
webmaster@1: * file or rename the file based on the $replace parameter.
webmaster@1: *
webmaster@1: * @param $source A string specifying the file location of the original file.
webmaster@1: * This parameter will contain the resulting destination filename in case of
webmaster@1: * success.
webmaster@1: * @param $dest A string containing the directory $source should be copied to.
webmaster@1: * If this value is omitted, Drupal's 'files' directory will be used.
webmaster@1: * @param $replace Replace behavior when the destination file already exists.
webmaster@1: * - FILE_EXISTS_REPLACE - Replace the existing file
webmaster@1: * - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique
webmaster@1: * - FILE_EXISTS_ERROR - Do nothing and return FALSE.
webmaster@1: * @return True for success, FALSE for failure.
webmaster@1: */
webmaster@1: function file_copy(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME) {
webmaster@1: $dest = file_create_path($dest);
webmaster@1:
webmaster@1: $directory = $dest;
webmaster@1: $basename = file_check_path($directory);
webmaster@1:
webmaster@1: // Make sure we at least have a valid directory.
webmaster@1: if ($basename === FALSE) {
webmaster@1: $source = is_object($source) ? $source->filepath : $source;
webmaster@1: drupal_set_message(t('The selected file %file could not be uploaded, because the destination %directory is not properly configured.', array('%file' => $source, '%directory' => $dest)), 'error');
webmaster@1: watchdog('file system', 'The selected file %file could not be uploaded, because the destination %directory could not be found, or because its permissions do not allow the file to be written.', array('%file' => $source, '%directory' => $dest), WATCHDOG_ERROR);
webmaster@1: return 0;
webmaster@1: }
webmaster@1:
webmaster@1: // Process a file upload object.
webmaster@1: if (is_object($source)) {
webmaster@1: $file = $source;
webmaster@1: $source = $file->filepath;
webmaster@1: if (!$basename) {
webmaster@1: $basename = $file->filename;
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: $source = realpath($source);
webmaster@1: if (!file_exists($source)) {
webmaster@1: drupal_set_message(t('The selected file %file could not be copied, because no file by that name exists. Please check that you supplied the correct filename.', array('%file' => $source)), 'error');
webmaster@1: return 0;
webmaster@1: }
webmaster@1:
webmaster@1: // If the destination file is not specified then use the filename of the source file.
webmaster@1: $basename = $basename ? $basename : basename($source);
webmaster@1: $dest = $directory .'/'. $basename;
webmaster@1:
webmaster@1: // Make sure source and destination filenames are not the same, makes no sense
webmaster@1: // to copy it if they are. In fact copying the file will most likely result in
webmaster@1: // a 0 byte file. Which is bad. Real bad.
webmaster@1: if ($source != realpath($dest)) {
webmaster@1: if (!$dest = file_destination($dest, $replace)) {
webmaster@1: drupal_set_message(t('The selected file %file could not be copied, because a file by that name already exists in the destination.', array('%file' => $source)), 'error');
webmaster@1: return FALSE;
webmaster@1: }
webmaster@1:
webmaster@1: if (!@copy($source, $dest)) {
webmaster@1: drupal_set_message(t('The selected file %file could not be copied.', array('%file' => $source)), 'error');
webmaster@1: return 0;
webmaster@1: }
webmaster@1:
webmaster@1: // Give everyone read access so that FTP'd users or
webmaster@1: // non-webserver users can see/read these files,
webmaster@1: // and give group write permissions so group members
webmaster@1: // can alter files uploaded by the webserver.
webmaster@1: @chmod($dest, 0664);
webmaster@1: }
webmaster@1:
webmaster@1: if (isset($file) && is_object($file)) {
webmaster@1: $file->filename = $basename;
webmaster@1: $file->filepath = $dest;
webmaster@1: $source = $file;
webmaster@1: }
webmaster@1: else {
webmaster@1: $source = $dest;
webmaster@1: }
webmaster@1:
webmaster@1: return 1; // Everything went ok.
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Determines the destination path for a file depending on how replacement of
webmaster@1: * existing files should be handled.
webmaster@1: *
webmaster@1: * @param $destination A string specifying the desired path.
webmaster@1: * @param $replace Replace behavior when the destination file already exists.
webmaster@1: * - FILE_EXISTS_REPLACE - Replace the existing file
webmaster@1: * - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is
webmaster@1: * unique
webmaster@1: * - FILE_EXISTS_ERROR - Do nothing and return FALSE.
webmaster@1: * @return The destination file path or FALSE if the file already exists and
webmaster@1: * FILE_EXISTS_ERROR was specified.
webmaster@1: */
webmaster@1: function file_destination($destination, $replace) {
webmaster@1: if (file_exists($destination)) {
webmaster@1: switch ($replace) {
webmaster@1: case FILE_EXISTS_RENAME:
webmaster@1: $basename = basename($destination);
webmaster@1: $directory = dirname($destination);
webmaster@1: $destination = file_create_filename($basename, $directory);
webmaster@1: break;
webmaster@1:
webmaster@1: case FILE_EXISTS_ERROR:
webmaster@1: drupal_set_message(t('The selected file %file could not be copied, because a file by that name already exists in the destination.', array('%file' => $destination)), 'error');
webmaster@1: return FALSE;
webmaster@1: }
webmaster@1: }
webmaster@1: return $destination;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Moves a file to a new location.
webmaster@1: * - Checks if $source and $dest are valid and readable/writable.
webmaster@1: * - Performs a file move if $source is not equal to $dest.
webmaster@1: * - If file already exists in $dest either the call will error out, replace the
webmaster@1: * file or rename the file based on the $replace parameter.
webmaster@1: *
webmaster@1: * @param $source A string specifying the file location of the original file.
webmaster@1: * This parameter will contain the resulting destination filename in case of
webmaster@1: * success.
webmaster@1: * @param $dest A string containing the directory $source should be copied to.
webmaster@1: * If this value is omitted, Drupal's 'files' directory will be used.
webmaster@1: * @param $replace Replace behavior when the destination file already exists.
webmaster@1: * - FILE_EXISTS_REPLACE - Replace the existing file
webmaster@1: * - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique
webmaster@1: * - FILE_EXISTS_ERROR - Do nothing and return FALSE.
webmaster@1: * @return True for success, FALSE for failure.
webmaster@1: */
webmaster@1: function file_move(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME) {
webmaster@1: $path_original = is_object($source) ? $source->filepath : $source;
webmaster@1:
webmaster@1: if (file_copy($source, $dest, $replace)) {
webmaster@1: $path_current = is_object($source) ? $source->filepath : $source;
webmaster@1:
webmaster@1: if ($path_original == $path_current || file_delete($path_original)) {
webmaster@1: return 1;
webmaster@1: }
webmaster@1: drupal_set_message(t('The removal of the original file %file has failed.', array('%file' => $path_original)), 'error');
webmaster@1: }
webmaster@1: return 0;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Munge the filename as needed for security purposes. For instance the file
webmaster@1: * name "exploit.php.pps" would become "exploit.php_.pps".
webmaster@1: *
webmaster@1: * @param $filename The name of a file to modify.
webmaster@1: * @param $extensions A space separated list of extensions that should not
webmaster@1: * be altered.
webmaster@1: * @param $alerts Whether alerts (watchdog, drupal_set_message()) should be
webmaster@1: * displayed.
webmaster@1: * @return $filename The potentially modified $filename.
webmaster@1: */
webmaster@1: function file_munge_filename($filename, $extensions, $alerts = TRUE) {
webmaster@1: $original = $filename;
webmaster@1:
webmaster@1: // Allow potentially insecure uploads for very savvy users and admin
webmaster@1: if (!variable_get('allow_insecure_uploads', 0)) {
webmaster@1: $whitelist = array_unique(explode(' ', trim($extensions)));
webmaster@1:
webmaster@1: // Split the filename up by periods. The first part becomes the basename
webmaster@1: // the last part the final extension.
webmaster@1: $filename_parts = explode('.', $filename);
webmaster@1: $new_filename = array_shift($filename_parts); // Remove file basename.
webmaster@1: $final_extension = array_pop($filename_parts); // Remove final extension.
webmaster@1:
webmaster@1: // Loop through the middle parts of the name and add an underscore to the
webmaster@1: // end of each section that could be a file extension but isn't in the list
webmaster@1: // of allowed extensions.
webmaster@1: foreach ($filename_parts as $filename_part) {
webmaster@1: $new_filename .= '.'. $filename_part;
webmaster@1: if (!in_array($filename_part, $whitelist) && preg_match("/^[a-zA-Z]{2,5}\d?$/", $filename_part)) {
webmaster@1: $new_filename .= '_';
webmaster@1: }
webmaster@1: }
webmaster@1: $filename = $new_filename .'.'. $final_extension;
webmaster@1:
webmaster@1: if ($alerts && $original != $filename) {
webmaster@1: drupal_set_message(t('For security reasons, your upload has been renamed to %filename.', array('%filename' => $filename)));
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: return $filename;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Undo the effect of upload_munge_filename().
webmaster@1: *
webmaster@1: * @param $filename string filename
webmaster@1: * @return string
webmaster@1: */
webmaster@1: function file_unmunge_filename($filename) {
webmaster@1: return str_replace('_.', '.', $filename);
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Create a full file path from a directory and filename. If a file with the
webmaster@1: * specified name already exists, an alternative will be used.
webmaster@1: *
webmaster@1: * @param $basename string filename
webmaster@1: * @param $directory string directory
webmaster@1: * @return
webmaster@1: */
webmaster@1: function file_create_filename($basename, $directory) {
webmaster@1: $dest = $directory .'/'. $basename;
webmaster@1:
webmaster@1: if (file_exists($dest)) {
webmaster@1: // Destination file already exists, generate an alternative.
webmaster@1: if ($pos = strrpos($basename, '.')) {
webmaster@1: $name = substr($basename, 0, $pos);
webmaster@1: $ext = substr($basename, $pos);
webmaster@1: }
webmaster@1: else {
webmaster@1: $name = $basename;
webmaster@1: }
webmaster@1:
webmaster@1: $counter = 0;
webmaster@1: do {
webmaster@1: $dest = $directory .'/'. $name .'_'. $counter++ . $ext;
webmaster@1: } while (file_exists($dest));
webmaster@1: }
webmaster@1:
webmaster@1: return $dest;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Delete a file.
webmaster@1: *
webmaster@1: * @param $path A string containing a file path.
webmaster@1: * @return TRUE for success, FALSE for failure.
webmaster@1: */
webmaster@1: function file_delete($path) {
webmaster@1: if (is_file($path)) {
webmaster@1: return unlink($path);
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Determine total disk space used by a single user or the whole filesystem.
webmaster@1: *
webmaster@1: * @param $uid
webmaster@1: * An optional user id. A NULL value returns the total space used
webmaster@1: * by all files.
webmaster@1: */
webmaster@1: function file_space_used($uid = NULL) {
webmaster@1: if (isset($uid)) {
webmaster@1: return (int) db_result(db_query('SELECT SUM(filesize) FROM {files} WHERE uid = %d', $uid));
webmaster@1: }
webmaster@1: return (int) db_result(db_query('SELECT SUM(filesize) FROM {files}'));
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Saves a file upload to a new location. The source file is validated as a
webmaster@1: * proper upload and handled as such.
webmaster@1: *
webmaster@1: * The file will be added to the files table as a temporary file. Temporary files
webmaster@1: * are periodically cleaned. To make the file permanent file call
webmaster@1: * file_set_status() to change its status.
webmaster@1: *
webmaster@1: * @param $source
webmaster@1: * A string specifying the name of the upload field to save.
webmaster@1: * @param $validators
webmaster@1: * An optional, associative array of callback functions used to validate the
webmaster@1: * file. The keys are function names and the values arrays of callback
webmaster@1: * parameters which will be passed in after the user and file objects. The
webmaster@1: * functions should return an array of error messages, an empty array
webmaster@1: * indicates that the file passed validation. The functions will be called in
webmaster@1: * the order specified.
webmaster@1: * @param $dest
webmaster@1: * A string containing the directory $source should be copied to. If this is
webmaster@1: * not provided or is not writable, the temporary directory will be used.
webmaster@1: * @param $replace
webmaster@1: * A boolean indicating whether an existing file of the same name in the
webmaster@1: * destination directory should overwritten. A false value will generate a
webmaster@1: * new, unique filename in the destination directory.
webmaster@1: * @return
webmaster@1: * An object containing the file information, or 0 in the event of an error.
webmaster@1: */
webmaster@1: function file_save_upload($source, $validators = array(), $dest = FALSE, $replace = FILE_EXISTS_RENAME) {
webmaster@1: global $user;
webmaster@1: static $upload_cache;
webmaster@1:
webmaster@1: // Add in our check of the the file name length.
webmaster@1: $validators['file_validate_name_length'] = array();
webmaster@1:
webmaster@1: // Return cached objects without processing since the file will have
webmaster@1: // already been processed and the paths in _FILES will be invalid.
webmaster@1: if (isset($upload_cache[$source])) {
webmaster@1: return $upload_cache[$source];
webmaster@1: }
webmaster@1:
webmaster@1: // If a file was uploaded, process it.
webmaster@1: if (isset($_FILES['files']) && $_FILES['files']['name'][$source] && is_uploaded_file($_FILES['files']['tmp_name'][$source])) {
webmaster@1: // Check for file upload errors and return FALSE if a
webmaster@1: // lower level system error occurred.
webmaster@1: switch ($_FILES['files']['error'][$source]) {
webmaster@1: // @see http://php.net/manual/en/features.file-upload.errors.php
webmaster@1: case UPLOAD_ERR_OK:
webmaster@1: break;
webmaster@1:
webmaster@1: case UPLOAD_ERR_INI_SIZE:
webmaster@1: case UPLOAD_ERR_FORM_SIZE:
webmaster@1: drupal_set_message(t('The file %file could not be saved, because it exceeds %maxsize, the maximum allowed size for uploads.', array('%file' => $source, '%maxsize' => format_size(file_upload_max_size()))), 'error');
webmaster@1: return 0;
webmaster@1:
webmaster@1: case UPLOAD_ERR_PARTIAL:
webmaster@1: case UPLOAD_ERR_NO_FILE:
webmaster@1: drupal_set_message(t('The file %file could not be saved, because the upload did not complete.', array('%file' => $source)), 'error');
webmaster@1: return 0;
webmaster@1:
webmaster@1: // Unknown error
webmaster@1: default:
webmaster@1: drupal_set_message(t('The file %file could not be saved. An unknown error has occurred.', array('%file' => $source)), 'error');
webmaster@1: return 0;
webmaster@1: }
webmaster@1:
webmaster@1: // Build the list of non-munged extensions.
webmaster@1: // @todo: this should not be here. we need to figure out the right place.
webmaster@1: $extensions = '';
webmaster@1: foreach ($user->roles as $rid => $name) {
webmaster@1: $extensions .= ' '. variable_get("upload_extensions_$rid",
webmaster@1: variable_get('upload_extensions_default', 'jpg jpeg gif png txt html doc xls pdf ppt pps odt ods odp'));
webmaster@1: }
webmaster@1:
webmaster@1: // Begin building file object.
webmaster@1: $file = new stdClass();
webmaster@1: $file->filename = file_munge_filename(trim(basename($_FILES['files']['name'][$source]), '.'), $extensions);
webmaster@1: $file->filepath = $_FILES['files']['tmp_name'][$source];
webmaster@9: $file->filemime = file_get_mimetype($file->filename);
webmaster@1:
webmaster@1: // Rename potentially executable files, to help prevent exploits.
webmaster@1: if (preg_match('/\.(php|pl|py|cgi|asp|js)$/i', $file->filename) && (substr($file->filename, -4) != '.txt')) {
webmaster@1: $file->filemime = 'text/plain';
webmaster@1: $file->filepath .= '.txt';
webmaster@1: $file->filename .= '.txt';
webmaster@1: }
webmaster@1:
webmaster@1: // If the destination is not provided, or is not writable, then use the
webmaster@1: // temporary directory.
webmaster@1: if (empty($dest) || file_check_path($dest) === FALSE) {
webmaster@1: $dest = file_directory_temp();
webmaster@1: }
webmaster@1:
webmaster@1: $file->source = $source;
webmaster@1: $file->destination = file_destination(file_create_path($dest .'/'. $file->filename), $replace);
webmaster@1: $file->filesize = $_FILES['files']['size'][$source];
webmaster@1:
webmaster@1: // Call the validation functions.
webmaster@1: $errors = array();
webmaster@1: foreach ($validators as $function => $args) {
webmaster@1: array_unshift($args, $file);
webmaster@1: $errors = array_merge($errors, call_user_func_array($function, $args));
webmaster@1: }
webmaster@1:
webmaster@1: // Check for validation errors.
webmaster@1: if (!empty($errors)) {
webmaster@1: $message = t('The selected file %name could not be uploaded.', array('%name' => $file->filename));
webmaster@1: if (count($errors) > 1) {
webmaster@1: $message .= '
- '. implode('
- ', $errors) .'
';
webmaster@1: }
webmaster@1: else {
webmaster@1: $message .= ' '. array_pop($errors);
webmaster@1: }
webmaster@1: form_set_error($source, $message);
webmaster@1: return 0;
webmaster@1: }
webmaster@1:
webmaster@1: // Move uploaded files from PHP's upload_tmp_dir to Drupal's temporary directory.
webmaster@1: // This overcomes open_basedir restrictions for future file operations.
webmaster@1: $file->filepath = $file->destination;
webmaster@1: if (!move_uploaded_file($_FILES['files']['tmp_name'][$source], $file->filepath)) {
webmaster@1: form_set_error($source, t('File upload error. Could not move uploaded file.'));
webmaster@1: watchdog('file', 'Upload error. Could not move uploaded file %file to destination %destination.', array('%file' => $file->filename, '%destination' => $file->filepath));
webmaster@1: return 0;
webmaster@1: }
webmaster@1:
webmaster@1: // If we made it this far it's safe to record this file in the database.
webmaster@1: $file->uid = $user->uid;
webmaster@1: $file->status = FILE_STATUS_TEMPORARY;
webmaster@1: $file->timestamp = time();
webmaster@1: drupal_write_record('files', $file);
webmaster@1:
webmaster@1: // Add file to the cache.
webmaster@1: $upload_cache[$source] = $file;
webmaster@1: return $file;
webmaster@1: }
webmaster@1: return 0;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Check for files with names longer than we can store in the database.
webmaster@1: *
webmaster@1: * @param $file
webmaster@1: * A Drupal file object.
webmaster@1: * @return
webmaster@1: * An array. If the file name is too long, it will contain an error message.
webmaster@1: */
webmaster@1: function file_validate_name_length($file) {
webmaster@1: $errors = array();
webmaster@1:
webmaster@1: if (strlen($file->filename) > 255) {
webmaster@1: $errors[] = t('Its name exceeds the 255 characters limit. Please rename the file and try again.');
webmaster@1: }
webmaster@1: return $errors;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Check that the filename ends with an allowed extension. This check is not
webmaster@1: * enforced for the user #1.
webmaster@1: *
webmaster@1: * @param $file
webmaster@1: * A Drupal file object.
webmaster@1: * @param $extensions
webmaster@1: * A string with a space separated
webmaster@1: * @return
webmaster@1: * An array. If the file extension is not allowed, it will contain an error message.
webmaster@1: */
webmaster@1: function file_validate_extensions($file, $extensions) {
webmaster@1: global $user;
webmaster@1:
webmaster@1: $errors = array();
webmaster@1:
webmaster@1: // Bypass validation for uid = 1.
webmaster@1: if ($user->uid != 1) {
webmaster@1: $regex = '/\.('. ereg_replace(' +', '|', preg_quote($extensions)) .')$/i';
webmaster@1: if (!preg_match($regex, $file->filename)) {
webmaster@1: $errors[] = t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => $extensions));
webmaster@1: }
webmaster@1: }
webmaster@1: return $errors;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Check that the file's size is below certain limits. This check is not
webmaster@1: * enforced for the user #1.
webmaster@1: *
webmaster@1: * @param $file
webmaster@1: * A Drupal file object.
webmaster@1: * @param $file_limit
webmaster@1: * An integer specifying the maximum file size in bytes. Zero indicates that
webmaster@1: * no limit should be enforced.
webmaster@1: * @param $$user_limit
webmaster@1: * An integer specifying the maximum number of bytes the user is allowed. Zero
webmaster@1: * indicates that no limit should be enforced.
webmaster@1: * @return
webmaster@1: * An array. If the file size exceeds limits, it will contain an error message.
webmaster@1: */
webmaster@1: function file_validate_size($file, $file_limit = 0, $user_limit = 0) {
webmaster@1: global $user;
webmaster@1:
webmaster@1: $errors = array();
webmaster@1:
webmaster@1: // Bypass validation for uid = 1.
webmaster@1: if ($user->uid != 1) {
webmaster@1: if ($file_limit && $file->filesize > $file_limit) {
webmaster@1: $errors[] = t('The file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => format_size($file->filesize), '%maxsize' => format_size($file_limit)));
webmaster@1: }
webmaster@1:
webmaster@1: $total_size = file_space_used($user->uid) + $file->filesize;
webmaster@1: if ($user_limit && $total_size > $user_limit) {
webmaster@1: $errors[] = t('The file is %filesize which would exceed your disk quota of %quota.', array('%filesize' => format_size($file->filesize), '%quota' => format_size($user_limit)));
webmaster@1: }
webmaster@1: }
webmaster@1: return $errors;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Check that the file is recognized by image_get_info() as an image.
webmaster@1: *
webmaster@1: * @param $file
webmaster@1: * A Drupal file object.
webmaster@1: * @return
webmaster@1: * An array. If the file is not an image, it will contain an error message.
webmaster@1: */
webmaster@1: function file_validate_is_image(&$file) {
webmaster@1: $errors = array();
webmaster@1:
webmaster@1: $info = image_get_info($file->filepath);
webmaster@1: if (!$info || empty($info['extension'])) {
webmaster@1: $errors[] = t('Only JPEG, PNG and GIF images are allowed.');
webmaster@1: }
webmaster@1:
webmaster@1: return $errors;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * If the file is an image verify that its dimensions are within the specified
webmaster@1: * maximum and minimum dimensions. Non-image files will be ignored.
webmaster@1: *
webmaster@1: * @param $file
webmaster@1: * A Drupal file object. This function may resize the file affecting its size.
webmaster@1: * @param $maximum_dimensions
webmaster@1: * An optional string in the form WIDTHxHEIGHT e.g. '640x480' or '85x85'. If
webmaster@1: * an image toolkit is installed the image will be resized down to these
webmaster@1: * dimensions. A value of 0 indicates no restriction on size, so resizing
webmaster@1: * will be attempted.
webmaster@1: * @param $minimum_dimensions
webmaster@1: * An optional string in the form WIDTHxHEIGHT. This will check that the image
webmaster@1: * meets a minimum size. A value of 0 indicates no restriction.
webmaster@1: * @return
webmaster@1: * An array. If the file is an image and did not meet the requirements, it
webmaster@1: * will contain an error message.
webmaster@1: */
webmaster@1: function file_validate_image_resolution(&$file, $maximum_dimensions = 0, $minimum_dimensions = 0) {
webmaster@1: $errors = array();
webmaster@1:
webmaster@1: // Check first that the file is an image.
webmaster@1: if ($info = image_get_info($file->filepath)) {
webmaster@1: if ($maximum_dimensions) {
webmaster@1: // Check that it is smaller than the given dimensions.
webmaster@1: list($width, $height) = explode('x', $maximum_dimensions);
webmaster@1: if ($info['width'] > $width || $info['height'] > $height) {
webmaster@1: // Try to resize the image to fit the dimensions.
webmaster@1: if (image_get_toolkit() && image_scale($file->filepath, $file->filepath, $width, $height)) {
webmaster@1: drupal_set_message(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $maximum_dimensions)));
webmaster@1:
webmaster@1: // Clear the cached filesize and refresh the image information.
webmaster@1: clearstatcache();
webmaster@1: $info = image_get_info($file->filepath);
webmaster@1: $file->filesize = $info['file_size'];
webmaster@1: }
webmaster@1: else {
webmaster@1: $errors[] = t('The image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => $maximum_dimensions));
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: if ($minimum_dimensions) {
webmaster@1: // Check that it is larger than the given dimensions.
webmaster@1: list($width, $height) = explode('x', $minimum_dimensions);
webmaster@1: if ($info['width'] < $width || $info['height'] < $height) {
webmaster@1: $errors[] = t('The image is too small; the minimum dimensions are %dimensions pixels.', array('%dimensions' => $minimum_dimensions));
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: return $errors;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Save a string to the specified destination.
webmaster@1: *
webmaster@1: * @param $data A string containing the contents of the file.
webmaster@1: * @param $dest A string containing the destination location.
webmaster@1: * @param $replace Replace behavior when the destination file already exists.
webmaster@1: * - FILE_EXISTS_REPLACE - Replace the existing file
webmaster@1: * - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique
webmaster@1: * - FILE_EXISTS_ERROR - Do nothing and return FALSE.
webmaster@1: *
webmaster@1: * @return A string containing the resulting filename or 0 on error
webmaster@1: */
webmaster@1: function file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME) {
webmaster@1: $temp = file_directory_temp();
webmaster@1: // On Windows, tempnam() requires an absolute path, so we use realpath().
webmaster@1: $file = tempnam(realpath($temp), 'file');
webmaster@1: if (!$fp = fopen($file, 'wb')) {
webmaster@1: drupal_set_message(t('The file could not be created.'), 'error');
webmaster@1: return 0;
webmaster@1: }
webmaster@1: fwrite($fp, $data);
webmaster@1: fclose($fp);
webmaster@1:
webmaster@1: if (!file_move($file, $dest, $replace)) {
webmaster@1: return 0;
webmaster@1: }
webmaster@1:
webmaster@1: return $file;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Set the status of a file.
webmaster@1: *
webmaster@1: * @param file A Drupal file object
webmaster@1: * @param status A status value to set the file to.
webmaster@1: * @return FALSE on failure, TRUE on success and $file->status will contain the
webmaster@1: * status.
webmaster@1: */
webmaster@1: function file_set_status(&$file, $status) {
webmaster@1: if (db_query('UPDATE {files} SET status = %d WHERE fid = %d', $status, $file->fid)) {
webmaster@1: $file->status = $status;
webmaster@1: return TRUE;
webmaster@1: }
webmaster@1: return FALSE;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Transfer file using http to client. Pipes a file through Drupal to the
webmaster@1: * client.
webmaster@1: *
webmaster@1: * @param $source File to transfer.
webmaster@1: * @param $headers An array of http headers to send along with file.
webmaster@1: */
webmaster@1: function file_transfer($source, $headers) {
webmaster@13: if (ob_get_level()) {
webmaster@13: ob_end_clean();
webmaster@13: }
webmaster@1:
webmaster@1: foreach ($headers as $header) {
webmaster@1: // To prevent HTTP header injection, we delete new lines that are
webmaster@1: // not followed by a space or a tab.
webmaster@1: // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
webmaster@1: $header = preg_replace('/\r?\n(?!\t| )/', '', $header);
webmaster@1: drupal_set_header($header);
webmaster@1: }
webmaster@1:
webmaster@1: $source = file_create_path($source);
webmaster@1:
webmaster@1: // Transfer file in 1024 byte chunks to save memory usage.
webmaster@1: if ($fd = fopen($source, 'rb')) {
webmaster@1: while (!feof($fd)) {
webmaster@1: print fread($fd, 1024);
webmaster@1: }
webmaster@1: fclose($fd);
webmaster@1: }
webmaster@1: else {
webmaster@1: drupal_not_found();
webmaster@1: }
webmaster@1: exit();
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Call modules that implement hook_file_download() to find out if a file is
webmaster@1: * accessible and what headers it should be transferred with. If a module
webmaster@1: * returns -1 drupal_access_denied() will be returned. If one or more modules
webmaster@1: * returned headers the download will start with the returned headers. If no
webmaster@1: * modules respond drupal_not_found() will be returned.
webmaster@1: */
webmaster@1: function file_download() {
webmaster@1: // Merge remainder of arguments from GET['q'], into relative file path.
webmaster@1: $args = func_get_args();
webmaster@1: $filepath = implode('/', $args);
webmaster@1:
webmaster@1: // Maintain compatibility with old ?file=paths saved in node bodies.
webmaster@1: if (isset($_GET['file'])) {
webmaster@1: $filepath = $_GET['file'];
webmaster@1: }
webmaster@1:
webmaster@1: if (file_exists(file_create_path($filepath))) {
webmaster@1: $headers = module_invoke_all('file_download', $filepath);
webmaster@1: if (in_array(-1, $headers)) {
webmaster@1: return drupal_access_denied();
webmaster@1: }
webmaster@1: if (count($headers)) {
webmaster@1: file_transfer($filepath, $headers);
webmaster@1: }
webmaster@1: }
webmaster@1: return drupal_not_found();
webmaster@1: }
webmaster@1:
webmaster@1:
webmaster@1: /**
webmaster@1: * Finds all files that match a given mask in a given directory.
webmaster@1: * Directories and files beginning with a period are excluded; this
webmaster@1: * prevents hidden files and directories (such as SVN working directories)
webmaster@1: * from being scanned.
webmaster@1: *
webmaster@1: * @param $dir
webmaster@1: * The base directory for the scan, without trailing slash.
webmaster@1: * @param $mask
webmaster@1: * The regular expression of the files to find.
webmaster@1: * @param $nomask
webmaster@1: * An array of files/directories to ignore.
webmaster@1: * @param $callback
webmaster@1: * The callback function to call for each match.
webmaster@1: * @param $recurse
webmaster@1: * When TRUE, the directory scan will recurse the entire tree
webmaster@1: * starting at the provided directory.
webmaster@1: * @param $key
webmaster@1: * The key to be used for the returned array of files. Possible
webmaster@1: * values are "filename", for the path starting with $dir,
webmaster@1: * "basename", for the basename of the file, and "name" for the name
webmaster@1: * of the file without an extension.
webmaster@1: * @param $min_depth
webmaster@1: * Minimum depth of directories to return files from.
webmaster@1: * @param $depth
webmaster@1: * Current depth of recursion. This parameter is only used internally and should not be passed.
webmaster@1: *
webmaster@1: * @return
webmaster@1: * An associative array (keyed on the provided key) of objects with
webmaster@1: * "path", "basename", and "name" members corresponding to the
webmaster@1: * matching files.
webmaster@1: */
webmaster@1: function file_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse = TRUE, $key = 'filename', $min_depth = 0, $depth = 0) {
webmaster@1: $key = (in_array($key, array('filename', 'basename', 'name')) ? $key : 'filename');
webmaster@1: $files = array();
webmaster@1:
webmaster@1: if (is_dir($dir) && $handle = opendir($dir)) {
webmaster@11: while (FALSE !== ($file = readdir($handle))) {
webmaster@1: if (!in_array($file, $nomask) && $file[0] != '.') {
webmaster@1: if (is_dir("$dir/$file") && $recurse) {
webmaster@1: // Give priority to files in this folder by merging them in after any subdirectory files.
webmaster@1: $files = array_merge(file_scan_directory("$dir/$file", $mask, $nomask, $callback, $recurse, $key, $min_depth, $depth + 1), $files);
webmaster@1: }
webmaster@1: elseif ($depth >= $min_depth && ereg($mask, $file)) {
webmaster@1: // Always use this match over anything already set in $files with the same $$key.
webmaster@1: $filename = "$dir/$file";
webmaster@1: $basename = basename($file);
webmaster@1: $name = substr($basename, 0, strrpos($basename, '.'));
webmaster@1: $files[$$key] = new stdClass();
webmaster@1: $files[$$key]->filename = $filename;
webmaster@1: $files[$$key]->basename = $basename;
webmaster@1: $files[$$key]->name = $name;
webmaster@1: if ($callback) {
webmaster@1: $callback($filename);
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: closedir($handle);
webmaster@1: }
webmaster@1:
webmaster@1: return $files;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Determine the default temporary directory.
webmaster@1: *
webmaster@1: * @return A string containing a temp directory.
webmaster@1: */
webmaster@1: function file_directory_temp() {
webmaster@1: $temporary_directory = variable_get('file_directory_temp', NULL);
webmaster@1:
webmaster@1: if (is_null($temporary_directory)) {
webmaster@1: $directories = array();
webmaster@1:
webmaster@1: // Has PHP been set with an upload_tmp_dir?
webmaster@1: if (ini_get('upload_tmp_dir')) {
webmaster@1: $directories[] = ini_get('upload_tmp_dir');
webmaster@1: }
webmaster@1:
webmaster@1: // Operating system specific dirs.
webmaster@1: if (substr(PHP_OS, 0, 3) == 'WIN') {
webmaster@1: $directories[] = 'c:\\windows\\temp';
webmaster@1: $directories[] = 'c:\\winnt\\temp';
webmaster@1: $path_delimiter = '\\';
webmaster@1: }
webmaster@1: else {
webmaster@1: $directories[] = '/tmp';
webmaster@1: $path_delimiter = '/';
webmaster@1: }
webmaster@1:
webmaster@1: foreach ($directories as $directory) {
webmaster@1: if (!$temporary_directory && is_dir($directory)) {
webmaster@1: $temporary_directory = $directory;
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: // if a directory has been found, use it, otherwise default to 'files/tmp' or 'files\\tmp';
webmaster@1: $temporary_directory = $temporary_directory ? $temporary_directory : file_directory_path() . $path_delimiter .'tmp';
webmaster@1: variable_set('file_directory_temp', $temporary_directory);
webmaster@1: }
webmaster@1:
webmaster@1: return $temporary_directory;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Determine the default 'files' directory.
webmaster@1: *
webmaster@1: * @return A string containing the path to Drupal's 'files' directory.
webmaster@1: */
webmaster@1: function file_directory_path() {
webmaster@1: return variable_get('file_directory_path', conf_path() .'/files');
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Determine the maximum file upload size by querying the PHP settings.
webmaster@1: *
webmaster@1: * @return
webmaster@1: * A file size limit in bytes based on the PHP upload_max_filesize and post_max_size
webmaster@1: */
webmaster@1: function file_upload_max_size() {
webmaster@1: static $max_size = -1;
webmaster@1:
webmaster@1: if ($max_size < 0) {
webmaster@1: $upload_max = parse_size(ini_get('upload_max_filesize'));
webmaster@1: $post_max = parse_size(ini_get('post_max_size'));
webmaster@1: $max_size = ($upload_max < $post_max) ? $upload_max : $post_max;
webmaster@1: }
webmaster@1: return $max_size;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@9: * Determine an Internet Media Type, or MIME type from a filename.
webmaster@9: *
webmaster@9: * @param $filename
webmaster@9: * Name of the file, including extension.
webmaster@9: * @param $mapping
webmaster@9: * An optional array of extension to media type mappings in the form
webmaster@9: * 'extension1|extension2|...' => 'type'.
webmaster@9: *
webmaster@9: * @return
webmaster@9: * The internet media type registered for the extension or application/octet-stream for unknown extensions.
webmaster@9: */
webmaster@9: function file_get_mimetype($filename, $mapping = NULL) {
webmaster@9: if (!is_array($mapping)) {
webmaster@9: $mapping = variable_get('mime_extension_mapping', array(
webmaster@9: 'ez' => 'application/andrew-inset',
webmaster@9: 'atom' => 'application/atom',
webmaster@9: 'atomcat' => 'application/atomcat+xml',
webmaster@9: 'atomsrv' => 'application/atomserv+xml',
webmaster@9: 'cap|pcap' => 'application/cap',
webmaster@9: 'cu' => 'application/cu-seeme',
webmaster@9: 'tsp' => 'application/dsptype',
webmaster@9: 'spl' => 'application/x-futuresplash',
webmaster@9: 'hta' => 'application/hta',
webmaster@9: 'jar' => 'application/java-archive',
webmaster@9: 'ser' => 'application/java-serialized-object',
webmaster@9: 'class' => 'application/java-vm',
webmaster@9: 'hqx' => 'application/mac-binhex40',
webmaster@9: 'cpt' => 'image/x-corelphotopaint',
webmaster@9: 'nb' => 'application/mathematica',
webmaster@9: 'mdb' => 'application/msaccess',
webmaster@9: 'doc|dot' => 'application/msword',
webmaster@9: 'bin' => 'application/octet-stream',
webmaster@9: 'oda' => 'application/oda',
webmaster@9: 'ogg|ogx' => 'application/ogg',
webmaster@9: 'pdf' => 'application/pdf',
webmaster@9: 'key' => 'application/pgp-keys',
webmaster@9: 'pgp' => 'application/pgp-signature',
webmaster@9: 'prf' => 'application/pics-rules',
webmaster@9: 'ps|ai|eps' => 'application/postscript',
webmaster@9: 'rar' => 'application/rar',
webmaster@9: 'rdf' => 'application/rdf+xml',
webmaster@9: 'rss' => 'application/rss+xml',
webmaster@9: 'rtf' => 'application/rtf',
webmaster@9: 'smi|smil' => 'application/smil',
webmaster@9: 'wpd' => 'application/wordperfect',
webmaster@9: 'wp5' => 'application/wordperfect5.1',
webmaster@9: 'xhtml|xht' => 'application/xhtml+xml',
webmaster@9: 'xml|xsl' => 'application/xml',
webmaster@9: 'zip' => 'application/zip',
webmaster@9: 'cdy' => 'application/vnd.cinderella',
webmaster@9: 'kml' => 'application/vnd.google-earth.kml+xml',
webmaster@9: 'kmz' => 'application/vnd.google-earth.kmz',
webmaster@9: 'xul' => 'application/vnd.mozilla.xul+xml',
webmaster@9: 'xls|xlb|xlt' => 'application/vnd.ms-excel',
webmaster@9: 'cat' => 'application/vnd.ms-pki.seccat',
webmaster@9: 'stl' => 'application/vnd.ms-pki.stl',
webmaster@9: 'ppt|pps' => 'application/vnd.ms-powerpoint',
webmaster@9: 'odc' => 'application/vnd.oasis.opendocument.chart',
webmaster@9: 'odb' => 'application/vnd.oasis.opendocument.database',
webmaster@9: 'odf' => 'application/vnd.oasis.opendocument.formula',
webmaster@9: 'odg' => 'application/vnd.oasis.opendocument.graphics',
webmaster@9: 'otg' => 'application/vnd.oasis.opendocument.graphics-template',
webmaster@9: 'odi' => 'application/vnd.oasis.opendocument.image',
webmaster@9: 'odp' => 'application/vnd.oasis.opendocument.presentation',
webmaster@9: 'otp' => 'application/vnd.oasis.opendocument.presentation-template',
webmaster@9: 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
webmaster@9: 'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template',
webmaster@9: 'odt' => 'application/vnd.oasis.opendocument.text',
webmaster@9: 'odm' => 'application/vnd.oasis.opendocument.text-master',
webmaster@9: 'ott' => 'application/vnd.oasis.opendocument.text-template',
webmaster@9: 'oth' => 'application/vnd.oasis.opendocument.text-web',
webmaster@9: 'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
webmaster@9: 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
webmaster@9: 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
webmaster@9: 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
webmaster@9: 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
webmaster@9: 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
webmaster@9: 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
webmaster@9: 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
webmaster@9: 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
webmaster@9: 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
webmaster@9: 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
webmaster@9: 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
webmaster@9: 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
webmaster@9: 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
webmaster@9: 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
webmaster@9: 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
webmaster@9: 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
webmaster@9: 'cod' => 'application/vnd.rim.cod',
webmaster@9: 'mmf' => 'application/vnd.smaf',
webmaster@9: 'sdc' => 'application/vnd.stardivision.calc',
webmaster@9: 'sds' => 'application/vnd.stardivision.chart',
webmaster@9: 'sda' => 'application/vnd.stardivision.draw',
webmaster@9: 'sdd' => 'application/vnd.stardivision.impress',
webmaster@9: 'sdf' => 'application/vnd.stardivision.math',
webmaster@9: 'sdw' => 'application/vnd.stardivision.writer',
webmaster@9: 'sgl' => 'application/vnd.stardivision.writer-global',
webmaster@9: 'sxc' => 'application/vnd.sun.xml.calc',
webmaster@9: 'stc' => 'application/vnd.sun.xml.calc.template',
webmaster@9: 'sxd' => 'application/vnd.sun.xml.draw',
webmaster@9: 'std' => 'application/vnd.sun.xml.draw.template',
webmaster@9: 'sxi' => 'application/vnd.sun.xml.impress',
webmaster@9: 'sti' => 'application/vnd.sun.xml.impress.template',
webmaster@9: 'sxm' => 'application/vnd.sun.xml.math',
webmaster@9: 'sxw' => 'application/vnd.sun.xml.writer',
webmaster@9: 'sxg' => 'application/vnd.sun.xml.writer.global',
webmaster@9: 'stw' => 'application/vnd.sun.xml.writer.template',
webmaster@9: 'sis' => 'application/vnd.symbian.install',
webmaster@9: 'vsd' => 'application/vnd.visio',
webmaster@9: 'wbxml' => 'application/vnd.wap.wbxml',
webmaster@9: 'wmlc' => 'application/vnd.wap.wmlc',
webmaster@9: 'wmlsc' => 'application/vnd.wap.wmlscriptc',
webmaster@9: 'wk' => 'application/x-123',
webmaster@9: '7z' => 'application/x-7z-compressed',
webmaster@9: 'abw' => 'application/x-abiword',
webmaster@9: 'dmg' => 'application/x-apple-diskimage',
webmaster@9: 'bcpio' => 'application/x-bcpio',
webmaster@9: 'torrent' => 'application/x-bittorrent',
webmaster@9: 'cab' => 'application/x-cab',
webmaster@9: 'cbr' => 'application/x-cbr',
webmaster@9: 'cbz' => 'application/x-cbz',
webmaster@9: 'cdf' => 'application/x-cdf',
webmaster@9: 'vcd' => 'application/x-cdlink',
webmaster@9: 'pgn' => 'application/x-chess-pgn',
webmaster@9: 'cpio' => 'application/x-cpio',
webmaster@9: 'csh' => 'text/x-csh',
webmaster@9: 'deb|udeb' => 'application/x-debian-package',
webmaster@9: 'dcr|dir|dxr' => 'application/x-director',
webmaster@9: 'dms' => 'application/x-dms',
webmaster@9: 'wad' => 'application/x-doom',
webmaster@9: 'dvi' => 'application/x-dvi',
webmaster@9: 'rhtml' => 'application/x-httpd-eruby',
webmaster@9: 'flac' => 'application/x-flac',
webmaster@9: 'pfa|pfb|gsf|pcf|pcf.Z' => 'application/x-font',
webmaster@9: 'mm' => 'application/x-freemind',
webmaster@9: 'gnumeric' => 'application/x-gnumeric',
webmaster@9: 'sgf' => 'application/x-go-sgf',
webmaster@9: 'gcf' => 'application/x-graphing-calculator',
webmaster@9: 'gtar|tgz|taz' => 'application/x-gtar',
webmaster@9: 'hdf' => 'application/x-hdf',
webmaster@9: 'phtml|pht|php' => 'application/x-httpd-php',
webmaster@9: 'phps' => 'application/x-httpd-php-source',
webmaster@9: 'php3' => 'application/x-httpd-php3',
webmaster@9: 'php3p' => 'application/x-httpd-php3-preprocessed',
webmaster@9: 'php4' => 'application/x-httpd-php4',
webmaster@9: 'ica' => 'application/x-ica',
webmaster@9: 'ins|isp' => 'application/x-internet-signup',
webmaster@9: 'iii' => 'application/x-iphone',
webmaster@9: 'iso' => 'application/x-iso9660-image',
webmaster@9: 'jnlp' => 'application/x-java-jnlp-file',
webmaster@9: 'js' => 'application/x-javascript',
webmaster@9: 'jmz' => 'application/x-jmol',
webmaster@9: 'chrt' => 'application/x-kchart',
webmaster@9: 'kil' => 'application/x-killustrator',
webmaster@9: 'skp|skd|skt|skm' => 'application/x-koan',
webmaster@9: 'kpr|kpt' => 'application/x-kpresenter',
webmaster@9: 'ksp' => 'application/x-kspread',
webmaster@9: 'kwd|kwt' => 'application/x-kword',
webmaster@9: 'latex' => 'application/x-latex',
webmaster@9: 'lha' => 'application/x-lha',
webmaster@9: 'lyx' => 'application/x-lyx',
webmaster@9: 'lzh' => 'application/x-lzh',
webmaster@9: 'lzx' => 'application/x-lzx',
webmaster@9: 'frm|maker|frame|fm|fb|book|fbdoc' => 'application/x-maker',
webmaster@9: 'mif' => 'application/x-mif',
webmaster@9: 'wmd' => 'application/x-ms-wmd',
webmaster@9: 'wmz' => 'application/x-ms-wmz',
webmaster@9: 'com|exe|bat|dll' => 'application/x-msdos-program',
webmaster@9: 'msi' => 'application/x-msi',
webmaster@9: 'nc' => 'application/x-netcdf',
webmaster@9: 'pac' => 'application/x-ns-proxy-autoconfig',
webmaster@9: 'nwc' => 'application/x-nwc',
webmaster@9: 'o' => 'application/x-object',
webmaster@9: 'oza' => 'application/x-oz-application',
webmaster@9: 'p7r' => 'application/x-pkcs7-certreqresp',
webmaster@9: 'crl' => 'application/x-pkcs7-crl',
webmaster@9: 'pyc|pyo' => 'application/x-python-code',
webmaster@9: 'qtl' => 'application/x-quicktimeplayer',
webmaster@9: 'rpm' => 'application/x-redhat-package-manager',
webmaster@9: 'sh' => 'text/x-sh',
webmaster@9: 'shar' => 'application/x-shar',
webmaster@9: 'swf|swfl' => 'application/x-shockwave-flash',
webmaster@9: 'sit|sitx' => 'application/x-stuffit',
webmaster@9: 'sv4cpio' => 'application/x-sv4cpio',
webmaster@9: 'sv4crc' => 'application/x-sv4crc',
webmaster@9: 'tar' => 'application/x-tar',
webmaster@9: 'tcl' => 'application/x-tcl',
webmaster@9: 'gf' => 'application/x-tex-gf',
webmaster@9: 'pk' => 'application/x-tex-pk',
webmaster@9: 'texinfo|texi' => 'application/x-texinfo',
webmaster@9: '~|%|bak|old|sik' => 'application/x-trash',
webmaster@9: 't|tr|roff' => 'application/x-troff',
webmaster@9: 'man' => 'application/x-troff-man',
webmaster@9: 'me' => 'application/x-troff-me',
webmaster@9: 'ms' => 'application/x-troff-ms',
webmaster@9: 'ustar' => 'application/x-ustar',
webmaster@9: 'src' => 'application/x-wais-source',
webmaster@9: 'wz' => 'application/x-wingz',
webmaster@9: 'crt' => 'application/x-x509-ca-cert',
webmaster@9: 'xcf' => 'application/x-xcf',
webmaster@9: 'fig' => 'application/x-xfig',
webmaster@9: 'xpi' => 'application/x-xpinstall',
webmaster@9: 'au|snd' => 'audio/basic',
webmaster@9: 'mid|midi|kar' => 'audio/midi',
webmaster@9: 'mpga|mpega|mp2|mp3|m4a' => 'audio/mpeg',
webmaster@9: 'm3u' => 'audio/x-mpegurl',
webmaster@9: 'oga|spx' => 'audio/ogg',
webmaster@9: 'sid' => 'audio/prs.sid',
webmaster@9: 'aif|aiff|aifc' => 'audio/x-aiff',
webmaster@9: 'gsm' => 'audio/x-gsm',
webmaster@9: 'wma' => 'audio/x-ms-wma',
webmaster@9: 'wax' => 'audio/x-ms-wax',
webmaster@9: 'ra|rm|ram' => 'audio/x-pn-realaudio',
webmaster@9: 'ra' => 'audio/x-realaudio',
webmaster@9: 'pls' => 'audio/x-scpls',
webmaster@9: 'sd2' => 'audio/x-sd2',
webmaster@9: 'wav' => 'audio/x-wav',
webmaster@9: 'alc' => 'chemical/x-alchemy',
webmaster@9: 'cac|cache' => 'chemical/x-cache',
webmaster@9: 'csf' => 'chemical/x-cache-csf',
webmaster@9: 'cbin|cascii|ctab' => 'chemical/x-cactvs-binary',
webmaster@9: 'cdx' => 'chemical/x-cdx',
webmaster@9: 'cer' => 'chemical/x-cerius',
webmaster@9: 'c3d' => 'chemical/x-chem3d',
webmaster@9: 'chm' => 'chemical/x-chemdraw',
webmaster@9: 'cif' => 'chemical/x-cif',
webmaster@9: 'cmdf' => 'chemical/x-cmdf',
webmaster@9: 'cml' => 'chemical/x-cml',
webmaster@9: 'cpa' => 'chemical/x-compass',
webmaster@9: 'bsd' => 'chemical/x-crossfire',
webmaster@9: 'csml|csm' => 'chemical/x-csml',
webmaster@9: 'ctx' => 'chemical/x-ctx',
webmaster@9: 'cxf|cef' => 'chemical/x-cxf',
webmaster@9: 'emb|embl' => 'chemical/x-embl-dl-nucleotide',
webmaster@9: 'spc' => 'chemical/x-galactic-spc',
webmaster@9: 'inp|gam|gamin' => 'chemical/x-gamess-input',
webmaster@9: 'fch|fchk' => 'chemical/x-gaussian-checkpoint',
webmaster@9: 'cub' => 'chemical/x-gaussian-cube',
webmaster@9: 'gau|gjc|gjf' => 'chemical/x-gaussian-input',
webmaster@9: 'gal' => 'chemical/x-gaussian-log',
webmaster@9: 'gcg' => 'chemical/x-gcg8-sequence',
webmaster@9: 'gen' => 'chemical/x-genbank',
webmaster@9: 'hin' => 'chemical/x-hin',
webmaster@9: 'istr|ist' => 'chemical/x-isostar',
webmaster@9: 'jdx|dx' => 'chemical/x-jcamp-dx',
webmaster@9: 'kin' => 'chemical/x-kinemage',
webmaster@9: 'mcm' => 'chemical/x-macmolecule',
webmaster@9: 'mmd|mmod' => 'chemical/x-macromodel-input',
webmaster@9: 'mol' => 'chemical/x-mdl-molfile',
webmaster@9: 'rd' => 'chemical/x-mdl-rdfile',
webmaster@9: 'rxn' => 'chemical/x-mdl-rxnfile',
webmaster@9: 'sd|sdf' => 'chemical/x-mdl-sdfile',
webmaster@9: 'tgf' => 'chemical/x-mdl-tgf',
webmaster@9: 'mcif' => 'chemical/x-mmcif',
webmaster@9: 'mol2' => 'chemical/x-mol2',
webmaster@9: 'b' => 'chemical/x-molconn-Z',
webmaster@9: 'gpt' => 'chemical/x-mopac-graph',
webmaster@9: 'mop|mopcrt|mpc|dat|zmt' => 'chemical/x-mopac-input',
webmaster@9: 'moo' => 'chemical/x-mopac-out',
webmaster@9: 'mvb' => 'chemical/x-mopac-vib',
webmaster@9: 'asn' => 'chemical/x-ncbi-asn1-spec',
webmaster@9: 'prt|ent' => 'chemical/x-ncbi-asn1-ascii',
webmaster@9: 'val|aso' => 'chemical/x-ncbi-asn1-binary',
webmaster@9: 'pdb|ent' => 'chemical/x-pdb',
webmaster@9: 'ros' => 'chemical/x-rosdal',
webmaster@9: 'sw' => 'chemical/x-swissprot',
webmaster@9: 'vms' => 'chemical/x-vamas-iso14976',
webmaster@9: 'vmd' => 'chemical/x-vmd',
webmaster@9: 'xtel' => 'chemical/x-xtel',
webmaster@9: 'xyz' => 'chemical/x-xyz',
webmaster@9: 'gif' => 'image/gif',
webmaster@9: 'ief' => 'image/ief',
webmaster@9: 'jpeg|jpg|jpe' => 'image/jpeg',
webmaster@9: 'pcx' => 'image/pcx',
webmaster@9: 'png' => 'image/png',
webmaster@9: 'svg|svgz' => 'image/svg+xml',
webmaster@9: 'tiff|tif' => 'image/tiff',
webmaster@9: 'djvu|djv' => 'image/vnd.djvu',
webmaster@9: 'wbmp' => 'image/vnd.wap.wbmp',
webmaster@9: 'ras' => 'image/x-cmu-raster',
webmaster@9: 'cdr' => 'image/x-coreldraw',
webmaster@9: 'pat' => 'image/x-coreldrawpattern',
webmaster@9: 'cdt' => 'image/x-coreldrawtemplate',
webmaster@9: 'ico' => 'image/x-icon',
webmaster@9: 'art' => 'image/x-jg',
webmaster@9: 'jng' => 'image/x-jng',
webmaster@9: 'bmp' => 'image/x-ms-bmp',
webmaster@9: 'psd' => 'image/x-photoshop',
webmaster@9: 'pnm' => 'image/x-portable-anymap',
webmaster@9: 'pbm' => 'image/x-portable-bitmap',
webmaster@9: 'pgm' => 'image/x-portable-graymap',
webmaster@9: 'ppm' => 'image/x-portable-pixmap',
webmaster@9: 'rgb' => 'image/x-rgb',
webmaster@9: 'xbm' => 'image/x-xbitmap',
webmaster@9: 'xpm' => 'image/x-xpixmap',
webmaster@9: 'xwd' => 'image/x-xwindowdump',
webmaster@9: 'eml' => 'message/rfc822',
webmaster@9: 'igs|iges' => 'model/iges',
webmaster@9: 'msh|mesh|silo' => 'model/mesh',
webmaster@9: 'wrl|vrml' => 'model/vrml',
webmaster@9: 'ics|icz' => 'text/calendar',
webmaster@9: 'css' => 'text/css',
webmaster@9: 'csv' => 'text/csv',
webmaster@9: '323' => 'text/h323',
webmaster@9: 'html|htm|shtml' => 'text/html',
webmaster@9: 'uls' => 'text/iuls',
webmaster@9: 'mml' => 'text/mathml',
webmaster@9: 'asc|txt|text|pot' => 'text/plain',
webmaster@9: 'rtx' => 'text/richtext',
webmaster@9: 'sct|wsc' => 'text/scriptlet',
webmaster@9: 'tm|ts' => 'text/texmacs',
webmaster@9: 'tsv' => 'text/tab-separated-values',
webmaster@9: 'jad' => 'text/vnd.sun.j2me.app-descriptor',
webmaster@9: 'wml' => 'text/vnd.wap.wml',
webmaster@9: 'wmls' => 'text/vnd.wap.wmlscript',
webmaster@9: 'bib' => 'text/x-bibtex',
webmaster@9: 'boo' => 'text/x-boo',
webmaster@9: 'h++|hpp|hxx|hh' => 'text/x-c++hdr',
webmaster@9: 'c++|cpp|cxx|cc' => 'text/x-c++src',
webmaster@9: 'h' => 'text/x-chdr',
webmaster@9: 'htc' => 'text/x-component',
webmaster@9: 'c' => 'text/x-csrc',
webmaster@9: 'd' => 'text/x-dsrc',
webmaster@9: 'diff|patch' => 'text/x-diff',
webmaster@9: 'hs' => 'text/x-haskell',
webmaster@9: 'java' => 'text/x-java',
webmaster@9: 'lhs' => 'text/x-literate-haskell',
webmaster@9: 'moc' => 'text/x-moc',
webmaster@9: 'p|pas' => 'text/x-pascal',
webmaster@9: 'gcd' => 'text/x-pcs-gcd',
webmaster@9: 'pl|pm' => 'text/x-perl',
webmaster@9: 'py' => 'text/x-python',
webmaster@9: 'etx' => 'text/x-setext',
webmaster@9: 'tcl|tk' => 'text/x-tcl',
webmaster@9: 'tex|ltx|sty|cls' => 'text/x-tex',
webmaster@9: 'vcs' => 'text/x-vcalendar',
webmaster@9: 'vcf' => 'text/x-vcard',
webmaster@9: '3gp' => 'video/3gpp',
webmaster@9: 'dl' => 'video/dl',
webmaster@9: 'dif|dv' => 'video/dv',
webmaster@9: 'fli' => 'video/fli',
webmaster@9: 'gl' => 'video/gl',
webmaster@9: 'mpeg|mpg|mpe' => 'video/mpeg',
webmaster@9: 'mp4' => 'video/mp4',
webmaster@9: 'ogv' => 'video/ogg',
webmaster@9: 'qt|mov' => 'video/quicktime',
webmaster@9: 'mxu' => 'video/vnd.mpegurl',
webmaster@9: 'lsf|lsx' => 'video/x-la-asf',
webmaster@9: 'mng' => 'video/x-mng',
webmaster@9: 'asf|asx' => 'video/x-ms-asf',
webmaster@9: 'wm' => 'video/x-ms-wm',
webmaster@9: 'wmv' => 'video/x-ms-wmv',
webmaster@9: 'wmx' => 'video/x-ms-wmx',
webmaster@9: 'wvx' => 'video/x-ms-wvx',
webmaster@9: 'avi' => 'video/x-msvideo',
webmaster@9: 'movie' => 'video/x-sgi-movie',
webmaster@9: 'ice' => 'x-conference/x-cooltalk',
webmaster@9: 'sisx' => 'x-epoc/x-sisx-app',
webmaster@9: 'vrm|vrml|wrl' => 'x-world/x-vrml',
webmaster@9: 'xps' => 'application/vnd.ms-xpsdocument',
webmaster@9: ));
webmaster@9: }
webmaster@9: foreach ($mapping as $ext_preg => $mime_match) {
webmaster@9: if (preg_match('!\.('. $ext_preg .')$!i', $filename)) {
webmaster@9: return $mime_match;
webmaster@9: }
webmaster@9: }
webmaster@9:
webmaster@9: return 'application/octet-stream';
webmaster@9: }
webmaster@9:
webmaster@9: /**
webmaster@1: * @} End of "defgroup file".
webmaster@1: */