/** * Returns the icon file path for a file type icon for a given file. * $mimeType = tx_dam::file_getType($filename); * * @param array $mimeType Describes the type of a file. Can be meta record array or array from tx_dam::file_getType(). * @param boolean $absolutePath If set the path to the icon is absolute. By default it's relative to typo3/ folder. * @param string $mode TYPO3_MODE to be used: 'FE', 'BE'. Constant TYPO3_MODE is default. * @return string Icon image file path * @see tx_dam::file_getType() */ function icon_getFileType($mimeType, $absolutePath = false, $mode = TYPO3_MODE) { static $iconCache = array(); static $iconCacheRel = array(); $iconfile = false; // first see if the icon is in the icon cache if (is_array($mimeType)) { if (!$absolutePath && ($cached = $iconCacheRel[$mimeType['file_type']])) { return $cached; } elseif ($cached = $iconCache[$mimeType['file_type']]) { $iconfile = $cached; } else { foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dam']['fileIconPaths_' . $mode] as $pathIcons) { // Check defined icons $fileType = tx_dam_db::getMediaExtension($mimeType['file_type']); // See if the icon is a DAM reference if (t3lib_div::testInt($fileType['icon'])) { $fileType['icon'] = tx_dam::file_getPathByUid($fileType['icon']); } if (@file_exists($fileType['icon'])) { $iconfile = $fileType['icon']; $cacheKey = $mimeType['file_type']; $iconCache[$cacheKey] = $iconfile; break; } // then try default PNG if (@file_exists($pathIcons . $mimeType['file_type'] . '.png')) { $iconfile = $pathIcons . $mimeType['file_type'] . '.png'; $cacheKey = $mimeType['file_type']; $iconCache[$cacheKey] = $iconfile; break; } // then go for default GIF if (@file_exists($pathIcons . $mimeType['file_type'] . '.gif')) { $iconfile = $pathIcons . $mimeType['file_type'] . '.gif'; $cacheKey = $mimeType['file_type']; $iconCache[$cacheKey] = $iconfile; break; } } if (!$iconfile && ($mediaType = tx_dam::convert_mediaType($mimeType['media_type']))) { foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dam']['fileIconPaths_' . $mode] as $pathIcons) { // first try PNG if (@file_exists($pathIcons . 'mtype_' . $mediaType . '.png')) { $iconfile = $pathIcons . 'mtype_' . $mediaType . '.png'; $cacheKey = '_mtype_' . $mimeType['media_type']; $iconCache[$cacheKey] = $iconfile; break; } // then go for GIF if (@file_exists($pathIcons . 'mtype_' . $mediaType . '.gif')) { $iconfile = $pathIcons . 'mtype_' . $mediaType . '.gif'; $cacheKey = '_mtype_' . $mimeType['media_type']; $iconCache[$cacheKey] = $iconfile; break; } } } if (!$iconfile) { $iconfile = PATH_txdam . 'i/18/' . 'mtype_undefined.gif'; $cacheKey = '__undefined'; } } } if (!$absolutePath) { $iconfile = preg_replace('#^' . preg_quote(PATH_site) . '#', '', $iconfile); if (TYPO3_MODE === 'BE') { $iconfile = '../' . $iconfile; $iconCacheRel[$cacheKey] = $iconfile; } } return $iconfile; }
/** * get the mime type of a file with full path * * @param string $pathname absolute path to file * @return array file information */ function getFileMimeType($pathname) { // this will be called from tx_dam therefore $pathname can be a fileInfo array $pathname = tx_dam::file_absolutePath($pathname); $TX_DAM = $GLOBALS['T3_VAR']['ext']['dam']; $mimeType = array(); $mimeType['fulltype'] = ''; $mimeType['file_mime_type'] = ''; $mimeType['file_mime_subtype'] = ''; $mimeType['file_type'] = ''; $path_parts = t3lib_div::split_fileref($pathname); $mimeType['file_type'] = strtolower($path_parts['realFileext']); // cleanup bakup files extension $mimeType['file_type'] = preg_replace('#\\~$#', '', $mimeType['file_type']); $this->setup['useInternalMimeList'] = tx_dam::config_checkValueEnabled('setup.indexing.useInternalMimeList', true); $this->setup['useMimeContentType'] = tx_dam::config_checkValueEnabled('setup.indexing.useMimeContentType', true); $this->setup['useFileCommand'] = tx_dam::config_checkValueEnabled('setup.indexing.useFileCommand', true); // Get the mimetype info from the DB $file_type = tx_dam_db::getMediaExtension($mimeType['file_type']); // try first to get the mime type by extension with own array // I made the experience that it is a bit safer than with 'file' if ($this->setup['useInternalMimeList'] and $mimeType['file_type'] and isset($file_type['mime'])) { $mt = $file_type['mime']; if ($this->writeDevLog) { t3lib_div::devLog('getFileMimeType(): used builtin conversion table', 'tx_dam_indexing'); } // next try } elseif ($this->setup['useMimeContentType'] and function_exists('mime_content_type')) { // available in PHP 4.3.0 $mt = mime_content_type($pathname); if ($this->writeDevLog) { t3lib_div::devLog('getFileMimeType(): used mime_content_type()', 'tx_dam_indexing'); } } // last chance if ($this->setup['useFileCommand'] and (!$mt or $mt === 'application/octet-stream')) { $osType = TYPO3_OS; if (!($osType === 'WIN')) { if ($cmd = t3lib_exec::getCommand('file')) { $dummy = array(); $ret = false; $mimeTypeTxt = trim(exec($cmd . ' --mime ' . escapeshellarg($pathname), $dummy, $ret)); if (!$ret and strstr($mimeTypeTxt, tx_dam::file_basename($pathname) . ':')) { $a = explode(':', $mimeTypeTxt); $a = explode(';', trim($a[1])); //a[1]: text/plain, English; charset=iso-8859-1 $a = explode(',', trim($a[0])); $a = explode(' ', trim($a[0])); $mt = trim($a[0]); } } } if ($this->writeDevLog) { t3lib_div::devLog('getFileMimeType(): used t3lib_exec::getCommand(\'file\')', 'tx_dam_indexing'); } } $mtarr = explode('/', $mt); if (is_array($mtarr) && count($mtarr) == 2) { $mimeType['fulltype'] = $mt; $mimeType['file_mime_type'] = $mtarr[0]; $mimeType['file_mime_subtype'] = $mtarr[1]; } if ($mimeType['file_type'] == '') { $file_type = tx_dam_db::getMediaExtension('', $mimeType['fulltype']); $mimeType['file_type'] = $file_type['mime']; } if ($this->writeDevLog) { t3lib_div::devLog('getFileMimeType()', 'tx_dam_indexing', 0, $mimeType); } unset($mimeType['fulltype']); return $mimeType; }