Repository navigation
Expand file tree
/
Copy pathphp_utils.php
More file actions
453 lines (401 loc) · 10.9 KB
/
Copy pathphp_utils.php
File metadata and controls
453 lines (401 loc) · 10.9 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<?php
// Simple utility function to check if there is an error to an instruction ;
// if no error, the instruction has to be such that a null-string is returned.
// Input:
// $error: the error string returned by the instruction we test
// Output:
// true if there is an error, false if there isn't
// Example:
// $is_there_an_error = has_error(some_funky_function(...));
function has_error($error)
{
return (strlen($error) > 0);
}
// Shortcut for recurring tests in the style of the above ; if from the above
// we get an error, the program exits, printing the error string to the user.
// Input:
// $trial: the instruction we want to test
// Output:
// None, the program just exits if there is an error
// Example:
// _test(some_funky_function(...));
function _test($trial)
{
if (has_error($trial))
{
exit($trial);
}
}
function _test2($trial)
{
$error = array_shift($trial);
if (has_error($error))
{
exit($error);
}
else
{
if (sizeof($trial) == 1)
{
return $trial[0];
}
else
{
return $trial;
}
}
}
// This function is used to get determine if some fasta content
// was given from a text box or from a fasta file, and yields the
// content back. The priorities are:
// - If there is no file uploaded, we assume the content of the
// text box.
// - Else, we first check if the file indeed has a name, and if
// it has a .fa or .fasta extension ; if yes, we return the
// content of the file, else we look at the text box again.
// Input:
// $seq: the content of the text box, a string
// $file: the array which comes with a "file" input (in $_FILES)
// Output:
// The content of the text box or of a file
// Example:
// $raw_output = get_sequence($_POST['sequence'], $_FILES['fasta']);
function get_sequence($seq, $file)
{
$seq = $seq ?? '';
$name = $file['name'] ?? '';
$temp = $file['tmp_name'] ?? '';
$ext = '';
$look_at_file = (!empty($temp) && is_uploaded_file($temp));
if ($look_at_file)
{
if (empty($temp))
$look_at_file = false;
else
$ext = pathinfo($name, PATHINFO_EXTENSION);
if ($ext != 'fa' && $ext != 'fasta')
$look_at_file = false;
}
if ($look_at_file)
{
return array(file_get_contents($temp), $name);
}
else
{
$upname = 'unknown sequence';
if (preg_match_all('/>\w*/', $seq, $matches))
{
$upname = implode(', ', $matches[0]);
}
return array($seq, $upname);
}
}
// A simple utility function to get a random sequence of characters.
// See $VALID_CHAR_LIST to see the range of characters.
// Input:
// $n_chars: the length of the sequence to generate
// Output:
// The string containing the sequence
function gen_random_sequence($n_chars)
{
include('variables.php');
$seq = '';
for ($i = 0; $i < $n_chars; $i++)
{
$seq .= $VALID_CHAR_LIST[random_int(0, strlen($VALID_CHAR_LIST) - 1)];
}
return $seq;
}
// Checks that a job name given by the user (e.g. through GET) has the
// format generated by gen_random_sequence, so that it cannot be used
// to reach files outside of the user's folders (e.g. '../../etc/passwd').
// Input:
// $job: the job name to check
// Output:
// An empty string if the name is valid, else an error string.
// Example:
// _test(check_job_name($job));
function check_job_name($job)
{
include('variables.php');
if (!is_string($job) || strlen($job) != $N_RANDOM_CHARS || strspn($job, $VALID_CHAR_LIST) != $N_RANDOM_CHARS)
{
return 'Error: Invalid job name !';
}
return '';
}
// Escapes a string so that it can be safely printed inside HTML
// (text or attribute value).
function escape_html($str)
{
return htmlspecialchars((string) $str, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
function create_context_for_user($id)
{
include('variables.php');
$path = "$UPDIR_PATH/$id";
if (empty($id))
{
return 'Error: No session ID !';
}
if (!file_exists($path))
{
if (!mkdir($path))
{
return "Error: Couldn't create directory $path !";
}
if (!chmod($path, $UPDIR_RIGHTS))
{
return "Error: Couldn't change permissions on $path !";
}
}
if (!file_exists("$path/$INPUT_FOLDER"))
{
if (!mkdir("$path/$INPUT_FOLDER"))
{
return "Error: Couldn't create directory $path/$INPUT_FOLDER !";
}
if (!chmod("$path/$INPUT_FOLDER", $UPDIR_RIGHTS))
{
return "Error: Couldn't change permissions on $path/$INPUT_FOLDER !";
}
}
if (!file_exists("$path/$FIMO_FOLDER"))
{
if (!mkdir("$path/$FIMO_FOLDER"))
{
return "Error: Couldn't create directory $path/$FIMO_FOLDER !";
}
if (!chmod("$path/$FIMO_FOLDER", $UPDIR_RIGHTS))
{
return "Error: Couldn't change permissions on $path/$FIMO_FOLDER !";
}
}
if (!file_exists("$path/$LOG_FOLDER"))
{
if (!mkdir("$path/$LOG_FOLDER"))
{
return "Error: Couldn't create directory $path/$LOG_FOLDER !";
}
if (!chmod("$path/$LOG_FOLDER", $UPDIR_RIGHTS))
{
return "Error: Couldn't change permissions on $path/$LOG_FOLDER !";
}
}
}
// Once we have got our sequences, we want to create a temporary directory
// and to create a file with the sequences inside it, so that FIMO can use
// them. The temporary directory has the name of the session ID, and is
// located in the place set by $UPDIR_PATH. The file created to store the
// sequences is randomly generated. Please note that the content is then
// put to upper case.
// Input:
// $id: the session ID, which will be the name of the directory
// $seqs: the sequences to store in a file
// Output:
// An error string if something went wrong
function move_input($id, $seqs, $oldname)
{
include('variables.php');
$path = "$UPDIR_PATH/$id";
if (empty($id))
{
return array('Error: No session ID !', '');
}
$filename = gen_random_sequence($N_RANDOM_CHARS);
if (empty($filename))
{
return array('Error: Couldn\'t generate a random name for uploading the sequence !', '');
}
$data = '';
$keys = array_keys($seqs);
foreach ($keys as $key)
{
$data .= "$key\n";
$data .= strtoupper($seqs[$key]) . "\n";
}
if (!file_put_contents("$path/$INPUT_FOLDER/$filename", $data))
{
return array("Error: Couldn't write the sequences to $path/$INPUT_FOLDER/$filename !", '');
}
if (!chmod("$path/$INPUT_FOLDER/$filename", $UPFILES_RIGHTS))
{
return array("Error: Couldn't change permissions on $path/$INPUT_FOLDER/$filename !", '');
}
if (!file_put_contents("$path/$INPUT_FOLDER/$filename.filename", $oldname))
{
return array("Error: Couldn't write the old filename to $path/$INPUT_FOLDER/$filename.filename !", '');
}
if (!chmod("$path/$INPUT_FOLDER/$filename.filename", $UPFILES_RIGHTS))
{
return array("Error: Couldn't change permissions on $path/$INPUT_FOLDER/$filename.filename !", '');
}
return array('', $filename);
}
// A function to delete any input by user with a certain ID, and
// deleting his temporary directory also.
// Input:
// $id: the user session ID
// Output:
// An error string if something went wrong.
function clean_user_input($id)
{
include('variables.php');
$path = "$UPDIR_PATH/$id";
// User input
$input_files = get_folder_content("$path/$INPUT_FOLDER");
foreach ($input_files as $file)
{
if (!unlink("$path/$INPUT_FOLDER/$file"))
{
return "Error: Couldn't delete file $path/$INPUT_FOLDER/$file !";
}
}
if (!rmdir("$path/$INPUT_FOLDER"))
{
return "Error: Couldn't remove directory $path/$INPUT_FOLDER !";
}
$fimo_folders = get_folder_content("$path/$FIMO_FOLDER");
foreach ($fimo_folders as $folder)
{
$fimo_files = get_folder_content("$path/$FIMO_FOLDER/$folder");
foreach ($fimo_files as $file)
{
if (!unlink("$path/$FIMO_FOLDER/$folder/$file"))
{
return "Error: Couldn't delete file $path/$FIMO_FOLDER/$folder/$file !";
}
}
if (!rmdir("$path/$FIMO_FOLDER/$folder"))
{
return "Error: Couldn't delete directory $path/$FIMO_FOLDER/$folder !";
}
}
if (!rmdir("$path/$FIMO_FOLDER"))
{
return "Error: Couldn't remove directory $path/$FIMO_FOLDER !";
}
if (!rmdir("$path"))
{
return "Error: Couldn't remove directory $path !";
}
return '';
}
function delete_job($id, $job)
{
include('variables.php');
$path = "$UPDIR_PATH/$id";
// Deleting input
if (!unlink("$path/$INPUT_FOLDER/$job"))
{
return "Error: Couldn't delete file $path/$FIMO_FOLDER/$job !";
}
if (!unlink("$path/$INPUT_FOLDER/$job.filename"))
{
return "Error: Couldn't delete fine $path/$FIMO_FOLDER/$job.filename !";
}
// Deleting log
if (!unlink("$path/$LOG_FOLDER/$job.log"))
{
return "Error: Couldn't delete file $path/$LOG_FOLDER/$job.log !";
}
// Deleting fimo output
if (!unlink("$path/$FIMO_FOLDER/$job.status"))
{
return "Error: Couldn't delete file $path/$FIMO_FOLDER/$job.status !";
}
// We won't assume fimo has finished running,
// so we want to delete temporary files.
$fimo_files = get_folder_content("$path/$FIMO_FOLDER/$job");
foreach ($fimo_files as $file)
{
if (!unlink("$path/$FIMO_FOLDER/$job/$file"))
{
return "Error: Couldn't delete file $path/$FIMO_FOLDER/$job/$file !";
}
}
if (!rmdir("$path/$FIMO_FOLDER/$job"))
{
return "Error: Couldn't delete directory $path/$FIMO_FOLDER/$job !";
}
return '';
}
function get_folder_content($path)
{
$files = scandir($path);
// 0 is '.' and 1 is '..'
unset($files[0]);
unset($files[1]);
return $files;
}
function get_GET_parameters($params)
{
$ret = array();
foreach ($params as $param)
{
if (!isset($_GET[$param]))
{
return array("Error: Parameter $param not set through GET !", '');
}
$ret[$param] = $_GET[$param];
}
return array('', $ret);
}
function fimo_get_log($id, $job)
{
include('variables.php');
$path = "$UPDIR_PATH/$id/$LOG_FOLDER";
if (!file_exists("$path/$job.log"))
{
return array("Error: Log for job $job not found !", '');
}
$content = file_get_contents("$path/$job.log");
return array('', $content);
}
function fimo_get_input($id, $job)
{
include('variables.php');
$path = "$UPDIR_PATH/$id/$INPUT_FOLDER";
if (!file_exists("$path/$job"))
{
return array("Error: Input for job $job not found !", '');
}
$content = file_get_contents("$path/$job");
return array('', $content);
}
function fimo_get_upname($id, $job)
{
include('variables.php');
$path = "$UPDIR_PATH/$id/$INPUT_FOLDER";
if (!file_exists("$path/$job.filename"))
{
return array("Error: Input file for job $job not found !", '');
}
$content = file_get_contents("$path/$job.filename");
return array('', $content);
}
function split_into_matrix($str)
{
$ret = array();
foreach (explode("\n", $str) as $line)
{
$ret[] = explode("\t", $line);
}
return $ret;
}
function get_matches($keys, $data)
{
$matches = array();
$n_patterns = sizeof($data);
// First we initialise $matches
foreach ($keys as $key)
{
$matches[$key] = array();
}
for ($i = 0; $i < $n_patterns; $i++)
{
$matches['>' . $data[$i][1]][] = $i;
}
return $matches;
}
?>