| 1 |
require 'cgi' |
|---|
| 2 |
require 'cgi/session' |
|---|
| 3 |
require 'digest/md5' |
|---|
| 4 |
|
|---|
| 5 |
class CGI |
|---|
| 6 |
class Session |
|---|
| 7 |
attr_reader :data |
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
def model |
|---|
| 11 |
@dbman.model if @dbman |
|---|
| 12 |
end |
|---|
| 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 |
class ActiveRecordStore |
|---|
| 57 |
|
|---|
| 58 |
class Session < ActiveRecord::Base |
|---|
| 59 |
|
|---|
| 60 |
cattr_accessor :data_column_name |
|---|
| 61 |
self.data_column_name = 'data' |
|---|
| 62 |
|
|---|
| 63 |
before_save :marshal_data! |
|---|
| 64 |
before_save :raise_on_session_data_overflow! |
|---|
| 65 |
|
|---|
| 66 |
class << self |
|---|
| 67 |
|
|---|
| 68 |
def reloadable? |
|---|
| 69 |
false |
|---|
| 70 |
end |
|---|
| 71 |
|
|---|
| 72 |
def data_column_size_limit |
|---|
| 73 |
@data_column_size_limit ||= columns_hash[@@data_column_name].limit |
|---|
| 74 |
end |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
def find_by_session_id(session_id) |
|---|
| 78 |
setup_sessid_compatibility! |
|---|
| 79 |
find_by_session_id(session_id) |
|---|
| 80 |
end |
|---|
| 81 |
|
|---|
| 82 |
def marshal(data) ActiveSupport::Base64.encode64(Marshal.dump(data)) if data end |
|---|
| 83 |
def unmarshal(data) Marshal.load(ActiveSupport::Base64.decode64(data)) if data end |
|---|
| 84 |
|
|---|
| 85 |
def create_table! |
|---|
| 86 |
connection.execute <<-end_sql |
|---|
| 87 |
CREATE TABLE |
|---|
| 88 |
id INTEGER PRIMARY KEY, |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
) |
|---|
| 92 |
end_sql |
|---|
| 93 |
end |
|---|
| 94 |
|
|---|
| 95 |
def drop_table! |
|---|
| 96 |
connection.execute "DROP TABLE #{table_name}" |
|---|
| 97 |
end |
|---|
| 98 |
|
|---|
| 99 |
private |
|---|
| 100 |
|
|---|
| 101 |
def setup_sessid_compatibility! |
|---|
| 102 |
|
|---|
| 103 |
reset_column_information |
|---|
| 104 |
if columns_hash['sessid'] |
|---|
| 105 |
def self.find_by_session_id(*args) |
|---|
| 106 |
find_by_sessid(*args) |
|---|
| 107 |
end |
|---|
| 108 |
|
|---|
| 109 |
define_method(:session_id) { sessid } |
|---|
| 110 |
define_method(:session_id=) { |session_id| self.sessid = session_id } |
|---|
| 111 |
else |
|---|
| 112 |
def self.find_by_session_id(session_id) |
|---|
| 113 |
find :first, :conditions => ["session_id #{attribute_condition(session_id)}", session_id] |
|---|
| 114 |
end |
|---|
| 115 |
end |
|---|
| 116 |
end |
|---|
| 117 |
end |
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
def data |
|---|
| 121 |
@data ||= self.class.unmarshal(read_attribute(@@data_column_name)) || {} |
|---|
| 122 |
end |
|---|
| 123 |
|
|---|
| 124 |
attr_writer :data |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
def loaded? |
|---|
| 128 |
!! @data |
|---|
| 129 |
end |
|---|
| 130 |
|
|---|
| 131 |
private |
|---|
| 132 |
|
|---|
| 133 |
def marshal_data! |
|---|
| 134 |
return false if !loaded? |
|---|
| 135 |
write_attribute(@@data_column_name, self.class.marshal(self.data)) |
|---|
| 136 |
end |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
def raise_on_session_data_overflow! |
|---|
| 142 |
return false if !loaded? |
|---|
| 143 |
limit = self.class.data_column_size_limit |
|---|
| 144 |
if loaded? and limit and read_attribute(@@data_column_name).size > limit |
|---|
| 145 |
raise ActionController::SessionOverflowError |
|---|
| 146 |
end |
|---|
| 147 |
end |
|---|
| 148 |
end |
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
class SqlBypass |
|---|
| 164 |
|
|---|
| 165 |
cattr_accessor :connection |
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
cattr_accessor :table_name |
|---|
| 169 |
@@table_name = 'sessions' |
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
cattr_accessor :session_id_column |
|---|
| 173 |
@@session_id_column = 'session_id' |
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
cattr_accessor :data_column |
|---|
| 177 |
@@data_column = 'data' |
|---|
| 178 |
|
|---|
| 179 |
class << self |
|---|
| 180 |
|
|---|
| 181 |
def connection |
|---|
| 182 |
@@connection ||= ActiveRecord::Base.connection |
|---|
| 183 |
end |
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
def find_by_session_id(session_id) |
|---|
| 187 |
if record = @@connection.select_one("SELECT * FROM #{@@table_name} WHERE #{@@session_id_column}=#{@@connection.quote(session_id)}") |
|---|
| 188 |
new(:session_id => session_id, :marshaled_data => record['data']) |
|---|
| 189 |
end |
|---|
| 190 |
end |
|---|
| 191 |
|
|---|
| 192 |
def marshal(data) ActiveSupport::Base64.encode64(Marshal.dump(data)) if data end |
|---|
| 193 |
def unmarshal(data) Marshal.load(ActiveSupport::Base64.decode64(data)) if data end |
|---|
| 194 |
|
|---|
| 195 |
def create_table! |
|---|
| 196 |
@@connection.execute <<-end_sql |
|---|
| 197 |
CREATE TABLE |
|---|
| 198 |
id INTEGER PRIMARY KEY, |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
) |
|---|
| 202 |
end_sql |
|---|
| 203 |
end |
|---|
| 204 |
|
|---|
| 205 |
def drop_table! |
|---|
| 206 |
@@connection.execute "DROP TABLE #{table_name}" |
|---|
| 207 |
end |
|---|
| 208 |
end |
|---|
| 209 |
|
|---|
| 210 |
attr_reader :session_id |
|---|
| 211 |
attr_writer :data |
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
def initialize(attributes) |
|---|
| 217 |
@session_id, @data, @marshaled_data = attributes[:session_id], attributes[:data], attributes[:marshaled_data] |
|---|
| 218 |
@new_record = @marshaled_data.nil? |
|---|
| 219 |
end |
|---|
| 220 |
|
|---|
| 221 |
def new_record? |
|---|
| 222 |
@new_record |
|---|
| 223 |
end |
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
def data |
|---|
| 227 |
unless @data |
|---|
| 228 |
if @marshaled_data |
|---|
| 229 |
@data, @marshaled_data = self.class.unmarshal(@marshaled_data) || {}, nil |
|---|
| 230 |
else |
|---|
| 231 |
@data = {} |
|---|
| 232 |
end |
|---|
| 233 |
end |
|---|
| 234 |
@data |
|---|
| 235 |
end |
|---|
| 236 |
|
|---|
| 237 |
def loaded? |
|---|
| 238 |
!! @data |
|---|
| 239 |
end |
|---|
| 240 |
|
|---|
| 241 |
def save |
|---|
| 242 |
return false if !loaded? |
|---|
| 243 |
marshaled_data = self.class.marshal(data) |
|---|
| 244 |
|
|---|
| 245 |
if @new_record |
|---|
| 246 |
@new_record = false |
|---|
| 247 |
@@connection.update <<-end_sql, 'Create session' |
|---|
| 248 |
INSERT INTO |
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
VALUES ( |
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
end_sql |
|---|
| 255 |
else |
|---|
| 256 |
@@connection.update <<-end_sql, 'Update session' |
|---|
| 257 |
UPDATE |
|---|
| 258 |
SET |
|---|
| 259 |
WHERE |
|---|
| 260 |
end_sql |
|---|
| 261 |
end |
|---|
| 262 |
end |
|---|
| 263 |
|
|---|
| 264 |
def destroy |
|---|
| 265 |
unless @new_record |
|---|
| 266 |
@@connection.delete <<-end_sql, 'Destroy session' |
|---|
| 267 |
DELETE FROM |
|---|
| 268 |
WHERE |
|---|
| 269 |
end_sql |
|---|
| 270 |
end |
|---|
| 271 |
end |
|---|
| 272 |
end |
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
cattr_accessor :session_class |
|---|
| 278 |
self.session_class = Session |
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
def initialize(session, option = nil) |
|---|
| 282 |
session_id = session.session_id |
|---|
| 283 |
unless @session = ActiveRecord::Base.silence { @@session_class.find_by_session_id(session_id) } |
|---|
| 284 |
unless session.new_session |
|---|
| 285 |
raise CGI::Session::NoSession, 'uninitialized session' |
|---|
| 286 |
end |
|---|
| 287 |
@session = @@session_class.new(:session_id => session_id, :data => {}) |
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
end |
|---|
| 292 |
end |
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
def model |
|---|
| 296 |
@session |
|---|
| 297 |
end |
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
def restore |
|---|
| 301 |
if @session |
|---|
| 302 |
@session.data |
|---|
| 303 |
end |
|---|
| 304 |
end |
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
def update |
|---|
| 308 |
if @session |
|---|
| 309 |
ActiveRecord::Base.silence { @session.save } |
|---|
| 310 |
end |
|---|
| 311 |
end |
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
def close |
|---|
| 315 |
if @session |
|---|
| 316 |
update |
|---|
| 317 |
@session = nil |
|---|
| 318 |
end |
|---|
| 319 |
end |
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
def delete |
|---|
| 323 |
if @session |
|---|
| 324 |
ActiveRecord::Base.silence { @session.destroy } |
|---|
| 325 |
@session = nil |
|---|
| 326 |
end |
|---|
| 327 |
end |
|---|
| 328 |
|
|---|
| 329 |
protected |
|---|
| 330 |
def logger |
|---|
| 331 |
ActionController::Base.logger rescue nil |
|---|
| 332 |
end |
|---|
| 333 |
end |
|---|
| 334 |
end |
|---|
| 335 |
end |
|---|