Open Journal Systems  3.3.0
LensGalleyPlugin.inc.php
1 <?php
2 
16 import('lib.pkp.classes.plugins.GenericPlugin');
17 
22  function register($category, $path, $mainContextId = null) {
23  if (parent::register($category, $path, $mainContextId)) {
24  if ($this->getEnabled()) {
25  HookRegistry::register('ArticleHandler::view::galley', array($this, 'articleCallback'));
26  HookRegistry::register('IssueHandler::view::galley', array($this, 'issueCallback'));
27  HookRegistry::register('ArticleHandler::download', array($this, 'articleDownloadCallback'), HOOK_SEQUENCE_LATE);
28  }
29  return true;
30  }
31  return false;
32  }
33 
39  return $this->getPluginPath() . '/settings.xml';
40  }
41 
46  function getDisplayName() {
47  return __('plugins.generic.lensGalley.displayName');
48  }
49 
53  function getDescription() {
54  return __('plugins.generic.lensGalley.description');
55  }
56 
63  function articleCallback($hookName, $args) {
64  $request =& $args[0];
65  $issue =& $args[1];
66  $galley =& $args[2];
67  $submission =& $args[3];
68 
69  $templateMgr = TemplateManager::getManager($request);
70  if ($galley && in_array($galley->getFileType(), array('application/xml', 'text/xml'))) {
71  $galleyPublication = null;
72  foreach ($submission->getData('publications') as $publication) {
73  if ($publication->getId() === $galley->getData('publicationId')) {
74  $galleyPublication = $publication;
75  break;
76  }
77  }
78  $templateMgr->assign(array(
79  'pluginLensPath' => $this->getLensPath($request),
80  'displayTemplatePath' => $this->getTemplateResource('display.tpl'),
81  'pluginUrl' => $request->getBaseUrl() . '/' . $this->getPluginPath(),
82  'galleyFile' => $galley->getFile(),
83  'issue' => $issue,
84  'article' => $submission,
85  'bestId' => $submission->getBestId(),
86  'isLatestPublication' => $submission->getData('currentPublicationId') === $galley->getData('publicationId'),
87  'galleyPublication' => $galleyPublication,
88  'galley' => $galley,
89  'jQueryUrl' => $this->_getJQueryUrl($request),
90  ));
91  $templateMgr->display($this->getTemplateResource('articleGalley.tpl'));
92  return true;
93  }
94 
95  return false;
96  }
97 
104  function issueCallback($hookName, $args) {
105  $request =& $args[0];
106  $issue =& $args[1];
107  $galley =& $args[2];
108 
109  $templateMgr = TemplateManager::getManager($request);
110  if ($galley && in_array($galley->getFileType(), array('application/xml', 'text/xml'))) {
111  $templateMgr->assign(array(
112  'pluginLensPath' => $this->getLensPath($request),
113  'displayTemplatePath' => $this->getTemplateResource('display.tpl'),
114  'pluginUrl' => $request->getBaseUrl() . '/' . $this->getPluginPath(),
115  'galleyFile' => $galley->getFile(),
116  'issue' => $issue,
117  'galley' => $galley,
118  'jQueryUrl' => $this->_getJQueryUrl($request),
119  ));
120  $templateMgr->addJavaScript(
121  'jquery',
122  $jquery,
123  array(
124  'priority' => STYLE_SEQUENCE_CORE,
125  'contexts' => 'frontend',
126  )
127  );
128  $templateMgr->display($this->getTemplateResource('issueGalley.tpl'));
129  return true;
130  }
131 
132  return false;
133  }
134 
140  private function _getJQueryUrl($request) {
141  $min = Config::getVar('general', 'enable_minified') ? '.min' : '';
142  if (Config::getVar('general', 'enable_cdn')) {
143  return '//ajax.googleapis.com/ajax/libs/jquery/' . CDN_JQUERY_VERSION . '/jquery' . $min . '.js';
144  } else {
145  return $request->getBaseUrl() . '/lib/pkp/lib/vendor/components/jquery/jquery' . $min . '.js';
146  }
147  }
148 
154  function getLensPath($request) {
155  return $request->getBaseUrl() . '/' . $this->getPluginPath() . '/lib/lens';
156  }
157 
163  function articleDownloadCallback($hookName, $args) {
164  $article =& $args[0];
165  $galley =& $args[1];
166  $fileId =& $args[2];
167  $request = Application::get()->getRequest();
168 
169  if ($galley && in_array($galley->getFileType(), array('application/xml', 'text/xml')) && $galley->getFileId() == $fileId) {
170  if (!HookRegistry::call('LensGalleyPlugin::articleDownload', array($article, &$galley, &$fileId))) {
171  $xmlContents = $this->_getXMLContents($request, $galley);
172  header('Content-Type: application/xml');
173  header('Content-Length: ' . strlen($xmlContents));
174  header('Content-Disposition: inline');
175  header('Cache-Control: private');
176  header('Pragma: public');
177  echo $xmlContents;
178  $returner = true;
179  HookRegistry::call('LensGalleyPlugin::articleDownloadFinished', array(&$returner));
180  }
181  return true;
182  }
183 
184  return false;
185  }
186 
194  function _getXMLContents($request, $galley) {
195  $journal = $request->getJournal();
196  $submissionFile = $galley->getFile();
197  $contents = file_get_contents($submissionFile->getFilePath());
198 
199  // Replace media file references
200  $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO');
201  import('lib.pkp.classes.submission.SubmissionFile'); // Constants
202  $embeddableFiles = array_merge(
203  $submissionFileDao->getLatestRevisions($submissionFile->getData('submissionId'), SUBMISSION_FILE_PROOF),
204  $submissionFileDao->getLatestRevisionsByAssocId(ASSOC_TYPE_SUBMISSION_FILE, $submissionFile->getFileId(), $submissionFile->getData('submissionId'), SUBMISSION_FILE_DEPENDENT)
205  );
206  $referredArticle = $referredPublication = null;
207  $submissionDao = DAORegistry::getDAO('SubmissionDAO');
208  $publicationService = Services::get('publication');
209  foreach ($embeddableFiles as $embeddableFile) {
210  // Ensure that the $referredArticle object refers to the article we want
211  if (!$referredArticle || !$referredPublication || $referredPublication->getData('submissionId') != $referredArticle->getId() || $referredPublication->getId() != $galley->getData('publicationId')) {
212  $referredPublication = $publicationService->get($galley->getData('publicationId'));
213  $referredArticle = $submissionDao->getById($referredPublication->getData('submissionId'));
214  }
215  $fileUrl = $request->url(null, 'article', 'download', array($referredArticle->getBestArticleId(), $galley->getBestGalleyId(), $embeddableFile->getFileId()));
216  $pattern = preg_quote($embeddableFile->getOriginalFileName());
217 
218  $contents = preg_replace(
219  $pattern='/([Ss][Rr][Cc]|[Hh][Rr][Ee][Ff]|[Dd][Aa][Tt][Aa])\s*=\s*"([^"]*' . $pattern . ')"/',
220  '\1="' . $fileUrl . '"',
221  $contents
222  );
223  }
224 
225  // Perform replacement for ojs://... URLs
226  $contents = preg_replace_callback(
227  '/(<[^<>]*")[Oo][Jj][Ss]:\/\/([^"]+)("[^<>]*>)/',
228  array($this, '_handleOjsUrl'),
229  $contents
230  );
231 
232  // Perform variable replacement for journal, issue, site info
233  $issueDao = DAORegistry::getDAO('IssueDAO');
234  $issue = $issueDao->getBySubmissionId($galley->getData('submissionId'));
235 
236  $journal = $request->getJournal();
237  $site = $request->getSite();
238 
239  $paramArray = array(
240  'issueTitle' => $issue?$issue->getIssueIdentification():__('editor.article.scheduleForPublication.toBeAssigned'),
241  'journalTitle' => $journal->getLocalizedName(),
242  'siteTitle' => $site->getLocalizedTitle(),
243  'currentUrl' => $request->getRequestUrl(),
244  );
245 
246  foreach ($paramArray as $key => $value) {
247  $contents = str_replace('{$' . $key . '}', $value, $contents);
248  }
249 
250  return $contents;
251  }
252 
253  function _handleOjsUrl($matchArray) {
254  $request = Application::get()->getRequest();
255  $url = $matchArray[2];
256  $anchor = null;
257  if (($i = strpos($url, '#')) !== false) {
258  $anchor = substr($url, $i+1);
259  $url = substr($url, 0, $i);
260  }
261  $urlParts = explode('/', $url);
262  if (isset($urlParts[0])) switch(strtolower_codesafe($urlParts[0])) {
263  case 'journal':
264  $url = $request->url(
265  isset($urlParts[1]) ?
266  $urlParts[1] :
267  $request->getRequestedJournalPath(),
268  null,
269  null,
270  null,
271  null,
272  $anchor
273  );
274  break;
275  case 'article':
276  if (isset($urlParts[1])) {
277  $url = $request->url(
278  null,
279  'article',
280  'view',
281  $urlParts[1],
282  null,
283  $anchor
284  );
285  }
286  break;
287  case 'issue':
288  if (isset($urlParts[1])) {
289  $url = $request->url(
290  null,
291  'issue',
292  'view',
293  $urlParts[1],
294  null,
295  $anchor
296  );
297  } else {
298  $url = $request->url(
299  null,
300  'issue',
301  'current',
302  null,
303  null,
304  $anchor
305  );
306  }
307  break;
308  case 'sitepublic':
309  array_shift($urlParts);
310  import ('classes.file.PublicFileManager');
311  $publicFileManager = new PublicFileManager();
312  $url = $request->getBaseUrl() . '/' . $publicFileManager->getSiteFilesPath() . '/' . implode('/', $urlParts) . ($anchor?'#' . $anchor:'');
313  break;
314  case 'public':
315  array_shift($urlParts);
316  $journal = $request->getJournal();
317  import ('classes.file.PublicFileManager');
318  $publicFileManager = new PublicFileManager();
319  $url = $request->getBaseUrl() . '/' . $publicFileManager->getContextFilesPath($journal->getId()) . '/' . implode('/', $urlParts) . ($anchor?'#' . $anchor:'');
320  break;
321  }
322  return $matchArray[1] . $url . $matchArray[3];
323  }
324 }
325 
326 ?>
LensGalleyPlugin\articleDownloadCallback
articleDownloadCallback($hookName, $args)
Definition: LensGalleyPlugin.inc.php:163
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
LensGalleyPlugin
Class for LensGalley plugin.
Definition: LensGalleyPlugin.inc.php:18
LensGalleyPlugin\_handleOjsUrl
_handleOjsUrl($matchArray)
Definition: LensGalleyPlugin.inc.php:253
PublicFileManager
Wrapper class for uploading files to a site/journal's public directory.
Definition: PublicFileManager.inc.php:18
LensGalleyPlugin\getDisplayName
getDisplayName()
Definition: LensGalleyPlugin.inc.php:46
LensGalleyPlugin\issueCallback
issueCallback($hookName, $args)
Definition: LensGalleyPlugin.inc.php:104
LensGalleyPlugin\getDescription
getDescription()
Definition: LensGalleyPlugin.inc.php:53
Plugin\getEnabled
getEnabled()
Definition: Plugin.inc.php:868
Config\getVar
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
Plugin\getTemplateResource
getTemplateResource($template=null, $inCore=false)
Definition: Plugin.inc.php:349
strtolower_codesafe
strtolower_codesafe($str)
Definition: functions.inc.php:280
Plugin\getPluginPath
getPluginPath()
Definition: Plugin.inc.php:330
Plugin\$request
$request
Definition: Plugin.inc.php:68
HookRegistry\register
static register($hookName, $callback, $hookSequence=HOOK_SEQUENCE_NORMAL)
Definition: HookRegistry.inc.php:70
LensGalleyPlugin\_getXMLContents
_getXMLContents($request, $galley)
Definition: LensGalleyPlugin.inc.php:194
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
LensGalleyPlugin\getLensPath
getLensPath($request)
Definition: LensGalleyPlugin.inc.php:154
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
LensGalleyPlugin\articleCallback
articleCallback($hookName, $args)
Definition: LensGalleyPlugin.inc.php:63
GenericPlugin
Abstract class for generic plugins.
Definition: GenericPlugin.inc.php:18
LensGalleyPlugin\getContextSpecificPluginSettingsFile
getContextSpecificPluginSettingsFile()
Definition: LensGalleyPlugin.inc.php:38
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49