Issues (1965)

html/user/get_output2.php (17 issues)

1
<?php
2
3
// This file is part of BOINC.
4
// http://boinc.berkeley.edu
5
// Copyright (C) 2023 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
// web API for fetching output files.
21
// This is for apps that don't use an assimilator
22
// that moves output files into project/results/...
23
// I.e., the output files are in the upload hierarchy with physical names
24
//
25
// For assim_move apps, use get_output3.php instead of this.
26
27
// This is an updated version of get_output.php;
28
// I didn't want to change that on the (unlikely) chance
29
// that someone is using it.
30
31
// args:
32
// cmd: batch, workunit, result or file
33
// auth: optional user authenticator (if not logged in)
34
// result:
35
//      result_id
36
// workunit:
37
//      wu_id
38
// batch:
39
//      batch_id
40
// file:
41
//      result_id
42
//      file_num
43
//
44
// action (see https://github.com/BOINC/boinc/issues/5262):
45
// result: if 1 output file, return it as resultname__logicalname
46
//          (that way you know what it is, and it has the right extension)
47
//      else return a zip file as resultname.zip,
48
//          containing the output files with their logical names
49
// workunit:
50
//      as above, for canonical instance
51
// batch:
52
//      as above for each workunit; return as batch_(batchid).zip
53
//
54
// In the result and workunit cases, they must be part of a batch
55
// (so that we know who the submitter is)
56
57
require_once("../inc/boinc_db.inc");
58
require_once("../inc/util.inc");
59
require_once("../inc/dir_hier.inc");
60
require_once("../inc/submit_util.inc");
61
62
function check_auth($auth, $batch) {
63
    $user = BoincUser::lookup_id($batch->user_id);
64
    if (!$user) die("no batch owner for $batch->id");
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...
65
    if ($user->authenticator != $auth) die('bad auth');
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...
66
}
67
68
function do_result_aux($result, $batch, $file_num=null) {
69
    $phys_names = get_outfile_phys_names($result);
70
    $log_names = get_outfile_log_names($result);
71
    if ($file_num !== null) {
72
        $path = upload_path($phys_names[$file_num]);
73
        do_download($path,
74
            sprintf("%s__%s", $result->name, $log_names[$file_num])
0 ignored issues
show
The call to do_download() has too many arguments starting with sprintf('%s__%s', $resul... $log_names[$file_num]). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        /** @scrutinizer ignore-call */ 
75
        do_download($path,

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
75
        );
76
    }
77
    if (count($phys_names) == 1) {
78
        $path = upload_path($phys_names[0]);
79
        do_download($path,
80
            sprintf("%s__%s", $result->name, $log_names[0])
81
        );
82
    } else {
83
        // make a temp dir in submit/batchid;
84
        // make symlinks there, and zip it
85
        $dir_path = "submit/$batch->id/$result->name";
86
        system("rm -r $dir_path");
87
        mkdir($dir_path);
88
        for ($i=0; $i<count($phys_names); $i++) {
0 ignored issues
show
Coding Style Performance introduced by
The use of count() inside a loop condition is not allowed; assign the return value to a variable and use the variable in the loop condition instead
Loading history...
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
89
            $cmd = sprintf('ln -s %s %s/%s',
90
                upload_path($phys_names[$i]),
91
                $dir_path,
92
                $log_names[$i]
93
            );
94
            system($cmd);
95
        }
96
        $cmd = sprintf('cd submit/%d; zip %s',
97
            $batch->id,
98
            $result->name
99
        );
100
        system($cmd);
101
        do_download("$dir_path.zip");
102
    }
103
}
104
105
function do_result($result_id, $auth, $file_num=null) {
106
    $result = BoincResult::lookup_id($result_id);
107
    if (!$result) die("no result $result_id");
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...
108
    $workunit = BoincWorkunit::lookup_id($result->workunitid);
109
    if (!$workunit) die("no workunit for $result_id");
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...
110
    $batch = BoincBatch::lookup_id($workunit->batch);
111
    if (!$batch) die("no batch for $result_id");
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...
112
    check_auth($auth, $batch);
113
    do_result_aux($result, $batch, $file_num);
114
}
115
116
function do_wu($wu_id, $auth) {
117
    $workunit = BoincWorkunit::lookup_id($wu_id);
118
    if (!$workunit) die("no workunit for $result_id");
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...
Comprehensibility Best Practice introduced by
The variable $result_id does not exist. Did you maybe mean $result?
Loading history...
119
    $batch = BoincBatch::lookup_id($workunit->batch);
120
    if (!$batch) die("no batch for $result_id");
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...
121
    $result = BoincResult::lookup_id($workunit->canonical_resultid);
122
    do_result_aux($result, $batch);
123
}
124
125
// make a temp dir in submit/batchid
126
// for each workunit in batch,
127
// put symlinks to its output file (or a dir of its output files) there.
128
// send the zipped temp dir.
129
//
130
function do_batch($batch_id, $auth) {
131
    $batch = BoincBatch::lookup_id($batch_id);
132
    if (!$batch) die("no batch $batch_id");
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...
133
    $dir_path = sprintf('submit/%d/batch_%d', $batch_id, $batch_id);
134
    system("rm -r $dir_path");
135
    mkdir($dir_path);
136
137
    $wus = BoincWorkunit::enum("batch=$batch_id and canonical_resultid<>0");
138
    foreach ($wus as $wu) {
139
        $result = BoincResult::lookup_id($wu->canonical_resultid);
140
        $phys_names = get_outfile_phys_names($result);
141
        $log_names = get_outfile_log_names($result);
142
        if (count($phys_names) == 1) {
143
            $cmd = sprintf('ln -s %s %s/%s__%s',
144
                upload_path($phys_names[0]),
145
                $dir_path,
146
                $result->name,
147
                $log_names[0]
148
            );
149
            system($cmd);
150
        } else {
151
            mkdir(sprintf('%s/%s', $dir_path, $result->name));
152
            for ($i=0; $i<count($phys_names); $i++) {
0 ignored issues
show
Coding Style Performance introduced by
The use of count() inside a loop condition is not allowed; assign the return value to a variable and use the variable in the loop condition instead
Loading history...
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
153
                $cmd = sprintf('ln -s %s %s/%s/%s',
154
                    upload_path($phys_names[$i]),
155
                    $dir_path,
156
                    $result->name,
157
                    $log_names[$i]
158
                );
159
                system($cmd);
160
            }
161
        }
162
    }
163
    $cmd = sprintf('cd submit/%d/batch_%d; zip -q -r ../batch_%d.zip *',
164
        $batch_id,
165
        $batch_id,
166
        $batch_id
167
    );
168
    system($cmd);
169
    do_download(sprintf('submit/%d/batch_%d.zip', $batch_id, $batch_id));
170
    // todo: clean up
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
171
}
172
173
$cmd = get_str('cmd');
174
$user = get_logged_in_user(false);
0 ignored issues
show
Are you sure the assignment to $user is correct as get_logged_in_user(false) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
175
if ($user) {
176
    $auth = $user->authenticator;
177
} else {
178
    $auth = get_str('auth');
179
}
180
switch ($cmd) {
181
case 'result':
182
    do_result(get_int('result_id'), $auth);
183
    break;
184
case 'workunit':
185
    do_wu(get_int('wu_id'), $auth);
186
    break;
187
case 'batch':
188
    do_batch(get_int('batch_id'), $auth);
189
    break;
190
case 'file':
191
    do_result(get_int('result_id'), $auth, get_int('file_num'));
192
    break;
193
default:
0 ignored issues
show
DEFAULT keyword must be indented 4 spaces from SWITCH keyword
Loading history...
194
    die("bad cmd\n");
195
}
196
?>
197