Passed
Pull Request — master (#5918)
by David
13:04
created

sandbox_lf_exists()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 2
dl 0
loc 17
rs 9.8333
c 0
b 0
f 0
1
<?php
2
// This file is part of BOINC.
3
// http://boinc.berkeley.edu
4
// Copyright (C) 2011 University of California
5
//
6
// BOINC is free software; you can redistribute it and/or modify it
7
// under the terms of the GNU Lesser General Public License
8
// as published by the Free Software Foundation,
9
// either version 3 of the License, or (at your option) any later version.
10
//
11
// BOINC is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
// See the GNU Lesser General Public License for more details.
15
//
16
// You should have received a copy of the GNU Lesser General Public License
17
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
18
19
// Utility functions for user file sandbox feature
20
//
21
// In this system:
22
// - each user (job submitter) has a 'sandbox' where they can store files
23
//      on the BOINC server, via a web interface.
24
//      These files are mutable; you can modify a file w/ a given name.
25
// - files are stored in a dir project/sandbox/<userid>
26
// - When a file is uploaded, its size and MD5 are computed and stored
27
//      in an 'info file' in a parallel dir, project/sandbox/<userid>/.md5
28
//
29
// Sandbox files can be used for web-based job submissions systems
30
// like BUDA and autodock on BOINC Central.
31
// Typically they are used as job input files or app files,
32
// in which case they are downloadable.
33
// When a file is used in this way,
34
// it must be copied to the download hierarchy,
35
// and assigned a physical name that includes its MD5.
36
// The name depends on the role of the file.
37
38
require_once("../inc/util.inc");
39
require_once("../inc/dir_hier.inc");
40
41
// Return path of sandbox directory for the given user.
42
// Create dir if not present.
43
//
44
function sandbox_dir($user) {
45
    $dir = parse_config(get_config(), "<sandbox_dir>");
46
    if (!$dir) {
47
        $dir = "../../sandbox/";
48
    }
49
    if (!is_dir($dir)) {
50
        mkdir($dir);
51
    }
52
    $d = "$dir/$user->id";
53
    if (!is_dir($d)) {
54
        mkdir($d);
55
    }
56
    if (!is_dir("$d/.md5")) {
57
        mkdir("$d/.md5");
58
    }
59
    return $d;
60
}
61
62
// parse a sandbox file's info file.
63
// If missing, create it.
64
//
65
function sandbox_parse_info_file($user, $name) {
66
    $dir = sandbox_dir($user);
67
    $info_path = "$dir/.md5/$name";
68
    $info = parse_info_file($info_path);
69
    if ($info) {
70
        return $info;
71
    }
72
    [$size, $md5] = get_file_info("$dir/$name");
73
    write_info_file($info_path, $md5, $size);
74
    return [$md5, $size];
75
}
76
77
// return list of files in sandbox
78
//
79
function sandbox_file_names($user) {
80
    $files = scandir(sandbox_dir($user));
81
    $names = array();
82
    foreach ($files as $f) {
83
        if ($f[0] == '.') continue;
84
        $names[] = $f;
85
    }
86
    natsort($names);
87
    return $names;
88
}
89
90
// return list of files matching given pattern,
91
// in the format used for form_select() and form_select_multiple()
92
//
93
function sandbox_select_items($user, $pattern=null) {
94
    $sbfiles = sandbox_file_names($user);
95
    $sbitems = [];
96
    foreach ($sbfiles as $f) {
97
        if ($pattern && !preg_match($pattern, $f)) continue;
98
        $sbitems[] = [$f, $f];
99
    }
100
    return $sbitems;
101
}
102
103
// return a <select> for files in sandbox
104
//
105
function sandbox_file_select(
106
    $user, $select_name, $regexp = null, $allow_none = false
0 ignored issues
show
Coding Style introduced by
Multi-line function declarations must define one parameter per line
Loading history...
107
) {
108
    $x = "<select class=\"form-control\" name=$select_name>\n";
109
    if ($allow_none) {
110
        $x .= "<option value=\"\">--- None</option>\n";
111
    }
112
    $files = sandbox_file_names($user);
113
    foreach ($files as $f) {
114
        if ($regexp && !preg_match("/$regexp/",$f)) continue;
115
        $x .= "<option value=\"$f\">$f</option>\n";
116
    }
117
    $x .= "</select>\n";
118
    return $x;
119
}
120
121
// copy file and info file from sandbox to $dir
122
// (which must have a subdir .md5/)
123
//
124
function copy_sandbox_file($user, $fname, $dir) {
125
    $sbdir = sandbox_dir($user);
126
    copy("$sbdir/$fname", "$dir/$fname");
127
    copy("$sbdir/.md5/$fname", "$dir/.md5/$fname");
128
}
129
130
?>
131