You are here

function fckeditor_file_download in FCKeditor - WYSIWYG HTML editor 6

Same name and namespace in other branches
  1. 5.2 fckeditor.module \fckeditor_file_download()
  2. 6.2 fckeditor.module \fckeditor_file_download()

Implementation of hook_file_download(). Support for private downloads. FCKeditor does not implement any kind of potection on private files.

File

./fckeditor.module, line 2357
FCKeditor - The text editor for Internet - http://www.fckeditor.net Copyright (C) 2003-2008 Frederico Caldeira Knabben

Code

function fckeditor_file_download($file) {
  if ($path = file_create_path($file)) {
    $result = db_query("SELECT f.* FROM {files} f WHERE filepath = '%s'", $path);
    if (db_fetch_object($result)) {
      return NULL;
    }

    //No info in DB? Probably a file uploaded with FCKeditor
    $global_profile = fckeditor_profile_load("FCKeditor Global Profile");

    //Assume that files inside of fckeditor directory belong to the FCKeditor. If private directory is set, let the decision about protection to the user.
    $private_dir = isset($global_profile->settings['private_dir']) ? trim($global_profile->settings['private_dir'], '\\/') : '';
    $private_dir = preg_quote($private_dir, '#');
    $private_dir = strtr($private_dir, array(
      '%u' => '(\\d+)',
    ));
    $private_dir = trim($private_dir, '\\/');
    $regex = '#^' . preg_quote(file_directory_path() . '/', '#') . $private_dir . '#';

    //If path to the file points to the FCKeditor private directory, allow downloading
    if (preg_match($regex, $path)) {
      $ctype = ($info = @getimagesize($path)) ? $info['mime'] : (function_exists('mime_content_type') ? mime_content_type($path) : 'application/x-download');
      return array(
        'Content-type: ' . $ctype,
      );
    }
  }
}