elfinder.admin.profiles.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 profile settings page
  10. */
  11. function elfinder_admin_profile($form, &$form_state) {
  12. global $user, $language;
  13. $action = $form_state['build_info']['args'][0];
  14. $profile_name = $form_state['build_info']['args'][1];
  15. drupal_add_css(drupal_get_path('module', 'elfinder') . '/css/elfinder.admin.css');
  16. $profiles = elfinder_admin_profile_get();
  17. $p = NULL;
  18. if (!is_array($profiles)) {
  19. $profiles = array($profiles);
  20. }
  21. foreach ($profiles as $prof) {
  22. if (is_object($prof) && $prof->name == $profile_name && $p == NULL) {
  23. $p = $prof;
  24. }
  25. }
  26. $profile = isset($p->settings) ? $p->settings : NULL;
  27. if ($action == 'profile_add') {
  28. $form['profile_name'] = array(
  29. '#type' => 'textfield',
  30. '#title' => t('Profile Name'),
  31. '#default_value' => '',
  32. '#size' => 14,
  33. );
  34. } else {
  35. $form['profile_name_label'] = array(
  36. '#title' => t('Profile Name'),
  37. '#markup' => $profile_name,
  38. '#size' => 14,
  39. '#type' => 'item',
  40. );
  41. $form['profile_name'] = array(
  42. '#default_value' => $profile_name,
  43. '#type' => 'hidden',
  44. );
  45. }
  46. $form['profile_description'] = array(
  47. '#type' => 'textarea',
  48. '#title' => t('Description'),
  49. '#default_value' => isset($p->description) ? $p->description : '',
  50. );
  51. $form['profile_role'] = array(
  52. '#type' => 'checkboxes',
  53. '#title' => t('Roles'),
  54. '#default_value' => isset($profile['profile_role']) ? array_keys($profile['profile_role']) : array(),
  55. '#options' => user_roles(),
  56. '#description' => t('Roles for which profile settings will be applied'),
  57. );
  58. $form['profile_action'] = array(
  59. '#default_value' => $action,
  60. '#type' => 'hidden',
  61. );
  62. $form['filesystem_settings'] = array(
  63. '#type' => 'fieldset',
  64. '#title' => t('File system'),
  65. '#collapsible' => TRUE,
  66. '#collapsed' => TRUE,
  67. );
  68. $form['volumes'] = array(
  69. '#type' => 'fieldset',
  70. '#title' => t('Volumes'),
  71. '#collapsible' => TRUE,
  72. '#collapsed' => FALSE,
  73. );
  74. $form['volumes']['volumes_wrapper'] = array(
  75. '#weight' => -4,
  76. '#prefix' => '<div class="clear-block" id="poll-choice-wrapper">',
  77. '#suffix' => '</div>',
  78. );
  79. $form['volumes']['volumes_wrapper']['volume'] = array(
  80. '#prefix' => '<div id="profile-volumes">',
  81. '#suffix' => '</div>',
  82. '#type' => 'markup',
  83. '#value' => '&nbsp;',
  84. );
  85. $dirs = $profile['volume'];
  86. if (isset($form_state['values']['volume'])) {
  87. $choices = $form_state['values']['volume'];
  88. $lasti = 0;
  89. for ($i = 0; $i < count($choices); $i++) {
  90. $form['volumes']['volumes_wrapper']['volume'][$i] = elfinder_admin_dir_form($i, $choices[$i], 0);
  91. $lasti = $i;
  92. }
  93. if (isset($form_state['clicked_button']['#name']) && $form_state['clicked_button']['#name'] == 'addfield') {
  94. $form['volumes']['volumes_wrapper']['volume'][] = elfinder_admin_dir_form(count($choices), '', 0);
  95. }
  96. } else {
  97. for ($i = 0; $i < count($dirs); $i++) {
  98. $form['volumes']['volumes_wrapper']['volume'][$i] = elfinder_admin_dir_form($i, $dirs[$i], 0);
  99. }
  100. if (isset($form_state['clicked_button']['#name']) && $form_state['clicked_button']['#name'] == 'addfield') {
  101. $form['volumes']['volumes_wrapper']['volume'][] = elfinder_admin_dir_form(count($dirs), '', 0);
  102. }
  103. }
  104. $ckeditor_volumes = array('' => t('Select One'));
  105. $i = 0;
  106. foreach ($form['volumes']['volumes_wrapper']['volume'] as $volume) {
  107. if (is_array($volume) && isset($volume['volume_settings'])) {
  108. $ckeditor_volumes[] = t('Volume @i (@path)', array(
  109. '@i' => $i,
  110. '@path' => $volume['volume_settings']['path']['#default_value'],
  111. ));
  112. $i++;
  113. }
  114. }
  115. $form['volumes']['volumes_wrapper']['volume_add'] = array(
  116. '#type' => 'submit',
  117. '#value' => t('Add volume'),
  118. '#weight' => 1,
  119. '#name' => 'addfield',
  120. '#submit' => array('elfinder_admin_profile_change_volume'),
  121. '#ajax' => array(
  122. 'wrapper' => 'profile-volumes',
  123. 'callback' => 'elfinder_admin_profile_callback',
  124. 'method' => 'replace',
  125. 'effect' => 'fade',
  126. ),
  127. );
  128. $form['filesystem_settings']['mime_detect'] = array(
  129. '#type' => 'radios',
  130. '#title' => t('File type detection'),
  131. '#default_value' => isset($profile['mimedetect']) ? $profile['mimedetect'] : 'auto',
  132. '#options' => array(
  133. 'auto' => t('Automatical detection'),
  134. ),
  135. );
  136. if (function_exists('finfo_open')) {
  137. $form['filesystem_settings']['mime_detect']['#options']['finfo'] = t('php finfo');
  138. }
  139. if (function_exists('mime_content_type')) {
  140. $form['filesystem_settings']['mime_detect']['#options']['php'] = t('php mime_content_type()');
  141. }
  142. $form['filesystem_settings']['mime_detect']['#options']['linux'] = t('file -ib (linux)');
  143. $form['filesystem_settings']['mime_detect']['#options']['bsd'] = t('file -Ib (bsd)');
  144. $form['filesystem_settings']['mime_detect']['#options']['internal'] = t('By file extension (built-in)');
  145. $form['filesystem_settings']['mime_detect']['#options']['drupal'] = t('Drupal API');
  146. $form['filesystem_settings']['file_url_type'] = array(
  147. '#type' => 'radios',
  148. '#title' => t('Selected file url type'),
  149. '#default_value' => $profile['file_url_type'] == 'true' ? 'true' : 'false',
  150. '#options' => array(
  151. 'true' => t('Absolute'),
  152. 'false' => t('Relative'),
  153. ),
  154. );
  155. $form['filesystem_settings']['file_perm'] = array(
  156. '#type' => 'textfield',
  157. '#title' => t('Created file permissions'),
  158. '#default_value' => isset($profile['file_perm']) ? $profile['file_perm'] : '0666',
  159. '#size' => 4,
  160. );
  161. $form['filesystem_settings']['dir_perm'] = array(
  162. '#type' => 'textfield',
  163. '#title' => t('Created directory permissions'),
  164. '#default_value' => isset($profile['dir_perm']) ? $profile['dir_perm'] : '0777',
  165. '#size' => 4,
  166. );
  167. $form['filesystem_settings']['max_filesize'] = array(
  168. '#type' => 'textfield',
  169. '#title' => t('Maximum upload size'),
  170. '#default_value' => isset($profile['max_filesize']) ? $profile['max_filesize'] : '',
  171. '#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()))),
  172. '#size' => 10,
  173. '#weight' => 5,
  174. );
  175. $form['filesystem_settings']['user_quota'] = array(
  176. '#type' => 'textfield',
  177. '#title' => t('User quota'),
  178. '#default_value' => isset($profile['user_quota']) ? $profile['user_quota'] : '',
  179. '#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 unlimited.'),
  180. '#size' => 10,
  181. '#weight' => 5,
  182. );
  183. $form['thumbnail_settings'] = array(
  184. '#type' => 'fieldset',
  185. '#title' => t('Thumbnails'),
  186. '#collapsible' => TRUE,
  187. '#collapsed' => TRUE,
  188. );
  189. $form['thumbnail_settings']['tmbsize'] = array(
  190. '#type' => 'textfield',
  191. '#title' => t('Thumbnail size'),
  192. '#default_value' => isset($profile['tmbsize']) ? $profile['tmbsize'] : '48',
  193. '#size' => 4,
  194. );
  195. $form['thumbnail_settings']['tmbdirname'] = array(
  196. '#type' => 'textfield',
  197. '#title' => t('Thumbnail directory name'),
  198. '#default_value' => isset($profile['tmbdirname']) ? $profile['tmbdirname'] : 'tmb',
  199. '#size' => 10,
  200. );
  201. $form['thumbnail_settings']['imglib'] = array(
  202. '#type' => 'radios',
  203. '#title' => t('Image manipulation library'),
  204. '#default_value' => isset($profile['imglib']) ? $profile['imglib'] : 'auto',
  205. '#options' => array(
  206. 'auto' => t('Automatical detection'),
  207. 'imagick' => t('Image Magick'),
  208. 'gd' => t('GD'),
  209. ),
  210. );
  211. $form['thumbnail_settings']['tmbcrop'] = array(
  212. '#type' => 'radios',
  213. '#title' => t('Image crop'),
  214. '#default_value' => $profile['tmbcrop'] == 'true' ? 'true' : 'false',
  215. '#options' => array(
  216. 'true' => t('Yes'),
  217. 'false' => t('No'),
  218. ),
  219. '#description' => t('Crop image to fit thumbnail size. Yes - crop, No - scale image to fit thumbnail size.'),
  220. );
  221. $form['misc_settings'] = array(
  222. '#type' => 'fieldset',
  223. '#title' => t('Miscellaneous'),
  224. '#collapsible' => TRUE,
  225. '#collapsed' => TRUE,
  226. );
  227. $form['misc_settings']['rememberlastdir'] = array(
  228. '#type' => 'radios',
  229. '#title' => t('Remember last opened directory'),
  230. '#default_value' => $profile['rememberlastdir'] == 'true' ? 'true' : 'false',
  231. '#options' => array(
  232. 'true' => t('Yes'),
  233. 'false' => t('No'),
  234. ),
  235. '#description' => t('Creates a cookie. Disable if you have issues with caching.'),
  236. );
  237. $form['misc_settings']['manager_width'] = array(
  238. '#type' => 'textfield',
  239. '#title' => t('File manager width'),
  240. '#default_value' => isset($profile['manager_width']) ? $profile['manager_width'] : '',
  241. '#size' => 4,
  242. );
  243. $form['misc_settings']['manager_height'] = array(
  244. '#type' => 'textfield',
  245. '#title' => t('File manager height'),
  246. '#default_value' => isset($profile['manager_height']) ? $profile['manager_height'] : '',
  247. '#size' => 4,
  248. );
  249. $form['misc_settings']['ckeditor_upload_settings'] = array(
  250. '#type' => 'fieldset',
  251. '#title' => t('CKEditor/FCKeditor Upload Settings'),
  252. '#collapsible' => TRUE,
  253. '#collapsed' => TRUE,
  254. '#description' => t('CKEditor and FCKeditor allowing to upload and insert image files directly from Insert Image dialog.'),
  255. );
  256. $form['misc_settings']['ckeditor_upload_settings']['ckeditor_upload_directory'] = array(
  257. '#type' => 'textfield',
  258. '#title' => t('Path'),
  259. '#default_value' => isset($profile['ckeditor_upload_directory']) ? $profile['ckeditor_upload_directory'] : '',
  260. '#size' => 40,
  261. '#description' => t('Image upload path. Default file uri is used if no uri prefix specified. Examples: public://ckeditor - image will be uploaded into public://ckeditor; images/ckeditor - image will be uploaded to :uriimages/ckeditor', array(':uri' => file_build_uri(''))),
  262. );
  263. $form['submit'] = array(
  264. '#type' => 'submit',
  265. '#name' => 'save_profile',
  266. '#value' => t('Save configuration')
  267. );
  268. $form['#submit'][] = 'elfinder_admin_submit';
  269. return $form;
  270. }
  271. function elfinder_admin_profile_callback($form, $form_state) {
  272. $form_state['rebuild'] = TRUE;
  273. return $form['volumes']['volumes_wrapper']['volume'];
  274. }
  275. /**
  276. * Settings form callback
  277. */
  278. function elfinder_admin_profiles($action = '', $profile_name = '') {
  279. $obj = NULL;
  280. if ($action == 'profile_delete') {
  281. $obj = drupal_get_form('elfinder_admin_profiles_delete_form', $action, $profile_name);
  282. } else {
  283. $obj = drupal_get_form('elfinder_admin_profile', $action, $profile_name);
  284. }
  285. return drupal_render($obj);
  286. }
  287. function elfinder_admin_profiles_delete_form($form_id, &$form_state, $action, $profile_name) {
  288. $form = array();
  289. $form['profile_name'] = array(
  290. '#type' => 'hidden',
  291. '#default_value' => $profile_name,
  292. );
  293. $form['#submit'][] = 'elfinder_admin_profile_delete_submit';
  294. return confirm_form($form, t('Are you sure you want to delete the profile %profile?', array('%profile' => $profile_name)), 'admin/config/media/elfinder', '', t('Delete'), t('Cancel')
  295. );
  296. return $form;
  297. }
  298. function elfinder_admin_profile_delete_submit($form, &$form_state) {
  299. if ($form_state['values']['confirm'] == 1) {
  300. drupal_set_message(t('Profile deleted'));
  301. $pid = elfinder_admin_profile_get_pid($form_state['values']['profile_name']);
  302. if ($pid > 0) {
  303. db_delete('elfinder_profile')
  304. ->condition('pid', $pid)
  305. ->execute();
  306. }
  307. }
  308. drupal_goto('admin/config/media/elfinder');
  309. }
  310. function elfinder_admin_dir_form($delta, $values = array(), $votes = 0) {
  311. $form = array();
  312. $pvtfiles = file_stream_wrapper_get_instance_by_uri('private://');
  313. $pubfiles = file_stream_wrapper_get_instance_by_uri('public://');
  314. if (is_object($pvtfiles)) {
  315. $defaulturl = $pvtfiles->getExternalUrl();
  316. } elseif (is_object($pubfiles)) {
  317. $defaulturl = $pubfiles->getExternalUrl();
  318. }
  319. $form['volume_settings'] = array(
  320. '#type' => 'fieldset',
  321. '#title' => t('Volume @n (@p)', array('@n' => ($delta + 1), '@p' => isset($values['path']) ? $values['path'] : '')),
  322. '#collapsible' => TRUE,
  323. '#collapsed' => FALSE,
  324. );
  325. $form['volume_settings']['path'] = array(
  326. '#type' => 'textfield',
  327. '#title' => t('Path'),
  328. '#default_value' => isset($values['path']) ? $values['path'] : '',
  329. '#parents' => array('volume', $delta, 'path'),
  330. '#prefix' => '<div class="elfinder-field-wrapper-volume-path">',
  331. '#suffix' => '</div>',
  332. '#size' => 40,
  333. );
  334. $form['volume_settings']['label'] = array(
  335. '#type' => 'textfield',
  336. '#title' => t('Label'),
  337. '#size' => 15,
  338. '#default_value' => isset($values['label']) ? $values['label'] : '',
  339. '#description' => t('Root directory label in directory tree'),
  340. '#parents' => array('volume', $delta, 'label'),
  341. );
  342. $form['volume_settings']['url'] = array(
  343. '#type' => 'textfield',
  344. '#title' => t('URL'),
  345. '#default_value' => isset($values['url']) ? $values['url'] : '',
  346. '#parents' => array('volume', $delta, 'url'),
  347. '#prefix' => '<div class="elfinder-field-wrapper-volume-path">',
  348. '#suffix' => '</div>',
  349. '#description' => t('Custom URL prefix (default %def)', array('%def' => $defaulturl)),
  350. '#size' => 40,
  351. );
  352. $form['volume_settings']['delete'] = array(
  353. '#type' => 'submit',
  354. '#value' => t('Delete'),
  355. '#name' => 'rmfield-' . $delta,
  356. '#submit' => array('elfinder_admin_profile_change_volume'),
  357. '#ajax' => array(
  358. 'callback' => 'elfinder_admin_profile_callback',
  359. 'wrapper' => 'profile-volumes',
  360. 'method' => 'replace',
  361. 'effect' => 'fade',
  362. ),
  363. );
  364. return $form;
  365. }
  366. function elfinder_admin_profile_change_volume($form, &$form_state) {
  367. $clickedbutton = $form_state['clicked_button']['#name'];
  368. if (preg_match("/^rmfield-(\d+)/", $clickedbutton, $m)) {
  369. $itemid = $m[1];
  370. unset($form_state['values']['volume'][$itemid]);
  371. $form_state['values']['volume'] = array_values($form_state['values']['volume']);
  372. unset($form_state['input']['volume'][$itemid]);
  373. $form_state['input']['volume'] = array_values($form_state['input']['volume']);
  374. } elseif ($clickedbutton = 'addfield') {
  375. $form_state['input']['volume'][] = '';
  376. }
  377. $form_state['rebuild'] = TRUE;
  378. }
  379. function elfinder_admin_profile_get_pid($name) {
  380. $pid = db_select('elfinder_profile', 'p')->condition('name', $name)->fields('p', array('pid'))->execute()->fetchField();
  381. return $pid;
  382. }
  383. /**
  384. * Save form data
  385. */
  386. function elfinder_admin_submit($form, &$form_state) {
  387. $settings = array('tmbsize', 'tmbdirname', 'mime_detect', 'file_url_type', 'imglib', 'file_perm', 'dir_perm', 'rememberlastdir', 'usesystemjquery', 'tmbcrop', 'max_filesize', 'manager_width', 'manager_height', 'volume', 'user_quota', 'ckeditor_upload_volume', 'ckeditor_upload_directory');
  388. $profile_settings = array();
  389. foreach ($settings as $setting) {
  390. if (isset($form_state['values'][$setting])) {
  391. /* Excluding empty volumes */
  392. if ($setting == 'volume') {
  393. for ($i = 0; $i < count($form_state['values']['volume']); $i++) {
  394. if (empty($form_state['values']['volume'][$i]['path'])) {
  395. unset($form_state['values']['volume'][$i]);
  396. }
  397. }
  398. }
  399. $profile_settings[$setting] = $form_state['values'][$setting];
  400. }
  401. }
  402. // Save roles.
  403. $profile_settings['profile_role'] = array();
  404. foreach ($form_state['values']['profile_role'] as $rid) {
  405. if ($rid != 0) {
  406. $profile_settings['profile_role'][$rid] = $rid;
  407. }
  408. }
  409. $profile_name = $form_state['values']['profile_name'];
  410. $profile_description = $form_state['values']['profile_description'];
  411. $pid = 0;
  412. if ($form_state['values']['profile_action'] == 'profile_edit' && $profile_name) {
  413. $pid = elfinder_admin_profile_get_pid($profile_name);
  414. }
  415. $profile = new StdClass;
  416. $profile->pid = $pid;
  417. $profile->name = $profile_name;
  418. $profile->description = $profile_description;
  419. $profile->settings = serialize($profile_settings);
  420. if (isset($form_state['values']['save_profile'])) {
  421. drupal_write_record('elfinder_profile', $profile, $pid > 0 ? array('pid') : array());
  422. drupal_set_message(t('Profile have been saved.'));
  423. drupal_goto('admin/config/media/elfinder');
  424. }
  425. }
  426. /**
  427. * Validate form data
  428. */
  429. function elfinder_admin_form_validate($form, &$form_state) {
  430. $tmbsize = $form_state['values']['tmbsize'];
  431. $mwidth = $form_state['values']['manager_width'];
  432. $mheight = $form_state['values']['manager_height'];
  433. if ($form_state['values']['filesystem_root'] == 'custom') {
  434. $customroot = $form_state['values']['filesystem_root_custom'];
  435. if ($customroot == '') {
  436. form_set_error('filesystem_root_custom', t('Filesystem root cannot be empty'));
  437. }
  438. $customroot = elfinder_parse_path_tokens($customroot);
  439. // checking directory accessibility by creating test file
  440. if (!elfinder_prepare_directory($customroot)) {
  441. form_set_error('filesystem_root_custom', t('Filesystem root is not writable'));
  442. }
  443. }
  444. if (!preg_match('/^\d{3,4}$/', $form_state['values']['file_perm'])) {
  445. form_set_error('file_perm', t('Created file permissions should be a numeric'));
  446. }
  447. if (!preg_match('/^\d{3,4}?$/', $form_state['values']['dir_perm'])) {
  448. form_set_error('dir_perm', t('Created directory permissions should be a numeric'));
  449. }
  450. if (!empty($form_state['values']['max_filesize']) && !is_numeric(parse_size($form_state['values']['max_filesize']))) {
  451. 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).'));
  452. }
  453. if (!empty($form_state['values']['user_quota']) && !is_numeric(parse_size($form_state['values']['user_quota']))) {
  454. form_set_error('user_quota', 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).'));
  455. }
  456. if ($mwidth && !is_numeric($mwidth)) {
  457. form_set_error('manager_width', t('Manager width size should be a number'));
  458. }
  459. if ($mheight && !is_numeric($mheight)) {
  460. form_set_error('manager_height', t('Manager height size should be a number'));
  461. }
  462. if ($tmbsize && !is_numeric($tmbsize)) {
  463. form_set_error('tmbsize', t('Thumbnail size should be a number'));
  464. }
  465. }