summaryrefslogtreecommitdiff
path: root/kolab.org/www/drupal-7.18/sites/all/modules/imce/imce.module
blob: f3dc4bf79dc3755c53a252de3f629f878e6f35d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php

/**
 * @file
 * Implements the necessary hooks for the file browser to work properly.
 */

/**
 * Implements hook_menu().
 */
function imce_menu() {
  $items = array();
  $access = array('administer imce');
  $items['imce'] = array(
    'title' => 'File browser',
    'page callback' => 'imce',
    'access callback' => 'imce_access',
    'access arguments' => array(FALSE, 1),
    'file' => 'inc/imce.page.inc',
    'type' => MENU_CALLBACK,
  );
  $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/imce.page.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
  );
  $items['admin/config/media/imce'] = array(
    'title' => 'IMCE',
    'description' => 'Control how your image/file browser works.',
    'page callback' => 'imce_admin',
    'access arguments' => $access,
    'file' => 'inc/imce.admin.inc',
  );
  $items['admin/config/media/imce/profile'] = array(
    'title' => 'Add new profile',
    'page callback' => 'imce_profile_operations',
    'access arguments' => $access,
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
    'file' => 'inc/imce.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function imce_permission() {
  return array(
    'administer imce' => array(
      'title' => t('Administer IMCE'),
      'restrict access' => TRUE,
    ),
  );
}

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

/**
 * Implements hook_file_download().
 * Support private downloads if not disabled.
 */
function imce_file_download($uri) {
  $serve = file_uri_scheme($uri) == 'private' && !variable_get('imce_settings_disable_private', 1) &&  file_exists($uri) && strpos(basename($uri), '.');
  if ($serve) {
    return array(
      'Content-type' => file_get_mimetype($uri),
      'Content-Length' => filesize($uri),
    );
  }
}

/**
 * Implements hook_element_info().
 */
function imce_element_info() {
  return array('textarea' => array('#process' => array('imce_textarea')));
}

/**
 * Inline image/link insertion to textareas.
 */
function imce_textarea($element) {
  static $regexp;
  if (!isset($regexp)) {
    $regexp = FALSE;
    if (imce_access() && $regexp = str_replace(' ', '', variable_get('imce_settings_textarea', ''))) {
      $regexp = '@^(' . str_replace(',', '|', implode('.*', array_map('preg_quote', explode('*', $regexp)))) . ')$@';
    }
  }
  if ($regexp && preg_match($regexp, $element['#id'])) {
    drupal_add_js(drupal_get_path('module', 'imce') . '/js/imce_set_inline.js');
    $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>';
  }
  return $element;
}

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

  // Set scheme
  if (empty($scheme)) {
    $scheme = variable_get('file_default_scheme', 'public');
  }

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

  // Check scheme
  $swrappers = file_get_stream_wrappers();
  if (!isset($swrappers[$scheme])) {
    return FALSE;
  }

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

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

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

  return FALSE;
}

/**
 * Checks if the user is assigned an imce profile.
 * A more detailed assignment check is performed before imce loads.
 */
function imce_access($user = FALSE, $scheme = NULL) {
  if ($user === FALSE) {
    global $user;
  }
  return imce_user_profile($user, $scheme) ? TRUE : FALSE;
}

/**
 * Checks 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) && ($profile = imce_user_profile($account)) && $profile['usertab'];
}

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