elfinder.module 47 KB

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