/www/include/lib_login.php

https://github.com/straup/parallel-flickr · PHP · 177 lines · 105 code · 51 blank · 21 comment · 16 complexity · 79ae7faaeb814b4d7cf2f30fe83bc634 MD5 · raw file

  1. <?php
  2. #
  3. # $Id$
  4. #
  5. #################################################################
  6. #
  7. # make sure the users is signed in. if not, bounce them
  8. # to the login page, with an optional post-login redirect.
  9. #
  10. function login_ensure_loggedin($redir=null){
  11. if ($GLOBALS['cfg']['user']['id']) return;
  12. $base_url = isset($GLOBALS['cfg']) && isset($GLOBALS['cfg']['abs_root_url'])
  13. ? rtrim($GLOBALS['cfg']['abs_root_url'], '/')
  14. : '';
  15. if (! $redir){
  16. $redir = ltrim($_SERVER['REQUEST_URI'], "/");
  17. }
  18. if ($redir){
  19. header("Location: {$base_url}/signin/?redir=".urlencode($redir));
  20. }else{
  21. header("Location: {$base_url}/signin/");
  22. }
  23. exit;
  24. }
  25. #################################################################
  26. #
  27. # make sure the user is NOT logged in. if they are, redirect them,
  28. # optionally logging them out first.
  29. #
  30. function login_ensure_loggedout($redir="/", $force_logout=false){
  31. if (!$GLOBALS['cfg']['user']['id']) return;
  32. if ($force_logout) login_do_logout();
  33. $base_url = isset($GLOBALS['cfg']) && isset($GLOBALS['cfg']['abs_root_url'])
  34. ? rtrim($GLOBALS['cfg']['abs_root_url'], '/')
  35. : '';
  36. $redir = ltrim($redir, '/');
  37. header("Location: {$base_url}/{$redir}");
  38. exit;
  39. }
  40. #################################################################
  41. function login_check_login(){
  42. if (!$GLOBALS['cfg']['enable_feature_signin']){
  43. return 0;
  44. }
  45. if ($GLOBALS['cfg']['user']['id']){
  46. return 1;
  47. }
  48. $auth_cookie = login_get_cookie($GLOBALS['cfg']['auth_cookie_name']);
  49. if (!$auth_cookie){
  50. return 0;
  51. }
  52. $auth_cookie = crypto_decrypt($auth_cookie, $GLOBALS['cfg']['crypto_cookie_secret']);
  53. list($user_id, $password) = explode(':', $auth_cookie, 2);
  54. if (!$user_id){
  55. return 0;
  56. }
  57. $user = users_get_by_id($user_id);
  58. if (!$user){
  59. return 0;
  60. }
  61. if ($user['deleted']){
  62. return 0;
  63. }
  64. if ($user['password'] !== $password){
  65. return 0;
  66. }
  67. $GLOBALS['cfg']['user'] = $user;
  68. return 1;
  69. }
  70. #################################################################
  71. function login_do_login(&$user, $redir=''){
  72. $expires = ($GLOBALS['cfg']['enable_feature_persistent_login']) ? strtotime('now +2 years') : 0;
  73. $auth_cookie = login_generate_auth_cookie($user);
  74. login_set_cookie($GLOBALS['cfg']['auth_cookie_name'], $auth_cookie, $expires);
  75. $base_url = isset($GLOBALS['cfg']) && isset($GLOBALS['cfg']['abs_root_url'])
  76. ? rtrim($GLOBALS['cfg']['abs_root_url'], '/')
  77. : '';
  78. if (! $redir){
  79. $redir = "{$base_url}/";
  80. }
  81. $redir = urlencode($redir);
  82. header("Location: {$base_url}/checkcookie/?redir={$redir}");
  83. exit;
  84. }
  85. #################################################################
  86. function login_do_logout(){
  87. $GLOBALS['cfg']['user'] = null;
  88. login_unset_cookie($GLOBALS['cfg']['auth_cookie_name']);
  89. }
  90. #################################################################
  91. function login_generate_auth_cookie(&$user){
  92. $cookie = implode(":", array($user['id'], $user['password']));
  93. return crypto_encrypt($cookie, $GLOBALS['cfg']['crypto_cookie_secret']);
  94. }
  95. #################################################################
  96. function login_get_cookie($name){
  97. return $_COOKIE[$name];
  98. }
  99. #################################################################
  100. function login_set_cookie($name, $value, $expire=0, $path='/'){
  101. $res = setcookie(
  102. $name,
  103. $value,
  104. $expire,
  105. $path,
  106. $GLOBALS['cfg']['auth_cookie_domain'],
  107. $GLOBALS['cfg']['auth_cookie_secure'],
  108. $GLOBALS['cfg']['auth_cookie_httponly']
  109. );
  110. }
  111. #################################################################
  112. function login_unset_cookie($name, $path='/'){
  113. $value = '';
  114. $expire = time() - 3600;
  115. $res = setcookie(
  116. $name,
  117. $value,
  118. $expire,
  119. $path,
  120. $GLOBALS['cfg']['auth_cookie_domain'],
  121. $GLOBALS['cfg']['auth_cookie_secure'],
  122. $GLOBALS['cfg']['auth_cookie_httponly']
  123. );
  124. }
  125. #################################################################
  126. ?>