PageRenderTime 64ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/common/user.php

https://github.com/carlosefonseca/ER-client-webapp
PHP | 215 lines | 122 code | 25 blank | 68 comment | 34 complexity | fcf6ef232d405f0831fd13db6ed22487 MD5 | raw file
  1. <?
  2. global $permissions;
  3. global $logged_in;
  4. function hasPermission($permission) {
  5. if ($permission[0] == 'j') {
  6. return hasGardenPermission($permission);
  7. }
  8. return (in_array($permission, $_SESSION["permissions"]) || in_array("admin", $_SESSION["permissions"]));
  9. }
  10. function hasGardenPermission($garden) {
  11. if ($garden[0] == 'j') {
  12. $garden = substr($garden, 1);
  13. }
  14. return (in_array('*', $_SESSION["permGardens"]) || in_array($garden, $_SESSION["permGardens"]));
  15. }
  16. /**
  17. * Checks whether or not the given username is in the
  18. * database, if so it checks if the given password is
  19. * the same password in the database for that user.
  20. * If the user doesn't exist or if the passwords don't
  21. * match up, it returns an error code (1 or 2).
  22. * On success it returns 0.
  23. */
  24. function confirmUser($username, $password){
  25. global $permissions;
  26. global $client;
  27. require_once(u("DBconnect.php"));
  28. /* Add slashes if necessary (for query) */
  29. if(!get_magic_quotes_gpc()) {
  30. $username = addslashes($username);
  31. $password = stripslashes($password);
  32. }
  33. /* Verify that user is in database */
  34. $q = "select users.user, email, gardens, permissions.permissions
  35. from users left join permissions on (users.user=permissions.user)
  36. WHERE (permissions.client = '$client' OR permissions.client = '*')
  37. and (users.user = '$username' OR users.email = '$username')
  38. and pass='$password';";
  39. // iLog($q);
  40. $result = mysql_query($q);
  41. if(!$result || (mysql_num_rows($result) != 1)){
  42. return false; //Indicates failure
  43. }
  44. $dbarray = mysql_fetch_array($result);
  45. if ($dbarray['gardens']=="" && $dbarray['permissions'] == "") {
  46. return "NO_PERMISSION";
  47. }
  48. $_SESSION["permissions"] = explode(",", $dbarray['permissions']);
  49. $_SESSION["permGardens"] = explode(",", $dbarray['gardens']);
  50. return $dbarray['user'];
  51. /* $dbarray['password'] = stripslashes($dbarray['password']);
  52. $password = stripslashes($password);
  53. /* Validate that password is correct *
  54. if(md5($password) == $dbarray['password']){
  55. return 0; //Success! Username and password confirmed
  56. }
  57. else{
  58. return 2; //Indicates password failure
  59. }*/
  60. }
  61. /**
  62. * checkLogin - Checks if the user has already previously
  63. * logged in, and a session with the user has already been
  64. * established. Also checks to see if user has been remembered.
  65. * If so, the database is queried to make sure of the user's
  66. * authenticity. Returns true if the user has logged in.
  67. */
  68. function checkLogin(){
  69. /* Check if user has been remembered */
  70. if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])){
  71. $_SESSION['username'] = $_COOKIE['cookname'];
  72. $_SESSION['password'] = $_COOKIE['cookpass'];
  73. }
  74. /* Username and password have been set */
  75. if(isset($_SESSION['username']) && isset($_SESSION['password'])){
  76. // iLog("User&Pass are set... confirming...");
  77. /* Confirm that username and password are valid */
  78. if(confirmUser($_SESSION['username'], $_SESSION['password'])){
  79. return true;
  80. } else {
  81. /* Variables are incorrect, user not logged in */
  82. unset($_SESSION['username']);
  83. unset($_SESSION['password']);
  84. return false;
  85. }
  86. }
  87. /* User not logged in */
  88. else{
  89. return false;
  90. }
  91. }
  92. function requireLogin() {
  93. if(!checkLogin()) header("Location: ".L(login,true));
  94. }
  95. /**
  96. * Determines whether or not to display the login
  97. * form or to show the user that he is logged in
  98. * based on if the session variables are set.
  99. */
  100. function displayLogin(){
  101. global $logged_in;
  102. if($logged_in){
  103. echo "<h3>Logged In!</h3>";
  104. echo "Bem-vindo <b>$_SESSION[username]</b>. <a href=\"".L("logout",true)."\">Logout</a>";
  105. }
  106. else{
  107. ?>
  108. <form action="<? L("login"); ?>" method="post">
  109. <h3>Login :: Área de Cliente</h3>
  110. <div id="username"><span>Utilizador ou email: </span><input type="text" name="user" maxlength="30" size="15"></div>
  111. <div id="password"><span>Password: </span><input type="password" name="pass" maxlength="30" size="15"></div>
  112. <? /*<tr><td colspan="2" align="left"><input type="checkbox" name="remember">
  113. <font size="2">Remember me next time</td></tr>*/?>
  114. <? if (isset($_GET['e'])): ?>
  115. <p class="login error">O nome de utilizador e password que introduziu não existem.</p>
  116. <? elseif (isset($_GET['np']) && isset($_GET['u'])): ?>
  117. <p class="login error">O utilizador '<?= $_GET['u'];?>' não tem permissões de acesso a este site. Contacte o seu responsável.</p>
  118. <? endif; ?>
  119. <div id="submit"><input type="submit" name="sublogin" class="botao" value="Login"></div>
  120. <p><a href="<? l("newaccount"); ?>">Criar uma conta</a></p>
  121. </form>
  122. <?
  123. }
  124. }
  125. /**
  126. * Checks to see if the user has submitted his
  127. * username and password through the login form,
  128. * if so, checks authenticity in database and
  129. * creates session.
  130. */
  131. if(isset($_POST['sublogin'])){
  132. global $logged_in;
  133. /* Check that all fields were typed in */
  134. if(!$_POST['user'] || !$_POST['pass']){
  135. die('You didn\'t fill in a required field.');
  136. }
  137. /* Spruce up username, check length */
  138. $_POST['user'] = trim($_POST['user']);
  139. if(strlen($_POST['user']) > 30){
  140. die("Sorry, the username is longer than 30 characters, please shorten it.");
  141. }
  142. /* Checks that username is in database and password is correct */
  143. $md5pass = md5($_POST['pass']);
  144. // iLog("2-User&Pass are set... confirming...");
  145. $result = confirmUser($_POST['user'], $md5pass);
  146. /* Check error codes */
  147. if($result === false){
  148. header("Location: ".url("login&e"));
  149. // echo 'That username/password doesn\'t exist in our database.';
  150. return ;
  151. }
  152. if ($result == "NO_PERMISSION") {
  153. header("Location: ".url("login&np&u=".$_POST['user']));
  154. return ;
  155. }
  156. /* Username and password correct, register session variables */
  157. $_POST['user'] = $result;//stripslashes($_POST['user']);
  158. $_SESSION['username'] = $result;//$_POST['user'];
  159. $_SESSION['password'] = $md5pass;
  160. $_SESSION['permissions'] = $permissions;
  161. /**
  162. * This is the cool part: the user has requested that we remember that
  163. * he's logged in, so we set two cookies. One to hold his username,
  164. * and one to hold his md5 encrypted password. We set them both to
  165. * expire in 100 days. Now, next time he comes to our site, we will
  166. * log him in automatically.
  167. *
  168. if(isset($_POST['remember'])){
  169. setcookie("cookname", $_SESSION['username'], time()+60*60*24*100, "/");
  170. setcookie("cookpass", $_SESSION['password'], time()+60*60*24*100, "/");
  171. }/
  172. /* Quick self-redirect to avoid resending data on refresh */
  173. //echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
  174. header("Location: $_SERVER[PHP_SELF]");
  175. return;
  176. } else {
  177. if(isset($_GET['q']) && $_GET['q'] == 'logout') {
  178. /* Kill session variables */
  179. unset($_SESSION['username']);
  180. unset($_SESSION['password']);
  181. unset($_SESSION['permissions']);
  182. $_SESSION = array(); // reset session array
  183. session_destroy(); // destroy session.
  184. }
  185. }
  186. /* Sets the value of the logged_in variable, which can be used in your code */
  187. $logged_in = checkLogin();
  188. ?>