File: sql.php

package info (click to toggle)
horde2 2.2.8-1sarge3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,832 kB
  • ctags: 2,897
  • sloc: php: 12,784; sh: 954; sql: 149; makefile: 104; perl: 97; xml: 24; pascal: 6
file content (183 lines) | stat: -rw-r--r-- 5,844 bytes parent folder | download
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/*
 * $Horde: horde/lib/Token/sql.php,v 1.4.2.6 2003/01/03 12:48:43 jan Exp $
 *
 * Copyright 1999-2003 Max Kalika <max@horde.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 */

/**
 * Token tracking implementation for PHP's PEAR database abstraction
 * layer.
 *
 * Required values for $params:
 *      'phptype'       The database type (ie. 'pgsql', 'mysql, etc.).
 *      'hostspec'      The hostname of the database server.
 *      'username'      The username with which to connect to the database.
 *      'password'      The password associated with 'username'.
 *      'database'      The name of the database.
 *      'table'         The name of the connections table in 'database'.
 *
 * Required by some database implementations:
 *      'options'       Additional options to pass to the database.
 *      'tty'           The TTY on which to connect to the database.
 *      'port'          The port on which to connect to the database.
 *
 * Optional value for $params:
 *      'timeout'       The period (in seconds) after which an id is purged.
 *
 * The table structure for the connections is as follows:
 *
 *  create table tokens (
 *      token_addr  varchar(8)  not null,
 *      token_id    varchar(32) not null,
 *      token_ts    int(14)     not null,
 *      primary key (token_addr, token_id)
 *  );
 *
 * @author  Max Kalika <max@horde.org>
 * @version $Revision: 1.4.2.6 $
 * @since   Horde 1.3
 * @package horde.token
 */
class Token_sql extends Token {

    /** Handle for the current database connection. */
    var $db = '';

    /** Boolean indicating whether or not we're connected to the SQL server. */
    var $connected = false;

    /**
     * Constructs a new SQL connection object.
     *
     * @param optional array $params   A hash containing connection parameters.
     */
    function Token_sql($params = array())
    {
        $this->params = $params;

        /* Set timeout to 24 hours if not specified. */
        if (!isset($this->params['timeout'])) {
            $this->params['timeout'] = 86400;
        }
    }

    /**
     * Opens a connection to the SQL server.
     *
     * @return bool         TOKEN_OK on success, TOKEN_ERROR_* on failure.
     */
    function connect()
    {
        if (!$this->connected) {
            if (!is_array($this->params)) return TOKEN_ERROR_PARAMS;
            if (!isset($this->params['phptype'])) return TOKEN_ERROR_PARAMS;
            if (!isset($this->params['hostspec'])) return TOKEN_ERROR_PARAMS;
            if (!isset($this->params['username'])) return TOKEN_ERROR_PARAMS;
            if (!isset($this->params['password'])) return TOKEN_ERROR_PARAMS;
            if (!isset($this->params['database'])) return TOKEN_ERROR_PARAMS;
            if (!isset($this->params['table'])) return TOKEN_ERROR_PARAMS;

            /* Connect to the SQL server using the supplied parameters. */
            include_once 'DB.php';
            $this->db = &DB::connect($this->params, true);
            if (DB::isError($this->db)) {
                return TOKEN_ERROR_CONNECT;
            }

            /* Enable the "portability" option. */
            $this->db->setOption('optimize', 'portability');

            $this->connected = true;
        }

        return TOKEN_OK;
    }

    /**
     * Disconnect from the SQL server and clean up the connection.
     *
     * @return bool         true on success, false on failure.
     */
    function disconnect()
    {
        if ($this->connected) {
            $this->connected = false;
            return $this->db->disconnect();
        }

        return true;
    }

    /**
     * Deletes all expired connection id's from the SQL server.
     *
     * @return bool         True on success, a PEAR_Error object on failure.
     */
    function purge()
    {
        /* If we're not already connected, invoke the connect(). */
        if (!$this->connected) {
            if ($this->connect() != TOKEN_OK) return TOKEN_ERROR_CONNECT;
        }

        /* Build SQL query. */
        $query = 'delete from ' . $this->params['table'] . ' where ';
        $query .= 'token_ts < ' . (time() - $this->params['timeout']);

        $result = $this->db->query($query, $this->db);

        /* Return an error if the update fails, too. */
        if ($result !== DB_OK) return TOKEN_ERROR;

        return TOKEN_OK;
    }

    function exists($connID)
    {
        /* If we're not already connected, invoke the connect(). */
        if (!$this->connected) {
            if ($this->connect() != TOKEN_OK) return TOKEN_ERROR_CONNECT;
        }

        /* Build SQL query. */
        $query  = 'SELECT token_id FROM ' . $this->params['table'];
        $query .= ' WHERE token_addr = ' . $this->db->quote($this->hexRemoteAddr());
        $query .= ' AND token_id = ' . $this->db->quote($connID);

        $result = $this->db->query($query);

        if (isset($result) && !DB::isError($result)) {
            $row = $result->fetchRow();
        }

        if (empty($row) || DB::isError($row)) {
            return false;
        }
        return true;
    }

    function add($connID)
    {
        /* If we're not already connected, invoke the connect(). */
        if (!$this->connected) {
            if ($this->connect() != TOKEN_OK) return TOKEN_ERROR_CONNECT;
        }

        /* Build SQL query. */
        $query  = 'INSERT INTO ' . $this->params['table'];
        $query .= ' VALUES (' . $this->db->quote($this->hexRemoteAddr());
        $query .= ', ' . $this->db->quote($connID) . ', ' . time() . ')';

        $result = $this->db->query($query);

        /* Return an error if the update fails, too. */
        if ($result !== DB_OK) return TOKEN_ERROR;

        return TOKEN_OK;
    }
}
?>