17 import(
'classes.handler.Handler');
19 use \Firebase\JWT\JWT;
41 function authorize($request, &$args, $roleAssignments) {
43 if ($header = array_search(
'Authorization', array_flip(getallheaders()))) {
44 list($bearer, $jwt) = explode(
' ', $header);
45 if (strcasecmp($bearer,
'Bearer') == 0) {
46 $apiToken = json_decode(JWT::decode($jwt,
Config::getVar(
'security',
'api_key_secret',
''), array(
'HS256')));
51 import(
'lib.pkp.classes.security.authorization.ContextRequiredPolicy');
54 import(
'classes.security.authorization.OpsServerMustPublishPolicy');
57 return parent::authorize($request, $args, $roleAssignments);
65 $urlPath = empty($args) ? 0 : array_shift($args);
68 $submission =
Services::get(
'submission')->getByUrlPath($urlPath, $request->getContext()->getId());
70 if (!$submission && ctype_digit((
string) $urlPath)) {
72 if ($submission && $request->getContext()->getId() != $submission->getContextId()) $submission =
null;
75 if (!$submission || $submission->getData(
'status') !== STATUS_PUBLISHED) {
76 $request->getDispatcher()->handle404();
81 $currentUrlPath = $submission->getBestId();
82 if ($currentUrlPath && $currentUrlPath != $urlPath) {
84 $newArgs[0] = $currentUrlPath;
85 $request->redirect(
null, $request->getRequestedPage(), $request->getRequestedOp(), $newArgs);
88 $this->preprint = $submission;
91 $subPath = empty($args) ? 0 : array_shift($args);
92 if ($subPath ===
'version') {
93 $publicationId = (int) array_shift($args);
94 $galleyId = empty($args) ? 0 : array_shift($args);
95 foreach ((array) $this->preprint->getData(
'publications') as
$publication) {
100 if (!$this->publication) {
101 $request->getDispatcher()->handle404();
104 $this->publication = $this->preprint->getCurrentPublication();
105 $galleyId = $subPath;
108 if ($this->publication->getData(
'status') !== STATUS_PUBLISHED) {
109 $request->getDispatcher()->handle404();
112 if ($galleyId && in_array($request->getRequestedOp(), [
'view',
'download'])) {
113 $galleys = (array) $this->publication->getData(
'galleys');
114 foreach ($galleys as
$galley) {
115 if (
$galley->getBestGalleyId() == $galleyId) {
123 if (!$this->galley) {
124 $publications = $submission->getPublishedPublications();
127 if (
$galley->getBestGalleyId() == $galleyId) {
128 $request->redirect(
null, $request->getRequestedPage(), $request->getRequestedOp(), [$submission->getBestId()]);
132 $request->getDispatcher()->handle404();
137 $this->fileId = array_shift($args);
147 function view($args, $request) {
149 $user = $request->getUser();
154 $templateMgr->assign(array(
157 'firstPublication' => reset(
$preprint->getData(
'publications')),
158 'currentPublication' =>
$preprint->getCurrentPublication(),
159 'galley' => $this->galley,
160 'fileId' => $this->fileId,
166 $publicationCategories = $categoryDao->getByPublicationId(
$publication->getId())->toArray();
167 foreach ($publicationCategories as $category) {
168 $title = $category->getLocalizedTitle();
169 if ($category->getParentId()) {
170 $title = $categoryDao->getById($category->getParentId())->getLocalizedTitle() .
' > ' . $title;
173 'path' => $category->getPath(),
178 $templateMgr->assign([
181 'section' => $sectionDao->getById(
$publication->getData(
'sectionId')),
182 'categories' => $categories,
193 $primaryGalleys = array();
194 $supplementaryGalleys = array();
197 $primaryGenres = $genreDao->getPrimaryByContextId(
$context->getId())->toArray();
198 $primaryGenreIds = array_map(
function($genre) {
199 return $genre->getId();
201 $supplementaryGenres = $genreDao->getBySupplementaryAndContextId(
true,
$context->getId())->toArray();
202 $supplementaryGenreIds = array_map(
function($genre) {
203 return $genre->getId();
204 }, $supplementaryGenres);
206 foreach ($galleys as
$galley) {
207 $remoteUrl =
$galley->getRemoteURL();
209 if (!$remoteUrl && !$file) {
212 if ($remoteUrl || in_array($file->getGenreId(), $primaryGenreIds)) {
214 } elseif (in_array($file->getGenreId(), $supplementaryGenreIds)) {
215 $supplementaryGalleys[] =
$galley;
219 $templateMgr->assign(array(
220 'primaryGalleys' => $primaryGalleys,
221 'supplementaryGalleys' => $supplementaryGalleys,
227 $parsedCitations = $citationDao->getByPublicationId(
$publication->getId());
228 $templateMgr->assign([
229 'parsedCitations' => $parsedCitations->toArray(),
235 $templateMgr->assign([
236 'licenseTerms' =>
$context->getLocalizedData(
'licenseTerms'),
238 'copyrightHolder' =>
$publication->getData(
'copyrightHolder'),
239 'copyrightYear' =>
$publication->getData(
'copyrightYear'),
245 if ($this->galley && $this->galley->getRemoteURL()) $request->redirectUrl($this->galley->getRemoteURL());
247 if (empty($this->galley)) {
252 $templateMgr->addHeader(
'noindex',
'<meta name="robots" content="noindex">');
253 $url = $request->getDispatcher()->url($request, ROUTE_PAGE,
null,
'preprint',
'view',
$preprint->getBestId());
254 $templateMgr->addHeader(
'canonical',
'<link rel="canonical" href="' . $url .
'">');
258 return $templateMgr->display(
'frontend/pages/preprint.tpl');
265 $templateMgr->addHeader(
'noindex',
'<meta name="robots" content="noindex">');
270 if ($this->publication->getId() !== $this->preprint->getCurrentPublication()->getId()) {
275 $this->galley->getBestGalleyId()
280 $this->galley->getBestGalleyId()
283 $request->redirect(
null,
null,
'download', $redirectArgs);
295 $preprintId = isset($args[0]) ? $args[0] : 0;
296 $galleyId = isset($args[1]) ? $args[1] : 0;
297 $fileId = isset($args[2]) ? (int) $args[2] : 0;
298 header(
'HTTP/1.1 301 Moved Permanently');
299 $request->redirect(
null,
null,
'download', array($preprintId, $galleyId,
$fileId));
308 if (!isset($this->galley)) $request->getDispatcher()->handle404();
309 if ($this->galley->getRemoteURL()) $request->redirectUrl($this->galley->getRemoteURL());
310 else if ($this->
userCanViewGalley($request, $this->preprint->getId(), $this->galley->getId())) {
311 if (!$this->fileId) {
312 $submissionFile = $this->galley->getFile();
313 if ($submissionFile) {
314 $this->fileId = $submissionFile->getFileId();
320 if (!$this->fileId) $request->getDispatcher()->handle404();
323 if ($this->fileId != $this->galley->getFileId()) {
325 $dependentFileIds = array_map(
326 function($f) {
return $f->getFileId();},
327 $submissionFileDao->getLatestRevisionsByAssocId(ASSOC_TYPE_SUBMISSION_FILE, $this->galley->getFileId(), $this->preprint->getId(), SUBMISSION_FILE_DEPENDENT)
329 if (!in_array($this->fileId, $dependentFileIds))
330 $request->getDispatcher()->handle404();
333 if (!
HookRegistry::call(
'PreprintHandler::download', array($this->preprint, &$this->galley, &$this->fileId))) {
334 import(
'lib.pkp.classes.file.SubmissionFileManager');
335 $submissionFileManager =
new SubmissionFileManager($this->preprint->getContextId(), $this->preprint->getId());
336 $submissionFileManager->downloadById($this->fileId,
null, $request->getUserVar(
'inline')?
true:
false);
339 header(
'HTTP/1.0 403 Forbidden');
340 echo
'403 Forbidden<br>';
352 if ($submission->getStatus() == STATUS_PUBLISHED) {
355 $request->redirect(
null,
'search');
365 parent::setupTemplate($request);