/models/User.php

https://github.com/josephwegner/holidaywish.es · PHP · 264 lines · 122 code · 55 blank · 87 comment · 25 complexity · bcc83d78f93e5fc0fa6ee7ac59c7ed92 MD5 · raw file

  1. <?php
  2. $classBuilder['User'] = new UserModel();
  3. class UserModel {
  4. /*
  5. * Purpose: Register a user
  6. *
  7. * @param int user's id (because the user is already created
  8. * @param string username
  9. * @param string password
  10. *
  11. * @return boolean true=worked
  12. * @return string error
  13. */
  14. public function register($id, $user, $pass) {
  15. if(!is_numeric($id)) return "Error Registering. Try again later.";
  16. $user = mysql_escape_string($user);
  17. $pass = mysql_escape_string($pass);
  18. $sql = "SELECT `id` FROM users WHERE `username`='".$user."'";
  19. $check = mysql_query($sql);
  20. if(mysql_num_rows($check) > 0) return "Username already in use";
  21. $salt = $this->makeSalt();
  22. $password = $this->encodePassword($user, $pass, $salt);
  23. $sql = "UPDATE users SET username='".$user."', password='".$password."', salt='".$salt."' WHERE id=".$id;
  24. mysql_query($sql);
  25. return true;
  26. }
  27. /*
  28. * Purpose: Attempt to log a user in
  29. *
  30. * @param string username
  31. * @param string password
  32. *
  33. * @return boolean false = failed login
  34. * @return int user id
  35. *
  36. * @note FUNCTION MAY RETURN FALSY VALUE. BE SURE TO CHECK ABSOLUTE FALSE AND NOT (0)
  37. */
  38. public function login($user, $pass) {
  39. if(isset($_SESSION['user'])) return false;
  40. $user = mysql_escape_string($user);
  41. $pass = mysql_escape_string($pass);
  42. $sql = "SELECT `password`, `salt`, `id`, `is_admin` FROM users WHERE `username`='".$user."'";
  43. $userDat = mysql_query($sql);
  44. if(mysql_num_rows($userDat) != 1) return false;
  45. $data = mysql_fetch_object($userDat);
  46. if($this->encodePassword($user, $pass, $data->salt) == $data->password) {//It worked
  47. $ret = new StdClass();
  48. $ret->id = $data->id;
  49. $ret->admin = $data->is_admin;
  50. return $ret;
  51. } else {//Bad Login
  52. return false;
  53. }
  54. }
  55. /*
  56. * Purpose: Return the username by the user's ID
  57. *
  58. * @param int id of user
  59. *
  60. * @return bool false=didn't work
  61. * @return string username
  62. */
  63. public function getUsername($id) {
  64. if(!is_numeric($id)) return false;
  65. $sql = "SELECT `username` FROM users WHERE `id`=".$id;
  66. $data = mysql_query($sql);
  67. $arr = mysql_fetch_array($data);
  68. return $arr['username'];
  69. }
  70. /*
  71. * Purpose: Return the user's name
  72. *
  73. * @param int id of user
  74. *
  75. * @return bool false=didn't work
  76. * @return string name
  77. */
  78. public function getName($id) {
  79. if(!is_numeric($id)) return false;
  80. $sql = "SELECT `name` FROM users WHERE `id`=".$id;
  81. $data = mysql_query($sql);
  82. $arr = mysql_fetch_array($data);
  83. return $arr['name'];
  84. }
  85. /*
  86. * Purpose: Get a list of all the usernames, except for the specified user
  87. *
  88. * @param int (opt) The user's id. Default is the current user
  89. *
  90. * @return bool false=didn't work
  91. * @return array of usernames
  92. */
  93. public function getAllUsernames($id = false) {
  94. if($id === false) {
  95. $id = $_SESSION['user'];
  96. }
  97. if(!is_numeric($id)) return false;
  98. $sql = "SELECT `username` FROM users WHERE id != ".$id;
  99. $data = mysql_query($sql);
  100. $users = array();
  101. while($user = mysql_fetch_object($data)) {
  102. array_push($users, $user->username);
  103. }
  104. return $users;
  105. }
  106. /*
  107. * Purpose: Get the secret santa recipient of a certain user.
  108. *
  109. * @param int (opt) The user's id. Default is the current user
  110. * @param bool (opt) If the secret santa recipient is new. Only flag this if the recipient has ALREADY BEEN SET.
  111. *
  112. * @return bool false=didn't work
  113. * @return StdClass with user data
  114. */
  115. public function getSSRecipient($id = false, $newSecretSanta = false) {
  116. if($id === false) {
  117. $id = $_SESSION['user'];
  118. }
  119. if(!is_numeric($id)) return false;
  120. $sql = "SELECT secretsanta.recipient, users.username, users.name FROM secretsanta, users WHERE secretsanta.santa=".$id." AND users.id=secretsanta.recipient";
  121. $data = mysql_query($sql);
  122. if(mysql_num_rows($data) != 1) {
  123. if($this->hasSecretSantaRecipient($id)) {
  124. return false;
  125. } else {
  126. $this->setSecretSantaRecipient($id);
  127. return $this->getSSRecipient($id, true);
  128. }
  129. } else {
  130. $userData = mysql_fetch_object($data);
  131. $user = new StdClass();
  132. $user->id = $userData->recipient;
  133. $user->username = $userData->username;
  134. $user->name = $userData->name;
  135. $user->isNew = $newSecretSanta;
  136. return $user;
  137. }
  138. }
  139. /*
  140. * Begin Private Functions
  141. */
  142. /*
  143. * Purpose: Check if a user has a secret santa recipient chosen
  144. *
  145. * @param int The user's id
  146. *
  147. * @return bool false=no Recipient || true=has recipient
  148. */
  149. private function hasSecretSantaRecipient($id) {
  150. if(!is_numeric($id)) return false;
  151. $sql = "SELECT `id` FROM secretsanta WHERE `santa`=".$id;
  152. $check = mysql_query($sql);
  153. return (mysql_num_rows($check) == 1);
  154. }
  155. /*
  156. * Purpose: Set the SS recipient for the user
  157. *
  158. * @param int id = The user's id
  159. *
  160. * @return bool false=failed true=worked
  161. */
  162. private function setSecretSantaRecipient($id) {
  163. if(!is_numeric($id)) return false;
  164. $sql = "SELECT `id` FROM users WHERE group_id=(SELECT group_id FROM users WHERE `id`=".$id.") AND ss_picked=0 AND NOT id=".$id;
  165. $userData = mysql_query($sql);
  166. $users = array();
  167. while($user = mysql_fetch_object($userData)) {
  168. array_push($users, $user);
  169. }
  170. $key = array_rand($users);
  171. $sql = "INSERT INTO secretsanta (`santa`, `recipient`) VALUES (".$id.", ".$users[$key]->id.")";
  172. mysql_query($sql);
  173. $sql = "UPDATE users SET ss_picked=1 WHERE id=".$users[$key]->id;
  174. mysql_query($sql);
  175. return true;
  176. }
  177. /*
  178. * Purpose: Encode a password with salt, and user-specific hashing
  179. *
  180. * @param string username
  181. * @param string password
  182. * @param string salt
  183. *
  184. * @return string encoded password
  185. */
  186. private function encodePassword($user, $pass, $salt) {
  187. $h_user = sha1($user);
  188. $h_pass = md5($pass); //Mix in an md5, so if our DB ever gets cracked, it's not painfully obvious how we did hashed things.
  189. $h_salt = sha1($salt);
  190. return sha1($h_user.$h_pass.$h_salt);
  191. }
  192. /*
  193. * Purpose: Make some salt
  194. *
  195. * @param int length of salt. Default is 8
  196. *
  197. * @return string salt
  198. */
  199. private function makeSalt($len = 8) {
  200. if(!is_numeric($len)) $len = 8;
  201. $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  202. $string = "";
  203. for($i=0; $i<$len; $i++) {
  204. $string .= $chars[mt_rand(0, strlen($chars))];
  205. }
  206. return $string;
  207. }
  208. }
  209. ?>