comparison sites/all/modules/imce/imce.module @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ff03f76ab3fe
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_permission().
51 */
52 function imce_permission() {
53 return array(
54 'administer imce' => array(
55 'title' => t('Administer IMCE'),
56 'restrict access' => TRUE,
57 ),
58 );
59 }
60
61 /**
62 * Implements hook_theme().
63 */
64 function imce_theme() {
65 $path = drupal_get_path('module', 'imce') . '/tpl';
66 $theme['imce_admin'] = array('function' => 'imce_admin_theme', 'render element' => 'form');
67 $theme['imce_directories'] = array('function' => 'imce_directories_theme', 'render element' => 'form');
68 $theme['imce_thumbnails'] = array('function' => 'imce_thumbnails_theme', 'render element' => 'form');
69 $theme['imce_root_text'] = array(
70 'variables' => array('imce_ref' => NULL),
71 );
72 $theme['imce_user_page'] = array(
73 'variables' => array('account' => NULL),
74 );
75 $theme['imce_file_list'] = array(
76 'template' => 'imce-file-list',
77 'variables' => array('imce_ref' => NULL),
78 'path' => $path,
79 );
80 $theme['imce_content'] = array(
81 'template' => 'imce-content',
82 'variables' => array('tree' => NULL, 'forms' => NULL, 'imce_ref' => NULL),
83 'path' => $path,
84 );
85 $theme['imce_page'] = array(
86 'template' => 'imce-page',
87 'variables' => array('content' => NULL),
88 'path' => $path,
89 );
90 return $theme;
91 }
92
93 /**
94 * Implements hook_file_download().
95 * Support private downloads if not disabled.
96 */
97 function imce_file_download($uri) {
98 $serve = file_uri_scheme($uri) == 'private' && !variable_get('imce_settings_disable_private', 1) && file_exists($uri) && strpos(basename($uri), '.');
99 if ($serve) {
100 return array(
101 'Content-type' => file_get_mimetype($uri),
102 'Content-Length' => filesize($uri),
103 );
104 }
105 }
106
107 /**
108 * Implements hook_element_info().
109 */
110 function imce_element_info() {
111 return array('textarea' => array('#process' => array('imce_textarea')));
112 }
113
114 /**
115 * Inline image/link insertion to textareas.
116 */
117 function imce_textarea($element) {
118 static $regexp;
119 if (!isset($regexp)) {
120 $regexp = FALSE;
121 if (imce_access() && $regexp = str_replace(' ', '', variable_get('imce_settings_textarea', ''))) {
122 $regexp = '@^(' . str_replace(',', '|', implode('.*', array_map('preg_quote', explode('*', $regexp)))) . ')$@';
123 }
124 }
125 if ($regexp && preg_match($regexp, $element['#id'])) {
126 drupal_add_js(drupal_get_path('module', 'imce') . '/js/imce_set_inline.js');
127 $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>';
128 }
129 return $element;
130 }
131
132 /**
133 * Returns the configuration profile assigned to a user for a specific file scheme.
134 */
135 function imce_user_profile($user, $scheme = NULL) {
136 static $ups = array();
137
138 // Set scheme
139 if (empty($scheme)) {
140 $scheme = variable_get('file_default_scheme', 'public');
141 }
142
143 // Return from cache.
144 if (isset($ups[$scheme][$user->uid])) {
145 return $ups[$scheme][$user->uid];
146 }
147 $ups[$scheme][$user->uid] = FALSE;
148
149 // Check scheme
150 $swrappers = file_get_stream_wrappers();
151 if (!isset($swrappers[$scheme])) {
152 return FALSE;
153 }
154
155 $profiles = variable_get('imce_profiles', array());
156 $scinfo = array('scheme' => $scheme);
157
158 // Handle user#1 separately
159 if ($user->uid == 1) {
160 return $ups[$scheme][$user->uid] = isset($profiles[1]) ? $profiles[1] + $scinfo : FALSE;
161 }
162
163 // Handle regular users.
164 $roles_profiles = variable_get('imce_roles_profiles', array());
165 $sckey = $scheme . '_pid';
166 foreach ($roles_profiles as $rid => $conf) {
167 if (isset($user->roles[$rid]) && isset($conf[$sckey]) && isset($profiles[$conf[$sckey]])) {
168 return $ups[$scheme][$user->uid] = $profiles[$conf[$sckey]] + $scinfo;
169 }
170 }
171
172 return FALSE;
173 }
174
175 /**
176 * Checks if the user is assigned an imce profile.
177 * A more detailed assignment check is performed before imce loads.
178 */
179 function imce_access($user = FALSE, $scheme = NULL) {
180 if ($user === FALSE) {
181 global $user;
182 }
183 return imce_user_profile($user, $scheme) ? TRUE : FALSE;
184 }
185
186 /**
187 * Checks access to user/{$account->uid}/imce for the $user.
188 */
189 function imce_user_page_access($account, $user = FALSE) {
190 if ($user === FALSE) {
191 global $user;
192 }
193 return ($user->uid == 1 || $account->uid == $user->uid) && ($profile = imce_user_profile($account)) && $profile['usertab'];
194 }
195
196 /**
197 * Check if the directory name is regular.
198 */
199 function imce_reg_dir($dirname) {
200 return $dirname == '.' || is_int($dirname) || (is_string($dirname) && $dirname != '' && !preg_match('@(^\s)|(^/)|(^\./)|(\s$)|(/$)|(/\.$)|(\.\.)|(//)|(\\\\)|(/\./)@', $dirname));
201 }