ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilHTTPS.php
Go to the documentation of this file.
1<?php
2
26{
27 protected const PROTOCOL_HTTP = 1;
28 protected const PROTOCOL_HTTPS = 2;
29 public const SETTINGS_GROUP_SERVER = 'server';
30 public const SETTING_HTTP_PATH = 'http_path';
31 public const SETTINGS_GROUP_HTTPS = 'https';
32 public const SETTING_AUTO_HTTPS_DETECT_ENABLED = "auto_https_detect_enabled";
33 public const SETTING_AUTO_HTTPS_DETECT_HEADER_NAME = "auto_https_detect_header_name";
34 public const SETTING_AUTO_HTTPS_DETECT_HEADER_VALUE = "auto_https_detect_header_value";
35 public const SETTING_FORCED = 'forced';
36 protected bool $enabled = false;
37 protected array $protected_classes = [];
38 protected array $protected_scripts = [];
39 protected bool $automatic_detection = false;
40 protected ?string $header_name = null;
41 protected ?string $header_value = null;
44
45 public function __construct()
46 {
47 global $DIC;
48 $this->ilias_ini = $DIC->iliasIni();
49 $this->client_ini = $DIC->clientIni();
50
51 if ($this->enabled = (bool) $this->ilias_ini->readVariable(
52 self::SETTINGS_GROUP_HTTPS,
53 self::SETTING_FORCED
54 )) {
55 $this->readProtectedScripts();
56 $this->readProtectedClasses();
57 }
58
59 if ($this->automatic_detection = (bool) $this->ilias_ini->readVariable(
60 self::SETTINGS_GROUP_HTTPS,
61 self::SETTING_AUTO_HTTPS_DETECT_ENABLED
62 )) {
63 $this->header_name = $this->ilias_ini->readVariable(
64 self::SETTINGS_GROUP_HTTPS,
65 self::SETTING_AUTO_HTTPS_DETECT_HEADER_NAME
66 );
67 $this->header_value = $this->ilias_ini->readVariable(
68 self::SETTINGS_GROUP_HTTPS,
69 self::SETTING_AUTO_HTTPS_DETECT_HEADER_VALUE
70 );
71 }
72 }
73
74 private function readProtectedScripts(): void
75 {
76 $this->protected_scripts[] = 'login.php';
77 $this->protected_scripts[] = 'index.php';
78 $this->protected_scripts[] = 'register.php';
79 $this->protected_scripts[] = 'webdav.php';
80 $this->protected_scripts[] = 'shib_login.php';
81 }
82
88 public function isDetected(): bool
89 {
90 if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] === "on") {
91 return true;
92 }
93
94 if ($this->automatic_detection) {
95 $header_name = "HTTP_" . str_replace("-", "_", strtoupper($this->header_name));
96 /* echo $header_name;
97 echo $_SERVER[$header_name];*/
98 if (isset($_SERVER[$header_name])) {
99 if (strcasecmp($_SERVER[$header_name], $this->header_value) === 0) {
100 $_SERVER["HTTPS"] = "on";
101 return true;
102 }
103 }
104 }
105
106 return false;
107 }
108
109 private function readProtectedClasses(): void
110 {
111 $this->protected_classes[] = 'ilstartupgui';
112 $this->protected_classes[] = 'ilaccountregistrationgui';
113 $this->protected_classes[] = 'ilpersonalsettingsgui';
114 }
115
116 public function checkHTTPS(int $port = 443): bool
117 {
118 if (($sp = fsockopen($_SERVER["SERVER_NAME"], $port, $errno, $error)) === false) {
119 return false;
120 }
121 fclose($sp);
122 return true;
123 }
124
125 public function enableSecureCookies(): void
126 {
127 $secure_disabled = (bool) $this->client_ini->readVariable('session', 'disable_secure_cookies');
128 if (!$secure_disabled && !$this->enabled && $this->isDetected() && !session_id()) {
129 if (!defined('IL_COOKIE_SECURE')) {
130 define('IL_COOKIE_SECURE', true);
131 }
132
133 session_set_cookie_params([
134 'lifetime' => IL_COOKIE_EXPIRE,
135 'path' => IL_COOKIE_PATH,
136 'domain' => IL_COOKIE_DOMAIN,
137 'secure' => IL_COOKIE_SECURE,
138 'httponly' => true,
139 'samesite' => (strtolower(session_get_cookie_params()['samesite'] ?? '')) === 'strict' ? session_get_cookie_params()['samesite'] : 'Lax'
140 ]);
141 }
142 }
143
144 public function checkProtocolAndRedirectIfNeeded(): bool
145 {
146 // if https is enabled for scripts or classes, check for redirection
147 if ($this->enabled) {
148 if ($this->shouldSwitchProtocol(self::PROTOCOL_HTTPS)) {
149 header("location: https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]);
150 exit;
151 }
152 if ($this->shouldSwitchProtocol(self::PROTOCOL_HTTP)) {
153 header("location: http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]);
154 exit;
155 }
156 }
157 return true;
158 }
159
160 private function shouldSwitchProtocol($to_protocol): bool
161 {
162 switch ($to_protocol) {
164 return (
165 !in_array(basename($_SERVER['SCRIPT_NAME']), $this->protected_scripts) &&
166 !in_array(strtolower($_GET['cmdClass']), $this->protected_classes)
167 ) && $_SERVER['HTTPS'] === 'on';
168
170 return (
171 in_array(basename($_SERVER['SCRIPT_NAME']), $this->protected_scripts) ||
172 in_array(strtolower($_GET['cmdClass']), $this->protected_classes)
173 ) && $_SERVER['HTTPS'] !== 'on';
174 }
175
176 return false;
177 }
178}
const IL_COOKIE_PATH(isset($_GET['client_id']))
Definition: index.php:47
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilIniFile $ilias_ini
const SETTINGS_GROUP_SERVER
const SETTING_HTTP_PATH
string $header_name
bool $automatic_detection
const SETTINGS_GROUP_HTTPS
bool $enabled
checkProtocolAndRedirectIfNeeded()
checkHTTPS(int $port=443)
readProtectedScripts()
string $header_value
isDetected()
check if https is detected
const SETTING_AUTO_HTTPS_DETECT_HEADER_VALUE
const SETTING_AUTO_HTTPS_DETECT_ENABLED
ilIniFile $client_ini
const SETTING_AUTO_HTTPS_DETECT_HEADER_NAME
enableSecureCookies()
shouldSwitchProtocol($to_protocol)
const PROTOCOL_HTTPS
array $protected_scripts
const SETTING_FORCED
const PROTOCOL_HTTP
array $protected_classes
readProtectedClasses()
INIFile Parser Early access in init proceess! Avoid further dependencies like logging or other servic...
exit
$_GET['cmd']
Definition: lti.php:26
$_SERVER['HTTP_HOST']
Definition: raiseError.php:26
global $DIC
Definition: shib_login.php:26