forked from pkp/texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextureHandler.inc.php
370 lines (337 loc) · 11.8 KB
/
TextureHandler.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/**
* @file plugins/generic/texture/TextureHandler.inc.php
*
* Copyright (c) 2014-2018 Simon Fraser University
* Copyright (c) 2003-2018 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class TextureHandler
* @ingroup plugins_generic_texture
*
* @brief Handle requests for Texture plugin
*/
import('classes.handler.Handler');
class TextureHandler extends Handler {
/** @var MarkupPlugin The Texture plugin */
protected $_plugin;
/**
* Constructor
*/
function __construct() {
parent::__construct();
$this->_plugin = PluginRegistry::getPlugin('generic', TEXTURE_PLUGIN_NAME);
$this->addRoleAssignment(
array(ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR, ROLE_ID_ASSISTANT, ROLE_ID_REVIEWER, ROLE_ID_AUTHOR),
array('editor', 'json', 'media')
);
}
/**
* @copydoc PKPHandler::authorize()
*/
function authorize($request, &$args, $roleAssignments) {
import('lib.pkp.classes.security.authorization.SubmissionFileAccessPolicy');
$this->addPolicy(new SubmissionFileAccessPolicy($request, $args, $roleAssignments, SUBMISSION_FILE_ACCESS_READ));
return parent::authorize($request, $args, $roleAssignments);
}
/**
* Display substance editor
*
* @param $args array
* @param $request PKPRequest
*
* @return string
*/
public function editor($args, $request) {
$stageId = (int) $request->getUserVar('stageId');
$submissionFile = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION_FILE);
if (!$submissionFile) {
fatalError('Invalid request');
}
$fileId = $submissionFile->getFileId();
$editorTemplateFile = method_exists($this->_plugin, 'getTemplateResource')?$this->_plugin->getTemplateResource('editor.tpl'):($this->_plugin->getTemplateResourceName() . ':templates/editor.tpl');
$router = $request->getRouter();
$documentUrl = $router->url($request, null, 'texture', 'json', null,
array(
'submissionId' => $submissionFile->getSubmissionId(),
'fileId' => $fileId,
'stageId' => $stageId,
)
);
AppLocale::requireComponents(LOCALE_COMPONENT_APP_COMMON, LOCALE_COMPONENT_PKP_MANAGER);
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign(array(
'documentUrl' => $documentUrl,
'textureUrl' => $this->_plugin->getTextureUrl($request),
'texturePluginUrl' => $this->_plugin->getPluginUrl($request),
));
return $templateMgr->fetch($editorTemplateFile);
}
/**
* fetch json archive
*
* @param $args array
* @param $request PKPRequest
*
* @return string
*/
public function json($args, $request) {
$user = $request->getUser();
$context = $request->getContext();
$submissionFile = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION_FILE);
if (!$submissionFile) {
fatalError('Invalid request');
}
$fileId = $submissionFile->getFileId();
$stageId = (int) $request->getUserVar('stageId');
$submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO');
if (empty($submissionFile)) {
echo __('plugins.generic.texture.archive.noArticle'); // TODO custom message
exit;
}
$filePath = $submissionFile->getFilePath();
$postData = $this->_parseRawHttpRequest();
if (!empty($postData)) {
$archive = json_decode($postData['_archive']);
$resources = (array) $archive->resources;
if (isset($resources['manuscript.xml']) && is_object($resources['manuscript.xml'])) {
$manuscriptXml = $resources['manuscript.xml']->data;
// save xml to temp file
$tmpfname = tempnam(sys_get_temp_dir(), 'texture');
file_put_contents($tmpfname, $manuscriptXml);
// temp file to submission file
$submissionDao = Application::getSubmissionDAO();
$submissionId = $submissionFile->getSubmissionId();
$submission = $submissionDao->getById($submissionId);
$genreId = $submissionFile->getGenreId();
$fileSize = filesize($tmpfname);
$submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO');
$newSubmissionFile = $submissionFileDao->newDataObjectByGenreId($genreId);
$newSubmissionFile->setSubmissionId($submission->getId());
$newSubmissionFile->setSubmissionLocale($submission->getLocale());
$newSubmissionFile->setGenreId($genreId);
$newSubmissionFile->setFileStage($submissionFile->getFileStage());
$newSubmissionFile->setDateUploaded(Core::getCurrentDate());
$newSubmissionFile->setDateModified(Core::getCurrentDate());
$newSubmissionFile->setOriginalFileName($submissionFile->getOriginalFileName());
$newSubmissionFile->setUploaderUserId($user->getId());
$newSubmissionFile->setFileSize($fileSize);
$newSubmissionFile->setFileType($submissionFile->getFileType());
$newSubmissionFile->setSourceFileId($submissionFile->getFileId());
$newSubmissionFile->setSourceRevision($submissionFile->getRevision());
$newSubmissionFile->setFileId($submissionFile->getFileId());
$newSubmissionFile->setRevision($submissionFile->getRevision()+1);
$insertedSubmissionFile = $submissionFileDao->insertObject($newSubmissionFile, $tmpfname);
return new JSONMessage(true, array(
'submissionId' => $insertedSubmissionFile->getSubmissionId(),
'fileId' => $insertedSubmissionFile->getFileIdAndRevision(),
'fileStage' => $insertedSubmissionFile->getFileStage(),
));
}
return new JSONMessage(false);
} else {
$assets = array();
$manuscriptXml = file_get_contents($filePath);
$manifestXml = $this->_buildManifestXMLFromDocument($manuscriptXml, $assets);
// media url
$mediaInfos = $this->_buildMediaInfo($request, $assets);
$resources = array(
'manifest.xml' => array(
'encoding' => 'utf8',
'data' => $manifestXml,
'size' => strlen($manifestXml),
'createdAt' => 0,
'updatedAt' => 0,
),
'manuscript.xml' => array(
'encoding' => 'utf8',
'data' => $manuscriptXml,
'size' => filesize($document->path),
'createdAt' => 0,
'updatedAt' => 0,
),
);
$data = array(
'version' => 'AE2F112D',
'resources' => array_merge($resources, $mediaInfos)
);
header('Content-Type: application/json');
return json_encode($data, JSON_UNESCAPED_SLASHES);
}
}
/**
* Helper function to manually parse raw multipart/form-data associated to
* texture PUT request on save
*/
protected function _parseRawHttpRequest() {
$formData = array();
// read incoming data
$input = file_get_contents('php://input');
// grab multipart boundary from content type header
preg_match('/boundary=(.*)$/', $_SERVER['CONTENT_TYPE'], $matches);
$boundary = $matches[1];
// split content by boundary and get rid of last -- element
$a_blocks = preg_split("/-+$boundary/", $input);
array_pop($a_blocks);
// loop data blocks
foreach ($a_blocks as $id => $block)
{
if (empty($block))
continue;
// you'll have to var_dump $block to understand this and maybe replace \n or \r with a visibile char
// parse uploaded files
if (strpos($block, 'application/octet-stream') !== FALSE)
{
// match "name", then everything after "stream" (optional) except for prepending newlines
preg_match("/name=\"([^\"]*)\".*stream[\n|\r]+([^\n\r].*)?$/s", $block, $matches);
}
// parse all other fields
else
{
// match "name" and optional value in between newline sequences
preg_match('/name=\"([^\"]*)\"[\n|\r]+([^\n\r].*)?\r$/s', $block, $matches);
}
$formData[$matches[1]] = $matches[2];
}
return $formData;
}
/**
* Build media info
*
* @param $request PKPRquest
* @param $assets array
* @return array
*/
protected function _buildMediaInfo($request, $assets) {
$infos = array();
$mediaDir = 'texture/media';
$context = $request->getContext();
$router = $request->getRouter();
$dispatcher = $router->getDispatcher();
$fileId = $request->getUserVar('fileId');
$stageId = $request->getUserVar('stageId');
$submissionId = $request->getUserVar('submissionId');
// build mapping to assets file paths
$assetsFilePaths = array();
$submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO');
import('lib.pkp.classes.submission.SubmissionFile'); // Constants
$dependentFiles = $submissionFileDao->getLatestRevisionsByAssocId(
ASSOC_TYPE_SUBMISSION_FILE,
$fileId,
$submissionId,
SUBMISSION_FILE_DEPENDENT
);
foreach ($dependentFiles as $dFile) {
$assetsFilePaths[$dFile->getOriginalFileName()] = $dFile->getFilePath();
}
foreach ($assets as $asset) {
$path = str_replace('media/', '', $asset['path']);
$filePath = $assetsFilePaths[$path];
$url = $dispatcher->url($request, ROUTE_PAGE, null, 'texture', 'media', null, array(
'submissionId' => $submissionId,
'fileId' => $fileId,
'stageId' => $stageId,
'fileName' => $path,
));
$infos[$asset['path']] = array(
'encoding' => 'url',
'data' => $url,
'size' => filesize($filePath),
'createdAt' => filemtime($filePath),
'updatedAt' => filectime($filePath),
);
}
return $infos;
}
/**
* build manifest.xml from xml document
*
* @param $document string raw XML
* @param $assets array list of figure metadata
*/
protected function _buildManifestXMLFromDocument($manuscriptXml, &$assets) {
$dom = new DOMDocument();
if (!$dom->loadXML($manuscriptXml)) {
fatalError("Unable to load XML document content in DOM in order to generate manifest XML.");
}
$k = 0;
$assets = array();
$figElements = $dom->getElementsByTagName('fig');
foreach ($figElements as $figure) {
$pos = $k+1;
$figItem = $figElements->item($k);
$graphic = $figItem->getElementsByTagName('graphic');
// figure without graphic?
if (!$figItem || !$graphic) {
continue;
}
// get fig id
$figId = null;
if ($figItem->hasAttribute('id')) {
$figId = $figItem->getAttribute('id');
}
else {
$figId = "ojs-fig-{$pos}";
}
// get path
$figGraphPath = $graphic->item(0)->getAttribute('xlink:href');
// save assets
$assets[] = array(
'id' => $figId,
'type' => 'image/jpg',
'path' => $figGraphPath,
);
$k++;
}
$sxml = simplexml_load_string('<dar><documents><document id="manuscript" type="article" path="manuscript.xml" /></documents><assets></assets></dar>');
foreach ($assets as $asset) {
$assetNode = $sxml->assets->addChild('asset');
$assetNode->addAttribute('id', $asset['id']);
$assetNode->addAttribute('type', $asset['type']);
$assetNode->addAttribute('path', $asset['path']);
}
return $sxml->asXML();
}
/**
* display images attached to XML document
*
* @param $args array
* @param $request PKPRequest
*
* @return void
*/
public function media($args, $request) {
$user = $request->getUser();
$context = $request->getContext();
$submissionFile = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION_FILE);
if (!$submissionFile) {
fatalError('Invalid request');
}
$submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO');
import('lib.pkp.classes.submission.SubmissionFile'); // Constants
$dependentFiles = $submissionFileDao->getLatestRevisionsByAssocId(
ASSOC_TYPE_SUBMISSION_FILE,
$submissionFile->getFileId(),
$submissionFile->getSubmissionId(),
SUBMISSION_FILE_DEPENDENT
);
// make sure this is an xml document
if (!in_array($submissionFile->getFileType(), array('text/xml', 'application/xml'))) {
fatalError('Invalid request');
}
$mediaSubmissionFile = null;
foreach ($dependentFiles as $dependentFile) {
if ($dependentFile->getOriginalFileName() == $request->getUserVar('fileName')) {
$mediaSubmissionFile = $dependentFile;
break;
}
}
if (!$mediaSubmissionFile) {
$request->getDispatcher()->handle404();
}
$filePath = $mediaSubmissionFile->getFilePath();
header('Content-Type:'.$mediaSubmissionFile->getFileType());
header('Content-Length: ' . $mediaSubmissionFile->getFileSize());
readfile($filePath);
}
}