Example #1
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');
     $fileManager = new 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->downloadFile($pdfFileName, $this->getFileType(), true);
     return true;
 }
 /**
  * Display the plugin.
  * @param $args array
  * @param $request PKPRequest
  */
 function display($args, $request)
 {
     $templateMgr = TemplateManager::getManager($request);
     $context = $request->getContext();
     parent::display($args, $request);
     $templateMgr->assign('plugin', $this);
     switch (array_shift($args)) {
         case 'index':
         case '':
             $templateMgr->display($this->getTemplatePath() . 'index.tpl');
             break;
         case 'uploadImportXML':
             $user = $request->getUser();
             import('lib.pkp.classes.file.TemporaryFileManager');
             $temporaryFileManager = new TemporaryFileManager();
             $temporaryFile = $temporaryFileManager->handleUpload('uploadedFile', $user->getId());
             if ($temporaryFile) {
                 $json = new JSONMessage(true);
                 $json->setAdditionalAttributes(array('temporaryFileId' => $temporaryFile->getId()));
             } else {
                 $json = new JSONMessage(false, __('common.uploadFailed'));
             }
             return $json->getString();
         case 'importBounce':
             $json = new JSONMessage(true);
             $json->setEvent('addTab', array('title' => __('plugins.importexport.users.results'), 'url' => $request->url(null, null, null, array('plugin', $this->getName(), 'import'), array('temporaryFileId' => $request->getUserVar('temporaryFileId')))));
             return $json->getString();
         case 'import':
             $temporaryFileId = $request->getUserVar('temporaryFileId');
             $temporaryFileDao = DAORegistry::getDAO('TemporaryFileDAO');
             $user = $request->getUser();
             $temporaryFile = $temporaryFileDao->getTemporaryFile($temporaryFileId, $user->getId());
             if (!$temporaryFile) {
                 $json = new JSONMessage(true, __('plugins.importexport.users.uploadFile'));
                 return $json->getString();
             }
             $temporaryFilePath = $temporaryFile->getFilePath();
             libxml_use_internal_errors(true);
             $users = $this->importUsers(file_get_contents($temporaryFilePath), $context, $user);
             $validationErrors = array_filter(libxml_get_errors(), create_function('$a', 'return $a->level == LIBXML_ERR_ERROR ||  $a->level == LIBXML_ERR_FATAL;'));
             $templateMgr->assign('validationErrors', $validationErrors);
             libxml_clear_errors();
             $templateMgr->assign('users', $users);
             $json = new JSONMessage(true, $templateMgr->fetch($this->getTemplatePath() . 'results.tpl'));
             return $json->getString();
         case 'export':
             $exportXml = $this->exportUsers((array) $request->getUserVar('selectedUsers'), $request->getContext(), $request->getUser());
             import('lib.pkp.classes.file.FileManager');
             $fileManager = new FileManager();
             $exportFileName = $this->getExportFileName($this->getExportPath(), 'users', $context, '.xml');
             $fileManager->writeFile($exportFileName, $exportXml);
             $fileManager->downloadFile($exportFileName);
             $fileManager->deleteFile($exportFileName);
             break;
         case 'exportAllUsers':
             $exportXml = $this->exportAllUsers($request->getContext(), $request->getUser());
             import('lib.pkp.classes.file.TemporaryFileManager');
             $fileManager = new TemporaryFileManager();
             $exportFileName = $this->getExportFileName($this->getExportPath(), 'users', $context, '.xml');
             $fileManager->writeFile($exportFileName, $exportXml);
             $fileManager->downloadFile($exportFileName);
             $fileManager->deleteFile($exportFileName);
             break;
         default:
             $dispatcher = $request->getDispatcher();
             $dispatcher->handle404();
     }
 }