BugFixing : viewlog.pl was broken (comma missing)
[koha_fer] / C4 / Auth.pm
old mode 100644 (file)
new mode 100755 (executable)
index 203388b..6d792ea
@@ -32,6 +32,7 @@ use C4::Members;
 use C4::Koha;
 use C4::Branch; # GetBranches
 
+# use utf8;
 # use Net::LDAP;
 # use Net::LDAP qw(:all);
 
@@ -58,10 +59,10 @@ C4::Auth - Authenticates Koha users
         {
             template_name   => "opac-main.tmpl",
             query           => $query,
-           type            => "opac",
-           authnotrequired => 1,
-           flagsrequired   => {borrow => 1},
-       }
+      type            => "opac",
+      authnotrequired => 1,
+      flagsrequired   => {borrow => 1},
+  }
     );
 
   print $query->header(
@@ -96,10 +97,10 @@ C4::Auth - Authenticates Koha users
         {
            template_name   => "opac-main.tmpl",
            query           => $query,
-          type            => "opac",
-          authnotrequired => 1,
-          flagsrequired   => {borrow => 1},
-       }
+     type            => "opac",
+     authnotrequired => 1,
+     flagsrequired   => {borrow => 1},
+  }
     );
 
     This call passes the C<query>, C<flagsrequired> and C<authnotrequired>
@@ -132,6 +133,19 @@ sub get_template_and_user {
     my $borrowernumber;
     my $insecure = C4::Context->preference('insecure');
     if ($user or $insecure) {
+
+               # load the template variables for stylesheets and JavaScript
+               $template->param( css_libs => $in->{'css_libs'} );
+               $template->param( css_module => $in->{'css_module'} );
+               $template->param( css_page => $in->{'css_page'} );
+               $template->param( css_widgets => $in->{'css_widgets'} );
+
+        $template->param( js_libs => $in->{'js_libs'} );
+        $template->param( js_module => $in->{'js_module'} );
+        $template->param( js_page => $in->{'js_page'} );
+        $template->param( js_widgets => $in->{'js_widgets'} );
+
+               # user info
         $template->param( loggedinusername => $user );
         $template->param( sessionID        => $sessionID );
 
@@ -157,7 +171,7 @@ sub get_template_and_user {
             $template->param( CAN_user_updatecharge     => 1 );
             $template->param( CAN_user_acquisition      => 1 );
             $template->param( CAN_user_management       => 1 );
-            $template->param( CAN_user_tools            => 1 );        
+            $template->param( CAN_user_tools            => 1 ); 
             $template->param( CAN_user_editauthorities  => 1 );
             $template->param( CAN_user_serials          => 1 );
             $template->param( CAN_user_reports          => 1 );
@@ -207,11 +221,11 @@ sub get_template_and_user {
         if ( $flags && $flags->{tools} == 1 ) {
             $template->param( CAN_user_tools => 1 );
         }
-       
+  
         if ( $flags && $flags->{editauthorities} == 1 ) {
             $template->param( CAN_user_editauthorities => 1 );
         }
-               
+    
         if ( $flags && $flags->{serials} == 1 ) {
             $template->param( CAN_user_serials => 1 );
         }
@@ -231,6 +245,8 @@ sub get_template_and_user {
             LibraryName        => C4::Context->preference("LibraryName"),
             LoginBranchcode    => (C4::Context->userenv?C4::Context->userenv->{"branch"}:"insecure"),
             LoginBranchname    => (C4::Context->userenv?C4::Context->userenv->{"branchname"}:"insecure"),
+            LoginFirstname     => (C4::Context->userenv?C4::Context->userenv->{"firstname"}:"Bel"),
+            LoginSurname       => C4::Context->userenv?C4::Context->userenv->{"surname"}:"Inconnu", 
             AutoLocation       => C4::Context->preference("AutoLocation"),
             hide_marc          => C4::Context->preference("hide_marc"),
             patronimages       => C4::Context->preference("patronimages"),
@@ -240,15 +256,15 @@ sub get_template_and_user {
             virtualshelves          => C4::Context->preference("virtualshelves"),
             LibraryName             => C4::Context->preference("LibraryName"),
             KohaAdminEmailAddress   => "" . C4::Context->preference("KohaAdminEmailAddress"),
+                       IntranetmainUserblock   => C4::Context->preference("IntranetmainUserblock"),
         );
     }
     else {
-        warn "template type should be OPAC, here it is=[" . $in->{'type'} . "]"
-          unless ( $in->{'type'} eq 'opac' );
+               warn "template type should be OPAC, here it is=[" . $in->{'type'} . "]" unless ( $in->{'type'} eq 'opac' );
         my $LibraryNameTitle = C4::Context->preference("LibraryName");
         $LibraryNameTitle =~ s/<(?:\/?)(?:br|p)\s*(?:\/?)>/ /sgi;
         $LibraryNameTitle =~ s/<(?:[^<>'"]|'(?:[^']*)'|"(?:[^"]*)")*>//sg;
-       $template->param(
+  $template->param(
             KohaAdminEmailAddress  => "" . C4::Context->preference("KohaAdminEmailAddress"),
             suggestion             => "" . C4::Context->preference("suggestion"),
             virtualshelves         => "" . C4::Context->preference("virtualshelves"),
@@ -268,6 +284,8 @@ sub get_template_and_user {
             LibraryNameTitle       => "" . $LibraryNameTitle,
             LoginBranchcode        => (C4::Context->userenv?C4::Context->userenv->{"branch"}:"insecure"),
             LoginBranchname        => C4::Context->userenv?C4::Context->userenv->{"branchname"}:"", 
+            LoginFirstname        => (C4::Context->userenv?C4::Context->userenv->{"firstname"}:"Bel"),
+            LoginSurname        => C4::Context->userenv?C4::Context->userenv->{"surname"}:"Inconnu", 
             OpacPasswordChange     => C4::Context->preference("OpacPasswordChange"),
             opacreadinghistory     => C4::Context->preference("opacreadinghistory"),
             opacuserjs             => C4::Context->preference("opacuserjs"),
@@ -345,7 +363,7 @@ has authenticated.
 
 sub checkauth {
     my $query = shift;
-       # warn "Checking Auth";
+  # warn "Checking Auth";
     # $authnotrequired will be set for scripts which will run without authentication
     my $authnotrequired = shift;
     my $flagsrequired   = shift;
@@ -356,6 +374,22 @@ sub checkauth {
     my $timeout = C4::Context->preference('timeout');
     $timeout = 600 unless $timeout;
 
+
+    # If Version syspref is unavailable, it means Koha is beeing installed,
+    # and so we must redirect to OPAC maintenance page or to the WebInstaller
+    unless (C4::Context->preference('Version')) {
+      if ($type ne 'opac') {
+        warn "Install required, redirecting to Installer";
+        print $query->redirect("/cgi-bin/koha/installer/install.pl");
+      } 
+      else {
+        warn "OPAC Install required, redirecting to maintenance";
+        print $query->redirect("/cgi-bin/koha/maintenance.pl");
+      }
+      exit;
+    }
+
+
     # state variables
     my $loggedin = 0;
     my %info;
@@ -371,7 +405,15 @@ sub checkauth {
         $loggedin = 1;
     }
     elsif ( $sessionID = $query->cookie("CGISESSID")) {
-        my $session = new CGI::Session("driver:MySQL", $sessionID, {Handle=>$dbh});
+               my $storage_method = C4::Context->preference('SessionStorage');
+               my $session;
+               if ($storage_method eq 'mysql'){
+                   $session = new CGI::Session("driver:MySQL", $sessionID, {Handle=>$dbh});
+               }
+               else {
+                       # catch all defaults to tmp should work on all systems
+                       $session = new CGI::Session("driver:File", $sessionID, {Directory=>'/tmp'});                    
+               }
         C4::Context->_new_userenv($sessionID);
         if ($session){
             C4::Context::set_userenv(
@@ -381,20 +423,24 @@ sub checkauth {
                 $session->param('branchname'),   $session->param('flags'),
                 $session->param('emailaddress'), $session->param('branchprinter')
             );
+#             warn       "".$session->param('cardnumber').",   ".$session->param('firstname').",
+#                 ".$session->param('surname').",      ".$session->param('branch');
         }
         my $ip;
-               my $lasttime;
-               if ($session) {
-                       $ip = $session->param('ip');
-                       $lasttime = $session->param('lasttime');
-            $userid = $session->param('id');
-               }
+        my $lasttime;
+        if ($session) {
+          $ip = $session->param('ip');
+          $lasttime = $session->param('lasttime');
+                $userid = $session->param('id');
+        }
         
-               
+    
         if ($logout) {
 
             # voluntary logout the user
-            $session->delete;
+
+            $session->flush;      
+                       $session->delete();
             C4::Context->_unset_userenv($sessionID);
             $sessionID = undef;
             $userid    = undef;
@@ -423,7 +469,7 @@ sub checkauth {
                 $info{'oldip'}        = $ip;
                 $info{'newip'}        = $ENV{'REMOTE_ADDR'};
                 $info{'different_ip'} = 1;
-                               $session->delete();
+        $session->delete();
                 C4::Context->_unset_userenv($sessionID);
                 $sessionID = undef;
                 $userid    = undef;
@@ -448,11 +494,20 @@ sub checkauth {
         }
     }
     unless ($userid) {
-        my $session = new CGI::Session("driver:MySQL", undef, {Handle=>$dbh});         
+               my $storage_method = C4::Context->preference('SessionStorage');
+               my $session;
+               if ($storage_method eq 'mysql'){
+                   $session = new CGI::Session("driver:MySQL", $sessionID, {Handle=>$dbh});
+               }
+               else {
+                       # catch all defaults to tmp should work on all systems
+                       $session = new CGI::Session("driver:File", $sessionID, {Directory=>'/tmp'});                    
+               }
+
         my $sessionID;
                if ($session) {
                        $sessionID = $session->id;
-                       }
+               }
         $userid    = $query->param('userid');
         C4::Context->_new_userenv($sessionID);
         my $password = $query->param('password');
@@ -491,7 +546,7 @@ sub checkauth {
                   = $sth->fetchrow
                   if ( $sth->rows );
 
-#                              warn "$cardnumber,$borrowernumber,$userid,$firstname,$surname,$userflags,$branchcode,$emailaddress";
+#         warn "$cardnumber,$borrowernumber,$userid,$firstname,$surname,$userflags,$branchcode,$emailaddress";
                 unless ( $sth->rows ) {
                     my $sth =
                       $dbh->prepare(
@@ -506,7 +561,7 @@ sub checkauth {
                       = $sth->fetchrow
                       if ( $sth->rows );
 
-#                                      warn "$cardnumber,$borrowernumber,$userid,$firstname,$surname,$userflags,$branchcode,$emailaddress";
+#           warn "$cardnumber,$borrowernumber,$userid,$firstname,$surname,$userflags,$branchcode,$emailaddress";
                     unless ( $sth->rows ) {
                         $sth->execute($userid);
                         (
@@ -528,7 +583,7 @@ sub checkauth {
                 my $branches = GetBranches();
                 my @branchesloop;
                 foreach my $br ( keys %$branches ) {
-                    #          now we work with the treatment of ip
+                    #     now we work with the treatment of ip
                     my $domain = $branches->{$br}->{'branchip'};
                     if ( $domain && $ip =~ /^$domain/ ) {
                         $branchcode = $branches->{$br}->{'branchcode'};
@@ -549,6 +604,8 @@ sub checkauth {
                 $session->param('emailaddress',$emailaddress);
                 $session->param('ip',$session->remote_addr());
                 $session->param('lasttime',time());
+#            warn       "".$session->param('cardnumber').",   ".$session->param('firstname').",
+#                 ".$session->param('surname').",      ".$session->param('branch');
             }
             elsif ( $return == 2 ) {
                 #We suppose the user is the superlibrarian
@@ -556,7 +613,7 @@ sub checkauth {
                         $session->param('id',C4::Context->config('user'));
                         $session->param('cardnumber',C4::Context->config('user'));
                         $session->param('firstname',C4::Context->config('user'));
-                        $session->param('surname',C4::Context->config('user'),);
+                        $session->param('surname',C4::Context->config('user'));
                         $session->param('branch','NO_LIBRARY_SET');
                         $session->param('branchname','NO_LIBRARY_SET');
                         $session->param('flags',1);
@@ -592,7 +649,7 @@ sub checkauth {
             $cookie = $query->cookie( CGISESSID => ''
             );
         }
-               return ( $userid, $cookie, $sessionID, $flags );
+    return ( $userid, $cookie, $sessionID, $flags );
 
     }
 
@@ -619,17 +676,6 @@ sub checkauth {
     # check that database and koha version are the same
     # there is no DB version, it's a fresh install,
     # go to web installer
-       warn "about to check version";
-    unless (C4::Context->preference('Version')){
-      if ($type ne 'opac'){
-        warn "Install required, redirecting to Installer";
-        print $query->redirect("/cgi-bin/koha/installer/install.pl");
-      } else {
-        warn "OPAC Install required, redirecting to maintenance";
-        print $query->redirect("/cgi-bin/koha/maintenance.pl");
-      }       
-      exit;
-    }
     # there is a DB version, compare it to the code version
     my $kohaversion=C4::Context::KOHAVERSION;
     # remove the 3 last . to have a Perl number
@@ -655,7 +701,7 @@ sub checkauth {
     my $template = gettemplate( $template_name, $type, $query );
     $template->param(branchloop => \@branch_loop,);
     $template->param(
-               login                            => 1,
+    login        => 1,
         INPUTS               => \@inputs,
         suggestion           => C4::Context->preference("suggestion"),
         virtualshelves       => C4::Context->preference("virtualshelves"),
@@ -689,7 +735,8 @@ sub checkauth {
 #    $cookie = $query->cookie(CGISESSID => $session->id
 #   );
     print $query->header(
-        -type   => 'utf-8',
+               -type   => 'text/html',
+        -charset => 'utf-8',
         -cookie => $cookie
       ),
       $template->output;
@@ -738,7 +785,7 @@ sub checkpw {
     {
 
 # Koha superuser account
-#              C4::Context->set_userenv(0,0,C4::Context->config('user'),C4::Context->config('user'),C4::Context->config('user'),"",1);
+#     C4::Context->set_userenv(0,0,C4::Context->config('user'),C4::Context->config('user'),C4::Context->config('user'),"",1);
         return 2;
     }
     if (   $userid && $userid eq 'demo'