PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/classes/login.class.php

http://domuslink.googlecode.com/
PHP | 231 lines | 138 code | 44 blank | 49 comment | 25 complexity | 85b84999c52ece5d985382a5c3cfe387 MD5 | raw file
  1. <?php
  2. /*
  3. * domus.Link :: PHP Web-based frontend for Heyu (X10 Home Automation)
  4. * Copyright (c) 2007, Istvan Hubay Cebrian (istvan.cebrian@domus.link.co.pt)
  5. * Project's homepage: http://domus.link.co.pt
  6. * Project's dev. homepage: http://domuslink.googlecode.com
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope's that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details. You should have
  17. * received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation,
  19. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. require_once("userdb.class.php");
  22. require_once("user.class.php");
  23. require_once(CLASS_FILE_LOCATION.'user.const.php');
  24. class Login {
  25. private $userDB;
  26. private $theUser;
  27. private $noSecurity;
  28. function __construct() {
  29. $args = func_get_args();
  30. $this->noSecurity = false;
  31. if (empty($args)) {
  32. throw new Exception("Login::construct - initialization requires userdb file location");
  33. }
  34. if (isset($args[1]) && strtolower($args[1]) == 'off') {
  35. $this->noSecurity = true;
  36. $this->userDB = new UserDB($args[0]);
  37. return;
  38. }
  39. $this->userDB = new UserDB($args[0]);
  40. // restore user from session if already set
  41. if(isset($_SESSION['username'])) {
  42. $this->theUser = $this->userDB->getUser($_SESSION['username']);
  43. }
  44. }
  45. /**
  46. * check Login with session or cookie
  47. */
  48. function login() {
  49. if ($this->noSecurity)
  50. return true;
  51. $result = false;
  52. if($this->checkSession())
  53. $result = true;
  54. elseif ($this->checkCookie())
  55. $result = true;
  56. return $result;
  57. }
  58. /**
  59. * check memory session
  60. */
  61. function checkSession() {
  62. if ($this->noSecurity)
  63. return true;
  64. if(isset($_SESSION["username"])) {
  65. if(!empty($_SESSION["username"])) {
  66. // error_log("session found in memory... ");
  67. return true;
  68. }
  69. }
  70. // error_log("no session found");
  71. return false;
  72. }
  73. /**
  74. *
  75. */
  76. function checkCookie() {
  77. if ($this->noSecurity)
  78. return true;
  79. error_log("check cookie");
  80. if(isset($_COOKIE[ "type" ]) && isset($_COOKIE[ "password" ])) {
  81. // $cookiePassword = $this->decrypt( $_COOKIE[ "password" ] );
  82. $cookiePassword = $_COOKIE[ "password" ];
  83. if(!empty($_COOKIE[ "type" ])) {
  84. error_log("** COOKIE FOUND ** decrypt");
  85. if ($_COOKIE[ "type" ] == PIN_TYPE_D )
  86. return $this->checkLoginByPin($cookiePassword,0);
  87. else
  88. return $this->checkLogin($_COOKIE[ "username" ] ,$cookiePassword,0);
  89. }
  90. }
  91. error_log("no cookieFound");
  92. return false;
  93. }
  94. function getKey() {
  95. return "thisIsASecureKeyForCookie";
  96. }
  97. function encrypt($data) {
  98. $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  99. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  100. return mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->getKey() , $data, MCRYPT_MODE_ECB, $iv);
  101. }
  102. function decrypt($data) {
  103. $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  104. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  105. return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->getKey(), $data, MCRYPT_MODE_ECB, $iv);
  106. }
  107. /**
  108. * store ident in a cookie
  109. * @param $theUser
  110. * @param $password: plain text password
  111. */
  112. function memoriseIdent($theUser,$password) {
  113. // todo store on a single cookie
  114. setcookie("login",$theUser->getUserName(), time()+3600*24*30, "/");
  115. // setcookie("password",$this->encrypt($password), time()+3600*24*30, "/");
  116. setcookie("password",$password, time()+3600*24*30, "/");
  117. setcookie("type",$theUser->getType(), time()+3600*24*30, "/");
  118. }
  119. /**
  120. *
  121. */
  122. function checkLoginByPin( $password, $remember) {
  123. if ($this->noSecurity)
  124. return true;
  125. $theUser = $this->userDB->findPIN($password);
  126. return $this->validateAndUpdateSession($theUser,"",$password, $remember);
  127. }
  128. function checkLogin($login, $password, $remember) {
  129. if ($this->noSecurity)
  130. return true;
  131. $theUser = $this->userDB->getUser($login);
  132. return $this->validateAndUpdateSession($theUser ,$login, $password, $remember);
  133. }
  134. function validateAndUpdateSession( $theUser, $login,$password, $remember) {
  135. if ($this->noSecurity)
  136. return true;
  137. if(isset($theUser)) {
  138. error_log("validateAndUpdateSession: userFound");
  139. $this->theUser = $theUser;
  140. if ($this->theUser->validatePassword($password)) {
  141. $this->ok = true;
  142. # store session
  143. $_SESSION['username'] = $this->theUser->getUserName();
  144. $_SESSION['password'] = $this->theUser->getPassword();
  145. if ($remember)
  146. $this->memoriseIdent($theUser, $password);
  147. error_log("validateAndUpdateSession: good user");
  148. return true;
  149. }
  150. }
  151. error_log("validateAndUpdateSession: bad user or bad password");
  152. return false;
  153. }
  154. /**
  155. *
  156. */
  157. function checkPassword($password) {
  158. if ($this->noSecurity)
  159. return true;
  160. if($this->theUser->validateMD5Password($password)) {
  161. $this->ok = true;
  162. return true;
  163. }
  164. return false;
  165. }
  166. function getUser() {
  167. if ($this->noSecurity) {
  168. $aUser = new User();
  169. $aUser->setSecurityLevel(0);
  170. return $aUser;
  171. }
  172. return $this->theUser;
  173. }
  174. function getUserDB() {
  175. return $this->userDB;
  176. }
  177. /**
  178. *
  179. */
  180. function logout() {
  181. $this->ok = false;
  182. unset($_SESSION['password']);
  183. unset($_SESSION['username']);
  184. setcookie("type", "", time() - 3600, "/");
  185. }
  186. }
  187. ?>