/Vendor/PHPSecureSession/SecureSession.php

https://github.com/adrianodemoura/phpGridOld · PHP · 186 lines · 105 code · 1 blank · 80 comment · 9 complexity · 1a08ed615b4d9314d833ad1e107cab84 MD5 · raw file

  1. <?php
  2. /**
  3. * ------------------------------------------------
  4. * Encrypt PHP session data using files
  5. * ------------------------------------------------
  6. * The encryption is built using mcrypt extension
  7. * and the randomness is managed by openssl
  8. *
  9. * @author Enrico Zimuel (enrico@zimuel.it)
  10. * @copyright GNU General Public License
  11. */
  12. class SecureSession {
  13. const CIPHER= MCRYPT_RIJNDAEL_256;
  14. const CIPHER_MODE= MCRYPT_MODE_CBC;
  15. /**
  16. * Key for encryption/decryption
  17. *
  18. * @var string
  19. */
  20. private static $_key;
  21. /**
  22. * Path of the session file
  23. *
  24. * @var string
  25. */
  26. private static $_path;
  27. /**
  28. * Session name (optional)
  29. *
  30. * @var string
  31. */
  32. private static $_name;
  33. /**
  34. * Size of the IV vector for encryption
  35. *
  36. * @var integer
  37. */
  38. private static $_ivSize;
  39. /**
  40. * Cookie variable name of the key
  41. *
  42. * @var string
  43. */
  44. private static $_keyName;
  45. /**
  46. * Generate a random key
  47. * fallback to mt_rand if PHP < 5.3 or no openssl available
  48. *
  49. * @param integer $length
  50. * @return string
  51. */
  52. private static function _randomKey($length=32) {
  53. if(function_exists('openssl_random_pseudo_bytes')) {
  54. $rnd = openssl_random_pseudo_bytes($length, $strong);
  55. if($strong === TRUE)
  56. return $rnd;
  57. }
  58. for ($i=0;$i<$length;$i++) {
  59. $sha= sha1(mt_rand());
  60. $char= mt_rand(0,30);
  61. $rnd.= chr(hexdec($sha[$char].$sha[$char+1]));
  62. }
  63. return $rnd;
  64. }
  65. /**
  66. * Open the session
  67. *
  68. * @param string $save_path
  69. * @param string $session_name
  70. * @return bool
  71. */
  72. public static function open($save_path, $session_name) {
  73. self::$_path= $save_path.'/';
  74. self::$_name= $session_name;
  75. self::$_keyName= "KEY_$session_name";
  76. self::$_ivSize= mcrypt_get_iv_size(self::CIPHER, self::CIPHER_MODE);
  77. if (empty($_COOKIE[self::$_keyName])) {
  78. $keyLength= mcrypt_get_key_size(self::CIPHER, self::CIPHER_MODE);
  79. self::$_key= self::_randomKey($keyLength);
  80. $cookie_param = session_get_cookie_params();
  81. setcookie(
  82. self::$_keyName,
  83. base64_encode(self::$_key),
  84. $cookie_param['lifetime'],
  85. $cookie_param['path'],
  86. $cookie_param['domain'],
  87. $cookie_param['secure'],
  88. $cookie_param['httponly']
  89. );
  90. } else {
  91. self::$_key= base64_decode($_COOKIE[self::$_keyName]);
  92. }
  93. return true;
  94. }
  95. /**
  96. * Close the session
  97. *
  98. * @return bool
  99. */
  100. public static function close() {
  101. return true;
  102. }
  103. /**
  104. * Read and decrypt the session
  105. *
  106. * @param integer $id
  107. * @return string
  108. */
  109. public static function read($id) {
  110. $sess_file = self::$_path.self::$_name."_$id";
  111. $data= @file_get_contents($sess_file);
  112. if (empty($data)) {
  113. return false;
  114. }
  115. $iv= substr($data,0,self::$_ivSize);
  116. $encrypted= substr($data,self::$_ivSize);
  117. $decrypt = mcrypt_decrypt(
  118. self::CIPHER,
  119. self::$_key,
  120. $encrypted,
  121. self::CIPHER_MODE,
  122. $iv
  123. );
  124. return rtrim($decrypt, "");
  125. }
  126. /**
  127. * Encrypt and write the session
  128. *
  129. * @param integer $id
  130. * @param string $data
  131. * @return bool
  132. */
  133. public static function write($id, $data) {
  134. $sess_file = self::$_path.self::$_name."_$id";
  135. $iv= mcrypt_create_iv(self::$_ivSize, MCRYPT_RAND);
  136. if ($fp = @fopen($sess_file, "w")) {
  137. $encrypted= mcrypt_encrypt(
  138. self::CIPHER,
  139. self::$_key,
  140. $data,
  141. self::CIPHER_MODE,
  142. $iv
  143. );
  144. $return = fwrite($fp, $iv.$encrypted);
  145. fclose($fp);
  146. return $return;
  147. } else {
  148. return false;
  149. }
  150. }
  151. /**
  152. * Destroy the session
  153. *
  154. * @param int $id
  155. * @return bool
  156. */
  157. public static function destroy($id) {
  158. $sess_file = self::$_path.self::$_name."_$id";
  159. setcookie (self::$_keyName, '', time() - 3600);
  160. return(@unlink($sess_file));
  161. }
  162. /**
  163. * Garbage Collector
  164. *
  165. * @param int $max
  166. * @return bool
  167. */
  168. public static function gc($max) {
  169. foreach (glob(self::$_path.self::$_name.'_*') as $filename) {
  170. if (filemtime($filename) + $max < time()) {
  171. @unlink($filename);
  172. }
  173. }
  174. return true;
  175. }
  176. }
  177. // Set the custom PHP session handler
  178. ini_set('session.save_handler', 'user');
  179. session_set_save_handler(array('SecureSession', 'open'),
  180. array('SecureSession', 'close'),
  181. array('SecureSession', 'read'),
  182. array('SecureSession', 'write'),
  183. array('SecureSession', 'destroy'),
  184. array('SecureSession', 'gc')
  185. );