summaryrefslogtreecommitdiffstats
path: root/lib/SQL.php
blob: 5024be7885ce8bdd27a280f55e994a0eba7a66c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/*
 +--------------------------------------------------------------------------+
 | This file is part of the Kolab Web Admin Panel                           |
 |                                                                          |
 | Copyright (C) 2011-2012, Kolab Systems AG                                |
 |                                                                          |
 | This program is free software: you can redistribute it and/or modify     |
 | it under the terms of the GNU Affero General Public License as published |
 | by the Free Software Foundation, either version 3 of the License, or     |
 | (at your option) any later version.                                      |
 |                                                                          |
 | This program is distributed in the hope that it will be useful,          |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             |
 | GNU Affero General Public License for more details.                      |
 |                                                                          |
 | You should have received a copy of the GNU Affero General Public License |
 | along with this program. If not, see <http://www.gnu.org/licenses/>      |
 +--------------------------------------------------------------------------+
 | Author: Aleksander Machniak <machniak@kolabsys.com>                      |
 | Author: Jeroen van Meeuwen <vanmeeuwen@kolabsys.com>                     |
 +--------------------------------------------------------------------------+
*/

class SQL
{
    static private $instance = array();

    private $sql_uri = "mysql://username:password@hostname/database";

    /* Placeholder for the existing MySQL connection */
    private $conn = FALSE;

    private $sql_stats = array(
        'queries' => 0,
        'query_time' => 0,
        'connections' => 0
    );

    /**
     * This implements the 'singleton' design pattern
     *
     * @return SQL The one and only instance associated with $_conn
     */
    static function get_instance($_conn = 'kolab_wap')
    {
        if (!isset(self::$instance[$_conn])) {
            self::$instance[$_conn] = new SQL($_conn);
        }

        return self::$instance[$_conn];
    }

    public function __construct($_conn = 'kolab_wap')
    {
        $this->name = $_conn;
        $conf = Conf::get_instance();
        $this->sql_uri = $conf->get($_conn, 'sql_uri');
    }

    public function query()
    {
        if (!$this->conn) {
            $this->_connect();
        }

        $i     = 0;
        $start = 0;
        $query = func_get_arg(0);

        while (strlen($query) > $start && ($pos = strpos($query, '?', $start)) !== false) {
            $i++;
            $param = $this->escape(func_get_arg($i));

            $query = substr_replace($query, $param, $pos, 1);
            $start = $pos + strlen($param) + 1;
        }

        $result = mysql_query($query);

        if (!$result) {
            write_log('errors', 'SQL Error: ' . mysql_error($this->conn));
        }

        return $result;
    }

    public function fetch_assoc($result)
    {
        if (!$result) {
            return array();
        }

        return mysql_fetch_assoc($result);
    }

    public function escape($str)
    {
        if ($str === null || is_array($str)) {
            return 'NULL';
        }

        return "'" . mysql_real_escape_string($str, $this->conn) . "'";
    }

    private function _connect()
    {
        if (!$this->conn) {
            $_uri = parse_url($this->sql_uri);
            $this->_username = $_uri['user'];
            $this->_password = $_uri['pass'];
            $this->_hostname = $_uri['host'];
            $this->_database = str_replace('/','',$_uri['path']);

            $this->conn = mysql_connect($this->_hostname, $this->_username, $this->_password);
            mysql_select_db($this->_database, $this->conn);
        }
    }

}