PageRenderTime 58ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/user/plugins/login/vendor/birke/rememberme/example/action.php

https://gitlab.com/asun89/socianovation-web
PHP | 119 lines | 81 code | 11 blank | 27 comment | 24 complexity | e9bac6deafa27feaa1bc2826885810eb MD5 | raw file
  1. <?php
  2. /**
  3. * This file demonstrates how to use the Rememberme library.
  4. *
  5. * Some code (autoload, templating) is just simple boilerplate and no shining
  6. * example of how to write php applications.
  7. *
  8. * @author Gabriel Birke
  9. */
  10. require_once __DIR__.'/../vendor/autoload.php';
  11. use Birke\Rememberme;
  12. /**
  13. * Helper function for redirecting and destroying the session
  14. * @param bool $destroySession
  15. * @return void
  16. */
  17. function redirect($destroySession=false) {
  18. if($destroySession) {
  19. session_regenerate_id(true);
  20. session_destroy();
  21. }
  22. header("Location: index.php");
  23. exit;
  24. }
  25. // Normally you would store the credentials in a DB
  26. $username = "demo";
  27. $password = "demo";
  28. // Initialize RememberMe Library with file storage
  29. $storagePath = dirname(__FILE__)."/tokens";
  30. if(!is_writable($storagePath) || !is_dir($storagePath)) {
  31. die("'$storagePath' does not exist or is not writable by the web server.
  32. To run the example, please create the directory and give it the
  33. correct permissions.");
  34. }
  35. $storage = new Rememberme\Storage\File($storagePath);
  36. $rememberMe = new Rememberme\Authenticator($storage);
  37. // First, we initialize the session, to see if we are already logged in
  38. session_start();
  39. if(!empty($_SESSION['username'])) {
  40. if(!empty($_GET['logout'])) {
  41. $rememberMe->clearCookie($_SESSION['username']);
  42. redirect(true);
  43. }
  44. if(!empty($_GET['completelogout'])) {
  45. $storage->cleanAllTriplets($_SESSION['username']);
  46. redirect(true);
  47. }
  48. // Check, if the Rememberme cookie exists and is still valid.
  49. // If not, we log out the current session
  50. if(!empty($_COOKIE[$rememberMe->getCookieName()]) && !$rememberMe->cookieIsValid()) {
  51. redirect(true);
  52. }
  53. // User is still logged in - show content
  54. $content = tpl("user_is_logged_in");
  55. }
  56. // If we are not logged in, try to log in via Rememberme cookie
  57. else {
  58. // If we can present the correct tokens from the cookie, we are logged in
  59. $loginresult = $rememberMe->login();
  60. if($loginresult) {
  61. $_SESSION['username'] = $loginresult;
  62. // There is a chance that an attacker has stolen the login token, so we store
  63. // the fact that the user was logged in via RememberMe (instead of login form)
  64. $_SESSION['remembered_by_cookie'] = true;
  65. redirect();
  66. }
  67. else {
  68. // If $rememberMe returned false, check if the token was invalid
  69. if($rememberMe->loginTokenWasInvalid()) {
  70. $content = tpl("cookie_was_stolen");
  71. }
  72. // $rememberMe returned false because of invalid/missing Rememberme cookie - normal login process
  73. else {
  74. if(!empty($_POST)) {
  75. if($username == $_POST['username'] && $password == $_POST['password']) {
  76. session_regenerate_id();
  77. $_SESSION['username'] = $username;
  78. // If the user wants to be remembered, create Rememberme cookie
  79. if(!empty($_POST['rememberme'])) {
  80. $rememberMe->createCookie($username);
  81. }
  82. else {
  83. $rememberMe->clearCookie();
  84. }
  85. redirect();
  86. }
  87. else {
  88. $content = tpl("login", "Invalid credentials");
  89. }
  90. }
  91. else {
  92. $content = tpl("login");
  93. }
  94. }
  95. }
  96. }
  97. // template function for including content, nothing interesting
  98. function tpl($template, $msg="") {
  99. $fn = __DIR__ . DIRECTORY_SEPARATOR . "templates" . DIRECTORY_SEPARATOR . $template . ".php";
  100. if(file_exists($fn)) {
  101. ob_start();
  102. include $fn;
  103. return ob_get_clean();
  104. }
  105. else {
  106. return "Template $fn not found";
  107. }
  108. }