You are here

function _imagecrop_file_load in Image javascript crop 6

Same name and namespace in other branches
  1. 5 imagecrop.module \_imagecrop_file_load()

Helper function to load a file into an object

Parameters

$fid file id:

$module specific module which does not use the files table:

Return value

$file with properties of the file or false

1 call to _imagecrop_file_load()
create_image_object in ./imagecrop.module
Helper function to create image

File

./imagecrop.module, line 761
Provides a javascript toolbox through an imagecache action.

Code

function _imagecrop_file_load($fid, $module) {
  global $user;
  if (empty($module) || $module == 'imagefield') {
    $file = db_fetch_object(db_query('SELECT * FROM {files} WHERE fid = %d', $fid));
  }
  elseif ($module == 'node_images') {
    $file = db_fetch_object(db_query('SELECT * FROM {node_images} WHERE id = %d', $fid));
  }
  elseif ($module == 'user') {
    $filepath = db_result(db_query('SELECT picture FROM {users} WHERE uid = %d', $fid));
    if ($filepath) {
      $file = new stdClass();
      $file->uid = $fid;
      $file->filepath = $filepath;
      $file->filemime = file_get_mimetype($filepath);
    }
  }
  elseif ($module == 'taxonomy_image') {
    $filepath = db_result(db_query('SELECT path FROM {term_image} WHERE tid = %d', $fid));
    if ($filepath) {
      $file_directory = variable_get('file_directory_path', 'sites/default/files');
      $file_directory .= '/' . variable_get('taxonomy_image_path', '');
      $file = new stdClass();
      $file->filepath = $file_directory . '/' . $filepath;
      $file->filemime = file_get_mimetype($filepath);
    }
  }
  if ($file) {

    // make sure it's an image. Any other mime extensions possible?
    // return false if it's not the right mime type
    $filemime = array(
      'image/jpeg',
      'image/gif',
      'image/png',
      'image/pjpeg',
    );
    if (!in_array($file->filemime, $filemime)) {
      return FALSE;
    }

    // access denied if current user hasn't enough permissions
    if (!user_access('crop any image with toolbox')) {
      if ($module != 'user' && !user_access('administer nodes') && $user->uid != $file->uid) {
        drupal_access_denied();
        exit;
      }
      elseif ($user->uid != $file->uid) {
        drupal_access_denied();
        exit;
      }
    }

    // all seems ok, return file
    return $file;
  }

  // return false if no file was found.
  return FALSE;
}