Guest User

Untitled

a guest
May 2nd, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. <?php
  2. //set off all error for security purposes
  3. error_reporting(E_ALL);
  4.  
  5.  
  6. //define some contstant
  7. define( "DB_DSN", "mysql:host=localhost;dbname=gryp" );
  8. define( "DB_USERNAME", "root" );
  9. define( "DB_PASSWORD", "" );
  10. define( "CLS_PATH", "class" );
  11.  
  12. //include the classes
  13. include_once( CLS_PATH . "/user.php" );
  14.  
  15. ?>
  16.  
  17. <?php
  18.  
  19. class Users {
  20. public $username = null;
  21. public $password = null;
  22. public $salt = null;
  23.  
  24. public function __construct( $data = array() ) {
  25. if( isset( $data['username'] ) ) $this->username = mysql_real_escape_string( htmlspecialchars( strip_tags( $data['username'] ) ) );
  26. if( isset( $data['password'] ) ) $this->password = mysql_real_escape_string( htmlspecialchars( strip_tags( $data['password'] ) ) );
  27.  
  28. }
  29.  
  30. public function storeFormValues( $params ) {
  31. //store the parameters
  32. $this->__construct( $params );
  33. }
  34.  
  35. public function userLogin() {
  36. $success = false;
  37. try{
  38. $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
  39. $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  40.  
  41. $sql = "SELECT * FROM users WHERE username = :username LIMIT 1";
  42. $fetch = $con->prepare( $sql );
  43. $fetch->bindValue( "username", $this->username, PDO::PARAM_STR );
  44. $fetch->execute();
  45. $row = $fetch->fetch(PDO::FETCH_ASSOC);
  46.  
  47. if($row){
  48. $this->salt=$row['salt'];
  49. if ( hash("sha256", $this->password . $this->salt) == $row['password'])
  50. {
  51. $success = true;
  52. $sql = "UPDATE users SET lastlogin=NOW() WHERE username=:username";
  53. $fetch = $con->prepare($sql);
  54. $fetch->bindValue( "username", $this->username, PDO::PARAM_STR );
  55. $fetch->execute();
  56. }
  57. }
  58.  
  59. $con = null;
  60. return $success;
  61. }catch (PDOException $e) {
  62. echo $e->getMessage();
  63. return $success;
  64. }
  65. }
  66.  
  67. public function register() {
  68. $correct = false;
  69. try {
  70. $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
  71. $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  72.  
  73. $this->salt = $this->unique_md5();
  74.  
  75. $sql = "INSERT INTO users(username, password,salt,registerdate) VALUES(:username, :password,:salt,NOW())";
  76.  
  77. $fetch = $con->prepare( $sql );
  78.  
  79.  
  80. $fetch->bindValue( "username", $this->username, PDO::PARAM_STR );
  81. $fetch->bindValue( "password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR );
  82. $fetch->bindValue( "salt", $this->salt, PDO::PARAM_STR );
  83. $fetch->execute();
  84. return "Registration Successful <br/> <a href='index.php'>Login Now</a>";
  85. }catch( PDOException $e ) {
  86. return $e->getMessage();
  87. }
  88. }
  89.  
  90. public function unique_md5() {
  91. mt_srand(microtime(true)*100000 + memory_get_usage(true));
  92. return md5(uniqid(mt_rand(), true));
  93. }
  94.  
  95. # name type collation null default extra
  96. 1 userID int(11) No None AUTO_INCREMENT
  97. 2 username varchar(50) latin1_swedish_ci No None
  98. 3 password varbinary(250) No None
  99. 4 salt varbinary(32) No None
  100. 5 registerdate datetime No None
  101. 6 lastlogin datetime No None
  102.  
  103. $this->salt=$stmt->fetchColumn(3);
  104.  
  105. class UserDAO {
  106.  
  107. private $dbh;
  108.  
  109. //$dbh is the dbhandle you get from new PDO(…)
  110. function __construct( $dbh ) {
  111. $this->dbh = $dbh;
  112. }
  113.  
  114. function save( $user ) {
  115. if ( !isset( $user->id ) || $user->id == 0 ) {
  116. $this->insert( $user );
  117. } else {
  118. $this->update( $user );
  119. }
  120. }
  121.  
  122. function getByUsername( $username ) {
  123. $stmt = $this->dbh->prepare( "SELECT * FROM user WHERE username = ?" );
  124. if ( !$stmt ) {
  125. echo 'Error in fetch query';
  126. die();
  127. }
  128. $stmt->setFetchMode( PDO::FETCH_CLASS, "User" );
  129. $stmt->execute( array( $username ) );
  130. $user = $stmt->fetch();
  131. return $user
  132. }
  133.  
  134. private function insert( $user ) {
  135. $stmt = $this->dbh->prepare( "INSERT INTO
  136. user (username, email, password)
  137. VALUES (:username, :email, :password)" );
  138. if ( !$stmt ) {
  139. echo 'Error in saving query.';
  140. die();
  141. }
  142. $stmt->bindParam( ':username', $user->username );
  143. $stmt->bindParam( ':email', $user->email );
  144. $stmt->bindParam( ':password', $user->password );
  145. if ( !$stmt->execute() ) {
  146. echo 'Error saving user data. ';
  147. echo $stmt->errorCode();
  148. print_r( $stmt->errorInfo() );
  149. die();
  150. }
  151. $user->id = $this->dbh->lastInsertId();
  152. }
  153.  
  154. private function update( $user ) {
  155. $stmt = $this->dbh->prepare( "UPDATE user
  156. SET username = :username,
  157. email = :email,
  158. password = :password
  159. WHERE
  160. id = :id" );
  161. if ( !$stmt ) {
  162. echo 'Error in update query';
  163. die();
  164. }
  165. $stmt->bindParam( ':username', $user->username );
  166. $stmt->bindParam( ':email', $user->email );
  167. $stmt->bindParam( ':password', $user->password );
  168. $stmt->bindParam( ':id', $user->id );
  169. if ( !$stmt->execute() ) {
  170. echo 'Error updating user';
  171. echo $stmt->errorCode();
  172. print_r( $stmt->errorInfo() );
  173. die();
  174. }
  175.  
  176. $dao = DAOFactory::getDAO("user");
  177. if ($user = $dao->getByUsername($this->username)) {
  178. if (Utils::checkPassword($password, $user->password)) {
  179. $_SESSION['username'] = $user->username;
  180. $user->last_login = date("Y-m-d H:i:s");
  181. $dao->save($user);
  182. header('Location: info.php');
  183. return;
  184. }
  185. }
  186. $this->errors[] = "Invalid username or password";
  187.  
  188. static function hashPassword($pwd) {
  189. $salt = self::secure_rand(16);
  190. $salt = str_replace('+', '.', base64_encode($salt));
  191. if (CRYPT_BLOWFISH == 1) {
  192. $salt = substr($salt, 0, 22);
  193. $cost = "07";
  194. $hash = crypt($pwd, '$2a$' . $cost . '$' . $salt);
  195. return $hash;
  196. } else {
  197. $salt = substr($salt, 0, 8);
  198. $hash = crypt($pwd, '$1$' . $salt . '$');
  199. return $hash;
  200. }
  201. }
  202. private static function secure_rand($length) {
  203. if(function_exists('openssl_random_pseudo_bytes')) {
  204. $rnd = openssl_random_pseudo_bytes($length, $strong);
  205. if ($strong === TRUE)
  206. return $rnd;
  207. }
  208. $sha =''; $rnd ='';
  209. for ($i=0; $i<$length; $i++) {
  210. $sha = hash('md5',$sha.mt_rand());
  211. $char = mt_rand(0,62);
  212. $rnd .= chr(hexdec($sha[$char].$sha[$char+1]));
  213. }
  214. return $rnd;
  215. }
  216.  
  217. static function checkPassword($pwd, $hash) {
  218. return $hash == crypt($pwd, $hash);
  219. }
  220.  
  221. public function __construct( $data = array() ) {
  222. $this->storeFormValues( $data );
  223. }
  224.  
  225. public function storeFormValues( $params ) {
  226. //store the parameters
  227. if( isset( $params['username'] ) ) $this->username = mysql_real_escape_string( htmlspecialchars( strip_tags( $params['username'] ) ) );
  228. if( isset( $params['password'] ) ) $this->password = mysql_real_escape_string( htmlspecialchars( strip_tags( $params['password'] ) ) );
  229. }
Advertisement
Add Comment
Please, Sign In to add comment