Issues (1839)

html/user/tree_threader.php (7 issues)

1
<?php
2
3
// Handler for TreeThreader remote job submission.
4
//
5
// Assumptions:
6
// - there is a file "tree_threader_templates_files" in the project root
7
//   containing (one per line) the names of files containing
8
//   gzipped collections of template files
9
// - These files are in the download hierarchy.
10
11
require_once("../inc/boinc_db.inc");
12
require_once("../inc/submit_db.inc");
13
require_once("../inc/xml.inc");
14
require_once("../inc/dir_hier.inc");
15
require_once("../inc/result.inc");
16
require_once("../inc/submit_util.inc");
17
18
display_errors();
19
20
$app_name = "treeThreader";
21
$log = fopen("/tmp/tt_job.log","a+");
22
23
function error($s) {
24
    echo "<error>\n<message>$s</message>\n</error>\n";
25
    exit;
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...
26
}
27
28
function handle_submit($r, $user, $app) {
29
    global $app_name,$log;
30
31
    $timestamp = date("Y-m-d H:i",time());
32
	// read the list of template filenames
33
	//
34
	$files = file("../../tree_threader_template_files");
35
	if ($files === false) {
36
        fwrite($log,"$timestamp\ttemplate file tree_threader_template_files\n");
37
        error("no templates file");
38
39
    }
40
	$njobs = sizeof($files);
41
	$now = time();
42
    $batch_id = BoincBatch::insert(
43
		"(user_id, create_time, njobs, name, app_id, state) values ($user->id, $now, $njobs, 'tree_threader batch', $app->id, ".BATCH_STATE_IN_PROGRESS.")"
44
    );
45
    if (!$batch_id) {
46
        $log_msg = "$timestamp\tfailed to create batch for user $user->id\n";
47
        fwrite($log, $log_msg);
48
        die("couldn't create batch\n");
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...
49
    } else {
50
        $log_msg = "$timestamp\tcreated batch $batch_id for user $user->id\n";
51
        fwrite($log, $log_msg);
52
    }
53
54
    // move the sequence file to the download hier
55
    //
56
    $config = simplexml_load_string(file_get_contents("../../config.xml"));
57
    $fanout = (int)$config->config->uldl_dir_fanout;
58
    $download_dir = trim((string)$config->config->download_dir);
59
60
    $seq_fname = "treeThreader_sequence_$batch_id.tar.gz";
61
    $seq_path = dir_hier_path($seq_fname, $download_dir, $fanout);
62
    $tmp_name = $_FILES['seq_file']['tmp_name'];
63
    $ret = rename($tmp_name, $seq_path);
64
    if ($ret === false) {
65
        error("couldn't rename sequence file");
66
    }
67
68
    $i = 1;
69
	foreach ($files as $file) {
70
        $file = trim($file);
71
        $wu_name = "ICT_".$batch_id."_$i";
72
73
		$cmd = "cd ../..; ./bin/create_work --appname $app_name --batch $batch_id --wu_name $wu_name --wu_template templates/ICT_in --result_template templates/ICT_out $seq_fname $file";
74
        fwrite($log, "$timestamp\t$cmd\n");
75
        system($cmd, $ret);
0 ignored issues
show
$ret of type true is incompatible with the type integer expected by parameter $result_code of system(). ( Ignorable by Annotation )

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

75
        system($cmd, /** @scrutinizer ignore-type */ $ret);
Loading history...
76
		if ($ret != 0) {
77
            fwrite($log, "can not creat job $wu_name\n");
78
			error("can't create job");
79
        }
80
        $i++;
81
	}
82
	echo "<tt_reply>\n<batch_id>$batch_id</batch_id>\n</tt_reply>\n";
83
}
84
85
// Enumerate all the successfully completed WUs for this batch.
86
// Each output file is a .zip that unzips into a directory ali/.
87
// Combine their output files into a zip file in /tmp,
88
// make a symbolic link to this from /download,
89
// and return the resulting URL
90
//
91
function handle_get_output($r, $batch) {
92
    global $log;
93
    $timestamp = date("Y-m-d H:i",time());
94
	$wus = BoincWorkUnit::enum("batch=$batch->id");
95
	$outdir = "/tmp/treeThreader_result_".$batch->id;
96
    @mkdir($outdir);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

96
    /** @scrutinizer ignore-unhandled */ @mkdir($outdir);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
97
	foreach ($wus as $wu) {
98
		if (!$wu->canonical_resultid) continue;
99
		$result = BoincResult::lookup_id($wu->canonical_resultid);
100
		if (!$result) continue;
101
		$paths = get_outfile_paths($result);
102
		if (sizeof($paths) < 1) continue;
103
104
		// there's only one output file
105
		//
106
		$path = $paths[0];
107
108
		// unzip it into a directory in /tmp
109
		//
110
		$dir = "/tmp/$wu->name";
111
        @mkdir($dir);
112
		$cmd = "cd $dir; unzip -q $path";
113
		system($cmd, $ret);
114
		if ($ret != 0) {
115
			error("can't unzip output file");
116
		}
117
		$cmd = "cp $dir/Aln/* $outdir";
118
		system($cmd, $ret);
119
		if ($ret != 0) {
120
			error("can't copy output files");
121
		}
122
123
        system("rm -rf $dir");
124
	}
125
126
	$cmd = "zip -r -q $outdir $outdir";
127
	system($cmd, $ret);
128
	if ($ret != $ret) {
129
		error("can't zip output files");
130
	}
131
	$fname = "treeThreader_result_".$batch->id.".zip";
132
    $treeThreader_dir="treeThreaderResult";
133
    if(!is_dir("../../download/$treeThreader_dir"))mkdir("../../download/$treeThreader_dir");
134
	@symlink("/tmp/$fname", "../../download/$treeThreader_dir/$fname");
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for symlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

134
	/** @scrutinizer ignore-unhandled */ @symlink("/tmp/$fname", "../../download/$treeThreader_dir/$fname");

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
135
    system("rm -fr $outdir");
136
    $config = simplexml_load_string(file_get_contents("../../config.xml"));
137
    $download_url = trim((string)$config->config->download_url);
138
	echo "<tt_reply>\n<url>$download_url/$treeThreader_dir/$fname</url>\n</tt_reply>\n";
139
    $log_msg="$timestamp\tuser $batch->user_id downloads results for batch $batch->id : $download_url/$treeThreader_dir/$fname\n";
140
    fwrite($log, $log_msg);
141
}
142
143
xml_header();
144
145
if (1) {
146
    $r = simplexml_load_string($_POST['request']);
147
} else {
148
    $x = file_get_contents("xml_req");
149
    $r = simplexml_load_string($x);
150
}
151
152
if (!$r) {
0 ignored issues
show
$r is of type SimpleXMLElement, thus it always evaluated to true.
Loading history...
153
    error("can't parse request message");
154
}
155
156
// authenticate the user
157
//
158
$auth = (string)$r->auth;
159
$user = BoincUser::lookup("authenticator='$auth'");
160
if (!$user) error("invalid authenticator");
161
$user_submit = BoincUserSubmit::lookup_userid($user->id);
162
if (!$user_submit) error("no submit access");
163
$app = BoincApp::lookup("name='$app_name'");
164
if (!$app) error("no tree_threader app");
165
166
if (!$user_submit->submit_all) {
167
    $usa = BoincUserSubmitApp::lookup("user_id=$user->id and app_id=$app->id");
168
    if (!$usa) {
169
        error("no submit access");
170
    }
171
}
172
173
switch ((string)$r->action) {
174
    case 'submit':
175
        handle_submit($r, $user, $app);
176
        break;
177
    case 'get_output':
178
		$batch_id = (int)$r->batch_id;
179
		$batch = BoincBatch::lookup_id($batch_id);
180
		if (!$batch) error("no such batch");
181
		if ($batch->user_id != $user->id) error("not owner of batch");
182
		handle_get_output($r, $batch);
183
		break;
184
    default: error("bad command");
0 ignored issues
show
DEFAULT case must have a breaking statement
Loading history...
185
}
186
187
?>
188