-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
361 lines (289 loc) · 9.54 KB
/
functions.php
File metadata and controls
361 lines (289 loc) · 9.54 KB
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
<?php
/**
* Loads a glossary from either a local file or GitHub.
*/
function load_glossary($glossary_name) {
$glossary_file = FALSE;
$cache_lifetime = 3600;
// Check if glossary selection is provided. Try to load from local file first,
// and from GitHub if no local file is found.
if ($glossary_name) {
$glossary_name = preg_replace('@[^a-zA-Z0-9_-]@', '_', $glossary_name);
if (file_exists($glossary_name . '.csv')) {
// If there is a local file with the given name, that takes precedence.
$glossary_file = $glossary_name . '.csv';
}
else {
// Check glossaries on GitHub.
$github_base = 'https://raw.githubusercontent.com/openadvocate/readclearly/master/';
$glossary_file = 'cache/' . $glossary_name . '.csv';
if (!file_exists($glossary_file) || filemtime($glossary_file) < time() - $cache_lifetime) {
$github_url = $github_base . $glossary_name . '/glossary.csv';
$glossary_content = @file_get_contents($github_url);
if (!empty($glossary_content)) {
// Cache the file.
file_put_contents($glossary_file, $glossary_content);
}
}
if (!file_exists($glossary_file)) {
// Invalid glossary name provided, or glossary failed to download from GitHub.
$glossary_name = FALSE;
$glossary_file = FALSE;
}
}
}
if (!$glossary_name) {
// Use default name in the predefined list.
$glossary_fp = fopen('glossary_list.csv', 'r');
$glossary_line = fgetcsv($glossary_fp, 1000, ',');
if (!empty($glossary_line[0]) && file_exists($glossary_line[0] . '.csv')) {
$glossary_name = $glossary_line[0];
$glossary_file = $glossary_line[0] . '.csv';
}
}
if (!$glossary_file) {
return;
}
$glossary = array();
$glossary_spanish = array();
$glossary_handle = fopen($glossary_file, 'r');
$i = 0;
while (($data = fgetcsv($glossary_handle, 2000, ',')) !== FALSE) {
$i++;
if ($i == 1) {
// First line is the header.
continue;
}
if (empty($data[1])) {
continue;
}
$word = trim(str_replace(array("\n", "\r", '#'), '', $data[0]));
$explanation = trim($data[1]);
$explanation = trim($explanation, ',');
// Quotes in the text are escaped.
$explanation = str_replace('""', '"', $explanation);
// Third column may be Spanish translation.
if (!empty($data[2])) {
$glossary_spanish[strtolower($word)] = array(
trim($data[2]), // Spanish description
!empty($data[3]) ? trim($data[3]) : '', // Spanish word
);
}
$glossary[strtolower($word)] = $explanation;
}
// Order words by length.
uksort($glossary, 'length_sort_compare');
return array(
'glossary' => $glossary,
'glossary_spanish' => $glossary_spanish,
'file' => $glossary_file,
);
}
/**
* Processes the user-submitted content.
*/
function process_input($content, $glossary_data) {
// Scan content for keywords.
$matched_words = array();
$new_words = array();
foreach ($glossary_data['glossary'] as $word => $explanation) {
$count = 0;
// Offer Spanish translation, if available.
$exp_spanish = '';
if (isset($glossary_data['glossary_spanish'][$word])) {
$exp_spanish = 'data-glossary-es-desc="' . htmlspecialchars($glossary_data['glossary_spanish'][$word][0]) . '" data-glossary-es="' . htmlspecialchars($glossary_data['glossary_spanish'][$word][1]) . '"';
}
// The regex matches the full word outside of tags unless the next tag is <span class="oarc-marker">, meaning it's in between oarc tags already.
// span.oarc-marker is needed for the regex so that it can identify the end of a marked word with certainty.
// The / and . in the lookbehind is added to avoid words inside urls.
$content = preg_replace('@(?<![\w/.])(' . preg_quote($word, '@') . ')(?![\w/]|[^<]*<span class="oarc-marker">|[^<]*>)@i',
'<span class="oarc-word" data-glossary="' . htmlspecialchars($explanation) . '" ' . $exp_spanish . '>${1}<span class="oarc-marker"></span></span>',
$content, -1, $count);
if ($count) {
if (!isset($matched_words[$word])) {
$matched_words[$word] = 0;
}
$matched_words[$word] += $count;
}
}
// Identify unmatched words for logging.
// Remove matches.
$unmatched = preg_replace('@<span class="oarc-word".+?<span class="oarc-marker"></span></span>@', ' ', $content);
// Remove the 'show glossary' button, in case it's in the content.
$unmatched = preg_replace('@<div id="oarc-activate".+?</div>@', ' ', $unmatched);
// Find words.
$unmatched = strip_tags($unmatched);
$unmatched = preg_replace('@[^a-z0-9&+-]@i', ' ', $unmatched);
$words = explode(' ', $unmatched);
foreach ($words as $word) {
// Drop too short words.
if (strlen($word) < 6) {
continue;
}
$word = strtolower($word);
if (!in_array($word, array_keys($matched_words))) {
if (!isset($new_words[$word])) {
$new_words[$word] = 0;
}
$new_words[$word]++;
}
}
// Logging
oarc_log_run();
oarc_log_words($matched_words, 'oarc_matched', $glossary_data['file']);
oarc_log_words($new_words, 'oarc_unmatched', $glossary_data['file']);
return $content;
}
/**
* Sort callback.
*/
function length_sort_compare($a, $b) {
$len_a = strlen($a);
$len_b = strlen($b);
return $len_a > $len_b ? -1 : ($len_a < $len_b ? 1 : 0);
}
/**
* Logs error into a file.
*/
function oarc_log_error($text) {
global $log_file;
file_put_contents($log_file, '[' . date('Y/m/d@G:i:s') . '] ' . $text . "\n", FILE_APPEND);
}
/**
* Establishes database connection.
* @return
* Returns FALSE if db connection fails.
*/
function oarc_ensure_db() {
global $db_hostname, $db_username, $db_password, $db_name, $db_resource;
if (!empty($db_resource)) {
// Connection already established.
return TRUE;
}
$db_resource = mysql_connect($db_hostname, $db_username, $db_password);
if ($db_resource) {
if (!mysql_select_db($db_name, $db_resource)) {
oarc_log_error("Unable to select database: " . mysql_error());
return FALSE;
}
}
else {
oarc_log_error("Unable to open database: " . mysql_error());
return FALSE;
}
return TRUE;
}
/**
* Logs a ReadClearly run.
*/
function oarc_log_run() {
if (!oarc_ensure_db()) {
return;
}
if (isset($_SERVER['HTTP_REFERER'])) {
$referer = strtolower($_SERVER['HTTP_REFERER']);
}
else {
$referer = 'unknown';
}
$query = "INSERT INTO oarc_log
(year, month, referer, count)
VALUES (
" . date('Y') . ",
" . date('n') . ",
'" . mysql_real_escape_string($referer) . "',
1
)
ON DUPLICATE KEY UPDATE count = count + 1;";
$result = mysql_query($query);
if (!$result) {
oarc_log_error("Database error: " . mysql_error());
}
}
/**
* Logs processed words into the database.
*
* @param $matched_words
* Array of words to log. Keys are the words, values are match counts.
* @param $table
* Db table to log to.
*/
function oarc_log_words($matched_words, $table, $glossary = FALSE) {
if (!oarc_ensure_db()) {
return;
}
if (!in_array($table, array('oarc_unmatched', 'oarc_matched', 'oarc_viewed', 'oarc_translated'))) {
return;
}
if (!$matched_words) {
return;
}
$sanitized_words = array_keys($matched_words);
foreach ($sanitized_words as $key => $value) {
$sanitized_words[$key] = '"' . mysql_real_escape_string(strtolower($value)) . '"';
}
$month = date('n');
$year = date('Y');
if (!$glossary) {
$glossary = 'default';
}
// Retrieve current counts.
$query = "SELECT * FROM $table WHERE word IN (" . implode(',', $sanitized_words) . ") AND
year = $year AND month = $month AND glossary = '" . mysql_real_escape_string(strtolower($glossary)) . "'";
$result = mysql_query($query);
if (!$result) {
oarc_log_error('Mysql error: ' . mysql_error());
return;
}
$current_counts = array();
while ($row = mysql_fetch_assoc($result)) {
$current_counts[$row['word']] = $row['count'];
}
// Merge new numbers.
$values = array();
foreach ($matched_words as $word => $count) {
// Word in the database should be all lowercase.
$lower_word = strtolower($word);
if (!empty($current_counts[$lower_word])) {
$matched_words[$word] += $current_counts[$lower_word];
}
$values[] = "($year, $month, '" . mysql_real_escape_string(strtolower($glossary)) . "', '" . mysql_real_escape_string($lower_word) . "', " . $matched_words[$word] . ")";
}
$query = "INSERT INTO $table (year, month, glossary, word, count) VALUES " . implode($values, ',') ."
ON DUPLICATE KEY UPDATE count = VALUES(count)";
$result = mysql_query($query);
if (!$result) {
oarc_log_error('Mysql error: ' . mysql_error());
return;
}
}
/**
* Logs a vote.
*/
function oarc_log_vote($word, $value, $glossary = FALSE) {
if (!oarc_ensure_db()) {
return;
}
$yes = 0;
$no = 0;
switch ($value) {
case 'yes':
$yes = 1;
break;
case 'no':
$no = 1;
break;
}
$month = date('n');
$year = date('Y');
if (!$glossary) {
$glossary = 'default';
}
$query = "INSERT INTO oarc_votes (year, month, glossary, word, yes, no) VALUES ($year, $month, '" . mysql_real_escape_string(strtolower($glossary)) . "', '" . mysql_real_escape_string(strtolower($word)) . "', $yes, $no)
ON DUPLICATE KEY UPDATE yes = yes + $yes, no = no + $no ";
$result = mysql_query($query);
if (!$result) {
oarc_log_error('Mysql error: ' . mysql_error());
return;
}
}