Skip to content

Instantly share code, notes, and snippets.

@KarlBaumann
Created September 13, 2016 15:23
Show Gist options
  • Save KarlBaumann/dec05d34719126f201a3538420fb6c13 to your computer and use it in GitHub Desktop.
Save KarlBaumann/dec05d34719126f201a3538420fb6c13 to your computer and use it in GitHub Desktop.
<?php
/**
* Delivers the content of the requested file.
*
* @param string $sObjectType The type of the requested file.
* @param string $sObjectUrl The file url.
*
* @return null
*/
public function getFile($sObjectType, $sObjectUrl)
{
$oObject = $this->_getFileSettingsByType($sObjectType, $sObjectUrl);
if ($oObject === null) {
return null;
}
$sFile = null;
if ($this->getAccessHandler()->checkObjectAccess($oObject->type, $oObject->id)) {
$sFile = $oObject->file;
} elseif ($oObject->isImage) {
$sFile = UAM_REALPATH.'gfx/noAccessPic.png';
} else {
wp_die(TXT_UAM_NO_RIGHTS);
}
//Deliver content
if (file_exists($sFile)) {
$sFileName = basename($sFile);
/*
* This only for compatibility
* mime_content_type has been deprecated as the PECL extension file info
* provides the same functionality (and more) in a much cleaner way.
*/
$sFileExt = strtolower(array_pop(explode('.', $sFileName)));
$aMimeTypes = $this->_getMimeTypes();
if (function_exists('finfo_open')) {
$sFileInfo = finfo_open(FILEINFO_MIME);
$sFileMimeType = finfo_file($sFileInfo, $sFile);
finfo_close($sFileInfo);
} elseif (function_exists('mime_content_type')) {
$sFileMimeType = mime_content_type($sFile);
} elseif (isset($aMimeTypes[$sFileExt])) {
$sFileMimeType = $aMimeTypes[$sFileExt];
} else {
$sFileMimeType = 'application/octet-stream';
}
header('Content-Description: File Transfer');
header('Content-Type: '.$sFileMimeType);
if (!$oObject->isImage) {
$sBaseName = str_replace(' ', '_', basename($sFile));
header('Content-Disposition: attachment; filename="'.$sBaseName.'"');
}
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($sFile));
$aUamOptions = $this->getAdminOptions();
if ($aUamOptions['download_type'] == 'fopen'
&& !$oObject->isImage
) {
$oHandler = fopen($sFile, 'r');
//TODO find better solution (prevent '\n' / '0A')
ob_clean();
flush();
while (!feof($oHandler)) {
if (!ini_get('safe_mode')) {
set_time_limit(30);
}
echo fread($oHandler, 1024);
}
exit;
} else {
ob_clean();
flush();
readfile($sFile);
exit;
}
} else {
wp_die(TXT_UAM_FILE_NOT_FOUND_ERROR);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment