Issues (1839)

html/inc/db.inc (7 issues)

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
require_once("../inc/util_basic.inc");
20
21
// database-related functions.
22
// Presentation code (HTML) shouldn't be here
23
24
// DEPRECATED; use boinc_db.inc instead.
25
// TODO: replace calls to these functions
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
26
27
// use mysqli if available,
28
// but let projects not use it if they want
29
// (put <no_mysqli/> in config.xml)
30
//
31
if (parse_bool(get_config(), "no_mysqli")) {
32
    define("MYSQLI", false);
33
} else {
34
    if (class_exists("mysqli")) {
35
        define("MYSQLI", true);
36
        $mysqli = null;
37
    } else {
38
        define("MYSQLI", false);
39
    }
40
}
41
42
if (MYSQLI) {
43
    function _mysql_connect($host, $user, $pass, $dbname) {
44
        global $mysqli;
45
        $x = explode(":", $host);
46
        if (sizeof($x)>1) {
47
            $host = $x[0];
48
            $port = $x[1];
49
        } else {
50
            $port = null;
51
        }
52
        $mysqli = @new mysqli($host, $user, $pass, $dbname, $port);
0 ignored issues
show
It seems like $port can also be of type string; however, parameter $port of mysqli::__construct() does only seem to accept integer, 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

52
        $mysqli = @new mysqli($host, $user, $pass, $dbname, /** @scrutinizer ignore-type */ $port);
Loading history...
53
        return $mysqli;
54
    }
55
    function _mysql_query($q) {
56
        global $mysqli;
57
        return mysqli_query($mysqli, $q);
58
    }
59
    function _mysql_num_rows($r) {
60
        return mysqli_num_rows($r);
61
    }
62
    function _mysql_num_fields($r) {
63
        global $mysqli;
64
        return mysqli_field_count($mysqli);
65
    }
66
    function _mysql_fetch_object($r) {
67
        return mysqli_fetch_object($r);
68
    }
69
    function _mysql_fetch_row($r) {
70
        return mysqli_fetch_row($r);
71
    }
72
    function _mysql_fetch_assoc($r) {
73
        return mysqli_fetch_assoc($r);
74
    }
75
    function _mysql_free_result($r) {
76
        return mysqli_free_result($r);
0 ignored issues
show
Are you sure the usage of mysqli_free_result($r) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
77
    }
78
    function _mysql_insert_id() {
79
        global $mysqli;
80
        return mysqli_insert_id($mysqli);
81
    }
82
    function _mysql_affected_rows() {
83
        global $mysqli;
84
        return mysqli_affected_rows($mysqli);
85
    }
86
    function _mysql_field_attrs($r, $i) {
87
        $x = mysqli_fetch_field_direct($r, $i);
88
        switch ($x->type) {
89
            case 1: $x->type = 'tinyint'; break;
90
            case 2: $x->type = 'smallint'; break;
91
            case 3: $x->type = 'int'; break;
92
            case 5: $x->type = 'double'; break;
93
            case 7: $x->type = 'timestamp'; break;
94
            case 252: $x->type = 'blob'; break;
95
            case 253: $x->type = 'varchar'; break;
96
            case 254: $x->type = 'char'; break;
97
        }
98
        return $x;
99
    }
100
    function _mysql_escape_string($x) {
101
        global $mysqli;
102
        return mysqli_escape_string($mysqli, $x);
103
    }
104
    function _mysql_error() {
105
        global $mysqli;
106
        return mysqli_error($mysqli);
107
    }
108
    function _mysql_fetch_array($r) {
109
        return mysqli_fetch_array($r);
110
    }
111
} else {
112
    function _mysql_connect($host, $user, $pass, $db_name) {
113
        $link = mysql_pconnect($host, $user, $pass);
114
        if (!$link) return null;
0 ignored issues
show
$link is of type resource, thus it always evaluated to false.
Loading history...
115
        if (!mysql_select_db($db_name, $link)) {
116
            return null;
117
        }
118
        return $link;
119
    }
120
    function _mysql_query($q) {
121
        return mysql_query($q);
122
    }
123
    function _mysql_num_rows($r) {
124
        return mysql_num_rows($r);
125
    }
126
    function _mysql_num_fields($r) {
127
        return mysql_num_fields($r);
128
    }
129
    function _mysql_fetch_object($r) {
130
        return mysql_fetch_object($r);
131
    }
132
    function _mysql_fetch_row($r) {
133
        return mysql_fetch_row($r);
134
    }
135
    function _mysql_fetch_assoc($r) {
136
        return mysql_fetch_assoc($r);
137
    }
138
    function _mysql_free_result($r) {
139
        return mysql_free_result($r);
140
    }
141
    function _mysql_insert_id() {
142
        return mysql_insert_id();
143
    }
144
    function _mysql_affected_rows() {
145
        return mysql_affected_rows();
146
    }
147
    function _mysql_field_attrs($r, $i) {
148
        $x = new StdClass;
149
        $x->name = mysql_field_name($r, $i);
150
        $x->type = mysql_field_type($r, $i);
151
        $x->length = mysql_field_len($r, $i);
152
        return $x;
153
    }
154
    function _mysql_escape_string($x) {
155
        return mysql_escape_string($x);
156
    }
157
    function _mysql_error() {
158
        return mysql_error();
159
    }
160
    function _mysql_fetch_array($r) {
161
        return mysql_fetch_array($r);
162
    }
163
}
164
165
function db_init_aux($try_replica=false) {
166
    $config = get_config();
167
    $user = parse_config($config, "<db_user>");
168
    $pass = parse_config($config, "<db_passwd>");
169
    $db_name = parse_config($config, "<db_name>");
170
    $host = null;
171
    if ($try_replica) {
172
        $x = parse_config($config, "<replica_db_host>");
173
        if ($x) {
174
            $host = $x;
175
            $x = parse_config($config, "<replica_db_user>");
176
            if ($x) $user = $x;
177
            $x = parse_config($config, "<replica_db_passwd>");
178
            if ($x) $pass = $x;
179
            $x = parse_config($config, "<replica_db_name>");
180
            if ($x) $db_name = $x;
181
        }
182
    }
183
    if ($host == null) {
184
        $host = parse_config($config, "<db_host>");
185
    }
186
    if ($host == null) {
0 ignored issues
show
It seems like you are loosely comparing $host of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
187
        $host = "localhost";
188
    }
189
    if (1) {
190
        if (!_mysql_connect($host, $user, $pass, $db_name)) {
0 ignored issues
show
Are you sure the usage of _mysql_connect($host, $user, $pass, $db_name) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
191
            return 1;
192
        }
193
    } else {
194
        $link = mysql_pconnect($host, $user, $pass);
195
        if (!$link) {
0 ignored issues
show
$link is of type resource, thus it always evaluated to false.
Loading history...
196
            return 1;
197
        }
198
        if (!mysql_select_db($db_name, $link)) {
199
            echo "selecting $db_name\n";
200
            return 2;
201
        }
202
    }
203
204
    return 0;
205
}
206
207
// escape a string for MySQL "like"
208
//
209
function escape_pattern($str) {
210
    $str = str_replace('_', '\\\\_', $str);
211
    $str = str_replace('%', '\\\\%', $str);
212
    return $str;
213
}
214
215
?>
216