|
1
|
|
|
<?php |
|
2
|
|
|
// This file is part of BOINC. |
|
3
|
|
|
// http://boinc.berkeley.edu |
|
4
|
|
|
// Copyright (C) 2008 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
|
|
|
// PHP utility functions for cmdline tools and RPC handlers |
|
20
|
|
|
// as well as web pages. |
|
21
|
|
|
// Doesn't contain web-specific stuff like translation.inc |
|
22
|
|
|
|
|
23
|
|
|
require_once("../inc/random_compat/random.inc"); |
|
24
|
|
|
|
|
25
|
|
|
// show PHP errors in output (e.g. web pages). |
|
26
|
|
|
// Call this from your project.inc if you want. |
|
27
|
|
|
// Not recommended for production projects; |
|
28
|
|
|
// check the Apache error log instead. |
|
29
|
|
|
// |
|
30
|
|
|
function display_errors() { |
|
31
|
|
|
error_reporting(E_ALL); |
|
32
|
|
|
ini_set('display_errors', true); |
|
|
|
|
|
|
33
|
|
|
ini_set('display_startup_errors', true); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// always log errors |
|
37
|
|
|
ini_set('log_errors', true); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
// set to true in RPC handlers. |
|
40
|
|
|
// Suppresses output that would invalidate the XML |
|
41
|
|
|
$generating_xml = false; |
|
42
|
|
|
|
|
43
|
|
|
// get project dir, assuming we're running in html/user or html/ops |
|
44
|
|
|
function project_dir() { |
|
45
|
|
|
$d = dirname(__FILE__); |
|
46
|
|
|
return "$d/../.."; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
function web_stopped() { |
|
50
|
|
|
$d = project_dir(); |
|
51
|
|
|
return file_exists("$d/stop_web"); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
function sched_stopped() { |
|
55
|
|
|
$d = project_dir(); |
|
56
|
|
|
return file_exists("$d/stop_sched"); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
function xml_error($num=-1, $msg=null, $file=null, $line=null) { |
|
60
|
|
|
global $xml_outer_tag; |
|
61
|
|
|
if (!$msg) { |
|
62
|
|
|
switch($num) { |
|
63
|
|
|
case -112: $msg = "Invalid XML"; break; |
|
64
|
|
|
case -136: $msg = "Not found"; break; |
|
65
|
|
|
case -137: $msg = "Name or email address is not unique"; break; |
|
66
|
|
|
case -138: $msg = "Can't access database"; break; |
|
67
|
|
|
case -183: $msg = "Project is temporarily offline"; break; |
|
68
|
|
|
case -205: $msg = "Email address has invalid syntax"; break; |
|
69
|
|
|
case -206: $msg = "Invalid password"; break; |
|
70
|
|
|
case -207: $msg = "Email address is not unique"; break; |
|
71
|
|
|
case -208: $msg = "Account creation is disabled"; break; |
|
72
|
|
|
case -209: $msg = "Invalid invitation code"; break; |
|
73
|
|
|
case -210: $msg = "Invalid request method"; break; |
|
74
|
|
|
default: $msg = "Unknown error"; break; |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
echo "<error> |
|
78
|
|
|
<error_num>$num</error_num> |
|
79
|
|
|
<error_msg>$msg</error_msg> |
|
80
|
|
|
"; |
|
81
|
|
|
if ($file) { |
|
82
|
|
|
echo " <file>$file</file>\n"; |
|
83
|
|
|
} |
|
84
|
|
|
if ($line) { |
|
85
|
|
|
echo " <line>$line</line>\n"; |
|
86
|
|
|
} |
|
87
|
|
|
echo "</error>\n"; |
|
88
|
|
|
if (isset($xml_outer_tag) && $xml_outer_tag != "") { |
|
89
|
|
|
echo "</$xml_outer_tag>\n"; |
|
90
|
|
|
} |
|
91
|
|
|
exit(); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$g_config = null; |
|
95
|
|
|
function get_config() { |
|
96
|
|
|
global $g_config; |
|
97
|
|
|
if ($g_config == null) { |
|
98
|
|
|
$d = project_dir(); |
|
99
|
|
|
$g_config = file_get_contents("$d/config.xml"); |
|
100
|
|
|
} |
|
101
|
|
|
return $g_config; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Look for an element in a line of XML text |
|
105
|
|
|
// If it's a single-tag element, and it's present, just return the tag |
|
106
|
|
|
// |
|
107
|
|
|
function parse_element($xml, $tag) { |
|
108
|
|
|
$closetag = "</" . substr($tag,1); |
|
109
|
|
|
$x = strstr($xml, $tag); |
|
110
|
|
|
if ($x) { |
|
111
|
|
|
if (strstr($tag, "/>")) return $tag; |
|
112
|
|
|
$y = substr($x, strlen($tag)); |
|
113
|
|
|
$n = strpos($y, $closetag); |
|
114
|
|
|
if ($n) { |
|
115
|
|
|
$element = substr($y, 0, $n); |
|
116
|
|
|
return trim($element); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
return null; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
function parse_next_element($xml, $tag, &$cursor) { |
|
123
|
|
|
$element = null; |
|
124
|
|
|
$closetag = "</" . substr($tag,1); |
|
125
|
|
|
$pos = substr($xml,$cursor); |
|
126
|
|
|
$x = strstr($pos, $tag); |
|
127
|
|
|
if ($x) { |
|
128
|
|
|
if (strstr($tag, "/>")) return $tag; |
|
129
|
|
|
$y = substr($x, strlen($tag)); |
|
130
|
|
|
$n = strpos($y, $closetag); |
|
131
|
|
|
if ($n) { |
|
132
|
|
|
$element = substr($y, 0, $n); |
|
133
|
|
|
} |
|
134
|
|
|
$cursor = (strlen($xml) - strlen($x)) + strlen($tag) + strlen($closetag) + strlen($element); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
if (!$element) return null; |
|
137
|
|
|
return trim($element); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// return true if XML contains either <tag/> or <tag>1</tag> |
|
141
|
|
|
// |
|
142
|
|
|
function parse_bool($xml, $tag) { |
|
143
|
|
|
$x = "<$tag/>"; |
|
144
|
|
|
if (strstr($xml, $x)) return true; |
|
145
|
|
|
$x = "<$tag>"; |
|
146
|
|
|
$y = (int)parse_element($xml, $x); |
|
147
|
|
|
if ($y != 0) return true; |
|
148
|
|
|
return false; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// look for a particular element in the config file |
|
152
|
|
|
// |
|
153
|
|
|
function parse_config($config, $tag) { |
|
154
|
|
|
$element = parse_element($config, $tag); |
|
155
|
|
|
return $element; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
// uniform 0..1 |
|
159
|
|
|
// |
|
160
|
|
|
function drand() { |
|
161
|
|
|
return ((double)rand())/getrandmax(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
// does the plan class use a GPU? |
|
165
|
|
|
// |
|
166
|
|
|
function is_gpu($plan_class) { |
|
167
|
|
|
if (strstr($plan_class, "ati")) return true; |
|
168
|
|
|
if (strstr($plan_class, "cuda")) return true; |
|
169
|
|
|
if (strstr($plan_class, "nvidia")) return true; |
|
170
|
|
|
if (strstr($plan_class, "intel_gpu")) return true; |
|
171
|
|
|
if (strstr($plan_class, "apple_gpu")) return true; |
|
172
|
|
|
return false; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
// the same as file_get_contents() but uses curl |
|
176
|
|
|
// |
|
177
|
|
|
function url_get_contents($url) { |
|
178
|
|
|
$ch = curl_init($url); |
|
179
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false); |
|
180
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
181
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
|
182
|
|
|
curl_setopt($ch, CURLOPT_MAXREDIRS, 5); |
|
183
|
|
|
$content = curl_exec($ch); |
|
184
|
|
|
curl_close($ch); |
|
185
|
|
|
return $content; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
// return hard-to-guess string of 32 random hex chars |
|
189
|
|
|
// |
|
190
|
|
|
function random_string() { |
|
191
|
|
|
return bin2hex(random_bytes(16)); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
// return high-resolution time |
|
195
|
|
|
// |
|
196
|
|
|
function dtime() { |
|
197
|
|
|
return microtime(true); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
// security vulnerabilities and user-supplied strings: |
|
201
|
|
|
// sources: |
|
202
|
|
|
// GET and POST arguments |
|
203
|
|
|
// including XML documents passed as args to RPC handlers |
|
204
|
|
|
// cookies |
|
205
|
|
|
// |
|
206
|
|
|
// when used as SQL query args: |
|
207
|
|
|
// use BoincDb::escape_string() to prevent SQL injection |
|
208
|
|
|
// when shown as HTML output |
|
209
|
|
|
// (e.g. 'not found' error pages, user names, forum posts) |
|
210
|
|
|
// use htmlspecialchars() to prevent XSS |
|
211
|
|
|
// when used as file or dir name |
|
212
|
|
|
// use is_valid_filename() |
|
213
|
|
|
|
|
214
|
|
|
// is $x a valid file (or dir) name? |
|
215
|
|
|
// we want to avoid |
|
216
|
|
|
// FS traversal, e.g. "../../foo" or "/usr/lib/..." |
|
217
|
|
|
// shell command injection, e.g. "foo; rm*" |
|
218
|
|
|
// XSS stuff |
|
219
|
|
|
// let's be conservative and allow only 'POSIX fully portable filenames', |
|
220
|
|
|
// which can have only A-Z a-z 0-9 . - _ |
|
221
|
|
|
// In some cases filenames are used on volunteer hosts, |
|
222
|
|
|
// whose OSs may have such restrictions. |
|
223
|
|
|
// |
|
224
|
|
|
function is_valid_filename($x) { |
|
225
|
|
|
if (strlen($x)>255) return false; |
|
226
|
|
|
// \w means A-Za-z0-9_ |
|
227
|
|
|
return preg_match('/^[\w\-.]+$/', $x); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
function filename_rules() { |
|
231
|
|
|
return 'Names can contain only A-Z a-z 0-9 . - _'; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
?> |
|
235
|
|
|
|