From 132404dee5572d0ff2033156382fc25d9564913a Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@biblibre.com>
Date: Fri, 6 Jul 2012 11:29:06 +0200
Subject: [PATCH] Bug 7167: Now, we check versions on mainpage.pl and after
 login
Content-Type: text/plain; charset="utf-8"

Signed-off-by: Paul Poulain <paul.poulain@biblibre.com>
Signed-off-by: Chris Nighswonger <cnighswonger@foundations.edu>
---
 C4/Auth.pm  |   60 +++++++++++++++++++++++++++++++++++++++++++++--------------
 mainpage.pl |   17 +----------------
 2 files changed, 47 insertions(+), 30 deletions(-)

diff --git a/C4/Auth.pm b/C4/Auth.pm
index e3d5ad8..ff96cf3 100644
--- a/C4/Auth.pm
+++ b/C4/Auth.pm
@@ -28,6 +28,7 @@ require Exporter;
 use C4::Context;
 use C4::Templates;    # to get the template
 use C4::Branch; # GetBranches
+use C4::Update::Database;
 use C4::VirtualShelves;
 use POSIX qw/strftime/;
 use List::MoreUtils qw/ any /;
@@ -134,9 +135,9 @@ sub get_template_and_user {
     my $in       = shift;
     my $template =
       C4::Templates::gettemplate( $in->{'template_name'}, $in->{'type'}, $in->{'query'} );
-    my ( $user, $cookie, $sessionID, $flags );
+    my ( $user, $cookie, $sessionID, $flags, $new_session );
     if ( $in->{'template_name'} !~m/maintenance/ ) {
-        ( $user, $cookie, $sessionID, $flags ) = checkauth(
+        ( $user, $cookie, $sessionID, $flags, $new_session ) = checkauth(
             $in->{'query'},
             $in->{'authnotrequired'},
             $in->{'flagsrequired'},
@@ -466,6 +467,12 @@ sub get_template_and_user {
 
         $template->param(OpacPublic => '1') if ($user || C4::Context->preference("OpacPublic"));
     }
+
+    if ( $new_session ) {
+        # Check the version and redirect if DB is not up-to-date
+        version_check($in->{query}, $in->{'type'}, $cookie);
+    }
+
     return ( $template, $borrowernumber, $cookie, $flags);
 }
 
@@ -563,6 +570,38 @@ sub _timeout_syspref {
     return $timeout;
 }
 
+sub version_check {
+    my ( $query, $type, $cookie ) = @_;
+    # check we have a Version. Otherwise => go to installer
+    unless ( C4::Context->preference('Version') ) {
+        if ( $type ne 'opac' ) {
+            $debug && warn "Install required, redirecting to Installer";
+            print $query->redirect("/cgi-bin/koha/installer/install.pl");
+        } else {
+            $debug && warn "OPAC Install required, redirecting to maintenance";
+            print $query->redirect("/cgi-bin/koha/maintenance.pl");
+        }
+        safe_exit;
+    }
+
+    # check if you're uptodate, and if you're not, head to updater
+    my $koha39 =~ "3.0900000";
+
+    # Old updatedatabase method
+    if (C4::Context->preference('Version') < $koha39) {
+        print $query->redirect("/cgi-bin/koha/installer/install.pl?step=3");
+        safe_exit;
+    }
+
+    # New updatedatabase method
+    unless ( C4::Update::Database::is_uptodate() ) {
+        # not up-to-date, redirect to updatedatabase page
+        warn "redirect to updatedatabase";
+        print $query->redirect(-location => "/cgi-bin/koha/admin/updatedatabase.pl", -cookie => $cookie);
+        safe_exit;
+    }
+}
+
 sub checkauth {
     my $query = shift;
 	$debug and warn "Checking Auth";
@@ -571,6 +610,7 @@ sub checkauth {
     my $flagsrequired   = shift;
     my $type            = shift;
     $type = 'opac' unless $type;
+    my $new_session = 0;
 
     my $dbh     = C4::Context->dbh;
     my $timeout = _timeout_syspref();
@@ -579,17 +619,6 @@ sub checkauth {
         $timeout = $1 * 86400;
     };
     $timeout = 600 unless $timeout;
-    # check we have a Version. Otherwise => go to installer
-    unless ( C4::Context->preference('Version') ) {    # assignment, not comparison
-        if ( $type ne 'opac' ) {
-            $debug && warn "Install required, redirecting to Installer";
-            print $query->redirect("/cgi-bin/koha/installer/install.pl");
-        } else {
-            $debug && warn "OPAC Install required, redirecting to maintenance";
-            print $query->redirect("/cgi-bin/koha/maintenance.pl");
-        }
-        safe_exit;
-    }
 
     # state variables
     my $loggedin = 0;
@@ -694,6 +723,7 @@ sub checkauth {
         my $sessionID = $session->id;
         C4::Context->_new_userenv($sessionID);
         $cookie = $query->cookie( CGISESSID => $sessionID );
+
         $userid = $query->param('userid');
         if (   ( $cas && $query->param('ticket') )
             || $userid
@@ -708,6 +738,7 @@ sub checkauth {
                   checkpw( $dbh, $userid, $password, $query );
                 $userid = $retuserid;
                 $info{'invalidCasLogin'} = 1 unless ($return);
+                $new_session = 1;
             }
             elsif (
                 ( $pki_field eq 'Common Name' && $ENV{'SSL_CLIENT_S_DN_CN'} )
@@ -746,6 +777,7 @@ sub checkauth {
                 ( $return, $cardnumber, $retuserid ) =
                   checkpw( $dbh, $userid, $password, $query );
                 $userid = $retuserid if ( $retuserid ne '' );
+                $new_session = 1;
             }
 		if ($return) {
                #_session_log(sprintf "%20s from %16s logged in  at %30s.\n", $userid,$ENV{'REMOTE_ADDR'},(strftime '%c', localtime));
@@ -887,7 +919,7 @@ sub checkauth {
         unless ($cookie) {
             $cookie = $query->cookie( CGISESSID => '' );
         }
-        return ( $userid, $cookie, $sessionID, $flags );
+        return ( $userid, $cookie, $sessionID, $flags, $new_session );
     }
 
 #
diff --git a/mainpage.pl b/mainpage.pl
index 3e7dd3a..505c166 100755
--- a/mainpage.pl
+++ b/mainpage.pl
@@ -28,7 +28,6 @@ use C4::NewsChannels;
 use C4::Review qw/numberofreviews/;
 use C4::Suggestions qw/CountSuggestion/;
 use C4::Tags qw/get_count_by_tag_status/;
-use C4::Update::Database;
 
 my $query     = new CGI;
 
@@ -44,21 +43,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     }
 );
 
-#
-# check if you're uptodate, and if you're not, head to updater
-#
-my $koha36= C4::Context::KOHAVERSION;
-$koha36 =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
-
-if (C4::Context->preference('Version') < $koha36) {
-    print $query->redirect("/cgi-bin/koha/installer/install.pl?step=3");
-    exit;
-}
-unless ( C4::Update::Database::is_uptodate() ) {
-    # not uptodate, redirect to updatedatabase page
-    print $query->redirect("/cgi-bin/koha/admin/updatedatabase.pl");
-    exit;
-}
+C4::Auth::version_check($query, 'intranet', $cookie);
 
 my $all_koha_news   = &GetNewsToDisplay("koha");
 my $koha_news_count = scalar @$all_koha_news;
-- 
1.7.9.5