/lib/user/database.php

https://github.com/kunalghosh/OwnCloud · PHP · 180 lines · 80 code · 16 blank · 84 comment · 7 complexity · e0137b20d0bdb1a4bf05b8f5f8e8ddec MD5 · raw file

  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @copyright 2010 Frank Karlitschek karlitschek@kde.org
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope 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 AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /*
  23. *
  24. * The following SQL statement is just a help for developers and will not be
  25. * executed!
  26. *
  27. * CREATE TABLE `users` (
  28. * `uid` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
  29. * `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  30. * PRIMARY KEY (`uid`)
  31. * ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  32. *
  33. */
  34. require_once 'phpass/PasswordHash.php';
  35. /**
  36. * Class for user management in a SQL Database (e.g. MySQL, SQLite)
  37. */
  38. class OC_User_Database extends OC_User_Backend {
  39. static private $userGroupCache=array();
  40. /**
  41. * @var PasswordHash
  42. */
  43. static private $hasher=null;
  44. private function getHasher(){
  45. if(!self::$hasher){
  46. //we don't want to use DES based crypt(), since it doesn't return a has with a recognisable prefix
  47. $forcePortable=(CRYPT_BLOWFISH!=1);
  48. self::$hasher=new PasswordHash(8,$forcePortable);
  49. }
  50. return self::$hasher;
  51. }
  52. /**
  53. * @brief Create a new user
  54. * @param $uid The username of the user to create
  55. * @param $password The password of the new user
  56. * @returns true/false
  57. *
  58. * Creates a new user. Basic checking of username is done in OC_User
  59. * itself, not in its subclasses.
  60. */
  61. public function createUser( $uid, $password ){
  62. if( $this->userExists($uid) ){
  63. return false;
  64. }else{
  65. $hasher=$this->getHasher();
  66. $hash = $hasher->HashPassword($password);
  67. $query = OC_DB::prepare( "INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )" );
  68. $result = $query->execute( array( $uid, $hash));
  69. return $result ? true : false;
  70. }
  71. }
  72. /**
  73. * @brief delete a user
  74. * @param $uid The username of the user to delete
  75. * @returns true/false
  76. *
  77. * Deletes a user
  78. */
  79. public function deleteUser( $uid ){
  80. // Delete user-group-relation
  81. $query = OC_DB::prepare( "DELETE FROM `*PREFIX*users` WHERE uid = ?" );
  82. $result = $query->execute( array( $uid ));
  83. return true;
  84. }
  85. /**
  86. * @brief Set password
  87. * @param $uid The username
  88. * @param $password The new password
  89. * @returns true/false
  90. *
  91. * Change the password of a user
  92. */
  93. public function setPassword( $uid, $password ){
  94. if( $this->userExists($uid) ){
  95. $hasher=$this->getHasher();
  96. $hash = $hasher->HashPassword($password);
  97. $query = OC_DB::prepare( "UPDATE *PREFIX*users SET password = ? WHERE uid = ?" );
  98. $result = $query->execute( array( $hash, $uid ));
  99. return true;
  100. }
  101. else{
  102. return false;
  103. }
  104. }
  105. /**
  106. * @brief Check if the password is correct
  107. * @param $uid The username
  108. * @param $password The password
  109. * @returns true/false
  110. *
  111. * Check if the password is correct without logging in the user
  112. */
  113. public function checkPassword( $uid, $password ){
  114. $query = OC_DB::prepare( "SELECT uid, password FROM *PREFIX*users WHERE uid LIKE ?" );
  115. $result = $query->execute( array( $uid));
  116. $row=$result->fetchRow();
  117. if($row){
  118. $storedHash=$row['password'];
  119. if (substr($storedHash,0,1)=='$'){//the new phpass based hashing
  120. $hasher=$this->getHasher();
  121. if($hasher->CheckPassword($password, $storedHash)){
  122. return $row['uid'];
  123. }else{
  124. return false;
  125. }
  126. }else{//old sha1 based hashing
  127. if(sha1($password)==$storedHash){
  128. //upgrade to new hashing
  129. $this->setPassword($row['uid'],$password);
  130. return $row['uid'];
  131. }else{
  132. return false;
  133. }
  134. }
  135. }else{
  136. return false;
  137. }
  138. }
  139. /**
  140. * @brief Get a list of all users
  141. * @returns array with all uids
  142. *
  143. * Get a list of all users.
  144. */
  145. public function getUsers(){
  146. $query = OC_DB::prepare( "SELECT uid FROM *PREFIX*users" );
  147. $result = $query->execute();
  148. $users=array();
  149. while( $row = $result->fetchRow()){
  150. $users[] = $row["uid"];
  151. }
  152. return $users;
  153. }
  154. /**
  155. * @brief check if a user exists
  156. * @param string $uid the username
  157. * @return boolean
  158. */
  159. public function userExists($uid){
  160. $query = OC_DB::prepare( "SELECT * FROM `*PREFIX*users` WHERE uid = ?" );
  161. $result = $query->execute( array( $uid ));
  162. return $result->numRows() > 0;
  163. }
  164. }