| 1 |
<?php |
<?php |
| 2 |
// $Id$ |
// $Id$ |
| 3 |
|
|
|
|
|
| 4 |
/** |
/** |
| 5 |
* Implementation of hook_form_alter(). |
* Implementation of hook_form_alter(). |
| 6 |
*/ |
*/ |
| 11 |
foreach (element_children($form['profile']['directories']) as $key) { |
foreach (element_children($form['profile']['directories']) as $key) { |
| 12 |
$form['profile']['directories'][$key]['mkdir'] = array( |
$form['profile']['directories'][$key]['mkdir'] = array( |
| 13 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 14 |
'#title' => t('Create directories'), |
'#title' => t('Add subdirectories'), |
| 15 |
'#default_value' => $form_state['profile']['directories'][$key]['mkdir'], |
'#default_value' => isset($form_state['profile']['directories'][$key]['mkdir']) ? $form_state['profile']['directories'][$key]['mkdir'] : 0, |
| 16 |
|
); |
| 17 |
|
$form['profile']['directories'][$key]['rmdir'] = array( |
| 18 |
|
'#type' => 'checkbox', |
| 19 |
|
'#title' => t('Remove subdirectories'), |
| 20 |
|
'#default_value' => isset($form_state['profile']['directories'][$key]['rmdir']) ? $form_state['profile']['directories'][$key]['rmdir'] : 0, |
| 21 |
); |
); |
| 22 |
} |
} |
| 23 |
$form['profile']['mkdirnum'] = array( |
$form['profile']['mkdirnum'] = array( |
| 24 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 25 |
'#title' => t('Maximum number of sub-directories'), |
'#title' => t('Maximum number of subdirectories'), |
| 26 |
'#default_value' => $form_state['profile']['mkdirnum'], |
'#default_value' => $form_state['profile']['mkdirnum'], |
| 27 |
'#description' => t('This setting is only applicable if you allow directory 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.'), |
'#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.'), |
| 28 |
); |
); |
| 29 |
break; |
break; |
| 30 |
|
|
| 31 |
} |
} |
| 32 |
} |
} |
| 33 |
|
|
| 34 |
/** |
/** |
| 35 |
* Custom profile process. Redefines mkdir permission based on some other parameters. |
* Custom profile process. Redefines mkdir permission based on some other parameters. |
| 36 |
*/ |
*/ |
| 38 |
if (!$imce['error']) { |
if (!$imce['error']) { |
| 39 |
$imce['mkdirnum'] = (int) $imce['mkdirnum']; |
$imce['mkdirnum'] = (int) $imce['mkdirnum']; |
| 40 |
$imce['perm']['mkdir'] = $imce['perm']['mkdir'] && (!$imce['mkdirnum'] || ($imce['direct'] && $imce['mkdirnum'] > count($imce['subdirectories']))); |
$imce['perm']['mkdir'] = $imce['perm']['mkdir'] && (!$imce['mkdirnum'] || ($imce['direct'] && $imce['mkdirnum'] > count($imce['subdirectories']))); |
| 41 |
|
$imce['perm']['rmdir'] = $imce['perm']['rmdir'] && (!$imce['mkdirnum'] || $imce['direct']); |
| 42 |
} |
} |
| 43 |
} |
} |
| 44 |
|
|
| 46 |
* Custom content. Returns directory creation form |
* Custom content. Returns directory creation form |
| 47 |
*/ |
*/ |
| 48 |
function imce_mkdir_content(&$imce) { |
function imce_mkdir_content(&$imce) { |
| 49 |
drupal_add_js(drupal_get_path('module', 'imce_mkdir') .'/imce_mkdir.js'); |
if (!$imce['error'] && (imce_perm_exists($imce, 'mkdir') || imce_perm_exists($imce, 'rmdir'))) { |
| 50 |
return $imce['error'] ? '' : drupal_get_form('imce_mkdir_form', array('imce' => &$imce)); |
drupal_add_js(drupal_get_path('module', 'imce_mkdir') .'/imce_mkdir.js'); |
| 51 |
|
return drupal_get_form('imce_mkdir_form', array('imce' => &$imce)); |
| 52 |
|
} |
| 53 |
} |
} |
| 54 |
|
|
| 55 |
/** |
/** |
| 57 |
*/ |
*/ |
| 58 |
function imce_mkdir_form(&$form_state, $ref) { |
function imce_mkdir_form(&$form_state, $ref) { |
| 59 |
$imce =& $ref['imce']; |
$imce =& $ref['imce']; |
| 60 |
$form['dirname'] = array( |
$mkdir['html1']['#value'] = '<div class="container-inline">'; |
| 61 |
|
$mkdir['dirname'] = array( |
| 62 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 63 |
'#title' => t('Directory name'), |
'#title' => t('Subdirectory name'), |
| 64 |
'#size' => 12, |
'#size' => 12, |
| 65 |
'#maxlength' => 64, |
'#maxlength' => 255, |
|
'#prefix' => '<div class="container-inline">', |
|
| 66 |
); |
); |
| 67 |
$form['mkdir'] = array( |
if (imce_perm_exists($imce, 'mkdir')) { |
| 68 |
'#type' => 'submit', |
$mkdir['mkdir'] = array( |
| 69 |
'#value' => t('Create'), |
'#type' => 'submit', |
| 70 |
'#submit' => $imce['perm']['mkdir'] ? array('imce_mkdir_submit') : NULL,//permission for submission |
'#value' => t('Add'), |
| 71 |
'#suffix' => '</div>', |
'#submit' => $imce['perm']['mkdir'] ? array('imce_mkdir_submit') : NULL, |
| 72 |
); |
); |
| 73 |
$form = array('fset_mkdir' => array('#type' => 'fieldset', '#title' => t('New directory')) + $form); |
} |
| 74 |
$form['#attributes']['class'] = $imce['perm']['mkdir'] ? '' : 'imce-hide'; |
if (imce_perm_exists($imce, 'rmdir')) { |
| 75 |
|
$mkdir['rmdir'] = array( |
| 76 |
|
'#type' => 'submit', |
| 77 |
|
'#value' => t('Remove'), |
| 78 |
|
'#submit' => $imce['perm']['rmdir'] ? array('imce_mkdir_rmdir_submit') : NULL, |
| 79 |
|
); |
| 80 |
|
} |
| 81 |
|
$mkdir['html2']['#value'] = '</div>'; |
| 82 |
|
$form['fset_mkdir'] = array( |
| 83 |
|
'#type' => 'fieldset', |
| 84 |
|
'#title' => t('Manage subdirectories'), |
| 85 |
|
) + $mkdir; |
| 86 |
$form['#action'] = $imce['url']; |
$form['#action'] = $imce['url']; |
| 87 |
return $form; |
return $form; |
| 88 |
} |
} |
| 92 |
*/ |
*/ |
| 93 |
function imce_mkdir_submit($form, &$form_state) { |
function imce_mkdir_submit($form, &$form_state) { |
| 94 |
$form_state['redirect'] = FALSE; |
$form_state['redirect'] = FALSE; |
| 95 |
$imce =& $form['#parameters'][2]['imce']; |
imce_mkdir_batch($form['#parameters'][2]['imce'], array(rawurldecode($form_state['values']['dirname']))); |
| 96 |
$dirname = $form_state['values']['dirname']; |
} |
|
$imce['diradded'] = array(); |
|
|
$dirpath = file_directory_path() . ($imce['dir'] == '.' ? '' : '/'. $imce['dir']) .'/'. $dirname; |
|
| 97 |
|
|
| 98 |
if (!preg_match('/^[A-Za-z0-9_\-]+$/', $dirname)) { |
/** |
| 99 |
return drupal_set_message(t('Directory name may contain only alphanumeric characters, hyphen and underscore.'), 'error'); |
* Batch add directories. |
| 100 |
|
*/ |
| 101 |
|
function imce_mkdir_batch(&$imce, $dirnames = array()) { |
| 102 |
|
if (!isset($imce['diradded'])) { |
| 103 |
|
$imce['diradded'] = array(); |
| 104 |
|
} |
| 105 |
|
$parent = file_directory_path() . ($imce['dir'] == '.' ? '' : '/'. $imce['dir']); |
| 106 |
|
|
| 107 |
|
foreach ($dirnames as $dirname) { |
| 108 |
|
if (!preg_match('/^[A-Za-z0-9_\-]+$/', $dirname)) { |
| 109 |
|
drupal_set_message(t('%dirname is not a valid directory name. It should contain only alphanumeric characters, hyphen and underscore.', array('%dirname' => $dirname)), 'error'); |
| 110 |
|
continue; |
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
$dirpath = $parent .'/'. $dirname; |
| 114 |
|
|
| 115 |
|
if (file_exists($dirpath)) { |
| 116 |
|
drupal_set_message(t('Subdirectory %dir already exists.', array('%dir' => $dirname)), 'error'); |
| 117 |
|
continue; |
| 118 |
|
} |
| 119 |
|
|
| 120 |
|
if (!file_check_directory($dirpath, FILE_CREATE_DIRECTORY)) { |
| 121 |
|
drupal_set_message(t('Subdirectory %dir could not be created.', array('%dir' => $dirname)), 'error'); |
| 122 |
|
continue; |
| 123 |
|
} |
| 124 |
|
|
| 125 |
|
drupal_set_message(t('Subdirectory %dir has been added.', array('%dir' => $dirname))); |
| 126 |
|
$imce['diradded'][] = $imce['subdirectories'][] = $dirname; |
| 127 |
} |
} |
| 128 |
|
} |
| 129 |
|
|
| 130 |
|
/** |
| 131 |
|
* Submit rmdir form. |
| 132 |
|
*/ |
| 133 |
|
function imce_mkdir_rmdir_submit($form, &$form_state) { |
| 134 |
|
$form_state['redirect'] = FALSE; |
| 135 |
|
imce_mkdir_rmdir_batch($form['#parameters'][2]['imce'], array(rawurldecode($form_state['values']['dirname']))); |
| 136 |
|
} |
| 137 |
|
|
| 138 |
if (file_exists($dirpath)) { |
/** |
| 139 |
return drupal_set_message(t('Directory %dir already exists.', array('%dir' => $dirname)), 'error'); |
* Batch remove directories. |
| 140 |
|
*/ |
| 141 |
|
function imce_mkdir_rmdir_batch(&$imce, $dirnames = array()) { |
| 142 |
|
if (!isset($imce['dirremoved'])) { |
| 143 |
|
$imce['dirremoved'] = array(); |
| 144 |
|
} |
| 145 |
|
$parent = file_directory_path() . ($imce['dir'] == '.' ? '' : '/'. $imce['dir']); |
| 146 |
|
|
| 147 |
|
foreach ($dirnames as $dirname) { |
| 148 |
|
$index = array_search($dirname, $imce['subdirectories']); |
| 149 |
|
|
| 150 |
|
if ($index === FALSE) { |
| 151 |
|
drupal_set_message(t('Subdirectory %dir does not exist.', array('%dir' => $dirname)), 'error'); |
| 152 |
|
continue; |
| 153 |
|
} |
| 154 |
|
|
| 155 |
|
$dirpath = $parent .'/'. $dirname; |
| 156 |
|
if (!imce_mkdir_rmdir_recursive($dirpath)) { |
| 157 |
|
drupal_set_message(t('Subdirectory %dir could not be removed.', array('%dir' => $dirname)), 'error'); |
| 158 |
|
continue; |
| 159 |
|
} |
| 160 |
|
|
| 161 |
|
drupal_set_message(t('Subdirectory %dir has been removed.', array('%dir' => $dirname))); |
| 162 |
|
$imce['dirremoved'] = array_merge($imce['dirremoved'], array_splice($imce['subdirectories'], $index, 1)); |
| 163 |
} |
} |
| 164 |
|
} |
| 165 |
|
|
| 166 |
if (!file_check_directory($dirpath, FILE_CREATE_DIRECTORY)) { |
/** |
| 167 |
return drupal_set_message(t('Directory creation failed!'), 'error'); |
* Recursive directory deletion |
| 168 |
|
*/ |
| 169 |
|
function imce_mkdir_rmdir_recursive($path) { |
| 170 |
|
if (is_dir($path) && !is_link($path)) { |
| 171 |
|
if ($handle = @opendir($path)) { |
| 172 |
|
while (($file = readdir($handle)) !== FALSE) { |
| 173 |
|
if ($file == '.' || $file == '..') { |
| 174 |
|
continue; |
| 175 |
|
} |
| 176 |
|
$filepath = $path .'/'. $file; |
| 177 |
|
if (!imce_mkdir_rmdir_recursive($filepath)) { |
| 178 |
|
drupal_set_message(t('%path could not be removed.', array('%path' => $filepath)), 'error'); |
| 179 |
|
} |
| 180 |
|
} |
| 181 |
|
closedir($handle); |
| 182 |
|
} |
| 183 |
|
return @rmdir($path); |
| 184 |
} |
} |
| 185 |
|
return imce_mkdir_unlink($path); |
| 186 |
|
} |
| 187 |
|
|
| 188 |
$imce['diradded'][] = $imce['subdirectories'][] = $dirname; |
/** |
| 189 |
|
* Delete a file in recursive directory deletion. |
| 190 |
|
*/ |
| 191 |
|
function imce_mkdir_unlink($filepath) { |
| 192 |
|
if (!file_delete($filepath)) { |
| 193 |
|
return FALSE; |
| 194 |
|
} |
| 195 |
|
db_query("DELETE FROM {files} WHERE filepath = '%s'", $filepath); |
| 196 |
|
return TRUE; |
| 197 |
} |
} |
| 198 |
|
|
| 199 |
/** |
/** |
| 201 |
*/ |
*/ |
| 202 |
function imce_js_mkdir(&$imce) { |
function imce_js_mkdir(&$imce) { |
| 203 |
if ($imce['perm']['mkdir']) { |
if ($imce['perm']['mkdir']) { |
| 204 |
$_POST['op'] = t('Create'); |
$_POST['op'] = t('Add'); |
| 205 |
|
drupal_get_form('imce_mkdir_form', array('imce' => &$imce)); |
| 206 |
|
return array('diradded' => array_map('rawurlencode', $imce['diradded'])); |
| 207 |
|
} |
| 208 |
|
} |
| 209 |
|
|
| 210 |
|
/** |
| 211 |
|
* Ajax operation: rmdir |
| 212 |
|
*/ |
| 213 |
|
function imce_js_rmdir(&$imce) { |
| 214 |
|
if ($imce['perm']['rmdir']) { |
| 215 |
|
$_POST['op'] = t('Remove'); |
| 216 |
drupal_get_form('imce_mkdir_form', array('imce' => &$imce)); |
drupal_get_form('imce_mkdir_form', array('imce' => &$imce)); |
| 217 |
return array_map('rawurlencode', $imce['diradded']); |
return array('dirremoved' => array_map('rawurlencode', $imce['dirremoved'])); |
| 218 |
} |
} |
| 219 |
} |
} |