Open Journal Systems  3.3.0
HtmlArticleGalleyPlugin.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)) return false;
24  if ($this->getEnabled($mainContextId)) {
25  HookRegistry::register('ArticleHandler::view::galley', array($this, 'articleViewCallback'), HOOK_SEQUENCE_LATE);
26  HookRegistry::register('ArticleHandler::download', array($this, 'articleDownloadCallback'), HOOK_SEQUENCE_LATE);
27  }
28  return true;
29  }
30 
36  return $this->getPluginPath() . '/settings.xml';
37  }
38 
43  function getDisplayName() {
44  return __('plugins.generic.htmlArticleGalley.displayName');
45  }
46 
50  function getDescription() {
51  return __('plugins.generic.htmlArticleGalley.description');
52  }
53 
59  function articleViewCallback($hookName, $args) {
60  $request =& $args[0];
61  $issue =& $args[1];
62  $galley =& $args[2];
63  $article =& $args[3];
64 
65  if ($galley && $galley->getFileType() == 'text/html') {
66  foreach ($article->getData('publications') as $publication) {
67  if ($publication->getId() === $galley->getData('publicationId')) {
68  $galleyPublication = $publication;
69  break;
70  }
71  }
72  $templateMgr = TemplateManager::getManager($request);
73  $templateMgr->assign(array(
74  'issue' => $issue,
75  'article' => $article,
76  'galley' => $galley,
77  'isLatestPublication' => $article->getData('currentPublicationId') === $galley->getData('publicationId'),
78  'galleyPublication' => $galleyPublication,
79  ));
80  $templateMgr->display($this->getTemplateResource('display.tpl'));
81 
82  return true;
83  }
84 
85  return false;
86  }
87 
93  function articleDownloadCallback($hookName, $args) {
94  $article =& $args[0];
95  $galley =& $args[1];
96  $fileId =& $args[2];
97  $request = Application::get()->getRequest();
98 
99  if ($galley && $galley->getFileType() == 'text/html' && $galley->getFileId() == $fileId) {
100  if (!HookRegistry::call('HtmlArticleGalleyPlugin::articleDownload', array($article, &$galley, &$fileId))) {
101  echo $this->_getHTMLContents($request, $galley);
102  $returner = true;
103  HookRegistry::call('HtmlArticleGalleyPlugin::articleDownloadFinished', array(&$returner));
104  }
105  return true;
106  }
107 
108  return false;
109  }
110 
118  function _getHTMLContents($request, $galley) {
119  $journal = $request->getJournal();
120  $submissionFile = $galley->getFile();
121  $submissionId = $submissionFile->getSubmissionId();
122  $contents = file_get_contents($submissionFile->getFilePath());
123 
124  // Replace media file references
125  $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO'); /* @var $submissionFileDao SubmissionFileDAO */
126  import('lib.pkp.classes.submission.SubmissionFile'); // Constants
127  $embeddableFiles = array_merge(
128  $submissionFileDao->getLatestRevisions($submissionId, SUBMISSION_FILE_PROOF),
129  $submissionFileDao->getLatestRevisionsByAssocId(ASSOC_TYPE_SUBMISSION_FILE, $submissionFile->getFileId(), $submissionId, SUBMISSION_FILE_DEPENDENT)
130  );
131  $referredArticle = null;
132  $submissionDao = DAORegistry::getDAO('SubmissionDAO'); /* @var $submissionDao SubmissionDAO */
133 
134  foreach ($embeddableFiles as $embeddableFile) {
135  $params = array();
136 
137  if ($embeddableFile->getFileType()=='text/plain' || $embeddableFile->getFileType()=='text/css') $params['inline']='true';
138 
139  // Ensure that the $referredArticle object refers to the article we want
140  if (!$referredArticle || $referredArticle->getId() != $submissionId) {
141  $referredArticle = $submissionDao->getById($submissionId);
142  }
143  $fileUrl = $request->url(null, 'article', 'download', array($referredArticle->getBestId(), $galley->getBestGalleyId(), $embeddableFile->getFileId()), $params);
144  $pattern = preg_quote(rawurlencode($embeddableFile->getOriginalFileName()));
145 
146  $contents = preg_replace(
147  '/([Ss][Rr][Cc]|[Hh][Rr][Ee][Ff]|[Dd][Aa][Tt][Aa])\s*=\s*"([^"]*' . $pattern . ')"/',
148  '\1="' . $fileUrl . '"',
149  $contents
150  );
151 
152  // Replacement for Flowplayer
153  $contents = preg_replace(
154  '/[Uu][Rr][Ll]\s*\:\s*\'(' . $pattern . ')\'/',
155  'url:\'' . $fileUrl . '\'',
156  $contents
157  );
158 
159  // Replacement for other players (ested with odeo; yahoo and google player won't work w/ OJS URLs, might work for others)
160  $contents = preg_replace(
161  '/[Uu][Rr][Ll]=([^"]*' . $pattern . ')/',
162  'url=' . $fileUrl ,
163  $contents
164  );
165 
166  }
167 
168  // Perform replacement for ojs://... URLs
169  $contents = preg_replace_callback(
170  '/(<[^<>]*")[Oo][Jj][Ss]:\/\/([^"]+)("[^<>]*>)/',
171  array($this, '_handleOjsUrl'),
172  $contents
173  );
174 
175  $templateMgr = TemplateManager::getManager($request);
176  $contents = $templateMgr->loadHtmlGalleyStyles($contents, $embeddableFiles);
177 
178  // Perform variable replacement for journal, issue, site info
179  $issueDao = DAORegistry::getDAO('IssueDAO'); /* @var $issueDao IssueDAO */
180  $issue = $issueDao->getBySubmissionId($submissionId);
181 
182  $journal = $request->getJournal();
183  $site = $request->getSite();
184 
185  $paramArray = array(
186  'issueTitle' => $issue?$issue->getIssueIdentification():__('editor.article.scheduleForPublication.toBeAssigned'),
187  'journalTitle' => $journal->getLocalizedName(),
188  'siteTitle' => $site->getLocalizedTitle(),
189  'currentUrl' => $request->getRequestUrl()
190  );
191 
192  foreach ($paramArray as $key => $value) {
193  $contents = str_replace('{$' . $key . '}', $value, $contents);
194  }
195 
196  return $contents;
197  }
198 
199  function _handleOjsUrl($matchArray) {
200  $request = Application::get()->getRequest();
201  $url = $matchArray[2];
202  $anchor = null;
203  if (($i = strpos($url, '#')) !== false) {
204  $anchor = substr($url, $i+1);
205  $url = substr($url, 0, $i);
206  }
207  $urlParts = explode('/', $url);
208  if (isset($urlParts[0])) switch(strtolower_codesafe($urlParts[0])) {
209  case 'journal':
210  $url = $request->url(
211  isset($urlParts[1]) ?
212  $urlParts[1] :
213  $request->getRequestedJournalPath(),
214  null,
215  null,
216  null,
217  null,
218  $anchor
219  );
220  break;
221  case 'article':
222  if (isset($urlParts[1])) {
223  $url = $request->url(
224  null,
225  'article',
226  'view',
227  $urlParts[1],
228  null,
229  $anchor
230  );
231  }
232  break;
233  case 'issue':
234  if (isset($urlParts[1])) {
235  $url = $request->url(
236  null,
237  'issue',
238  'view',
239  $urlParts[1],
240  null,
241  $anchor
242  );
243  } else {
244  $url = $request->url(
245  null,
246  'issue',
247  'current',
248  null,
249  null,
250  $anchor
251  );
252  }
253  break;
254  case 'sitepublic':
255  array_shift($urlParts);
256  import ('classes.file.PublicFileManager');
257  $publicFileManager = new PublicFileManager();
258  $url = $request->getBaseUrl() . '/' . $publicFileManager->getSiteFilesPath() . '/' . implode('/', $urlParts) . ($anchor?'#' . $anchor:'');
259  break;
260  case 'public':
261  array_shift($urlParts);
262  $journal = $request->getJournal();
263  import ('classes.file.PublicFileManager');
264  $publicFileManager = new PublicFileManager();
265  $url = $request->getBaseUrl() . '/' . $publicFileManager->getContextFilesPath($journal->getId()) . '/' . implode('/', $urlParts) . ($anchor?'#' . $anchor:'');
266  break;
267  }
268  return $matchArray[1] . $url . $matchArray[3];
269  }
270 }
HtmlArticleGalleyPlugin\getDisplayName
getDisplayName()
Definition: HtmlArticleGalleyPlugin.inc.php:43
HtmlArticleGalleyPlugin\articleViewCallback
articleViewCallback($hookName, $args)
Definition: HtmlArticleGalleyPlugin.inc.php:59
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
HtmlArticleGalleyPlugin\_handleOjsUrl
_handleOjsUrl($matchArray)
Definition: HtmlArticleGalleyPlugin.inc.php:199
HtmlArticleGalleyPlugin\getDescription
getDescription()
Definition: HtmlArticleGalleyPlugin.inc.php:50
HtmlArticleGalleyPlugin\articleDownloadCallback
articleDownloadCallback($hookName, $args)
Definition: HtmlArticleGalleyPlugin.inc.php:93
PublicFileManager
Wrapper class for uploading files to a site/journal's public directory.
Definition: PublicFileManager.inc.php:18
HtmlArticleGalleyPlugin\getContextSpecificPluginSettingsFile
getContextSpecificPluginSettingsFile()
Definition: HtmlArticleGalleyPlugin.inc.php:35
Plugin\getEnabled
getEnabled()
Definition: Plugin.inc.php:868
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
HtmlArticleGalleyPlugin\_getHTMLContents
_getHTMLContents($request, $galley)
Definition: HtmlArticleGalleyPlugin.inc.php:118
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
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
GenericPlugin
Abstract class for generic plugins.
Definition: GenericPlugin.inc.php:18
HtmlArticleGalleyPlugin
Class for HtmlArticleGalley plugin.
Definition: HtmlArticleGalleyPlugin.inc.php:18