| 1 |
<?php
|
| 2 |
/* $Id$ */
|
| 3 |
|
| 4 |
/**
|
| 5 |
* dbfm_mkdir -called from the ajax action - switch case 'mkdir':
|
| 6 |
* zzzzzzzzzzzz you need to update your header!!!
|
| 7 |
* @param string $source - it's the parent directory path
|
| 8 |
* @param string $dest - it's the proposed new directory name
|
| 9 |
* @param bool $rename - This flag allows a directory rename for cases where the directory already exists
|
| 10 |
* if you were trying to create a directory myfiles then it would try to rename it
|
| 11 |
* myfiles_01, myfiles_02 etc. until it found a name that hadn't been used
|
| 12 |
* @param string $error - the address of an array used to store any error messages
|
| 13 |
*
|
| 14 |
* @ret bool -true if the directory is created successfully (with 775 permissions for FS)
|
| 15 |
*/
|
| 16 |
function dbfm_mkdir($source, $dest, $rename = FALSE, &$error) {
|
| 17 |
//source is the parent directory, dest is the proposed name for our directory
|
| 18 |
$mkdir_path = ($source.'/'.$dest);
|
| 19 |
|
| 20 |
global $user;
|
| 21 |
// we've got to work out which directory node we're adding to by searching on the fpath field
|
| 22 |
$currentdate = date("Y-m-d H:i:s");
|
| 23 |
$setmime = "directory";
|
| 24 |
|
| 25 |
//we need to check first whether there's already a directory of this name, if there is then we
|
| 26 |
// simply append a number and try again ... and again, eventually we'll find an unused name
|
| 27 |
$count = 0;
|
| 28 |
|
| 29 |
$numret = 1;
|
| 30 |
$dirpath = $mkdir_path;
|
| 31 |
while ($numret) {
|
| 32 |
$parent = db_query("SELECT * FROM {dbfm_file} WHERE fpath = '%s'", $dirpath);
|
| 33 |
$parentrec = db_fetch_object($parent);
|
| 34 |
$numret = db_num_rows($parent);
|
| 35 |
if ($numret) {
|
| 36 |
$count++;
|
| 37 |
$dirpath = $mkdir_path . "_" . $count;
|
| 38 |
}
|
| 39 |
}
|
| 40 |
if ($count > 0) {
|
| 41 |
$dest = $dest . "_" . $count;
|
| 42 |
}
|
| 43 |
|
| 44 |
// find the database entry representing the parent
|
| 45 |
$parent = db_query("SELECT * FROM {dbfm_file} WHERE fpath = '%s'", $source);
|
| 46 |
$parentrec = db_fetch_object($parent);
|
| 47 |
$numret = db_num_rows($parent);
|
| 48 |
if ($numret != 1) {
|
| 49 |
//should only be one parent
|
| 50 |
$error[] = "ERROR: Unable to find parent directory " . $source;
|
| 51 |
return FALSE;
|
| 52 |
}
|
| 53 |
//increment fsize - shows the number of entries in the directory
|
| 54 |
$numdir = $parentrec->fsize + 1;
|
| 55 |
|
| 56 |
// we'll write to the database
|
| 57 |
$result = db_query("INSERT INTO {dbfm_file} (uid, fpath, fname, fsize, fmime, fcreatedate, flastmod, fparent) VALUES (%d, '%s', '%s', %d, '%s', '%s', '%s', %d)",$user->uid, $dirpath, $dest, 0, $setmime, $currentdate, $currentdate, $parentrec->fid);
|
| 58 |
if ($result === FALSE) {
|
| 59 |
$error[] = "ERROR: Failed to insert the directory " . $dest . " into the database";
|
| 60 |
return FALSE;
|
| 61 |
}
|
| 62 |
|
| 63 |
// and update the parent record - fsize stores the number of directories parented
|
| 64 |
$result = db_query("UPDATE {dbfm_file} SET `fsize` = %d WHERE fid = %d ", $numdir, $parentrec->fid);
|
| 65 |
if ($result === FALSE) {
|
| 66 |
$error[] = "ERROR: Failed to update the parent directory for " . $dest;
|
| 67 |
return FALSE;
|
| 68 |
}
|
| 69 |
//return the directory path of the newly created directory in the error array
|
| 70 |
//SUCCESS!!
|
| 71 |
$error = $dirpath;
|
| 72 |
return TRUE;
|
| 73 |
}
|
| 74 |
|
| 75 |
/**
|
| 76 |
* dbfm_rename -called from the ajax action - switch case 'rename' is this ONLY for directories??
|
| 77 |
*
|
| 78 |
* @param string $source - the source directory path
|
| 79 |
* @param string $dest - the destination directory name
|
| 80 |
* @param string &$err_arr - ref to error array for client feedback
|
| 81 |
*
|
| 82 |
* @ret bool -true if the directory/file is renamed and file record(s)
|
| 83 |
* updated if dir/file under db control
|
| 84 |
*/
|
| 85 |
function dbfm_rename($source, $dest, &$err_arr) {
|
| 86 |
|
| 87 |
//check what the $source is - a file or directory ZZZZ will need extra code here to deal with versions
|
| 88 |
$qsource = db_query("SELECT * FROM {dbfm_file} WHERE fpath = '%s'",$source);
|
| 89 |
$source_rec = db_fetch_object ($qsource);
|
| 90 |
//the source MUST already exist otherwise the user wouldn't have been able to select it
|
| 91 |
|
| 92 |
//does the destination already exist?
|
| 93 |
$qdest = db_query("SELECT * FROM {dbfm_file} WHERE fpath = '%s'",$dest);
|
| 94 |
$numret = db_num_rows($qdest);
|
| 95 |
if ($numret > 0) {
|
| 96 |
$err_arr = "Rename failed - destination " . $dest . " already exists";
|
| 97 |
return FALSE;
|
| 98 |
}
|
| 99 |
// a line to split the filename away from the file path
|
| 100 |
// reverse the path and look for the first slash. Get a substring of the reversed path from the beginning up to,
|
| 101 |
//but not including, the slash. Now reverse it to give you the filename
|
| 102 |
// hmnnn - someone not heard of strrchr? perhaps
|
| 103 |
// $filename = substr(strrchr($dest, '/'), 1); //substr allows you to start after the slash
|
| 104 |
$filename = strrev(substr(strrev($dest), 0, strpos(strrev($dest), '/')));
|
| 105 |
|
| 106 |
// simplified code - we'd need to check here for a valid name and extension, things like apostrophes etc. need catching
|
| 107 |
|
| 108 |
// i'd sure like to use a dbfm function for this but the current update function
|
| 109 |
// only changes path and doesn't deal with fname. For now I'll put some inline code
|
| 110 |
// ZZZZ I think I'll update the last modified date too
|
| 111 |
$result = db_query("UPDATE {dbfm_file} SET fpath = '%s', fname = '%s' WHERE fid = %d", $dest, $filename, $source_rec->fid);
|
| 112 |
if($result === FALSE) {
|
| 113 |
$err_arr = "ERROR: Rename failed. Couldn't change name to " . $filename;
|
| 114 |
return FALSE;
|
| 115 |
}
|
| 116 |
|
| 117 |
if ($source_rec->fmime == 'directory' && $source_rec->fsize > 0) {
|
| 118 |
//if it's a directory we need to recurse through the database changing the path for all the children
|
| 119 |
//need to update the sourec record first
|
| 120 |
$source_rec->fpath = $dest;
|
| 121 |
$ret = TRUE;
|
| 122 |
return dbfm_recur_dbrename($source_rec, $ret , $err_arr);
|
| 123 |
}
|
| 124 |
return TRUE;
|
| 125 |
|
| 126 |
}
|
| 127 |
|
| 128 |
/**
|
| 129 |
* dbfm_recur_dbrename -the function to recursively rename (actually repath) database records
|
| 130 |
*
|
| 131 |
* @param record $source - the source directory record
|
| 132 |
* @param bool $ret - the return value from the recursive function
|
| 133 |
* @param string &$err_arr - ref to error array for client feedback
|
| 134 |
*
|
| 135 |
* @ret bool -true if the directory/file is updated in the database
|
| 136 |
*
|
| 137 |
*/
|
| 138 |
|
| 139 |
//new function to recursively change the database fpath entries where a directory has been renamed or moved
|
| 140 |
//only used where storage is to the database
|
| 141 |
// the first parameter is a file record for the source directory (which has already had it's fpath changed to the correct one)
|
| 142 |
// the second parameter allows the return condition to be passed on, the third is the address of an array where error messages
|
| 143 |
// can be stored
|
| 144 |
function dbfm_recur_dbrename($source, $ret, &$err_arr) {
|
| 145 |
//look in the database for children of the directory
|
| 146 |
//for each child, re-path it. If it's a directory recurse and re-path all its children
|
| 147 |
$qkidlist = db_query("SELECT * FROM {dbfm_file} WHERE fparent = %d", $source->fid);
|
| 148 |
while ($sdata = db_fetch_object($qkidlist)) {
|
| 149 |
//update the object
|
| 150 |
$new_path = $source->fpath . "/" . $sdata->fname;
|
| 151 |
$result = db_query("UPDATE {dbfm_file} SET fpath = '%s' WHERE fid = %d", $new_path, $sdata->fid);
|
| 152 |
if ($result === FALSE) {
|
| 153 |
$err_arr[] = "ERROR: Rename to " . $new_path . " for " . $sdata->fname . " failed";
|
| 154 |
$ret = FALSE;
|
| 155 |
}
|
| 156 |
|
| 157 |
// if it's a directory, has it got any entries, if so recurse
|
| 158 |
if ($sdata->fmime == 'directory' AND $sdata->fsize > 0) {
|
| 159 |
// update the source record first
|
| 160 |
$sdata->fpath = $new_path;
|
| 161 |
$ret = dbfm_recur_dbrename($sdata, $ret, $err_arr);
|
| 162 |
}
|
| 163 |
|
| 164 |
} // end of while loop
|
| 165 |
return $ret;
|
| 166 |
}
|
| 167 |
|
| 168 |
|
| 169 |
/**
|
| 170 |
* dbfm_delete - called from the ajax action - switch case 'delete':
|
| 171 |
*
|
| 172 |
* @param string $source - the source directory path
|
| 173 |
* @param string &$err_arr - ref to error array for client feedback
|
| 174 |
*
|
| 175 |
* @ret bool -true if the directory or file is deleted and all file records updated
|
| 176 |
*/
|
| 177 |
function dbfm_delete($source, &$err_arr) {
|
| 178 |
// bring back the source record from the database
|
| 179 |
$qsource = db_query("SELECT * FROM {dbfm_file} WHERE fpath = '%s'", $source);
|
| 180 |
$numret = db_num_rows($qsource);
|
| 181 |
if ($numret != 1) {
|
| 182 |
//either no records or too many records have been found
|
| 183 |
$err_arr[] = "ERROR: Unable to recover the database entry for " . $source;
|
| 184 |
return FALSE;
|
| 185 |
}
|
| 186 |
$rec_to_delete = db_fetch_object($qsource);
|
| 187 |
|
| 188 |
// in fact there's a sneaky way of dealing with the whole thing... we'll just mark the parent record id as -1
|
| 189 |
// fill in the last modified date. then have an admin function that REALLY deletes these entries at a later date
|
| 190 |
// this means we don't need a recursive directory delete (at least not here, it will be in admin).
|
| 191 |
// Directories and their contents become invisible
|
| 192 |
$currentdate = date("Y-m-d H:i:s");
|
| 193 |
|
| 194 |
//need to update the parent record
|
| 195 |
$qparent = db_query("SELECT * FROM {dbfm_file} WHERE fid = %d", $rec_to_delete->parentid);
|
| 196 |
$parentrec = db_fetch_object($qsource);
|
| 197 |
$newfilecount = $parentrec->fsize - 1;
|
| 198 |
$result = db_query("UPDATE {dbfm_file} SET fsize = %d, flastmod = '%s' WHERE fid = %d", $newfilecount, $currentdate, $parentrec->fid);
|
| 199 |
if ($result === FALSE) {
|
| 200 |
$err_arr[] = "ERROR: Unable to update filecount for directory " . $parentrec->fname;
|
| 201 |
return FALSE;
|
| 202 |
}
|
| 203 |
|
| 204 |
$result = db_query("UPDATE {dbfm_file} SET fparent = %d, flastmod = '%s' WHERE fid = %d", '-1', $currentdate, $rec_to_delete->fid);
|
| 205 |
if ($result === FALSE) {
|
| 206 |
$err_arr[] = "ERROR: Delete Failed for " . $rec_to_delete->fname;
|
| 207 |
return FALSE;
|
| 208 |
}
|
| 209 |
return TRUE;
|
| 210 |
}
|
| 211 |
|
| 212 |
|
| 213 |
/**
|
| 214 |
* Move a directory or file and update database
|
| 215 |
*
|
| 216 |
* @param string $source - the source directory path (what you're moving)
|
| 217 |
* @param string $dest - the destination directory name (where you're moving it)
|
| 218 |
* @param string &$err_arr - ref to error array for client feedback
|
| 219 |
*
|
| 220 |
* @ret bool -true if the file and the database entries are updated successfully
|
| 221 |
*
|
| 222 |
*/
|
| 223 |
function dbfm_move($source, $dest, &$err_arr) {
|
| 224 |
|
| 225 |
//read the db record for that path
|
| 226 |
$qsource = db_query("SELECT * FROM {dbfm_file} WHERE fpath = '%s'",$source);
|
| 227 |
$source_rec = db_fetch_object($qsource);
|
| 228 |
//the source MUST already exist otherwise the user wouldn't have been able to select it
|
| 229 |
|
| 230 |
//also open the $source files current parent - we need to strip back to the last slash
|
| 231 |
$parentpath = substr($source, 0, strrpos($source, '/'));
|
| 232 |
|
| 233 |
$qoldparent = db_query("SELECT * FROM {dbfm_file} WHERE fpath = '%s'",$parentpath);
|
| 234 |
$numret = db_num_rows($qoldparent);
|
| 235 |
if ($numret != 1) {
|
| 236 |
$err_arr[] = "Move failed - unable to open parent directory " . $parentpath;
|
| 237 |
return FALSE;
|
| 238 |
}
|
| 239 |
$parent_rec = db_fetch_object($qoldparent);
|
| 240 |
|
| 241 |
//does the destination directory already exist - it should do - error if not!
|
| 242 |
$qdest = db_query("SELECT * FROM {dbfm_file} WHERE fpath = '%s'",$dest);
|
| 243 |
$numret = db_num_rows($qdest);
|
| 244 |
if ($numret != 1) {
|
| 245 |
// this can't happen! the record must exist for it to have been displayed on screen
|
| 246 |
$err_arr[] = "Move failed - destination " . $dest . " does not exist";
|
| 247 |
return FALSE;
|
| 248 |
}
|
| 249 |
|
| 250 |
// check the destination is a directory and not a file
|
| 251 |
$dest_rec = db_fetch_object ($qdest);
|
| 252 |
if ($dest_rec->fmime != 'directory') {
|
| 253 |
$err_arr[] = "Move failed - destination " . $dest . " must be a directory";
|
| 254 |
return FALSE;
|
| 255 |
}
|
| 256 |
|
| 257 |
$new_path = $dest . '/' . $source_rec->fname;
|
| 258 |
|
| 259 |
// we first check whether a file / directory of that name already exists in the destination
|
| 260 |
// directory. (that's if the directory has any entries)
|
| 261 |
if ($dest_rec->fsize > 0) {
|
| 262 |
$qdestkids = db_query("SELECT * FROM {dbfm_file} WHERE fparent = %d", $dest_rec->fid);
|
| 263 |
//look through the children in the destination directory - if one has the same name then
|
| 264 |
while ($dest_kidrec = db_fetch_object ($qdestkids)) {
|
| 265 |
if ($dest_kidrec-> fpath == $new_path) {
|
| 266 |
$err_arr[] = "ERROR: An object of that name already exists in the destination directory";
|
| 267 |
return FALSE;
|
| 268 |
}
|
| 269 |
}
|
| 270 |
}
|
| 271 |
|
| 272 |
//edit the curent record to correct the parent and change the path
|
| 273 |
$result = db_query("UPDATE {dbfm_file} SET fpath = '%s', fparent = %d WHERE fid = %d", $new_path, $dest_rec->fid, $source_rec->fid);
|
| 274 |
if ($result === FALSE) {
|
| 275 |
$err_arr[] = "ERROR: Unable to update the database path for move to " . $new_path;
|
| 276 |
return FALSE;
|
| 277 |
}
|
| 278 |
|
| 279 |
//edit the destination parent record to add one to the number of entries held in the fsize field
|
| 280 |
$result = db_query("UPDATE {dbfm_file} SET fsize = %d WHERE fid = %d", $dest_rec->fsize + 1, $dest_rec->fid);
|
| 281 |
if ($result === FALSE) {
|
| 282 |
$err_arr[] = "ERROR: Unable to update the destination parent directory " . $dest_rec->fpath;
|
| 283 |
return FALSE;
|
| 284 |
}
|
| 285 |
|
| 286 |
//edit the previous parent record to deduct one from the number of entries held in the fsize field
|
| 287 |
$result = db_query("UPDATE {dbfm_file} SET fsize = %d WHERE fid = %d", $parent_rec->fsize - 1, $parent_rec->fid);
|
| 288 |
if ($result === FALSE) {
|
| 289 |
$err_arr[] = "ERROR: Unable to update the previous parent directory " . $parentpath;
|
| 290 |
return FALSE;
|
| 291 |
}
|
| 292 |
|
| 293 |
$ret = TRUE;
|
| 294 |
|
| 295 |
//test whether the source is a directory, if so we need to recursively change all the children of the directory
|
| 296 |
if ($source_rec->fmime == 'directory' && $source_rec->fsize > 0) {
|
| 297 |
//we need to update the source record first
|
| 298 |
$source_rec->fpath = $new_path;
|
| 299 |
$ret = dbfm_recur_dbrename($source_rec, $ret, $err_arr);
|
| 300 |
}
|
| 301 |
return $ret;
|
| 302 |
}
|
| 303 |
|
| 304 |
|
| 305 |
/**
|
| 306 |
* dbfm_insert_file - inserts a file into the dbfm_file table
|
| 307 |
*
|
| 308 |
* @param string $path - path relative to drupal root
|
| 309 |
* @param string &$err_arr - ref to error array for client feedback
|
| 310 |
*
|
| 311 |
* @return bool - TRUE if query executed successfully, otherwise FALSE
|
| 312 |
*/
|
| 313 |
function dbfm_insert_file($path, &$err_arr){
|
| 314 |
$file = new stdClass();
|
| 315 |
$file->filepath = $path;
|
| 316 |
if(dbfm_enum_validate($file, $err_arr)) {
|
| 317 |
if(($ret = dbfm_dbinsert_file($file, $err)) === FALSE) {
|
| 318 |
$err_arr[] = $err;
|
| 319 |
}
|
| 320 |
return $ret;
|
| 321 |
}
|
| 322 |
return FALSE;
|
| 323 |
}
|
| 324 |
|
| 325 |
?>
|