[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/net/http/session/ -> sessionmanager.class.php (source)

   1  <?php
   2  
   3      
   4      lt_include( PLOG_CLASS_PATH."class/net/url.class.php" );
   5      lt_include( PLOG_CLASS_PATH."class/net/http/httpvars.class.php" );
   6      lt_include( PLOG_CLASS_PATH."class/net/http/session/sessioninfo.class.php" );
   7      lt_include( PLOG_CLASS_PATH."class/config/config.class.php" );
   8      
   9      $__sessionManagerInitialized = false;
  10      
  11      /**
  12       * \ingroup Net_HTTP
  13       *
  14       * Class that deals with session, initializing, session cookies, etc. It also takes care of setting
  15       * the session folder paths, by calling the method SessionManager::setSessionSavePath().
  16       */
  17      class SessionManager 
  18      {
  19      
  20          /**
  21           * static method that takes care of initializing a session if there is none yet. It will also call
  22           * setSessionSavePath() in order to tell PHP where sessions should be read and stored.
  23           *
  24           * @return nothing
  25           * @static
  26           * @see SessionInfo
  27           * @see SessionManager::setSessionSavePath()
  28           */
  29  		function init()
  30          {
  31              global $__sessionManagerInitialized;
  32  
  33              if( !$__sessionManagerInitialized )
  34              {
  35                  // this needs to be done before anything else
  36                  SessionManager::setSessionSavePath();
  37              
  38                  // this needs to be done before the session is started
  39                  $sessionPath   = SessionManager::setSessionCookiePath();
  40                  //$sessionPath = "/";
  41                  $sessionDomain = SessionManager::setSessionCookieDomain();
  42              
  43                  session_cache_limiter( "" );
  44                  // Use base_url as part of session name to support multiplue lifetype installations in the one site
  45                  $config =& Config::getConfig();
  46                  $baseURL = $config->getValue( "base_url" );
  47                  $sessionName = "LT" . preg_replace("/[^a-zA-Z0-9]/", "", $baseURL);
  48                  session_name( $sessionName );
  49      
  50                  session_set_cookie_params(0, $sessionPath, $sessionDomain);
  51                  session_start();
  52      
  53                  //
  54                  // if there is no session object, we better create one
  55                  //
  56                  $session = HttpVars::getSession();
  57                  if( !array_key_exists("SessionInfo", $session) || empty($session["SessionInfo"]) ) {
  58                      $session["SessionInfo"] = new SessionInfo();
  59                      HttpVars::setSession( $session );
  60                  }
  61                  
  62                  // inform the other methods that the session manager has already been intialized
  63                  $__sessionManagerInitialized = true;
  64              }
  65          }
  66          
  67          /**
  68           * @private
  69           * @static
  70           * Sets the session cookie path to the right path
  71           * @return nothing
  72           */
  73  		function setSessionCookiePath()
  74          {
  75              // get the current base url and fetch it dynamically according to the values
  76              // returned by Apache/PHP if not set yet in the config settings
  77              $config =& Config::getConfig();
  78              $scriptUrl = $config->getValue( "base_url" );
  79              if( $scriptUrl == "" )
  80                  $scriptUrl = HttpVars::getBaseUrl();
  81                  
  82              $url = new Url( $scriptUrl );
  83              $path = dirname($url->getPath());
  84  
  85              if( $path == "" || $path == "\\" ) 
  86                  $path = "/";            
  87          
  88              return $path;
  89          }
  90          
  91          /**
  92           * @private
  93           * @static
  94           * Sets the right domain for the cookie
  95           * @return nothing
  96           */
  97  		function setSessionCookieDomain()
  98          {
  99              $scriptUrl = HttpVars::getBaseUrl();
 100                              
 101              $url = new Url( $scriptUrl );
 102              $domain = preg_replace("/^www\./", ".", $url->getHost());
 103              
 104              // this won't work for top level domains and domains such as
 105              // 'localhost' or internal domains for obvious security reasons...
 106              // See comments in http://fi.php.net/manual/en/function.session-set-cookie-params.php
 107              if( count(explode('.', $domain)) > 1 ) {
 108                  return $domain;                
 109              }
 110          }
 111          
 112          /**
 113           * Sets the folder where sessions should be saved, in case we'd like to save
 114           * them somewhere else. This class will check the config parameter <b>session_save_path</b>: if
 115           * it's not empty, it will use its value as the name of the folder where sessions should be saved, and it
 116           * will also take care of creating the folder if it does not exist. If the folder exists but it cannot
 117           * be read, it will throw an exception and quit (because this is a big issue)
 118           * If the value of this parameter is empty, it will not do anything and use PHP's default settings for this.
 119           *
 120           * @static
 121           */
 122  		function setSessionSavePath()
 123          {
 124              $config =& Config::getConfig();
 125              $sessionFolder = $config->getValue( "session_save_path" );
 126              // do we need to do anything if we are using the default
 127              // session path?  PHP defaults to /tmp/, so there isn't
 128              // anything to do
 129              if( isset( $sessionFolder )) {
 130                  if( !File::exists( $sessionFolder )) {
 131                      // create folder with only user permissions
 132                      // since we want to protect the session data
 133                      if( !File::createDir( $sessionFolder, 0700 )) {
 134                          throw( new Exception( "Sessions should be " .
 135                              "saved in $sessionFolder but it " .
 136                              "doesn't exist and I can't create it!" ));
 137                          die();
 138                      }
 139                  }
 140  
 141                  // check if the folder is accessible
 142                  if( !File::isReadable( $sessionFolder ) ||
 143                      !File::isWritable( $sessionFolder )) {
 144                      if( !File::chMod( $sessionFolder, 0700 )) {
 145                          throw( new Exception( "Sessions should be " .
 146                              "saved in $sessionFolder but it is " . 
 147                              "not accessible!" ));
 148                          die();
 149                      }
 150                  }
 151                  // if everything ok, we can continue...
 152                  session_save_path( $sessionFolder );
 153              }
 154              
 155              return true;
 156          }
 157          
 158          /**
 159           * return the value of a parameter from the session
 160           *
 161           * @param key The name of the parameter
 162           * @return The value assigned to this key
 163           */
 164  		function getSessionValue( $key )
 165          {
 166              // switch that informs whether the session manager has already been initialized or not
 167              global $__sessionManagerInitialized;
 168              
 169              // check if the session manager has already been initialized
 170              if( !$__sessionManagerInitialized ) {
 171                  throw( new Exception( "SessionManager::init() must be called before SessionManager::getSessionValue()" ));
 172                  die();
 173              }
 174              
 175              // get the session and SessionInfo object
 176              $session = HttpVars::getSession();
 177              $sessionInfo = $session["SessionInfo"];
 178              
 179              // and now return the value
 180              return( $sessionInfo->getValue( $key ));
 181          }
 182          
 183          /**
 184           * sets a value in the session
 185           *
 186           * @param key the key assigned to this vlaue
 187           * @param value The value assigned
 188           * @return always true
 189           */
 190  		function setSessionValue( $key, $value )
 191          {
 192              // switch that informs whether the session manager has already been initialized or not
 193              global $__sessionManagerInitialized;
 194  
 195              // check if the session manager has already been initialized
 196              if( !$__sessionManagerInitialized ) {
 197                  throw( new Exception( "SessionManager::init() must be called before SessionManager::getSessionValue()" ));
 198                  die();
 199              }
 200              
 201              // get the session and SessionInfo object
 202              $session = HttpVars::getSession();
 203              $sessionInfo = $session["SessionInfo"];
 204              // set the value and save it to the session
 205              $sessionInfo->setValue( $key, $value );
 206              $session["SessionInfo"] = $sessionInfo;
 207              HttpVars::setSession( $session );
 208              
 209              return true;
 210          }
 211  
 212          /**
 213           * Retrieve the UserInfo object from the session, if any
 214           *
 215           * @return A UserInfo object if found, or false otherwise
 216           */
 217  		function getUserInfoFromSession()
 218          {
 219              $session = HttpVars::getSession();
 220              $sessionInfo = $session["SessionInfo"];
 221              $userInfo = $sessionInfo->getValue("userInfo");
 222              if( !is_object( $userInfo ))
 223                  return false;
 224              
 225              return( $userInfo );
 226          }
 227      }
 228  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics