web/drupal/modules/imce/imce.module
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 <?php
       
     2 // $Id: imce.module,v 1.23.2.5 2008/07/19 13:21:02 ufku Exp $
       
     3 
       
     4 /**
       
     5  * Implementation of hook_menu().
       
     6  */
       
     7 function imce_menu() {
       
     8   $items = array();
       
     9   $access = array('administer site configuration');
       
    10   $items['imce'] = array(
       
    11     'title' => 'File browser',
       
    12     'page callback' => 'imce_page',
       
    13     'access callback' => 'imce_access',
       
    14     'file' => 'inc/page.inc',
       
    15     'type' => MENU_CALLBACK,
       
    16   );
       
    17   $items['admin/settings/imce'] = array(
       
    18     'title' => 'IMCE',
       
    19     'description' => 'Control how your image/file browser works.',
       
    20     'page callback' => 'imce_admin',
       
    21     'access arguments' => $access,
       
    22     'file' => 'inc/admin.inc',
       
    23   );
       
    24   $items['admin/settings/imce/profiles'] = array(
       
    25     'title' => 'Settings',
       
    26     'type' => MENU_DEFAULT_LOCAL_TASK,
       
    27   );
       
    28   $items['admin/settings/imce/subdirectory'] = array(
       
    29     'title' => 'Directory creation tool',
       
    30     'page callback' => 'drupal_get_form',
       
    31     'page arguments' => array('imce_form_subdirectory'),
       
    32     'access arguments' => $access,
       
    33     'type' => MENU_LOCAL_TASK,
       
    34     'weight' => 1,
       
    35     'file' => 'inc/subdir.inc',
       
    36   );
       
    37   $items['admin/settings/imce/profile'] = array(
       
    38     'title' => 'Add new profile',
       
    39     'page callback' => 'imce_profile_operations',
       
    40     'access arguments' => $access,
       
    41     'type' => MENU_CALLBACK,
       
    42     'file' => 'inc/admin.inc',
       
    43   );
       
    44   $items['user/%user/imce'] = array(
       
    45     'title' => 'File browser',
       
    46     'page callback' => 'imce_user_page',
       
    47     'page arguments' => array(1),
       
    48     'access callback' => 'imce_user_page_access',
       
    49     'access arguments' => array(1),
       
    50     'file' => 'inc/page.inc',
       
    51     'type' => MENU_LOCAL_TASK,
       
    52     'weight' => 10,
       
    53   );
       
    54   return $items;
       
    55 }
       
    56 
       
    57 /**
       
    58  * Implementation of hook_theme().
       
    59  */
       
    60 function imce_theme() {
       
    61   $path = drupal_get_path('module', 'imce') .'/tpl';
       
    62   $theme['imce_admin']['function'] = 'imce_admin_theme';
       
    63   $theme['imce_directories']['function'] = 'imce_directories_theme';
       
    64   $theme['imce_thumbnails']['function'] = 'imce_thumbnails_theme';
       
    65   $theme['imce_file_list'] = array(
       
    66     'template' => 'imce-file-list',
       
    67     'arguments' => array('imce_ref' => NULL),
       
    68     'path' => $path,
       
    69   );
       
    70   $theme['imce_content'] = array(
       
    71     'template' => 'imce-content',
       
    72     'arguments' => array('tree' => NULL, 'forms' => NULL, 'imce_ref' => NULL),
       
    73     'path' => $path,
       
    74   );
       
    75   $theme['imce_page'] = array(
       
    76     'template' => 'imce-page',
       
    77     'arguments' => array('content' => NULL),
       
    78     'path' => $path,
       
    79   );
       
    80   return $theme;
       
    81 }
       
    82 
       
    83 /**
       
    84  * Implementation of hook_file_download().
       
    85  */
       
    86 function imce_file_download($file) {
       
    87   if ($path = file_create_path($file)) {
       
    88     if ($info = @getimagesize($path)) {
       
    89       $type = $info['mime'];
       
    90     }
       
    91     else if (function_exists('finfo_file') && $finfo = @finfo_open(FILEINFO_MIME)) {
       
    92       $type = finfo_file($finfo, $path);
       
    93       finfo_close($finfo);
       
    94     }
       
    95     else if ($result = db_result(db_query("SELECT filemime FROM {files} WHERE filepath = '%s'", $path))) {
       
    96       $type = $result;
       
    97     }
       
    98     else if (function_exists('mime_content_type')) {
       
    99       $type = mime_content_type($path);
       
   100     }
       
   101     else {
       
   102       $type = 'application/x-download';
       
   103     }
       
   104     return array('Content-type: '. $type, 'Content-Length: '. filesize($path));
       
   105   }
       
   106 }
       
   107 
       
   108 /**
       
   109  * Implementation of hook_elements().
       
   110  */
       
   111 function imce_elements() {
       
   112   return array('textarea' => array('#process' => array('imce_textarea')));
       
   113 }
       
   114 
       
   115 /**
       
   116  * Inline image/link insertion to textareas.
       
   117  */
       
   118 function imce_textarea($element) {
       
   119   static $ids;
       
   120   if (!isset($ids)) {
       
   121     $ids = FALSE;
       
   122     if (imce_access() && $setting = str_replace(' ', '', variable_get('imce_settings_textarea', ''))) {
       
   123       $ids = array();
       
   124       foreach (explode(',', $setting) as $id) {
       
   125         $ids[$id] = 1;
       
   126       }
       
   127     }
       
   128   }
       
   129   if ($ids && isset($ids[$element['#id']])) {
       
   130     drupal_add_js(drupal_get_path('module', 'imce') .'/js/imce_set_inline.js');
       
   131     $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>';
       
   132   }
       
   133   return $element;
       
   134 }
       
   135 
       
   136 /**
       
   137  * Get the profile for the user.
       
   138  */
       
   139 function imce_user_profile($user) {
       
   140   $profiles = variable_get('imce_profiles', array());
       
   141   if ($user->uid == 1 && isset($profiles[1])) {
       
   142     return $profiles[1];
       
   143   }
       
   144   else {
       
   145     foreach (variable_get('imce_roles_profiles', array()) as $rid => $role) {
       
   146       if (isset($user->roles[$rid]) && isset($profiles[$role['pid']])) {
       
   147         return $profiles[$role['pid']];
       
   148       }
       
   149     }
       
   150   }
       
   151   return FALSE;
       
   152 }
       
   153 
       
   154 /**
       
   155  * Check if the user has access to imce.
       
   156  */
       
   157 function imce_access($user = FALSE) {
       
   158   if ($user === FALSE) {
       
   159     global $user;
       
   160   }
       
   161 
       
   162   if ($user->uid == 1) {
       
   163     return TRUE;
       
   164   }
       
   165 
       
   166   $roles_profiles = variable_get('imce_roles_profiles', array());
       
   167   foreach ($user->roles as $rid => $name) {
       
   168     if (isset($roles_profiles[$rid]['pid']) && $roles_profiles[$rid]['pid']) {
       
   169       return TRUE;
       
   170     }
       
   171   }
       
   172 
       
   173   return FALSE;
       
   174 }
       
   175 
       
   176 /**
       
   177  * Defines access to user/{$account->uid}/imce for the $user.
       
   178  */
       
   179 function imce_user_page_access($account, $user = FALSE) {
       
   180   if ($user === FALSE) {
       
   181     global $user;
       
   182   }
       
   183 
       
   184   return $user->uid == 1 || ($account->uid == $user->uid && imce_access($user));
       
   185 }
       
   186 
       
   187 /**
       
   188  * Check if the directory name is regular.
       
   189  */
       
   190 function imce_reg_dir($dirname) {
       
   191   return $dirname == '.' || (is_string($dirname) && $dirname != '' && !preg_match('@(^\s)|(^/)|(^\./)|(\s$)|(/$)|(/\.$)|(\.\.)|(//)|(\\\\)|(/\./)@', $dirname));
       
   192 }