diff --git a/application/utils.inc.php b/application/utils.inc.php index 2cd05f8d3..e45cca621 100644 --- a/application/utils.inc.php +++ b/application/utils.inc.php @@ -179,37 +179,6 @@ class utils * @used-by GetAbsoluteUrlAppRoot */ private static $sAbsoluteUrlAppRootCache = null; - private static $aKnownExtensions = [ - 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', - 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', - 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template', - 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', - 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', - 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide', - 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', - 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', - 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12', - 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', - 'jpg' => 'image/jpeg', - 'jpeg' => 'image/jpeg', - 'gif' => 'image/gif', - 'png' => 'image/png', - 'pdf' => 'application/pdf', - 'doc' => 'application/msword', - 'dot' => 'application/msword', - 'xls' => 'application/vnd.ms-excel', - 'ppt' => 'application/vnd.ms-powerpoint', - 'vsd' => 'application/x-visio', - 'vdx' => 'application/visio.drawing', - 'odt' => 'application/vnd.oasis.opendocument.text', - 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', - 'odp' => 'application/vnd.oasis.opendocument.presentation', - 'zip' => 'application/zip', - 'txt' => 'text/plain', - 'htm' => 'text/html', - 'html' => 'text/html', - 'exe' => 'application/octet-stream', - ]; protected static function LoadParamFile($sParamFile) @@ -2490,42 +2459,12 @@ SQL; } $sFilePath = APPROOT.$sFilePath; - return utils::GetDocumentFromFile($sFilePath); + return ormDocument::FromFile($sFilePath); } return false; } - /** - * @param string $sPath Absolute path of the document to read - * - * @return \ormDocument - * @throws \Exception - */ - public static function GetDocumentFromFile(string $sPath):ormDocument - { - $sPath = utils::RealPath($sPath, APPROOT); - if (false === $sPath) { - throw new Exception("Failed to load the file '$sPath'. The file does not exist or the current process is not allowed to access it."); - } - $sData = @file_get_contents($sPath); - if (false === $sData) { - throw new Exception("Failed to load the file '$sPath'. The file does not exist or the current process is not allowed to access it."); - } - $sExtension = strtolower(pathinfo($sPath, PATHINFO_EXTENSION)); - $sFileName = basename($sPath); - - $sMimeType = 'text/plain'; - if (array_key_exists($sExtension, self::$aKnownExtensions)) { - $sMimeType = self::$aKnownExtensions[$sExtension]; - } else if (extension_loaded('fileinfo')) { - $fInfo = new finfo(FILEINFO_MIME); - $sMimeType = $fInfo->file($sPath); - } - - return new ormDocument($sData, $sMimeType, $sFileName); - } - /** * Read the content of a file (and retrieve its MIME type) from either: * - an URL pointing to a blob (image/document) on the current iTop server @@ -2572,7 +2511,7 @@ TXT $aHeaders = static::ParseHeaders($http_response_header); $sMimeType = array_key_exists('Content-Type', $aHeaders) ? strtolower($aHeaders['Content-Type']) : 'application/x-octet-stream'; // Compute the file extension from the MIME Type - foreach (self::$aKnownExtensions as $sExtValue => $sMime) { + foreach (ormDocument::GetKnownExtensions() as $sExtValue => $sMime) { if ($sMime === $sMimeType) { $sExtension = '.'.$sExtValue; break; @@ -2591,7 +2530,7 @@ TXT // Local file if (UserRights::IsAdministrator()) { // Only administrators are allowed to read local files - return utils::GetDocumentFromFile($sPath); + return ormDocument::FromFile($sPath); } return null; diff --git a/core/ormdocument.class.inc.php b/core/ormdocument.class.inc.php index 358fc3eea..27af2d970 100644 --- a/core/ormdocument.class.inc.php +++ b/core/ormdocument.class.inc.php @@ -48,6 +48,42 @@ class ormDocument * @since 3.1.0 */ public const DEFAULT_DOWNLOADS_COUNT = 0; + private static $aKnownExtensions = [ + 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', + 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template', + 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', + 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', + 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide', + 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', + 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12', + 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', + 'jpg' => 'image/jpeg', + 'jpeg' => 'image/jpeg', + 'gif' => 'image/gif', + 'png' => 'image/png', + 'pdf' => 'application/pdf', + 'doc' => 'application/msword', + 'dot' => 'application/msword', + 'xls' => 'application/vnd.ms-excel', + 'ppt' => 'application/vnd.ms-powerpoint', + 'vsd' => 'application/x-visio', + 'vdx' => 'application/visio.drawing', + 'odt' => 'application/vnd.oasis.opendocument.text', + 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', + 'odp' => 'application/vnd.oasis.opendocument.presentation', + 'zip' => 'application/zip', + 'txt' => 'text/plain', + 'htm' => 'text/html', + 'html' => 'text/html', + 'exe' => 'application/octet-stream', + ]; + + public static function GetKnownExtensions(): array + { + return self::$aKnownExtensions; + } protected $m_data; protected $m_sMimeType; @@ -76,6 +112,36 @@ class ormDocument $this->m_iDownloadsCount = $iDownloadsCount; } + /** + * @param string $sPath Absolute path of the document to read + * + * @return \ormDocument + * @throws \Exception + */ + public static function FromFile(string $sPath): ormDocument + { + $sPath = utils::RealPath($sPath, APPROOT); + if (false === $sPath) { + throw new Exception("Failed to load the file '$sPath'. The file does not exist or the current process is not allowed to access it."); + } + $sData = @file_get_contents($sPath); + if (false === $sData) { + throw new Exception("Failed to load the file '$sPath'. The file does not exist or the current process is not allowed to access it."); + } + $sExtension = strtolower(pathinfo($sPath, PATHINFO_EXTENSION)); + $sFileName = basename($sPath); + + $sMimeType = 'text/plain'; + if (array_key_exists($sExtension, ormDocument::$aKnownExtensions)) { + $sMimeType = ormDocument::$aKnownExtensions[$sExtension]; + } else if (extension_loaded('fileinfo')) { + $fInfo = new finfo(FILEINFO_MIME); + $sMimeType = $fInfo->file($sPath); + } + + return new ormDocument($sData, $sMimeType, $sFileName); + } + public function __toString() { if($this->IsEmpty()) return '';