-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreactions.php
More file actions
381 lines (324 loc) · 13.8 KB
/
Copy pathreactions.php
File metadata and controls
381 lines (324 loc) · 13.8 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/******************************************************************************
* MetGENE – Hardened reactions.php
*
* BEHAVIOR:
* - If ?viewType=json|txt → returns raw JSON / TXT from R (no HTML shell)
* - Else → full HTML page with reactions per gene
*
* SECURITY:
* - Uses metgene_common.php helpers (security headers, escaping, safeGet)
* - Sanitizes all GET input (viewType, species, GeneInfoStr, GeneIDType)
* - Uses buildRscriptCommand() → escapeshellarg() for all exec() calls
* - Sanitizes gene IDs before passing to R
* - Safe includes for nav.php and footer.php
*****************************************************************************/
declare(strict_types=1);
// SECURITY FIX: Start session before any output
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
require_once __DIR__ . '/metgene_common.php';
// SECURITY FIX: Send security headers
sendSecurityHeaders();
/* ---------------------------- BRANCH ON viewType -------------------------- */
$view_type = strtolower(safeGet('viewType')); // from metgene_common.php
if ($view_type === 'json' || $view_type === 'txt') {
/**************************************************************************
* DATA / API BRANCH: return JSON or plain text (no HTML shell)
**************************************************************************/
$species = safeGet('species');
$gene_list = safeGet('GeneInfoStr');
$gene_id_type = safeGet('GeneIDType');
// SECURITY FIX: normalizeSpecies returns array, extract just the code
list($species, $species_label, $species_sci) = normalizeSpecies($species);
// Whitelist geneIDType
$allowed_types = ['SYMBOL', 'SYMBOL_OR_ALIAS', 'ENTREZID', 'ENSEMBL', 'REFSEQ', 'UNIPROT'];
if (!in_array($gene_id_type, $allowed_types, true)) {
$gene_id_type = 'SYMBOL';
}
// Sanitize gene list for Rscript: allow alphanumeric + common gene ID chars
$tmp = str_replace('__', ',', $gene_list);
$parts = explode(',', $tmp);
$clean_genes = [];
// SECURITY FIX: Use pattern consistent with other files
$pat = '/^[A-Za-z0-9._-]+$/';
foreach ($parts as $g) {
$g = trim($g);
if ($g === '') {
continue;
}
if (preg_match($pat, $g)) {
$clean_genes[] = $g;
}
}
$gene_info_str = implode('__', $clean_genes);
$domain_name = $_SERVER['SERVER_NAME'] ?? 'localhost';
// ---- Call R script to map IDs <-> symbols securely ----
$cmd = buildRscriptCommand('extractGeneIDsAndSymbols.R', [
$species,
$gene_info_str,
$gene_id_type,
$domain_name,
]);
// SECURITY FIX: Check if command was built successfully
if ($cmd === '') {
if ($view_type === 'json') {
header('Content-Type: application/json; charset=UTF-8');
echo json_encode(['error' => 'Script not available']);
} else {
header('Content-Type: text/plain; charset=UTF-8');
echo "Error: Script not available\n";
}
error_log("SECURITY: extractGeneIDsAndSymbols.R not found or not readable");
exit;
}
$symbol_gene_ids = [];
$retvar = 0;
exec($cmd, $symbol_gene_ids, $retvar);
// SECURITY FIX: Check return code
if ($retvar !== 0) {
error_log("R script extractGeneIDsAndSymbols.R failed with exit code: $retvar");
if ($view_type === 'json') {
header('Content-Type: application/json; charset=UTF-8');
echo json_encode(['error' => 'Gene ID extraction failed']);
} else {
header('Content-Type: text/plain; charset=UTF-8');
echo "Error: Gene ID extraction failed\n";
}
exit;
}
$gene_symbols = [];
$gene_array = [];
$gene_id_symbols_arr = [];
foreach ($symbol_gene_ids as $val) {
// R returns a single comma-separated vector
$gene_id_symbols_arr = explode(',', $val);
}
$length = count($gene_id_symbols_arr);
for ($i = 0; $i < $length; $i++) {
$my_str = $gene_id_symbols_arr[$i];
$trimmed_str = trim($my_str, "\" \t\n\r\0\x0B");
if ($i < $length / 2) {
$gene_symbols[] = $trimmed_str;
} else {
$gene_array[] = $trimmed_str;
}
}
// Set content type once
if ($view_type === 'json') {
header('Content-Type: application/json; charset=UTF-8');
} else {
header('Content-Type: text/plain; charset=UTF-8');
}
// For each gene ID, call extractReactionInfo.R safely
$first = true;
foreach ($gene_array as $value) {
if (!preg_match($pat, $value)) {
continue; // skip anything that doesn't match pattern
}
$cmd_rxn = buildRscriptCommand('extractReactionInfo.R', [
$species,
$value,
$view_type,
]);
// SECURITY FIX: Check if command was built successfully
if ($cmd_rxn === '') {
error_log("SECURITY: extractReactionInfo.R not found or not readable");
continue;
}
$output = [];
$ret_var2 = 0;
exec($cmd_rxn, $output, $ret_var2);
if ($ret_var2 !== 0) {
error_log("R script extractReactionInfo.R failed for gene $value with exit code: $ret_var2");
// skip failed ones but don't break others
continue;
}
$buff = implode("\n", $output);
if (!$first) {
echo "\n";
}
echo $buff;
$first = false;
}
exit;
}
/******************************************************************************
* HTML VIEW BRANCH
******************************************************************************/
$base_dir = getBaseDir(); // from metgene_common.php
// Track whether data changed for caching
$_SESSION['prev_rxn_species'] = $_SESSION['prev_rxn_species'] ?? '';
$_SESSION['prev_rxn_geneList'] = $_SESSION['prev_rxn_geneList'] ?? '';
$_SESSION['prev_rxn_anatomy'] = $_SESSION['prev_rxn_anatomy'] ?? '';
$_SESSION['prev_rxn_disease'] = $_SESSION['prev_rxn_disease'] ?? '';
$_SESSION['prev_rxn_pheno'] = $_SESSION['prev_rxn_pheno'] ?? '';
if (strcmp($_SESSION['prev_rxn_species'], $_SESSION['species'] ?? '') !== 0) {
$_SESSION['prev_rxn_species'] = $_SESSION['species'] ?? '';
$_SESSION['rxn_changed'] = 1;
} elseif (strcmp($_SESSION['prev_rxn_geneList'], $_SESSION['geneList'] ?? '') !== 0) {
$_SESSION['prev_rxn_geneList'] = $_SESSION['geneList'] ?? '';
$_SESSION['rxn_changed'] = 1;
} elseif (strcmp($_SESSION['prev_rxn_disease'], $_SESSION['disease'] ?? '') !== 0) {
$_SESSION['prev_rxn_disease'] = $_SESSION['disease'] ?? '';
$_SESSION['rxn_changed'] = 1;
} elseif (strcmp($_SESSION['prev_rxn_anatomy'], $_SESSION['anatomy'] ?? '') !== 0) {
$_SESSION['prev_rxn_anatomy'] = $_SESSION['anatomy'] ?? '';
$_SESSION['rxn_changed'] = 1;
} elseif (strcmp($_SESSION['prev_rxn_pheno'], $_SESSION['phenotype'] ?? '') !== 0) {
$_SESSION['prev_rxn_pheno'] = $_SESSION['phenotype'] ?? '';
$_SESSION['rxn_changed'] = 1;
} else {
$_SESSION['rxn_changed'] = $_SESSION['rxn_changed'] ?? 0;
}
$species = $_SESSION['species'] ?? '';
$org_name = $_SESSION['org_name'] ?? '';
$gene_array = $_SESSION['geneArray'] ?? [];
$gene_syms = $_SESSION['geneSymbols'] ?? '';
// SECURITY FIX: Ensure geneArray is actually an array
if (!is_array($gene_array)) {
$gene_array = [];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MetGENE: Reactions</title>
<link rel="apple-touch-icon" sizes="180x180" href="<?= escapeHtml($base_dir) ?>/images/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="<?= escapeHtml($base_dir) ?>/images/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="<?= escapeHtml($base_dir) ?>/images/favicon-16x16.png">
<link rel="manifest" href="<?= escapeHtml($base_dir) ?>/site.webmanifest">
<?php
// SECURITY FIX: Validate nav.php path with realpath
$nav_file = realpath(__DIR__ . '/nav.php');
if ($nav_file !== false && strpos($nav_file, __DIR__) === 0 && is_readable($nav_file)) {
include $nav_file;
}
?>
</head>
<body>
<div id="constrain"><div class="constrain">
<br><br>
<p>
<?php
// ---------------------------- CACHE HANDLING -------------------------------
$url = $_SERVER['SCRIPT_NAME'] ?? '';
$parts = explode('/', $url);
$file = $parts[count($parts) - 1] ?? 'reactions.php';
// SECURITY FIX: Sanitize session ID and use absolute path
$safeSession = preg_replace('/[^A-Za-z0-9]/', '', session_id());
$cachefile = __DIR__ . '/cache/cached-' . $safeSession . '-' . basename($file, '.php') . '.html';
$_SESSION['rxn_cache_file'] = $cachefile;
$cachetime = 18000;
// Serve from cache if unchanged & fresh
if (
($_SESSION['rxn_changed'] ?? 0) === 0 &&
isset($_SESSION['rxn_cache_file']) &&
file_exists($_SESSION['rxn_cache_file']) &&
(time() - $cachetime) < filemtime($_SESSION['rxn_cache_file'])
) {
echo '<!-- Cached copy, generated ' . date('H:i', filemtime($_SESSION['rxn_cache_file'])) . " -->\n";
readfile($_SESSION['rxn_cache_file']);
} else {
ob_start(); // Start output buffer for caching
$gene_sym_arr = $gene_syms !== '' ? explode(',', $gene_syms) : [];
$i = 0;
if (!empty($species) && !empty($gene_array) && ($_SESSION['rxn_changed'] ?? 0) === 1) {
// SECURITY FIX: Ensure gene_array is valid
if (!is_array($gene_array)) {
echo "<h3>Error: Invalid gene data</h3>";
} else {
foreach ($gene_array as $value) {
$gene_symbol_str = $gene_sym_arr[$i] ?? '';
// SECURITY FIX: Use stricter pattern for gene IDs
if ($value !== 'NA' && $value !== '' && preg_match('/^[A-Za-z0-9._-]+$/', $value)) {
$h3_str = '<h3>Reaction information for <i><b>' .
escapeHtml($org_name) .
'</b></i> gene <i><b>' .
escapeHtml($gene_symbol_str) .
'</b></i></h3>';
echo $h3_str;
$view_type_html = 'html';
$cmd_rxn = buildRscriptCommand('extractReactionInfo.R', [
$species,
$value,
$view_type_html,
]);
// SECURITY FIX: Check if command was built successfully
if ($cmd_rxn === '') {
error_log("SECURITY: extractReactionInfo.R not found or not readable");
$err = '<h3>Error: Reaction script not available for <i><b>' .
escapeHtml($org_name) .
'</b></i> gene <i><b>' .
escapeHtml($gene_symbol_str) .
'</b></i></h3>';
echo $err . "<br>\n";
} else {
$output = [];
$retvar = 0;
exec($cmd_rxn, $output, $retvar);
// R script returns HTML table; we intentionally do NOT escape it
// because downstream JS (tableHTMLExport) needs the real table markup.
if ($retvar === 0) {
echo "<pre>";
echo implode("\n", $output);
echo "</pre><br>\n";
} else {
error_log("R script extractReactionInfo.R failed for gene $value with exit code: $retvar");
$err = '<h3>No reaction information found for <i><b>' .
escapeHtml($org_name) .
'</b></i> gene <i><b>' .
escapeHtml($gene_symbol_str) .
'</b></i></h3>';
echo $err . "<br>\n";
}
}
} else {
$h3_str = '<h3>No reaction information found for <i><b>' .
escapeHtml($org_name) .
'</b></i> gene <i><b>' .
escapeHtml($gene_symbol_str) .
'</b></i></h3>';
echo $h3_str . "<br>\n";
}
$i++;
}
// UPDATED: Use generateExportButtons helper
echo generateExportButtons($gene_array, 'Reactions');
$_SESSION['rxn_changed'] = 0;
// SECURITY FIX: Add error handling for cache write
$cachefile = $_SESSION['rxn_cache_file'];
if (!is_dir(dirname($cachefile))) {
@mkdir(dirname($cachefile), 0755, true);
}
$cached = @fopen($cachefile, 'w');
if ($cached) {
fwrite($cached, ob_get_contents());
fclose($cached);
@chmod($cachefile, 0640); // Restrict permissions
} else {
error_log("Failed to write cache file: $cachefile");
}
ob_end_flush(); // send buffer
}
}
}
?>
</p>
</div></div>
<!-- UPDATED: Load scripts from external files -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="<?= escapeHtml($base_dir) ?>/src/tableHTMLExport.js"></script>
<script src="<?= escapeHtml($base_dir) ?>/js/table-export-handler.js"></script>
<script src="<?= escapeHtml($base_dir) ?>/js/table-export-init.js"></script>
<?php
// SECURITY FIX: Validate footer.php path with realpath
$footer_file = realpath(__DIR__ . '/footer.php');
if ($footer_file !== false && strpos($footer_file, __DIR__) === 0 && is_readable($footer_file)) {
include $footer_file;
}
?>
</body>
</html>