imce.module 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * @file
  4. * Implements the necessary hooks for the file browser to work properly.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function imce_menu() {
  10. $items = array();
  11. $access = array('administer imce');
  12. $items['imce'] = array(
  13. 'title' => 'File browser',
  14. 'page callback' => 'imce',
  15. 'access callback' => 'imce_access',
  16. 'access arguments' => array(FALSE, 1),
  17. 'file' => 'inc/imce.page.inc',
  18. 'type' => MENU_CALLBACK,
  19. );
  20. $items['user/%user/imce'] = array(
  21. 'title' => 'File browser',
  22. 'page callback' => 'imce_user_page',
  23. 'page arguments' => array(1),
  24. 'access callback' => 'imce_user_page_access',
  25. 'access arguments' => array(1),
  26. 'file' => 'inc/imce.page.inc',
  27. 'type' => MENU_LOCAL_TASK,
  28. 'weight' => 10,
  29. );
  30. $items['admin/config/media/imce'] = array(
  31. 'title' => 'IMCE',
  32. 'description' => 'Control how your image/file browser works.',
  33. 'page callback' => 'imce_admin',
  34. 'access arguments' => $access,
  35. 'file' => 'inc/imce.admin.inc',
  36. );
  37. $items['admin/config/media/imce/profile'] = array(
  38. 'title' => 'Add new profile',
  39. 'page callback' => 'imce_profile_operations',
  40. 'access arguments' => $access,
  41. 'type' => MENU_VISIBLE_IN_BREADCRUMB,
  42. 'file' => 'inc/imce.admin.inc',
  43. );
  44. return $items;
  45. }
  46. /**
  47. * Implements hook_admin_paths().
  48. */
  49. function imce_admin_paths() {
  50. if (variable_get('imce_settings_admin_theme', FALSE)) {
  51. return array(
  52. 'imce' => TRUE,
  53. 'imce/*' => TRUE,
  54. 'file/imce/*' => TRUE,
  55. 'imce-filefield/*' => TRUE,
  56. );
  57. }
  58. }
  59. /**
  60. * Implements hook_permission().
  61. */
  62. function imce_permission() {
  63. return array(
  64. 'administer imce' => array(
  65. 'title' => t('Administer IMCE'),
  66. 'restrict access' => TRUE,
  67. ),
  68. );
  69. }
  70. /**
  71. * Implements hook_theme().
  72. */
  73. function imce_theme() {
  74. $path = drupal_get_path('module', 'imce') . '/tpl';
  75. $theme['imce_admin'] = array('function' => 'imce_admin_theme', 'render element' => 'form');
  76. $theme['imce_directories'] = array('function' => 'imce_directories_theme', 'render element' => 'form');
  77. $theme['imce_thumbnails'] = array('function' => 'imce_thumbnails_theme', 'render element' => 'form');
  78. $theme['imce_root_text'] = array(
  79. 'variables' => array('imce_ref' => NULL),
  80. );
  81. $theme['imce_user_page'] = array(
  82. 'variables' => array('account' => NULL),
  83. );
  84. $theme['imce_file_list'] = array(
  85. 'template' => 'imce-file-list',
  86. 'variables' => array('imce_ref' => NULL),
  87. 'path' => $path,
  88. );
  89. $theme['imce_content'] = array(
  90. 'template' => 'imce-content',
  91. 'variables' => array('tree' => NULL, 'forms' => NULL, 'imce_ref' => NULL),
  92. 'path' => $path,
  93. );
  94. $theme['imce_page'] = array(
  95. 'template' => 'imce-page',
  96. 'variables' => array('content' => NULL),
  97. 'path' => $path,
  98. );
  99. return $theme;
  100. }
  101. /**
  102. * Implements hook_file_download().
  103. * Support private downloads if not disabled.
  104. */
  105. function imce_file_download($uri) {
  106. $serve = file_uri_scheme($uri) == 'private' && !variable_get('imce_settings_disable_private', 1) && file_exists($uri) && strpos(basename($uri), '.');
  107. if ($serve) {
  108. return array(
  109. 'Content-type' => file_get_mimetype($uri),
  110. 'Content-Length' => filesize($uri),
  111. );
  112. }
  113. }
  114. /**
  115. * Implements hook_element_info().
  116. */
  117. function imce_element_info() {
  118. return array('textarea' => array('#process' => array('imce_textarea')));
  119. }
  120. /**
  121. * Inline image/link insertion to textareas.
  122. */
  123. function imce_textarea($element) {
  124. static $regexp;
  125. if (!isset($regexp)) {
  126. $regexp = FALSE;
  127. if (imce_access() && $regexp = str_replace(' ', '', variable_get('imce_settings_textarea', ''))) {
  128. $regexp = '@^(' . str_replace(',', '|', implode('.*', array_map('preg_quote', explode('*', $regexp)))) . ')$@';
  129. }
  130. }
  131. if ($regexp && preg_match($regexp, $element['#id'])) {
  132. drupal_add_js(drupal_get_path('module', 'imce') . '/js/imce_set_inline.js');
  133. $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>';
  134. }
  135. return $element;
  136. }
  137. /**
  138. * Returns the configuration profile assigned to a user for a specific file scheme.
  139. */
  140. function imce_user_profile($user, $scheme = NULL) {
  141. static $ups = array();
  142. // Set scheme
  143. if (empty($scheme)) {
  144. $scheme = variable_get('file_default_scheme', 'public');
  145. }
  146. // Return from cache.
  147. if (isset($ups[$scheme][$user->uid])) {
  148. return $ups[$scheme][$user->uid];
  149. }
  150. $ups[$scheme][$user->uid] = FALSE;
  151. // Check scheme
  152. $swrappers = file_get_stream_wrappers();
  153. if (!isset($swrappers[$scheme])) {
  154. return FALSE;
  155. }
  156. $profiles = variable_get('imce_profiles', array());
  157. $scinfo = array('scheme' => $scheme);
  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. // Handle regular users.
  163. $roles_profiles = variable_get('imce_roles_profiles', array());
  164. $sckey = $scheme . '_pid';
  165. foreach ($roles_profiles as $rid => $conf) {
  166. if (isset($user->roles[$rid]) && isset($conf[$sckey]) && isset($profiles[$conf[$sckey]])) {
  167. return $ups[$scheme][$user->uid] = $profiles[$conf[$sckey]] + $scinfo;
  168. }
  169. }
  170. return FALSE;
  171. }
  172. /**
  173. * Checks if the user is assigned an imce profile.
  174. * A more detailed assignment check is performed before imce loads.
  175. */
  176. function imce_access($user = FALSE, $scheme = NULL) {
  177. if ($user === FALSE) {
  178. global $user;
  179. }
  180. return imce_user_profile($user, $scheme) ? TRUE : FALSE;
  181. }
  182. /**
  183. * Checks access to user/{$account->uid}/imce for the $user.
  184. */
  185. function imce_user_page_access($account, $user = FALSE) {
  186. if ($user === FALSE) {
  187. global $user;
  188. }
  189. return ($user->uid == 1 || $account->uid == $user->uid) && ($profile = imce_user_profile($account)) && $profile['usertab'];
  190. }
  191. /**
  192. * Check if the directory name is regular.
  193. */
  194. function imce_reg_dir($dirname) {
  195. return $dirname == '.' || is_int($dirname) || (is_string($dirname) && $dirname != '' && !preg_match('@(^\s)|(^/)|(^\./)|(\s$)|(/$)|(/\.$)|(\.\.)|(//)|(\\\\)|(/\./)@', $dirname));
  196. }