imce.module 6.1 KB

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