]> git.donarmstrong.com Git - biopieces.git/blob - code_perl/Maasha/BGB/Session.pm
renaming BBrowser to BGB
[biopieces.git] / code_perl / Maasha / BGB / Session.pm
1 package Maasha::BGB::Session;
2
3 # Copyright (C) 2009 Martin A. Hansen.
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24
25 # Routines for session handling of the Biopieces Browser.
26
27
28 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
29
30
31 use warnings;
32 use strict;
33 use Data::Dumper;
34 use Digest::MD5;
35 use Maasha::Common;
36 use Maasha::Filesys;
37
38 use vars qw( @ISA @EXPORT );
39
40 @ISA = qw( Exporter );
41
42
43 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
44
45
46 sub session_new
47 {
48     # Martin A. Hansen, December 2009.
49   
50     # Create a new session id which is md5 hashed.
51
52     # Returns a string.
53
54     my ( $sid );
55
56     $sid = Digest::MD5::md5_hex( Maasha::Common::get_sessionid() );
57
58     return $sid;
59 }
60
61
62 sub session_restore
63 {
64     # Martin A. Hansen, December 2009.
65     
66     # Parses a tab seperated session file and returns the data
67     # as a hash with user as key, and the rest of the columns as
68     # a hash.
69
70     my ( $file,   # session file
71        ) = @_;
72
73     # Returns a hashref.
74
75     my ( $fh, $line, $user, $password, $sid, $time, %session );
76
77     $fh = Maasha::Filesys::file_read_open( $file );
78
79     while ( $line = <$fh> )
80     {
81         chomp $line;
82
83         ( $user, $password, $sid, $time ) = split /\t/, $line;
84
85         $session{ $user } = {
86             PASSWORD   => $password,
87             SESSION_ID => $sid,
88             TIME       => $time,
89         };
90     }
91
92     close $fh;
93
94     return wantarray ? %session : \%session;
95 }
96
97
98 sub session_store
99 {
100     # Martin A. Hansen, December 2009.
101
102     # Stores a session hash to file.
103
104     my ( $file,      # file to store in.
105          $session,   # session to store.
106        ) = @_;
107
108     # Returns nothing.
109
110     my ( $fh, $user );
111
112     $fh = Maasha::Filesys::file_write_open( $file );
113
114     foreach $user ( keys %{ $session } )
115     {
116         print $fh join(
117             "\t",
118             $user,
119             $session->{ $user }->{ 'PASSWORD' },
120             $session->{ $user }->{ 'SESSION_ID' },
121             $session->{ $user }->{ 'TIME' }
122         ), "\n";
123     }
124
125     close $fh;
126 }
127
128
129 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
130
131
132 1;