forked from mkucej/i-librarian
/
rtfscan.php
317 lines (285 loc) · 11.6 KB
/
rtfscan.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
<?php
include_once 'data.php';
include_once 'functions.php';
session_write_close();
if (isset($_FILES['manuscript']) && is_uploaded_file($_FILES['manuscript']['tmp_name'])) {
$response = array();
$errors = array();
//MOVE UPLOADED FILE TO TEMP FOLDER
$temp_file = IL_TEMP_PATH . DIRECTORY_SEPARATOR . 'lib_' . session_id() . DIRECTORY_SEPARATOR . basename(str_replace('\\', '/', urldecode($_FILES['manuscript']['name'])));
$move = move_uploaded_file($_FILES['manuscript']['tmp_name'], $temp_file);
if (!$move) {
$errors[] = 'Error! Manuscript upload failed.';
$response['errors'] = $errors;
$content = json_encode($response, JSON_HEX_APOS);
die($content);
}
//CONVERT DOC, DOCX, ODT TO RTF
$file_extension = '';
if (isset($_FILES['manuscript']))
$file_extension = pathinfo($_FILES['manuscript']['name'], PATHINFO_EXTENSION);
if (in_array($file_extension, array('doc', 'docx', 'odt'))) {
if (PHP_OS == 'Linux' || PHP_OS == 'Darwin')
putenv('HOME=' . IL_TEMP_PATH);
exec(select_soffice() . ' --headless --convert-to rtf --outdir "' . IL_TEMP_PATH . DIRECTORY_SEPARATOR . 'lib_' . session_id() . '" "' . $temp_file . '"');
if (PHP_OS == 'Linux' || PHP_OS == 'Darwin')
putenv('HOME=""');
unlink($temp_file);
$converted_file = IL_TEMP_PATH . DIRECTORY_SEPARATOR . 'lib_' . session_id() . DIRECTORY_SEPARATOR . basename($_FILES['manuscript']['name'], '.' . $file_extension) . '.rtf';
if (!is_file($converted_file)) {
$error[] = "Error! Conversion to RTF failed.";
$response['errors'] = $errors;
$content = json_encode($response, JSON_HEX_APOS);
die($content);
} else {
$temp_file = $converted_file;
}
}
//READ FILE TO VARIABLE
$rtf_string = file_get_contents($temp_file);
if (empty($rtf_string)) {
$errors[] = 'Error! Manuscript file is empty.';
$response['errors'] = $errors;
$content = json_encode($response, JSON_HEX_APOS);
die($content);
}
//FETCH REFERENCE IDS INTO ORDERED ARRAY
preg_match_all('/(\-\S+\-ID)(\d+)/', $rtf_string, $ids);
$cites_ordered = $ids[2];
if (count($cites_ordered) === 0) {
$errors[] = 'Error! No citations found.';
$response['errors'] = $errors;
$content = json_encode($response, JSON_HEX_APOS);
die($content);
}
//FETCH REFERENCE DATA FROM DATABASE IN ORDER
$unique_ids = array_unique($cites_ordered);
$id_query = join(',', $unique_ids);
$orderby = ' ORDER BY CASE id ';
while (list($key, $cite) = each($unique_ids)) {
$orderby .= ' WHEN ' . $cite . ' THEN ' . $key;
}
$orderby .= ' END';
database_connect(IL_DATABASE_PATH, 'library');
$result = $dbHandle->query("SELECT * FROM library WHERE id IN (" . $id_query . ")" . $orderby);
$dbHandle = null;
$citations = array();
while ($items = $result->fetch(PDO::FETCH_ASSOC)) {
$authors = '';
$editors = '';
$id = '';
$title = '';
$secondary_title = '';
$tertiary_title = '';
$pages = '';
$volume = '';
$issue = '';
$doi = '';
$journal = '';
$date_parts = array();
$publisher = '';
$place_published = '';
extract($items);
$i = 0;
$new_authors = array();
$array = array();
$array = explode(';', $authors);
$array = array_filter($array);
if (!empty($array)) {
foreach ($array as $author) {
$array2 = explode(',', $author);
$last = trim($array2[0]);
$last = substr($array2[0], 3, -1);
$first = '';
if (isset($array2[1])) {
$first = trim($array2[1]);
$first = substr($array2[1], 3, -1);
}
$new_authors[$i]['family'] = $last;
$new_authors[$i]['given'] = $first;
$i++;
}
}
$i = 0;
$new_editors = array();
$array = array();
$array = explode(';', $editors);
$array = array_filter($array);
if (!empty($array)) {
foreach ($array as $editor) {
$array2 = explode(',', $editor);
$last = trim($array2[0]);
$last = substr($array2[0], 3, -1);
$first = '';
if (isset($array2[1])) {
$first = trim($array2[1]);
$first = substr($array2[1], 3, -1);
}
$new_editors[$i]['family'] = $last;
$new_editors[$i]['given'] = $first;
$i++;
}
}
$date_parts['date-parts'][] = explode("-", $year);
$type = convert_type($reference_type, 'ilib', 'csl');
// CSL book type, shift secondary title to tertiary title
if ($reference_type == 'book') {
$tertiary_title = $secondary_title;
$secondary_title = '';
}
$json['ID' . $id] = array(
"id" => 'ID' . $id,
"type" => $type,
"title" => $title,
"container-title" => $secondary_title,
"collection-title" => $tertiary_title,
"page" => $pages,
"volume" => $volume,
"issue" => $issue,
"DOI" => $doi,
"journalAbbreviation" => $journal,
"author" => $new_authors,
"editor" => $new_editors,
"issued" => $date_parts,
"publisher" => $publisher,
"publisher-place" => $place_published
);
}
$response['references'] = $json;
//FORMAT CITATION ARRAY FOR CITEPROC-JS
foreach ($cites_ordered as $key => $id) {
if (isset($json['ID' . $id])) {
$citations[] = array(
"citationItems" => array(array('id' => 'ID' . $id)),
"properties" => array('noteIndex' => $key + 1)
);
} else {
$errors[] = 'Error! Reference ' . $id . ' not found.';
}
}
$response['citations'] = $citations;
//FETCH CITATION STYLE
if (!empty($_POST['citation-style']) || !empty($_POST['last-style'])) {
if (!empty($_POST['last-style']))
$_POST['citation-style'] = $_POST['last-style'];
$dbHandle = database_connect(__DIR__, 'styles');
$title_q = $dbHandle->quote(strtolower($_POST['citation-style']));
$result = $dbHandle->query('SELECT style FROM styles WHERE title=' . $title_q);
$style = $result->fetchColumn();
if (empty($style)) {
$errors[] = 'Error! This citation style does not exist.';
$response['errors'] = $errors;
$content = json_encode($response, JSON_HEX_APOS);
die($content);
}
$style = gzuncompress($style);
$style = str_replace(array("\r\n", "\r", "\n"), "", $style);
$style = str_replace("'", "\'", $style);
$response['style'] = $style;
}
if (count($errors) > 0)
$response['errors'] = $errors;
$content = json_encode($response, JSON_HEX_APOS);
die($content);
}
// fomat RTF file
if (!empty($_POST['bibliography']) && count($_POST['cites']) > 0 && !empty($_POST['rtfname'])) {
$response = array('OK');
$errors = array();
$file_extension = '';
if (isset($_POST['rtfname']))
$file_extension = pathinfo($_POST['rtfname'], PATHINFO_EXTENSION);
$temp_file = IL_TEMP_PATH . DIRECTORY_SEPARATOR . 'lib_' . session_id() . DIRECTORY_SEPARATOR
. basename(str_replace('\\', '/', urldecode($_POST['rtfname'])), '.' . $file_extension) . '.rtf';
$output_file = IL_TEMP_PATH . DIRECTORY_SEPARATOR . 'lib_' . session_id() . DIRECTORY_SEPARATOR
. 'formatted-' . basename(str_replace('\\', '/', urldecode($_POST['rtfname'])));
//READ RTF TO VARIABLE
$rtf_string = file_get_contents($temp_file);
if (empty($rtf_string)) {
$errors[] = 'Error! Could not read manuscript file.';
$response['errors'] = $errors;
$content = json_encode($response, JSON_HEX_APOS);
die($content);
}
//INSERT CITATIONS
$i = 0;
preg_match_all('/\\\{\S+\-\S+\-ID\d+\\\}/', $rtf_string, $ids);
foreach ($ids[0] as $target) {
$rtf_string = str_replace($target, $_POST['cites'][$i], $rtf_string);
$i++;
}
//INSERT BIBLIOGRAPHY
$position = strrpos($rtf_string, '}');
$rtf_string = substr($rtf_string, 0, $position - 1) . PHP_EOL . $_POST['bibliography'] . PHP_EOL . '}';
//CONVERT RTF TO DOC, DOCX, ODT TO RTF
if (in_array($file_extension, array('doc', 'docx', 'odt'))) {
if (PHP_OS == 'Linux' || PHP_OS == 'Darwin')
putenv('HOME=' . IL_TEMP_PATH);
exec(select_soffice() . ' --headless --convert-to ' . $file_extension . ' --outdir "' . IL_TEMP_PATH . DIRECTORY_SEPARATOR . 'lib_' . session_id() . '" "' . $temp_file . '"');
if (PHP_OS == 'Linux' || PHP_OS == 'Darwin')
putenv('HOME=""');
rename(IL_TEMP_PATH . DIRECTORY_SEPARATOR . 'lib_' . session_id() . DIRECTORY_SEPARATOR . basename(str_replace('\\', '/', urldecode($_POST['rtfname']))), $output_file);
if (!is_file($output_file)) {
$errors[] = "Error! RTF conversion failed.";
$response['errors'] = $errors;
$content = json_encode($response, JSON_HEX_APOS);
die($content);
}
}
//WRITE MODIFIED FILE
$put = file_put_contents($output_file, $rtf_string);
if (!$put) {
$errors[] = 'Error! Could not write to manuscript file.';
$response['errors'] = $errors;
$content = json_encode($response, JSON_HEX_APOS);
die($content);
}
@unlink($temp_file);
if (count($errors) > 0)
$response['errors'] = $errors;
$content = json_encode($response, JSON_HEX_APOS);
die($content);
}
?>
<div class="item-sticker ui-widget-content ui-corner-all" style="margin: auto;width:50%;margin-top:4em">
<div class="ui-dialog-titlebar ui-state-default ui-corner-top" style="text-align:center">Manuscript citation scan</div>
<form id="rtfscanform" enctype="multipart/form-data" action="rtfscan.php" method="POST">
<table cellspacing="0" class="alternating_row ui-corner-bottom" style="width:100%;border-spacing:6px;margin:auto">
<tr>
<td style="width:8em">
Manuscript file:
</td>
<td style="vertical-align: middle">
<input type="file" name="manuscript">
</td>
</tr>
<tr>
<td>
Citation style:
</td>
<td style="vertical-align: middle">
<input type="text" name="citation-style" id="citation-style" placeholder=" e.g. Cell" style="width:95%" autocomplete="false">
</td>
</tr>
<tr style="display:none">
<td>
Last style:
</td>
<td id="last-style-td" class="select_span" style="vertical-align: middle">
<input type="checkbox" name="last-style" style="display:none" value="">
<i class="fa fa-square-o"></i>
<span></span>
</td>
</tr>
<tr>
<td>
</td>
<td style="vertical-align: middle">
<input type="submit" value="Format Citations">
</td>
</tr>
</table>
</form>
</div>
<div id="rtfscan-results" style="width:50%;margin:auto;padding:0;margin-top:2em">
</div>