90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
(session:del! s:session "*sessionvars*" key))
(define s:session-var-delete! s:session-var-del!)
;; utility to get all vars as hash table
(define (s:session-get-sessionvars)
(sdat-get-sessionvars s:session))
;; inputs
;;
;; param: (dtype [tag1 tag2 ...])
;; dtype:
;; 'raw : do no conversion
;; 'number : convert to number, return #f if fails
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
(session:del! s:session "*sessionvars*" key))
(define s:session-var-delete! s:session-var-del!)
;; utility to get all vars as hash table
(define (s:session-get-sessionvars)
(sdat-get-sessionvars s:session))
;; to obscure and indirect database ids use one time keys
;;
;; (s:get-key 'n 1) => "n99e1882" n=number 99e is the week number since 1970, remainder is random
;; (s:key->val "n1882") => 1
;;
;; first letter is a type: n=number, s=string, b=boolean
(define (s:get-key key-type val)
(let ((mkrandstr (lambda (innum)(number->string (random innum) 16)))
(week (number->string (quotient (current-seconds) (* 7 24 60 60)) 16)))
(let loop ((siz 1000)
(key (conc key-type week (mkrandstr 100)))
(num 0))
(if (s:session-var-get key) ;; have a collision
(loop (cond ;; in the unlikey event we have trouble getting a new var, keep increasing the size of the number
((< num 50) 100)
((< num 100) 1000)
((< num 200) 10000)
((< num 300) 100000)
((< num 400) 1000000) ;; can't imagine needing to get here. remember that this is for a single user
(else 100000000))
(conc key-type (mkrandstr siz))
(+ num 1))
(begin
(s:session-var-set! key val)
key)))))
;; given a key Xnnnn, look up the stored value and convert it appropriately, then
;; destroy the stored session var
;;
(define (s:key->val key)
(let ((val (s:session-var-get key))
(typ (string->symbol (substring key 0 1))))
(if val
(begin
(s:session-var-del! key)
;; we take this opportunity to clean up old keyed session vars
;; if more than 100 vars, remove all that are over 1-2 weeks old
;(s:cleanup-session-vars)
(case typ
((n)(string->number val))
((s) val)
(else val)))
val)))
;; clean up session vars
;;
(define (s:cleanup-session-vars)
(let* ((session-vars (hash-table-keys (s:session-get-sessionvars)))
(week-num (quotient (current-seconds) (* 7 24 60 60)))
(week (number->string week-num 16)))
(if (> (length session-vars) 100)
(for-each
(lambda (var)
(if (> (string-length var) 5) ;; can't have keyed values with keys less than 5 characters long
(let ((var-week (string->number (substring var 1 4) 16)))
(if (and var-week
(>= (- week-num var-week) 2))
(s:session-var-del! var)))))
session-vars))))
;; inputs
;;
;; param: (dtype [tag1 tag2 ...])
;; dtype:
;; 'raw : do no conversion
;; 'number : convert to number, return #f if fails
|