-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.c
210 lines (170 loc) · 4.17 KB
/
session.c
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* $Id: session.c 7357 2004-10-27 12:56:29Z piotras $
Copyright (C) 1999 Jukka Zitting <jukka.zitting@iki.fi>
Copyright (C) 2000 The Midgard Project ry
Copyright (C) 2001 David Guerizec, Aurora SA <david@guerizec.net>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "php_midgard.h"
#if HAVE_MIDGARD
#if HAVE_MIDGARD_SESSION
#include "../session/php_session.h"
#include "mgd_session.h"
#define GET_MGD midgard * mgd = mgd_handle();
typedef struct {
int id;
int ready;
} ps_midgard;
ps_module ps_mod_midgard = {
PS_MOD(midgard)
};
static int ps_midgard_valid_key(const char *key)
{
size_t len;
const char *p;
char c;
int ret = 1;
for (p = key; (c = *p); p++) {
/* valid characters are a..z,A..Z,0..9 */
if (!((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9'))) {
ret = 0;
break;
}
}
len = p - key;
if (len == 0)
ret = 0;
return ret;
}
static int ps_midgard_write(ps_midgard * data, const char * key, const char * val)
{
GET_MGD;
if(!data->ready)
return FAILURE;
if(data->id) {
if(mgd_update(mgd, "session", data->id,
"sess_data=$q, "
"expire=UNIX_TIMESTAMP() + 3600, "
"changed=UNIX_TIMESTAMP()",
val))
return SUCCESS;
assert(0);
} else {
if((data->id = mgd_create(mgd, "session",
"sess_key, sess_data, person, expire, changed",
"$q, $q, $d, UNIX_TIMESTAMP() + 3600, UNIX_TIMESTAMP()",
key, val, mgd_user(mgd))))
return SUCCESS;
assert(0);
}
return FAILURE;
}
static void ps_midgard_open(ps_midgard *data, const char *key)
{
GET_MGD;
if(data->id) {
php_error(E_WARNING, "Called twice ??");
}
data->id = 0;
if(!mgd) {
php_error(E_WARNING, "Midgard is not ready yet for session management,"
" please come back later...");
return;
}
if(!ps_midgard_valid_key(key))
return;
data->id = mgd_exists_id(mgd, "session",
"sess_key=$q AND person=$d", // AND expire>=UNIX_TIMESTAMP()",
key, mgd_user(mgd));
data->ready = 1;
return;
}
#define PS_MIDGARD_DATA ps_midgard *data = PS_GET_MOD_DATA()
PS_OPEN_FUNC(midgard)
{
ps_midgard *data;
data = ecalloc(sizeof(*data), 1);
PS_SET_MOD_DATA(data);
data->id = 0;
data->ready = 0;
return SUCCESS;
}
PS_CLOSE_FUNC(midgard)
{
PS_MIDGARD_DATA;
efree(data);
*mod_data = NULL;
return SUCCESS;
}
PS_READ_FUNC(midgard)
{
midgard_res * res;
PS_MIDGARD_DATA;
GET_MGD;
ps_midgard_open(data, key);
if(!data->id) return FAILURE;
res = mgd_query(mgd, "SELECT sess_data FROM session WHERE id=$d",
data->id);
if(res) {
if(mgd_fetch(res)) {
*vallen = strlen(mgd_colvalue(res, 0));
*val = estrdup(mgd_colvalue(res, 0));
mgd_release(res);
return SUCCESS;
}
mgd_release(res);
}
return FAILURE;
}
PS_WRITE_FUNC(midgard)
{
PS_MIDGARD_DATA;
GET_MGD;
ps_midgard_open(data, key);
if(!mgd) {
php_error(E_WARNING, "Midgard is not ready yet for session management,"
" please come back later...");
return FAILURE;
}
return ps_midgard_write(data, key, val);
}
PS_DESTROY_FUNC(midgard)
{
PS_MIDGARD_DATA;
GET_MGD;
ps_midgard_open(data, key);
if(!data->id) return FAILURE;
if(mgd_delete(mgd, "session", data->id))
return SUCCESS;
return FAILURE;
}
PS_GC_FUNC(midgard)
{
midgard_res * res;
GET_MGD;
if(!mgd) {
php_error(E_WARNING, "Midgard is not ready yet for session management,"
" please come back later...");
return FAILURE;
}
res = mgd_query(mgd, "DELETE FROM session "
"WHERE expire < UNIX_TIMESTAMP()");
if(res) {
mgd_release(res);
return SUCCESS;
}
return FAILURE;
}
#endif // HAVE_MIDGARD_SESSION
#endif // HAVE_MIDGARD