ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilHTTPS.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
13 class ilHTTPS
14 {
15  const PROTOCOL_HTTP = 1;
16  const PROTOCOL_HTTPS = 2;
17 
18  private static $instance = null;
19 
20  protected $enabled = false;
21 
22  protected $protected_classes = array();
23  protected $protected_scripts = array();
24 
26  protected $headerName = false;
27  protected $headerValue = false;
28 
33  function __construct()
34  {
36 
37  if($this->enabled = (bool)$ilSetting->get('https'))
38  {
39  $this->__readProtectedScripts();
40  $this->__readProtectedClasses();
41  }
42 
43  if ($this->automaticHTTPSDetectionEnabled = (bool)$ilIliasIniFile->readVariable('https', "auto_https_detect_enabled"))
44  {
45  $this->headerName = $ilIliasIniFile->readVariable('https', "auto_https_detect_header_name");
46  $this->headerValue = $ilIliasIniFile->readVariable('https', "auto_https_detect_header_value");
47  }
48  }
49 
54  public static function getInstance()
55  {
56  if(self::$instance)
57  {
58  return self::$instance;
59  }
60  return self::$instance = new ilHTTPS();
61  }
62 
67  protected function shouldSwitchProtocol($to_protocol)
68  {
69  switch($to_protocol)
70  {
71  case self::PROTOCOL_HTTP:
72  $should_switch_to_http = (
73  !in_array(basename($_SERVER['SCRIPT_NAME']), $this->protected_scripts) &&
74  !in_array(strtolower($_GET['cmdClass']), $this->protected_classes)
75  ) && $_SERVER['HTTPS'] == 'on';
76 
77  return $should_switch_to_http;
78  break;
79 
80  case self::PROTOCOL_HTTPS:
81  $should_switch_to_https = (
82  in_array(basename($_SERVER['SCRIPT_NAME']), $this->protected_scripts) ||
83  in_array(strtolower($_GET['cmdClass']), $this->protected_classes)
84  ) && $_SERVER['HTTPS'] != 'on';
85 
86  return $should_switch_to_https;
87  break;
88  }
89 
90  return false;
91  }
92 
98  function checkPort()
99  {
100  // if https is enabled for scripts or classes, check for redirection
101  if ($this->enabled)
102  {
103  if($this->shouldSwitchProtocol(self::PROTOCOL_HTTPS))
104  {
105  header("location: https://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
106  exit;
107  }
108  if($this->shouldSwitchProtocol(self::PROTOCOL_HTTP))
109  {
110  header("location: http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
111  exit;
112  }
113  }
114  return true;
115  }
116 
118  {
119  $this->protected_scripts[] = 'login.php';
120  $this->protected_scripts[] = 'index.php';
121  $this->protected_scripts[] = 'register.php';
122  // BEGIN WebDAV Use SSL for WebDAV.
123  $this->protected_scripts[] = 'webdav.php';
124  // END WebDAV Use SSL for WebDAV.
125  $this->protected_scripts[] = 'shib_login.php';
126 
127  return true;
128  }
129 
135  public function isDetected ()
136  {
137  if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on")
138  return true;
139 
140  if ($this->automaticHTTPSDetectionEnabled)
141  {
142  $headerName = "HTTP_".str_replace("-","_", strtoupper($this->headerName));
143  /* echo $headerName;
144  echo $_SERVER[$headerName];*/
145  if (strcasecmp($_SERVER[$headerName],$this->headerValue)==0)
146  {
147  $_SERVER["HTTPS"] = "on";
148  return true;
149  }
150  /*
151  if(isset($_SERVER[$this->headerName]) && (strcasecmp($_SERVER[$this->headerName],$this->headerValue) == 0))
152  {
153  $_SERVER['HTTPS'] = 'on';
154  return true;
155  }
156  */
157  }
158 
159  return false;
160  }
161 
163  {
164  $this->protected_classes[] = 'ilstartupgui';
165  $this->protected_classes[] = 'ilaccountregistrationgui';
166  $this->protected_classes[] = 'ilpersonalsettingsgui';
167  }
168 
174  public static function _checkHTTPS()
175  {
176  // only check standard port in the moment
177  $port = 443;
178 
179  if(($sp = fsockopen($_SERVER["SERVER_NAME"],$port,$errno,$error)) === false)
180  {
181  return false;
182  }
183  fclose($sp);
184  return true;
185  }
192  function _checkHTTP()
193  {
194  $port = 80;
195 
196  if(($sp = fsockopen($_SERVER["SERVER_NAME"],$port,$errno,$error)) === false)
197  {
198  return false;
199  }
200  fclose($sp);
201  return true;
202  }
203 
211  public function enableSecureCookies()
212  {
213  global $ilLog,$ilClientIniFile;
214 
215  $secure_disabled = $ilClientIniFile->readVariable('session','disable_secure_cookies');
216  if(!$secure_disabled and !$this->enabled and $this->isDetected() and !session_id())
217  {
218  #$ilLog->write(__CLASS__.': Enabled secure cookies');
219  session_set_cookie_params(
220  IL_COOKIE_EXPIRE, IL_COOKIE_PATH, IL_COOKIE_DOMAIN, true, IL_COOKIE_HTTPONLY
221  );
222  }
223  return true;
224  }
225 }
226 ?>
_checkHTTP()
static method to check if http connections are possible for this server
$automaticHTTPSDetectionEnabled
$error
Definition: Error.php:17
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
$_GET["client_id"]
__construct()
use ilHTTPS::getInstance()
static _checkHTTPS()
static method to check if https connections are possible for this server public
const PROTOCOL_HTTP
HTTPS.
const IL_COOKIE_PATH(isset($_GET["client_id"]))
Definition: index.php:26
Add a drawing to the header
Definition: 04printing.php:69
Create styles array
The data for the language used.
static getInstance()
Get https instance.
const PROTOCOL_HTTPS
__readProtectedScripts()
isDetected()
check if https is detected
__readProtectedClasses()
global $ilSetting
Definition: privfeed.php:17
enableSecureCookies()
enable secure cookies
static $instance
$ilIliasIniFile
shouldSwitchProtocol($to_protocol)
checkPort()
check if current port usage is right: if https should be used than redirection is done...