Issues (1963)

html/inc/sandbox.inc (1 issue)

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 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 by BOINC clients.
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 can depend on the role of the file;
37
// e.g. it can include a batch ID or BUDA app name
38
39
require_once("../inc/util.inc");
40
require_once("../inc/dir_hier.inc");
41
42
// Return path of sandbox directory for the given user.
43
// Create dir if not present.
44
//
45
function sandbox_dir($user) {
46
    $dir = parse_config(get_config(), "<sandbox_dir>");
47
    if (!$dir) {
48
        $dir = "../../sandbox";
49
    }
50
    if (!is_dir($dir)) {
51
        mkdir($dir);
52
    }
53
    $d = "$dir/$user->id";
54
    if (!is_dir($d)) {
55
        mkdir($d);
56
    }
57
    if (!is_dir("$d/.md5")) {
58
        mkdir("$d/.md5");
59
    }
60
    return $d;
61
}
62
63
// return path of sandbox file
64
//
65
function sandbox_path($user, $fname) {
66
    $dir = sandbox_dir($user);
67
    return "$dir/$fname";
68
}
69
70
// parse a sandbox file's info file.
71
// If missing, create it.
72
//
73
function sandbox_parse_info_file($user, $name) {
74
    $dir = sandbox_dir($user);
75
    $info_path = "$dir/.md5/$name";
76
    $info = parse_info_file($info_path);
77
    if ($info) {
78
        return $info;
79
    }
80
    [$md5, $size] = get_file_info("$dir/$name");
81
    write_info_file($info_path, $md5, $size);
82
    return [$md5, $size];
83
}
84
85
// return list of files in sandbox
86
//
87
function sandbox_file_names($user) {
88
    $files = scandir(sandbox_dir($user));
89
    $names = array();
90
    foreach ($files as $f) {
91
        if ($f[0] == '.') continue;
92
        $names[] = $f;
93
    }
94
    natsort($names);
95
    return $names;
96
}
97
98
// return list of files matching given pattern,
99
// in the format used for form_select() and form_select_multiple()
100
//
101
function sandbox_select_items($user, $pattern=null) {
102
    $sbfiles = sandbox_file_names($user);
103
    $sbitems = [];
104
    foreach ($sbfiles as $f) {
105
        if ($pattern && !preg_match($pattern, $f)) continue;
106
        $sbitems[] = [$f, $f];
107
    }
108
    return $sbitems;
109
}
110
111
// return a <select> for files in sandbox
112
//
113
function sandbox_file_select(
114
    $user, $select_name, $regexp = null, $allow_none = false
0 ignored issues
show
Multi-line function declarations must define one parameter per line
Loading history...
115
) {
116
    $x = "<select class=\"form-control\" name=$select_name>\n";
117
    if ($allow_none) {
118
        $x .= "<option value=\"\">--- None</option>\n";
119
    }
120
    $files = sandbox_file_names($user);
121
    foreach ($files as $f) {
122
        if ($regexp && !preg_match("/$regexp/",$f)) continue;
123
        $x .= "<option value=\"$f\">$f</option>\n";
124
    }
125
    $x .= "</select>\n";
126
    return $x;
127
}
128
129
// Copy file and info file from sandbox to $dir
130
// (which must have a subdir .md5/)
131
// Used for BUDA app files.
132
//
133
function copy_sandbox_file($user, $fname, $dir) {
134
    $sbdir = sandbox_dir($user);
135
    copy("$sbdir/$fname", "$dir/$fname");
136
    copy("$sbdir/.md5/$fname", "$dir/.md5/$fname");
137
}
138
139
//////////// tables with sortable columns ////////////////
140
141
// maybe this should go in util.inc
142
143
$g_field = null;
144
145
function field_compare($a, $b) {
146
    global $g_field;
147
    if ($a->$g_field == $b->$g_field) return 0;
148
    return $a->$g_field > $b->$g_field;
149
}
150
151
function column_sort(&$items, $field, $rev) {
152
    global $g_field;
153
    $g_field = $field;
154
    usort($items, 'field_compare');
155
    if ($rev) {
156
        $items = array_reverse($items);
157
    }
158
}
159
160
// column header for a given field.
161
// If it's the sort field, show up or down arrow to change order
162
// Otherwise name is a link to make it the sort field
163
//
164
function column_sort_header($field, $title, $url, $sort_field, $sort_rev) {
165
    if ($field == $sort_field) {
166
        return sprintf(
167
            '%s <a href="%s&sort_field=%s&sort_rev=%d">%s</a>',
168
            $title,
169
            $url, $sort_field,
170
            $sort_rev?0:1,
171
            $sort_rev?'&uarr;':'&darr;'
172
        );
173
    } else {
174
        return sprintf(
175
            '<a href="%s&sort_field=%s">%s</a>',
176
            $url, $field, $title
177
        );
178
    }
179
}
180
181
?>
182