From c2bdd6134be53dcf822632192af10cf3cf80be1e Mon Sep 17 00:00:00 2001 From: Robin Date: Sun, 25 Apr 2010 14:21:04 +0200 Subject: some cleanup/refactoring --- inc/lib_files.php | 309 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 187 insertions(+), 122 deletions(-) (limited to 'inc/lib_files.php') diff --git a/inc/lib_files.php b/inc/lib_files.php index 9c6cb25346a..a4e1c6a5a4f 100755 --- a/inc/lib_files.php +++ b/inc/lib_files.php @@ -29,132 +29,197 @@ */ class OC_FILES { - /** - * show a web GUI filebrowser - * - * @param basedir $basedir - * @param dir $dir - */ - public static function showbrowser($basedir,$dir){/* - global $CONFIG_DATEFORMAT; - global $WEBROOT; - - $directory=$basedir.'/'.$dir; - - // exit if try to access files outside our directory - if(strstr($dir,'..')<>false) exit(); - $directory=realpath($directory); - - $dirs=explode('/',$dir); - - // breadcrumb - if(count($dirs)>1) { - echo('
'); - echo(''); - $currentdir=''; - foreach($dirs as $d) { - $currentdir.='/'.$d.''; - if($d<>'') echo(''); - } - echo('
home '.$d.'
'); - } + /** + * show a web GUI filebrowser + * + * @param basedir $basedir + * @param dir $dir + */ + public static function showbrowser($basedir,$dir){ + echo '
'; + } + + /** + * get the content of a directory + * @param dir $directory + */ + public static function getdirectorycontent($directory){ + $filesfound=true; + $content=array(); + $dirs=array(); + $file=array(); + $files=array(); + if (is_dir($directory)) { + if ($dh = opendir($directory)) { + while (($filename = readdir($dh)) !== false) { + if($filename<>'.' and $filename<>'..'){ + $file=array(); + $filesfound=true; + $file['name']=$filename; + $file['directory']=$directory; + $stat=stat($directory.'/'.$filename); + $file=array_merge($file,$stat); + $file['type']=filetype($directory .'/'. $filename); + if($file['type']=='dir'){ + $dirs[$file['name']]=$file; + }else{ + $files[$file['name']]=$file; + } + } + } + closedir($dh); + } + } + ksort($dirs); + ksort($files); + $content=array_merge($dirs,$files); + if($filesfound){ + return $content; + }else{ + return false; + } + } + + + + /** + * return the content of a file or return a zip file containning multiply files + * + * @param dir $dir + * @param file $file + */ + public static function get($dir,$files){ + global $CONFIG_DATADIRECTORY; + if(strstr($files,'..') or strstr($dir,'..')){ + die(); + } + if(is_array($files)){ + $zip = new ZipArchive(); + $filename = sys_get_temp_dir()."/ownCloud.zip"; + if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { + exit("cannot open <$filename>\n"); + } + foreach($files as $file){ + $file=$CONFIG_DATADIRECTORY.'/'.$dir.'/'.$file; + if(is_file($file)){ + $zip->addFile($file,basename($file)); + }elseif(is_dir($file)){ + zipAddDir($file,$zip); + } + } + $zip->close(); + }elseif(is_dir($CONFIG_DATADIRECTORY.'/'.$dir.'/'.$files)){ + $zip = new ZipArchive(); + $filename = sys_get_temp_dir()."/ownCloud.zip"; + if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { + exit("cannot open <$filename>\n"); + } + $file=$CONFIG_DATADIRECTORY.'/'.$dir.'/'.$files; + zipAddDir($file,$zip); + $zip->close(); + }else{ + $zip=false; + $filename=$CONFIG_DATADIRECTORY.'/'.$dir.'/'.$files; + } + header('Content-Description: File Transfer'); + header('Content-Type: application/octet-stream'); + header('Content-Disposition: attachment; filename='.basename($filename)); + header('Content-Transfer-Encoding: binary'); + header('Expires: 0'); + header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); + header('Pragma: public'); + header('Content-Length: ' . filesize($filename)); + ob_end_clean(); + readfile($filename); + if($zip){ + unlink($filename); + } + } + + /** + * move a file or folder + * + * @param dir $sourceDir + * @param file $source + * @param dir $targetDir + * @param file $target + */ + public static function move($sourceDir,$source,$targetDir,$target){ + global $CONFIG_DATADIRECTORY; + if(OC_USER::isLoggedIn() and strpos($sourceDir,'..')===false and strpos($source,'..')===false and strpos($targetDir,'..')===false and strpos($target,'..')===false){ + $targetFile=$CONFIG_DATADIRECTORY.'/'.$targetDir.'/'.$target; + $sourceFile=$CONFIG_DATADIRECTORY.'/'.$sourceDir.'/'.$source; + rename($sourceFile,$targetFile); + } + } + + /** + * create a new file or folder + * + * @param dir $dir + * @param file $name + * @param type $type + */ + public static function newfile($dir,$name,$type){ + global $CONFIG_DATADIRECTORY; + if(OC_USER::isLoggedIn() and strpos($dir,'..')===false and strpos($name,'..')===false){ + $file=$CONFIG_DATADIRECTORY.'/'.$dir.'/'.$name; + if($type=='dir'){ + mkdir($file); + }elseif($type=='file'){ + $fileHandle=fopen($file, 'w') or die("can't open file"); + fclose($fileHandle); + } + } + } + + /** + * deletes a file or folder + * + * @param dir $dir + * @param file $name + */ + public static function delete($dir,$file){ + global $CONFIG_DATADIRECTORY; + if(OC_USER::isLoggedIn() and strpos($dir,'..')===false){ + $file=$CONFIG_DATADIRECTORY.'/'.$dir.'/'.$file; + if(is_file($file)){ + unlink($file); + }elseif(is_dir($file)){ + rmdir($file); + } + } + } +} - // files and directories - echo('
'); - $filesfound=false; - $content=self::getdirectorycontent($directory); - if($content){ - foreach($content as $file){ - echo(''); - OC_UTIL::showicon($file['type']); - if($file['type']=='dir') echo(''); - if($file['type']<>'dir') echo(''); - if($file['type']<>'dir') echo(''); else echo(''); - echo(''); - echo(''); - } - } - echo('
'.$file['name'].''.$file['name'].''.$file['size'].' byte'.date($CONFIG_DATEFORMAT,$file['mtime']).'
'); - if(!$content) echo('

no files here

'); - echo('
');*/ - echo '
'; - } - - /** - * get the content of a directory - * @param dir $directory - */ - public static function getdirectorycontent($directory){ - $filesfound=true; - $content=array(); - $dirs=array(); - $file=array(); - $files=array(); - if (is_dir($directory)) { - if ($dh = opendir($directory)) { - while (($filename = readdir($dh)) !== false) { - if($filename<>'.' and $filename<>'..'){ - $file=array(); - $filesfound=true; - $file['name']=$filename; - $file['directory']=$directory; - $stat=stat($directory.'/'.$filename); - $file=array_merge($file,$stat); - $file['type']=filetype($directory .'/'. $filename); - if($file['type']=='dir'){ - $dirs[$file['name']]=$file; - }else{ - $files[$file['name']]=$file; - } - } +function zipAddDir($dir,$zip,$internalDir=''){ + $dirname=basename($dir); + $zip->addEmptyDir($internalDir.$dirname); + $internalDir.=$dirname.='/'; + $files=OC_FILES::getdirectorycontent($dir); + foreach($files as $file){ + $filename=$file['name']; + $file=$dir.'/'.$filename; + if(is_file($file)){ + $zip->addFile($file,$internalDir.$filename); + }elseif(is_dir($file)){ + zipAddDir($file,$zip,$internalDir); } - closedir($dh); - } - } - ksort($dirs); - ksort($files); - $content=array_merge($dirs,$files); - if($filesfound){ - return $content; - }else{ - return false; } - } - - - - /** - * return the cntent of a file - * - * @param dir $dir - * @param file $file - */ - public static function get($dir,$file){ - if(isset($_SESSION['username']) and $_SESSION['username']<>'') { - global $CONFIG_DATADIRECTORY; - $filename=$CONFIG_DATADIRECTORY.'/'.$dir.'/'.$file; - - // exit if try to access files outside our directory - if(strstr($filename,'..')<>false) exit(); - - OC_LOG::event($_SESSION['username'],3,$dir.'/'.$file); +} - header('Content-Description: File Transfer'); - header('Content-Type: application/octet-stream'); - header('Content-Disposition: attachment; filename='.basename($file)); - header('Content-Transfer-Encoding: binary'); - header('Expires: 0'); - header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); - header('Pragma: public'); - header('Content-Length: ' . filesize($filename)); - readfile($filename); +if(!function_exists('sys_get_temp_dir')) { + function sys_get_temp_dir() { + if( $temp=getenv('TMP') ) return $temp; + if( $temp=getenv('TEMP') ) return $temp; + if( $temp=getenv('TMPDIR') ) return $temp; + $temp=tempnam(__FILE__,''); + if (file_exists($temp)) { + unlink($temp); + return dirname($temp); + } + return null; } - exit; - } - - } - - -?> +?> \ No newline at end of file -- cgit v1.2.3 From afc0ef420b58b92e38ea600c2eac972ed132fbac Mon Sep 17 00:00:00 2001 From: Robin Date: Sun, 25 Apr 2010 15:04:13 +0200 Subject: bug fix when starting multiply uploads while the old ones arent finsihed, detect file actions on mimetype not on extention --- files/upload.php | 2 ++ inc/lib_files.php | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ js/filebrowser.js | 6 ++--- js/lib_files.js | 48 ++++++++++++++++++++++++++-------------- 4 files changed, 102 insertions(+), 20 deletions(-) (limited to 'inc/lib_files.php') diff --git a/files/upload.php b/files/upload.php index b5fed2ed5b1..0aa435cad6f 100644 --- a/files/upload.php +++ b/files/upload.php @@ -22,6 +22,8 @@ */ require_once('../inc/lib_base.php'); +// sleep(5); //immitate slow internet. + $fileName=$_FILES['file']['name']; $source=$_FILES['file']['tmp_name']; $target=$CONFIG_DATADIRECTORY.'/'.$_GET['dir'].'/'.$fileName; diff --git a/inc/lib_files.php b/inc/lib_files.php index a4e1c6a5a4f..6188723c028 100755 --- a/inc/lib_files.php +++ b/inc/lib_files.php @@ -59,6 +59,7 @@ class OC_FILES { $file['directory']=$directory; $stat=stat($directory.'/'.$filename); $file=array_merge($file,$stat); + $file['mime']=OC_FILES::getMimeType($directory .'/'. $filename); $file['type']=filetype($directory .'/'. $filename); if($file['type']=='dir'){ $dirs[$file['name']]=$file; @@ -190,6 +191,71 @@ class OC_FILES { } } } + + /** + * try to detect the mime type of a file + * + * @param string file path + * @return string guessed mime type + */ + function getMimeType($fspath){ + if (@is_dir($fspath)) { + // directories are easy + return "httpd/unix-directory"; + } else if (function_exists("mime_content_type")) { + // use mime magic extension if available + $mime_type = mime_content_type($fspath); + } else if ($this->_can_execute("file")) { + // it looks like we have a 'file' command, + // lets see it it does have mime support + $fp = popen("file -i '$fspath' 2>/dev/null", "r"); + $reply = fgets($fp); + pclose($fp); + + // popen will not return an error if the binary was not found + // and find may not have mime support using "-i" + // so we test the format of the returned string + + // the reply begins with the requested filename + if (!strncmp($reply, "$fspath: ", strlen($fspath)+2)) { + $reply = substr($reply, strlen($fspath)+2); + // followed by the mime type (maybe including options) + if (preg_match('/^[[:alnum:]_-]+/[[:alnum:]_-]+;?.*/', $reply, $matches)) { + $mime_type = $matches[0]; + } + } + } + if (empty($mime_type)) { + // Fallback solution: try to guess the type by the file extension + // TODO: add more ... + switch (strtolower(strrchr(basename($fspath), "."))) { + case ".html": + $mime_type = "text/html"; + break; + case ".txt": + $mime_type = "text/plain"; + break; + case ".css": + $mime_type = "text/css"; + break; + case ".gif": + $mime_type = "image/gif"; + break; + case ".jpg": + $mime_type = "image/jpeg"; + break; + case ".jpg": + $mime_type = "png/jpeg"; + break; + default: + $mime_type = "application/octet-stream"; + break; + } + } + + return $mime_type; + } + } function zipAddDir($dir,$zip,$internalDir=''){ diff --git a/js/filebrowser.js b/js/filebrowser.js index f12cec44143..cc03fe33156 100644 --- a/js/filebrowser.js +++ b/js/filebrowser.js @@ -129,16 +129,16 @@ OC_FILES.browser.files.show=function(parent,fileList){ for(name in fileList){ file=fileList[name]; if(!OC_FILES.browser.files.fileNodes[file.name]){ - OC_FILES.browser.files.add(file.name,file.type,file.size,file.date); + OC_FILES.browser.files.add(file.name,file.type,file.size,file.date,file.mime); } } } } -OC_FILES.browser.files.add=function(name,type,size,date){ +OC_FILES.browser.files.add=function(name,type,size,date,mime){ if(name){ if(!size) size=0; if(!date) date=getTimeString(); - OC_FILES.files[name]=new OC_FILES.file(OC_FILES.dir,name,type); + OC_FILES.files[name]=new OC_FILES.file(OC_FILES.dir,name,type,mime); tr=document.createElement('tr'); OC_FILES.browser.files.fileNodes[name]=tr; OC_FILES.browser.files.tbody.appendChild(tr); diff --git a/js/lib_files.js b/js/lib_files.js index 7c23ee16a9d..f60b399746d 100644 --- a/js/lib_files.js +++ b/js/lib_files.js @@ -49,7 +49,7 @@ OC_FILES.getdirectorycontent_parse=function(req){ if(fileElements.length>0){ for(index=0;index Date: Sun, 25 Apr 2010 18:27:02 +0200 Subject: bug fix in mimetype detect code --- inc/lib_files.php | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'inc/lib_files.php') diff --git a/inc/lib_files.php b/inc/lib_files.php index 6188723c028..2e77067b4a7 100755 --- a/inc/lib_files.php +++ b/inc/lib_files.php @@ -205,7 +205,7 @@ class OC_FILES { } else if (function_exists("mime_content_type")) { // use mime magic extension if available $mime_type = mime_content_type($fspath); - } else if ($this->_can_execute("file")) { + } else if (OC_FILES::canExecute("file")) { // it looks like we have a 'file' command, // lets see it it does have mime support $fp = popen("file -i '$fspath' 2>/dev/null", "r"); @@ -255,6 +255,49 @@ class OC_FILES { return $mime_type; } + + /** + * detect if a given program is found in the search PATH + * + * helper function used by _mimetype() to detect if the + * external 'file' utility is available + * + * @param string program name + * @param string optional search path, defaults to $PATH + * @return bool true if executable program found in path + */ + function canExecute($name, $path = false) + { + // path defaults to PATH from environment if not set + if ($path === false) { + $path = getenv("PATH"); + } + + // check method depends on operating system + if (!strncmp(PHP_OS, "WIN", 3)) { + // on Windows an appropriate COM or EXE file needs to exist + $exts = array(".exe", ".com"); + $check_fn = "file_exists"; + } else { + // anywhere else we look for an executable file of that name + $exts = array(""); + $check_fn = "is_executable"; + } + + // now check the directories in the path for the program + foreach (explode(PATH_SEPARATOR, $path) as $dir) { + // skip invalid path entries + if (!file_exists($dir)) continue; + if (!is_dir($dir)) continue; + + // and now look for the file + foreach ($exts as $ext) { + if ($check_fn("$dir/$name".$ext)) return true; + } + } + + return false; + } } -- cgit v1.2.3