Passed
Push — server_release/1/1.4 ( fa038f...715173 )
by
unknown
19:04 queued 09:24
created

is_valid_filename()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 4
rs 10
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;
0 ignored issues
show
Coding Style introduced by
DEFAULT keyword must be indented 4 spaces from SWITCH keyword
Loading history...
Coding Style introduced by
Blank lines are not allowed after DEFAULT statements
Loading history...
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();
0 ignored issues
show
Best Practice introduced by
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...
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);
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

124
        $cursor = (strlen($xml) - strlen($x)) + strlen($tag) + strlen($closetag) + strlen(/** @scrutinizer ignore-type */ $element);
Loading history...
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