elfinder.admin.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. /**
  3. * elFinder Integration
  4. *
  5. * Copyright (c) 2010-2018, Alexey Sukhotin. All rights reserved.
  6. */
  7. /**
  8. * @file
  9. * file manager admin settings page
  10. */
  11. /**
  12. * Settings form callback
  13. */
  14. function elfinder_admin() {
  15. $obj = drupal_get_form('elfinder_d7_admin_form');
  16. return drupal_render($obj);
  17. }
  18. /**
  19. * Settings form definition for D7
  20. */
  21. function elfinder_d7_admin_form($form_id, &$form_state) {
  22. return elfinder_admin_form($form_state);
  23. }
  24. function elfinder_admin_profile_links($profile_name) {
  25. $links = l(t('Edit'), 'admin/config/media/elfinder/profile/' . $profile_name . '/edit') . ' ' . l(t('Delete'), 'admin/config/media/elfinder/profile/' . $profile_name . '/delete');
  26. return $links;
  27. }
  28. /**
  29. * Settings form definition
  30. */
  31. function elfinder_admin_form(&$form_state) {
  32. global $user, $language;
  33. $path = drupal_get_path('module', 'elfinder');
  34. // require_once $path .'/inc/' . 'elfinder.admin.profiles.inc';
  35. $langCode = isset($language->language) ? $language->language : 'en';
  36. $form['profiles'] = array(
  37. '#type' => 'fieldset',
  38. '#title' => t('Profiles'),
  39. '#collapsible' => TRUE,
  40. '#collapsed' => FALSE,
  41. );
  42. $profiles = elfinder_admin_profile_get();
  43. $roles = user_roles();
  44. $rows = array();
  45. if ($profiles) {
  46. if (is_array($profiles)) {
  47. foreach ($profiles as $profile) {
  48. $rows[] = array($profile->pid, $profile->name, $profile->description, implode(', ', array_intersect_key($roles, $profile->settings['profile_role'])), elfinder_admin_profile_links($profile->name));
  49. }
  50. } else {
  51. $profile = $profiles;
  52. $rows[] = array($profile->pid, $profile->name, $profile->description, implode(', ', array_intersect_key($roles, $profile->settings['profile_role'])), elfinder_admin_profile_links($profile->name));
  53. }
  54. }
  55. $profile_table = theme('table', array(
  56. 'header' => array(t('Id'), t('Name'), t('Description'), t('Role'), ''),
  57. 'rows' => $rows
  58. ));
  59. $addprofile = l(t('Add profile'), 'admin/config/media/elfinder/profile');
  60. $form['profiles']['profile_list'] = array(
  61. '#type' => 'markup',
  62. '#markup' => "$profile_table<br/>$addprofile",
  63. );
  64. $form['filesystem_settings'] = array(
  65. '#type' => 'fieldset',
  66. '#title' => t('File system settings'),
  67. '#collapsible' => TRUE,
  68. '#collapsed' => FALSE,
  69. );
  70. $form['filesystem_settings']['filesystem_public_root_label'] = array(
  71. '#prefix' => '<div class="custom-container">',
  72. '#suffix' => '</div>',
  73. '#type' => 'textfield',
  74. '#title' => t('Public files root directory label'),
  75. '#default_value' => variable_get('elfinder_settings_filesystem_public_root_label', ''),
  76. '#description' => t('Root directory label in directory tree'),
  77. );
  78. $form['filesystem_settings']['filesystem_private_root_label'] = array(
  79. '#prefix' => '<div class="custom-container">',
  80. '#suffix' => '</div>',
  81. '#type' => 'textfield',
  82. '#title' => t('Private files root directory label'),
  83. '#default_value' => variable_get('elfinder_settings_filesystem_private_root_label', ''),
  84. '#description' => t('Root directory label in directory tree'),
  85. );
  86. $form['filesystem_settings']['filesystem_unmanaged_root_label'] = array(
  87. '#prefix' => '<div class="custom-container">',
  88. '#suffix' => '</div>',
  89. '#type' => 'textfield',
  90. '#title' => t('Unmanaged files root directory label'),
  91. '#default_value' => variable_get('elfinder_settings_filesystem_unmanaged_root_label', ''),
  92. '#description' => t('Root directory label in directory tree'),
  93. );
  94. $form['filesystem_settings']['filesystem_root_custom'] = array(
  95. '#prefix' => '<div class="custom-container">',
  96. '#suffix' => '</div>',
  97. '#type' => 'textfield',
  98. '#title' => t('Custom unmanaged files path'),
  99. '#default_value' => variable_get('elfinder_settings_filesystem_root_custom', ''),
  100. '#description' => t('Custom filesystem root path.') . '<br/>' . t('Available tokens: <code>%files</code> (base path, eg: <code>/</code>), <code>%name</code> (current username, eg: <code>@u</code>, <b>NOTE:</b> it is not unique - users can have same username, so better to combine it with user id value), <code>%uid</code> (current user id, eg: <code>@uid</code>), <code>%lang</code> (current language code, eg: <code>@lang</code>), plus all tokens provided by token module', array('@u' => $user->name, '@uid' => $user->uid, '@lang' => $langCode)),
  101. );
  102. $form['filesystem_settings']['filesystem_url_custom'] = array(
  103. '#prefix' => '<div class="custom-container">',
  104. '#suffix' => '</div>',
  105. '#type' => 'textfield',
  106. '#title' => t('Custom unmanaged files URL'),
  107. '#default_value' => variable_get('elfinder_settings_filesystem_url_custom', ''),
  108. '#description' => t('Custom filesystem url.') . '<br/>' . t('Available tokens: <code>%files</code> (base path, eg: <code>/</code>), <code>%name</code> (current username, eg: <code>@u</code>, <b>NOTE:</b> it is not unique - users can have same username, so better to combine it with user id value), <code>%uid</code> (current user id, eg: <code>@uid</code>), <code>%lang</code> (current language code, eg: <code>@lang</code>), plus all tokens provided by token module', array('@u' => $user->name, '@uid' => $user->uid, '@lang' => $langCode)),
  109. );
  110. $form['filesystem_settings']['mime_detect'] = array(
  111. '#type' => 'radios',
  112. '#title' => t('File type detection'),
  113. '#default_value' => variable_get('elfinder_settings_filesystem_mimedetect', 'auto'),
  114. '#options' => array(
  115. 'auto' => t('Automatical detection'),
  116. ),
  117. );
  118. $form['filesystem_settings']['filesystem_allowed_extensions'] = array(
  119. '#prefix' => '<div class="custom-container">',
  120. '#suffix' => '</div>',
  121. '#type' => 'textfield',
  122. '#title' => t('Allowed file extensions'),
  123. '#maxlength' => 1000,
  124. '#default_value' => variable_get('elfinder_settings_filesystem_allowed_extensions', ''),
  125. '#description' => t('Separate extensions with a space or comma and do not include the leading dot.'),
  126. );
  127. if (function_exists('finfo_open')) {
  128. $form['filesystem_settings']['mime_detect']['#options']['finfo'] = t('php finfo');
  129. }
  130. if (function_exists('mime_content_type')) {
  131. $form['filesystem_settings']['mime_detect']['#options']['php'] = t('php mime_content_type()');
  132. }
  133. $form['filesystem_settings']['mime_detect']['#options']['linux'] = t('file -ib (linux)');
  134. $form['filesystem_settings']['mime_detect']['#options']['bsd'] = t('file -Ib (bsd)');
  135. $form['filesystem_settings']['mime_detect']['#options']['internal'] = t('By file extension (built-in)');
  136. $form['filesystem_settings']['mime_detect']['#options']['drupal'] = t('Drupal API');
  137. $form['filesystem_settings']['filesystem_inline_preview'] = array(
  138. '#type' => 'radios',
  139. '#title' => t('Preview'),
  140. '#default_value' => variable_get('elfinder_settings_filesystem_inline_preview', 'default'),
  141. '#description' => t('File types allowed to display in preview'),
  142. '#options' => array(
  143. 'default' => t('Default - images, video, audio, pdf, text'),
  144. 'all' => t('All supported for preview files - additional: md, psd, html, archives, swf, sharecad.org, MS Office Online, Google Docs - be careful'),
  145. 'custom' => t('Custom regex'),
  146. 'disabled' => t('Disabled'),
  147. ),
  148. );
  149. $form['filesystem_settings']['filesystem_inline_preview_custom'] = array(
  150. '#prefix' => '<div class="custom-container">',
  151. '#suffix' => '</div>',
  152. '#type' => 'textfield',
  153. '#title' => t('Custom preview match regex'),
  154. '#default_value' => variable_get('elfinder_settings_filesystem_inlinepreviewcustom', '^(?:(?:image|video|audio)|application/(?:x-mpegURL|dash\\+xml)|(?:text/plain|application/pdf)$)'),
  155. '#description' => t('Custom mime type match regex for preview'),
  156. );
  157. $form['filesystem_settings']['filesystem_external_preview'] = array(
  158. '#type' => 'radios',
  159. '#title' => t('External Service Preview'),
  160. '#default_value' => variable_get('elfinder_settings_filesystem_external_preview', 'disabled'),
  161. '#description' => t('Use Microsoft, Google and other online services to preview some office documents. <b>Warning!</b> By previewing document with external services <b>YOU ARE ULOADING</b> the document to them. Google, Microsoft and other service owners usually <b>TRACK</b> your activity and <b>share it with Sales, CIA, FSB (KGB), FBI, governors, etc.</b>'),
  162. '#options' => array(
  163. 'default' => t('Use Microsoft Office and Google Docs for preview'),
  164. 'disabled' => t('Disabled'),
  165. ),
  166. );
  167. $form['filesystem_settings']['file_url_type'] = array(
  168. '#type' => 'radios',
  169. '#title' => t('Selected file url type'),
  170. '#default_value' => variable_get('elfinder_settings_filesystem_fileurl', 'true'),
  171. '#options' => array(
  172. 'true' => t('Absolute'),
  173. 'false' => t('Relative'),
  174. ),
  175. );
  176. $form['filesystem_settings']['file_perm'] = array(
  177. '#type' => 'textfield',
  178. '#title' => t('Created file permissions'),
  179. '#default_value' => variable_get('elfinder_settings_filesystem_fileperm', '0666'),
  180. '#size' => 4,
  181. );
  182. $form['filesystem_settings']['dir_perm'] = array(
  183. '#type' => 'textfield',
  184. '#title' => t('Created directory permissions'),
  185. '#default_value' => variable_get('elfinder_settings_filesystem_dirperm', '0777'),
  186. '#size' => 4,
  187. );
  188. $form['filesystem_settings']['max_filesize'] = array(
  189. '#type' => 'textfield',
  190. '#title' => t('Maximum upload size'),
  191. '#default_value' => variable_get('elfinder_settings_filesystem_maxfilesize', ''),
  192. '#description' => t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size. If left empty the file sizes will be limited only by PHP\'s maximum post and file upload sizes (current limit <strong>%limit</strong>).', array('%limit' => format_size(file_upload_max_size()))),
  193. '#size' => 10,
  194. '#weight' => 5,
  195. );
  196. $form['filesystem_settings']['max_filecount'] = array(
  197. '#type' => 'textfield',
  198. '#title' => t('Maximum folder size'),
  199. '#default_value' => variable_get('elfinder_settings_filesystem_maxfilecount', ''),
  200. '#description' => t('The maximum number of files allowed in a directory. 0 for unlimited.'),
  201. '#size' => 10,
  202. '#weight' => 5,
  203. );
  204. $form['filesystem_settings']['handleprivate'] = array(
  205. '#type' => 'radios',
  206. '#title' => t('Handle private downloads'),
  207. '#default_value' => variable_get('elfinder_settings_filesystem_handleprivate', 'true'),
  208. '#options' => array(
  209. 'true' => t('Yes'),
  210. 'false' => t('No'),
  211. ),
  212. '#description' => t('Use elFinder to handle private file downloads'),
  213. );
  214. $form['thumbnail_settings'] = array(
  215. '#type' => 'fieldset',
  216. '#title' => t('Image settings'),
  217. '#collapsible' => TRUE,
  218. '#collapsed' => FALSE,
  219. );
  220. $form['thumbnail_settings']['tmbsize'] = array(
  221. '#type' => 'textfield',
  222. '#title' => t('Thumbnail size'),
  223. '#default_value' => variable_get('elfinder_settings_thumbnail_size', '48'),
  224. '#size' => 4,
  225. );
  226. $form['thumbnail_settings']['tmbdirname'] = array(
  227. '#type' => 'textfield',
  228. '#title' => t('Thumbnail directory name'),
  229. '#default_value' => variable_get('elfinder_settings_thumbnail_dirname', 'tmb'),
  230. '#size' => 10,
  231. );
  232. $form['thumbnail_settings']['imglib'] = array(
  233. '#type' => 'radios',
  234. '#title' => t('Image manipulation library'),
  235. '#default_value' => variable_get('elfinder_settings_thumbnail_imglib', 'auto'),
  236. '#options' => array(
  237. 'auto' => t('Automatical detection'),
  238. 'imagick' => t('Image Magick'),
  239. 'gd' => t('GD'),
  240. ),
  241. );
  242. $form['thumbnail_settings']['tmbcrop'] = array(
  243. '#type' => 'radios',
  244. '#title' => t('Image crop'),
  245. '#default_value' => variable_get('elfinder_settings_thumbnail_tmbcrop', 'true'),
  246. '#options' => array(
  247. 'true' => t('Yes'),
  248. 'false' => t('No'),
  249. ),
  250. '#description' => t('Crop image to fit thumbnail size. Yes - crop, No - scale image to fit thumbnail size.'),
  251. );
  252. $form['misc_settings'] = array(
  253. '#type' => 'fieldset',
  254. '#title' => t('Miscellaneous settings'),
  255. '#collapsible' => TRUE,
  256. '#collapsed' => FALSE,
  257. );
  258. $form['misc_settings']['rememberlastdir'] = array(
  259. '#type' => 'radios',
  260. '#title' => t('Remember last opened directory'),
  261. '#default_value' => variable_get('elfinder_settings_misc_rememberlastdir', 'true'),
  262. '#options' => array(
  263. 'true' => t('Yes'),
  264. 'false' => t('No'),
  265. ),
  266. '#description' => t('Creates a cookie. Disable if you have issues with caching.'),
  267. );
  268. $form['misc_settings']['usesystemjquery'] = array(
  269. '#type' => 'radios',
  270. '#title' => t('Use system jQuery'),
  271. '#default_value' => variable_get('elfinder_settings_misc_usesystemjquery', 'true'),
  272. '#options' => array(
  273. 'true' => t('Yes'),
  274. 'false' => t('No'),
  275. ),
  276. '#description' => t('Use system jQuery and jQuery UI when possible. If set to \'No\' jQuery hosted at Google will be uses.'),
  277. );
  278. $form['misc_settings']['manager_width'] = array(
  279. '#type' => 'textfield',
  280. '#title' => t('File manager width'),
  281. '#default_value' => variable_get('elfinder_settings_misc_manager_width', ''),
  282. '#size' => 4,
  283. );
  284. $form['misc_settings']['manager_height'] = array(
  285. '#type' => 'textfield',
  286. '#title' => t('File manager height'),
  287. '#default_value' => variable_get('elfinder_settings_misc_manager_height', ''),
  288. '#size' => 4,
  289. );
  290. $form['misc_settings']['hidden_folders'] = array(
  291. '#type' => 'textfield',
  292. '#title' => t('Hidden folders'),
  293. '#maxlength' => 1000,
  294. '#default_value' => variable_get('elfinder_settings_misc_hidden_folders', ''),
  295. '#description' => t('Folders listed here will be hidden. Separate with a comma. Example: "color, css_injector, ctools, styles".'),
  296. );
  297. $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  298. $form['#submit'][] = 'elfinder_admin_submit';
  299. return $form;
  300. }
  301. /**
  302. * Save form data
  303. */
  304. function elfinder_admin_submit($form, &$form_state) {
  305. variable_set('elfinder_settings_thumbnail_size', $form_state['values']['tmbsize']);
  306. variable_set('elfinder_settings_thumbnail_dirname', $form_state['values']['tmbdirname']);
  307. if ($form_state['values']['filesystem_root_custom'] != '') {
  308. variable_set('elfinder_settings_filesystem_root_custom', $form_state['values']['filesystem_root_custom']);
  309. }
  310. variable_set('elfinder_settings_filesystem_url_custom', $form_state['values']['filesystem_url_custom']);
  311. variable_set('elfinder_settings_filesystem_mimedetect', $form_state['values']['mime_detect']);
  312. variable_set('elfinder_settings_filesystem_fileurl', $form_state['values']['file_url_type']);
  313. variable_set('elfinder_settings_thumbnail_imglib', $form_state['values']['imglib']);
  314. variable_set('elfinder_settings_filesystem_fileperm', $form_state['values']['file_perm']);
  315. variable_set('elfinder_settings_filesystem_dirperm', $form_state['values']['dir_perm']);
  316. variable_set('elfinder_settings_misc_rememberlastdir', $form_state['values']['rememberlastdir']);
  317. variable_set('elfinder_settings_misc_usesystemjquery', $form_state['values']['usesystemjquery']);
  318. variable_set('elfinder_settings_thumbnail_tmbcrop', $form_state['values']['tmbcrop']);
  319. variable_set('elfinder_settings_filesystem_maxfilesize', $form_state['values']['max_filesize']);
  320. variable_set('elfinder_settings_filesystem_maxfilecount', $form_state['values']['max_filecount']);
  321. variable_set('elfinder_settings_filesystem_handleprivate', $form_state['values']['handleprivate']);
  322. variable_set('elfinder_settings_filesystem_public_root_label', $form_state['values']['filesystem_public_root_label']);
  323. variable_set('elfinder_settings_filesystem_private_root_label', $form_state['values']['filesystem_private_root_label']);
  324. variable_set('elfinder_settings_filesystem_unmanaged_root_label', $form_state['values']['filesystem_unmanaged_root_label']);
  325. variable_set('elfinder_settings_misc_manager_width', $form_state['values']['manager_width']);
  326. variable_set('elfinder_settings_misc_manager_height', $form_state['values']['manager_height']);
  327. variable_set('elfinder_settings_misc_hidden_folders', $form_state['values']['hidden_folders']);
  328. variable_set('elfinder_settings_filesystem_allowed_extensions', $form_state['values']['filesystem_allowed_extensions']);
  329. if ($form_state['values']['filesystem_inline_preview'] == 'default') {
  330. variable_set('elfinder_settings_filesystem_inlinepreviewregex', '^(?:(?:image|video|audio)|application/(?:x-mpegURL|dash\\+xml)|(?:text/plain|application/pdf)$)');
  331. } else if ($form_state['values']['filesystem_inline_preview'] == 'all') {
  332. variable_set('elfinder_settings_filesystem_inlinepreviewregex', '.');
  333. } else if ($form_state['values']['filesystem_inline_preview'] == 'custom') {
  334. variable_set('elfinder_settings_filesystem_inlinepreviewregex', variable_get('elfinder_settings_filesystem_inlinepreviewcustom', '^$'));
  335. } else {
  336. variable_set('elfinder_settings_filesystem_inlinepreviewregex', '^$');
  337. }
  338. variable_set('elfinder_settings_filesystem_inline_preview', $form_state['values']['filesystem_inline_preview']);
  339. variable_set('elfinder_settings_filesystem_inlinepreviewcustom', $form_state['values']['filesystem_inline_preview_custom']);
  340. variable_set('elfinder_settings_filesystem_external_preview', $form_state['values']['filesystem_external_preview']);
  341. drupal_set_message(t('Changes have been saved.'));
  342. }
  343. /**
  344. * Validate form data
  345. */
  346. function elfinder_admin_form_validate($form, &$form_state) {
  347. $tmbsize = $form_state['values']['tmbsize'];
  348. $mwidth = $form_state['values']['manager_width'];
  349. $mheight = $form_state['values']['manager_height'];
  350. if ($form_state['values']['filesystem_root'] == 'custom') {
  351. $customroot = $form_state['values']['filesystem_root_custom'];
  352. if ($customroot == '') {
  353. form_set_error('filesystem_root_custom', t('Filesystem root cannot be empty'));
  354. }
  355. $customroot = elfinder_parse_path_tokens($customroot);
  356. // checking directory accessibility by creating test file
  357. if (!elfinder_prepare_directory($customroot)) {
  358. form_set_error('filesystem_root_custom', t('Filesystem root is not writable'));
  359. }
  360. }
  361. if (!preg_match('/^\d{3,4}$/', $form_state['values']['file_perm'])) {
  362. form_set_error('file_perm', t('Created file permissions should be a numeric'));
  363. }
  364. if (!preg_match('/^\d{3,4}?$/', $form_state['values']['dir_perm'])) {
  365. form_set_error('dir_perm', t('Created directory permissions should be a numeric'));
  366. }
  367. if (!empty($form_state['values']['max_filesize']) && !is_numeric(parse_size($form_state['values']['max_filesize']))) {
  368. form_set_error('max_filesize', t('The file size option must contain a valid value. You may either leave the text field empty or enter a string like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes).'));
  369. }
  370. if ($mwidth && !is_numeric($mwidth)) {
  371. form_set_error('manager_width', t('Manager width size should be a number'));
  372. }
  373. if ($mheight && !is_numeric($mheight)) {
  374. form_set_error('manager_height', t('Manager height size should be a number'));
  375. }
  376. if ($tmbsize && !is_numeric($tmbsize)) {
  377. form_set_error('tmbsize', t('Thumbnail size should be a number'));
  378. }
  379. }