|
1 <?php |
|
2 // $Id: subdir.inc,v 1.3 2008/02/29 16:57:12 ufku Exp $ |
|
3 |
|
4 /** |
|
5 * Subdirectory creation form. |
|
6 */ |
|
7 function imce_form_subdirectory(&$form_state) { |
|
8 $form['help'] = array( |
|
9 '#type' => 'markup', |
|
10 '#value' => t('<p>Here you can create subdirectories for your users. Multiple directory creation is possible at a time with the <strong>*</strong> character. For example, specifying <strong>user*/foo</strong> will create <strong>foo</strong> named directories under each directory starting with <strong>user</strong>. */foo*/bar will create bar named directories under directories starting with foo in each directory of file system path.</p>'), |
|
11 ); |
|
12 $form['path'] = array( |
|
13 '#title' => t('Directory path'), |
|
14 '#type' => 'textfield', |
|
15 '#default_value' => isset($form_state['values']['path']) ? $form_state['values']['path'] : '', |
|
16 '#maxlength' => 255, |
|
17 '#field_prefix' => file_directory_path() .'/', |
|
18 ); |
|
19 $form['submit'] = array('#type' => 'submit', '#value' => t('Create')); |
|
20 return $form; |
|
21 } |
|
22 |
|
23 /** |
|
24 * Subdirectory form submit. |
|
25 */ |
|
26 function imce_form_subdirectory_submit($form, &$form_state) { |
|
27 imce_create_subdirectories($form_state['values']['path'], file_directory_path()); |
|
28 $form_state['redirect'] = FALSE; |
|
29 } |
|
30 |
|
31 /** |
|
32 * Create directories under root according to path. |
|
33 */ |
|
34 function imce_create_subdirectories($path, $root) { |
|
35 $directories = explode('/', str_replace('\\', '/', $path)); |
|
36 $paths = array($root); |
|
37 |
|
38 foreach ($directories as $directory) { |
|
39 |
|
40 //no asterisks. create the directory(s) |
|
41 if (strpos($directory, '*') === FALSE) { |
|
42 //check and create directories under each path. |
|
43 foreach ($paths as $i => $path) { |
|
44 $paths[$i] = $path = $path .'/'. $directory; |
|
45 if (!file_check_location($path, $root) || !file_check_directory($path, FILE_CREATE_DIRECTORY)) { |
|
46 drupal_set_message(t('Specified path must be under file sytem path.'), 'error'); |
|
47 return FALSE; |
|
48 } |
|
49 } |
|
50 } |
|
51 |
|
52 //asterisks. get matching subdirectories and update paths. |
|
53 else { |
|
54 $newpaths = array(); |
|
55 $regexp = str_replace('*', '.*', $directory); |
|
56 |
|
57 //add matching paths. |
|
58 foreach ($paths as $path) { |
|
59 $newpaths = array_merge($newpaths, imce_get_subdirectories($path, $regexp)); |
|
60 } |
|
61 |
|
62 //no matching paths |
|
63 if (empty($newpaths)) { |
|
64 drupal_set_message(t('No matching subdirectories found.'), 'error'); |
|
65 return FALSE; |
|
66 } |
|
67 |
|
68 $paths = $newpaths; |
|
69 } |
|
70 |
|
71 } |
|
72 |
|
73 return TRUE; |
|
74 } |
|
75 |
|
76 /** |
|
77 * Scan directory and return directories matching the expression. |
|
78 */ |
|
79 function imce_get_subdirectories($dir, $expr = '') { |
|
80 $directories = array(); |
|
81 |
|
82 if ($handle = @opendir($dir)) { |
|
83 while (($file = readdir($handle)) !== FALSE) { |
|
84 if ($file != '.' && $file != '..' && is_dir($dir .'/'. $file) && preg_match('/^'. $expr .'$/', $file)) { |
|
85 $directories[] = $dir .'/'. $file; |
|
86 } |
|
87 } |
|
88 closedir($handle); |
|
89 } |
|
90 |
|
91 return $directories; |
|
92 } |