File:  [LON-CAPA] / loncom / auth / lonlinkexit.pm
Revision 1.1: download - view: text, annotated - select for diffs
Thu Jun 30 21:04:13 2022 UTC (22 months, 1 week ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6907
  "Exit Tool" button available to logout a session launched via deep link
  and escape iframe and redirect (for LTI-protected link).

# The LearningOnline Network
# Re-launch guidance for deep linked access with username mismatch
#
# $Id: lonlinkexit.pm,v 1.1 2022/06/30 21:04:13 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#

package Apache::lonlinkexit;

use strict;
use lib '/home/httpd/lib/perl/';
use Apache::Constants qw(:common);
use Apache::lonnet;
use Apache::loncommon;
use Apache::lonlocal;
use LONCAPA;
use CGI::Cookie();

sub handler {
    my $r = shift;

    my $handle = &Apache::lonnet::check_for_valid_session($r);
    my ($exiturl,$deeplinktarget);
    if ($handle ne '') {
        my $lonidsdir=$r->dir_config('lonIDsDir');
        &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
        if ($env{'request.deeplink.login'}) {
            if ($env{'request.deeplink.target'} ne '') {
                $deeplinktarget = $env{'request.deeplink.target'};
            }
            if ($env{'request.linkprotexit'} =~ m{^https?://}) {
                $exiturl = $env{'request.linkprotexit'};
                &js_escape(\$exiturl);
            }
        }
        if (unlink("$lonidsdir/$handle.id")) {
            if (($env{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&
                (-l "$lonidsdir/$env{'user.linkedenv'}.id") &&
                (readlink("$lonidsdir/$env{'user.linkedenv'}.id") eq "$lonidsdir/$handle.id")) {
                unlink("$lonidsdir/$env{'user.linkedenv'}.id");
            }
        }
        my %temp=('logout' => time);
        my $ip = &Apache::lonnet::get_requestor_ip();
        &Apache::lonnet::put('email_status',\%temp);
        &Apache::lonnet::log($env{'user.domain'},
                             $env{'user.name'},
                             $env{'user.home'},
                             "Logout $ip");
        #expire the cookies
        my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
        foreach my $name (keys(%cookies)) {
            next unless ($name =~ /^lon(|S|Link|Pub)ID$/);
            my $c = new CGI::Cookie(-name    => $name,
                                    -value   => '',
                                    -expires => '-10y',);
            $r->headers_out->add('Set-cookie' => $c);
        }
    }
    if (!$Apache::lonlocal::lh) {
        &Apache::lonlocal::get_language_handle($r);
    }
    &Apache::loncommon::content_type($r,'text/html');
    $r->send_http_header;
    return OK if $r->header_only;

    my ($msg,$js);
    $msg = '<p>'.&mt('Expired any existing session').'</p>';
    my $args = {'only_body' => 1};
    if ($exiturl) {
        $js = <<ENDJS;
<script type="text/javascript">
// <![CDATA[
\$(document).ready( function() {
    setTimeout(function() {
        if (window.self !== window.top) {
            window.top.location.href = '$exiturl';
        } else {
            document.location.href = '$exiturl';
        }
    },100);
});
// ]]>
</script>
ENDJS
        $msg .= '<p>'.&mt('Redirecting ...').'</p>';
    }

    $r->print(&Apache::loncommon::start_page('Session removed',$js,{'only_body' => 1}));
    $r->print($msg);
    $r->print(&Apache::loncommon::end_page());
    return OK;
}

1;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>