imce_mkdir.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Implements hook_form_formID_alter().
  4. */
  5. function _imce_mkdir_form_imce_profile_form_alter(&$form, &$form_state) {
  6. foreach (element_children($form['profile']['directories']) as $key) {
  7. $form['profile']['directories'][$key]['mkdir'] = array(
  8. '#type' => 'checkbox',
  9. '#title' => t('Add subdirectories'),
  10. '#default_value' => isset($form_state['profile']['directories'][$key]['mkdir']) ? $form_state['profile']['directories'][$key]['mkdir'] : 0,
  11. );
  12. $form['profile']['directories'][$key]['rmdir'] = array(
  13. '#type' => 'checkbox',
  14. '#title' => t('Remove subdirectories'),
  15. '#default_value' => isset($form_state['profile']['directories'][$key]['rmdir']) ? $form_state['profile']['directories'][$key]['rmdir'] : 0,
  16. );
  17. }
  18. $form['profile']['mkdirnum'] = array(
  19. '#type' => 'textfield',
  20. '#title' => t('Maximum number of subdirectories'),
  21. '#default_value' => isset($form_state['profile']['mkdirnum']) ? $form_state['profile']['mkdirnum'] : 2,
  22. '#description' => t('This setting is applicable only if you allow subdirectory creation under any of the predefined directories. Define here the maximum number of subdirectories a directory can have. Setting this to 0 removes the limit and also allows infinite subdirectory depth.'),
  23. );
  24. }
  25. /**
  26. * Mkdir form.
  27. */
  28. function _imce_mkdir_form($form, &$form_state, &$imce) {
  29. $mkdir['html1']['#markup'] = '<div class="container-inline">';
  30. $mkdir['dirname'] = array(
  31. '#type' => 'textfield',
  32. '#title' => t('Subdirectory name'),
  33. '#size' => 12,
  34. '#maxlength' => 255,
  35. );
  36. if (imce_perm_exists($imce, 'mkdir')) {
  37. $mkdir['mkdir'] = array(
  38. '#type' => 'submit',
  39. '#value' => t('Add'),
  40. '#submit' => $imce['perm']['mkdir'] ? array('imce_mkdir_submit') : NULL,
  41. );
  42. }
  43. if (imce_perm_exists($imce, 'rmdir')) {
  44. $mkdir['rmdir'] = array(
  45. '#type' => 'submit',
  46. '#value' => t('Remove'),
  47. '#submit' => $imce['perm']['rmdir'] ? array('imce_mkdir_rmdir_submit') : NULL,
  48. );
  49. }
  50. $mkdir['html2']['#markup'] = '</div>';
  51. $form['fset_mkdir'] = array(
  52. '#type' => 'fieldset',
  53. '#title' => t('Directory'),
  54. ) + $mkdir;
  55. $form['#action'] = $imce['url'];
  56. return $form;
  57. }
  58. /**
  59. * Submits mkdir form.
  60. */
  61. function imce_mkdir_submit($form, &$form_state) {
  62. $form_state['redirect'] = FALSE;
  63. $imce = &$form_state['build_info']['args'][0]['imce'];
  64. imce_mkdir_batch($imce, array(rawurldecode($form_state['values']['dirname'])));
  65. }
  66. /**
  67. * Batch adds directories.
  68. */
  69. function imce_mkdir_batch(&$imce, $dirnames = array()) {
  70. if (!isset($imce['diradded'])) {
  71. $imce['diradded'] = array();
  72. }
  73. $parent = imce_dir_uri($imce);
  74. foreach ($dirnames as $dirname) {
  75. if (!preg_match('/^[A-Za-z0-9_\-]+$/', $dirname)) {
  76. drupal_set_message(t('%dirname is not a valid directory name. It should contain only alphanumeric characters, hyphen and underscore.', array('%dirname' => $dirname)), 'error');
  77. continue;
  78. }
  79. $dirpath = $parent . $dirname;
  80. if (file_exists($dirpath)) {
  81. drupal_set_message(t('Subdirectory %dir already exists.', array('%dir' => $dirname)), 'error');
  82. continue;
  83. }
  84. if (!file_prepare_directory($dirpath, FILE_CREATE_DIRECTORY)) {
  85. drupal_set_message(t('Subdirectory %dir could not be created.', array('%dir' => $dirname)), 'error');
  86. continue;
  87. }
  88. drupal_set_message(t('Subdirectory %dir has been added.', array('%dir' => $dirname)));
  89. $imce['diradded'][] = $imce['subdirectories'][] = $dirname;
  90. }
  91. }
  92. /**
  93. * Submits rmdir form.
  94. */
  95. function imce_mkdir_rmdir_submit($form, &$form_state) {
  96. $form_state['redirect'] = FALSE;
  97. $imce = &$form_state['build_info']['args'][0]['imce'];
  98. imce_mkdir_rmdir_batch($imce, array(rawurldecode($form_state['values']['dirname'])));
  99. }
  100. /**
  101. * Batch removes directories.
  102. */
  103. function imce_mkdir_rmdir_batch(&$imce, $dirnames = array()) {
  104. if (!isset($imce['dirremoved'])) {
  105. $imce['dirremoved'] = array();
  106. }
  107. $parent = imce_dir_uri($imce);
  108. $prefix = ($imce['dir'] == '.' ? '' : $imce['dir'] . '/');
  109. foreach ($dirnames as $dirname) {
  110. $index = array_search($dirname, $imce['subdirectories']);
  111. if ($index === FALSE) {
  112. drupal_set_message(t('Subdirectory %dir does not exist.', array('%dir' => $dirname)), 'error');
  113. continue;
  114. }
  115. if (isset($imce['directories'][$prefix . $dirname])) {
  116. drupal_set_message(t('Subdirectory %dir is a predefined directory and can not be removed.', array('%dir' => $dirname)), 'error');
  117. continue;
  118. }
  119. $dirpath = $parent . $dirname;
  120. if (!imce_mkdir_rmdir_recursive($dirpath)) {
  121. drupal_set_message(t('Subdirectory %dir could not be removed.', array('%dir' => $dirname)), 'error');
  122. continue;
  123. }
  124. drupal_set_message(t('Subdirectory %dir has been removed.', array('%dir' => $dirname)));
  125. $imce['dirremoved'] = array_merge($imce['dirremoved'], array_splice($imce['subdirectories'], $index, 1));
  126. }
  127. }
  128. /**
  129. * Recursive directory deletion
  130. */
  131. function imce_mkdir_rmdir_recursive($path) {
  132. static $dirlen;
  133. if (!isset($dirlen)) {
  134. $dirlen = strlen(file_uri_scheme($path)) + 3;
  135. }
  136. if (is_dir($path) && !is_link($path)) {
  137. if ($handle = @opendir($path)) {
  138. while (($file = readdir($handle)) !== FALSE) {
  139. if ($file == '.' || $file == '..') {
  140. continue;
  141. }
  142. $filepath = $path . '/' . $file;
  143. if (!imce_mkdir_rmdir_recursive($filepath)) {
  144. drupal_set_message(t('%path could not be removed.', array('%path' => substr($filepath, $dirlen))), 'error');
  145. break;
  146. }
  147. }
  148. closedir($handle);
  149. }
  150. return @rmdir($path);
  151. }
  152. return imce_delete_filepath($path);
  153. }