Passed
Push — dpa_sandbox3 ( ba60bd )
by David
12:10
created

column_sort_header()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
c 0
b 0
f 0
nc 2
nop 5
dl 0
loc 13
rs 9.9
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
// parse a sandbox file's info file.
64
// If missing, create it.
65
//
66
function sandbox_parse_info_file($user, $name) {
67
    $dir = sandbox_dir($user);
68
    $info_path = "$dir/.md5/$name";
69
    $info = parse_info_file($info_path);
70
    if ($info) {
71
        return $info;
72
    }
73
    [$md5, $size] = get_file_info("$dir/$name");
74
    write_info_file($info_path, $md5, $size);
75
    return [$md5, $size];
76
}
77
78
// return list of files in sandbox
79
//
80
function sandbox_file_names($user) {
81
    $files = scandir(sandbox_dir($user));
82
    $names = array();
83
    foreach ($files as $f) {
84
        if ($f[0] == '.') continue;
85
        $names[] = $f;
86
    }
87
    natsort($names);
88
    return $names;
89
}
90
91
// return list of files matching given pattern,
92
// in the format used for form_select() and form_select_multiple()
93
//
94
function sandbox_select_items($user, $pattern=null) {
95
    $sbfiles = sandbox_file_names($user);
96
    $sbitems = [];
97
    foreach ($sbfiles as $f) {
98
        if ($pattern && !preg_match($pattern, $f)) continue;
99
        $sbitems[] = [$f, $f];
100
    }
101
    return $sbitems;
102
}
103
104
// return a <select> for files in sandbox
105
//
106
function sandbox_file_select(
107
    $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...
108
) {
109
    $x = "<select class=\"form-control\" name=$select_name>\n";
110
    if ($allow_none) {
111
        $x .= "<option value=\"\">--- None</option>\n";
112
    }
113
    $files = sandbox_file_names($user);
114
    foreach ($files as $f) {
115
        if ($regexp && !preg_match("/$regexp/",$f)) continue;
116
        $x .= "<option value=\"$f\">$f</option>\n";
117
    }
118
    $x .= "</select>\n";
119
    return $x;
120
}
121
122
// Copy file and info file from sandbox to $dir
123
// (which must have a subdir .md5/)
124
// Used for BUDA app files.
125
//
126
function copy_sandbox_file($user, $fname, $dir) {
127
    $sbdir = sandbox_dir($user);
128
    copy("$sbdir/$fname", "$dir/$fname");
129
    copy("$sbdir/.md5/$fname", "$dir/.md5/$fname");
130
}
131
132
//////////// tables with sortable columns ////////////////
133
134
// maybe this should go in util.inc
135
136
$g_field = null;
137
138
function field_compare($a, $b) {
139
    global $g_field;
140
    if ($a->$g_field == $b->$g_field) return 0;
141
    return $a->$g_field > $b->$g_field;
142
}
143
144
function column_sort(&$items, $field, $rev) {
145
    global $g_field;
146
    $g_field = $field;
147
    usort($items, 'field_compare');
148
    if ($rev) {
149
        $items = array_reverse($items);
150
    }
151
}
152
153
// column header for a given field.
154
// If it's the sort field, show up or down arrow to change order
155
// Otherwise name is a link to make it the sort field
156
//
157
function column_sort_header($field, $title, $url, $sort_field, $sort_rev) {
158
    if ($field == $sort_field) {
159
        return sprintf(
160
            '%s <a href="%s&sort_field=%s&sort_rev=%d">%s</a>',
161
            $title,
162
            $url, $sort_field,
163
            $sort_rev?0:1,
164
            $sort_rev?'&uarr;':'&darr;'
165
        );
166
    } else {
167
        return sprintf(
168
            '<a href="%s&sort_field=%s">%s</a>',
169
            $url, $field, $title
170
        );
171
    }
172
}
173
174
?>
175