Passed
Push — dpa_buda5 ( e14f71...f24fc2 )
by David
10:02
created

add_form()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 50
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 29
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 50
rs 9.456
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
// Per-user "file sandboxes".
20
// Files are stored in <project>/sandbox/<userid>/
21
// File infos (md5/size) are scored in a parallel dir
22
// <project>/sandbox/<userid>/.md5/
23
24
// NOTE: PHP's default max file upload size is 2MB.
25
// To increase this, edit /etc/php.ini, and change, e.g.
26
//
27
// upload_max_filesize = 64M
28
// post_max_size = 64M
29
30
require_once("../inc/sandbox.inc");
31
require_once("../inc/submit_util.inc");
32
33
display_errors();
34
35
function add_form() {
36
    page_head('Upload files to your sandbox');
37
    echo "
38
        There are several ways to upload files:
39
        <p>
40
        <hr>
41
        <h3>Upload files from this computer</h3>
42
    ";
43
    form_start('sandbox.php', 'post', 'ENCTYPE="multipart/form-data"');
44
    form_general('',
45
        "NOTE: if you upload text files from Windows,
46
        they will be given CRLF line endings.
47
        If they are shell scripts, they won't work on Linux.
48
        Add shell scripts using 'Add text file' below."
49
    );
50
    form_input_hidden('action', 'upload_file');
51
    form_general('',
52
        '<input size=80 type=file name="new_file[]" multiple="multiple">'
53
    );
54
    form_submit('Upload');
55
    form_end();
56
if (0) {
57
    echo "
58
        <form action=sandbox.php method=post ENCTYPE=\"multipart/form-data\">
59
        <input type=hidden name=action value=upload_file>
60
        <p><p><input size=80 type=file name=\"new_file[]\" multiple=\"multiple\">
61
        <p> <input class=\"btn btn-success\" type=submit value=Upload>
62
        </form>
63
    ";
64
}
65
    echo "
66
        <hr>
67
        <h3>Add text file</h3>
68
    ";
69
    form_start('sandbox.php', 'post');
70
    form_input_hidden('action', 'add_file');
71
    form_input_text('Name', 'name');
72
    form_input_textarea('Contents', 'contents');
73
    form_submit('OK');
74
    form_end();
75
    echo "
76
        <hr>
77
        <h3>Get web file</h3>
78
    ";
79
    form_start('sandbox.php', 'post');
80
    form_input_hidden('action', 'get_file');
81
    form_input_text('URL', 'url');
82
    form_submit('OK');
83
    form_end();
84
    page_tail();
85
}
86
87
function list_files($user) {
88
    $dir = sandbox_dir($user);
89
    if (!is_dir($dir)) error_page("Can't open sandbox directory");
90
    page_head("File sandbox");
91
    $notice = get_str('notice', true);
92
    if ($notice) {
93
        echo "<p>$notice<hr>";
94
    }
95
    echo "
96
        <p>
97
        Your 'File sandbox' is where you store files to this BOINC server.
98
    ";
99
    $files = array();
100
    foreach (scandir($dir) as $f) {
101
        if ($f[0] == '.') continue;
102
        $files[] = $f;
103
    }
104
    if (count($files) == 0) {
105
        echo "Your sandbox is currently empty.";
106
    } else {
107
        sort($files);
108
        start_table();
109
        table_header("Name<br><small>(click to view text files)</small>", "Modified", "Size (bytes)", "MD5", "Delete","Download");
110
        foreach ($files as $f) {
111
            [$md5, $size] = sandbox_parse_info_file($user, $f);
112
            $path = "$dir/$f";
113
            $ct = time_str(filemtime($path));
114
            table_row(
115
                "<a href=sandbox.php?action=view_file&name=$f>$f</a>",
116
                $ct,
117
                $size,
118
                $md5,
119
                button_text(
120
                    "sandbox.php?action=delete_file&name=$f",
121
                    "Delete"
122
                ),
123
                button_text(
124
                    "sandbox.php?action=download_file&name=$f",
125
                    "Download"
126
                )
127
            );
128
        }
129
        end_table();
130
    }
131
    show_button('sandbox.php?action=add_form', 'Upload files');
132
    page_tail();
133
}
134
135
// upload one or more files
136
137
function upload_file($user) {
138
    $notice = "";
139
    $dir = sandbox_dir($user);
140
    $count = count($_FILES['new_file']['tmp_name']);
141
    for ($i=0; $i<$count; $i++) {
142
        $tmp_name = $_FILES['new_file']['tmp_name'][$i];
143
        if (!is_uploaded_file($tmp_name)) {
144
            error_page("$tmp_name is not uploaded file");
145
        }
146
        $name = $_FILES['new_file']['name'][$i];
147
        if (strstr($name, "/")) {
148
            error_page("no / allowed");
149
        }
150
        if (file_exists("$dir/$name")) {
151
            $notice .= "can't upload $name; file exists.<br>";
152
            continue;
153
        }
154
        move_uploaded_file($tmp_name, "$dir/$name");
155
156
        // write info file
157
        //
158
        [$md5, $size] = get_file_info("$dir/$name");
159
        write_info_file("$dir/.md5/$name", $md5, $size);
160
161
        $notice .= "Uploaded file <strong>$name</strong><br/>";
162
    }
163
    header(sprintf('Location: sandbox.php?notice=%s', urlencode($notice)));
164
}
165
166
function add_file($user) {
167
    $dir = sandbox_dir($user);
168
    $name = post_str('name');
169
    if (!$name) error_page('No name given');
170
    if (file_exists("$dir/$name")) {
171
        error_page("file $name exists");
172
    }
173
    $contents = post_str('contents');
174
    $contents = str_replace("\r\n", "\n", $contents);
175
    file_put_contents("$dir/$name", $contents);
176
177
    [$md5, $size] = get_file_info("$dir/$name");
178
    write_info_file("$dir/.md5/$name", $md5, $size);
179
180
    $notice = "Added file <strong>$name</strong> ($size bytes)";
181
    header(sprintf('Location: sandbox.php?notice=%s', urlencode($notice)));
182
}
183
184
function get_file($user) {
185
    $dir = sandbox_dir($user);
186
    $url = post_str('url');
187
    $fname = basename($url);
188
    $path = "$dir/$fname";
189
    if (file_exists($path)) {
190
        error_page("File $fname exists; delete it first.");
191
    }
192
    copy($url, $path);
193
    $notice = "Fetched file from <strong>$url</strong><br/>";
194
    header(sprintf('Location: sandbox.php?notice=%s', urlencode($notice)));
195
}
196
197
// delete a sandbox file.
198
//
199
function delete_file($user) {
200
    $name = get_str('name');
201
    $dir = sandbox_dir($user);
202
    unlink("$dir/$name");
203
    unlink("$dir/.md5/$name");
204
    $notice = "<strong>$name</strong> was deleted from your sandbox<br/>";
205
    header(sprintf('Location: sandbox.php?notice=%s', urlencode($notice)));
206
}
207
208
function download_file($user) {
209
    $name = get_str('name');
210
    $dir = sandbox_dir($user);
211
    do_download("$dir/$name");
212
}
213
214
function view_file($user) {
215
    $name = get_str('name');
216
    $dir = sandbox_dir($user);
217
    $path = "$dir/$name";
218
    if (!is_file($path)) {
219
        error_path("no such file $name");
220
    }
221
    echo "<pre>\n";
222
    readfile($path);
223
    echo "</pre>\n";
224
}
225
226
$user = get_logged_in_user();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $user is correct as get_logged_in_user() 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...
227
if (!has_file_access($user)) error_page("no job submission access");
228
229
$action = get_str('action', true);
230
if (!$action) $action = post_str('action', true);
231
232
switch ($action) {
233
case '': list_files($user); break;
234
case 'upload_file': upload_file($user); break;
235
case 'add_file': add_file($user); break;
236
case 'get_file': get_file($user); break;
237
case 'delete_file': delete_file($user); break;
238
case 'download_file': download_file($user); break;
239
case 'view_file': view_file($user); break;
0 ignored issues
show
Unused Code introduced by
The call to view_file() has too many arguments starting with $user. ( Ignorable by Annotation )

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

239
case 'view_file': /** @scrutinizer ignore-call */ view_file($user); break;

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...
240
case 'add_form': add_form($user); break;
0 ignored issues
show
Unused Code introduced by
The call to add_form() has too many arguments starting with $user. ( Ignorable by Annotation )

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

240
case 'add_form': /** @scrutinizer ignore-call */ add_form($user); break;

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...
241
default: error_page("no such action: $action");
0 ignored issues
show
Coding Style introduced by
DEFAULT keyword must be indented 4 spaces from SWITCH keyword
Loading history...
Coding Style introduced by
DEFAULT case must have a breaking statement
Loading history...
242
}
243
244
?>
245