• 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         //  Session-Namen setzen, Session initialisieren   
00049         session_name(isset($sessionName)
00050             ? $sessionName
00051             : session_name());
00052 
00053         @session_start();
00054         
00055         //  Prüen ob die Session-ID die Standardlänge
00056         //  von 32 Zeichen hat,
00057         //  ansonsten Session-ID neu setzen 
00058         if (strlen(session_id()) != 32)
00059             {
00060                 mt_srand ((double)microtime()*1000000);
00061                 session_id(md5(uniqid(mt_rand())));
00062             }
00063         
00064         //  Prüfen, ob eine Session-ID übergeben wurde
00065         //  (über Cookie, POST oder GET)
00066         $IDpassed = false;
00067         if  (   isset($_COOKIE[session_name()]) &&
00068                 @strlen($_COOKIE[session_name()]) == 32
00069             )   $IDpassed = true;
00070 
00071         if  (   isset($_POST[session_name()]) &&
00072                 @strlen($_POST[session_name()]) == 32
00073             )   $IDpassed = true;
00074 
00075         if  (   isset($_GET[session_name()]) &&
00076                 @strlen($_GET[session_name()]) == 32
00077             )   $IDpassed = true;
00078         
00079         if  (!$IDpassed)  
00080             {   
00081                 // Es wurde keine (gültige) Session-ID übergeben.
00082                 // Script-Parameter der URL zufügen
00083                 
00084                 $query = @$_SERVER["QUERY_STRING"] != "" ? "?".$_SERVER["QUERY_STRING"] : "";
00085              
00086                 header("Status: 302 Found");
00087                 
00088                 // Script terminiert
00089                 $this->redirectTo($_SERVER["PHP_SELF"].$query);
00090             }
00091             
00092         // Wenn die Session-ID übergeben wurde, muss sie
00093                                 // nicht unbedingt gültig sein!
00094         
00095         // Für weiteren Gebrauch merken    
00096         $this->usesCookies =
00097                        (isset($_COOKIE[session_name()]) &&
00098                         @strlen($_COOKIE[session_name()])
00099                         == 32);
00100     }    
00101  
00102 ### -------------------------------------------------------
00103 
00111     function sendNoCacheHeader()    {        
00112         header("Expires: Sat, 05 Aug 2000 22:27:00 GMT");
00113         header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
00114         header("Cache-Control: no-cache, must-revalidate");
00115         header("Pragma: no-cache");
00116         header("Cache-Control: post-check=0, pre-check=0");        
00117     }
00118 
00119 ### -------------------------------------------------------
00120 
00133     function redirectTo($pathInfo) {
00134         
00135         // Relativer Pfad?
00136         if ($pathInfo[0] != "/")
00137             {   $pathInfo = substr(getenv("PATH_INFO"),
00138                                    0,
00139                                    strrpos(getenv("PATH_INFO"),"/")+1
00140                                    )
00141                             .$pathInfo;
00142             }
00143 
00144         // Läuft dieses Script auf einem non-standard Port? 
00145         $port    = !preg_match( "/^(80|443)$/",
00146                                 getenv("SERVER_PORT"),
00147                                 $portMatch)
00148                    ? ":".getenv("SERVER_PORT")
00149                    : "";
00150                                          
00151         // Redirect    
00152         header("Location: "
00153                .(($portMatch[1] == 443) ? "https://" : "http://")
00154                .$_SERVER["HTTP_HOST"].$port.$this->url($pathInfo));
00155         exit;
00156     }
00157 
00158 ### -------------------------------------------------------
00159 
00165     function removeTrail($pathInfo) {
00166         $dummy = preg_match("/(.*)(?<!&|\?)/",$pathInfo,$match);
00167         return $match[0];  
00168     }
00169 
00170 ### -------------------------------------------------------
00171 
00177     function url($pathInfo)  {        
00178         if ($this->usesCookies || $this->transSID) return $pathInfo;
00179 
00180         // Anchor-Fragment extrahieren
00181         $dummyArray = split("#",$pathInfo);
00182         $pathInfo = $dummyArray[0];
00183 
00184         // evtl. (kaputte) Session-ID(s) aus dem Querystring entfernen
00185         $pathInfo = preg_replace(   "/[?|&]".session_name()."=[^&]*/",
00186                                     "",
00187                                     $pathInfo);
00188         
00189         // evtl. Query-Delimiter korrigieren
00190         if (preg_match("/&/",$pathInfo) && !preg_match("/\?/",$pathInfo))
00191             {
00192                 // 4ter Parameter für "preg_replace()" erst ab 4.0.1pl2
00193                 $pathInfo = preg_replace("/&/","?",$pathInfo,1); 
00194             }
00195         
00196         // Restmüll entsorgen
00197         $pathInfo = $this->removeTrail($pathInfo);
00198         
00199         // Session-Name und Session-ID frisch hinzufügen  
00200         $pathInfo .= preg_match("/\?/",$pathInfo) ? "&" : "?";
00201         $pathInfo .= session_name()."=".session_id();
00202         
00203         // Anchor-Fragment wieder anfügen
00204         $pathInfo .= isset($dummyArray[1]) ? "#".$dummyArray[1] : "";
00205         
00206         return $pathInfo;                       
00207     }
00208     
00209 ### -------------------------------------------------------
00210 
00219     function hidden() {
00220         if ($this->usesCookies || $this->transSID) return "";
00221         return "<INPUT  type=\"hidden\"
00222                         name=\"".session_name()."\"
00223                         value=\"".session_id()."\">";
00224     }
00225 } // of class    
00226     
00227 ?>

Generated on Fri Dec 13 2013 09:06:37 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1