imce.admin.inc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. <?php
  2. /**
  3. * @file
  4. * Serves administration pages of IMCE.
  5. */
  6. /**
  7. * Admin main page.
  8. */
  9. function imce_admin() {
  10. $profiles = variable_get('imce_profiles', array());
  11. $header = array(t('Profile name'), array('data' => t('Operations'), 'colspan' => 2));
  12. $rows = array();
  13. foreach ($profiles as $pid => $profile) {
  14. $rows[] = array(
  15. check_plain($profile['name']),
  16. l(t('Edit'), 'admin/config/media/imce/profile/edit/' . $pid),
  17. $pid == 1 ? '' : l(t('Delete'), 'admin/config/media/imce/profile/delete/' . $pid),
  18. );
  19. }
  20. $rows[] = array('', array('data' => l(t('Add new profile'), 'admin/config/media/imce/profile'), 'colspan' => 2));
  21. $output['title'] = array(
  22. '#markup' => '<h2 class="title">' . t('Configuration profiles') . '</h2>',
  23. );
  24. $output['table'] = array(
  25. '#theme' => 'table',
  26. '#header' => $header,
  27. '#rows' => $rows,
  28. '#attributes' => array('id' => 'imce-profiles-list'),
  29. );
  30. $output['form'] = drupal_get_form('imce_admin_form');
  31. // Display security warnings
  32. if (empty($_POST)) {
  33. $roles = variable_get('imce_roles_profiles', array());
  34. if (!empty($roles[DRUPAL_ANONYMOUS_RID]['public_pid']) || !empty($roles[DRUPAL_ANONYMOUS_RID]['private_pid'])) {
  35. drupal_set_message(t('Anonymous user role has access to IMCE.') . ' ' . t('Make sure this is not a misconfiguration.'), 'warning');
  36. }
  37. if (imce_admin_check_wildcard_upload(DRUPAL_AUTHENTICATED_RID, $roles)) {
  38. drupal_set_message(t('Authenticated user role is assigned a configuration profile with unrestricted file extensions.') . ' ' . t('Make sure this is not a misconfiguration.'), 'warning');
  39. }
  40. }
  41. return $output;
  42. }
  43. /**
  44. * Admin form.
  45. */
  46. function imce_admin_form($form, &$form_state) {
  47. //roles profiles
  48. $form['roles'] = array('#tree' => TRUE);
  49. $roles = imce_sorted_roles();
  50. $form['#weighted'] = count($roles) > 3;
  51. foreach ($roles as $rid => $role) {
  52. $core = $rid == DRUPAL_ANONYMOUS_RID || $rid == DRUPAL_AUTHENTICATED_RID;
  53. $form['roles'][$rid] = imce_role_form($role, $form['#weighted'], $core);
  54. }
  55. //common settings
  56. $form['common'] = array(
  57. '#type' => 'fieldset',
  58. '#title' => t('Common settings'),
  59. '#collapsible' => TRUE,
  60. '#collapsed' => TRUE,
  61. );
  62. $form['common']['textarea'] = array(
  63. '#type' => 'textfield',
  64. '#title' => t('Enable inline image/file insertion into plain textareas'),
  65. '#default_value' => variable_get('imce_settings_textarea', ''),
  66. '#maxlength' => NULL,
  67. '#description' => t('If you don\'t use any WYSIWYG editor, this feature will allow you to add your images or files as <strong>html code into any plain textarea</strong>. Enter <strong>comma separated textarea IDs</strong> under which you want to enable a link to IMCE. The * character is a wildcard. Hint: ID of Body fields in most node types starts with edit-body*.'),
  68. );
  69. $form['common']['absurls'] = array(
  70. '#type' => 'checkbox',
  71. '#title' => t('Absolute URLs'),
  72. '#default_value' => variable_get('imce_settings_absurls', 0),
  73. '#description' => t('Check if you want IMCE to return absolute file URLs.'),
  74. );
  75. $form['common']['replace'] = array(
  76. '#type' => 'radios',
  77. '#title' => t('Default behaviour for existing files during file uploads'),
  78. '#default_value' => variable_get('imce_settings_replace', FILE_EXISTS_RENAME),
  79. '#options' => array(
  80. FILE_EXISTS_RENAME => t('Keep the existing file renaming the new one'),
  81. FILE_EXISTS_ERROR => t('Keep the existing file rejecting the new one'),
  82. FILE_EXISTS_REPLACE => t('Replace the existing file with the new one')
  83. ),
  84. );
  85. $form['common']['thumb_method'] = array(
  86. '#type' => 'radios',
  87. '#title' => t('Default method for creating thumbnails'),
  88. '#default_value' => variable_get('imce_settings_thumb_method', 'scale_and_crop'),
  89. '#options' => array(
  90. 'scale' => t('Scale the image with respect to the thumbnail dimensions.'),
  91. 'scale_and_crop' => t('First scale then crop the image to fit the thumbnail dimensions.')
  92. ),
  93. );
  94. $form['common']['disable_private'] = array(
  95. '#type' => 'checkbox',
  96. '#title' => t('Disable serving of private files'),
  97. '#default_value' => variable_get('imce_settings_disable_private', 1),
  98. '#description' => t('IMCE serves all files under private files directory without applying any access restrictions. This allows anonymous access to any file(/system/files/filename) unless there is a module restricting access to the files. Here you can disable this feature.'),
  99. );
  100. $form['common']['admin_theme'] = array(
  101. '#type' => 'checkbox',
  102. '#title' => t('Use admin theme for IMCE paths'),
  103. '#default_value' => variable_get('imce_settings_admin_theme', FALSE),
  104. '#description' => t('If you have user interface issues with the active theme you may consider switching to admin theme.'),
  105. );
  106. $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  107. $form['#theme'] = 'imce_admin';
  108. $form['#submit'][] = 'imce_admin_submit';
  109. return $form;
  110. }
  111. /**
  112. * Admin form themed.
  113. */
  114. function imce_admin_theme($variables) {
  115. $form = $variables['form'];
  116. $profile1 = imce_user1_profile();
  117. $header = array(t('User role'));
  118. $rows = array(array(t('Site maintenance account')));
  119. $keys = array('name');
  120. //add each stream wrapper as a column
  121. $swrappers = file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE);
  122. foreach ($swrappers as $scheme => $info) {
  123. $header[] = l($info['name'], 'imce/' . $scheme);
  124. $rows[0][] = check_plain($profile1['name']);
  125. $keys[] = $scheme . '_pid';
  126. }
  127. //in case we need profile weights.
  128. $weight_info = '';
  129. if ($form['#weighted']) {
  130. $header[] = t('Weight');
  131. $rows[0][] = t('n/a');
  132. $keys[] = 'weight';
  133. $weight_info = t('For users who have <strong>multiple roles</strong>, the <strong>weight</strong> property will determine the assigned profile. Lighter roles that are placed upper will take the precedence. So, an administrator role should be placed over other roles by having a smaller weight, ie. -10.');
  134. }
  135. foreach (element_children($form['roles']) as $rid) {
  136. $cells = array();
  137. foreach ($keys as $key) {
  138. $cells[] = drupal_render($form['roles'][$rid][$key]);
  139. }
  140. $rows[] = $cells;
  141. }
  142. $output = '<h2 class="title">' . t('Role-profile assignments') . '</h2>';
  143. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  144. $output .= '<div class="form-item"><div class="description">' . t('Assign profiles to user roles for available file systems. Your default file system is %name.', array('%name' => $swrappers[variable_get('file_default_scheme', 'public')]['name'])) . ' ' . $weight_info . '</div></div>';
  145. $output .= drupal_render($form['common']);
  146. $output .= drupal_render_children($form);
  147. return $output;
  148. }
  149. /**
  150. * Validate admin form.
  151. */
  152. function imce_admin_form_validate($form, &$form_state) {
  153. $roles = $form_state['values']['roles'];
  154. // Check anonymous profile. Do not allow wildcard upload.
  155. if ($key = imce_admin_check_wildcard_upload(DRUPAL_ANONYMOUS_RID, $roles)) {
  156. form_error($form['roles'][DRUPAL_ANONYMOUS_RID][$key], t('Anonymous user role can not have a configuration profile with unrestricted file extensions.'));
  157. }
  158. }
  159. /**
  160. * Submit admin form.
  161. */
  162. function imce_admin_submit($form, &$form_state) {
  163. $roles = $form_state['values']['roles'];
  164. if (count($roles) > 3) {
  165. uasort($roles, 'imce_rolesort');
  166. }
  167. variable_set('imce_roles_profiles', $roles);
  168. variable_set('imce_settings_textarea', $form_state['values']['textarea']);
  169. variable_set('imce_settings_absurls', $form_state['values']['absurls']);
  170. variable_set('imce_settings_replace', $form_state['values']['replace']);
  171. variable_set('imce_settings_thumb_method', $form_state['values']['thumb_method']);
  172. variable_set('imce_settings_disable_private', $form_state['values']['disable_private']);
  173. variable_set('imce_settings_admin_theme', $form_state['values']['admin_theme']);
  174. drupal_set_message(t('Changes have been saved.'));
  175. }
  176. /**
  177. * Add-Edit-Delete profiles.
  178. */
  179. function imce_profile_operations($op = 'add', $pid = 0) {
  180. //delete
  181. if ($op == 'delete') {
  182. drupal_set_title(t('Delete configuration profile'));
  183. return drupal_get_form('imce_profile_delete_form', $pid);
  184. }
  185. //add-edit
  186. if ($op === 'add' || $op === 'edit') {
  187. return drupal_get_form('imce_profile_form', $pid);
  188. }
  189. drupal_access_denied();
  190. }
  191. /**
  192. * Profile form.
  193. */
  194. function imce_profile_form($form, &$form_state, $pid = 0) {
  195. if ($pid && $profile = imce_load_profile($pid)) {
  196. drupal_set_title($profile['name']);
  197. }
  198. else {
  199. $pid = 0;
  200. $profile = imce_sample_profile();
  201. $profile['name'] = '';
  202. }
  203. //import profile
  204. if (isset($_GET['import']) && $imported = imce_load_profile($_GET['import'])) {
  205. if (empty($form_state['post'])) {
  206. drupal_set_message(t('Settings were imported from the profile %name', array('%name' => $imported['name'])));
  207. }
  208. $imported['name'] = $profile['name']; //preserve the original name.
  209. $profile = $imported;
  210. }
  211. $form_state['profile'] = $profile;//store the original profile just in case.
  212. $form = array('#tree' => TRUE);
  213. $form['name'] = array(
  214. '#type' => 'textfield',
  215. '#title' => t('Profile name'),
  216. '#default_value' => $profile['name'],
  217. '#description' => t('Give a name to this profile.'),
  218. '#required' => TRUE,
  219. );
  220. $form['import'] = array(
  221. '#markup' => imce_profile_import_html($pid),
  222. );
  223. $form['usertab'] = array(
  224. '#type' => 'checkbox',
  225. '#title' => t('Display file browser tab in user profile pages.'),
  226. '#default_value' => $profile['usertab'],
  227. );
  228. $form['filesize'] = array(
  229. '#type' => 'textfield',
  230. '#title' => t('Maximum file size per upload'),
  231. '#default_value' => $profile['filesize'],
  232. '#description' => t('Set to 0 to use the maximum value available.') . ' ' . t('Your PHP settings limit the maximum file size per upload to %size.', array('%size' => format_size(file_upload_max_size()))),
  233. '#field_suffix' => t('MB'),
  234. );
  235. $form['quota'] = array(
  236. '#type' => 'textfield',
  237. '#title' => t('Directory quota'),
  238. '#default_value' => $profile['quota'],
  239. '#description' => t('Define the upload quota per directory.') . ' ' . t('Set to 0 to use the maximum value available.'),
  240. '#field_suffix' => t('MB'),
  241. );
  242. $form['tuquota'] = array(
  243. '#type' => 'textfield',
  244. '#title' => t('Total user quota'),
  245. '#default_value' => $profile['tuquota'],
  246. '#description' => t('This quota measures the size of all user uploaded files in the database and does not include FTP files. You can either use both quotations together or safely ignore this by setting the value to 0.'),
  247. '#field_suffix' => t('MB'),
  248. );
  249. $form['extensions'] = array(
  250. '#type' => 'textfield',
  251. '#title' => t('Permitted file extensions'),
  252. '#default_value' => $profile['extensions'],
  253. '#maxlength' => 255,
  254. '#description' => t('Specify the allowed file extensions for uploaded files. Separate extensions with a space and do not include the leading dot.') . ' ' . t('Set to * to remove the restriction.'),
  255. );
  256. $form['dimensions'] = array(
  257. '#type' => 'textfield',
  258. '#title' => t('Maximum image dimensions'),
  259. '#default_value' => $profile['dimensions'],
  260. '#description' => t('The maximum allowed image size (e.g. 640x480). Set to 0 for no restriction. If an <a href="!image-toolkit-link">image toolkit</a> is installed, files exceeding this value will be scaled down to fit.', array('!image-toolkit-link' => url('admin/config/media/image-toolkit'))),
  261. '#field_suffix' => '<kbd>' . t('WIDTHxHEIGHT') . '</kbd>',
  262. );
  263. $form['filenum'] = array(
  264. '#type' => 'textfield',
  265. '#title' => t('Maximum number of files per operation'),
  266. '#default_value' => $profile['filenum'],
  267. '#description' => t('You can allow users to select multiple files for operations such as delete, resize, etc. Entire batch file operation is executed in a single drupal load, which may be good. However there will be an increase in script execution time, cpu load and memory consumption possibly exceeding the limits of your server, which is really bad. For unlimited number of file handling, set this to 0.'),
  268. );
  269. //Directories
  270. $form['directories']['#theme'] = 'imce_directories';
  271. $form['directories']['#weight'] = 1;
  272. for ($i = 0; $i < count($profile['directories']); $i++) {
  273. $form['directories'][$i] = imce_directory_form($profile['directories'][$i]);
  274. }
  275. $form['directories'][$i] = imce_directory_form();
  276. $form['directories'][$i+1] = imce_directory_form();
  277. //Thumbnails
  278. $form['thumbnails']['#theme'] = 'imce_thumbnails';
  279. $form['thumbnails']['#weight'] = 2;
  280. for ($i = 0; $i < count($profile['thumbnails']); $i++) {
  281. $form['thumbnails'][$i] = imce_thumbnail_form($profile['thumbnails'][$i]);
  282. }
  283. $form['thumbnails'][$i] = imce_thumbnail_form();
  284. $form['thumbnails'][$i+1] = imce_thumbnail_form();
  285. $form = array('profile' => $form);
  286. $form['pid'] = array('#type' => 'hidden', '#value' => $pid);
  287. $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  288. $form['#validate'][] = 'imce_profile_validate';
  289. $form['#submit'][] = 'imce_profile_submit';
  290. return $form;
  291. }
  292. /**
  293. * Profile form validate.
  294. */
  295. function imce_profile_validate($form, &$form_state) {
  296. $profile = &$form_state['values']['profile'];
  297. $dim_re = '/^\d+x\d+$/';
  298. $dim_error = t('Dimensions must be specified in <kbd>WIDTHxHEIGHT</kbd> format.');
  299. // Check max image dimensions
  300. if ($profile['dimensions'] && !preg_match($dim_re, $profile['dimensions'])) {
  301. return form_set_error('profile][dimensions', $dim_error);
  302. }
  303. // Check thumbnails dimensions
  304. foreach ($profile['thumbnails'] as $i => $thumb) {
  305. if (trim($thumb['name']) != '' && !preg_match($dim_re, $thumb['dimensions'])) {
  306. return form_set_error("profile][thumbnails][$i][dimensions", $dim_error);
  307. }
  308. }
  309. }
  310. /**
  311. * Profile form submit.
  312. */
  313. function imce_profile_submit($form, &$form_state) {
  314. $profile = $form_state['values']['profile'];
  315. $pid = $form_state['values']['pid'];
  316. $message = $pid > 0 ? t('The changes have been saved.') : t('Profile has been added.');
  317. //unset empty fields of directories and thumbnails.
  318. imce_clean_profile_fields($profile);
  319. //save profile.
  320. $pid = imce_update_profiles($pid, $profile);
  321. drupal_set_message($message);
  322. $form_state['redirect'] = 'admin/config/media/imce/profile/edit/' . $pid;
  323. }
  324. /**
  325. * directory settings form
  326. */
  327. function imce_directory_form($directory = array()) {
  328. if (empty($directory)) {
  329. $directory = array('name' => '', 'subnav' => 0, 'browse' => 0, 'upload' => 0, 'thumb' => 0, 'delete' => 0, 'resize' => 0);
  330. }
  331. $form['name'] = array(
  332. '#type' => 'textfield',
  333. '#default_value' => $directory['name'],
  334. '#size' => 24,
  335. '#maxlength' => NULL,
  336. );
  337. $form['subnav'] = array(
  338. '#type' => 'checkbox',
  339. '#title' => t('Including subdirectories'),
  340. '#default_value' => $directory['subnav'],
  341. );
  342. $form['browse'] = array(
  343. '#type' => 'checkbox',
  344. '#title' => t('Browse'),
  345. '#default_value' => $directory['browse'],
  346. );
  347. $form['upload'] = array(
  348. '#type' => 'checkbox',
  349. '#title' => t('Upload'),
  350. '#default_value' => $directory['upload'],
  351. );
  352. $form['thumb'] = array(
  353. '#type' => 'checkbox',
  354. '#title' => t('Thumbnails'),
  355. '#default_value' => $directory['thumb'],
  356. );
  357. $form['delete'] = array(
  358. '#type' => 'checkbox',
  359. '#title' => t('Delete'),
  360. '#default_value' => $directory['delete'],
  361. );
  362. $form['resize'] = array(
  363. '#type' => 'checkbox',
  364. '#title' => t('Resize'),
  365. '#default_value' => $directory['resize'],
  366. );
  367. return $form;
  368. }
  369. /**
  370. * Directorys form themed.
  371. */
  372. function imce_directories_theme($variables) {
  373. $form = $variables['form'];
  374. $rows = array();
  375. $root = t('root');
  376. foreach (element_children($form) as $key) {
  377. //directory path
  378. $row = array('<div class="container-inline">&lt;' . $root . '&gt;' . '/' . drupal_render($form[$key]['name']) . '</div>' . drupal_render($form[$key]['subnav']));
  379. unset($form[$key]['name'], $form[$key]['subnav']);
  380. //permissions
  381. $header = array();
  382. foreach (element_children($form[$key]) as $perm) {
  383. $header[] = $form[$key][$perm]['#title'];
  384. unset($form[$key][$perm]['#title']);
  385. $row[] = drupal_render($form[$key][$perm]);
  386. }
  387. $rows[] = $row;
  388. }
  389. array_unshift($header, t('Directory path'));
  390. $output = '<h3 class="title">' . t('Directories') . '</h3>';
  391. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  392. $output .= '<div class="form-item"><div class="description">' . t('Define directories that users of this profile can access.
  393. <ul>
  394. <li>Use alphanumeric characters as directory paths.</li>
  395. <li>To specify file system root, just enter <strong>.</strong>(dot) character.</li>
  396. <li>Use <strong>%uid</strong> as a placeholder for user ID. Ex: <em>users/user%uid</em> creates directories such as <em>users/user1</em>, <em>users/user42</em>, etc.</li>
  397. <li>To remove a directory from the list, leave the directory path blank.</li>
  398. <li>If you want more flexibility in directory paths you can execute php to return a directory path.<br />
  399. For php execution your directory path must start with <strong>php:</strong> and the rest must be a valid php code that is expected to return the actual directory path. <br />Ex: <strong>php: return \'users/\'.$user->name;</strong> defines <strong>users/USER-NAME</strong> as the directory path.<br />
  400. A multi-level directory example <strong>php: return date(\'Y\', $user->created).\'/\'.date(\'m\', $user->created).\'/\'.$user->uid;</strong> defines <strong>MEMBERSHIP-YEAR/MONTH/USER-ID</strong> as the directory path, resulting in self-categorized user directories based on membership date.<br />
  401. Note that you should use the $user variable instead of $GLOBALS[\'user\'] since they are not always the same object.</li>
  402. </ul>
  403. <p>Note that thumbnails permission does not affect thumbnail creation on upload. See thumbnails decription below.</p>
  404. <p>If you need more fields, just fill all and save, and you will get two more on the next page.</p>') . '</div></div>';
  405. $output .= drupal_render_children($form);
  406. return $output;
  407. }
  408. /**
  409. * thumbnail settings form
  410. */
  411. function imce_thumbnail_form($thumb = array()) {
  412. if (empty($thumb)) {
  413. $thumb = array('name' => '', 'dimensions' => '', 'prefix' => '', 'suffix' => '');
  414. }
  415. $form['name'] = array(
  416. '#type' => 'textfield',
  417. '#default_value' => $thumb['name'],
  418. '#size' => 20,
  419. );
  420. $form['dimensions'] = array(
  421. '#type' => 'textfield',
  422. '#default_value' => $thumb['dimensions'],
  423. '#size' => 20,
  424. );
  425. $form['prefix'] = array(
  426. '#type' => 'textfield',
  427. '#default_value' => $thumb['prefix'],
  428. '#size' => 20,
  429. );
  430. $form['suffix'] = array(
  431. '#type' => 'textfield',
  432. '#default_value' => $thumb['suffix'],
  433. '#size' => 20,
  434. );
  435. return $form;
  436. }
  437. /**
  438. * Thumbnails form themed.
  439. */
  440. function imce_thumbnails_theme($variables) {
  441. $form = $variables['form'];
  442. $header = array(t('Name'), t('Dimensions'), t('Prefix'), t('Suffix'));
  443. $rows = array();
  444. foreach (element_children($form) as $key) {
  445. $rows[] = array(
  446. drupal_render($form[$key]['name']),
  447. drupal_render($form[$key]['dimensions']),
  448. drupal_render($form[$key]['prefix']),
  449. drupal_render($form[$key]['suffix']),
  450. );
  451. }
  452. $output = '<h3 class="title">' . t('Thumbnails') . '</h3>';
  453. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  454. $output .= '<div class="form-item"><div class="description">' . t('You may create a list of thumbnail options that users can choose from.
  455. <ul>
  456. <li>Use alphanumeric characters as thumbnail names.</li>
  457. <li>Specify dimensions as <strong>WidthxHeight</strong>.</li>
  458. <li>Prefix and suffix are strings that are added to original file name to create the thumbnail name.</li>
  459. <li>An example thumbnail: Name = <strong>Small</strong>, Dimensions = <strong>80x80</strong>, Prefix = <strong>small_</strong></li>
  460. </ul>
  461. <p>Note that users will always be able to create these thumbnails on file upload no matter what the thumbnail permission is.</p>
  462. <p>If you need more fields, just fill all and save, and you will get two more on the next page.</p>') . '</div></div>';
  463. $output .= drupal_render_children($form);
  464. return $output;
  465. }
  466. /**
  467. * Role-profile form
  468. */
  469. function imce_role_form($role, $weight = TRUE, $core = TRUE) {
  470. $form['name'] = array(
  471. '#markup' => check_plain($role['name']),
  472. );
  473. if ($weight) {
  474. $form['weight'] = $core ? array(
  475. '#type' => 'textfield',
  476. '#value' => $role['weight'],
  477. '#attributes' => array('readonly' => 'readonly', 'style' => 'border: none; width: 2em; background-color: transparent;'),
  478. ) : array(
  479. '#type' => 'weight',
  480. '#default_value' => $role['weight'],
  481. );
  482. }
  483. foreach (array_keys(file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE)) as $scheme) {
  484. $form[$scheme . '_pid'] = array(
  485. '#type' => 'select',
  486. '#options' => imce_profile_options(),
  487. '#default_value' => $role[$scheme . '_pid'],
  488. '#empty_value' => 0,
  489. );
  490. }
  491. return $form;
  492. }
  493. /**
  494. * Profile delete form
  495. */
  496. function imce_profile_delete_form($form, &$form_state, $pid) {
  497. if ($pid > 1 && $profile = imce_load_profile($pid)) {
  498. $form['#submit'][] = 'imce_profile_delete_submit';
  499. $form['pid'] = array('#type' => 'hidden', '#value' => $pid);
  500. return confirm_form($form,
  501. t('Are you sure you want to delete the profile %name?',
  502. array('%name' => $profile['name'])),
  503. 'admin/config/media/imce',
  504. '',
  505. t('Delete'),
  506. t('Cancel')
  507. );
  508. }
  509. drupal_goto('admin/config/media/imce');
  510. }
  511. /**
  512. * Profile delete form submit
  513. */
  514. function imce_profile_delete_submit($form, &$form_state) {
  515. imce_update_profiles($form_state['values']['pid'], NULL);
  516. drupal_set_message(t('Profile has been deleted.'));
  517. $form_state['redirect'] = 'admin/config/media/imce';
  518. }
  519. /**
  520. * Profile options.
  521. */
  522. function imce_profile_options() {
  523. $options = array();
  524. foreach (variable_get('imce_profiles', array()) as $pid => $profile) {
  525. $options[$pid] = $profile['name'];
  526. }
  527. return $options;
  528. }
  529. /**
  530. * Profile import links.
  531. */
  532. function imce_profile_import_html($pid = 0) {
  533. $output = '';
  534. $links = array();
  535. foreach (variable_get('imce_profiles', array()) as $id => $profile) {
  536. if ($pid != $id) {
  537. $links[] = l($profile['name'], $_GET['q'], array('query' => array('import' => $id)));
  538. }
  539. }
  540. if (!empty($links)) {
  541. $output = '<p><strong>' . t('Import settings from other profiles') . '</strong>: ';
  542. $output .= implode(', ', $links) . '</p>';
  543. }
  544. return $output;
  545. }
  546. /**
  547. * Update role-profile assignments.
  548. */
  549. function imce_update_roles($pid) {
  550. $roles = variable_get('imce_roles_profiles', array());
  551. foreach ($roles as $rid => $role) {
  552. foreach ($role as $key => $value) {
  553. if (substr($key, -4) == '_pid') {
  554. if ($value == $pid) {
  555. $roles[$rid][$key] = 0;
  556. }
  557. elseif ($value > $pid) {
  558. $roles[$rid][$key]--;
  559. }
  560. }
  561. }
  562. }
  563. variable_set('imce_roles_profiles', $roles);
  564. }
  565. /**
  566. * Add, update or delete a profile.
  567. */
  568. function imce_update_profiles($pid, $profile = NULL) {
  569. $profiles = variable_get('imce_profiles', array());
  570. //add or update
  571. if (isset($profile)) {
  572. $pid = isset($profiles[$pid]) ? $pid : count($profiles)+1;
  573. $profiles[$pid] = $profile;
  574. }
  575. //delete
  576. elseif (isset($profiles[$pid]) && $pid > 1) {
  577. unset($profiles[$pid]);
  578. for ($i = $pid+1; isset($profiles[$i]); $i++) {
  579. $profiles[$i-1] = $profiles[$i];
  580. unset($profiles[$i]);
  581. }
  582. imce_update_roles($pid);
  583. }
  584. variable_set('imce_profiles', $profiles);
  585. return $pid;
  586. }
  587. /**
  588. * Unset empty fields in thumbnails and directory paths.
  589. */
  590. function imce_clean_profile_fields(&$profile) {
  591. $clean = array();
  592. foreach ($profile['thumbnails'] as $thumb) {
  593. if (trim($thumb['name']) != '') {
  594. $clean[] = $thumb;
  595. }
  596. }
  597. $profile['thumbnails'] = $clean;
  598. $clean = array();
  599. $names = array();
  600. foreach ($profile['directories'] as $dir) {
  601. $dir['name'] = trim($dir['name'], '/ ');
  602. if ($dir['name'] == '') {
  603. continue;
  604. }
  605. if (isset($names[$dir['name']])) {
  606. drupal_set_message(t('Duplicate directory paths are not allowed.'), 'error');
  607. continue;
  608. }
  609. if (!imce_reg_dir($dir['name'])) {
  610. drupal_set_message(t('%dirname is not accepted as a proper directory name.', array('%dirname' => $dir['name'])), 'error');
  611. continue;
  612. }
  613. $clean[] = $dir;
  614. $names[$dir['name']] = 1;
  615. }
  616. $profile['directories'] = $clean;
  617. }
  618. /**
  619. * Profile load.
  620. */
  621. function imce_load_profile($pid) {
  622. $profiles = variable_get('imce_profiles', array());
  623. return isset($profiles[$pid]) ? $profiles[$pid] : NULL;
  624. }
  625. /**
  626. * Sort roles according to their weights.
  627. */
  628. function imce_sorted_roles() {
  629. static $sorted;
  630. if (!isset($sorted)) {
  631. $sorted = array();
  632. $roles = user_roles();
  633. $profiles = variable_get('imce_profiles', array());
  634. $roles_profiles = variable_get('imce_roles_profiles', array());
  635. $roles_profiles[DRUPAL_ANONYMOUS_RID]['weight'] = 12;
  636. $roles_profiles[DRUPAL_AUTHENTICATED_RID]['weight'] = 11;
  637. $schemes = array_keys(file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE));
  638. foreach ($roles as $rid => $name) {
  639. $sorted[$rid] = array(
  640. 'name' => $name,
  641. 'weight' => isset($roles_profiles[$rid]['weight']) ? $roles_profiles[$rid]['weight'] : 0
  642. );
  643. foreach ($schemes as $scheme) {
  644. $key = $scheme . '_pid';
  645. $sorted[$rid][$key] = isset($roles_profiles[$rid][$key]) && isset($profiles[$roles_profiles[$rid][$key]]) ? $roles_profiles[$rid][$key] : 0;
  646. }
  647. }
  648. uasort($sorted, 'imce_rolesort');
  649. }
  650. return $sorted;
  651. }
  652. /**
  653. * Sorting function for roles.
  654. */
  655. function imce_rolesort($r1, $r2) {
  656. return $r1['weight']-$r2['weight'];
  657. }
  658. /**
  659. * Checks if the given role can upload all extensions.
  660. */
  661. function imce_admin_check_wildcard_upload($rid, $conf = NULL) {
  662. if (!isset($conf)) {
  663. $conf = variable_get('imce_roles_profiles', array());
  664. }
  665. if (!empty($conf[$rid])) {
  666. foreach ($conf[$rid] as $key => $pid) {
  667. if ($pid && substr($key, -4) == '_pid') {
  668. if ($profile = imce_load_profile($pid)) {
  669. if ($profile['extensions'] === '*' && !empty($profile['directories'])) {
  670. foreach ($profile['directories'] as $dirconf) {
  671. if (!empty($dirconf['upload'])) {
  672. return $key;
  673. }
  674. }
  675. }
  676. }
  677. }
  678. }
  679. }
  680. return FALSE;
  681. }
  682. //Include core profile functions.
  683. include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'imce') . '/inc/imce.core.profiles.inc';