|
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
|
|
|
// minimal set of utility functions, usable outside a BOINC project. |
|
20
|
|
|
// Doesn't pull in translation.inc etc. |
|
21
|
|
|
|
|
22
|
|
|
require_once("../inc/random_compat/random.inc"); |
|
23
|
|
|
|
|
24
|
|
|
$generating_xml = false; |
|
25
|
|
|
|
|
26
|
|
|
function project_dir() { |
|
27
|
|
|
$d = dirname(__FILE__); |
|
28
|
|
|
return "$d/../.."; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
function web_stopped() { |
|
32
|
|
|
$d = project_dir(); |
|
33
|
|
|
return file_exists("$d/stop_web"); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
function sched_stopped() { |
|
37
|
|
|
$d = project_dir(); |
|
38
|
|
|
return file_exists("$d/stop_sched"); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
function show_page($x, $y) { |
|
42
|
|
|
echo " |
|
43
|
|
|
<title>$x</title> |
|
44
|
|
|
<h1>$x</h1> |
|
45
|
|
|
$y |
|
46
|
|
|
"; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
function xml_error($num=-1, $msg=null, $file=null, $line=null) { |
|
50
|
|
|
global $xml_outer_tag; |
|
51
|
|
|
if (!$msg) { |
|
52
|
|
|
switch($num) { |
|
53
|
|
|
case -112: $msg = "Invalid XML"; break; |
|
54
|
|
|
case -136: $msg = "Not found"; break; |
|
55
|
|
|
case -137: $msg = "Name or email address is not unique"; break; |
|
56
|
|
|
case -138: $msg = "Can't access database"; break; |
|
57
|
|
|
case -183: $msg = "Project is temporarily offline"; break; |
|
58
|
|
|
case -205: $msg = "Email address has invalid syntax"; break; |
|
59
|
|
|
case -206: $msg = "Invalid password"; break; |
|
60
|
|
|
case -207: $msg = "Email address is not unique"; break; |
|
61
|
|
|
case -208: $msg = "Account creation is disabled"; break; |
|
62
|
|
|
case -209: $msg = "Invalid invitation code"; break; |
|
63
|
|
|
case -210: $msg = "Invalid request method"; break; |
|
64
|
|
|
default: $msg = "Unknown error"; break; |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
echo "<error> |
|
68
|
|
|
<error_num>$num</error_num> |
|
69
|
|
|
<error_msg>$msg</error_msg> |
|
70
|
|
|
"; |
|
71
|
|
|
if ($file) { |
|
72
|
|
|
echo " <file>$file</file>\n"; |
|
73
|
|
|
} |
|
74
|
|
|
if ($line) { |
|
75
|
|
|
echo " <line>$line</line>\n"; |
|
76
|
|
|
} |
|
77
|
|
|
echo "</error>\n"; |
|
78
|
|
|
if (isset($xml_outer_tag) && $xml_outer_tag != "") { |
|
79
|
|
|
echo "</$xml_outer_tag>\n"; |
|
80
|
|
|
} |
|
81
|
|
|
exit(); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$g_config = null; |
|
85
|
|
|
function get_config() { |
|
86
|
|
|
global $g_config; |
|
87
|
|
|
if ($g_config == null) { |
|
88
|
|
|
$d = project_dir(); |
|
89
|
|
|
$g_config = file_get_contents("$d/config.xml"); |
|
90
|
|
|
} |
|
91
|
|
|
return $g_config; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// Look for an element in a line of XML text |
|
95
|
|
|
// If it's a single-tag element, and it's present, just return the tag |
|
96
|
|
|
// |
|
97
|
|
|
function parse_element($xml, $tag) { |
|
98
|
|
|
$closetag = "</" . substr($tag,1); |
|
99
|
|
|
$x = strstr($xml, $tag); |
|
100
|
|
|
if ($x) { |
|
101
|
|
|
if (strstr($tag, "/>")) return $tag; |
|
102
|
|
|
$y = substr($x, strlen($tag)); |
|
103
|
|
|
$n = strpos($y, $closetag); |
|
104
|
|
|
if ($n) { |
|
105
|
|
|
$element = substr($y, 0, $n); |
|
106
|
|
|
return trim($element); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
return null; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
function parse_next_element($xml, $tag, &$cursor) { |
|
113
|
|
|
$element = null; |
|
114
|
|
|
$closetag = "</" . substr($tag,1); |
|
115
|
|
|
$pos = substr($xml,$cursor); |
|
116
|
|
|
$x = strstr($pos, $tag); |
|
117
|
|
|
if ($x) { |
|
118
|
|
|
if (strstr($tag, "/>")) return $tag; |
|
119
|
|
|
$y = substr($x, strlen($tag)); |
|
120
|
|
|
$n = strpos($y, $closetag); |
|
121
|
|
|
if ($n) { |
|
122
|
|
|
$element = substr($y, 0, $n); |
|
123
|
|
|
} |
|
124
|
|
|
$cursor = (strlen($xml) - strlen($x)) + strlen($tag) + strlen($closetag) + strlen($element); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
if (!$element) return null; |
|
127
|
|
|
return trim($element); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// return true if XML contains either <tag/> or <tag>1</tag> |
|
131
|
|
|
// |
|
132
|
|
|
function parse_bool($xml, $tag) { |
|
133
|
|
|
$x = "<$tag/>"; |
|
134
|
|
|
if (strstr($xml, $x)) return true; |
|
135
|
|
|
$x = "<$tag>"; |
|
136
|
|
|
$y = (int)parse_element($xml, $x); |
|
137
|
|
|
if ($y != 0) return true; |
|
138
|
|
|
return false; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
// look for a particular element in the config file |
|
142
|
|
|
// |
|
143
|
|
|
function parse_config($config, $tag) { |
|
144
|
|
|
$element = parse_element($config, $tag); |
|
145
|
|
|
return $element; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
function drand() { |
|
149
|
|
|
return ((double)rand())/getrandmax(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
// kludge |
|
153
|
|
|
// |
|
154
|
|
|
function is_gpu($plan_class) { |
|
155
|
|
|
if (strstr($plan_class, "ati")) return true; |
|
156
|
|
|
if (strstr($plan_class, "cuda")) return true; |
|
157
|
|
|
if (strstr($plan_class, "nvidia")) return true; |
|
158
|
|
|
if (strstr($plan_class, "intel_gpu")) return true; |
|
159
|
|
|
return false; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// the same as file_get_contents() but uses curl |
|
163
|
|
|
// |
|
164
|
|
|
function url_get_contents($url) { |
|
165
|
|
|
$ch = curl_init($url); |
|
166
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false); |
|
167
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
168
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
|
169
|
|
|
curl_setopt($ch, CURLOPT_MAXREDIRS, 5); |
|
170
|
|
|
$content = curl_exec($ch); |
|
171
|
|
|
curl_close($ch); |
|
172
|
|
|
return $content; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
// return hard-to-guess string of 32 random hex chars |
|
177
|
|
|
// |
|
178
|
|
|
function random_string() { |
|
179
|
|
|
return bin2hex(random_bytes(16)); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
// return high-resolution time |
|
183
|
|
|
// |
|
184
|
|
|
function dtime() { |
|
185
|
|
|
return microtime(true); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
// is $x a valid file (or dir) name? |
|
189
|
|
|
// |
|
190
|
|
|
function is_valid_filename($x) { |
|
191
|
|
|
if (htmlspecialchars($x) != $x) return false; |
|
192
|
|
|
if (strstr($x, '/')) return false; |
|
193
|
|
|
return true; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
?> |
|
197
|
|
|
|