1<?php
2
3/**
4 * An SQLite store.
5 *
6 * @package OpenID
7 */
8
9/**
10 * Require the base class file.
11 */
12require_once "Auth/OpenID/SQLStore.php";
13
14/**
15 * An SQL store that uses SQLite as its backend.
16 *
17 * @package OpenID
18 */
19class Auth_OpenID_SQLiteStore extends Auth_OpenID_SQLStore {
20    function setSQL()
21    {
22        $this->sql['nonce_table'] =
23            "CREATE TABLE %s (server_url VARCHAR(2047), timestamp INTEGER, ".
24            "salt CHAR(40), UNIQUE (server_url, timestamp, salt))";
25
26        $this->sql['assoc_table'] =
27            "CREATE TABLE %s (server_url VARCHAR(2047), handle VARCHAR(255), ".
28            "secret BLOB(128), issued INTEGER, lifetime INTEGER, ".
29            "assoc_type VARCHAR(64), PRIMARY KEY (server_url, handle))";
30
31        $this->sql['set_assoc'] =
32            "INSERT OR REPLACE INTO %s VALUES (?, ?, ?, ?, ?, ?)";
33
34        $this->sql['get_assocs'] =
35            "SELECT handle, secret, issued, lifetime, assoc_type FROM %s ".
36            "WHERE server_url = ?";
37
38        $this->sql['get_assoc'] =
39            "SELECT handle, secret, issued, lifetime, assoc_type FROM %s ".
40            "WHERE server_url = ? AND handle = ?";
41
42        $this->sql['remove_assoc'] =
43            "DELETE FROM %s WHERE server_url = ? AND handle = ?";
44
45        $this->sql['add_nonce'] =
46            "INSERT INTO %s (server_url, timestamp, salt) VALUES (?, ?, ?)";
47
48        $this->sql['clean_nonce'] =
49            "DELETE FROM %s WHERE timestamp < ?";
50
51        $this->sql['clean_assoc'] =
52            "DELETE FROM %s WHERE issued + lifetime < ?";
53    }
54
55    /**
56     * @access private
57     */
58    function _add_nonce($server_url, $timestamp, $salt)
59    {
60        // PECL SQLite extensions 1.0.3 and older (1.0.3 is the
61        // current release at the time of this writing) have a broken
62        // sqlite_escape_string function that breaks when passed the
63        // empty string. Prefixing all strings with one character
64        // keeps them unique and avoids this bug. The nonce table is
65        // write-only, so we don't have to worry about updating other
66        // functions with this same bad hack.
67        return parent::_add_nonce('x' . $server_url, $timestamp, $salt);
68    }
69}
70
71