class FileManager {
/**
* Download a file
*
* @param string $filePath the file to be downloaded
* @param string $mediaType MIME type of the file (optional)
* @param bool $inline set true to display inline, false for attachment (optional)
* @param string|null $fileName the name of the file to be used for the download (optional)
* @return bool
*/
function downloadByPath($filePath, $mediaType = null, $inline = false, $fileName = null) {
$result = null;
// Hook call for any external customization before proceeding
if (HookRegistry::call('FileManager::downloadFile', array(&$filePath, &$mediaType, &$inline, &$result, &$fileName))) {
return $result;
}
if (is_readable($filePath)) {
// Detect the MIME type if it's not passed
if ($mediaType === null) {
$mediaType = mime_content_type($filePath);
if (empty($mediaType)) $mediaType = 'application/octet-stream';
}
// If file name is not passed, use the file name from path
if ($fileName === null) {
$fileName = basename($filePath);
}
// Send the headers to force download
header("Content-Type: $mediaType");
header('Content-Length: ' . filesize($filePath));
header('Accept-Ranges: none');
header('Content-Disposition: ' . ($inline ? 'inline' : 'attachment') . "; filename="$fileName"");
header('Cache-Control: private');
header('Pragma: public');
// Output file content to the browser
$this->readFileFromPath($filePath, true);
return true;
} else {
return false;
}
}
/**
* Reads a file and optionally outputs it
*
* @param string $filePath the file path to be read
* @param bool $output true to directly output the content, false to return it as a string
* @return string|bool returns file contents if $output is false, otherwise returns true/false on success
*/
function readFileFromPath($filePath, $output = false) {
if (is_readable($filePath)) {
$f = fopen($filePath, 'rb');
if (!$f) return false;
$data = '';
while (!feof($f)) {
$data .= fread($f, 4096);
if ($output) {
echo $data;
$data = ''; // Reset the data to avoid sending unnecessary output
}
}
fclose($f);
if ($output) return true;
return $data;
}
return false;
}
/**
* Get document type based on MIME type
*
* @param string $type the MIME type
* @return string document type (image, pdf, word, etc.)
*/
function getDocumentType($type) {
// Checking for common image types
if ($this->getImageExtension($type)) {
return 'image';
}
switch ($type) {
case 'application/pdf':
case 'application/x-pdf':
return 'pdf';
case 'application/msword':
return 'word';
case 'application/excel':
return 'excel';
case 'text/html':
return 'html';
case 'application/zip':
case 'application/x-zip':
return 'zip';
case 'application/epub':
case 'application/epub+zip':
return 'epub';
default:
return 'default';
}
}
/**
* Get document extension based on MIME type
*
* @param string $type the MIME type
* @return string|false document extension or false if not found
*/
function getDocumentExtension($type) {
switch ($type) {
case 'application/pdf':
return '.pdf';
case 'application/msword':
return '.doc';
case 'text/html':
return '.html';
case 'application/epub+zip':
return '.epub';
default:
return false;
}
}
/**
* Get image extension based on MIME type
*
* @param string $type the MIME type
* @return string|false image extension or false if not found
*/
function getImageExtension($type) {
switch ($type) {
case 'image/gif':
return '.gif';
case 'image/jpeg':
return '.jpg';
case 'image/png':
return '.png';
default:
return false;
}
}
/**
* Parse file extension from file name
*
* @param string $fileName the file name
* @return string file extension
*/
function parseFileExtension($fileName) {
$fileParts = explode('.', $fileName);
if (count($fileParts) > 1) {
$fileExtension = end($fileParts);
} else {
return 'txt'; // Default to txt if no extension found
}
// Block dangerous extensions like PHP files
$blockedExtensions = ['php', 'php3', 'php4', 'phtml'];
if (in_array(strtolower($fileExtension), $blockedExtensions)) {
return 'txt'; // Block these extensions
}
return $fileExtension;
}
/**
* Decompress the passed gzipped file
*
* @param string $filePath the path of the gzipped file
* @return string the decompressed file path
*/
function decompressFile($filePath) {
return $this->_executeGzip($filePath, true);
}
/**
* Compress the passed file
*
* @param string $filePath the path of the file to be compressed
* @return string the compressed file path
*/
function compressFile($filePath) {
return $this->_executeGzip($filePath, false);
}
/**
* Execute gzip to compress or extract files
*
* @param string $filePath the file path
* @param bool $decompress true if decompression is required
* @return string the file path after compression or decompression
*/
private function _executeGzip($filePath, $decompress = false) {
$gzipPath = '/usr/bin/gzip'; // Path to gzip
if (!is_executable($gzipPath)) {
throw new Exception("Error executing gzip at $gzipPath.");
}
$gzipCmd = escapeshellarg($gzipPath);
if ($decompress) {
$gzipCmd .= ' -d'; // Decompression flag
}
// Run gzip command
$output = [];
$returnValue = 0;
$gzipCmd .= ' ' . escapeshellarg($filePath);
exec($gzipCmd, $output, $returnValue);
if ($returnValue !== 0) {
throw new Exception("Error executing gzip: " . implode("n", $output));
}
return $filePath;
}
}
Fatal error: Uncaught Error: Class "FileManager" not found in /home/ejurnal.universitas-bth.ac.id/public_html/lib/pkp/classes/file/PKPPublicFileManager.inc.php:19
Stack trace:
#0 /home/ejurnal.universitas-bth.ac.id/public_html/lib/pkp/includes/functions.inc.php(25): require_once()
#1 /home/ejurnal.universitas-bth.ac.id/public_html/classes/file/PublicFileManager.inc.php(16): import()
#2 /home/ejurnal.universitas-bth.ac.id/public_html/lib/pkp/includes/functions.inc.php(25): require_once('...')
#3 /home/ejurnal.universitas-bth.ac.id/public_html/classes/template/TemplateManager.inc.php(19): import()
#4 /home/ejurnal.universitas-bth.ac.id/public_html/lib/pkp/includes/functions.inc.php(25): require_once('...')
#5 /home/ejurnal.universitas-bth.ac.id/public_html/lib/pkp/classes/core/PKPApplication.inc.php(149): import()
#6 /home/ejurnal.universitas-bth.ac.id/public_html/lib/pkp/includes/bootstrap.inc.php(41): PKPApplication->__construct()
#7 /home/ejurnal.universitas-bth.ac.id/public_html/index.php(64): require('...')
#8 {main}
thrown in /home/ejurnal.universitas-bth.ac.id/public_html/lib/pkp/classes/file/PKPPublicFileManager.inc.php on line 19