Merge branch 'seeddms-5.1.x' into seeddms-6.0.x

This commit is contained in:
Uwe Steinmann 2022-11-17 15:24:32 +01:00
commit 330073f52a
6 changed files with 76 additions and 58 deletions

View File

@ -2325,7 +2325,7 @@ class SeedDMS_Core_Document extends SeedDMS_Core_Object { /* {{{ */
}
foreach($resArr as $res) {
$file = $this->_dms->contentDir . $this->getDir().'r'.$res['reviewLogID'];
if(file_exists($file))
if(SeedDMS_Core_File::file_exists($file))
SeedDMS_Core_File::removeFile($file);
}
}
@ -2354,7 +2354,7 @@ class SeedDMS_Core_Document extends SeedDMS_Core_Object { /* {{{ */
}
foreach($resArr as $res) {
$file = $this->_dms->contentDir . $this->getDir().'a'.$res['approveLogID'];
if(file_exists($file))
if(SeedDMS_Core_File::file_exists($file))
SeedDMS_Core_File::removeFile($file);
}
}
@ -2458,7 +2458,7 @@ class SeedDMS_Core_Document extends SeedDMS_Core_Object { /* {{{ */
return false;
}
if (file_exists( $this->_dms->contentDir.$version->getPath() ))
if (SeedDMS_Core_File::file_exists( $this->_dms->contentDir.$version->getPath() ))
if (!SeedDMS_Core_File::removeFile( $this->_dms->contentDir.$version->getPath() )) {
$db->rollbackTransaction();
return false;
@ -2820,7 +2820,7 @@ class SeedDMS_Core_Document extends SeedDMS_Core_Object { /* {{{ */
return false;
}
if (file_exists( $this->_dms->contentDir . $file->getPath() )){
if (SeedDMS_Core_File::file_exists( $this->_dms->contentDir . $file->getPath() )){
if (!SeedDMS_Core_File::removeFile( $this->_dms->contentDir . $file->getPath() )) {
$db->rollbackTransaction();
return false;
@ -2887,7 +2887,7 @@ class SeedDMS_Core_Document extends SeedDMS_Core_Object { /* {{{ */
// TODO: versioning file?
if (file_exists( $this->_dms->contentDir . $this->getDir() ))
if (SeedDMS_Core_File::file_exists( $this->_dms->contentDir . $this->getDir() ))
if (!SeedDMS_Core_File::removeDir( $this->_dms->contentDir . $this->getDir() )) {
$db->rollbackTransaction();
return false;
@ -4180,7 +4180,7 @@ class SeedDMS_Core_DocumentContent extends SeedDMS_Core_Object { /* {{{ */
}
foreach($res as &$t) {
$filename = $this->_dms->contentDir . $this->_document->getDir().'r'.$t['reviewLogID'];
if(file_exists($filename))
if(SeedDMS_Core_File::file_exists($filename))
$t['file'] = $filename;
else
$t['file'] = '';
@ -4360,7 +4360,7 @@ class SeedDMS_Core_DocumentContent extends SeedDMS_Core_Object { /* {{{ */
}
foreach($res as &$t) {
$filename = $this->_dms->contentDir . $this->_document->getDir().'a'.$t['approveLogID'];
if(file_exists($filename))
if(SeedDMS_Core_File::file_exists($filename))
$t['file'] = $filename;
else
$t['file'] = '';
@ -7046,7 +7046,7 @@ class SeedDMS_Core_DocumentContent extends SeedDMS_Core_Object { /* {{{ */
$dms = $this->_document->getDMS();
$db = $this->_dms->getDB();
if(file_exists($this->_dms->contentDir.$this->_document->getDir() . $this->_version . $this->_fileType)) {
if(SeedDMS_Core_File::file_exists($this->_dms->contentDir.$this->_document->getDir() . $this->_version . $this->_fileType)) {
if(strlen($this->_fileType) < 2) {
switch($this->_mimeType) {
case "application/pdf":
@ -7071,7 +7071,7 @@ class SeedDMS_Core_DocumentContent extends SeedDMS_Core_Object { /* {{{ */
break;
}
}
} elseif(file_exists($this->_document->getDir() . $this->_version . '.')) {
} elseif(SeedDMS_Core_File::file_exists($this->_document->getDir() . $this->_version . '.')) {
echo "no file";
} else {
echo $this->_dms->contentDir.$this->_document->getDir() . $this->_version . $this->_fileType;

View File

@ -9,19 +9,21 @@
* @author Uwe Steinmann <uwe@steinmann.cx>
* @copyright Copyright (C) 2002-2005 Markus Westphal,
* 2006-2008 Malcolm Cowe, 2010 Matteo Lucarelli,
* 2010 Uwe Steinmann
* 2010-2022 Uwe Steinmann
* @version Release: @package_version@
*/
/**
* Class to represent a user in the document management system
* Class to file operation in the document management system
* Use the methods of this class only for files below the content
* directory but not for temporäry files, cache files or log files.
*
* @category DMS
* @package SeedDMS_Core
* @author Markus Westphal, Malcolm Cowe, Uwe Steinmann <uwe@steinmann.cx>
* @copyright Copyright (C) 2002-2005 Markus Westphal,
* 2006-2008 Malcolm Cowe, 2010 Matteo Lucarelli,
* 2010 Uwe Steinmann
* 2010-2022 Uwe Steinmann
* @version Release: @package_version@
*/
class SeedDMS_Core_File {
@ -78,8 +80,37 @@ class SeedDMS_Core_File {
} /* }}} */
/**
* @param $size
* @param array $sizes
* Return the mimetype of a given file
*
* This method uses finfo to determine the mimetype
* but will correct some mimetypes which are
* not propperly determined or could be more specific, e.g. text/plain
* when it is actually text/markdown. In thoses cases
* the file extension will be taken into account.
*
* @param string $filename name of file on disc
* @return string mimetype
*/
static function mimetype($filename) { /* {{{ */
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $filename);
switch($mimetype) {
case 'application/octet-stream':
case 'text/plain':
$lastDotIndex = strrpos($filename, ".");
if($lastDotIndex === false) $fileType = ".";
else $fileType = substr($filename, $lastDotIndex);
if($fileType == '.md')
$mimetype = 'text/markdown';
break;
}
return $mimetype;
} /* }}} */
/**
* @param integer $size
* @param array $sizes list of units for 10^0, 10^3, 10^6, ..., 10^(n*3) bytes
* @return string
*/
static function format_filesize($size, $sizes = array('Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')) { /* {{{ */
@ -90,18 +121,22 @@ class SeedDMS_Core_File {
} /* }}} */
/**
* Parses a string like '[0-9]+ *[BKMGT]*' into an integer
* B,K,M,G,T stand for byte, kilo byte, mega byte, giga byte, tera byte
* If the last character is omitted, bytes are assumed.
*
* @param $str
* @return bool|int
*/
static function parse_filesize($str) { /* {{{ */
preg_replace('/\s\s+/', '', $str);
if(in_array(strtoupper(substr($str, -1)), array('B','K','M','G'))) {
$value = (int) substr($str, 0, -1);
$unit = substr($str, -1, 1);
} else {
return (int) $str;
}
switch(strtoupper($unit)) {
if(!preg_match('/^([0-9]+) *([BKMGT]*)$/', trim($str), $matches))
return false;
$value = $matches[1];
$unit = $matches[2] ? $matches[2] : 'B';
switch($unit) {
case 'T':
return $value * 1024 * 1024 * 1024 *1024;
break;
case 'G':
return $value * 1024 * 1024 * 1024;
break;
@ -112,13 +147,21 @@ class SeedDMS_Core_File {
return $value * 1024;
break;
default;
return $value;
return (int) $value;
break;
}
/** @noinspection PhpUnreachableStatementInspection */
return false;
} /* }}} */
/**
* @param $file
* @return string
*/
static function file_exists($file) { /* {{{ */
return file_exists($file);
} /* }}} */
/**
* @param $file
* @return string
@ -129,7 +172,7 @@ class SeedDMS_Core_File {
/**
* @param $string mimetype
* @return string
* @return string file extension with the dot or an empty string
*/
static function fileExtension($mimetype) { /* {{{ */
switch($mimetype) {
@ -224,6 +267,7 @@ class SeedDMS_Core_File {
'text/x-log' => 'log',
'audio/x-m4a' => 'm4a',
'application/vnd.mpegurl' => 'm4u',
'text/markdown' => 'md',
'audio/midi' => 'mid',
'application/vnd.mif' => 'mif',
'video/quicktime' => 'mov',
@ -405,7 +449,7 @@ class SeedDMS_Core_File {
continue;
else if (is_dir($path . DIRECTORY_SEPARATOR . $entry))
{
if (!self::removeDir($path . DIRECTORY_SEPARATOR . $entry . "/"))
if (!self::removeDir($path . DIRECTORY_SEPARATOR . $entry ))
return false;
}
else
@ -452,10 +496,10 @@ class SeedDMS_Core_File {
*/
static function moveDir($sourcePath, $targetPath) { /* {{{ */
/** @noinspection PhpUndefinedFunctionInspection */
if (!copyDir($sourcePath, $targetPath))
if (!self::copyDir($sourcePath, $targetPath))
return false;
/** @noinspection PhpUndefinedFunctionInspection */
return removeDir($sourcePath);
return self::removeDir($sourcePath);
} /* }}} */
// code by Kioob (php.net manual)

View File

@ -2029,6 +2029,8 @@ add method SeedDMS_Core_DatabaseAccess::setLogFp()
- fix sql error when deleting a folder attribute
- add SeedDMS_Core_Attribute::getParsedValue() and use it in SeedDMS_Core_Object::getAttributeValue()
- add SeedDMS_Core_DMS::getDuplicateSequenceNo() and SeedDMS_Core_Folder::reorderDocuments()
- add SeedDMS_Core_File::mimetype(), fix SeedDMS_Core_File::moveDir()
- all file operations use methods of SeedDMS_Core_File
</notes>
</release>
<release>

View File

@ -395,34 +395,6 @@ function getFilenameByDocname($content) { /* {{{ */
return mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $filename);
} /* }}} */
/**
* Return the mimetype of a given file
*
* This functions uses finfo but will correct some mimetypes which are
* not propperly determined or could be more specific, e.g. text/plain
* which is actually text/markdown. In thoses cases
* the file extension will be taken into account.
*
* @param string $filename name of file on disc
* @return string mimetype
*/
function getMimeType($filename) { /* {{{ */
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $filename);
switch($mimetype) {
case 'application/octet-stream':
case 'text/plain':
$lastDotIndex = strrpos($filename, ".");
if($lastDotIndex === false) $fileType = ".";
else $fileType = substr($filename, $lastDotIndex);
if($fileType == '.md')
$mimetype = 'text/markdown';
break;
}
return $mimetype;
} /* }}} */
function getLogger($prefix='', $mask=PEAR_LOG_INFO) { /* {{{ */
global $settings;

View File

@ -675,7 +675,7 @@ switch($command) {
if($content) {
$document = $content->getDocument();
if ($document->getAccessMode($user) >= M_READWRITE) {
$realmimetype = getMimeType($dms->contentDir . $content->getPath());
$realmimetype = SeedDMS_Core_File::mimetype($dms->contentDir . $content->getPath());
if (!$content->setMimeType($realmimetype)) {
header('Content-Type: application/json');
echo json_encode(array('success'=>false, 'message'=>'Error setting mimetype', 'data'=>''));

View File

@ -359,7 +359,7 @@ $(document).ready( function() {
if($file->getName() != $file->getOriginalFileName())
print "<li>".htmlspecialchars($file->getOriginalFileName())."</li>\n";
if ($file_exists) {
$realmimetype = getMimeType($dms->contentDir . $file->getPath());
$realmimetype = SeedDMS_Core_File::mimetype($dms->contentDir . $file->getPath());
print "<li>".SeedDMS_Core_File::format_filesize(filesize($dms->contentDir . $file->getPath())) ." bytes, ".htmlspecialchars($file->getMimeType())."</li>";
} else print "<li>".htmlspecialchars($file->getMimeType())." - <span class=\"warning\">".getMLText("document_deleted")."</span></li>";
@ -775,7 +775,7 @@ $(document).ready( function() {
print "<li>". SeedDMS_Core_File::format_filesize($latestContent->getFileSize()) .", ";
print htmlspecialchars($latestContent->getMimeType());
if($user->isAdmin()) {
$realmimetype = getMimeType($dms->contentDir . $latestContent->getPath());
$realmimetype = SeedDMS_Core_File::mimetype($dms->contentDir . $latestContent->getPath());
if($realmimetype != $latestContent->getMimeType())
echo " <i class=\"fa fa-exclamation-triangle ajax-click\" data-param1=\"command=setmimetype\" data-param2=\"contentid=".$latestContent->getId()."\" data-param3=\"formtoken=".createFormKey('setmimetype')."\" title=\"".htmlspecialchars($realmimetype)."\"></i> ";
}