00001 <?php
00002
00015
00016
00017
00018 import('article.ArticleHTMLGalley');
00019 import('article.SuppFileDAO');
00020
00021 class ArticleXMLGalley extends ArticleHTMLGalley {
00022
00026 function ArticleXMLGalley() {
00027 parent::ArticleHTMLGalley();
00028 }
00029
00034 function isHTMLGalley() {
00035 switch ($this->getFileType()) {
00036 case 'application/xhtml':
00037 case 'application/xhtml+xml':
00038 case 'text/html':
00039 case 'application/xml':
00040 case 'text/xml':
00041 return true;
00042 default: return false;
00043 }
00044 }
00045
00050 function &_getXSLTCache($key) {
00051 static $caches;
00052 if (!isset($caches)) {
00053 $caches = array();
00054 }
00055
00056 if (!isset($caches[$key])) {
00057 import('cache.CacheManager');
00058 $cacheManager =& CacheManager::getManager();
00059 $caches[$key] = $cacheManager->getFileCache(
00060 'xsltGalley', $key,
00061 array(&$this, '_xsltCacheMiss')
00062 );
00063
00064
00065 $cacheTime = $caches[$key]->getCacheTime();
00066
00067 if ($cacheTime !== null && $cacheTime < filemtime($this->getFilePath())) {
00068 $caches[$key]->flush();
00069 }
00070
00071 }
00072 return $caches[$key];
00073 }
00074
00079 function _xsltCacheMiss(&$cache) {
00080 static $contents;
00081 if (!isset($contents)) {
00082 $journal = &Request::getJournal();
00083 $xmlGalleyPlugin = &PluginRegistry::getPlugin('generic', 'XMLGalleyPlugin');
00084
00085 $xsltRenderer = $xmlGalleyPlugin->getSetting($journal->getJournalId(), 'XSLTrenderer');
00086
00087
00088 if ($xsltRenderer == "external") $xsltRenderer = $xmlGalleyPlugin->getSetting($journal->getJournalId(), 'externalXSLT');
00089
00090
00091 $xslStylesheet = $xmlGalleyPlugin->getSetting($journal->getJournalId(), 'XSLstylesheet');
00092 switch ($xslStylesheet) {
00093 case 'NLM':
00094
00095 if ($this->isPdfGalley()) {
00096 $xslSheet = $xmlGalleyPlugin->getPluginPath() . '/transform/nlm/nlm-fo.xsl';
00097 } else {
00098 $xslSheet = $xmlGalleyPlugin->getPluginPath() . '/transform/nlm/nlm-xhtml.xsl';
00099 }
00100 break;
00101 case 'custom';
00102
00103 import('file.JournalFileManager');
00104 $journalFileManager =& new JournalFileManager($journal);
00105 $xslSheet = $journalFileManager->filesDir . $xmlGalleyPlugin->getSetting($journal->getJournalId(), 'customXSL');
00106 break;
00107 }
00108
00109
00110 $contents = $this->transformXSLT($this->getFilePath(), $xslSheet, $xsltRenderer);
00111
00112
00113 if ($contents) $cache->setEntireCache($contents);
00114 }
00115 return null;
00116 }
00117
00124 function getHTMLContents() {
00125 $xmlGalleyPlugin = &PluginRegistry::getPlugin('generic', 'XMLGalleyPlugin');
00126
00127
00128
00129 if ( !$xmlGalleyPlugin ) return parent::getHTMLContents();
00130 if ( !$xmlGalleyPlugin->getEnabled() ) return parent::getHTMLContents();
00131
00132 $cache =& $this->_getXSLTCache($this->getFileName() . '-' . $this->getGalleyId());
00133 $contents = $cache->getContents();
00134
00135
00136
00137 if ($contents == "") return parent::getHTMLContents();
00138
00139
00140 $images = &$this->getImageFiles();
00141
00142 $journal =& Request::getJournal();
00143
00144 if ($images !== null) {
00145 foreach ($images as $image) {
00146 $imageUrl = Request::url(null, 'article', 'viewFile', array($this->getArticleId(), $this->getBestGalleyId($journal), $image->getFileId()));
00147 $contents = preg_replace(
00148 '/(src|href)\s*=\s*"([^"]*' . preg_quote($image->getOriginalFileName()) . ')"/i',
00149 '$1="' . $imageUrl . '"',
00150 $contents
00151 );
00152 }
00153 }
00154
00155
00156 $contents = String::regexp_replace_callback(
00157 '/(<[^<>]*")[Oo][Jj][Ss]:\/\/([^"]+)("[^<>]*>)/',
00158 array(&$this, '_handleOjsUrl'),
00159 $contents
00160 );
00161
00162
00163 $this->suppFileDao = &DAORegistry::getDAO('SuppFileDAO');
00164 $suppFiles = $this->suppFileDao->getSuppFilesByArticle($this->getArticleId());
00165
00166 if ($suppFiles) {
00167 foreach ($suppFiles as $supp) {
00168 $journal = &Request::getJournal();
00169 $suppUrl = Request::url(null, 'article', 'downloadSuppFile', array($this->getArticleId(), $supp->getBestSuppFileId($journal)));
00170
00171 $contents = preg_replace(
00172 '/href="' . preg_quote($supp->getOriginalFileName()) . '"/',
00173 'href="' . $suppUrl . '"',
00174 $contents
00175 );
00176 }
00177 }
00178
00179
00180
00181 if (LOCALE_ENCODING == "iso-8859-1") $contents = &String::utf2html($contents);
00182
00183 return $contents;
00184 }
00185
00191 function viewFileContents() {
00192 import('file.FileManager');
00193 $pdfFileName = CacheManager::getFileCachePath() . DIRECTORY_SEPARATOR . 'fc-xsltGalley-' . str_replace(FileManager::parseFileExtension($this->getFileName()), 'pdf', $this->getFileName());
00194
00195
00196 if ( !FileManager::fileExists($pdfFileName) || filemtime($pdfFileName) < filemtime($this->getFilePath()) ) {
00197
00198
00199 $cache =& $this->_getXSLTCache($this->getFileName() . '-' . $this->getGalleyId());
00200
00201 $contents = $cache->getContents();
00202 if ($contents == "") return false;
00203
00204
00205 $images = &$this->getImageFiles();
00206
00207 if ($images !== null) {
00208
00209
00210 foreach ($images as $image) {
00211 $contents = preg_replace(
00212 '/src\s*=\s*"([^"]*)' . preg_quote($image->getOriginalFileName()) . '([^"]*)"/i',
00213 'src="${1}' . dirname($this->getFilePath()) . DIRECTORY_SEPARATOR . $image->getFileName() . '$2"',
00214 $contents );
00215 }
00216 }
00217
00218
00219 $this->suppFileDao = &DAORegistry::getDAO('SuppFileDAO');
00220 $suppFiles = $this->suppFileDao->getSuppFilesByArticle($this->getArticleId());
00221
00222 if ($suppFiles) {
00223 $journal = &Request::getJournal();
00224 foreach ($suppFiles as $supp) {
00225 $suppUrl = Request::url(null, 'article', 'downloadSuppFile', array($this->getArticleId(), $supp->getBestSuppFileId($journal)));
00226
00227 $contents = preg_replace(
00228 '/external-destination\s*=\s*"([^"]*)' . preg_quote($supp->getOriginalFileName()) . '([^"]*)"/i',
00229 'external-destination="' . $suppUrl . '"',
00230 $contents
00231 );
00232 }
00233 }
00234
00235
00236 import('file.TemporaryFileManager');
00237 $temporaryFileManager = &new TemporaryFileManager();
00238 $tempFoName = $temporaryFileManager->filesDir . $this->getFileName() . '-' . $this->getGalleyId() . '.fo';
00239
00240 $temporaryFileManager->writeFile($tempFoName, $contents);
00241
00242
00243 $journal = &Request::getJournal();
00244 $xmlGalleyPlugin = &PluginRegistry::getPlugin('generic', 'XMLGalleyPlugin');
00245
00246 $fopCommand = str_replace(array('%fo', '%pdf'),
00247 array($tempFoName, $pdfFileName),
00248 $xmlGalleyPlugin->getSetting($journal->getJournalId(), 'externalFOP'));
00249
00250
00251 if( !ini_get('safe_mode') ) $fopCommand = escapeshellcmd($fopCommand);
00252
00253
00254 exec($fopCommand . ' 2>&1', $contents, $status);
00255
00256
00257 if ($status != false) {
00258 if ($contents != '') {
00259 echo implode("\n", $contents);
00260 $cache->flush();
00261 return true;
00262 } else return false;
00263 }
00264
00265
00266 FileManager::deleteFile($tempFoName);
00267 }
00268
00269
00270 FileManager::viewFile($pdfFileName, $this->getFileType());
00271
00272 return true;
00273 }
00274
00284 function transformXSLT($xmlFile, $xslFile, $xsltType = "", $arguments = null) {
00285
00286 if (!FileManager::fileExists($xmlFile) || !FileManager::fileExists($xslFile)) return false;
00287
00288
00289 if ( version_compare(PHP_VERSION,'5','>=') && extension_loaded('xsl') && extension_loaded('dom') ) {
00290
00291
00292 if ( $xsltType == "PHP5" || $xsltType == "" ) {
00293
00294 $xmlDom = new DOMDocument("1.0", "UTF-8");
00295
00296
00297
00298
00299
00300
00301
00302
00303 $xmlDom->substituteEntities = true;
00304 $xmlDom->resolveExternals = true;
00305
00306 $xmlDom->load($xmlFile);
00307
00308
00309 $xslDom = new DOMDocument("1.0", "UTF-8");
00310 $xslDom->load($xslFile);
00311
00312 $proc = new XsltProcessor();
00313 $proc->importStylesheet($xslDom);
00314
00315
00316 foreach ($arguments as $param => $value) {
00317 $proc->setParameter(null, $param, $value);
00318 }
00319
00320
00321 $contents = $proc->transformToXML($xmlDom);
00322
00323 return $contents;
00324 }
00325 }
00326
00327 if ( version_compare(PHP_VERSION,'5','<') && extension_loaded('xslt') ) {
00328
00329
00330 if ( $xsltType == "PHP4" || $xsltType == "" ) {
00331
00332 $proc = xslt_create();
00333
00334
00335 $contents = xslt_process($proc, $xmlFile, $xslFile, null, null, $arguments);
00336
00337 return $contents;
00338 }
00339
00340 }
00341
00342 if ( $xsltType != "" ) {
00343
00344
00345
00346 if ( strpos($xsltType, '%xsl') === false ) return false;
00347
00348
00349 $xsltCommand = str_replace(array('%xsl', '%xml'), array($xslFile, $xmlFile), $xsltType);
00350
00351
00352 if( !ini_get('safe_mode') ) $xsltCommand = escapeshellcmd($xsltCommand);
00353
00354
00355 exec($xsltCommand . ' 2>&1', $contents, $status);
00356
00357
00358 if ($status != false) {
00359 if ($contents != '') {
00360 echo implode("\n", $contents);
00361 return true;
00362 } else return false;
00363 }
00364
00365 return implode("\n", $contents);
00366
00367 } else {
00368
00369 return false;
00370 }
00371 }
00372 }
00373
00374 ?>