| Server IP : 127.0.1.1 / Your IP : 216.73.216.83 Web Server : Apache/2.4.58 (Ubuntu) System : Linux nepub 6.8.0-88-generic #89-Ubuntu SMP PREEMPT_DYNAMIC Sat Oct 11 01:02:46 UTC 2025 x86_64 User : root ( 0) PHP Version : 8.2.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/html/public_html/lib/pkp/classes/file/ |
Upload File : |
<?php
/**
* @file classes/file/PKPLibraryFileManager.inc.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2003-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class PKPLibraryFileManager
* @ingroup file
*
* @brief Wrapper class for uploading files to a site/context' library directory.
*/
import('lib.pkp.classes.context.LibraryFile');
import('lib.pkp.classes.file.PrivateFileManager');
class PKPLibraryFileManager extends PrivateFileManager {
/* @var Context id for the current context */
var $contextId;
/**
* Constructor
* @param $contextId int
*/
function __construct($contextId) {
parent::__construct();
$this->contextId = $contextId;
}
/**
* Get the base path for file storage.
* @return string
*/
function getBasePath() {
return parent::getBasePath() . '/contexts/' . $this->contextId . '/library/';
}
/**
* Delete a file by ID.
* @param $fileId int
* @return int number of files removed
*/
function deleteById($fileId) {
$libraryFileDao = DAORegistry::getDAO('LibraryFileDAO'); /* @var $libraryFileDao LibraryFileDAO */
$libraryFile = $libraryFileDao->getById($fileId);
parent::deleteByPath($this->getBasePath() . $libraryFile->getServerFileName());
$libraryFileDao->deleteById($fileId);
}
/**
* Generate a filename for a library file.
* @param $type int LIBRARY_FILE_TYPE_...
* @param $originalFileName string
* @return string
*/
function generateFileName($type, $originalFileName) {
$libraryFileDao = DAORegistry::getDAO('LibraryFileDAO'); /* @var $libraryFileDao LibraryFileDAO */
$suffix = $this->getFileSuffixFromType($type);
$ext = $this->getExtension($originalFileName);
$truncated = $this->truncateFileName($originalFileName, 127 - PKPString::strlen($suffix) - 1);
$baseName = PKPString::substr($truncated, 0, PKPString::strpos($originalFileName, $ext) - 1);
// Try a simple syntax first
$fileName = $baseName . '-' . $suffix . '.' . $ext;
if (!$libraryFileDao->filenameExists($this->contextId, $fileName)) return $fileName;
for ($i = 1; ; $i++) {
$fullSuffix = $suffix . '-' . $i;
//truncate more if necessary
$truncated = $this->truncateFileName($originalFileName, 127 - PKPString::strlen($fullSuffix) - 1);
// get the base name and append the suffix
$baseName = PKPString::substr($truncated, 0, PKPString::strpos($originalFileName, $ext) - 1);
//try the following
$fileName = $baseName . '-' . $fullSuffix . '.' . $ext;
if (!$libraryFileDao->filenameExists($this->contextId, $fileName)) {
return $fileName;
}
}
}
/**
* Routine to copy a library file from a temporary file.
* @param $temporaryFile object
* @param $libraryFileType int LIBRARY_FILE_TYPE_...
* @return LibraryFile|false the generated file, prepared as much as possible for insert (false if upload failed)
*/
function ©FromTemporaryFile(&$temporaryFile, $libraryFileType) {
$libraryFileDao = DAORegistry::getDAO('LibraryFileDAO'); /* @var $libraryFileDao LibraryFileDAO */
$libraryFile = $libraryFileDao->newDataObject();
$libraryFile = $this->assignFromTemporaryFile($temporaryFile, $libraryFileType, $libraryFile);
if (!$this->copyFile($temporaryFile->getFilePath(), $this->getBasePath() . $libraryFile->getServerFileName())) {
return false;
}
return $libraryFile;
}
/**
* Routine to replace a library file from a temporary file.
* @param $temporaryFile object
* @param $libraryFileType int LIBRARY_FILE_TYPE_...
* @param $libraryFile LibraryFile
* @return LibraryFile|false the updated LibraryFile, or false on error
*/
function &replaceFromTemporaryFile(&$temporaryFile, $libraryFileType, $libraryFile) {
$originalServerFilename = $libraryFile->getServerFileName();
$libraryFile = $this->assignFromTemporaryFile($temporaryFile, $libraryFileType, $libraryFile);
if (!$this->copyFile($temporaryFile->getFilePath(), $this->getBasePath() . $libraryFile->getServerFileName())) {
return false;
}
if ($originalServerFilename !== $libraryFile->getServerFileName()) {
unlink($this->getBasePath() . $originalServerFilename);
}
return $libraryFile;
}
/**
* Routine to assign metadata to a library file from a temporary file
* @param $temporaryFile object
* @param $libraryFileType int LIBRARY_FILE_TYPE_...
* @param $libraryFile LibraryFile
* @return LibraryFile the updated LibraryFile
*/
function &assignFromTemporaryFile(&$temporaryFile, $libraryFileType, $libraryFile) {
$libraryFile->setDateUploaded($temporaryFile->getDateUploaded());
$libraryFile->setDateModified($temporaryFile->getDateUploaded());
$libraryFile->setFileType($temporaryFile->getFileType());
$libraryFile->setFileSize($temporaryFile->getFileSize());
$libraryFile->setServerFileName($this->generateFileName($libraryFileType, $temporaryFile->getOriginalFileName()));
$libraryFile->setOriginalFileName($temporaryFile->getOriginalFileName());
return $libraryFile;
}
/**
* Get the file suffix for the given file type
* @param $type int LIBRARY_FILE_TYPE_...
*/
function getFileSuffixFromType($type) {
$typeSuffixMap =& $this->getTypeSuffixMap();
return $typeSuffixMap[$type];
}
/**
* Get the type => suffix mapping array
*
* @hook PublisherLibrary::types::suffixes [[&$map]]
* @return array
*/
function &getTypeSuffixMap() {
static $map = array(
LIBRARY_FILE_TYPE_MARKETING => 'MAR',
LIBRARY_FILE_TYPE_PERMISSION => 'PER',
LIBRARY_FILE_TYPE_REPORT => 'REP',
LIBRARY_FILE_TYPE_OTHER => 'OTH'
);
HookRegistry::call('PublisherLibrary::types::suffixes', [&$map]);
return $map;
}
/**
* Get the symbolic name from the type
* @param $type int LIBRARY_FILE_TYPE_...
*/
function getNameFromType($type) {
$typeNameMap =& $this->getTypeNameMap();
if (isset($typeNameMap[$type])) {
return $typeNameMap[$type];
} else {
return false;
}
}
/**
* Get the type => locale key mapping array
*
* @hook PublisherLibrary::types::titles [[&$map]]
* @return array
*/
function &getTypeTitleKeyMap() {
static $map = array(
LIBRARY_FILE_TYPE_MARKETING => 'settings.libraryFiles.category.marketing',
LIBRARY_FILE_TYPE_PERMISSION => 'settings.libraryFiles.category.permissions',
LIBRARY_FILE_TYPE_REPORT => 'settings.libraryFiles.category.reports',
LIBRARY_FILE_TYPE_OTHER => 'settings.libraryFiles.category.other'
);
HookRegistry::call('PublisherLibrary::types::titles', [&$map]);
return $map;
}
/**
* Get the display name locale key from the type title
* @param $type int LIBRARY_FILE_TYPE_...
*/
function getTitleKeyFromType($type) {
$typeTitleKeyMap =& $this->getTypeTitleKeyMap();
return $typeTitleKeyMap[$type];
}
/**
* Get the type => name mapping array
*
* @hook PublisherLibrary::types::names [[&$typeNameMap]]
* @return array
*/
function &getTypeNameMap() {
static $typeNameMap = array(
LIBRARY_FILE_TYPE_MARKETING => 'marketing',
LIBRARY_FILE_TYPE_PERMISSION => 'permissions',
LIBRARY_FILE_TYPE_REPORT => 'reports',
LIBRARY_FILE_TYPE_OTHER => 'other',
);
HookRegistry::call('PublisherLibrary::types::names', [&$typeNameMap]);
return $typeNameMap;
}
}