web/drupal/modules/imce/imce.module
author cavaliet@caf4f556-3d62-0410-8435-a86758001935
Fri, 21 Aug 2009 16:26:26 +0000
branchdrupal
changeset 74 0ff3ba646492
permissions -rw-r--r--
Create branch for drupal with first commit from local working copy

<?php
// $Id: imce.module,v 1.23.2.5 2008/07/19 13:21:02 ufku Exp $

/**
 * Implementation of hook_menu().
 */
function imce_menu() {
  $items = array();
  $access = array('administer site configuration');
  $items['imce'] = array(
    'title' => 'File browser',
    'page callback' => 'imce_page',
    'access callback' => 'imce_access',
    'file' => 'inc/page.inc',
    'type' => MENU_CALLBACK,
  );
  $items['admin/settings/imce'] = array(
    'title' => 'IMCE',
    'description' => 'Control how your image/file browser works.',
    'page callback' => 'imce_admin',
    'access arguments' => $access,
    'file' => 'inc/admin.inc',
  );
  $items['admin/settings/imce/profiles'] = array(
    'title' => 'Settings',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/settings/imce/subdirectory'] = array(
    'title' => 'Directory creation tool',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('imce_form_subdirectory'),
    'access arguments' => $access,
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
    'file' => 'inc/subdir.inc',
  );
  $items['admin/settings/imce/profile'] = array(
    'title' => 'Add new profile',
    'page callback' => 'imce_profile_operations',
    'access arguments' => $access,
    'type' => MENU_CALLBACK,
    'file' => 'inc/admin.inc',
  );
  $items['user/%user/imce'] = array(
    'title' => 'File browser',
    'page callback' => 'imce_user_page',
    'page arguments' => array(1),
    'access callback' => 'imce_user_page_access',
    'access arguments' => array(1),
    'file' => 'inc/page.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function imce_theme() {
  $path = drupal_get_path('module', 'imce') .'/tpl';
  $theme['imce_admin']['function'] = 'imce_admin_theme';
  $theme['imce_directories']['function'] = 'imce_directories_theme';
  $theme['imce_thumbnails']['function'] = 'imce_thumbnails_theme';
  $theme['imce_file_list'] = array(
    'template' => 'imce-file-list',
    'arguments' => array('imce_ref' => NULL),
    'path' => $path,
  );
  $theme['imce_content'] = array(
    'template' => 'imce-content',
    'arguments' => array('tree' => NULL, 'forms' => NULL, 'imce_ref' => NULL),
    'path' => $path,
  );
  $theme['imce_page'] = array(
    'template' => 'imce-page',
    'arguments' => array('content' => NULL),
    'path' => $path,
  );
  return $theme;
}

/**
 * Implementation of hook_file_download().
 */
function imce_file_download($file) {
  if ($path = file_create_path($file)) {
    if ($info = @getimagesize($path)) {
      $type = $info['mime'];
    }
    else if (function_exists('finfo_file') && $finfo = @finfo_open(FILEINFO_MIME)) {
      $type = finfo_file($finfo, $path);
      finfo_close($finfo);
    }
    else if ($result = db_result(db_query("SELECT filemime FROM {files} WHERE filepath = '%s'", $path))) {
      $type = $result;
    }
    else if (function_exists('mime_content_type')) {
      $type = mime_content_type($path);
    }
    else {
      $type = 'application/x-download';
    }
    return array('Content-type: '. $type, 'Content-Length: '. filesize($path));
  }
}

/**
 * Implementation of hook_elements().
 */
function imce_elements() {
  return array('textarea' => array('#process' => array('imce_textarea')));
}

/**
 * Inline image/link insertion to textareas.
 */
function imce_textarea($element) {
  static $ids;
  if (!isset($ids)) {
    $ids = FALSE;
    if (imce_access() && $setting = str_replace(' ', '', variable_get('imce_settings_textarea', ''))) {
      $ids = array();
      foreach (explode(',', $setting) as $id) {
        $ids[$id] = 1;
      }
    }
  }
  if ($ids && isset($ids[$element['#id']])) {
    drupal_add_js(drupal_get_path('module', 'imce') .'/js/imce_set_inline.js');
    $element['#description'] .= '<div class="imce-inline-wrapper" style="display:none">'. t('Insert !image or !link.', array('!image' => l(t('image'), 'imce', array('attributes' => array('name' => $element['#id'] .'-IMCE-image', 'class' => 'imce-inline-image'))), '!link' => l(t('link'), 'imce', array('attributes' => array('name' => $element['#id'] .'-IMCE-link', 'class' => 'imce-inline-link'))))) .'</div>';
  }
  return $element;
}

/**
 * Get the profile for the user.
 */
function imce_user_profile($user) {
  $profiles = variable_get('imce_profiles', array());
  if ($user->uid == 1 && isset($profiles[1])) {
    return $profiles[1];
  }
  else {
    foreach (variable_get('imce_roles_profiles', array()) as $rid => $role) {
      if (isset($user->roles[$rid]) && isset($profiles[$role['pid']])) {
        return $profiles[$role['pid']];
      }
    }
  }
  return FALSE;
}

/**
 * Check if the user has access to imce.
 */
function imce_access($user = FALSE) {
  if ($user === FALSE) {
    global $user;
  }

  if ($user->uid == 1) {
    return TRUE;
  }

  $roles_profiles = variable_get('imce_roles_profiles', array());
  foreach ($user->roles as $rid => $name) {
    if (isset($roles_profiles[$rid]['pid']) && $roles_profiles[$rid]['pid']) {
      return TRUE;
    }
  }

  return FALSE;
}

/**
 * Defines access to user/{$account->uid}/imce for the $user.
 */
function imce_user_page_access($account, $user = FALSE) {
  if ($user === FALSE) {
    global $user;
  }

  return $user->uid == 1 || ($account->uid == $user->uid && imce_access($user));
}

/**
 * Check if the directory name is regular.
 */
function imce_reg_dir($dirname) {
  return $dirname == '.' || (is_string($dirname) && $dirname != '' && !preg_match('@(^\s)|(^/)|(^\./)|(\s$)|(/$)|(/\.$)|(\.\.)|(//)|(\\\\)|(/\./)@', $dirname));
}