/[drupal]/contributions/modules/webfm/webfm_file.inc
ViewVC logotype

Contents of /contributions/modules/webfm/webfm_file.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.12 - (show annotations) (download) (as text)
Wed Aug 19 15:37:09 2009 UTC (3 months, 1 week ago) by robmilne
Branch: MAIN
CVS Tags: DRUPAL-6--2-11, DRUPAL-6--2-10, HEAD
Changes since 1.11: +2 -2 lines
File MIME type: text/x-php
fix drupal_set_message as per node 545536
1 <?php
2 /* $Id: webfm_file.inc,v 1.11 2008/10/15 17:16:56 robmilne Exp $ */
3
4 /**
5 * webfm_mkdir -called from the ajax action - switch case 'mkdir':
6 *
7 * @param string $source - the source directory path
8 * @param string $dest - the destination directory name
9 *
10 * @ret string -name of directory if created
11 * FALSE -if unsuccessful
12 */
13 function webfm_mkdir($source, $dest, $rename = FALSE, &$error){
14 $mkdir_path = ($source.'/'.$dest);
15 if (!is_dir($mkdir_path)) {
16 if(@mkdir($mkdir_path)) {
17 chmod($mkdir_path, 0775);
18 if (!is_writable($mkdir_path))
19 $error = t('The directory ').$mkdir_path.t(' is not writable');
20 else
21 return $mkdir_path;
22 }
23 } else if($rename == TRUE) {
24 $count = 0;
25 while(is_dir($munge_path = ($mkdir_path.'_'.$count))){
26 $count++;
27 }
28 if(@mkdir($munge_path)) {
29 chmod($munge_path, 0775);
30 if (!is_writable($munge_path))
31 $error = t('The directory ').$munge_path.t(' is not writable');
32 else
33 return $munge_path;
34 } else {
35 $error = t('mkdir for ').$munge_path.t(' failed');
36 }
37 } else {
38 $error = t('The directory ').$mkdir_path.t(' already exists');
39 }
40 return FALSE;
41 }
42
43 /**
44 * webfm_rename -called from the ajax action - switch case 'rename':
45 *
46 * @param string $source - the source directory path
47 * @param string $dest - the destination directory name
48 * @param bool $uid - user id
49 * @param string &$err_arr - ref to error array for client feedback
50 *
51 * @ret bool -true if the directory/file is renamed and file record(s)
52 * updated if dir/file under db control
53 */
54 function webfm_rename($source, $dest, $uid, &$err_arr) {
55 $dest_temp = $dest.'~'; //handle possible case problem
56 //File
57 //if target is a file and new name isn't already used...
58 if(is_file($source) && !is_file($dest)) {
59 $file_o = webfm_get_file_record('', $source);
60 //if we are admin or owner of target file...
61 if($uid == 1 ||
62 ($file_o && ($uid == $file_o->uid)) ||
63 ($file_o && webfm_file_mod_access($file_o))) {
64 //check validity of new file name
65 $file = new stdClass();
66 $file->filename = basename($dest);
67 $file->filepath = $source;
68 $file->filesize = 0; //avoid fail for size - we're interested in extensions
69 if(webfm_enum_validate($file, $err_arr) === FALSE) {
70 //illegal file extension
71 $err_arr[] = t('Invalid name');
72 return FALSE;
73 }
74 //if file is not read-only...rename
75 if(@rename($source, $dest_temp)) {
76 if(!@rename($dest_temp, $dest)) {
77 @rename($dest_temp, $source);
78 $err_arr[] = t('rename ').basename($source).t(' fail');
79 return FALSE;
80 }
81 } else {
82 $err_arr[] = basename($source).t(' read-only');
83 return FALSE;
84 }
85 } else {
86 $err_arr[] = t('permission denied');
87 return FALSE;
88 }
89 return(webfm_rename_db_file($source, $dest, $err_arr));
90 }
91
92 //Directory
93 //if target is a directory, new name is a unique path and we are an admin...
94 else if(is_dir($source) && !is_dir($dest) && ($uid == 1)) {
95 //if the target isn't read-only
96 if(@rename($source, $dest_temp)) {
97 //directory rename is OK, back out, rename db files and rename dir again
98 @rename($dest_temp, $source);
99 if(webfm_rename_db_dir_recur($source, $dest, TRUE, $err_arr)) {
100 @rename($source, $dest_temp);
101 @rename($dest_temp, $dest);
102 } else {
103 $err_arr[] = t('webfm_rename_db_dir_recur error - db may be corrupted');
104 drupal_set_message(t('webfm_rename_db_dir_recur error - db may be corrupted'), 'error');
105 return FALSE;
106 }
107 return TRUE;
108 } else {
109 $err_arr[] = $source.' read-only';
110 return FALSE;
111 }
112 }
113
114 $err_arr[] = 'rename '.array_pop(explode('/', $source)).' to '.array_pop(explode('/', $dest)).' fail';
115
116 return FALSE;
117 }
118
119 function webfm_rename_db_file($source, $dest, &$err_arr) {
120 if($fid = webfm_get_fid($source)) {
121 if(!webfm_dbupdate_file($fid, $dest)) {
122 $err_arr[] = t('webfm_dbupdate_file error - fid: ').$fid;
123 return FALSE;
124 }
125 }
126 return TRUE;
127 }
128
129 function webfm_rename_db_dir_recur($source, $dest, $ret, &$err_arr) {
130 if($handle = opendir($source)) {
131 while(($file = readdir($handle)) !== FALSE) {
132 if($file != '.' && $file != '..') {
133 $source_item = $source.'/'.$file;
134 $dest_item = $dest.'/'.$file;
135 if(is_file($source_item)) {
136 if(!(webfm_rename_db_file($source_item, $dest_item, $err_arr)))
137 $ret = FALSE;
138 } elseif(is_dir($source_item)) {
139 $ret = webfm_rename_db_dir_recur($source_item, $dest_item, $ret, $err_arr);
140 }
141 }
142 }
143 closedir($handle);
144 }
145 return $ret;
146 }
147
148 /**
149 * webfm_delete_dir_recur - called from the ajax action - switch case 'delete':
150 *
151 * @param string $source - the source directory path
152 * @param bool $ret - return value seed
153 * @param string &$err_arr - ref to error array for client feedback
154 *
155 * @ret bool -true if the directory or file is deleted and all file records updated
156 */
157 function webfm_delete_dir_recur($source, $ret, &$err_arr) {
158 if($handle = opendir($source)) {
159 while(($file = readdir($handle)) !== FALSE) {
160 if($file != '.' && $file != '..') {
161 $source_item = $source.'/'.$file;
162 if(is_file($source_item)) {
163 if(!(webfm_delete_file($source_item, $error))) {
164 $err_arr[] = $error;
165 $ret = FALSE;
166 }
167 } elseif (is_dir($source_item)) {
168 webfm_delete_dir_recur($source_item, $ret, $err_arr);
169 }
170 }
171 }
172 closedir($handle);
173 } else {
174 $err_arr[] = t('Unable to opendir ').$source;
175 return FALSE;
176 }
177 if(($retn = rmdir($source)) == FALSE)
178 $err_arr[] = t('Unable to rmdir ').$source;
179 return ($retn == TRUE && $ret == TRUE);
180 }
181
182 function webfm_delete_file($source, &$error) {
183 $file = webfm_get_file_record('', $source);
184 if(@unlink($source)) {
185 if($file && (!webfm_dbdelete_file($file->fid))) {
186 $error = t('webfm_dbdelete_file() fail for ').$source;
187 return FALSE;
188 }
189 } else if(file_exists($source)) {
190 $error = $source.t(' could not be deleted');
191 return FALSE;
192 }
193 return TRUE;
194 }
195
196 /**
197 * Move a directory or file and update database
198 *
199 * @param string $source - the source directory path
200 * @param string $dest - the destination directory name
201 * @param bool $uid - uid of user making move request
202 * @param string &$err_arr - ref to error array for client feedback
203 *
204 * @ret bool -true if any directory change (used to switch client cache)
205 *
206 */
207 function webfm_move($source, $dest, $uid, &$err_arr) {
208 $dest .= '/' . strrev(substr(strrev($source), 0, strpos(strrev($source), '/')));
209 return(webfm_rename($source, $dest, $uid, $err_arr));
210 }
211
212 /**
213 * webfm_insert_file - inserts a file into the webfm_file table
214 *
215 * @param string $path - path relative to drupal root
216 * @param string &$err_arr - ref to error array for client feedback
217 *
218 * @return bool - TRUE if query executed successfully, otherwise FALSE
219 */
220 function webfm_insert_file($path, &$err_arr){
221 if(!webfm_get_fid($path)) {
222 $file = new stdClass();
223 $file->filepath = $path;
224 if(webfm_enum_validate($file, $err_arr)) {
225 if(($ret = webfm_dbinsert_file($file, $err)) === FALSE) {
226 $err_arr[] = $err;
227 }
228 return $ret;
229 }
230 } else {
231 $err_arr[] = $path .t(' already in db');
232 }
233 return FALSE;
234 }
235
236 /**
237 * webfm_insert_dir - inserts files into the webfm_file table
238 *
239 * @param string $path - path relative to drupal root
240 * @param bool $recur - flag to indicate if recursive operation into sub-dirs
241 * @param string &$err_arr - ref to error array for client feedback
242 *
243 * @return obj - contains error flag and count of successful and failed inserts
244 */
245 function webfm_insert_dir($path, $recur, &$err_arr){
246 $result = new stdClass();
247 $result->cnt = 0;
248 $result->errcnt = 0;
249 if($handle = opendir($path)) {
250 $bl = array('.', '..', '.svn', '.htaccess');
251 while(false !== ($file = readdir($handle))) {
252 if(!in_array(strtolower($file), $bl)){
253 $file_path = $path.'/'.$file;
254 if(is_file($file_path)) {
255 // file
256 if(webfm_insert_file($file_path, $err_arr)) {
257 $result->cnt++;
258 } else {
259 $result->errcnt++;
260 }
261 } else if($recur && is_dir($file_path)) {
262 $nest_result = webfm_insert_dir($file_path, $recur, $err_arr);
263 $result->cnt += $nest_result->cnt;
264 $result->errcnt += $nest_result->errcnt;
265 }
266 }
267 }
268 closedir($handle);
269 } else {
270 $err_arr[] = t('Cannot open dir ').$path;
271 }
272 return $result;
273 }
274 ?>

  ViewVC Help
Powered by ViewVC 1.1.2