PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/session.php

https://github.com/Fusion/lenses
PHP | 109 lines | 81 code | 8 blank | 20 comment | 12 complexity | 2257e72a91b0ecf4297a7eb177bfb137 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * @package Lenses
  4. * @copyright (c) Chris F. Ravenscroft
  5. * @license See 'license.txt'
  6. */
  7. class Session
  8. {
  9. public $userId,
  10. $loggedIn;
  11. function __construct($userId = -1)
  12. {
  13. // If we are passing a new user id OR have no user id yet...
  14. if($userId != -1 || empty($_SESSION['uid']))
  15. $_SESSION['uid'] = $userId;
  16. $this->userId = $_SESSION['uid'];
  17. }
  18. static function start()
  19. {
  20. session_start();
  21. // 'in' is set when we create this session
  22. // If it isn't, then we just arrived.
  23. if(!self::defined('In'))
  24. {
  25. // Create session object in database
  26. $_SESSION['In'] = true;
  27. // If we have a cookie, then we're coming back, aren't we?
  28. if(!empty($_COOKIE[Config::$cookie]))
  29. {
  30. $cookie = unserialize($_COOKIE[Config::$cookie]);
  31. // TODO MEMBER NEEDS TO BE MOVED TO LIBS
  32. }
  33. }
  34. else
  35. {
  36. // We are not coming back...
  37. // Are we a member?
  38. if(self::defined('Member'))
  39. {
  40. // If no cookie set, then create one and persist
  41. if(empty($_COOKIE[Config::$cookie]))
  42. {
  43. $persistKey = sha1(time());
  44. $member = self::get('Member');
  45. // Update member with SID so that cookie can persist
  46. $member->persist_key = $persistKey;
  47. $member->save();
  48. // Create cookie for peristence
  49. setCookie(
  50. Config::$cookie,
  51. serialize(
  52. array(
  53. 'uid' => $member->id,
  54. 'persist_key' => $member->persist_key
  55. )
  56. ),
  57. time()+60*60*24*365
  58. );
  59. }
  60. }
  61. }
  62. }
  63. static function persist($obj)
  64. {
  65. $_SESSION[$obj->name] = $obj;
  66. }
  67. static function delete()
  68. {
  69. // Persistence cookie
  70. unset($_COOKIE[Config::$cookie]);
  71. setCookie(Config::$cookie, '', time()-3600);
  72. // Current session
  73. foreach($_SESSION as $key => $value)
  74. {
  75. unset($_SESSION[$key]);
  76. }
  77. session_destroy();
  78. }
  79. static function defined($key)
  80. {
  81. return !(empty($_SESSION[$key]));
  82. }
  83. static function get($key)
  84. {
  85. return empty($_SESSION[$key]) ? null : $_SESSION[$key];
  86. }
  87. function logMeIn($username, $password)
  88. {
  89. global $db, $salt;
  90. if(empty($_SESSION['username']) || empty($_SESSION['password']))
  91. return false;
  92. if( $_SESSION['username'] != $username ||
  93. $_SESSION['password'] != sha1(Config::$salt . $password))
  94. return false;
  95. $this->loggedIn = true;
  96. return true;
  97. }
  98. }
  99. #global $user;
  100. #$user = new Session();
  101. ?>