Guest User

Untitled

a guest
Sep 11th, 2018
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. Session Management with Sharp UMS
  2. include_once("includes.php");
  3. $auth = new TAuthentication();
  4. $accept_roles = array('plugin');
  5. $auth_result = $auth->validateSession($accept_roles);
  6.  
  7. if ($auth_result->auth_code == AUTH_NO_SESSION) {
  8. header('Access-Control-Allow-Origin: *');
  9. echo "AUTH_NO_SESSION";
  10. // means that no session was found, therefore the page is being accessed anonymously.
  11. } elseif ($auth_result->auth_code == AUTH_OKAY) {
  12. header('Access-Control-Allow-Origin: *');
  13. echo "AUTH_OKAY";
  14. // means that there was a session and the user owns all the required roles to access this content.
  15. } elseif ($auth_result->auth_code == AUTH_INSUFFICIENT_ROLES) {
  16. header('Access-Control-Allow-Origin: *');
  17. echo "AUTH_INSUFFICIENT_ROLES";
  18. // means that a session exists, but the user does not own the required roles to access this content.
  19. } else {
  20. // no code here
  21. }
  22.  
  23. <!DOCTYPE html>
  24. <html>
  25. <head>
  26. <script type="text/javascript">
  27. function loadXMLDoc()
  28. {
  29. if (window.XMLHttpRequest)
  30. {// code for IE7+, Firefox, Chrome, Opera, Safari
  31. xmlhttp=new XMLHttpRequest();
  32. }
  33. else
  34. {// code for IE6, IE5
  35. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  36. }
  37. xmlhttp.onreadystatechange=function()
  38. {
  39. if (xmlhttp.readyState==4 && xmlhttp.status==200)
  40. {
  41. document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  42. }
  43. }
  44. xmlhttp.open("GET","user_cred.php",true);
  45. xmlhttp.send();
  46. }
  47. </script>
  48. </head>
  49. <body>
  50.  
  51. <h2>Using the XMLHttpRequest object</h2>
  52. <div id="myDiv"></div>
  53. <button type="button" onclick="loadXMLDoc()">Change Content</button>
  54.  
  55. </body>
  56. </html>
  57.  
  58. <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
  59. <ul>
  60. <li class="listitem">
  61. <div class="row">
  62. <label>Username:</label>
  63. <input class="textbox" type="text" name="username" value="" maxlength="80"/>
  64. </div>
  65. <div class="row">
  66. <label>Password:</label>
  67. <input class="textbox" type="password" name="password" value="" maxlength="80"/>
  68. </div>
  69. </li>
  70. <li class="listitem">
  71. <div class="row">
  72. <input class="form-button" type="submit" name="signin" value="Signin"/>
  73. <a class="loginoptions indentmore" href="signup.php">Signup</a>
  74. <a class="loginoptions" href="resetpassword.php">Forgot your password?</a>
  75. </div>
  76. </li>
  77. </ul>
  78. </form>
  79.  
  80. include_once("includes.php");
  81.  
  82. class TSigninController extends TAbstractController {
  83.  
  84. public function run($allowedRoles = null)
  85. {
  86. $this->allowedRoles = $allowedRoles;
  87. $this->execute();
  88. }
  89.  
  90. protected function execute()
  91. {
  92. $this->auth_result = parent::validateSession(null);
  93.  
  94. if ($this->auth_result->auth_code == AUTH_OKAY)
  95. {
  96. $this->goToAfterSignInPage($this->auth_result->roles);
  97. }
  98. else if (!$this->getUserAction())
  99. {
  100. $this->loadview("signin");
  101. }
  102. else
  103. {
  104. $this->signin();
  105. }
  106. }
  107.  
  108. protected function signin()
  109. {
  110. $input = $this->getUserInput();
  111. $model = $this->loadmodel("Users");
  112. $account = $model->getUser($input["username"], $input["password"]);
  113.  
  114. if ($account == null || sizeof($account) == 0)
  115. {
  116. $data = array("error" => "Could not sign you in");
  117. $this->loadview("signin", $data);
  118. return;
  119. }
  120.  
  121. if ($account["disabled"] == 1 || $account["admin_disabled"] == 1)
  122. {
  123. $data = array("error" => ($account["admin_disabled"] == 0) ? "This account is disabled." : "This account is been locked by the admin. Please contact the site admin!");
  124. $this->loadview("signin", $data);
  125. return;
  126. }
  127.  
  128. $this->createNewSession($account);
  129. $this->goToAfterSignInPage($account["roles"]);
  130. }
  131.  
  132. protected function createNewSession($account) {
  133. $model = $this->loadmodel("Sessions");
  134. $sessionid = crypt($account["username"] . date('now'));
  135.  
  136. $_SESSION['SESSIONID'] = $sessionid;
  137. $model->createNewSession($sessionid, $account["id"]);
  138. }
  139.  
  140. public function goToAfterSignInPage($roles)
  141. {
  142. foreach($roles as $role)
  143. {
  144. if ($this->utils->stringsEqual($role["name"], "admin", false))
  145. {
  146. $this->redirect(SITE_URL . "/admin/dashboard.php");
  147. return;
  148. }
  149. }
  150.  
  151. $this->redirect(SITE_URL . "/user/userprofile.php");
  152. }
  153.  
  154. protected function getUserAction()
  155. {
  156. if ($this->post("signin"))
  157. return "signin";
  158. else
  159. return null;
  160. }
  161.  
  162. protected function getUserInput()
  163. {
  164. return array(
  165. "username" => $this->post("username"),
  166. "password" => $this->post("password")
  167. );
  168. }
  169. }
  170.  
  171. $controller = new TSigninController();
  172. $controller->run();
  173.  
  174. SELECT
  175. s.userid,
  176. s.id,
  177. s.started_on,
  178. (
  179. DATE_ADD(
  180. s.started_on,
  181. INTERVAL $this->sessionlength SECOND
  182. ) < NOW()
  183. ) expired
  184. FROM sessions s
  185. WHERE s.id = '$sessionid'
  186.  
  187. protected function createNewSession($account) {
  188. $model = $this->loadmodel("Sessions");
  189. $sessionid = crypt($account["username"] . date('now'));
  190.  
  191. $_SESSION['SESSIONID'] = $sessionid;
  192. $model->createNewSession($sessionid, $account["id"]);
  193. }
Add Comment
Please, Sign In to add comment