|
Line 0
Link Here
|
|
|
1 |
package Koha::Session; |
| 2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but |
| 11 |
# 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 Koha; if not, see <http://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
use CGI::Session; |
| 20 |
|
| 21 |
use C4::Context; |
| 22 |
use Koha::Caches; |
| 23 |
|
| 24 |
sub _get_session_params { |
| 25 |
my $class = shift; |
| 26 |
my $storage_method = C4::Context->preference('SessionStorage'); |
| 27 |
if ( $storage_method eq 'mysql' ) { |
| 28 |
my $dbh = C4::Context->dbh; |
| 29 |
return { dsn => "serializer:yamlxs;driver:MySQL;id:md5", dsn_args => { Handle => $dbh } }; |
| 30 |
} elsif ( $storage_method eq 'Pg' ) { |
| 31 |
my $dbh = C4::Context->dbh; |
| 32 |
return { dsn => "serializer:yamlxs;driver:PostgreSQL;id:md5", dsn_args => { Handle => $dbh } }; |
| 33 |
} elsif ( $storage_method eq 'memcached' && Koha::Caches->get_instance->memcached_cache ) { |
| 34 |
my $memcached = Koha::Caches->get_instance()->memcached_cache; |
| 35 |
return { dsn => "serializer:yamlxs;driver:memcached;id:md5", dsn_args => { Memcached => $memcached } }; |
| 36 |
} else { |
| 37 |
|
| 38 |
# catch all defaults to tmp should work on all systems |
| 39 |
my $dir = C4::Context::temporary_directory; |
| 40 |
my $instance = C4::Context->config('database') |
| 41 |
; #actually for packages not exactly the instance name, but generally safer to leave it as it is |
| 42 |
return { dsn => "serializer:yamlxs;driver:File;id:md5", dsn_args => { Directory => "$dir/cgisess_$instance" } }; |
| 43 |
} |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
sub get_session { |
| 48 |
my ( $class, $args ) = @_; |
| 49 |
my $sessionID = $args->{sessionID}; |
| 50 |
my $params = $class->_get_session_params(); |
| 51 |
my $session; |
| 52 |
if ($sessionID) { # find existing |
| 53 |
CGI::Session::ErrorHandler->set_error(q{}); # clear error, cpan issue #111463 |
| 54 |
$session = CGI::Session->load( $params->{dsn}, $sessionID, $params->{dsn_args} ); |
| 55 |
} else { |
| 56 |
$session = CGI::Session->new( $params->{dsn}, $sessionID, $params->{dsn_args} ); |
| 57 |
|
| 58 |
# no need to flush here |
| 59 |
} |
| 60 |
return $session; |
| 61 |
} |
| 62 |
|
| 63 |
1; |