elfinder.module 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. <?php
  2. /**
  3. * elFinder is a AJAX file manager with Wysiwyg API capable editors support
  4. * Copyright (c) 2010-2013, Alexey Sukhotin
  5. */
  6. define('ELFINDER_POPUP_WIDTH', 700);
  7. define('ELFINDER_POPUP_HEIGHT', 400);
  8. include_once dirname(elfinder_connector_path()) . DIRECTORY_SEPARATOR . 'elFinder.class.php';
  9. if (class_exists('elFinder')) {
  10. include_once drupal_get_path('module', 'elfinder') . DIRECTORY_SEPARATOR . 'inc' . DIRECTORY_SEPARATOR . 'elfinder.drupal.inc';
  11. }
  12. function elfinder_check_badpaths() {
  13. $ret = array('result' => TRUE, 'message' => '');
  14. $libpath = elfinder_lib_path();
  15. $badfiles = array('connectors/php/connector.php', 'php/connector.php', 'php/connector.minimal.php');
  16. $badpaths = array();
  17. foreach ($badfiles as $badfile) {
  18. $path = $libpath . '/' . $badfile;
  19. if (file_exists($path)) {
  20. $badpaths[] = $path;
  21. }
  22. }
  23. if (count($badpaths) > 0) {
  24. $message = t('elFinder library directory containing features test files :files. Please remove these files to avoid security hole.', array(':files' => implode(', ', $badpaths)));
  25. watchdog('elfinder', $message, array(), WATCHDOG_ALERT);
  26. drupal_set_message($message, 'error');
  27. $ret['result'] = FALSE;
  28. $ret['message'] = $message;
  29. }
  30. return $ret;
  31. }
  32. function elfinder_init() {
  33. elfinder_check_badpaths();
  34. }
  35. /**
  36. * Implements hook_menu().
  37. */
  38. function elfinder_menu() {
  39. $items = array();
  40. $access = array('use file manager');
  41. $items['admin/content/files'] = array(
  42. 'title' => 'Files',
  43. 'page callback' => 'elfinder_file_browser',
  44. 'page arguments' => array('backend'),
  45. 'description' => 'Manage media files.',
  46. 'access arguments' => array('administer file manager'),
  47. );
  48. $items['admin/content/files']['type'] = MENU_LOCAL_TASK;
  49. $items['elfinder'] = array(
  50. 'title' => 'File browser',
  51. 'page callback' => 'elfinder_file_browser',
  52. 'page arguments' => array('default'),
  53. 'access arguments' => $access,
  54. 'type' => MENU_CALLBACK,
  55. );
  56. $items['elfinder/connector'] = array(
  57. 'title' => 'File browser',
  58. 'page callback' => 'elfinder_filesystemconnector',
  59. 'page arguments' => array(NULL),
  60. 'access arguments' => $access,
  61. 'type' => MENU_CALLBACK,
  62. );
  63. // Forced connector relative url mode. For filefield_sources support.
  64. $items['elfinder/connector/relative'] = array(
  65. 'title' => 'File browser',
  66. 'page callback' => 'elfinder_filesystemconnector',
  67. 'page arguments' => array('relative'),
  68. 'access arguments' => $access,
  69. 'type' => MENU_CALLBACK,
  70. );
  71. $settings_page = array(
  72. 'title' => 'elFinder',
  73. 'description' => 'elFinder file manager settings',
  74. 'page callback' => 'elfinder_admin',
  75. 'access arguments' => array('administer file manager'),
  76. 'file' => 'inc/elfinder.admin.inc',
  77. );
  78. $items['admin/config/elfinder'] = $items['admin/config/media/elfinder'] = $settings_page;
  79. $items['admin/config/elfinder']['type'] = MENU_CALLBACK;
  80. $items['admin/config/media/elfinder/profile'] = array(
  81. 'title' => 'Add Profile',
  82. 'page callback' => 'elfinder_admin_profiles',
  83. 'page arguments' => array('profile_add'),
  84. 'access arguments' => array('administer file manager'),
  85. 'type' => MENU_CALLBACK,
  86. 'file' => 'inc/elfinder.admin.profiles.inc',
  87. );
  88. $items['admin/config/media/elfinder/profile/%/delete'] = array(
  89. 'title' => 'Delete Profile',
  90. 'page callback' => 'elfinder_admin_profiles',
  91. 'page arguments' => array('profile_delete', 5),
  92. 'access arguments' => array('administer file manager'),
  93. 'type' => MENU_CALLBACK,
  94. 'file' => 'inc/elfinder.admin.profiles.inc',
  95. );
  96. $items['admin/config/media/elfinder/profile/%/edit'] = array(
  97. 'title' => 'Edit Profile',
  98. 'page callback' => 'drupal_get_form',
  99. 'page arguments' => array('elfinder_admin_profile', 'profile_edit', 5),
  100. 'access arguments' => array('administer file manager'),
  101. 'type' => MENU_CALLBACK,
  102. 'file' => 'inc/elfinder.admin.profiles.inc',
  103. );
  104. $items['elfinder/ahah/dir'] = array(
  105. 'title' => 'JavaScript Volume Add Item',
  106. 'page callback' => 'elfinder_admin_form_dir_js',
  107. 'page arguments' => array('item_add'),
  108. 'access arguments' => array('administer file manager'),
  109. 'type' => MENU_CALLBACK,
  110. 'file' => 'inc/elfinder.admin.profiles.inc',
  111. );
  112. $items['elfinder/ahah/rmdir/%'] = array(
  113. 'title' => 'JavaScript Volume Remove Item',
  114. 'page callback' => 'elfinder_admin_form_dir_js',
  115. 'page arguments' => array('item_remove', 3),
  116. 'access arguments' => array('administer file manager'),
  117. 'type' => MENU_CALLBACK,
  118. 'file' => 'inc/elfinder.admin.profiles.inc',
  119. );
  120. $items['elfinder/upload/fckeditor'] = array(
  121. 'title' => 'FCKeditor Quick Upload',
  122. 'page callback' => 'elfinder_editor_upload_fckeditor',
  123. 'access arguments' => array('access content'),
  124. 'type' => MENU_CALLBACK,
  125. 'file' => 'editors/fckeditor/fckeditor.upload.inc',
  126. );
  127. $items['elfinder/upload/ckeditor'] = array(
  128. 'title' => 'CKeditor Quick Upload',
  129. 'page callback' => 'elfinder_editor_upload_ckeditor',
  130. 'access arguments' => array('access content'),
  131. 'type' => MENU_CALLBACK,
  132. 'file' => 'editors/ckeditor/ckeditor.upload.inc',
  133. );
  134. /* Getting updated menu items from hook_elfinder_menu */
  135. $newitems = module_invoke_all('elfinder_menu', $items);
  136. return ($newitems) ? $newitems : $items;
  137. }
  138. function elfinder_js_alter(&$javascript) {
  139. //$javascript['misc/jquery.js']['data'] = drupal_get_path('module', 'elfinder') . '/js/elfinder.jquery.min.js';
  140. }
  141. /**
  142. * File browser render
  143. */
  144. function elfinder_file_browser($browser_mode = 'default') {
  145. $libpath = elfinder_lib_path() . '/';
  146. $p = elfinder_get_user_profile();
  147. if (is_readable(elfinder_connector_path())) {
  148. global $language;
  149. $path = drupal_get_path('module', 'elfinder');
  150. $editorApp = '';
  151. $langCode = isset($language->language) ? $language->language : 'en';
  152. if (isset($_GET['app'])) {
  153. if (preg_match("/^[a-zA-Z]+$/", $_GET['app'])) {
  154. $editorApp = $_GET['app'];
  155. } elseif (preg_match("/^([a-zA-Z]+)|/", $_GET['app'], $m)) {
  156. $editorApp = $m[1];
  157. }
  158. }
  159. if (isset($_GET['langCode'])) {
  160. if (preg_match("/^[a-zA-z]{2}$/", $_GET['langCode'])) {
  161. $langCode = $_GET['langCode'];
  162. }
  163. }
  164. /* Initial frontend settings */
  165. $elfinder_js_settings = array(
  166. 'connectorUrl' => url('elfinder/connector'),
  167. 'moduleUrl' => url('elfinder'),
  168. 'editorApp' => $editorApp,
  169. 'langCode' => $langCode,
  170. 'rememberLastDir' => variable_get('elfinder_settings_misc_rememberlastdir', 'true') == 'true' ? TRUE : FALSE, // remember last opened directory
  171. 'disabledCommands' => elfinder_get_disabled_commands(),
  172. 'requestType' => variable_get('elfinder_settings_misc_requesttype', 'get') == 'get' ? 'get' : 'post',
  173. 'browserMode' => $browser_mode,
  174. 'token' => drupal_get_token('elFinder'),
  175. 'soundPath' => base_path() . elfinder_lib_path() . '/sounds',
  176. );
  177. if (($mheight = variable_get('elfinder_settings_misc_manager_height', '')) != '') {
  178. $elfinder_js_settings['height'] = (int)$mheight;
  179. }
  180. $elfinder_js_settings['resizable'] = ($browser_mode == 'backend');
  181. if ($editorApp) {
  182. $elfinder_js_settings['editorCallback'] = 'elfinder_' . $editorApp . '_callback';
  183. }
  184. $options = array(
  185. 'group' => CSS_DEFAULT,
  186. 'every_page' => FALSE,
  187. );
  188. $sysjqver = variable_get('jquery_update_jquery_version', '1.10');
  189. if (!(module_exists('jquery_update') && preg_match('/1\.(8|9|10|11|12)/', $sysjqver))) {
  190. drupal_add_js($path . '/js/elfinder.jquery.min.js');
  191. drupal_add_js($path . '/js/elfinder.jquery-ui.min.js');
  192. } else {
  193. drupal_add_library('system', 'ui.button');
  194. drupal_add_library('system', 'ui.dialog');
  195. drupal_add_library('system', 'ui.draggable');
  196. drupal_add_library('system', 'ui.droppable');
  197. drupal_add_library('system', 'ui.resizable');
  198. drupal_add_library('system', 'ui.selectable');
  199. drupal_add_library('system', 'ui.slider');
  200. }
  201. elfinder_add_css($path . '/css/ui-themes/smoothness/jquery-ui.custom.css', $options);
  202. elfinder_add_css($path . '/css/elfinder.common.css', $options);
  203. if (is_readable($libpath . 'css/elfinder.min.css') && is_readable($libpath . 'js/elfinder.min.js')) {
  204. elfinder_add_css($libpath . 'css/elfinder.min.css');
  205. drupal_add_js($libpath . 'js/elfinder.min.js');
  206. } else if (is_readable($libpath . 'css/elfinder.full.css') && is_readable($libpath . 'js/elfinder.full.js')) {
  207. elfinder_add_css($libpath . 'css/elfinder.full.css');
  208. drupal_add_js($libpath . 'js/elfinder.full.js');
  209. } else if (is_readable($libpath . 'css/common.css') && is_readable($libpath . 'js/elFinder.js')) {
  210. elfinder_add_css($libpath . 'css/common.css');
  211. elfinder_add_css($libpath . 'css/dialog.css');
  212. elfinder_add_css($libpath . 'css/toolbar.css');
  213. elfinder_add_css($libpath . 'css/navbar.css');
  214. elfinder_add_css($libpath . 'css/statusbar.css');
  215. elfinder_add_css($libpath . 'css/cwd.css');
  216. elfinder_add_css($libpath . 'css/quicklook.css');
  217. elfinder_add_css($libpath . 'css/commands.css');
  218. elfinder_add_css($libpath . 'css/contextmenu.css');
  219. elfinder_add_css($libpath . 'css/theme.css');
  220. drupal_add_js($libpath . 'js/elFinder.js');
  221. drupal_add_js($libpath . 'js/elFinder.version.js');
  222. drupal_add_js($libpath . 'js/jquery.elfinder.js');
  223. drupal_add_js($libpath . 'js/elFinder.resources.js');
  224. drupal_add_js($libpath . 'js/elFinder.options.js');
  225. drupal_add_js($libpath . 'js/elFinder.history.js');
  226. drupal_add_js($libpath . 'js/elFinder.command.js');
  227. drupal_add_js($libpath . 'js/ui/overlay.js');
  228. drupal_add_js($libpath . 'js/ui/workzone.js');
  229. drupal_add_js($libpath . 'js/ui/navbar.js');
  230. drupal_add_js($libpath . 'js/ui/dialog.js');
  231. drupal_add_js($libpath . 'js/ui/tree.js');
  232. drupal_add_js($libpath . 'js/ui/cwd.js');
  233. drupal_add_js($libpath . 'js/ui/toolbar.js');
  234. drupal_add_js($libpath . 'js/ui/button.js');
  235. drupal_add_js($libpath . 'js/ui/uploadButton.js');
  236. drupal_add_js($libpath . 'js/ui/viewbutton.js');
  237. drupal_add_js($libpath . 'js/ui/searchbutton.js');
  238. drupal_add_js($libpath . 'js/ui/panel.js');
  239. drupal_add_js($libpath . 'js/ui/contextmenu.js');
  240. drupal_add_js($libpath . 'js/ui/path.js');
  241. drupal_add_js($libpath . 'js/ui/stat.js');
  242. drupal_add_js($libpath . 'js/ui/places.js');
  243. drupal_add_js($libpath . 'js/commands/back.js');
  244. drupal_add_js($libpath . 'js/commands/forward.js');
  245. drupal_add_js($libpath . 'js/commands/reload.js');
  246. drupal_add_js($libpath . 'js/commands/up.js');
  247. drupal_add_js($libpath . 'js/commands/home.js');
  248. drupal_add_js($libpath . 'js/commands/copy.js');
  249. drupal_add_js($libpath . 'js/commands/cut.js');
  250. drupal_add_js($libpath . 'js/commands/paste.js');
  251. drupal_add_js($libpath . 'js/commands/open.js');
  252. drupal_add_js($libpath . 'js/commands/rm.js');
  253. drupal_add_js($libpath . 'js/commands/duplicate.js');
  254. drupal_add_js($libpath . 'js/commands/rename.js');
  255. drupal_add_js($libpath . 'js/commands/help.js');
  256. drupal_add_js($libpath . 'js/commands/getfile.js');
  257. drupal_add_js($libpath . 'js/commands/mkdir.js');
  258. drupal_add_js($libpath . 'js/commands/mkfile.js');
  259. drupal_add_js($libpath . 'js/commands/upload.js');
  260. drupal_add_js($libpath . 'js/commands/download.js');
  261. drupal_add_js($libpath . 'js/commands/edit.js');
  262. drupal_add_js($libpath . 'js/commands/quicklook.js');
  263. drupal_add_js($libpath . 'js/commands/quicklook.plugins.js');
  264. drupal_add_js($libpath . 'js/commands/extract.js');
  265. drupal_add_js($libpath . 'js/commands/archive.js');
  266. drupal_add_js($libpath . 'js/commands/search.js');
  267. drupal_add_js($libpath . 'js/commands/view.js');
  268. drupal_add_js($libpath . 'js/commands/resize.js');
  269. drupal_add_js($libpath . 'js/commands/info.js');
  270. drupal_add_js($libpath . 'js/commands/sort.js');
  271. drupal_add_js($libpath . 'js/commands/netmount.js');
  272. drupal_add_js($libpath . 'js/proxy/elFinderSupportVer1.js');
  273. drupal_add_js($libpath . 'js/jquery.dialogelfinder.js');
  274. } else {
  275. watchdog('Failed Requirement', 'elFinder unable to load required JS and CSS files', array(), WATCHDOG_ERROR);
  276. }
  277. $langfile = $libpath . 'js/i18n/elfinder.' . $langCode . '.js';
  278. if (is_readable($libpath . 'css/theme.css')) {
  279. elfinder_add_css($libpath . 'css/theme.css');
  280. }
  281. /* Adding localization */
  282. if (is_readable($langfile)) {
  283. drupal_add_js($langfile);
  284. }
  285. /* Getting updated frontend settings from hook_elfinder_js_settings */
  286. $newjssettings = module_invoke_all('elfinder_js_settings', $elfinder_js_settings);
  287. if (isset($newjssettings['all'])) {
  288. $elfinder_js_settings = array_merge($elfinder_js_settings, (array)$newjssettings['all']);
  289. }
  290. if (isset($newjssettings[$editorApp])) {
  291. $elfinder_js_settings = array_merge($elfinder_js_settings, (array)$newjssettings[$editorApp]);
  292. }
  293. /* Loading callbacks for returning url from file manager to caller */
  294. $editor_plugins = elfinder_system_listing("$editorApp\.callback\.js\$", $path . '/editors' , 'name', 0);
  295. $callback_file = '';
  296. if (isset($editorApp)) {
  297. if (isset($editorApp) && isset($editor_plugins[$editorApp . '.callback'])) {
  298. $callback_file = $editor_plugins[$editorApp . '.callback']->uri;
  299. if (isset($callback_file)) {
  300. drupal_add_js($callback_file);
  301. }
  302. }
  303. if (isset($elfinder_js_settings['browserscripts'])) {
  304. foreach ((array)$elfinder_js_settings['browserscripts'] as $script) {
  305. drupal_add_js($script, array('weight' => 999));
  306. }
  307. unset($elfinder_js_settings['browserscripts']);
  308. }
  309. }
  310. if (property_exists('elFinder', 'ApiVersion')) {
  311. $elfinder_js_settings['api21'] = true;
  312. } else {
  313. drupal_add_js($path . '/js/info.js');
  314. }
  315. // drupal_add_js($path . '/js/mouseover.js');
  316. drupal_add_js($path . '/js/elfinder.callback.js');
  317. drupal_add_js(array('elfinder' => $elfinder_js_settings), 'setting');
  318. $libs = elfinder_get_libraries('tinymce');
  319. if (count($libs) > 0 && !empty($libs['tinymce'])) {
  320. $path = $libs['tinymce'] . '/jscripts/tiny_mce/tiny_mce_popup.js';
  321. $abspath = base_path() . "/$path";
  322. drupal_add_js($path);
  323. }
  324. } else {
  325. drupal_set_message(t('elFinder library was not found. Please download it from <a href="@url">@url</a> and install to @libpath.', array('@url' => elfinder_download_url(), '@libpath' => $libpath)), 'error');
  326. }
  327. if ($browser_mode && $browser_mode == 'backend') {
  328. return theme('elfinder_page_backend');
  329. } else {
  330. print theme('elfinder_page');
  331. }
  332. module_invoke_all('exit');
  333. exit();
  334. }
  335. function elfinder_get_disabled_commands() {
  336. $disabled_commands = array();
  337. $permissions = elfinder_permission();
  338. $commands = elfinder_cmd_permissions();
  339. // Disable not allowed commands
  340. foreach ($permissions as $perm_name => $perm_data) {
  341. if (array_key_exists($perm_name, $commands) && !user_access($perm_name)) {
  342. array_push($disabled_commands, $commands[$perm_name]);
  343. }
  344. }
  345. /* if (!user_access('download own uploaded files') && !user_access('download all uploaded files')) {
  346. $disabled_commands[] = 'getfile';
  347. }*/
  348. return $disabled_commands;
  349. }
  350. function elfinder_user_roles() {
  351. global $user;
  352. return array_reverse(array_keys($user->roles));
  353. }
  354. function elfinder_get_user_profile() {
  355. /* Get role id list sorted by weight descending */
  356. $profiles = elfinder_admin_profile_get();
  357. if (!is_array($profiles)) {
  358. $profiles = array($profiles);
  359. }
  360. $first_profile = NULL;
  361. $rids = elfinder_user_roles();
  362. $rids[] = 1; // anonymous user
  363. /* Get first matching profile for higest weight user role */
  364. foreach ($rids as $rid) {
  365. foreach ($profiles as $profile) {
  366. if (isset($profile->settings['profile_role']) && in_array($rid, $profile->settings['profile_role']) ) {
  367. if (is_null($first_profile)) {
  368. $first_profile = $profile;
  369. } else if (isset($profile->settings['volume'])) {
  370. /* Adding all available to current user volumes from other profiles */
  371. foreach ($profile->settings['volume'] as $volume) {
  372. $first_profile->settings['volume'][] = $volume;
  373. }
  374. }
  375. }
  376. }
  377. }
  378. return $first_profile;
  379. }
  380. /**
  381. * File browser to filesystem php connector callback
  382. */
  383. function elfinder_filesystemconnector($mode = 'default') {
  384. global $base_root, $user;
  385. $p = elfinder_get_user_profile();
  386. $path = drupal_get_path('module', 'elfinder');
  387. include_once dirname(elfinder_connector_path()) . DIRECTORY_SEPARATOR . 'elFinderConnector.class.php';
  388. include_once dirname(elfinder_connector_path()) . DIRECTORY_SEPARATOR . 'elFinderVolumeDriver.class.php';
  389. include_once dirname(elfinder_connector_path()) . DIRECTORY_SEPARATOR . 'elFinderVolumeLocalFileSystem.class.php';
  390. include_once $path . DIRECTORY_SEPARATOR . 'inc' . DIRECTORY_SEPARATOR . 'elfinder.drupalfs.driver.inc';
  391. include_once $path . DIRECTORY_SEPARATOR . 'inc' . DIRECTORY_SEPARATOR . 'elfinder.drupalfs.acl.inc';
  392. include_once $path . DIRECTORY_SEPARATOR . 'inc' . DIRECTORY_SEPARATOR . 'elfinder.admin.profiles.inc';
  393. /**
  394. * Emulate json_encode required by filesystem connector for old php without JSON support
  395. */
  396. if (!function_exists('json_encode')) {
  397. function json_encode($arg) {
  398. return drupal_json($arg);
  399. }
  400. }
  401. /* Disabling incompartible devel module features */
  402. if (module_exists('devel')) {
  403. $devel_incompat = array('dev_timer', 'devel_query_display', 'dev_mem', 'devel_redirect_page');
  404. $flag = FALSE;
  405. foreach ($devel_incompat as $di) {
  406. if (variable_get($di, 0)) {
  407. $flag = TRUE;
  408. variable_set($di, 0);
  409. }
  410. }
  411. if ($flag) {
  412. drupal_set_message(t('elFinder disabled incompatible devel module features.'), 'warning');
  413. }
  414. }
  415. // Set absolute/relative url variable.
  416. if(!isset($mode) && variable_get('elfinder_settings_filesystem_fileurl', 'true') == 'false') {
  417. $mode = 'relative';
  418. }
  419. $disabled_commands = elfinder_get_disabled_commands();
  420. $acl = new elFinderDrupalACL();
  421. $pubFiles = file_stream_wrapper_get_instance_by_uri('public://');
  422. $pvtFiles = file_stream_wrapper_get_instance_by_uri('private://');
  423. $roots = array();
  424. if ($p) {
  425. $profile = $p->settings;
  426. } else {
  427. $profile = array();
  428. }
  429. $options_defs = array(
  430. 'disabled' => $disabled_commands, // list of not allowed commands
  431. 'debug' => FALSE,
  432. 'dirSize' => FALSE,
  433. 'tmbSize' => isset($profile['tmbsize']) ? $profile['tmbsize'] : variable_get('elfinder_settings_thumbnail_size', '48'), // thumbnail image size
  434. 'tmbPath' => variable_get('elfinder_settings_thumbnail_dirname', 'tmb'), // thumbnail images directory (tmbPath in 2.x)
  435. 'tmbCrop' => variable_get('elfinder_settings_thumbnail_tmbcrop', 'true') == 'true' ? TRUE : FALSE, // crop thumbnail image
  436. 'dateFormat' => variable_get('date_format_long', 'j M Y H:i'), // file modification date format
  437. 'mimeDetect' => isset($profile['mime_detect']) ? $profile['mime_detect'] : variable_get('elfinder_settings_filesystem_mimedetect', 'auto'), // file type detection method
  438. 'imgLib' => isset($profile['imglib']) ? $profile['imglib'] : variable_get('elfinder_settings_thumbnail_imglib', 'auto'), // image manipulation library
  439. 'fileMode' => isset($profile['file_perm']) ? octdec($profile['file_perm']) : octdec(variable_get('elfinder_settings_filesystem_fileperm', '0666')), // created file permissions
  440. 'dirMode' => isset($profile['dir_perm']) ? octdec($profile['dir_perm']) : octdec(variable_get('elfinder_settings_filesystem_dirperm', '0777')), // created directory permissions
  441. 'accessControlData' => array('uid' => $user->uid),
  442. 'acceptedName' => '/^[^\.]+/',
  443. 'uploadMaxSize' => parse_size(variable_get('elfinder_settings_filesystem_maxfilesize', '')),
  444. 'userProfile' => $p,
  445. 'jpgQuality' => variable_get('image_jpeg_quality', 75),
  446. );
  447. if (is_object($pubFiles) && user_access('access public files')) {
  448. $roots[] = array_merge($options_defs, array(
  449. 'driver' => 'Drupal',
  450. 'path' => drupal_realpath('public://'), // path to root directory (named 'path' in elFinder 2.0)
  451. 'URL' => $mode == 'relative' ? base_path() . $pubFiles->getDirectoryPath() : $pubFiles->getExternalUrl(), // root directory URL
  452. 'alias' => variable_get('elfinder_settings_filesystem_public_root_label', '') != '' ? variable_get('elfinder_settings_filesystem_public_root_label', '') : t('Public Files'), // display this instead of root directory name (named 'alias' in elFinder 2.0)
  453. 'accessControl' => array($acl, 'fsAccessPublic'),
  454. ));
  455. }
  456. if (is_object($pvtFiles) && user_access('access private files')) {
  457. $roots[] = array_merge($options_defs, array(
  458. 'driver' => 'Drupal',
  459. 'path' => drupal_realpath('private://'), // path to root directory (named 'path' in elFinder 2.0)
  460. 'URL' => $mode == 'relative' ? base_path() . $pvtFiles->getDirectoryPath() : $pvtFiles->getExternalUrl(), // root directory URL
  461. 'alias' => variable_get('elfinder_settings_filesystem_private_root_label', '') != '' ? variable_get('elfinder_settings_filesystem_private_root_label', '') : t('Private Files'), // display this instead of root directory name (named 'alias' in elFinder 2.0)
  462. 'accessControl' => array($acl, 'fsAccessPrivate'),
  463. ));
  464. }
  465. if (user_access('access unmanaged files')) {
  466. $roots[] = array_merge($options_defs, array(
  467. 'driver' => 'LocalFileSystem',
  468. 'path' => elfinder_file_directory_path(TRUE), // path to root directory (named 'path' in elFinder 2.0)
  469. 'URL' => elfinder_file_directory_url($mode == 'relative' ? TRUE : FALSE), // root directory URL
  470. 'alias' => variable_get('elfinder_settings_filesystem_unmanaged_root_label', '') != '' ? variable_get('elfinder_settings_filesystem_unmanaged_root_label', '') : t('Unmanaged Files'), // display this instead of root directory name (named 'alias' in elFinder 2.0)
  471. 'accessControl' => array($acl, 'fsAccessUnmanaged'),
  472. ));
  473. }
  474. if (isset($profile['volume'])) {
  475. foreach ($profile['volume'] as $volume) {
  476. $root = array(
  477. 'alias' => $volume['label'],
  478. );
  479. $is_subdir = FALSE;
  480. $rootpath = '';
  481. if (isset($volume['path']) && substr($volume['path'], 0, 1) != DIRECTORY_SEPARATOR) {
  482. $is_subdir = TRUE;
  483. }
  484. if ($is_subdir) {
  485. $root['driver'] = 'Drupal';
  486. $scheme = file_uri_scheme($volume['path']);
  487. if ($scheme == FALSE) {
  488. if (is_object(file_stream_wrapper_get_instance_by_uri('private://'))) {
  489. $scheme = 'private';
  490. } else {
  491. $scheme = 'public';
  492. }
  493. }
  494. $rootpath = $volume['path'];
  495. if ($pos = strpos($rootpath, '://')) {
  496. $rootpath = substr($rootpath, $pos + 3);
  497. }
  498. $streamWrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
  499. if (is_object($streamWrapper)) {
  500. $volpath = $streamWrapper->realpath();
  501. $volurl = $mode == 'relative' ? base_path() . $streamWrapper->getDirectoryPath() : $streamWrapper->getExternalUrl();
  502. $url = isset($volume['url']) && $volume['url'] != '' ? elfinder_parse_path_tokens($volume['url']) : $volurl;
  503. }
  504. $rootpath = elfinder_parse_path_tokens($rootpath);
  505. $trimmedpath = ltrim($rootpath, './');
  506. $rootpath = $volpath . DIRECTORY_SEPARATOR . $trimmedpath;
  507. $url = rtrim($url, '/');
  508. $url .= "/$trimmedpath";
  509. if (!file_prepare_directory($rootpath, FILE_CREATE_DIRECTORY)) {
  510. drupal_set_message(t('Error. Cannot initialize directory %dir', array('%dir' => $rootpath)), 'error');
  511. }
  512. $root['path'] = drupal_realpath($rootpath);
  513. $root['URL'] = $url;
  514. $root['tmbPath'] = $volpath . DIRECTORY_SEPARATOR . variable_get('elfinder_settings_thumbnail_dirname', 'tmb');
  515. $root['tmbURL'] = $volurl . '/' . variable_get('elfinder_settings_thumbnail_dirname', 'tmb');
  516. } else {
  517. $rootpath = elfinder_parse_path_tokens($volume['path']);
  518. $root['driver'] = 'LocalFileSystem';
  519. $root['path'] = $rootpath;
  520. if (isset($volume['url'])) {
  521. $root['URL'] = elfinder_parse_path_tokens($volume['url']);
  522. }
  523. }
  524. $root = array_merge($options_defs, $root);
  525. $roots[] = $root;
  526. }
  527. }
  528. $opts = array(
  529. 'roots' => $roots,
  530. 'driver_defaults' => $options_defs,
  531. );
  532. $newopts = module_invoke_all('elfinder_connector_config', $opts);
  533. if ($newopts) {
  534. $opts = $newopts;
  535. }
  536. class elFinderConnectorDrupal extends elFinderConnector {
  537. public function test() {
  538. }
  539. }
  540. try {
  541. $elFinderObj = new elFinderDrupal($opts);
  542. } catch (Exception $e) {
  543. module_invoke_all('exit');
  544. exit(drupal_json_encode(array('error' => array(t('Unable to initialize elFinder object. :msg', array(':msg' => basename($e->getFile()) .": ". $e->getMessage()))))));
  545. }
  546. $bindcmds = module_invoke_all('elfinder_bind');
  547. foreach ($bindcmds as $cmd => $cmdfunc) {
  548. $elFinderObj->bind($cmd, $cmdfunc);
  549. }
  550. try {
  551. $elFinderConnectorObj = new elFinderConnectorDrupal($elFinderObj);
  552. } catch (Exception $e) {
  553. module_invoke_all('exit');
  554. exit(drupal_json_encode(array('error' => array(t('Unable to initialize elFinder connector object. :msg', array(':msg' => basename($e->getFile()) .": ". $e->getMessage()))))));
  555. }
  556. module_invoke_all('elfinder_connector_init', $elFinderConnectorObj);
  557. if (!drupal_valid_token($_REQUEST['token'], 'elFinder', TRUE)) {
  558. module_invoke_all('exit');
  559. exit(drupal_json_encode(array('error' => array(t('Access denied')))));
  560. } else {
  561. try {
  562. $elFinderConnectorObj->run();
  563. } catch (Exception $e) {
  564. module_invoke_all('exit');
  565. exit(drupal_json_encode(array('error' => array(t('Unable to run elFinder connector. :msg', array(':msg' => basename($e->getFile()) .": ". $e->getMessage()))))));
  566. }
  567. module_invoke_all('exit');
  568. exit();
  569. }
  570. }
  571. function elfinder_elfinder_bind() {
  572. return array('test' => 'test2');
  573. }
  574. /**
  575. * Implements hook_theme().
  576. */
  577. function elfinder_theme() {
  578. $theme = array();
  579. $theme['elfinder_page'] = array(
  580. 'template' => 'elfinder-page',
  581. 'arguments' => array('libPath' => '/' . elfinder_lib_path()),
  582. 'path' => drupal_get_path('module', 'elfinder') . '/tpl',
  583. );
  584. $theme['elfinder_page_backend'] = array(
  585. 'template' => 'elfinder-page-backend',
  586. 'arguments' => array('libPath' => '/' . elfinder_lib_path()),
  587. 'path' => drupal_get_path('module', 'elfinder') . '/tpl',
  588. );
  589. return $theme;
  590. }
  591. /**
  592. * Implements hook_permission(), D7
  593. */
  594. function elfinder_permission() {
  595. $perm = array(
  596. 'use file manager' => array(
  597. 'title' => t('Use elFinder file manager'),
  598. 'description' => t('Allow accessing elFinder file manager module'),
  599. ),
  600. 'administer file manager' => array(
  601. 'title' => t('Administer file manager'),
  602. 'description' => t('Allow users to administer file manager'),
  603. ),
  604. 'create new directories' => array(
  605. 'title' => t('Create new directories'),
  606. 'description' => t('Allow users to create new directories'),
  607. ),
  608. 'create new files' => array(
  609. 'title' => t('Create new files'),
  610. 'description' => t('Allow users to create new files'),
  611. ),
  612. 'rename files and directories' => array(
  613. 'title' => t('Rename files and directories'),
  614. 'description' => t('Allow users to rename files and directories'),
  615. ),
  616. 'file uploads' => array(
  617. 'title' => t('File uploads'),
  618. 'description' => t('Allow users to upload files'),
  619. ),
  620. 'paste from clipboard' => array(
  621. 'title' => t('Paste from clipboard'),
  622. 'description' => t('Allow users to paste files from clipboard'),
  623. ),
  624. 'copy to clipboard' => array(
  625. 'title' => t('Copy to clipboard'),
  626. 'description' => t('Allow users to copy files to clipboard'),
  627. ),
  628. 'cut to clipboard' => array(
  629. 'title' => t('Cut to clipboard'),
  630. 'description' => t('Allow users to cut files to clipboard'),
  631. ),
  632. 'view file info' => array(
  633. 'title' => t('View file info'),
  634. 'description' => t('Allow users to view file info'),
  635. ),
  636. 'preview files' => array(
  637. 'title' => t('Preview files'),
  638. 'description' => t('Allow users to preview files'),
  639. ),
  640. 'delete files and directories' => array(
  641. 'title' => t('Delete files and directories'),
  642. 'description' => t('Allow users to delete files and directories'),
  643. ),
  644. 'duplicate files' => array(
  645. 'title' => t('Duplicate files'),
  646. 'description' => t('Allow users to duplicate files'),
  647. ),
  648. 'edit files' => array(
  649. 'title' => t('Edit files'),
  650. 'description' => t('Allow users to edit files'),
  651. ),
  652. 'add files to archive' => array(
  653. 'title' => t('Add files to archive'),
  654. 'description' => t('Allow users add files to archive'),
  655. ),
  656. 'extract files from archive' => array(
  657. 'title' => t('Extract files from archive'),
  658. 'description' => t('Allow users to extract files from archive'),
  659. ),
  660. 'resize images' => array(
  661. 'title' => t('Resize images'),
  662. 'description' => t('Allow users to resize images'),
  663. ),
  664. 'download own uploaded files' => array(
  665. 'title' => t('Download own uploaded files'),
  666. 'description' => t('Allow users to download own uploaded files'),
  667. ),
  668. 'download all uploaded files' => array(
  669. 'title' => t('Download all uploaded files'),
  670. 'description' => t('Allow users to download all uploaded files'),
  671. ),
  672. 'access public files' => array(
  673. 'title' => t('Access public files'),
  674. 'description' => t('Allow users to access public files directory'),
  675. ),
  676. 'access private files' => array(
  677. 'title' => t('Access private files'),
  678. 'description' => t('Allow users to access private files directory'),
  679. ),
  680. 'access unmanaged files' => array(
  681. 'title' => t('Access unmanaged files'),
  682. 'description' => t('Allow users to access unmanaged files in custom directory'),
  683. ),
  684. 'write public files' => array(
  685. 'title' => t('Write public files'),
  686. 'description' => t('Allow users write access to public files directory'),
  687. ),
  688. 'write private files' => array(
  689. 'title' => t('Write private files'),
  690. 'description' => t('Allow users write access to private files directory'),
  691. ),
  692. 'access unmanaged files' => array(
  693. 'title' => t('Access unmanaged files'),
  694. 'description' => t('Allow users write access to unmanaged files in custom directory'),
  695. ),
  696. 'view file description' => array(
  697. 'title' => t('View file descriptions'),
  698. 'description' => t('Allow users to view file descriptions'),
  699. ),
  700. 'edit file description' => array(
  701. 'title' => t('Edit file descriptions'),
  702. 'description' => t('Allow users to edit file descriptions'),
  703. ),
  704. 'view file owner' => array(
  705. 'title' => t('View file owner'),
  706. 'description' => t('Allow users to view file owner'),
  707. ),
  708. 'view file downloads' => array(
  709. 'title' => t('View file downloads'),
  710. 'description' => t('Allow users to view file downloads'),
  711. ),
  712. 'mount network volumes' => array(
  713. 'title' => t('Mount network volumes'),
  714. 'description' => t('Allow users to mount remote network volumes'),
  715. ),
  716. );
  717. $newperms = module_invoke_all('elfinder_perms', $perm);
  718. return ($newperms) ? $newperms : $perm;
  719. }
  720. /**
  721. * elFider commandsto permissions assignment
  722. */
  723. function elfinder_cmd_permissions() {
  724. $perm = array(
  725. 'create new directories' => 'mkdir',
  726. 'create new files' => 'mkfile',
  727. 'rename files and directories' => 'rename',
  728. 'file uploads' => 'upload',
  729. 'copy to clipboard' => 'copy',
  730. 'cut to clipboard' => 'cut',
  731. 'paste from clipboard' => 'paste',
  732. 'preview files' => 'quicklook',
  733. 'view file info' => 'info',
  734. 'delete files and directories' => 'rm',
  735. 'duplicate files' => 'duplicate',
  736. 'edit files' => 'edit',
  737. 'add files to archive' => 'archive',
  738. 'extract files from archive' => 'extract',
  739. 'resize images' => 'resize',
  740. 'download own uploaded files' => 'download',
  741. 'view file description' => 'desc',
  742. 'edit file description' => 'editdesc',
  743. 'view file owner' => 'owner',
  744. 'view file downloads' => 'downloadcount',
  745. 'mount network volumes' => 'netmount',
  746. );
  747. $newcperms = module_invoke_all('elfinder_cmd_perms', $perm);
  748. return ($newcperms) ? $newcperms : $perm;
  749. }
  750. /**
  751. * Implements hook_wysiwyg_plugin().
  752. */
  753. function elfinder_wysiwyg_plugin($editor, $version) {
  754. $path = drupal_get_path('module', 'elfinder');
  755. drupal_add_js($path . '/js/elfinder.wysiwyg.init.js', array('group' => JS_LIBRARY));
  756. $options = array(
  757. 'elfinder_url' => url('elfinder', array('query' => array('app' => $editor ))),
  758. 'homepage_url' => elfinder_homepage_url(),
  759. 'path' => $path,
  760. );
  761. /* bueditor hack */
  762. if (preg_match('/^(bueditor|bue)/', $editor)) {
  763. $editor = 'bueditor';
  764. }
  765. $editor_plugins = elfinder_system_listing("$editor\.inc\$", $path . '/editors' , 'name', 0);
  766. /* loading editor plugin */
  767. $plugin_file = '';
  768. if (isset($editor_plugins[$editor])) {
  769. $plugin_file = $editor_plugins[$editor]->uri;
  770. if ($plugin_file) {
  771. $options['plugin_url_base'] = "$path/editors/$editor";
  772. require_once $plugin_file;
  773. }
  774. }
  775. /* hook for adding another editor support */
  776. //module_invoke_all('elfinder_editor_plugin');
  777. /* wysiwyg plugin generation func for selected editor */
  778. $pluginfunc = 'elfinder_' . $editor . '_elfinder_editor_plugin';
  779. /* returning elFinder wysiwyg editor plugin for selected editor */
  780. if (function_exists($pluginfunc)) {
  781. return call_user_func_array($pluginfunc, array($options));
  782. }
  783. }
  784. /**
  785. * library homepage url
  786. */
  787. function elfinder_homepage_url() {
  788. return 'http://elfinder.org/';
  789. }
  790. /**
  791. * library download url
  792. */
  793. function elfinder_download_url() {
  794. return 'http://sourceforge.net/projects/drupal-elfinder/files/Library/';
  795. }
  796. /**
  797. * library files path
  798. */
  799. function elfinder_lib_path($name = 'elfinder') {
  800. $libraries = &drupal_static(__FUNCTION__);
  801. if (!isset($libraries)) {
  802. $libraries = elfinder_get_libraries();
  803. }
  804. $path = '';
  805. if (!isset($libraries[$name])) {
  806. return FALSE;
  807. } else {
  808. $path .= $libraries[$name];
  809. }
  810. return $path;
  811. }
  812. function elfinder_get_libraries() {
  813. $directory = 'libraries';
  814. $searchdir = array();
  815. $profile = drupal_get_profile();
  816. $config = conf_path();
  817. // Similar to 'modules' and 'themes' directories in the root directory,
  818. // certain distributions may want to place libraries into a 'libraries'
  819. // directory in Drupal's root directory.
  820. $searchdir[] = $directory;
  821. // The 'profiles' directory contains pristine collections of modules and
  822. // themes as organized by a distribution. It is pristine in the same way
  823. // that /modules is pristine for core; users should avoid changing anything
  824. // there in favor of sites/all or sites/<domain> directories.
  825. if (file_exists("profiles/$profile/$directory")) {
  826. $searchdir[] = "profiles/$profile/$directory";
  827. }
  828. // Always search sites/all/*.
  829. $searchdir[] = 'sites/all/' . $directory;
  830. // Also search sites/<domain>/*.
  831. if (file_exists("$config/$directory")) {
  832. $searchdir[] = "$config/$directory";
  833. }
  834. // Retrieve list of directories.
  835. // @todo Core: Allow to scan for directories.
  836. $directories = array();
  837. $nomask = array('CVS');
  838. foreach ($searchdir as $dir) {
  839. if (is_dir($dir) && $handle = opendir($dir)) {
  840. while (FALSE !== ($file = readdir($handle))) {
  841. if (!in_array($file, $nomask) && $file[0] != '.') {
  842. if (is_dir("$dir/$file")) {
  843. $directories[$file] = "$dir/$file";
  844. }
  845. }
  846. }
  847. closedir($handle);
  848. }
  849. }
  850. return $directories;
  851. }
  852. /**
  853. * library to filesystem connector path
  854. */
  855. function elfinder_connector_path() {
  856. return elfinder_lib_path() . '/php/elFinder.class.php';
  857. }
  858. /**
  859. * path to files directory
  860. */
  861. function elfinder_file_directory_path($absolute = FALSE) {
  862. $path = variable_get('elfinder_settings_filesystem_root_custom', '');
  863. $path = elfinder_parse_path_tokens($path);
  864. return $path;
  865. }
  866. /**
  867. * Returning site document root
  868. */
  869. function elfinder_document_root() {
  870. return realpath('');
  871. }
  872. /**
  873. * replace path tokens to its values (%uid, %user, etc)
  874. */
  875. function elfinder_parse_path_tokens($unparsed_path) {
  876. global $user, $language;
  877. $path = $unparsed_path;
  878. $uname = isset($user->name) ? preg_replace('/[[:^alnum:]]+/', '', $user->name) : '';
  879. $langCode = isset($language->language) ? $language->language : 'en';
  880. $path_tokens = array(
  881. '%uid' => $user->uid,
  882. '%name' => $uname,
  883. '%user' => $uname,
  884. '%files' => elfinder_default_directory_path(),
  885. '%lang' => $langCode,
  886. );
  887. $path = strtr($path, $path_tokens);
  888. if (function_exists('token_replace')) {
  889. $node = null;
  890. if (module_exists('og_context') AND isset($_SESSION['og_context']['gid'])) {
  891. $gid = $_SESSION['og_context']['gid'];
  892. $node = node_load($gid);
  893. }
  894. $path = token_replace($path, array('node' => $node, 'user' => $user));
  895. }
  896. return $path;
  897. }
  898. /**
  899. * default path to files directory
  900. */
  901. function elfinder_default_directory_path() {
  902. $filepath = '';
  903. $scheme = file_default_scheme();
  904. if ($scheme == 'public') {
  905. $filepath = variable_get('file_public_path', conf_path() . '/files');
  906. } else {
  907. $filepath = variable_get('file_private_path', conf_path());
  908. }
  909. return $filepath;
  910. }
  911. /**
  912. * files directory url
  913. */
  914. function elfinder_file_directory_url($force_relative = FALSE) {
  915. global $base_root;
  916. $fileurl = '';
  917. $fileurl = variable_get('elfinder_settings_filesystem_url_custom', '') . '/';
  918. if ($fileurl) {
  919. $fileurl = elfinder_parse_path_tokens($fileurl);
  920. }
  921. // Cutting off absolute url prefix
  922. if ($force_relative) {
  923. $fileurl = str_replace($base_root, '', $fileurl);
  924. }
  925. return $fileurl;
  926. }
  927. /**
  928. * prepare directory for files
  929. */
  930. function elfinder_prepare_directory($directory) {
  931. $rc = file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
  932. return $rc;
  933. }
  934. /**
  935. * Add css
  936. */
  937. function elfinder_add_css($path, $options = array()) {
  938. return drupal_add_css($path, $options);
  939. }
  940. /**
  941. * Removing unwanted css
  942. */
  943. function elfinder_browser_css() {
  944. $css = drupal_add_css();
  945. $rms = array();
  946. if (module_exists('admin_menu')) {
  947. $apath = drupal_get_path('module', 'admin_menu');
  948. $atpath = drupal_get_path('module', 'admin_menu_toolbar');
  949. $rms[] = isset($apath) ? $apath : '';
  950. $rms[] = isset($atpath) ? $atpath : '';
  951. foreach (array_keys($css) as $k) {
  952. foreach ($rms as $rm) {
  953. if (preg_match('|' . $rm . '|', $k) && $rm != '') {
  954. unset($css[$k]);
  955. }
  956. }
  957. }
  958. }
  959. return drupal_get_css($css);
  960. }
  961. /**
  962. * Removing unwanted js
  963. */
  964. function elfinder_browser_js() {
  965. $js = drupal_add_js(NULL, array('scope' => 'header'));
  966. if (module_exists('admin_menu')) {
  967. $apath = drupal_get_path('module', 'admin_menu');
  968. $atpath = drupal_get_path('module', 'admin_menu_toolbar');
  969. $rms[] = isset($apath) ? $apath : '';
  970. $rms[] = isset($atpath) ? $atpath : '';
  971. foreach (array_keys($js) as $k) {
  972. foreach ($rms as $rm) {
  973. if (preg_match('|' . $rm . '|', $k) && $rm != '') {
  974. unset($js[$k]);
  975. }
  976. }
  977. }
  978. }
  979. return drupal_get_js('header', $js);
  980. }
  981. function elfinder_system_listing($matchexpr, $path, $arg1, $arg2) {
  982. return drupal_system_listing("/$matchexpr/", $path, $arg1, $arg2);
  983. }
  984. function elfinder_get_drupal_file_obj($uri) {
  985. global $user;
  986. $file = NULL;
  987. $fid = db_select('file_managed', 'f')
  988. ->condition('uri', rawurldecode($uri))
  989. ->fields('f', array('fid'))
  990. ->execute()
  991. ->fetchField();
  992. if ($fid) {
  993. $file = file_load($fid);
  994. }
  995. else {
  996. $file = new StdClass;
  997. $file->uid = $user->uid;
  998. $file->filename = basename($uri);
  999. $file->uri = $uri;
  1000. $file->filemime = file_get_mimetype($uri);
  1001. if (is_file(drupal_realpath($uri))) {
  1002. $file->filesize = filesize($uri);
  1003. // Use the file_entity module's mimetype to entity type
  1004. // functionality if we have that method available.
  1005. if (function_exists('file_entity_file_type')) {
  1006. $types = file_entity_file_type($file);
  1007. if (count($types) > 0) {
  1008. $file->type = $types[0];
  1009. } else {
  1010. $file->type = 'default';
  1011. }
  1012. } else {
  1013. list($type) = explode("/", $file->filemime);
  1014. switch($type) {
  1015. case 'image':
  1016. case 'video':
  1017. $file->type = $type;
  1018. break;
  1019. default:
  1020. $file->type = 'default';
  1021. break;
  1022. }
  1023. }
  1024. }
  1025. $file->timestamp = REQUEST_TIME;
  1026. $file->status = FILE_STATUS_PERMANENT;
  1027. }
  1028. return $file;
  1029. }
  1030. function elfinder_file_download($uri) {
  1031. if (variable_get('elfinder_settings_filesystem_handleprivate', 'true') == 'true') {
  1032. global $user;
  1033. $accessown = 'download own uploaded files';
  1034. $accessall = 'download all uploaded files';
  1035. $info = elfinder_get_drupal_file_obj($uri);
  1036. if (($info->uid == $user->uid && user_access($accessown)) || user_access($accessall)) {
  1037. $disposition = preg_match("/^(text|image)\//", $info->filemime) ? 'inline' : 'attachment';
  1038. return array(
  1039. 'Content-Type' => $info->filemime,
  1040. 'Content-Length' => $info->filesize,
  1041. 'Content-Disposition' => elFinderDrupal::GetContentDisposition($info->filename, $info->filemime, FALSE),
  1042. );
  1043. }
  1044. return NULL;
  1045. }
  1046. }
  1047. function elfinder_admin_profile_get($name = '') {
  1048. $q = db_select('elfinder_profile', 'p')->fields('p', array('pid', 'name', 'description', 'settings'));
  1049. if ($name) {
  1050. $q->condition('p.name', $name);
  1051. }
  1052. $result = $q->execute();
  1053. $profile = array();
  1054. foreach ($result as $record) {
  1055. $row = $record;
  1056. $row->settings = unserialize($row->settings);
  1057. $profile[] = $row;
  1058. }
  1059. if (count($profile) == 1) {
  1060. $profile = $profile[0];
  1061. }
  1062. return $profile;
  1063. }
  1064. function elfinder_block_info() {
  1065. // Makes it show up on /admin/build/block page.
  1066. $blocks = array();
  1067. $blocks[0]['info'] = t('File Manager');
  1068. return $blocks;
  1069. }
  1070. function elfinder_block_view() {
  1071. // Show the block.
  1072. $block = array();
  1073. $block['subject'] = t('File Manager');
  1074. $block['content'] = elfinder_file_browser('backend');
  1075. return $block;
  1076. }
  1077. /**
  1078. * Implements hook_ckeditor_settings_alter().
  1079. */
  1080. function elfinder_ckeditor_settings_alter(&$settings, $conf) {
  1081. $width = variable_get('elfinder_settings_misc_manager_width', '');
  1082. if(!$width) {
  1083. $width = ELFINDER_POPUP_WIDTH;
  1084. }
  1085. $height = variable_get('elfinder_settings_misc_manager_height', '');
  1086. if(!$height) {
  1087. $height = ELFINDER_POPUP_HEIGHT;
  1088. }
  1089. $settings['js_conf']['filebrowserWindowWidth'] = $width;
  1090. $settings['js_conf']['filebrowserWindowHeight'] = $height;
  1091. }