File:  [LON-CAPA] / loncom / auth / lonlinkexit.pm
Revision 1.2: download - view: text, annotated - select for diffs
Thu Jul 7 15:28:08 2022 UTC (21 months, 3 weeks ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_4_msu, HEAD
- Bug 6907
  Value of deeplink parameter in effect for session launched via deep-link
  determines if "Exit Tool" button is present in inline menu, the text used
  for the button and whether redirect to a URL can occur when pushed.

# The LearningOnline Network
# Re-launch guidance for deep linked access with username mismatch
#
# $Id: lonlinkexit.pm,v 1.2 2022/07/07 15:28:08 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::lonnavmaps;
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.course.id'}) { 
                my ($deeplink_symb,$deeplink);
                my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                if (($cnum ne '') && ($cdom ne '')) {
                    $deeplink_symb = &Apache::loncommon::deeplink_login_symb($cnum,$cdom);
                    if ($deeplink_symb) {
                        if ($deeplink_symb =~ /\.(page|sequence)$/) {
                            my $mapname = &Apache::lonnet::deversion((&Apache::lonnet::decode_symb($deeplink_symb))[2]);
                            my $navmap = Apache::lonnavmaps::navmap->new();
                            if (ref($navmap)) {
                                $deeplink = $navmap->get_mapparam(undef,$mapname,'0.deeplink');
                            }
                        } else {
                            $deeplink = &Apache::lonnet::EXT('resource.0.deeplink',$deeplink_symb);
                        }
                        if ($deeplink ne '') {
                            my ($state,$others,$listed,$scope,$protect,$display,$target,$exit) = split(/,/,$deeplink);
                            if ($exit) {
                                my ($show,$text) = split(/:/,$exit);
                                if ($show eq 'url') {
                                    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>