Projet

Général

Profil

Paste
Télécharger (6,32 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / imce / imce.module @ 05237dd8

1
<?php
2

    
3
/**
4
 * @file
5
 * Implements the necessary hooks for the file browser to work properly.
6
 */
7

    
8
/**
9
 * Implements hook_menu().
10
 */
11
function imce_menu() {
12
  $items = array();
13
  $access = array('administer imce');
14
  $items['imce'] = array(
15
    'title' => 'File browser',
16
    'page callback' => 'imce',
17
    'access callback' => 'imce_access',
18
    'access arguments' => array(FALSE, 1),
19
    'file' => 'inc/imce.page.inc',
20
    'type' => MENU_CALLBACK,
21
  );
22
  $items['user/%user/imce'] = array(
23
    'title' => 'File browser',
24
    'page callback' => 'imce_user_page',
25
    'page arguments' => array(1),
26
    'access callback' => 'imce_user_page_access',
27
    'access arguments' => array(1),
28
    'file' => 'inc/imce.page.inc',
29
    'type' => MENU_LOCAL_TASK,
30
    'weight' => 10,
31
  );
32
  $items['admin/config/media/imce'] = array(
33
    'title' => 'IMCE',
34
    'description' => 'Control how your image/file browser works.',
35
    'page callback' => 'imce_admin',
36
    'access arguments' => $access,
37
    'file' => 'inc/imce.admin.inc',
38
  );
39
  $items['admin/config/media/imce/profile'] = array(
40
    'title' => 'Add new profile',
41
    'page callback' => 'imce_profile_operations',
42
    'access arguments' => $access,
43
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
44
    'file' => 'inc/imce.admin.inc',
45
  );
46
  return $items;
47
}
48

    
49
/**
50
 * Implements hook_admin_paths().
51
 */
52
function imce_admin_paths() {
53
  if (variable_get('imce_settings_admin_theme', FALSE)) {
54
    return array(
55
      'imce' => TRUE,
56
      'imce/*' => TRUE,
57
      'file/imce/*' => TRUE,
58
      'imce-filefield/*' => TRUE,
59
    );
60
  }
61
}
62

    
63
/**
64
 * Implements hook_permission().
65
 */
66
function imce_permission() {
67
  return array(
68
    'administer imce' => array(
69
      'title' => t('Administer IMCE'),
70
      'restrict access' => TRUE,
71
    ),
72
  );
73
}
74

    
75
/**
76
 * Implements hook_theme().
77
 */
78
function imce_theme() {
79
  $path = drupal_get_path('module', 'imce') . '/tpl';
80
  $theme['imce_admin'] = array('function' => 'imce_admin_theme', 'render element' => 'form');
81
  $theme['imce_directories'] = array('function' => 'imce_directories_theme', 'render element' => 'form');
82
  $theme['imce_thumbnails'] = array('function' => 'imce_thumbnails_theme', 'render element' => 'form');
83
  $theme['imce_root_text'] = array(
84
    'variables' => array('imce_ref' => NULL),
85
  );
86
  $theme['imce_user_page'] = array(
87
    'variables' => array('account' => NULL),
88
  );
89
  $theme['imce_file_list'] = array(
90
    'template' => 'imce-file-list',
91
    'variables' => array('imce_ref' => NULL),
92
    'path' => $path,
93
  );
94
  $theme['imce_content'] = array(
95
    'template' => 'imce-content',
96
    'variables' => array('tree' => NULL, 'forms' => NULL, 'imce_ref' => NULL),
97
    'path' => $path,
98
  );
99
  $theme['imce_page'] = array(
100
    'template' => 'imce-page',
101
    'variables' => array('content' => NULL),
102
    'path' => $path,
103
  );
104
  return $theme;
105
}
106

    
107
/**
108
 * Implements hook_file_download().
109
 * Support private downloads if not disabled.
110
 */
111
function imce_file_download($uri) {
112
  $serve = file_uri_scheme($uri) == 'private' && !variable_get('imce_settings_disable_private', 1) &&  file_exists($uri) && strpos(basename($uri), '.');
113
  if ($serve) {
114
    return array(
115
      'Content-type' => file_get_mimetype($uri),
116
      'Content-Length' => filesize($uri),
117
    );
118
  }
119
}
120

    
121
/**
122
 * Implements hook_element_info().
123
 */
124
function imce_element_info() {
125
  return array('textarea' => array('#process' => array('imce_textarea')));
126
}
127

    
128
/**
129
 * Inline image/link insertion to textareas.
130
 */
131
function imce_textarea($element) {
132
  static $regexp;
133
  if (!isset($regexp)) {
134
    $regexp = FALSE;
135
    if (imce_access() && $regexp = str_replace(' ', '', variable_get('imce_settings_textarea', ''))) {
136
      $regexp = '@^(' . str_replace(',', '|', implode('.*', array_map('preg_quote', explode('*', $regexp)))) . ')$@';
137
    }
138
  }
139
  if ($regexp && preg_match($regexp, $element['#id'])) {
140
    drupal_add_js(drupal_get_path('module', 'imce') . '/js/imce_set_inline.js');
141
    $element['#description'] = (isset($element['#description']) ? $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' => array('imce-inline-image')))), '!link' => l(t('link'), 'imce', array('attributes' => array('name' => $element['#id'] . '-IMCE-link', 'class' => array('imce-inline-link')))))) . '</div>';
142
  }
143
  return $element;
144
}
145

    
146
/**
147
 * Returns the configuration profile assigned to a user for a specific file scheme.
148
 */
149
function imce_user_profile($user, $scheme = NULL) {
150
  static $ups = array();
151

    
152
  // Set scheme
153
  if (empty($scheme)) {
154
    $scheme = variable_get('file_default_scheme', 'public');
155
  }
156

    
157
  // Return from cache.
158
  if (isset($ups[$scheme][$user->uid])) {
159
    return $ups[$scheme][$user->uid];
160
  }
161
  $ups[$scheme][$user->uid] = FALSE;
162

    
163
  // Check scheme
164
  $swrappers = file_get_stream_wrappers();
165
  if (!isset($swrappers[$scheme])) {
166
    return FALSE;
167
  }
168

    
169
  $profiles = variable_get('imce_profiles', array());
170
  $scinfo = array('scheme' => $scheme);
171

    
172
  // Handle user#1 separately
173
  if ($user->uid == 1) {
174
    return $ups[$scheme][$user->uid] = isset($profiles[1]) ? $profiles[1] + $scinfo : FALSE;
175
  }
176

    
177
  // Handle regular users.
178
  $roles_profiles = variable_get('imce_roles_profiles', array());
179
  $sckey = $scheme . '_pid';
180
  foreach ($roles_profiles as $rid => $conf) {
181
    if (isset($user->roles[$rid]) && isset($conf[$sckey]) && isset($profiles[$conf[$sckey]])) {
182
      return $ups[$scheme][$user->uid] = $profiles[$conf[$sckey]] + $scinfo;
183
    }
184
  }
185

    
186
  return FALSE;
187
}
188

    
189
/**
190
 * Checks if the user is assigned an imce profile.
191
 * A more detailed assignment check is performed before imce loads.
192
 */
193
function imce_access($user = FALSE, $scheme = NULL) {
194
  if ($user === FALSE) {
195
    global $user;
196
  }
197
  return imce_user_profile($user, $scheme) ? TRUE : FALSE;
198
}
199

    
200
/**
201
 * Checks access to user/{$account->uid}/imce for the $user.
202
 */
203
function imce_user_page_access($account, $user = FALSE) {
204
  if ($user === FALSE) {
205
    global $user;
206
  }
207
  return ($user->uid == 1 || $account->uid == $user->uid) && ($profile = imce_user_profile($account)) && $profile['usertab'];
208
}
209

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