includes/sessions.php
changeset 378 c1c7fa6b329f
parent 377 bb3e6c3bd4f4
child 387 92664d2efab8
equal deleted inserted replaced
377:bb3e6c3bd4f4 378:c1c7fa6b329f
    11  *
    11  *
    12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
    12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
    13  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    13  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    14  */
    14  */
    15  
    15  
    16 // Prepare a string for insertion into a MySQL database
       
    17 function filter($str) { global $db; return $db->escape($str); }
       
    18 
       
    19 /**
    16 /**
    20  * Anything and everything related to security and user management. This includes AES encryption, which is illegal in some countries.
    17  * Anything and everything related to security and user management. This includes AES encryption, which is illegal in some countries.
    21  * Documenting the API was not easy - I hope you folks enjoy it.
    18  * Documenting the API was not easy - I hope you folks enjoy it.
    22  * @package Enano
    19  * @package Enano
    23  * @subpackage Session manager
    20  * @subpackage Session manager
  1181    */
  1178    */
  1182    
  1179    
  1183   function validate_session($key)
  1180   function validate_session($key)
  1184   {
  1181   {
  1185     global $db, $session, $paths, $template, $plugins; // Common objects
  1182     global $db, $session, $paths, $template, $plugins; // Common objects
  1186     $aes = new AESCrypt(AES_BITS, AES_BLOCKSIZE, true);
  1183     profiler_log("SessionManager: checking session: " . sha1($key));
       
  1184     $aes = AESCrypt::singleton(AES_BITS, AES_BLOCKSIZE);
  1187     $decrypted_key = $aes->decrypt($key, $this->private_key, ENC_HEX);
  1185     $decrypted_key = $aes->decrypt($key, $this->private_key, ENC_HEX);
  1188     
  1186     
  1189     if ( !$decrypted_key )
  1187     if ( !$decrypted_key )
  1190     {
  1188     {
  1191       // die_semicritical('AES encryption error', '<p>Something went wrong during the AES decryption process.</p><pre>'.print_r($decrypted_key, true).'</pre>');
  1189       // die_semicritical('AES encryption error', '<p>Something went wrong during the AES decryption process.</p><pre>'.print_r($decrypted_key, true).'</pre>');
  1282     
  1280     
  1283     $this->user_extra = $user_extra;
  1281     $this->user_extra = $user_extra;
  1284     // Leave the rest to PHP's automatic garbage collector ;-)
  1282     // Leave the rest to PHP's automatic garbage collector ;-)
  1285     
  1283     
  1286     $row['password'] = md5($real_pass);
  1284     $row['password'] = md5($real_pass);
       
  1285     
       
  1286     profiler_log("SessionManager: finished session check");
       
  1287     
  1287     return $row;
  1288     return $row;
  1288   }
  1289   }
  1289   
  1290   
  1290   /**
  1291   /**
  1291    * Validates a session key, and returns the userdata associated with the key or false. Optimized for compatibility with the old MD5-based auth system.
  1292    * Validates a session key, and returns the userdata associated with the key or false. Optimized for compatibility with the old MD5-based auth system.
  1358     $ou = $this->username;
  1359     $ou = $this->username;
  1359     $oid = $this->user_id;
  1360     $oid = $this->user_id;
  1360     if($level > USER_LEVEL_CHPREF)
  1361     if($level > USER_LEVEL_CHPREF)
  1361     {
  1362     {
  1362       $aes = AESCrypt::singleton(AES_BITS, AES_BLOCKSIZE);
  1363       $aes = AESCrypt::singleton(AES_BITS, AES_BLOCKSIZE);
  1363       if(!$this->user_logged_in || $this->auth_level < USER_LEVEL_MOD)
  1364       if(!$this->user_logged_in || $this->auth_level < ( USER_LEVEL_MEMBER + 1))
  1364       {
  1365       {
  1365         return 'success';
  1366         return 'success';
  1366       }
  1367       }
       
  1368       // See if we can get rid of the cached decrypted session key
       
  1369       $key_bin = $aes->hextostring(strrev($this->sid_super));
       
  1370       $key_hash = sha1($key_bin . '::' . $this->private_key);
       
  1371       aes_decrypt_cache_destroy($key_hash);
  1367       // Destroy elevated privileges
  1372       // Destroy elevated privileges
  1368       $keyhash = md5(strrev($this->sid_super));
  1373       $keyhash = md5(strrev($this->sid_super));
  1369       $this->sql('DELETE FROM '.table_prefix.'session_keys WHERE session_key=\''.$keyhash.'\' AND user_id=\'' . $this->user_id . '\';');
  1374       $this->sql('DELETE FROM '.table_prefix.'session_keys WHERE session_key=\''.$keyhash.'\' AND user_id=\'' . $this->user_id . '\';');
  1370       $this->sid_super = false;
  1375       $this->sid_super = false;
  1371       $this->auth_level = USER_LEVEL_MEMBER;
  1376       $this->auth_level = USER_LEVEL_MEMBER;
  1372     }
  1377     }
  1373     else
  1378     else
  1374     {
  1379     {
  1375       if($this->user_logged_in)
  1380       if($this->user_logged_in)
  1376       {
  1381       {
       
  1382         $aes = AESCrypt::singleton(AES_BITS, AES_BLOCKSIZE);
       
  1383         // See if we can get rid of the cached decrypted session key
       
  1384         $key_bin = $aes->hextostring($this->sid);
       
  1385         $key_hash = sha1($key_bin . '::' . $this->private_key);
       
  1386         aes_decrypt_cache_destroy($key_hash);
  1377         // Completely destroy our session
  1387         // Completely destroy our session
  1378         if($this->auth_level > USER_LEVEL_CHPREF)
  1388         if($this->auth_level > USER_LEVEL_CHPREF)
  1379         {
  1389         {
  1380           $this->logout(USER_LEVEL_ADMIN);
  1390           $this->logout(USER_LEVEL_ADMIN);
  1381         }
  1391         }