|
Server : Apache/2.4.62 System : FreeBSD fbsdweb2.web.rcn.net 14.1-RELEASE FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC amd64 User : www ( 80) PHP Version : 8.3.8 Disable Function : NONE Directory : /domains/simes/unused/fCMSBackend/includes/classes/ |
Upload File : |
<?php
// name: UD - UploadDownload class
// file: UD.class.php
class UD {
var $rootDir;
var $allowedExtensions;
var $readOnlyFiles;
var $maxFileSize;
var $uploadedFiles;
var $maxRecursionDirs;
var $restrictedDirs = Array();
var $fileContent;
var $createImageThumb = false;
var $thumbDirName = "_thumb";
var $thumbImageExtension = Array("jpg", "jpeg", "gif", "png");
var $thumbHeight = 100;
var $thumbWidth = 100;
var $thumbKeepAspectRation = true;
var $forceJpgConversion = false;
var $_recursionDirs;
var $_err;
function UD($rDir=false){
if(!$rDir)
$dir = $_SERVER['DOCUMENT_ROOT'];
// set main properties
$this->rootDir = $this->_addSlash($rDir);
$this->allowedExtensions = array();
$this->readOnlyFiles = false;
$this->maxFileSize = 0;
$this->uploadedFiles = array();
// initialize Error Message
$this->_initErrorMsg();
} // UD function end
# ----------------------------------------------------------
function getFoldersAndFiles($fileDir=""){
$FaF = array();
$pathToDir = $this->_addRootPathToFolder($fileDir);
//echo $pathToDir;
if (!is_dir($pathToDir)){
return -1;
}
if (!($dh = opendir($pathToDir)))
return -2;
$i=0;
while (($file = readdir($dh)) !== false) {
$F[name] = $file;
// remove '.' and '..' if root
if($pathToDir == $this->_addSlash($this->rootDir) && ($F[name]=='.' || $F[name]=='..'))
continue;
$F[type] = filetype($pathToDir.$file);
// if read only files
if($this->readOnlyFiles && $F[type]=="dir")
continue;
// filter restricted
if(in_array($F[name], $this->restrictedDirs) && $F[type]=="dir")
continue;
// get extension
if($F[type]=='file'){
$F[extension] = $this->_getExtension($file);
}
else if($F[type]=='dir'){
unset($F[extension]);
}
// filter files
if($F[type]=="file"){
if(count($this->allowedExtensions)>0){
if(in_array($F[extension], $this->allowedExtensions) == FALSE)
continue;
}
}
$F[chmod] = $this->_getPermision($pathToDir.$file);
$F[size] = filesize($pathToDir.$file);
$F[type] = filetype($pathToDir.$file);
$FaF[$i]=$F;
$i++;
}
closedir($dh);
return $FaF;
} // getFoldersAndFiles function end
# ----------------------------------------------------------
function readFile($fileDir){
$pathToFile = $this->_addRootPathToFile($fileDir);
readfile($pathToFile);
die;
}
# ----------------------------------------------------------
function getFileContent($fileDir){
$this->fileContent = "";
$pathToFile = $this->_addRootPathToFile($fileDir);
if(!is_file($pathToFile)){
return -16;
}
$fp = fopen($pathToFile,"r", true);
$this->fileContent = fread($fp, filesize($pathToFile));
fclose($fp);
# return file contents
return true;
}
# ----------------------------------------------------------
function saveFileContent($fileDir, $fileContent){
$pathToFile = $this->_addRootPathToFile($fileDir);
if(!is_file($pathToFile)){
return -16;
}
$fp = fopen($pathToFile,"w", true);
fwrite($fp, $fileContent);
fclose($fp);
# return file contents
return true;
}
# ----------------------------------------------------------
function deleteFolder($fileDir=""){
$pathToDir = $this->_addRootPathToFolder($fileDir);
if($pathToDir == $this->rootDir){
return -4;
}
if(!is_dir($pathToDir)){
return -1;
}
if(@rmdir($pathToDir)===false){
return -3;
}
return true;
}
# ----------------------------------------------------------
function deleteFile($fileDir){
$pathToFile = $this->_addRootPathToFile($fileDir);
if(!is_file($pathToFile)){
return -16;
}
if(@unlink($pathToFile)===false){
return -17;
}
return true;
}
# ----------------------------------------------------------
function renameFile($pathToFile, $newFilePath){
$pathToFile = $this->_addRootPathToFile($pathToFile);
$newFilePath = $this->_addRootPathToFile($newFilePath);
if(!is_file($pathToFile)){
return -16;
}
if(is_dir($newFilePath)){
return -18;
}
if (!in_array($this->_getExtension($newFilePath), $this->allowedExtensions) && !empty($this->allowedExtensions)){
return -20;
}
if(@rename($pathToFile, $newFilePath)===false){
return -19;
}
return true;
}
# ----------------------------------------------------------
function renameFolder($pathToDir, $newDirPath){
$pathToDir = $this->_addRootPathToFolder($pathToDir);
$newDirPath = $this->_addRootPathToFolder($newDirPath);
if($pathToDir == $this->rootDir){
return -8;
}
if(!is_dir($pathToDir)){
return -1;
}
if(is_dir($newDirPath)){
return -5;
}
if(@rename($pathToDir, $newDirPath)===false){
return -6;
}
return true;
}
# ----------------------------------------------------------
function addFolder($newDirPath){
$newDirPath = $this->_addRootPathToFolder($newDirPath);
if(is_dir($newDirPath)){
return -5;
}
if(mkdir($newDirPath)===false){
return -7;
}
chmod($newDirPath, 0777);
return true;
}
# ----------------------------------------------------------
function uploadFiles($fileDir, $imageQuality=75){
$pathToDir = $this->_addRootPathToFolder($fileDir);
if (!is_dir($pathToDir)){
return -1;
}
$i=-1;
foreach ($_FILES as $varName => $file) {
$i++;
$fileName = $file['name'];
if (empty($fileName)) {
$this->uploadedFiles[$i]['status']=-15;
continue;
}
$i=count($this->uploadedFiles);
$this->uploadedFiles[$i]['name'] = $fileName;
if (!is_uploaded_file($file['tmp_name'])) {
$this->uploadedFiles[$i]['status']=-14;
continue;
}
if ($file['size'] > $this->maxFileSize && $this->maxFileSize!=0){
$this->uploadedFiles[$i]['status']=-11;
continue;
}
if (!in_array($this->_getExtension($fileName), $this->allowedExtensions) && !empty($this->allowedExtensions)){
$this->uploadedFiles[$i]['status']=-12;
continue;
}
$_fpath = str_replace("//","/",$pathToDir.$fileName);
if (!move_uploaded_file($file['tmp_name'], $_fpath)){
$this->uploadedFiles[$i]['status']=-13;
continue;
}
chmod($_fpath, 0777);
// convert to jpg image
if($this->forceJpgConversion){
$fileName = $this->convertToJpg(str_replace("//","/",$pathToDir.$fileName), $imageQuality);
if($fileName===false){
$this->uploadedFiles[$i]['status'] = -30;
continue;
}
$this->uploadedFiles[$i]['name'] = $fileName;
}
$this->uploadedFiles[$i]['status']=1;
// create thumbnails
if($this->createImageThumb && in_array($this->_getExtension($fileName), $this->thumbImageExtension)){
$this->addFolder($fileDir."/".$this->thumbDirName);
$this->createThumbnail($pathToDir.$fileName, $pathToDir.$this->thumbDirName."/".$fileName, $this->thumbWidth, $this->thumbHeight, $this->thumbKeepAspectRation);
}
}
return true;
} // uploadFiles function end
# ----------------------------------------------------------
function getTreeArray($relPath="/"){
$this->_getTreeArray($relPath, $this->_recursionDirs);
return $this->_recursionDirs;
}
# ----------------------------------------------------------
function convertToJpg($filePath, $imageQuality){
$extenzion = $this->_getExtension($filePath);
if($extenzion == "jpg" || $extenzion == "jpeg")
$im = imagecreatefromjpeg($filePath);
else if($extenzion == "gif")
$im = imagecreatefromgif($filePath);
else if($extenzion == "png")
$im = imagecreatefrompng($filePath);
else
return false;
$newFilePath = substr($filePath, 0,strrpos($filePath, ".")) . ".jpg";
imageJPEG($im, $newFilePath, $imageQuality);
chmod($newFilePath, 0777);
if($this->_getExtensionCS($filePath)!="jpg")
@unlink($filePath);
$newFileName = substr($newFilePath, strrpos($filePath, "/")+1);
return $newFileName;
}
# ----------------------------------------------------------
function createThumbnail($filePath, $thumbFilePath, $maxWidth, $maxHeigth, $keepAspectRatio=false){
$heigth = $maxHeigth;
$width = $maxWidth;
$extenzion = $this->_getExtension($filePath);
if($extenzion == "jpg" || $extenzion == "jpeg")
$im = imagecreatefromjpeg($filePath);
else if($extenzion == "gif")
$im = imagecreatefromgif($filePath);
else if($extenzion == "png")
$im = imagecreatefrompng($filePath);
else
return false;
$imWidth = imagesx($im);
$imHeigth = imagesy($im);
if($imHeigth<=$maxHeigth && $imWidth<=$maxWidth){
$heigth = $imHeigth;
$width = $imWidth;
}
else{
if($keepAspectRatio){
if(($imWidth/$maxWidth)>($imHeigth/$maxHeigth)){
$heigth = round($imHeigth/($imWidth/$maxWidth));
}
else{
$width = round($imWidth/($imHeigth/$maxHeigth));
}
}
}
$imThumb = ImageCreateTrueColor($width,$heigth);
@imagecopyresized ($imThumb, $im, 0, 0, 0, 0, $width, $heigth, $imWidth, $imHeigth);
if($extenzion == "jpg" || $extenzion == "jpeg")
imageJPEG($imThumb, $thumbFilePath, 70);
if($extenzion == "gif")
imageGIF($imThumb, $thumbFilePath);
if($extenzion == "png")
imagePNG($imThumb, $thumbFilePath);
else
return false;
chmod($thumbFilePath, 0777);
return true;
}
# ----------------------------------------------------------
function getErrorMsg($e){
$id = $this->getErrorID($e);
return $this->_err["$id"];
} // getErrorMsg function end
# ----------------------------------------------------------
function getErrorID($e){
$e=abs($e);
strlen($e)==1 ? $id="00".$e : (strlen($e)==2 ? $id="0".$e : true);
return $id;
} // getErrorID function end
function formatSize($bytes){
if($bytes/1024 < 1 )
return $bytes;
if($bytes/(1024*1024) < 1 ){
return round($bytes/(1024)) . "K";
}
if($bytes/(1024*1024*1024) < 1 ){
return round($bytes/(1024*1024)) . "M";
}
if($bytes/(1024*1024*1024*1024) < 1 ){
return round($bytes/(1024*1024*1024)) . "G";
}
}
////////////////////////////////////////////////////////////
# ----------------------------------------------------------
// Private
# ----------------------------------------------------------
function _getPermision($file){
$chmod = fileperms($file);
$chmod = sprintf('%o', $chmod);
$chmod = substr($chmod,-3);
return $chmod;
} // _getPermision function end
# ----------------------------------------------------------
function _addSlash($dir){
if(empty($dir))
return "";
if(substr($dir,-1)!="/")
$dir.="/";
$dir = str_replace("//","/",$dir);
return $dir;
} // _addSlash function end
# ----------------------------------------------------------
function _addRootPathToFile($fileDir){
$pathToFile = $this->_addSlash($this->rootDir) . $fileDir;
$pathToFile = str_replace("//","/",$pathToFile);
return $pathToFile;
}
function _addRootPathToFolder($folderDir){
$pathToDir = $this->_addSlash($this->rootDir) . $this->_addSlash($folderDir);
$pathToDir = str_replace("//","/",$pathToDir);
return $pathToDir;
}
# ----------------------------------------------------------
function _getExtensionCS($file){
$pos = strrpos($file, ".");
if ($pos === false) {
return "";
}
$extension = substr($file, $pos+1);
return $extension;
} // _getExtension function end
# ----------------------------------------------------------
function _getExtension($file){
$file = $this->_getExtensionCS($file);
$extension = strtolower($file);
return $extension;
} // _getExtension function end
# ----------------------------------------------------------
function _initErrorMsg(){
$this->_err = array();
$this->_err["001"] = "NotDir";
$this->_err["002"] = "ErrorOpenningDir";
$this->_err["003"] = "DirNotEmpty";
$this->_err["004"] = "CantDeleteRoot";
$this->_err["005"] = "DirAlreadyExists";
$this->_err["006"] = "CantRenameDir";
$this->_err["007"] = "CantCreateDir";
$this->_err["008"] = "CantRenameRoot";
$this->_err["011"] = "FileSizeOverflow";
$this->_err["012"] = "ExtensionBlocked";
$this->_err["013"] = "CantMoveFile";
$this->_err["014"] = "UploadFailed";
$this->_err["015"] = "EmptyFilename";
$this->_err["016"] = "NotFile";
$this->_err["017"] = "CantDeleteFile";
$this->_err["018"] = "FileExists";
$this->_err["019"] = "CantRenameFile";
$this->_err["020"] = "ExtensionBlocked";
$this->_err["030"] = "Can't convert to jpg image";
} // _initErrorMsg function end
function _getTreeArray($relPath, &$arr, $aktinst = 0){
$ff = $this->getFoldersAndFiles($relPath);
if($ff<0) return;
$arr = $ff;
while (list($key, $val) = each($ff)){
if ($val['type']=="dir" and $val['name']!="." and $val['name']!=".."){
$_relPath = $relPath;
$_relPath .= str_replace("//","/","/".$val['name']."/");
$absPath = $this->_addRootPathToFolder($_relPath) ;
if (is_dir($absPath) && ($aktinst < $this->maxRecursionDirs || $this->maxRecursionDirs == 0)){
$arr[$key][folders] = array();
$this->_getTreeArray($_relPath, $arr[$key][folders],$aktinst+1);
}
}
}
}
} // UD class end
?>