Issues (1963)

html/user/get_output.php (3 issues)

1
<?php
2
3
// This file is part of BOINC.
4
// http://boinc.berkeley.edu
5
// Copyright (C) 2013 University of California
6
//
7
// BOINC is free software; you can redistribute it and/or modify it
8
// under the terms of the GNU Lesser General Public License
9
// as published by the Free Software Foundation,
10
// either version 3 of the License, or (at your option) any later version.
11
//
12
// BOINC is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
// See the GNU Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public License
18
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
19
20
// handler for output file requests from remote job submission.
21
// See https://github.com/BOINC/boinc/wiki/RemoteJobs
22
23
require_once("../inc/util.inc");
24
require_once("../inc/result.inc");
25
require_once("../inc/submit_util.inc");
26
27
function return_error($str) {
28
    die("ERROR: $str");
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
29
}
30
31
// get a single output file
32
//
33
function get_output_file($instance_name, $file_num, $auth_str) {
34
    $result = BoincResult::lookup_name(BoincDb::escape_string($instance_name));
35
    if (!$result) {
36
        return_error("no job instance ".htmlspecialchars($instance_name));
37
    }
38
    $workunit = BoincWorkunit::lookup_id($result->workunitid);
39
    if (!$workunit) {
40
        return_error("no job $result->workunitid");
41
    }
42
    $batch = BoincBatch::lookup_id($workunit->batch);
43
    if (!$batch) {
44
        return_error("no batch $workunit->batch");
45
    }
46
    $user = BoincUser::lookup_id($batch->user_id);
47
    if (!$user) {
48
        return_error("no user $batch->user_id");
49
    }
50
    $x = md5($user->authenticator.$result->name);
51
    if ($x != $auth_str) {
52
        return_error("bad authenticator");
53
    }
54
55
    $names = get_outfile_phys_names($result);
56
    if ($file_num >= count($names)) {
57
        return_error("bad file num: $file_num > ".count($names));
58
    }
59
    $name = $names[$file_num];
60
61
    $fanout = parse_config(get_config(), "<uldl_dir_fanout>");
62
    $upload_dir = parse_config(get_config(), "<upload_dir>");
63
64
    $path = dir_hier_path($name, $upload_dir, $fanout);
65
    if (!is_file($path)) {
66
        return_error("no such file $path");
67
    }
68
    do_download($path);
69
}
70
71
// get all the output files of a batch (canonical instances only)
72
// and make a zip of all of them
73
//
74
function get_batch_output_files($auth_str) {
75
    $batch_id = get_int('batch_id', true);
76
    if ($batch_id) {
77
        $batch = BoincBatch::lookup_id($batch_id);
78
        if (!$batch) {
79
            return_error("no batch $batch_id");
80
        }
81
    } else {
82
        $batch_name = get_int('batch_name');
83
        $batch_name = BoincDb::escape_string($batch_name);
84
        $batch = BoincBatch::lookup("name='$batch_name'");
85
        if (!$batch) {
86
            return_error("no batch $batch_name");
87
        }
88
    }
89
90
    $user = BoincUser::lookup_id($batch->user_id);
91
    if (!$user) {
92
        return_error("no user $batch->user_id");
93
    }
94
    $x = md5($user->authenticator.$batch->id);
95
    if ($x != $auth_str) {
96
        return_error("bad auth str");
97
    }
98
99
    $zip_basename = tempnam("../cache", "boinc_batch_");
100
    $zip_filename = $zip_basename.".zip";
101
    $fanout = parse_config(get_config(), "<uldl_dir_fanout>");
102
    $upload_dir = parse_config(get_config(), "<upload_dir>");
103
104
    $wus = BoincWorkunit::enum("batch=$batch->id");
105
    foreach ($wus as $wu) {
106
        if (!$wu->canonical_resultid) continue;
107
        $result = BoincResult::lookup_id($wu->canonical_resultid);
108
        $names = get_outfile_phys_names($result);
109
        foreach ($names as $name) {
110
            $path = dir_hier_path($name, $upload_dir, $fanout);
111
            if (is_file($path)) {
112
                system(" nice -9 zip -jq $zip_basename $path");
113
            }
114
            // output file may be optional; don't complain if not there
115
        }
116
    }
117
    // if no output files, make empty zip file
118
    //
119
    if (!file_exists($zip_filename)) {
120
        touch($zip_filename);
121
    }
122
    do_download($zip_filename);
123
    unlink($zip_filename);
124
    unlink($zip_basename);
125
}
126
127
// return a single output file of a WU's canonical instance
128
//
129
function get_wu_output_file($wu_name, $file_num, $auth_str) {
130
    $wu_name = BoincDb::escape_string($wu_name);
131
    $wu = BoincWorkunit::lookup("name='$wu_name'");
132
    if (!$wu) {
133
        return_error("no workunit ".htmlspecialchars($wu_name));
134
    }
135
    $batch = BoincBatch::lookup_id($wu->batch);
136
    if (!$batch) {
137
        return_error("no batch $wu->batch");
138
    }
139
    $user = BoincUser::lookup_id($batch->user_id);
140
    if (!$user) {
141
        return_error("no user $batch->user_id");
142
    }
143
    if ($user->authenticator != $auth_str) {
144
        return_error("bad authenticator");
145
    }
146
    $fanout = parse_config(get_config(), "<uldl_dir_fanout>");
147
    $upload_dir = parse_config(get_config(), "<upload_dir>");
148
    if (!$wu->canonical_resultid) {
149
        return_error("no canonical result for wu ".htmlspecialchars($wu->name));
150
    }
151
    $result = BoincResult::lookup_id($wu->canonical_resultid);
152
    $names = get_outfile_phys_names($result);
153
    $path = dir_hier_path($names[$file_num], $upload_dir, $fanout);
154
    if (file_exists($path)) {
155
        do_download($path);
156
    } else {
157
        return_error("no such file: ".htmlspecialchars($path));
158
    }
159
}
160
161
// return a zip of all the output files of a workunit's canonical instance
162
//
163
function get_wu_output_files($wu_id, $auth_str) {
164
    $wu = BoincWorkunit::lookup_id($wu_id);
165
    if (!$wu) {
166
        return_error("no workunit $wu_id");
167
    }
168
    $batch = BoincBatch::lookup_id($wu->batch);
169
    if (!$batch) {
170
        return_error("no batch $wu->batch");
171
    }
172
    $user = BoincUser::lookup_id($batch->user_id);
173
    if (!$user) {
174
        return_error("no user $batch->user_id");
175
    }
176
    $x = md5($user->authenticator.$wu_id);
177
    if ($x != $auth_str) {
178
        return_error("bad authenticator");
179
    }
180
181
    $zip_basename = tempnam("/tmp", "boinc_wu_".$wu->name."_");
182
    $zip_filename = $zip_basename.".zip";
183
    $fanout = parse_config(get_config(), "<uldl_dir_fanout>");
184
    $upload_dir = parse_config(get_config(), "<upload_dir>");
185
186
    if (!$wu->canonical_resultid) {
187
        return_error("no canonical result for wu ".htmlspecialchars($wu->name));
188
    }
189
    $result = BoincResult::lookup_id($wu->canonical_resultid);
190
    $names = get_outfile_phys_names($result);
191
    foreach ($names as $name) {
192
        $path = dir_hier_path($name, $upload_dir, $fanout);
193
        if (is_file($path)) {
194
            system("nice -9 zip -jq $zip_basename $path");
195
        }
196
        // output file may be optional; don't complain if not there
197
        //
198
    }
199
    // if no output files, make empty zip file
200
    //
201
    if (!file_exists($zip_filename)) {
202
        touch($zip_filename);
203
    }
204
    do_download($zip_filename);
205
    unlink($zip_filename);
206
    unlink($zip_basename);
207
}
208
209
$cmd = get_str('cmd');
210
$auth_str = get_str('auth_str');
211
switch ($cmd) {
212
case 'result_file';
213
    $result_name = get_str('result_name');
214
    $file_num = get_int('file_num');
215
    get_output_file($result_name, $file_num, $auth_str);
216
    break;
217
case 'batch_files':
218
    get_batch_output_files($auth_str);
219
    break;
220
case 'workunit_file':
221
    $file_num = get_int('file_num');
222
    $wu_name = get_str('wu_name');
223
    get_wu_output_file($wu_name, $file_num, $auth_str);
224
    break;
225
case 'workunit_files':
226
    $wu_id = get_int('wu_id');
227
    get_wu_output_files($wu_id, $auth_str);
228
    break;
229
default:
0 ignored issues
show
DEFAULT keyword must be indented 4 spaces from SWITCH keyword
Loading history...
DEFAULT case must have a breaking statement
Loading history...
230
    echo "bad command\n";
231
}
232
?>
233