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); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
33 | ini_set('display_startup_errors', true); |
||||
34 | } |
||||
35 | |||||
36 | // always log errors |
||||
37 | ini_set('log_errors', true); |
||||
0 ignored issues
–
show
true of type true is incompatible with the type string expected by parameter $value of ini_set() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
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; |
||||
0 ignored issues
–
show
|
|||||
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(); |
||||
0 ignored issues
–
show
|
|||||
92 | } |
||||
93 | |||||
94 | function get_config() { |
||||
95 | static $config; |
||||
96 | if ($config == null) { |
||||
97 | $d = project_dir(); |
||||
98 | $config = file_get_contents("$d/config.xml"); |
||||
99 | } |
||||
100 | return $config; |
||||
101 | } |
||||
102 | |||||
103 | // Look for an element in a line of XML text |
||||
104 | // If it's a single-tag element, and it's present, just return the tag |
||||
105 | // |
||||
106 | function parse_element($xml, $tag) { |
||||
107 | $closetag = "</" . substr($tag,1); |
||||
108 | $x = strstr($xml, $tag); |
||||
109 | if ($x) { |
||||
110 | if (strstr($tag, "/>")) return $tag; |
||||
111 | $y = substr($x, strlen($tag)); |
||||
112 | $n = strpos($y, $closetag); |
||||
113 | if ($n) { |
||||
114 | $element = substr($y, 0, $n); |
||||
115 | return trim($element); |
||||
116 | } |
||||
117 | } |
||||
118 | return null; |
||||
119 | } |
||||
120 | |||||
121 | function parse_next_element($xml, $tag, &$cursor) { |
||||
122 | $element = null; |
||||
123 | $closetag = "</" . substr($tag,1); |
||||
124 | $pos = substr($xml,$cursor); |
||||
125 | $x = strstr($pos, $tag); |
||||
126 | if ($x) { |
||||
127 | if (strstr($tag, "/>")) return $tag; |
||||
128 | $y = substr($x, strlen($tag)); |
||||
129 | $n = strpos($y, $closetag); |
||||
130 | if ($n) { |
||||
131 | $element = substr($y, 0, $n); |
||||
132 | } |
||||
133 | $cursor = (strlen($xml) - strlen($x)) + strlen($tag) + strlen($closetag) + strlen($element); |
||||
0 ignored issues
–
show
It seems like
$element can also be of type null ; however, parameter $string of strlen() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
134 | } |
||||
135 | if (!$element) return null; |
||||
136 | return trim($element); |
||||
137 | } |
||||
138 | |||||
139 | // return true if XML contains either <tag/> or <tag>1</tag> |
||||
140 | // |
||||
141 | function parse_bool($xml, $tag) { |
||||
142 | $x = "<$tag/>"; |
||||
143 | if (strstr($xml, $x)) return true; |
||||
144 | $x = "<$tag>"; |
||||
145 | $y = (int)parse_element($xml, $x); |
||||
146 | if ($y != 0) return true; |
||||
147 | return false; |
||||
148 | } |
||||
149 | |||||
150 | // look for a particular element in the config file |
||||
151 | // |
||||
152 | function parse_config($config, $tag) { |
||||
153 | $element = parse_element($config, $tag); |
||||
154 | return $element; |
||||
155 | } |
||||
156 | |||||
157 | // uniform 0..1 |
||||
158 | // |
||||
159 | function drand() { |
||||
160 | return ((double)rand())/getrandmax(); |
||||
161 | } |
||||
162 | |||||
163 | // does the plan class use a GPU? |
||||
164 | // |
||||
165 | function is_gpu($plan_class) { |
||||
166 | if (strstr($plan_class, "ati")) return true; |
||||
167 | if (strstr($plan_class, "cuda")) return true; |
||||
168 | if (strstr($plan_class, "nvidia")) return true; |
||||
169 | if (strstr($plan_class, "intel_gpu")) return true; |
||||
170 | if (strstr($plan_class, "apple_gpu")) return true; |
||||
171 | return false; |
||||
172 | } |
||||
173 | |||||
174 | // the same as file_get_contents() but uses curl |
||||
175 | // |
||||
176 | function url_get_contents($url) { |
||||
177 | $ch = curl_init($url); |
||||
178 | curl_setopt($ch, CURLOPT_HEADER, false); |
||||
179 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||||
180 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
||||
181 | curl_setopt($ch, CURLOPT_MAXREDIRS, 5); |
||||
182 | $content = curl_exec($ch); |
||||
183 | curl_close($ch); |
||||
184 | return $content; |
||||
185 | } |
||||
186 | |||||
187 | // return hard-to-guess string of 32 random hex chars |
||||
188 | // |
||||
189 | function random_string() { |
||||
190 | return bin2hex(random_bytes(16)); |
||||
191 | } |
||||
192 | |||||
193 | // return high-resolution time |
||||
194 | // |
||||
195 | function dtime() { |
||||
196 | return microtime(true); |
||||
197 | } |
||||
198 | |||||
199 | // security vulnerabilities and user-supplied strings: |
||||
200 | // sources: |
||||
201 | // GET and POST arguments |
||||
202 | // including XML documents passed as args to RPC handlers |
||||
203 | // cookies |
||||
204 | // |
||||
205 | // when used as SQL query args: |
||||
206 | // use BoincDb::escape_string() to prevent SQL injection |
||||
207 | // when shown as HTML output |
||||
208 | // (e.g. 'not found' error pages, user names, forum posts) |
||||
209 | // use htmlspecialchars() to prevent XSS |
||||
210 | // when used as file or dir name |
||||
211 | // use is_valid_filename() |
||||
212 | |||||
213 | // is $x a valid file (or dir) name? |
||||
214 | // we want to avoid |
||||
215 | // FS traversal, e.g. "../../foo" or "/usr/lib/..." |
||||
216 | // shell command injection, e.g. "foo; rm*" |
||||
217 | // XSS stuff |
||||
218 | // let's be conservative and allow only 'POSIX fully portable filenames', |
||||
219 | // which can have only A-Z a-z 0-9 . - _ |
||||
220 | // In some cases filenames are used on volunteer hosts, |
||||
221 | // whose OSs may have such restrictions. |
||||
222 | // |
||||
223 | function is_valid_filename($x) { |
||||
224 | if (strlen($x)>255) return false; |
||||
225 | // \w means A-Za-z0-9_ |
||||
226 | return preg_match('/^[\w\-.]+$/', $x); |
||||
227 | } |
||||
228 | |||||
229 | function filename_rules() { |
||||
230 | return 'Names can contain only A-Z a-z 0-9 . - _'; |
||||
231 | } |
||||
232 | |||||
233 | ?> |
||||
234 |