]> git.donarmstrong.com Git - biopieces.git/blobdiff - www/index.cgi
added security to browser
[biopieces.git] / www / index.cgi
index a8eabb311361c86350347b3b799ebc7195b37c58..0da25999dfc111cedc77cec42144471291e40dd1 100755 (executable)
@@ -27,11 +27,13 @@ use lib $ENV{ 'BP_PERL' };
 
 use CGI;
 use Data::Dumper;
+use Digest::MD5;
 use Maasha::Common;
 use Maasha::Filesys;
 use Maasha::Calc;
 use Maasha::XHTML;
 use Maasha::KISS;
+use Maasha::BBrowser::Session;
 use Maasha::BBrowser::Track;
 use Maasha::BBrowser::Draw;
 
@@ -51,7 +53,7 @@ push @html, Maasha::XHTML::html_header(
 );
 
 push @html, Maasha::XHTML::h1( txt => "Biopieces Genome Browser", class => 'center' );
-push @html, Maasha::XHTML::form_beg( action => $cookie->{ 'SCRIPT' }, method => "get", enctype => "multipart/form-data" );
+push @html, Maasha::XHTML::form_beg( action => $cookie->{ 'SCRIPT' }, method => "post", enctype => "multipart/form-data" );
 
 push @html, page( $cookie );
 
@@ -60,7 +62,8 @@ push @html, Maasha::XHTML::body_end;
 push @html, Maasha::XHTML::html_end;
 
 # push @html, Maasha::XHTML::hdump( [ $cgi->Vars ] );  # DEBUG
-# push @html, Maasha::XHTML::hdump( $cookie );     # DEBUG
+# push @html, Maasha::XHTML::hdump( \%ENV );           # DEBUG
+# push @html, Maasha::XHTML::hdump( $cookie );         # DEBUG
 
 print "$_\n" foreach @html;
 
@@ -103,9 +106,17 @@ sub cookie_default
 
     $cookie->{ 'SCRIPT' }          = Maasha::Common::get_scriptname();
     $cookie->{ 'DATA_DIR' }        = "Data";
-    $cookie->{ 'LIST_PAGES' }      = [ qw( user clade genome assembly contig browse ) ];
-    $cookie->{ 'PAGE' }            = $cgi->param( 'page' )     || 'user';
-    $cookie->{ 'USER' }            = $cgi->param( 'user' )     || '';
+    $cookie->{ 'SESSION_DIR' }     = "Sessions";
+    $cookie->{ 'LIST_PAGES' }      = [ qw( clade genome assembly contig browse ) ];
+
+    $cookie->{ 'USER' }            = $cgi->param( 'user' );
+    $cookie->{ 'PASSWORD' }        = $cgi->param( 'password' );
+    $cookie->{ 'SESSION_ID' }      = $cgi->param( 'session_id' );
+
+    cookie_session( $cookie );
+    cookie_login( $cookie );
+
+    $cookie->{ 'PAGE' }          ||= $cgi->param( 'page' )     || 'login';
     $cookie->{ 'CLADE' }           = $cgi->param( 'clade' )    || '';
     $cookie->{ 'GENOME' }          = $cgi->param( 'genome' )   || '';
     $cookie->{ 'ASSEMBLY' }        = $cgi->param( 'assembly' ) || '';
@@ -173,6 +184,68 @@ sub cookie_default
 }
 
 
+sub cookie_session
+{
+    # Martin A. Hansen, December 2009.
+
+    # Check cookie information against session information.
+
+    my ( $cookie,   # cookie hash
+       ) = @_;
+
+    # Returns nothing.
+
+    my ( $session );
+
+    $session = Maasha::BBrowser::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/sessions.txt" );
+
+    $cookie->{ 'PAGE' } = 'login' if not $cookie->{ 'SESSION_ID' };
+    $cookie->{ 'PAGE' } = 'login' if $cookie->{ 'SESSION_ID' } ne $ENV{ 'SSL_SESSION_ID' };
+    $cookie->{ 'PAGE' } = 'login' if not exists $session->{ $cookie->{ 'USER' } };
+    $cookie->{ 'PAGE' } = 'login' if $session->{ $cookie->{ 'USER' } }->{ 'SESSION_ID' } ne $cookie->{ 'SESSION_ID' };
+}
+
+
+sub cookie_login
+{
+    # Martin A. Hansen, December 2009.
+
+    # Check a user and password from CGI against the password file and
+    # set the session ID, if found, in the cookie.
+
+    my ( $cookie,   # cookie hash
+       ) = @_;
+
+    # Returns nothing.
+
+    my ( $session );
+
+    $session = Maasha::BBrowser::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/sessions.txt" );
+    
+    if ( $cookie->{ 'USER' } and $cookie->{ 'PASSWORD' } )
+    {
+        if ( exists $session->{ $cookie->{ 'USER' } } and
+             $session->{ $cookie->{ 'USER' } }->{ 'PASSWORD' } eq Digest::MD5::md5_hex( $cookie->{ 'PASSWORD' } ) )
+        {
+            # $session->{ $cookie->{ 'USER' } }->{ 'SESSION_ID' } ||= Maasha::BBrowser::Session::session_new();
+            $session->{ $cookie->{ 'USER' } }->{ 'SESSION_ID' }   = $ENV{ 'SSL_SESSION_ID' };
+            $session->{ $cookie->{ 'USER' } }->{ 'TIME' }         = Maasha::Common::time_stamp();
+
+            $cookie->{ 'SESSION_ID' } = $session->{ $cookie->{ 'USER' } }->{ 'SESSION_ID' };
+            $cookie->{ 'LOGIN' }      = "OK";
+            $cookie->{ 'USER' }       = $cookie->{ 'USER' };
+            $cookie->{ 'PAGE' }       = 'clade'; 
+
+            Maasha::BBrowser::Session::session_store( "$cookie->{ 'SESSION_DIR' }/sessions.txt", $session );
+        }
+        else
+        {
+            $cookie->{ 'LOGIN' } = "ERROR";
+        }
+    }
+}
+
+
 sub cookie_start
 {
     # Martin A. Hansen, November 2009.
@@ -330,10 +403,10 @@ sub cookie_href
 
     my ( @href_list, %href_hash, $href );
 
+    $href_hash{ "user" } = $cookie->{ 'USER' } if $cookie->{ 'USER' };
+
     while ( 1 )
     {
-        $href_hash{ "user" }  = $cookie->{ 'USER' }         if $cookie->{ 'USER' };
-        last if $page eq 'user';
         $href_hash{ "clade" } = $cookie->{ 'CLADE' }        if $cookie->{ 'CLADE' };
         last if $page eq 'clade';
         $href_hash{ "genome" } = $cookie->{ 'GENOME' }      if $cookie->{ 'GENOME' };
@@ -349,10 +422,11 @@ sub cookie_href
         $href_hash{ "nav_search" } = "$cookie->{ 'NAV_START' }-$cookie->{ 'NAV_END' }";
     }
 
-    $href_hash{ "s_beg" }  = $cookie->{ 'S_BEG' }  if defined $cookie->{ 'S_BEG' };
-    $href_hash{ "s_end" }  = $cookie->{ 'S_END' }  if defined $cookie->{ 'S_END' };
-    $href_hash{ "q_id" }   = $cookie->{ 'Q_ID' }   if defined $cookie->{ 'Q_ID' };
-    $href_hash{ "strand" } = $cookie->{ 'STRAND' } if defined $cookie->{ 'STRAND' };
+    $href_hash{ "s_beg" }      = $cookie->{ 'S_BEG' }      if defined $cookie->{ 'S_BEG' };
+    $href_hash{ "s_end" }      = $cookie->{ 'S_END' }      if defined $cookie->{ 'S_END' };
+    $href_hash{ "q_id" }       = $cookie->{ 'Q_ID' }       if defined $cookie->{ 'Q_ID' };
+    $href_hash{ "strand" }     = $cookie->{ 'STRAND' }     if defined $cookie->{ 'STRAND' };
+    $href_hash{ "session_id" } = $cookie->{ 'SESSION_ID' } if defined $cookie->{ 'SESSION_ID' };
 
     push @href_list, "$cookie->{ 'SCRIPT' }?page=$page";
 
@@ -402,27 +476,31 @@ sub page
 
     my ( @html );
 
-    if ( $cookie->{ 'PAGE' } eq 'export' ) {
-        push @html, page_export( $cookie );
+    if ( $cookie->{ 'PAGE' } eq 'login' ) {
+        push @html, page_login( $cookie );
     } elsif ( $cookie->{ 'PAGE' } eq 'search' ) {
         push @html, page_search( $cookie );
     } elsif ( $cookie->{ 'PAGE' } eq 'browse' ) {
         push @html, page_browse( $cookie );
     } elsif ( $cookie->{ 'PAGE' } eq 'dna' ) {
         push @html, page_dna( $cookie );
+    } elsif ( $cookie->{ 'PAGE' } eq 'export' ) {
+        push @html, page_export( $cookie );
     } else {
         push @html, page_taxonomy( $cookie );
     }
 
+    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "session_id", value => "$cookie->{ 'SESSION_ID' }" ) );
+
     return wantarray ? @html : \@html;
 }
 
 
-sub page_export
+sub page_login
 {
-    # Martin A. Hansen, November 2009.
+    # Martin A. Hansen, December 2009.
 
-    # Renders the export page.
+    # Renders the login page.
 
     my ( $cookie,
        ) = @_;
@@ -431,8 +509,7 @@ sub page_export
 
     my ( @html );
 
-    push @html, section_taxonomy_table( $cookie );
-    push @html, section_export( $cookie );
+    push @html, section_login( $cookie );
 
     return wantarray ? @html : \@html;
 }
@@ -499,6 +576,26 @@ sub page_dna
 }
 
 
+sub page_export
+{
+    # Martin A. Hansen, November 2009.
+
+    # Renders the export page.
+
+    my ( $cookie,
+       ) = @_;
+
+    # Returns a list.
+
+    my ( @html );
+
+    push @html, section_taxonomy_table( $cookie );
+    push @html, section_export( $cookie );
+
+    return wantarray ? @html : \@html;
+}
+
+
 sub page_taxonomy
 {
     # Martin A. Hansen, November 2009.
@@ -522,6 +619,39 @@ sub page_taxonomy
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SECTIONS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
+sub section_login
+{
+    # Martin A. Hansen, December 2009.
+   
+    # Returns a HTML section with a login menu.
+
+    my ( $cookie,   # cookie hash
+       ) = @_;
+
+    # Returns a list.
+
+    my ( $user, $password, $login, @html );
+
+    $user     = Maasha::XHTML::text( name => "user", value => "", size => 20 );
+    $password = Maasha::XHTML::password( name => "password", value => "", size => 20 );
+    $login    = Maasha::XHTML::submit( name => "login_submit", value => "Login" );
+
+    push @html, Maasha::XHTML::h2( txt => "Login", class => 'center' );
+
+    push @html, Maasha::XHTML::table_beg( summary => "Login table", align => 'center' );
+    push @html, Maasha::XHTML::table_row_simple( tr => [ "User:", $user ] );
+    push @html, Maasha::XHTML::table_row_simple( tr => [ "Password:", $password ] );
+    push @html, Maasha::XHTML::table_row_simple( tr => [ "", $login ] );
+    push @html, Maasha::XHTML::table_end;
+
+    if ( $cookie->{ 'LOGIN' } and $cookie->{ 'LOGIN' } eq 'ERROR' ) {
+        push @html, Maasha::XHTML::h3( txt => "Bad user or password - please retry", class => 'error' );
+    }
+
+    return wantarray ? @html : \@html;
+}
+
+
 sub section_taxonomy_table
 {
     # Martin A. Hansen, November 2009.
@@ -653,7 +783,7 @@ sub section_browse
 
     # Returns a list.
 
-    my ( @tracks, $i, @features, $feat, $elem, $file, $surface, $cr, @html, @img );
+    my ( @tracks, $i, @features, $feat, $elem, $surface, $cr, $png_data, @html, @img );
 
     push @features, [ Maasha::BBrowser::Track::track_ruler( $cookie ) ];
     push @features, [ Maasha::BBrowser::Track::track_seq( $cookie ) ];
@@ -667,8 +797,6 @@ sub section_browse
         push @features, [ Maasha::BBrowser::Track::track_feature( $tracks[ $i ], $cookie ) ];
     }
 
-    $file = "fisk.png";   # FIXME
-
     $surface = Cairo::ImageSurface->create( 'argb32', $cookie->{ 'IMG_WIDTH' }, $cookie->{ 'TRACK_OFFSET' } );
     $cr      = Cairo::Context->create( $surface );
 
@@ -680,10 +808,10 @@ sub section_browse
         Maasha::BBrowser::Draw::draw_feature( $cr, $feat ) if $feat;
     }
 
-    Maasha::BBrowser::Draw::file_png( $surface, $file );
+    $png_data = Maasha::BBrowser::Draw::base64_png( $surface );
 
     push @img, Maasha::XHTML::img(
-        src    => $file,
+        src    => "data:image/png;base64,$png_data",
         alt    => "Browser Tracks",
         height => $cookie->{ 'TRACK_OFFSET' },
         width  => $cookie->{ 'IMG_WIDTH' },