imce_dir_man.module 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * @file
  4. * imce_dir_man.module provides functions for managing configuration
  5. * determining what the imce_dir_man_path() function. This function can
  6. * be used as php code in the directory setting of an IMCE profile to
  7. * allow for per user configuration of directory restrictions
  8. */
  9. /**
  10. * hook menu
  11. */
  12. function imce_dir_man_menu() {
  13. $menu['admin/config/media/imce_dir_man'] = array(
  14. 'title' => 'IMCE User Directory Access Manager',
  15. 'description' => 'Administer directory restrictions for IMCE',
  16. 'page callback' => 'drupal_get_form',
  17. 'page arguments' => array('imce_dir_man_form'),
  18. 'access arguments' => array('administer site configuration'),
  19. );
  20. return $menu;
  21. }
  22. /**
  23. * generates admin form for managing user directory restrictions
  24. */
  25. function imce_dir_man_form() {
  26. $query = db_select('imce_dir_man', 'i');
  27. $query->join('users', 'u', 'u.uid = i.uid');
  28. $res = $query->fields('i', array('uid', 'dir'))
  29. ->fields('u', array('name'))
  30. ->orderBy('name')
  31. ->execute();
  32. $weight = 1;
  33. foreach ($res as $row) {
  34. $form[$row->uid] = array(
  35. '#title' => t('User @name', array('@name' => $row->name)),
  36. '#type' => 'fieldset',
  37. '#collapsible' => TRUE,
  38. '#collapsed' => TRUE,
  39. '#tree' => TRUE,
  40. '#weight' => $weight++,
  41. );
  42. $form[$row->uid]['del'] = array(
  43. '#title' => t('Remove restrictions'),
  44. '#description' => t('Deletes restrictions set for this user'),
  45. '#type' => 'checkbox',
  46. '#weight' => 10,
  47. );
  48. $form[$row->uid]['dir'] = array(
  49. '#title' => t('Directory'),
  50. '#description' => t('Directory to restrict user to relative to site file upload root, comma separate to specify multiple directories, a/b/c restricts a user to !root/a/b/c', array('!root' => variable_get('file_public_path', conf_path() . '/files'))),
  51. '#type' => 'textfield',
  52. '#size' => '100',
  53. '#maxsize' => '255',
  54. '#default_value' => $row->dir,
  55. '#weight' => 20,
  56. );
  57. }
  58. $form['new_user'] = array(
  59. '#title' => t('Add restriction for new user'),
  60. '#type' => 'fieldset',
  61. '#tree' => TRUE,
  62. '#weight' => $weight++,
  63. );
  64. $form['new_user']['name'] = array(
  65. '#title' => t('User'),
  66. '#type' => 'textfield',
  67. '#size' => '100',
  68. '#maxsize' => '255',
  69. '#weight' => 10,
  70. );
  71. $form['new_user']['dir'] = array(
  72. '#title' => t('Directory'),
  73. '#type' => 'textfield',
  74. '#description' => t('Directory to restrict user to relative to site file upload root, comma separate to specify multiple directories, a/b/c restricts a user to sites/default/files/a/b/c'),
  75. '#size' => '100',
  76. '#maxsize' => '255',
  77. '#weight' => 20,
  78. );
  79. $form['submit'] = array(
  80. '#type' => 'submit',
  81. '#value' => 'Save',
  82. '#weight' => $weight,
  83. );
  84. $form['#submit'] = array('imce_dir_man_form_submit');
  85. $form['#validate'] = array('imce_dir_man_form_validate');
  86. return $form;
  87. }
  88. /**
  89. * Validate restriction form (imce_dir_man_form) settings
  90. */
  91. function imce_dir_man_form_validate($form, &$form_state) {
  92. foreach ($form_state['values'] as $uid => $data) {
  93. $data['dir'] = trim($data['dir'], ',');
  94. if ($data['dir'] == '' && (isset($data['del']) && $data['del'] != 1) && $data['name'] != '') {
  95. form_set_error($uid . '][dir', t('Invalid directory restriction (blank or contains only commas)'));
  96. }
  97. else if ($uid == 'new_user' && $data['name'] != '') {
  98. $uid = db_select('users', 'u')
  99. ->fields('u', array('uid'))
  100. ->condition('name', $data['name'])
  101. ->execute()
  102. ->fetchCol();
  103. if (!$uid) {
  104. form_set_error($uid . '][name', t('Invalid user'));
  105. }
  106. else {
  107. $uid = db_select('imce_dir_man', 'i')
  108. ->fields('i', array('uid'))
  109. ->condition('uid', $uid[0])
  110. ->execute()
  111. ->fetchCol();
  112. if ($uid) {
  113. form_set_error($uid . '][name', t('User @name already has restriction configured, please update their entry to make modifications', array('@name' => $data['name'])));
  114. }
  115. }
  116. }
  117. }
  118. }
  119. /**
  120. * Save configured restriction settings adding new settings,
  121. * updating or deleting existing settings
  122. */
  123. function imce_dir_man_form_submit($form, &$form_state) {
  124. $rec = new stdclass();
  125. foreach ($form_state['values'] as $uid => $data) {
  126. $rec->dir = trim($data['dir'], ',');
  127. $rec->uid = $uid;
  128. if ($uid == 'new_user' && $data['name'] != '' && $data['dir'] != '') {
  129. $uid = db_select('users', 'u')
  130. ->fields('u', array('uid'))
  131. ->condition('name', $data['name'])
  132. ->execute()
  133. ->fetchCol();
  134. if ($uid) {
  135. $rec->uid = $uid[0];
  136. drupal_write_record('imce_dir_man', $rec);
  137. }
  138. }
  139. else if (isset($data['del']) && $data['del']) {
  140. db_delete('imce_dir_man')->condition('uid', $uid)->execute();
  141. }
  142. else {
  143. drupal_write_record('imce_dir_man', $rec, array('uid'));
  144. }
  145. }
  146. }
  147. /**
  148. * returns an array representing a user's currently accessible file directories
  149. * used by the imce uploader
  150. * If a user has not been restricted in the configuration, . (all directories)
  151. * is returned
  152. */
  153. function imce_dir_man_path() {
  154. global $user;
  155. $dir = db_select('imce_dir_man', 'i')
  156. ->fields('i', array('dir'))
  157. ->condition('uid', $user->uid)
  158. ->execute()
  159. ->fetchCol();
  160. if (!$dir || !$dir[0]) {
  161. $dir[0] = '.';
  162. }
  163. return preg_split('/,/', $dir[0]);
  164. }