• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

setup/classes/class.Session.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00024 // Datei:       class.Session.inc
00025 // Benoetigt:    mind. 4.0.1pl2
00026 
00033 class Session {
00034     var $version     = 106;     // V1.06
00035     var $usesCookies = false;   // Client nimmt Cookies an
00036     var $transSID    = false;   // Wurde mit --enable-trans-sid
00037                                 // kompiliert  
00038 
00039 //---------------------------------------------------------
00040 
00045     function Session($sessionName="SESSID") {
00046         $this->sendNoCacheHeader();
00047         
00048         // force 4 hash bits per character for session_id       // Sascha Hofmann (2005-10-19)
00049                 ini_set("session.hash_bits_per_character","4");
00050 
00051         //  Session-Namen setzen, Session initialisieren   
00052         session_name(isset($sessionName)
00053             ? $sessionName
00054             : session_name());
00055 
00056         @session_start();
00057         
00058         //  Prüen ob die Session-ID die Standardlänge
00059         //  von 32 Zeichen hat,
00060         //  ansonsten Session-ID neu setzen 
00061         if (strlen(session_id()) != 32)
00062             {
00063                 mt_srand ((double)microtime()*1000000);
00064                 session_id(md5(uniqid(mt_rand())));
00065             }
00066         
00067         //  Prüfen, ob eine Session-ID übergeben wurde
00068         //  (über Cookie, POST oder GET)
00069         $IDpassed = false;
00070         if  (   isset($_COOKIE[session_name()]) &&
00071                 @strlen($_COOKIE[session_name()]) == 32
00072             )   $IDpassed = true;
00073 
00074         if  (   isset($_POST[session_name()]) &&
00075                 @strlen($_POST[session_name()]) == 32
00076             )   $IDpassed = true;
00077 
00078         if  (   isset($_GET[session_name()]) &&
00079                 @strlen($_GET[session_name()]) == 32
00080             )   $IDpassed = true;
00081         
00082         if  (!$IDpassed)  
00083             {   
00084                 // Es wurde keine (gültige) Session-ID übergeben.
00085                 // Script-Parameter der URL zufügen
00086                 
00087                 $query = @$_SERVER["QUERY_STRING"] != "" ? "?".$_SERVER["QUERY_STRING"] : "";
00088              
00089                 header("Status: 302 Found");
00090                 
00091                 // Script terminiert
00092                 $this->redirectTo($_SERVER["PHP_SELF"].$query);
00093             }
00094             
00095         // Wenn die Session-ID übergeben wurde, muss sie
00096                                 // nicht unbedingt gültig sein!
00097         
00098         // Für weiteren Gebrauch merken    
00099         $this->usesCookies =
00100                        (isset($_COOKIE[session_name()]) &&
00101                         @strlen($_COOKIE[session_name()])
00102                         == 32);
00103     }    
00104  
00105 ### -------------------------------------------------------
00106 
00114     function sendNoCacheHeader()    {        
00115         header("Expires: Sat, 05 Aug 2000 22:27:00 GMT");
00116         header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
00117         header("Cache-Control: no-cache, must-revalidate");
00118         header("Pragma: no-cache");
00119         header("Cache-Control: post-check=0, pre-check=0");        
00120     }
00121 
00122 ### -------------------------------------------------------
00123 
00136     function redirectTo($pathInfo) {
00137         
00138         // Relativer Pfad?
00139         if ($pathInfo[0] != "/")
00140             {   $pathInfo = substr(getenv("PATH_INFO"),
00141                                    0,
00142                                    strrpos(getenv("PATH_INFO"),"/")+1
00143                                    )
00144                             .$pathInfo;
00145             }
00146 
00147         // Läuft dieses Script auf einem non-standard Port? 
00148         $port    = !preg_match( "/^(80|443)$/",
00149                                 getenv("SERVER_PORT"),
00150                                 $portMatch)
00151                    ? ":".getenv("SERVER_PORT")
00152                    : "";
00153                                          
00154         // Redirect    
00155         header("Location: "
00156                .(($portMatch[1] == 443) ? "https://" : "http://")
00157                .$_SERVER["HTTP_HOST"].$port.$this->url($pathInfo));
00158         exit;
00159     }
00160 
00161 ### -------------------------------------------------------
00162 
00168     function removeTrail($pathInfo) {
00169         $dummy = preg_match("/(.*)(?<!&|\?)/",$pathInfo,$match);
00170         return $match[0];  
00171     }
00172 
00173 ### -------------------------------------------------------
00174 
00180     function url($pathInfo)  {        
00181         if ($this->usesCookies || $this->transSID) return $pathInfo;
00182 
00183         // Anchor-Fragment extrahieren
00184         $dummyArray = split("#",$pathInfo);
00185         $pathInfo = $dummyArray[0];
00186 
00187         // evtl. (kaputte) Session-ID(s) aus dem Querystring entfernen
00188         $pathInfo = preg_replace(   "/[?|&]".session_name()."=[^&]*/",
00189                                     "",
00190                                     $pathInfo);
00191         
00192         // evtl. Query-Delimiter korrigieren
00193         if (preg_match("/&/",$pathInfo) && !preg_match("/\?/",$pathInfo))
00194             {
00195                 // 4ter Parameter für "preg_replace()" erst ab 4.0.1pl2
00196                 $pathInfo = preg_replace("/&/","?",$pathInfo,1); 
00197             }
00198         
00199         // Restmüll entsorgen
00200         $pathInfo = $this->removeTrail($pathInfo);
00201         
00202         // Session-Name und Session-ID frisch hinzufügen  
00203         $pathInfo .= preg_match("/\?/",$pathInfo) ? "&" : "?";
00204         $pathInfo .= session_name()."=".session_id();
00205         
00206         // Anchor-Fragment wieder anfügen
00207         $pathInfo .= isset($dummyArray[1]) ? "#".$dummyArray[1] : "";
00208         
00209         return $pathInfo;                       
00210     }
00211     
00212 ### -------------------------------------------------------
00213 
00222     function hidden() {
00223         if ($this->usesCookies || $this->transSID) return "";
00224         return "<INPUT  type=\"hidden\"
00225                         name=\"".session_name()."\"
00226                         value=\"".session_id()."\">";
00227     }
00228 } // of class    
00229     
00230 ?>

Generated on Fri Dec 13 2013 10:18:32 for ILIAS Release_3_5_x_branch .rev 46805 by  doxygen 1.7.1