예제 #1
0
 /**
  * Display the files associated with a journal.
  */
 function files($args)
 {
     $this->validate();
     $this->setupTemplate(true);
     import('lib.pkp.classes.file.FileManager');
     $templateMgr =& TemplateManager::getManager();
     $templateMgr->assign('pageHierarchy', array(array(Request::url(null, 'manager'), 'manager.journalManagement')));
     FilesHandler::parseDirArg($args, $currentDir, $parentDir);
     $currentPath = FilesHandler::getRealFilesDir($currentDir);
     if (@is_file($currentPath)) {
         $fileMgr = new FileManager();
         if (Request::getUserVar('download')) {
             $fileMgr->downloadFile($currentPath);
         } else {
             $fileMgr->viewFile($currentPath, FilesHandler::fileMimeType($currentPath));
         }
     } else {
         $files = array();
         if ($dh = @opendir($currentPath)) {
             while (($file = readdir($dh)) !== false) {
                 if ($file != '.' && $file != '..') {
                     $filePath = $currentPath . '/' . $file;
                     $isDir = is_dir($filePath);
                     $info = array('name' => $file, 'isDir' => $isDir, 'mimetype' => $isDir ? '' : FilesHandler::fileMimeType($filePath), 'mtime' => filemtime($filePath), 'size' => $isDir ? '' : FileManager::getNiceFileSize(filesize($filePath)));
                     $files[$file] = $info;
                 }
             }
             closedir($dh);
         }
         ksort($files);
         $templateMgr->assign_by_ref('files', $files);
         $templateMgr->assign('currentDir', $currentDir);
         $templateMgr->assign('parentDir', $parentDir);
         $templateMgr->assign('helpTopicId', 'journal.managementPages.fileBrowser');
         $templateMgr->display('manager/files/index.tpl');
     }
 }
예제 #2
0
 /**
  * Output PDF generated from the XML/XSL/FO source to browser
  * This function performs any necessary filtering, like image URL replacement.
  * @return string
  */
 function viewFileContents()
 {
     import('lib.pkp.classes.file.FileManager');
     $pdfFileName = CacheManager::getFileCachePath() . DIRECTORY_SEPARATOR . 'fc-xsltGalley-' . str_replace(FileManager::parseFileExtension($this->getFileName()), 'pdf', $this->getFileName());
     // if file does not exist or is outdated, regenerate it from FO
     if (!FileManager::fileExists($pdfFileName) || filemtime($pdfFileName) < filemtime($this->getFilePath())) {
         // render XML into XSL-FO
         $cache =& $this->_getXSLTCache($this->getFileName() . '-' . $this->getId());
         $contents = $cache->getContents();
         if ($contents == "") {
             return false;
         }
         // if for some reason the XSLT failed, show original file
         // Replace image references
         $images =& $this->getImageFiles();
         if ($images !== null) {
             // TODO: this should "smart replace" the file path ($this->getFilePath()) in the XSL-FO
             // in lieu of requiring XSL parameters, and transparently for FO that are hardcoded
             foreach ($images as $image) {
                 $contents = preg_replace('/src\\s*=\\s*"([^"]*)' . preg_quote($image->getOriginalFileName()) . '([^"]*)"/i', 'src="${1}' . dirname($this->getFilePath()) . DIRECTORY_SEPARATOR . $image->getFileName() . '$2"', $contents);
             }
         }
         // Replace supplementary file references
         $this->suppFileDao =& DAORegistry::getDAO('SuppFileDAO');
         $suppFiles = $this->suppFileDao->getSuppFilesByArticle($this->getArticleId());
         if ($suppFiles) {
             $journal =& Request::getJournal();
             foreach ($suppFiles as $supp) {
                 $suppUrl = Request::url(null, 'article', 'downloadSuppFile', array($this->getArticleId(), $supp->getBestSuppFileId($journal)));
                 $contents = preg_replace('/external-destination\\s*=\\s*"([^"]*)' . preg_quote($supp->getOriginalFileName()) . '([^"]*)"/i', 'external-destination="' . $suppUrl . '"', $contents);
             }
         }
         // create temporary FO file and write the contents
         import('classes.file.TemporaryFileManager');
         $temporaryFileManager = new TemporaryFileManager();
         $tempFoName = $temporaryFileManager->filesDir . $this->getFileName() . '-' . $this->getId() . '.fo';
         $temporaryFileManager->writeFile($tempFoName, $contents);
         // perform %fo and %pdf replacements for fully-qualified shell command
         $journal =& Request::getJournal();
         $xmlGalleyPlugin =& PluginRegistry::getPlugin('generic', $this->parentPluginName);
         $fopCommand = str_replace(array('%fo', '%pdf'), array($tempFoName, $pdfFileName), $xmlGalleyPlugin->getSetting($journal->getId(), 'externalFOP'));
         // check for safe mode and escape the shell command
         if (!ini_get('safe_mode')) {
             $fopCommand = escapeshellcmd($fopCommand);
         }
         // run the shell command and get the results
         exec($fopCommand . ' 2>&1', $contents, $status);
         // if there is an error, spit out the shell results to aid debugging
         if ($status != false) {
             if ($contents != '') {
                 echo implode("\n", $contents);
                 $cache->flush();
                 // clear the XSL cache in case it's a FO error
                 return true;
             } else {
                 return false;
             }
         }
         // clear the temporary FO file
         FileManager::deleteFile($tempFoName);
     }
     // use FileManager to send file to browser
     FileManager::viewFile($pdfFileName, $this->getFileType());
     return true;
 }