Subversion Repositories Applications.annuaire

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
42 aurelien 1
<?php
2
 
3
/**
4
 * A PostgreSQL store.
5
 *
6
 * @package OpenID
7
 */
8
 
9
/**
10
 * Require the base class file.
11
 */
12
require_once "Auth/OpenID/SQLStore.php";
13
 
14
/**
15
 * An SQL store that uses PostgreSQL as its backend.
16
 *
17
 * @package OpenID
18
 */
19
class Auth_OpenID_PostgreSQLStore extends Auth_OpenID_SQLStore {
20
    /**
21
     * @access private
22
     */
23
    function setSQL()
24
    {
25
        $this->sql['nonce_table'] =
26
            "CREATE TABLE %s (nonce CHAR(8) UNIQUE PRIMARY KEY, ".
27
            "expires INTEGER)";
28
 
29
        $this->sql['assoc_table'] =
30
            "CREATE TABLE %s (server_url VARCHAR(2047), handle VARCHAR(255), ".
31
            "secret BYTEA, issued INTEGER, lifetime INTEGER, ".
32
            "assoc_type VARCHAR(64), PRIMARY KEY (server_url, handle), ".
33
            "CONSTRAINT secret_length_constraint CHECK ".
34
            "(LENGTH(secret) <= 128))";
35
 
36
        $this->sql['settings_table'] =
37
            "CREATE TABLE %s (setting VARCHAR(128) UNIQUE PRIMARY KEY, ".
38
            "value BYTEA, ".
39
            "CONSTRAINT value_length_constraint CHECK (LENGTH(value) <= 20))";
40
 
41
        $this->sql['create_auth'] =
42
            "INSERT INTO %s VALUES ('auth_key', '!')";
43
 
44
        $this->sql['get_auth'] =
45
            "SELECT value FROM %s WHERE setting = 'auth_key'";
46
 
47
        $this->sql['set_assoc'] =
48
            array(
49
                  'insert_assoc' => "INSERT INTO %s (server_url, handle, ".
50
                  "secret, issued, lifetime, assoc_type) VALUES ".
51
                  "(?, ?, '!', ?, ?, ?)",
52
                  'update_assoc' => "UPDATE %s SET secret = '!', issued = ?, ".
53
                  "lifetime = ?, assoc_type = ? WHERE server_url = ? AND ".
54
                  "handle = ?"
55
                  );
56
 
57
        $this->sql['get_assocs'] =
58
            "SELECT handle, secret, issued, lifetime, assoc_type FROM %s ".
59
            "WHERE server_url = ?";
60
 
61
        $this->sql['get_assoc'] =
62
            "SELECT handle, secret, issued, lifetime, assoc_type FROM %s ".
63
            "WHERE server_url = ? AND handle = ?";
64
 
65
        $this->sql['remove_assoc'] =
66
            "DELETE FROM %s WHERE server_url = ? AND handle = ?";
67
 
68
        $this->sql['add_nonce'] =
69
            array(
70
                  'insert_nonce' => "INSERT INTO %s (nonce, expires) VALUES ".
71
                  "(?, ?)",
72
                  'update_nonce' => "UPDATE %s SET expires = ? WHERE nonce = ?"
73
                  );
74
 
75
        $this->sql['get_nonce'] =
76
            "SELECT * FROM %s WHERE nonce = ?";
77
 
78
        $this->sql['remove_nonce'] =
79
            "DELETE FROM %s WHERE nonce = ?";
80
    }
81
 
82
    /**
83
     * @access private
84
     */
85
    function _set_assoc($server_url, $handle, $secret, $issued, $lifetime,
86
                        $assoc_type)
87
    {
88
        $result = $this->_get_assoc($server_url, $handle);
89
        if ($result) {
90
            // Update the table since this associations already exists.
91
            $this->connection->query($this->sql['set_assoc']['update_assoc'],
92
                                     array($secret, $issued, $lifetime,
93
                                           $assoc_type, $server_url, $handle));
94
        } else {
95
            // Insert a new record because this association wasn't
96
            // found.
97
            $this->connection->query($this->sql['set_assoc']['insert_assoc'],
98
                                     array($server_url, $handle, $secret,
99
                                           $issued, $lifetime, $assoc_type));
100
        }
101
    }
102
 
103
    /**
104
     * @access private
105
     */
106
    function _add_nonce($nonce, $expires)
107
    {
108
        if ($this->_get_nonce($nonce)) {
109
            return $this->resultToBool($this->connection->query(
110
                                      $this->sql['add_nonce']['update_nonce'],
111
                                      array($expires, $nonce)));
112
        } else {
113
            return $this->resultToBool($this->connection->query(
114
                                      $this->sql['add_nonce']['insert_nonce'],
115
                                      array($nonce, $expires)));
116
        }
117
    }
118
 
119
    /**
120
     * @access private
121
     */
122
    function blobEncode($blob)
123
    {
124
        return $this->_octify($blob);
125
    }
126
 
127
    /**
128
     * @access private
129
     */
130
    function blobDecode($blob)
131
    {
132
        return $this->_unoctify($blob);
133
    }
134
}
135
 
136
?>