1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Authors: John Coggeshall <john@php.net> |
16 : | Wez Furlong <wez@thebrainroom.com> |
17 : +----------------------------------------------------------------------+
18 : */
19 :
20 : /* $Id: sess_sqlite.c 276986 2009-03-10 23:40:06Z helly $ */
21 :
22 : #include "php.h"
23 :
24 : #if HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION)
25 :
26 : #include "ext/session/php_session.h"
27 : #include "ext/standard/php_lcg.h"
28 : #include <sqlite.h>
29 : #define SQLITE_RETVAL(__r) ((__r) == SQLITE_OK ? SUCCESS : FAILURE)
30 : #define PS_SQLITE_DATA sqlite *db = (sqlite*)PS_GET_MOD_DATA()
31 : extern int sqlite_encode_binary(const unsigned char *in, int n, unsigned char *out);
32 : extern int sqlite_decode_binary(const unsigned char *in, unsigned char *out);
33 :
34 : PS_FUNCS(sqlite);
35 :
36 : ps_module ps_mod_sqlite = {
37 : PS_MOD(sqlite)
38 : };
39 :
40 : PS_OPEN_FUNC(sqlite)
41 5 : {
42 5 : char *errmsg = NULL;
43 : sqlite *db;
44 :
45 5 : db = sqlite_open(save_path, 0666, &errmsg);
46 5 : if (db == NULL) {
47 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING,
48 : "SQLite: failed to open/create session database `%s' - %s", save_path, errmsg);
49 0 : sqlite_freemem(errmsg);
50 0 : return FAILURE;
51 : }
52 :
53 : /* allow up to 1 minute when busy */
54 5 : sqlite_busy_timeout(db, 60000);
55 :
56 5 : sqlite_exec(db, "PRAGMA default_synchronous = OFF", NULL, NULL, NULL);
57 5 : sqlite_exec(db, "PRAGMA count_changes = OFF", NULL, NULL, NULL);
58 :
59 : /* This will fail if the table already exists, but that's not a big problem. I'm
60 : unclear as to how to check for a table's existence in SQLite -- that would be better here. */
61 5 : sqlite_exec(db,
62 : "CREATE TABLE session_data ("
63 : " sess_id PRIMARY KEY,"
64 : " value TEXT, "
65 : " updated INTEGER "
66 : ")", NULL, NULL, NULL);
67 :
68 5 : PS_SET_MOD_DATA(db);
69 :
70 5 : return SUCCESS;
71 : }
72 :
73 : PS_CLOSE_FUNC(sqlite)
74 7 : {
75 7 : PS_SQLITE_DATA;
76 :
77 7 : sqlite_close(db);
78 :
79 7 : return SUCCESS;
80 : }
81 :
82 : PS_READ_FUNC(sqlite)
83 5 : {
84 5 : PS_SQLITE_DATA;
85 : char *query;
86 : const char *tail;
87 : sqlite_vm *vm;
88 : int colcount, result;
89 : const char **rowdata, **colnames;
90 : char *error;
91 :
92 5 : *val = NULL;
93 5 : *vallen = 0;
94 :
95 5 : query = sqlite_mprintf("SELECT value FROM session_data WHERE sess_id='%q' LIMIT 1", key);
96 5 : if (query == NULL) {
97 : /* no memory */
98 0 : return FAILURE;
99 : }
100 :
101 5 : if (sqlite_compile(db, query, &tail, &vm, &error) != SQLITE_OK) {
102 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLite: Could not compile session read query: %s", error);
103 0 : sqlite_freemem(error);
104 0 : sqlite_freemem(query);
105 0 : return FAILURE;
106 : }
107 :
108 5 : switch ((result = sqlite_step(vm, &colcount, &rowdata, &colnames))) {
109 : case SQLITE_ROW:
110 2 : if (rowdata[0] != NULL) {
111 2 : *vallen = strlen(rowdata[0]);
112 2 : if (*vallen) {
113 2 : *val = emalloc(*vallen);
114 2 : *vallen = sqlite_decode_binary(rowdata[0], *val);
115 2 : (*val)[*vallen] = '\0';
116 : } else {
117 0 : *val = STR_EMPTY_ALLOC();
118 : }
119 : }
120 2 : break;
121 : default:
122 3 : sqlite_freemem(error);
123 3 : error = NULL;
124 : }
125 :
126 5 : if (SQLITE_OK != sqlite_finalize(vm, &error)) {
127 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLite: session read: error %s", error);
128 0 : sqlite_freemem(error);
129 0 : error = NULL;
130 : }
131 :
132 5 : sqlite_freemem(query);
133 :
134 5 : return *val == NULL ? FAILURE : SUCCESS;
135 : }
136 :
137 : PS_WRITE_FUNC(sqlite)
138 4 : {
139 4 : PS_SQLITE_DATA;
140 : char *error;
141 : time_t t;
142 : char *binary;
143 : int binlen;
144 : int rv;
145 :
146 4 : t = time(NULL);
147 :
148 4 : binary = safe_emalloc(1 + vallen / 254, 257, 3);
149 4 : binlen = sqlite_encode_binary((const unsigned char*)val, vallen, binary);
150 :
151 4 : rv = sqlite_exec_printf(db, "REPLACE INTO session_data VALUES('%q', '%q', %d)", NULL, NULL, &error, key, binary, t);
152 4 : if (rv != SQLITE_OK) {
153 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLite: session write query failed: %s", error);
154 0 : sqlite_freemem(error);
155 : }
156 4 : efree(binary);
157 :
158 4 : return SQLITE_RETVAL(rv);
159 : }
160 :
161 : PS_DESTROY_FUNC(sqlite)
162 1 : {
163 : int rv;
164 1 : PS_SQLITE_DATA;
165 :
166 1 : rv = sqlite_exec_printf(db, "DELETE FROM session_data WHERE sess_id='%q'", NULL, NULL, NULL, key);
167 :
168 1 : return SQLITE_RETVAL(rv);
169 : }
170 :
171 : PS_GC_FUNC(sqlite)
172 0 : {
173 0 : PS_SQLITE_DATA;
174 : int rv;
175 0 : time_t t = time(NULL);
176 :
177 0 : rv = sqlite_exec_printf(db,
178 : "DELETE FROM session_data WHERE (%d - updated) > %d",
179 : NULL, NULL, NULL, t, maxlifetime);
180 :
181 : /* because SQLite does not actually clear the deleted data from the database
182 : * we need to occassionaly do so manually to prevent the sessions database
183 : * from growing endlessly.
184 : */
185 0 : if ((int) ((float) PS(gc_divisor) * PS(gc_divisor) * php_combined_lcg(TSRMLS_C)) < PS(gc_probability)) {
186 0 : rv = sqlite_exec_printf(db, "VACUUM", NULL, NULL, NULL);
187 : }
188 0 : return SQLITE_RETVAL(rv);
189 : }
190 :
191 : #endif /* HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION) */
192 :
193 : /*
194 : * Local variables:
195 : * tab-width: 4
196 : * c-basic-offset: 4
197 : * End:
198 : * vim600: sw=4 ts=4 fdm=marker
199 : * vim<600: sw=4 ts=4
200 : */
|