ILIAS  trunk Revision v12.0_alpha-1221-g4e438232683
shib_logout.php
Go to the documentation of this file.
1<?php
2
19require_once("../vendor/composer/vendor/autoload.php");
21require_once("../artifacts/bootstrap_default.php");
22
23global $DIC;
24
25$q = $DIC->http()->wrapper()->query();
26if (
27 $q->has('return')
28 && $q->has('action')
29 && $q->retrieve('action', $DIC->refinery()->to()->string()) === 'logout'
30) {
31 entry_point("ILIAS Legacy Initialisation Adapter");
32 // Logout out user from application
33 // Destroy application session/cookie etc
34 $GLOBALS['DIC']['ilAuthSession']->logout();
35
36 // Finally, send user to the return URL
37 ilUtil::redirect($q->retrieve('action', $DIC->refinery()->kindlyTo()->string()));
38}
39
40// Back channel logout //
41
42// Note: This is the preferred logout channel because it also allows
43// administrative logout. However, it requires your application to be
44// adapated in the sense that the user's Shibboleth session ID must be
45// stored in the application's session data.
46// See function LogoutNotification below
47
48elseif (!empty(file_get_contents('php://input'))) {
50
51 // Load ILIAS libraries and initialise ILIAS in non-web context
52 entry_point("ILIAS Legacy Initialisation Adapter");
53
54 // Set SOAP header
55 $server = new SoapServer('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '/LogoutNotification.wsdl');
56 $server->addFunction("LogoutNotification");
57 $server->handle();
58}
59
60// Return WSDL
61
62// Note: This is needed for the PHP SoapServer class.
63// Since I'm not a web service guru it might be that the code below is not
64// absolutely correct but at least it seems to to its job properly when it
65// comes to Shibboleth logout
66
67else {
68 header('Content-Type: text/xml');
69
70 $url = filter_var("https://{$_SERVER['HTTP_HOST']}/shib_logout.php", FILTER_SANITIZE_URL);
71
72 echo <<<WSDL
73<?xml version ="1.0" encoding ="UTF-8" ?>
74<definitions name="LogoutNotification" targetNamespace="urn:mace:shibboleth:2.0:sp:notify"
75 xmlns:notify="urn:mace:shibboleth:2.0:sp:notify"
76 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
77 xmlns="http://schemas.xmlsoap.org/wsdl/">
78
79 <types>
80 <schema targetNamespace="urn:mace:shibboleth:2.0:sp:notify"
81 xmlns="http://www.w3.org/2000/10/XMLSchema"
82 xmlns:notify="urn:mace:shibboleth:2.0:sp:notify">
83
84 <simpleType name="string">
85 <restriction base="string">
86 <minLength value="1"/>
87 </restriction>
88 </simpleType>
89
90 <element name="OK" type="notify:OKType"/>
91 <complexType name="OKType">
92 <sequence/>
93 </complexType>
94
95 </schema>
96 </types>
97
98 <message name="getLogoutNotificationRequest">
99 <part name="SessionID" type="notify:string" />
100 </message>
101
102 <message name="getLogoutNotificationResponse">
103 <part name="OK"/>
104 </message>
105
106 <portType name="LogoutNotificationPortType">
107 <operation name="LogoutNotification">
108 <input message="getLogoutNotificationRequest"/>
109 <output message="getLogoutNotificationResponse"/>
110 </operation>
111 </portType>
112
113 <binding name="LogoutNotificationBinding" type="notify:LogoutNotificationPortType">
114 <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
115 <operation name="LogoutNotification">
116 <soap:operation soapAction="urn:xmethods-logout-notification#LogoutNotification"/>
117 </operation>
118 </binding>
119
120 <service name="LogoutNotificationService">
121 <port name="LogoutNotificationPort" binding="notify:LogoutNotificationBinding">
122 <soap:address location="{$url}"/>
123 </port>
124 </service>
125</definitions>
126WSDL;
127 exit;
128}
129
130/******************************************************************************/
132function LogoutNotification($SessionID): ?\SoapFault
133{
134 // Delete session of user using $SessionID to locate the user's session file
135 // on the file system or in the database
136 // Then delete this entry or record to clear the session
137 // However, for that to work it is essential that the user's Shibboleth
138 // SessionID is stored in the user session data!
139
140 global $ilDB;
141
142 $q = "SELECT session_id, data FROM usr_session WHERE expires > 'NOW()'";
143 $r = $ilDB->query($q);
144
145 while ($session = $r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
146 $session_data = unserializesession($session['data']);
147 // Delete this session entry
148 if (
149 is_array($session_data)
150 && array_key_exists('shibboleth_session_id', $session_data)
151 && $session_data['shibboleth_session_id'] === $SessionID
152 && !ilSession::_destroy($session['session_id']
153 )
154 ) {
155 return new SoapFault('LogoutError', 'Could not delete session entry in database.');
156 }
157 }
158 // If no SoapFault is returned, all is fine
159 return null;
160}
161
162/******************************************************************************/
163// Deserializes session data and returns it in a hash array of arrays
167function unserializesession($serialized_string): array
168{
169 $variables = [];
170 $a = preg_split("/(\w+)\|/", (string) $serialized_string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
171 $counter = count($a);
172 for ($i = 0; $i < $counter; $i += 2) {
173 $variables[$a[$i]] = unserialize($a[$i + 1]);
174 }
175
176 return ($variables);
177}
const CONTEXT_SHIBBOLETH
Definition: ilContext.php:45
static init(string $a_type)
Init context by type.
Definition: ilContext.php:52
const CONTEXT_SOAP
Definition: ilContext.php:34
const FETCHMODE_ASSOC
static _destroy($a_session_id, ?int $a_closing_context=null, $a_expired_at=null)
Destroy session.
static redirect(string $a_script)
exit
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$_SERVER['HTTP_HOST']
Definition: raiseError.php:26
entry_point(string $name)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: result1.php:21
$server
Definition: shib_login.php:28
global $DIC
Definition: shib_logout.php:23
PREG_SPLIT_NO_EMPTY PREG_SPLIT_DELIM_CAPTURE
$q
Definition: shib_logout.php:25
$url
Definition: shib_logout.php:70
$counter
$GLOBALS["DIC"]
Definition: wac.php:54