From 7da400b6349b0223eb4850af0e081b47b537a12f Mon Sep 17 00:00:00 2001 From: martinahansen Date: Sat, 13 Mar 2010 17:13:48 +0000 Subject: [PATCH] changed BGB session format to JSON git-svn-id: http://biopieces.googlecode.com/svn/trunk@898 74ccb610-7750-0410-82ae-013aeee3265d --- code_perl/Maasha/BGB/Session.pm | 41 ++++++++++++++------------------- www/index.cgi | 6 ++--- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/code_perl/Maasha/BGB/Session.pm b/code_perl/Maasha/BGB/Session.pm index 129e41f..d55132a 100644 --- a/code_perl/Maasha/BGB/Session.pm +++ b/code_perl/Maasha/BGB/Session.pm @@ -32,6 +32,7 @@ use warnings; use strict; use Data::Dumper; use Digest::MD5; +use JSON; use Maasha::Common; use Maasha::Filesys; @@ -72,26 +73,21 @@ sub session_restore # Returns a hashref. - my ( $fh, $line, $user, $password, $sid, $time, %session ); + my ( $fh, $json, $json_text, $session ); + + local $/ = undef; $fh = Maasha::Filesys::file_read_open( $file ); - while ( $line = <$fh> ) - { - chomp $line; + $json_text = <$fh>; - ( $user, $password, $sid, $time ) = split /\t/, $line; + close $fh; - $session{ $user } = { - PASSWORD => $password, - SESSION_ID => $sid, - TIME => $time, - }; - } + $json = new JSON; - close $fh; + $session = $json->decode( $json_text ); - return wantarray ? %session : \%session; + return wantarray ? %{ $session } : $session; } @@ -107,20 +103,15 @@ sub session_store # Returns nothing. - my ( $fh, $user ); + my ( $json, $json_text, $fh ); + + $json = new JSON; + + $json_text = $json->pretty->encode( $session ); $fh = Maasha::Filesys::file_write_open( $file ); - foreach $user ( keys %{ $session } ) - { - print $fh join( - "\t", - $user, - $session->{ $user }->{ 'PASSWORD' }, - $session->{ $user }->{ 'SESSION_ID' }, - $session->{ $user }->{ 'TIME' } - ), "\n"; - } + print $fh $json_text; close $fh; } @@ -130,3 +121,5 @@ sub session_store 1; + +__END__ diff --git a/www/index.cgi b/www/index.cgi index ec03bf6..8a3377f 100755 --- a/www/index.cgi +++ b/www/index.cgi @@ -202,7 +202,7 @@ sub cookie_session my ( $session ); - $session = Maasha::BGB::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/sessions.txt" ); + $session = Maasha::BGB::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/sessions.json" ); # if ( $cookie->{ 'SESSION_ID' } ) # { @@ -252,7 +252,7 @@ sub cookie_login if ( $cookie->{ 'USER' } and $cookie->{ 'PASSWORD' } ) { - $session = Maasha::BGB::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/sessions.txt" ); + $session = Maasha::BGB::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/sessions.json" ); if ( exists $session->{ $cookie->{ 'USER' } } and $session->{ $cookie->{ 'USER' } }->{ 'PASSWORD' } eq Digest::MD5::md5_hex( $cookie->{ 'PASSWORD' } ) ) @@ -264,7 +264,7 @@ sub cookie_login $cookie->{ 'SESSION_ID' } = $session->{ $cookie->{ 'USER' } }->{ 'SESSION_ID' }; $cookie->{ 'LOGIN' } = "OK"; - Maasha::BGB::Session::session_store( "$cookie->{ 'SESSION_DIR' }/sessions.txt", $session ); + Maasha::BGB::Session::session_store( "$cookie->{ 'SESSION_DIR' }/sessions.json", $session ); } else { -- 2.39.5