ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  private static $instance = null;
16 
17  var $enabled = false;
18  var $protected_scripts = array();
19 
21  var $headerName = false;
22  var $headerValue = false;
23 
28  function ilHTTPS()
29  {
30  global $ilSetting;
31 
32  if($this->enabled = (bool) $ilSetting->get('https'))
33  {
34  $this->__readProtectedScripts();
35  $this->__readProtectedClasses();
36  }
37  if ($this->automaticHTTPSDetectionEnabled = (bool) $ilSetting->get("ps_auto_https_enabled"))
38  {
39  $this->headerName = $ilSetting->get("ps_auto_https_headername");
40  $this->headerValue = $ilSetting->get("ps_auto_https_headervalue");
41  }
42  }
43 
48  public static function getInstance()
49  {
50  if(self::$instance)
51  {
52  return self::$instance;
53  }
54  return self::$instance = new ilHTTPS();
55  }
56 
62  function checkPort()
63  {
64  // if https is enabled for scripts or classes, check for redirection
65  if ($this->enabled)
66  {
67  if((in_array(basename($_SERVER["SCRIPT_NAME"]),$this->protected_scripts) or
68  in_array($_GET['cmdClass'],$this->protected_classes)) and
69  $_SERVER["HTTPS"] != "on")
70  {
71  header("location: https://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
72  exit;
73  }
74  if((!in_array(basename($_SERVER["SCRIPT_NAME"]),$this->protected_scripts) and
75  !in_array($_GET['cmdClass'],$this->protected_classes)) and
76  $_SERVER["HTTPS"] == "on")
77  {
78  header("location: http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
79  exit;
80  }
81  }
82  return true;
83  }
84 
86  {
87  $this->protected_scripts[] = 'login.php';
88  $this->protected_scripts[] = 'index.php';
89  $this->protected_scripts[] = 'payment.php';
90  $this->protected_scripts[] = 'register.php';
91  // BEGIN WebDAV Use SSL for WebDAV.
92  $this->protected_scripts[] = 'webdav.php';
93  // END WebDAV Use SSL for WebDAV.
94  $this->protected_scripts[] = 'shib_login.php';
95 
96  return true;
97  }
98 
104  public function isDetected ()
105  {
106  if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on")
107  return true;
108 
109  if ($this->automaticHTTPSDetectionEnabled)
110  {
111  $headerName = "HTTP_".str_replace("-","_",$this->headerName);
112  /* echo $headerName;
113  echo $_SERVER[$headerName];*/
114  if (strcasecmp($_SERVER[$headerName],$this->headerValue)==0)
115  {
116  $_SERVER["HTTPS"] = "on";
117  return true;
118  }
119  /*
120  if(isset($_SERVER[$this->headerName]) && (strcasecmp($_SERVER[$this->headerName],$this->headerValue) == 0))
121  {
122  $_SERVER['HTTPS'] = 'on';
123  return true;
124  }
125  */
126  }
127 
128  return false;
129  }
130 
132  {
133  $this->protected_classes[] = 'ilstartupgui';
134  $this->protected_classes[] = 'ilaccountregistrationgui';
135  $this->protected_classes[] = 'ilpurchasebmfgui';
136  $this->protected_classes[] = 'ilpurchasepaypal';
137  $this->protected_classes[] = 'ilshopshoppingcartgui';
138  $this->protected_classes[] = 'ilpurchasebillgui';
139  }
140 
146  function _checkHTTPS()
147  {
148  // only check standard port in the moment
149  $port = 443;
150 
151  if(($sp = fsockopen($_SERVER["SERVER_NAME"],$port,$errno,$error)) === false)
152  {
153  return false;
154  }
155  fclose($sp);
156  return true;
157  }
164  function _checkHTTP()
165  {
166  $port = 80;
167 
168  if(($sp = fsockopen($_SERVER["SERVER_NAME"],$port,$errno,$error)) === false)
169  {
170  return false;
171  }
172  fclose($sp);
173  return true;
174  }
175 
183  public function enableSecureCookies()
184  {
185  global $ilLog,$ilClientIniFile;
186 
187  $secure_disabled = $ilClientIniFile->readVariable('session','disable_secure_cookies');
188  if(!$secure_disabled and !$this->enabled and $this->isDetected() and !session_id())
189  {
190  $ilLog->write(__CLASS__.': Enabled secure cookies');
191 
192  // session_set_cookie_params() supports 5th parameter
193  // only for php version 5.2.0 and above
194  if( version_compare(PHP_VERSION, '5.2.0', '>=') )
195  {
196  // PHP version >= 5.2.0
197  session_set_cookie_params(
198  IL_COOKIE_EXPIRE, IL_COOKIE_PATH, IL_COOKIE_DOMAIN, true, IL_COOKIE_HTTPONLY
199  );
200  }
201  else
202  {
203  // PHP version < 5.2.0
204  session_set_cookie_params(
205  IL_COOKIE_EXPIRE, IL_COOKIE_PATH, IL_COOKIE_DOMAIN, true
206  );
207  }
208  }
209  return true;
210  }
211 }
212 ?>