]> git.donarmstrong.com Git - biopieces.git/blobdiff - www/index.cgi
added persistent sessions to BGB
[biopieces.git] / www / index.cgi
index b4ccffc2a3ad9cd19478f3ef2fd39423e2af97f8..c0235f0549bf96d3d107629ae151d5e0de80d7a1 100755 (executable)
@@ -26,14 +26,16 @@ use warnings;
 use lib $ENV{ 'BP_PERL' };
 
 use CGI;
+use URI::Escape;
 use Data::Dumper;
+use Digest::MD5;
 use Maasha::Common;
 use Maasha::Filesys;
 use Maasha::Calc;
 use Maasha::XHTML;
-use Maasha::KISS;
-use Maasha::BBrowser::Track;
-use Maasha::BBrowser::Draw;
+use Maasha::BGB::Session;
+use Maasha::BGB::Track;
+use Maasha::BGB::Draw;
 
 my ( $cgi, $cookie, @html );
 
@@ -42,16 +44,16 @@ $cookie = cookie_default( $cgi );;
 
 push @html, Maasha::XHTML::html_header(
     cgi_header  => 1,
-    title       => "Biopieces Genome Browser",
-    css_file    => "bgb.css",
-    author      => "Martin A. Hansen, mail\@maasha.dk",
-    description => "Biopieces Genome Browser",
+    title       => 'Biopieces Genome Browser',
+    css_file    => 'bgb.css',
+    author      => 'Martin A. Hansen, mail@maasha.dk',
+    description => 'Biopieces Genome Browser',
     keywords    => [ qw( Biopieces biopiece genome browser viewer bacterium bacteria prokaryote prokaryotes ) ],
     no_cache    => 1,
 );
 
 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 );
 
@@ -59,8 +61,9 @@ push @html, Maasha::XHTML::form_end;
 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( { $cgi->Vars } );  # DEBUG
+# push @html, Maasha::XHTML::hdump( $cookie );         # DEBUG
+# push @html, Maasha::XHTML::hdump( \%ENV );           # DEBUG
 
 print "$_\n" foreach @html;
 
@@ -82,7 +85,7 @@ sub cookie_default
 
     # Returns a hash.
 
-    my ( $cookie, $path );
+    my ( $cookie, $session );
 
     $cookie = {};
 
@@ -103,17 +106,50 @@ 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->{ 'CLADE' }           = $cgi->param( 'clade' )    || '';
-    $cookie->{ 'GENOME' }          = $cgi->param( 'genome' )   || '';
-    $cookie->{ 'ASSEMBLY' }        = $cgi->param( 'assembly' ) || '';
-    $cookie->{ 'CONTIG' }          = $cgi->param( 'contig' )   || '';
-    $cookie->{ 'Q_ID' }            = $cgi->param( 'q_id' );
-    $cookie->{ 'S_BEG' }           = $cgi->param( 's_beg' );
-    $cookie->{ 'S_END' }           = $cgi->param( 's_end' );
-    $cookie->{ 'STRAND' }          = $cgi->param( 'strand' );
+    $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' );
+
+    $session = session_restore( $cookie );
+
+    cookie_login( $cookie );
+
+    # session_id_check( $cookie, $session );
+
+    if ( $cookie->{ 'LOGIN_ERROR' } or $cookie->{ 'SESSION_ERROR' } )
+    {
+        $cookie->{ 'PAGE' } = 'login';
+    }
+    elsif ( $cookie->{ 'LOGIN' } )
+    {
+        if ( $cgi->param( 'genome' ) ) {
+            $cookie->{ 'PAGE' } = 'browse';
+        } else {
+            $cookie->{ 'PAGE' } = $session->{ 'PAGE' } || 'clade';
+        }
+    }
+    elsif ( not defined $cookie->{ 'USER' } )
+    {
+        $cookie->{ 'PAGE' } = 'login';
+    }
+    else
+    {
+         $cookie->{ 'PAGE' } = $cgi->param( 'page' ) || 'login';
+    }
+
+    $cookie->{ 'CLADE' }           = $cgi->param( 'clade' )       || $session->{ 'CLADE' };
+    $cookie->{ 'GENOME' }          = $cgi->param( 'genome' )      || $session->{ 'GENOME' };
+    $cookie->{ 'ASSEMBLY' }        = $cgi->param( 'assembly' )    || $session->{ 'ASSEMBLY' };
+    $cookie->{ 'CONTIG' }          = $cgi->param( 'contig' )      || $session->{ 'CONTIG' };
+    $cookie->{ 'Q_ID' }            = $cgi->param( 'q_id' )        || $session->{ 'Q_ID' };
+    $cookie->{ 'NAV_START' }       = defined $cgi->param( 'nav_start' ) ? $cgi->param( 'nav_start' ) : $session->{ 'NAV_START' };
+    $cookie->{ 'NAV_END' }         = $cgi->param( 'nav_end' )     || $session->{ 'NAV_END' };
+    $cookie->{ 'NAV_CENTER' }      = $cgi->param( 'nav_center' );
+    $cookie->{ 'S_BEG' }           = $cgi->param( 's_beg' )       || $session->{ 'S_BEG' };
+    $cookie->{ 'S_END' }           = $cgi->param( 's_end' )       || $session->{ 'S_END' };
+    $cookie->{ 'STRAND' }          = $cgi->param( 'strand' )      || $session->{ 'STRAND' };
     $cookie->{ 'ZOOM_IN1' }        = $cgi->param( 'zoom_in1' );
     $cookie->{ 'ZOOM_IN2' }        = $cgi->param( 'zoom_in2' );
     $cookie->{ 'ZOOM_IN3' }        = $cgi->param( 'zoom_in3' );
@@ -127,49 +163,160 @@ sub cookie_default
     $cookie->{ 'MOVE_RIGHT2' }     = $cgi->param( 'move_right2' );
     $cookie->{ 'MOVE_RIGHT3' }     = $cgi->param( 'move_right3' );
 
-    $cookie->{ 'IMG_WIDTH' }       = 1200;
-    $cookie->{ 'IMG_HEIGHT' }      = 800;
+    $cookie->{ 'IMG_WIDTH' }       = 1200;   # Width of browser image in pixels
+    $cookie->{ 'IMG_HEIGHT' }      = 800;    # Height of browser image in pixels
+    $cookie->{ 'WIGGLE_HEIGHT' }   = 75;     # Height of Wiggle tracks in pixels
     $cookie->{ 'TRACK_OFFSET' }    = 20;
     $cookie->{ 'TRACK_SPACE' }     = 20;
-    $cookie->{ 'RULER_FONT_SIZE' } = 10;
+    $cookie->{ 'RULER_FONT_SIZE' } = 10;     # Size of ruler font in pixels
     $cookie->{ 'RULER_COLOR' }     = [ 0, 0, 0 ];
     $cookie->{ 'SEQ_FONT_SIZE' }   = 10;
     $cookie->{ 'SEQ_COLOR' }       = [ 0, 0, 0, ];
     $cookie->{ 'FEAT_WIDTH' }      = 5;
     $cookie->{ 'FEAT_COLOR' }      = [ 0, 0, 0 ];
-    $cookie->{ 'FEAT_MAX' }        = 5000;
+    $cookie->{ 'FEAT_MAX' }        = 5000;   # TODO: Reduntant?
 
-    $path = "$cookie->{ 'DATA_DIR' }/Users";
+#    $cookie->{ 'LIST_USER' }       = Maasha::BGB::Track::list_users();  # TODO: Redundant?
 
-    $cookie->{ 'LIST_USER' }       = Maasha::Filesys::ls_dirs_base( $path ) if -d $path;
+    if ( $cookie->{ 'USER' } and not $cookie->{ 'LOGIN_ERROR' } )
+    {
+        $cookie->{ 'LIST_CLADE' }      = Maasha::BGB::Track::list_clades(     $cookie->{ 'USER' } );
+        $cookie->{ 'LIST_GENOME' }     = Maasha::BGB::Track::list_genomes(    $cookie->{ 'USER' }, $cookie->{ 'CLADE' } );
+        $cookie->{ 'LIST_ASSEMBLY' }   = Maasha::BGB::Track::list_assemblies( $cookie->{ 'USER' }, $cookie->{ 'CLADE' }, $cookie->{ 'GENOME' } );
+        $cookie->{ 'LIST_CONTIG' }     = Maasha::BGB::Track::list_contigs(    $cookie->{ 'USER' }, $cookie->{ 'CLADE' }, $cookie->{ 'GENOME' }, $cookie->{ 'ASSEMBLY' } );
 
-    $path .= "/$cookie->{ 'USER' }";
+        if ( $cookie->{ 'CONTIG' } )
+        {
+            cookie_start( $cookie );
+            cookie_end( $cookie );
+            cookie_zoom( $cookie );
+            cookie_move( $cookie );
+            cookie_center( $cookie );
+        }
+    }
 
-    $cookie->{ 'LIST_CLADE' }      = Maasha::Filesys::ls_dirs_base( $path ) if -d $path;
+    $cookie->{ 'STRAND' } = '+' if defined $cookie->{ 'STRAND' } and $cookie->{ 'STRAND' } eq ' ';   # FIXME ugly HTML fix
 
-    $path .= "/$cookie->{ 'CLADE' }";
+    session_store( $cookie, $session );
 
-    $cookie->{ 'LIST_GENOME' }     = Maasha::Filesys::ls_dirs_base( $path ) if -d $path;
+    return wantarray ? %{ $cookie } : $cookie;
+}
+
+
+sub session_restore
+{
+    # Martin A. Hansen, March 2010.
+
+    # Restores session and returns this.
+
+    my ( $cookie,   # cookie hash
+       ) = @_;
+
+    # Returns hashref.
+
+    my ( $session );
 
-    $path .= "/$cookie->{ 'GENOME' }";
+    if ( defined $cookie->{ 'USER' } and -d "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }" ) {
+        $session = Maasha::BGB::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }/session.json" );
+    }
+
+    return wantarray ? %{ $session } : $session;
+}
+
+
+sub session_store
+{
+    # Martin A. Hansen, March 2010.
 
-    $cookie->{ 'LIST_ASSEMBLY' }   = Maasha::Filesys::ls_dirs_base( $path ) if -d $path;
+    # Store a session updated with cookie information to session file.
 
-    $path .= "/$cookie->{ 'ASSEMBLY' }";
+    my ( $cookie,    # cookie hash
+         $session,   # session hash
+       ) = @_;
 
-    $cookie->{ 'LIST_CONTIG' }     = Maasha::Filesys::ls_dirs_base( $path ) if -d $path;
+    # Returns nothing.
 
-    if ( $cookie->{ 'CONTIG' } )
+    if ( defined $cookie->{ 'USER' } and $cookie->{ 'USER' } ne '' and -d "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }" )
     {
-        cookie_start( $cookie );
-        cookie_end( $cookie );
-        cookie_zoom( $cookie );
-        cookie_move( $cookie );
+        $session->{ 'TIME' }      = Maasha::Common::time_stamp();
+        $session->{ 'PAGE' }      = $cookie->{ 'PAGE' };
+        $session->{ 'CLADE' }     = $cookie->{ 'CLADE' };
+        $session->{ 'GENOME' }    = $cookie->{ 'GENOME' };
+        $session->{ 'ASSEMBLY' }  = $cookie->{ 'ASSEMBLY' };
+        $session->{ 'CONTIG' }    = $cookie->{ 'CONTIG' };
+        $session->{ 'Q_ID' }      = $cookie->{ 'Q_ID' };
+        $session->{ 'S_BEG' }     = $cookie->{ 'S_BEG' };
+        $session->{ 'S_END' }     = $cookie->{ 'S_END' };
+        $session->{ 'STRAND' }    = $cookie->{ 'STRAND' };
+        $session->{ 'NAV_START' } = $cookie->{ 'PAGE' } eq 'browse' ? $cookie->{ 'NAV_START' } : undef;
+        $session->{ 'NAV_END' }   = $cookie->{ 'PAGE' } eq 'browse' ? $cookie->{ 'NAV_END' }   : undef;
+
+        Maasha::BGB::Session::session_store( "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }/session.json", $session );
     }
+}
 
-    $cookie->{ 'STRAND' } = '+' if defined $cookie->{ 'STRAND' } and $cookie->{ 'STRAND' } eq ' ';   # FIXME ugly HTML fix
 
-    return wantarray ? %{ $cookie } : $cookie;
+sub session_id_check
+{
+    # Martin A. Hansen, March 2010.
+
+    # Check that the session id in the cookie and session match.
+    # Sets SESSION_ERROR flag in cookie if not matching.
+
+    my ( $cookie,    # cookie hash
+         $session,   # session hash
+       ) = @_;
+
+    # Returns nothing.
+
+    if ( defined $cookie->{ 'SESSION_ID' } and defined $session->{ 'SESSION_ID' } )
+    {
+        if ( $cookie->{ 'SESSION_ID' } ne $session->{ 'SESSION_ID' } ) {
+            $cookie->{ 'SESSION_ERROR' } = 1;
+        } else {
+            $cookie->{ 'SESSION_ERROR' } = 0;
+        }
+    }
+}
+
+
+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 );
+
+    if ( defined $cookie->{ 'PASSWORD' } )
+    {
+        if ( defined $cookie->{ 'USER' } and -d "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }" )
+        {
+            $session = Maasha::BGB::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }/session.json" );
+
+            if ( $session->{ 'PASSWORD' } eq Digest::MD5::md5_hex( $cookie->{ 'PASSWORD' } ) )
+            {
+                $cookie->{ 'TIME' }          = Maasha::Common::time_stamp();
+                $cookie->{ 'SESSION_ID' }  ||= Maasha::BGB::Session::session_new();
+                $cookie->{ 'LOGIN' }         = 1;
+                $cookie->{ 'LOGIN_ERROR' }   = 0;
+            }
+            else
+            {
+                $cookie->{ 'LOGIN_ERROR' } = 1;
+            }
+        }
+        else
+        {
+            $cookie->{ 'LOGIN_ERROR' } = 1;
+        }
+    }
 }
 
 
@@ -212,7 +359,7 @@ sub cookie_end
 
     my ( $max );
     
-    $max = Maasha::Filesys::file_size( Maasha::BBrowser::Track::path_seq( $cookie ) );
+    $max = Maasha::Filesys::file_size( Maasha::BGB::Track::path_seq( $cookie ) );
 
     if ( defined $cookie->{ 'NAV_END' } )
     {
@@ -240,7 +387,7 @@ sub cookie_zoom
 
     my ( $max, $dist, $new_dist, $dist_diff );
 
-    $max = Maasha::Filesys::file_size( Maasha::BBrowser::Track::path_seq( $cookie ) );
+    $max = Maasha::Filesys::file_size( Maasha::BGB::Track::path_seq( $cookie ) );
 
     $dist = $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' };
 
@@ -281,9 +428,11 @@ sub cookie_move
     my ( $cookie,   # cookie hash
        ) = @_;
 
+    # Returns nothing.
+    
     my ( $max, $dist, $shift, $new_start, $new_end );
 
-    $max = Maasha::Filesys::file_size( Maasha::BBrowser::Track::path_seq( $cookie ) );
+    $max = Maasha::Filesys::file_size( Maasha::BGB::Track::path_seq( $cookie ) );
 
     $dist = $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' };
 
@@ -315,52 +464,31 @@ sub cookie_move
 }
 
 
-sub cookie_href
+sub cookie_center
 {
-    # Martin A. Hansen, November 2009.
-
-    # Returns the href for a taxonomy path terminated at a given
-    # page using information stored in a cookie.
+    # Martin A. Hansen, March 2010
+   
+    # Adjust the cookie values for NAV_START and NAV_END based
+    # on cookie CENTER value if defined.
 
     my ( $cookie,   # cookie hash
-         $page,     # page
        ) = @_;
 
-    # Returns a string.
+    # Returns nothing.
 
-    my ( @href_list, %href_hash, $href );
+    my ( $l_dist, $r_dist );
 
-    while ( 1 )
+    if ( defined $cookie->{ 'NAV_CENTER' } )
     {
-        $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' };
-        last if $page eq 'genome';
-        $href_hash{ "assembly" } = $cookie->{ 'ASSEMBLY' }  if $cookie->{ 'ASSEMBLY' };
-        last if $page eq 'assembly';
-        $href_hash{ "contig" } = $cookie->{ 'CONTIG' }      if $cookie->{ 'CONTIG' };
-        last if $page eq 'contig';
-        last;
-    }
-
-    if ( defined $cookie->{ 'NAV_START' } and defined $cookie->{ 'NAV_END' } ) {
-        $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' };
-
-    push @href_list, "$cookie->{ 'SCRIPT' }?page=$page";
+        $l_dist = $cookie->{ 'NAV_CENTER' } - $cookie->{ 'NAV_START' };
+        $r_dist = $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_CENTER' };
 
-    foreach $href ( keys %href_hash ) {
-        push @href_list, "$href=$href_hash{ $href }";
+        if ( $l_dist > $r_dist ) {
+            $cookie->{ 'NAV_START' } = $cookie->{ 'NAV_END' } - 2 * $r_dist;
+        } else {
+            $cookie->{ 'NAV_END' } = $cookie->{ 'NAV_START' } + 2 * $l_dist;
+        }
     }
-
-    return join "&", @href_list;
 }
 
 
@@ -402,14 +530,20 @@ sub page
 
     my ( @html );
 
-    if ( $cookie->{ 'PAGE' } eq 'export' ) {
-        push @html, page_export( $cookie );
+    if ( $cookie->{ 'SESSION_ID' } ) {
+        push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "session_id", value => $cookie->{ 'SESSION_ID' } ) );
+    }
+
+    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 );
     }
@@ -418,11 +552,11 @@ sub page
 }
 
 
-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 +565,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;
 }
@@ -474,6 +607,7 @@ sub page_browse
     push @html, section_taxonomy_table( $cookie );
     push @html, section_navigate( $cookie );
     push @html, section_browse( $cookie );
+    push @html, section_linkout( $cookie );
 
     return wantarray ? @html : \@html;
 }
@@ -499,6 +633,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 +676,55 @@ 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 ( defined $cookie->{ 'CLADE' }     and
+         defined $cookie->{ 'GENOME' }    and
+         defined $cookie->{ 'ASSEMBLY' }  and
+         defined $cookie->{ 'CONTIG' }    and
+         defined $cookie->{ 'NAV_START' } and
+         defined $cookie->{ 'NAV_END' }
+    )
+    {
+        push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "clade",     value => $cookie->{ 'CLADE' } ) );
+        push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "genome",    value => $cookie->{ 'GENOME' } ) );
+        push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "assembly",  value => $cookie->{ 'ASSEMBLY' } ) );
+        push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "contig",    value => $cookie->{ 'CONTIG' } ) );
+        push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "nav_start", value => $cookie->{ 'NAV_START' } ) );
+        push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "nav_end",   value => $cookie->{ 'NAV_END' } ) );
+    }
+
+    if ( $cookie->{ 'LOGIN_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.
@@ -541,7 +744,13 @@ sub section_taxonomy_table
     {
         last if $page eq $cookie->{ 'PAGE' } or $page eq 'browse';
 
-        $href = cookie_href( $cookie, $page );
+        $href  = "$cookie->{ 'SCRIPT' }?page=$page";
+        $href .= "&session_id=$cookie->{ 'SESSION_ID' }";
+        $href .= "&user=$cookie->{ 'USER' }";
+        $href .= "&clade=$cookie->{ 'CLADE' }"       if $page !~ /clade/;
+        $href .= "&genome=$cookie->{ 'GENOME' }"     if $page !~ /clade|genome/;
+        $href .= "&assembly=$cookie->{ 'ASSEMBLY' }" if $page !~ /clade|genome|assembly/;
+        $href .= "&contig=$cookie->{ 'CONTIG' }"     if $page !~ /clade|genome|assembly|contig/;
 
         $txt = ": $cookie->{ uc $page }";
 
@@ -581,7 +790,13 @@ sub section_taxonomy_select
     {
         $cookie->{ uc $cookie->{ 'PAGE' } } = $item;
 
-        $href = cookie_href( $cookie, cookie_page_next( $cookie ) );
+        $href  = "$cookie->{ 'SCRIPT' }?page=" . cookie_page_next( $cookie );
+        $href .= "&session_id=$cookie->{ 'SESSION_ID' }";
+        $href .= "&user=$cookie->{ 'USER' }";
+        $href .= "&clade=$cookie->{ 'CLADE' }";
+        $href .= "&genome=$cookie->{ 'GENOME' }"     if $cookie->{ 'GENOME' };
+        $href .= "&assembly=$cookie->{ 'ASSEMBLY' }" if $cookie->{ 'ASSEMBLY' };
+        $href .= "&contig=$cookie->{ 'CONTIG' }"     if $cookie->{ 'CONTIG' };
 
         push @html, Maasha::XHTML::table_row_simple( tr => [ Maasha::XHTML::ln( txt => $item, href => $href ) ] );
     }
@@ -636,11 +851,43 @@ sub section_navigate
     push @html, Maasha::XHTML::table_end;
 
     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "page",     value => "browse" ) );
-    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "user",     value => "$cookie->{ 'USER' }" ) );
-    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "clade",    value => "$cookie->{ 'CLADE' }" ) );
-    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "genome",   value => "$cookie->{ 'GENOME' }" ) );
-    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "assembly", value => "$cookie->{ 'ASSEMBLY' }" ) );
-    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "contig",   value => "$cookie->{ 'CONTIG' }" ) );
+    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "user",     value => $cookie->{ 'USER' } ) );
+    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "clade",    value => $cookie->{ 'CLADE' } ) );
+    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "genome",   value => $cookie->{ 'GENOME' } ) );
+    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "assembly", value => $cookie->{ 'ASSEMBLY' } ) );
+    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "contig",   value => $cookie->{ 'CONTIG' } ) );
+
+    return wantarray ? @html : \@html;
+}
+
+
+sub section_linkout
+{
+    # Martin A. Hansen, March 2010.
+
+    # Returns a HTML section for a static link based on information in the cookie.
+
+    my ( $cookie,   # cookie hash
+       ) = @_;
+
+    # Returns a list.
+
+    my ( $link, @html );
+
+    $link = Maasha::XHTML::ln(
+        txt  => 'link',
+        href => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
+                           "clade=$cookie->{ 'CLADE' }",
+                           "genome=$cookie->{ 'GENOME' }",
+                           "assembly=$cookie->{ 'ASSEMBLY' }",
+                           "contig=$cookie->{ 'CONTIG' }",
+                           "nav_start=$cookie->{ 'NAV_START' }",
+                           "nav_end=$cookie->{ 'NAV_END' }",
+              ),
+        title  => "Static link to this view",
+    );
+
+    push @html, Maasha::XHTML::p( txt => $link, class => 'center' );
 
     return wantarray ? @html : \@html;
 }
@@ -653,18 +900,18 @@ sub section_browse
 
     # Returns a list.
 
-    my ( @tracks, $i, @features, $feat, $elem, $surface, $cr, $png_data, @html, @img );
+    my ( @tracks, $i, @features, $feat, $elem, $surface, $cr, $png_data, @html, @img, $x1, $y1, $x2, $y2, $factor, $center );
 
-    push @features, [ Maasha::BBrowser::Track::track_ruler( $cookie ) ];
-    push @features, [ Maasha::BBrowser::Track::track_seq( $cookie ) ];
+    push @features, [ Maasha::BGB::Track::track_ruler( $cookie ) ];
+    push @features, [ Maasha::BGB::Track::track_seq( $cookie ) ];
 
-    @tracks = Maasha::BBrowser::Track::path_tracks( $cookie );
+    @tracks = Maasha::BGB::Track::path_tracks( $cookie );
 
     for ( $i = 0; $i < @tracks; $i++ )
     {
-        $cookie->{ 'FEAT_COLOR' } = Maasha::BBrowser::Draw::palette( $i );
+        $cookie->{ 'FEAT_COLOR' } = Maasha::BGB::Draw::palette( $i );
 
-        push @features, [ Maasha::BBrowser::Track::track_feature( $tracks[ $i ], $cookie ) ];
+        push @features, [ Maasha::BGB::Track::track_feature( $tracks[ $i ], $cookie ) ];
     }
 
     $surface = Cairo::ImageSurface->create( 'argb32', $cookie->{ 'IMG_WIDTH' }, $cookie->{ 'TRACK_OFFSET' } );
@@ -675,10 +922,10 @@ sub section_browse
     $cr->fill;
 
     foreach $feat ( @features ) {
-        Maasha::BBrowser::Draw::draw_feature( $cr, $feat ) if $feat;
+        Maasha::BGB::Draw::draw_feature( $cr, $feat ) if $feat;
     }
 
-    $png_data = Maasha::BBrowser::Draw::base64_png( $surface );
+    $png_data = Maasha::BGB::Draw::base64_png( $surface );
 
     push @img, Maasha::XHTML::img(
         src    => "data:image/png;base64,$png_data",
@@ -691,11 +938,41 @@ sub section_browse
 
     push @img, Maasha::XHTML::map_beg( name => "browser_map", id => "browser_map" );
 
+    $factor = ( $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' } + 1 ) / $cookie->{ 'IMG_WIDTH' };
+
+    for ( $i = 0; $i < $cookie->{ 'IMG_WIDTH' }; $i += 5 )
+    {
+        $x1 = $i;
+        $y1 = 10;
+        $x2 = $i + 5;
+        $y2 = 20;
+
+        $center = int( $cookie->{ 'NAV_START' } + $factor * $i );
+
+        push @img, Maasha::XHTML::area(
+            href     => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
+                                   "session_id=$cookie->{ 'SESSION_ID' }",
+                                   "user=$cookie->{ 'USER' }",
+                                   "clade=$cookie->{ 'CLADE' }",
+                                   "genome=$cookie->{ 'GENOME' }",
+                                   "assembly=$cookie->{ 'ASSEMBLY' }",
+                                   "contig=$cookie->{ 'CONTIG' }",
+                                   "nav_start=$cookie->{ 'NAV_START' }",
+                                   "nav_end=$cookie->{ 'NAV_END' }",
+                                   "nav_center=$center",
+                      ),
+            shape  => 'rect',
+            coords => "$x1, $y1, $x2, $y2",
+            title  => "Center on " . Maasha::Calc::commify( $center ),
+        );
+    }
+
     foreach $feat ( @features )
     {
         foreach $elem ( @{ $feat } )
         {
             next if $elem->{ 'type' } eq 'text';
+            next if $elem->{ 'type' } eq 'wiggle';
 
             #$elem->{ 'strand' } = '&#43' if $elem->{ 'strand' } eq '+';
 
@@ -705,10 +982,20 @@ sub section_browse
             $cookie->{ 'STRAND' } = $elem->{ 'strand' };
 
             push @img, Maasha::XHTML::area(
-                href   => cookie_href( $cookie, "export" ),
-                # href   => cookie_href( $cookie, "export" ) . "&q_id=$elem->{ 'q_id' }&s_beg=$elem->{ 's_beg' }&s_end=$elem->{ 's_end' }&strand=$elem->{ 'strand' }",
-                shape  => "rect",
-                coords => "$elem->{ x1 }, $elem->{ y1 }, $elem->{ x2 }, $elem->{ y2 }", title => "$elem->{ 'title' }",
+                href     => join( "&", "$cookie->{ 'SCRIPT' }?page=export",
+                                       "session_id=$cookie->{ 'SESSION_ID' }",
+                                       "user=$cookie->{ 'USER' }",
+                                       "clade=$cookie->{ 'CLADE' }",
+                                       "genome=$cookie->{ 'GENOME' }",
+                                       "assembly=$cookie->{ 'ASSEMBLY' }",
+                                       "contig=$cookie->{ 'CONTIG' }",
+                                       "s_beg=$cookie->{ 'S_BEG' }",
+                                       "s_end=$cookie->{ 'S_END' }",
+                                       "strand=$cookie->{ 'STRAND' }",
+                          ),
+                shape  => 'rect',
+                coords => "$elem->{ x1 }, $elem->{ y1 }, $elem->{ x2 }, $elem->{ y2 }",
+                title  => "$elem->{ 'title' }",
             );
         }
     }
@@ -734,9 +1021,11 @@ sub section_export
 
     # Returns a list.
 
-    my ( @row_dna_contig, @html );
+    my ( $feat, @row_dna_contig, @html );
 
-    push @row_dna_contig, Maasha::XHTML::p( txt => qq(Export Feature: "$cookie->{ 'Q_ID' }" DNA from Contig: ), class => 'inline' );
+    $feat = $cookie->{ 'Q_ID' } || "[no name]";
+
+    push @row_dna_contig, Maasha::XHTML::p( txt => qq(Export Feature: "$feat" DNA from Contig: ), class => 'inline' );
     push @row_dna_contig, Maasha::XHTML::p( txt  => "$cookie->{ 'CONTIG' } Start: ", class => 'inline' );
     push @row_dna_contig, Maasha::XHTML::text( name => "s_beg", value => Maasha::Calc::commify( $cookie->{ 'S_BEG' } ), size => 15 );
     push @row_dna_contig, Maasha::XHTML::p( txt => "End: ", class => 'inline' );
@@ -750,12 +1039,12 @@ sub section_export
     push @html, Maasha::XHTML::table_row_simple( tr => \@row_dna_contig );
     push @html, Maasha::XHTML::table_end;
 
-    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "page", value => "dna" ) );
-    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "user",     value => "$cookie->{ 'USER' }" ) );
-    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "clade",    value => "$cookie->{ 'CLADE' }" ) );
-    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "genome",   value => "$cookie->{ 'GENOME' }" ) );
-    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "assembly", value => "$cookie->{ 'ASSEMBLY' }" ) );
-    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "contig",   value => "$cookie->{ 'CONTIG' }" ) );
+    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "page",     value => "dna" ) );
+    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "user",     value => $cookie->{ 'USER' } ) );
+    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "clade",    value => $cookie->{ 'CLADE' } ) );
+    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "genome",   value => $cookie->{ 'GENOME' } ) );
+    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "assembly", value => $cookie->{ 'ASSEMBLY' } ) );
+    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "contig",   value => $cookie->{ 'CONTIG' } ) );
 
     return wantarray ? @html : \@html;
 }
@@ -774,7 +1063,7 @@ sub section_search
 
     my ( $results, $result, $count, @html, $export, $browse );
 
-    $results = Maasha::BBrowser::Track::search_tracks( $cookie );
+    $results = Maasha::BGB::Track::search_tracks( $cookie );
 
     $count = scalar @{ $results };
 
@@ -796,8 +1085,35 @@ sub section_search
             $cookie->{ 'STRAND' }    = $result->{ 'STRAND' };
             $cookie->{ 'Q_ID' }      = $result->{ 'Q_ID' };
 
-            $browse = Maasha::XHTML::ln( txt => "browse", href => cookie_href( $cookie, "browse" ) );
-            $export = Maasha::XHTML::ln( txt => "export", href => cookie_href( $cookie, "export" ) );
+            $browse = Maasha::XHTML::ln(
+                txt  => "browse",
+                href => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
+                                   "session_id=$cookie->{ 'SESSION_ID' }",
+                                   "user=$cookie->{ 'USER' }",
+                                   "clade=$cookie->{ 'CLADE' }",
+                                   "genome=$cookie->{ 'GENOME' }",
+                                   "assembly=$cookie->{ 'ASSEMBLY' }",
+                                   "contig=$cookie->{ 'CONTIG' }",
+                                   "nav_start=$cookie->{ 'NAV_START' }",
+                                   "nav_end=$cookie->{ 'NAV_END' }",
+                ),
+            );
+
+
+            $export = Maasha::XHTML::ln(
+                txt  => "export",
+                href => join( "&", "$cookie->{ 'SCRIPT' }?page=export",
+                                   "session_id=$cookie->{ 'SESSION_ID' }",
+                                   "user=$cookie->{ 'USER' }",
+                                   "clade=$cookie->{ 'CLADE' }",
+                                   "genome=$cookie->{ 'GENOME' }",
+                                   "assembly=$cookie->{ 'ASSEMBLY' }",
+                                   "contig=$cookie->{ 'CONTIG' }",
+                                   "s_beg=$cookie->{ 'S_BEG' }",
+                                   "s_end=$cookie->{ 'S_END' }",
+                                   "strand=$cookie->{ 'STRAND' }",
+                ),
+            );
 
             push @html, Maasha::XHTML::table_row_advanced(
                 tr => [ { td => $result->{ 'S_ID' } },
@@ -843,8 +1159,8 @@ sub section_dna
     $beg =~ tr/,//d;
     $end =~ tr/,//d;
 
-    $seq = ">$cookie->{ 'CONTIG' }_$beg" . "_$end" . "_$cookie->{ 'STRAND' }\n";
-    $seq .= Maasha::BBrowser::Track::dna_get( $cookie );
+    $seq = join ";", ">$cookie->{ 'GENOME' }", $cookie->{ 'ASSEMBLY' }, $cookie->{ 'CONTIG' }, $beg, $end, "$cookie->{ 'STRAND' }\n";
+    $seq .= Maasha::BGB::Track::dna_get( $cookie );
 
     push @html, Maasha::XHTML::h2( txt => "DNA", class => 'center' );
     push @html, Maasha::XHTML::pre( txt => $seq );