filename = basename($source); $file->filepath = $source; $file->filesize = 0; //avoid fail for size - we're interested in extensions if(webfm_upload_validate($file, $err_arr) === FALSE) { //illegal file extension $err_arr[] = "Invalid file type"; return FALSE; } else { if(rename($source, $dest_temp)) { if(!(rename($dest_temp, $dest))) { rename($dest_temp, $source); } else if($db_check) { return(webfm_rename_db_file($source, $dest, $err_arr)); } else { return TRUE; } } } } else if(is_dir($source) && !is_dir($dest) && rename($source, $dest_temp)) { if(!(rename($dest_temp, $dest))) { rename($dest_temp, $source); $err_arr[] = 'rename '.$source.' dir fail'; return FALSE; } else if($db_check) { //directory rename is OK, back out, rename db files and rename dir again rename($dest, $dest_temp); rename($dest_temp, $source); if(webfm_rename_db_dir_recur($source, $dest, TRUE, $err_arr)) { rename($source, $dest_temp); rename($dest_temp, $dest); } else { $err_arr[] = 'webfm_rename_db_dir_recur error - db may be corrupted'; drupal_set_message(t('webfm_rename_db_dir_recur error - db may be corrupted'), error); return FALSE; } } return TRUE; } $err_arr[] = 'rename '.$source.' to '.$dest.' fail'; return FALSE; } function webfm_rename_db_file($source, $dest, &$err_arr) { if($fid = webfm_get_fid($source)) { if(!webfm_dbupdate_file($fid, $dest)) { $err_arr[] = 'webfm_dbupdate_file error - fid: '.$fid; return FALSE; } } return TRUE; } function webfm_rename_db_dir_recur($source, $dest, $ret, &$err_arr) { if($handle = opendir($source)) { while(($file = readdir($handle)) !== FALSE) { if($file != '.' && $file != '..') { $source_item = $source.'/'.$file; $dest_item = $dest.'/'.$file; if(is_file($source_item)) { if(!(webfm_rename_db_file($source_item, $dest_item, $err_arr))) $ret = FALSE; } elseif(is_dir($source_item)) { $ret = webfm_rename_db_dir_recur($source_item, $dest_item, $ret, $err_arr); } } } closedir($handle); } return $ret; } /** * webfm_delete - called from the ajax action - switch case 'delete': * * @param string $source - the source directory path * @param bool $db_check - flag for db file record check (ftp transfer = FALSE) * * @ret bool -true if the directory or file is deleted and all file records updated */ function webfm_delete($source, $db_check, &$err_arr){ if(is_dir($source)) { return webfm_delete_dir_recur($source, $db_check, TRUE, $err_arr); } else if(is_file($source)) { if(!webfm_delete_file($source, $db_check, $error)) { $err_arr[] = $error; return FALSE; } else { return TRUE; } } return FALSE; } function webfm_delete_file($source, $db_check, &$error){ if(@unlink($source)) { if($db_check) { if($fid = webfm_get_fid($source)) { if(webfm_dbdelete_file($fid)) { return TRUE; } else { $error = 'webfm_dbdelete_file() fail for '.$source; return FALSE; } } else { $error = $source.' fid not found'; // Since mixed files (in and not-in db) this is now not a fail } } else { return TRUE; } } else if(file_exists($source)) { $error = $source.' could not be deleted'; return FALSE; } return TRUE; } function webfm_delete_dir_recur($source, $db_check, $ret, &$err_arr) { if($handle = opendir($source)) { while(($file = readdir($handle)) !== FALSE) { if($file != '.' && $file != '..') { $source_item = $source.'/'.$file; if(is_file($source_item)) { if(!(webfm_delete_file($source_item, $db_check, $error))) { $err_arr[] = $error; $ret = FALSE; } } elseif (is_dir($source_item)) { webfm_delete_dir_recur($source_item, $db_check, $ret, $err_arr); } } } closedir($handle); } else { $err_arr[] = 'Unable to opendir '.$source; return FALSE; } if(($retn = rmdir($source)) == FALSE) $err_arr[] = 'Unable to rmdir '.$source; return ($retn == TRUE && $ret == TRUE); } /** * Move a directory or file and update database * * @param string $source - the source directory path * @param string $dest - the destination directory name * @param bool $db_check - flag for db file record check (ftp transfer = FALSE) * @param string &$err_arr - ref to error array for client feedback * * @ret bool -true if the file and the database entries are updated successfully * */ function webfm_move($source, $dest, $db_check, &$err_arr) { if(is_dir($source)) { return webfm_move_dir_recur($source, $dest, $db_check, TRUE, $err_arr); } elseif(is_file($source)) { $dest .= '/' . strrev(substr(strrev($source), 0, strpos(strrev($source), '/'))); return webfm_move_file($source, $dest, $db_check, $err_arr); } return FALSE; } function webfm_move_file($source, $dest, $db_check, &$err_arr) { if($db_check) { $fid = webfm_get_fid($source); if(file_move($source, $dest, FILE_EXISTS_ERROR)) { if($fid) { if(!webfm_dbupdate_file($fid, $dest)) { $err_arr[] = "db update for fid=".$fid." failed"; return FALSE; } } } else { $err_arr[] = "file_move for fid=".$fid." failed"; return FALSE; } } else { //For ftp - build a file record with current source path //update file record path to destination after successful copy $file = new stdClass(); $file->filename = basename($source); $file->filepath = $source; $file->filesize = filesize($source); if(webfm_upload_validate($file, $err_arr) === TRUE) { if(!webfm_dbinsert_file($file, $error)) { $err_arr[] = $error; return FALSE; } else { //Successful creation of webfm_files record - get fid if($fid = webfm_get_fid($source)) { //move from ftp - no delete if($ret = file_copy($source, $dest, FILE_EXISTS_RENAME)) { if(!(webfm_dbupdate_file($fid, $dest))) { $err_arr[] = "db update for fid=".$fid." failed"; return FALSE; } } else { //remove file record if unsuccessful copy _webfm_dbdelete_file_fid($fid); } } else { $err_arr[] = "webfm_get_fid for fid=".$fid." failed"; return FALSE; } } } else { return FALSE; } } return TRUE; } function webfm_move_dir_recur($source, $dest, $db_check, $ret, &$err_arr) { $dest .= '/' . strrev(substr(strrev($source), 0, strpos(strrev($source), '/'))); if(!is_dir($dest)) { if(!(@mkdir($dest))) { return FALSE; } chmod($dest, 0775); if (!is_writable($dest)) { $err_arr[] = $dest." is not writable"; return FALSE; } } else { //directory already exists (will only occur at topmost directory) $err_arr[] = $dest." directory already exists"; return FALSE; } if($handle = opendir($source)) { while(false !== ($file = readdir($handle))) { if($file != '.' && $file != '..') { $source_item = $source.'/'.$file; if(is_file($source_item)) { // file if(!(webfm_move_file($source_item, $dest.'/'.$file, $db_check, $err_arr))) $ret = FALSE; } elseif(is_dir($source_item)) { // directory $ret = webfm_move_dir_recur($source_item, $dest, $db_check, $ret, $err_arr); } } } closedir($handle); } else { return FALSE; } if($db_check) //Do not delete ftp directories (permission err) @rmdir($source); return $ret; } /** * webfm_insert_file - inserts a file into the webfm_file table * * @param string $path - path relative to drupal root * @param string &$err_arr - ref to error array for client feedback * * @return bool - TRUE if query executed successfully, otherwise FALSE */ function webfm_insert_file($path, &$err_arr){ $file = new stdClass(); $file->filepath = $path; if(webfm_enum_validate($file, $err_arr)) { if(($ret = webfm_dbinsert_file($file, $err)) === FALSE) { $err_arr[] = $err; } return $ret; } return FALSE; } /** * webfm_insert_dir - inserts files into the webfm_file table * * @param string $path - path relative to drupal root * @param bool $recur - flag to indicate if recursive operation into sub-dirs * @param string &$err_arr - ref to error array for client feedback * * @return obj - contains error flag and count of successful and failed inserts */ function webfm_insert_dir($path, $recur, &$err_arr){ $result = new stdClass(); $result->cnt = 0; $result->errcnt = 0; if($handle = opendir($path)) { $bl = array('.', '..', '.svn', '.htaccess'); while(false !== ($file = readdir($handle))) { if(!in_array(strtolower($file), $bl)){ $file_path = $path.'/'.$file; if(is_file($file_path)) { // file if(!webfm_get_fid($file_path)) { // not in db if(webfm_insert_file($file_path, $err_arr)) { $result->cnt++; } else { $result->errcnt++; } } } else if($recur && is_dir($file_path)) { $nest_result = webfm_insert_dir($file_path, $recur, $err_arr); $result->cnt += $nest_result->cnt; $result->errcnt += $nest_result->errcnt; } } } closedir($handle); } else { $err_arr[] = 'Cannot open dir '.$path; } return $result; } ?>