1 | <?php |
||||||||
2 | class Db_Pgsql implements IDb { |
||||||||
3 | private $link; |
||||||||
4 | private $last_error; |
||||||||
5 | |||||||||
6 | public function connect($host, $user, $pass, $db, $port) { |
||||||||
7 | $string = "dbname=$db user=$user"; |
||||||||
8 | |||||||||
9 | if ($pass) { |
||||||||
10 | $string .= " password=$pass"; |
||||||||
11 | } |
||||||||
12 | |||||||||
13 | if ($host) { |
||||||||
14 | $string .= " host=$host"; |
||||||||
15 | } |
||||||||
16 | |||||||||
17 | if (is_numeric($port) && $port > 0) { |
||||||||
18 | $string = "$string port=".$port; |
||||||||
19 | } |
||||||||
20 | |||||||||
21 | $this->link = pg_connect($string); |
||||||||
22 | |||||||||
23 | if (!$this->link) { |
||||||||
24 | print("Unable to connect to database (as $user to $host, database $db):".pg_last_error()); |
||||||||
25 | exit(102); |
||||||||
0 ignored issues
–
show
|
|||||||||
26 | } |
||||||||
27 | |||||||||
28 | $this->init(); |
||||||||
29 | |||||||||
30 | return $this->link; |
||||||||
31 | } |
||||||||
32 | |||||||||
33 | public function escape_string($s, $strip_tags = true) { |
||||||||
34 | if ($strip_tags) { |
||||||||
35 | $s = strip_tags($s); |
||||||||
36 | } |
||||||||
37 | |||||||||
38 | return pg_escape_string($s); |
||||||||
0 ignored issues
–
show
The call to
pg_escape_string() has too few arguments starting with data .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||||||
39 | } |
||||||||
40 | |||||||||
41 | public function query($query, $die_on_error = true) { |
||||||||
42 | $result = @pg_query($this->link, $query); |
||||||||
43 | |||||||||
44 | if (!$result) { |
||||||||
0 ignored issues
–
show
|
|||||||||
45 | $this->last_error = @pg_last_error($this->link); |
||||||||
46 | |||||||||
47 | @pg_query($this->link, "ROLLBACK"); |
||||||||
0 ignored issues
–
show
It seems like you do not handle an error condition for
pg_query() . This can introduce security issues, and is generally not recommended.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
![]() |
|||||||||
48 | $query = htmlspecialchars($query); // just in case |
||||||||
49 | user_error("query $query failed: ".($this->link ? $this->last_error : "No connection"), |
||||||||
50 | $die_on_error ? E_USER_ERROR : E_USER_WARNING); |
||||||||
51 | } |
||||||||
52 | return $result; |
||||||||
53 | } |
||||||||
54 | |||||||||
55 | public function fetch_assoc($result) { |
||||||||
56 | return pg_fetch_assoc($result); |
||||||||
57 | } |
||||||||
58 | |||||||||
59 | |||||||||
60 | public function num_rows($result) { |
||||||||
61 | return pg_num_rows($result); |
||||||||
62 | } |
||||||||
63 | |||||||||
64 | public function fetch_result($result, $row, $param) { |
||||||||
65 | return pg_fetch_result($result, $row, $param); |
||||||||
66 | } |
||||||||
67 | |||||||||
68 | public function close() { |
||||||||
69 | return pg_close($this->link); |
||||||||
70 | } |
||||||||
71 | |||||||||
72 | public function affected_rows($result) { |
||||||||
73 | return pg_affected_rows($result); |
||||||||
74 | } |
||||||||
75 | |||||||||
76 | public function last_error() { |
||||||||
77 | return pg_last_error($this->link); |
||||||||
78 | } |
||||||||
79 | |||||||||
80 | public function last_query_error() { |
||||||||
81 | return $this->last_error; |
||||||||
82 | } |
||||||||
83 | |||||||||
84 | public function init() { |
||||||||
85 | $this->query("set client_encoding = 'UTF-8'"); |
||||||||
86 | pg_set_client_encoding("UNICODE"); |
||||||||
0 ignored issues
–
show
The call to
pg_set_client_encoding() has too few arguments starting with encoding .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() 'UNICODE' of type string is incompatible with the type resource expected by parameter $connection of pg_set_client_encoding() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||
87 | $this->query("set datestyle = 'ISO, european'"); |
||||||||
88 | $this->query("set TIME ZONE 0"); |
||||||||
89 | $this->query("set cpu_tuple_cost = 0.5"); |
||||||||
90 | |||||||||
91 | return true; |
||||||||
92 | } |
||||||||
93 | } |
||||||||
94 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.