db   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 2
1
<?php
2
###############################################################################
3
# ASTPP - Open Source VoIP Billing Solution
4
#
5
# Copyright (C) 2016 iNextrix Technologies Pvt. Ltd.
6
# Samir Doshi <[email protected]>
7
# ASTPP Version 3.0 and above
8
# License https://www.gnu.org/licenses/agpl-3.0.html
9
#
10
# This program is free software: you can redistribute it and/or modify
11
# it under the terms of the GNU Affero General Public License as
12
# published by the Free Software Foundation, either version 3 of the
13
# License, or (at your option) any later version.
14
# 
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
# GNU Affero General Public License for more details.
19
# 
20
# You should have received a copy of the GNU Affero General Public License
21
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
###############################################################################
23
24
class db extends PDO {
25
26
    private $error;
27
    private $sql;
28
    private $bind;
29
    private $errorCallbackFunction;
0 ignored issues
show
Unused Code introduced by
The property $errorCallbackFunction is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
    private $errorMsgFormat;
0 ignored issues
show
Unused Code introduced by
The property $errorMsgFormat is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
31
32
    public function __construct($dsn = "", $user = "", $passwd = "") {
33
34
        $config = parse_ini_file("/var/lib/astpp/astpp-config.conf");
35
36
        $options = array(
37
            PDO::ATTR_PERSISTENT => true,
38
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
39
        );
40
41
        try {
42
            parent::__construct("mysql:host=".$config['dbhost'].";dbname=".$config['dbname']."", $config['dbuser'], $config['dbpass'], $options);
43
        } catch (PDOException $e) {
44
45
            $this->error = $e->getMessage();
46
        }
47
        echo $this->error;
48
    }
49
50
    /**
51
     * @param string $bind
52
     */
53
    public function cleanup($bind) {
54
        if ( ! is_array($bind)) {
55
            if ( ! empty($bind))
56
                $bind = array($bind);
57
            else
58
                $bind = array();
59
        }
60
        return $bind;
61
    }
62
63
    public function run($sql, $bind = "") {
64
        $this->sql = trim($sql);
65
        $this->bind = $this->cleanup($bind);
66
        $this->error = "";
67
68
        try {
69
            $pdostmt = $this->prepare($this->sql);
70
            if ($pdostmt->execute($this->bind) !== false) {
71
                if (preg_match("/^(".implode("|", array("select", "describe", "pragma")).") /i", $this->sql))
72
                    return $pdostmt->fetchAll(PDO::FETCH_ASSOC);
73
                elseif (preg_match("/^(".implode("|", array("delete", "insert", "update")).") /i", $this->sql))
74
                    return $pdostmt->rowCount();
75
            }
76
        } catch (PDOException $e) {
77
            $this->error = $e->getMessage();
78
            return $this->error;
79
        }
80
    }
81
82
}
83
84
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
85