PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/models/user.php

https://github.com/pope/jQuery-Mobile-PHP-MVC
PHP | 296 lines | 267 code | 28 blank | 1 comment | 65 complexity | 24b6120f4a2f214a5820dbd8d10a30a6 MD5 | raw file
  1. <?php
  2. class UserModel {
  3. var $user_id;
  4. var $name;
  5. var $email;
  6. var $password;
  7. var $ok;
  8. var $msg;
  9. var $is_logged;
  10. function __construct(){
  11. global $db;
  12. $this->user_id = 0;
  13. $this->email = "Guest";
  14. $this->name = "Guest";
  15. $this->ok = false;
  16. if(!$this->check_session()) $this->check_cookie();
  17. return $this->ok;
  18. }
  19. function check_session(){
  20. if(!empty($_SESSION['auth_email']) && !empty($_SESSION['auth_secret']))
  21. return $this->check($_SESSION['auth_email'], $_SESSION['auth_secret']);
  22. else
  23. return false;
  24. }
  25. function check_cookie(){
  26. if(!empty($_COOKIE['auth_email']) && !empty($_COOKIE['auth_secret']))
  27. return $this->check($_COOKIE['auth_email'], $_COOKIE['auth_secret']);
  28. else
  29. return false;
  30. }
  31. function create($info,$login = true){
  32. global $db;
  33. $name = mysql_real_escape_string($info['name']);
  34. $email = mysql_real_escape_string($info['email']);
  35. $password = md5(mysql_real_escape_string($info['password']) . PASSWORD_SALT);
  36. $status = $info['status'] ? mysql_real_escape_string($info['status']) : 1;
  37. $created_ip = $_SERVER['REMOTE_ADDR'];
  38. $this->ok = false;
  39. if(!$info['name'] || !$info['email'] || !$info['password'] || !$info['password2']){
  40. $this->msg = "Error! All fields are required.";
  41. return false;
  42. }elseif($info['password'] != $info['password2']){
  43. $this->msg = "Error! Passwords do not match.";
  44. return false;
  45. }elseif(!$this->validEmail($info['email'])){
  46. $this->msg = "Error! Please enter a valid e-mail address.";
  47. return false;
  48. }
  49. $db->query("SELECT user_id, password FROM users WHERE email = '".mysql_real_escape_string($email)."'");
  50. if(mysql_num_rows($db->result) == 1){
  51. $this->msg = "Error! E-mail address is already in use.";
  52. }else{
  53. $query = $db->query("INSERT INTO users (name,email,password,status,created_ip) VALUES ('$name','$email','$password','$status','$created_ip')");
  54. if($query){
  55. $this->msg = "User successfully added.";
  56. $this->ok = true;
  57. if($login) $this->login($info['email'],$info['password']);
  58. return true;
  59. }else{
  60. $this->msg = "There was a problem, please try again.";
  61. }
  62. }
  63. return false;
  64. }
  65. function update($info){
  66. global $db;
  67. $this->ok = false;
  68. $name = mysql_real_escape_string($info['name']);
  69. $email = mysql_real_escape_string($info['email']);
  70. $password = md5(mysql_real_escape_string($info['password']) . PASSWORD_SALT);
  71. if($info['password'] != $info['password2']){
  72. $this->msg = "Error! Passwords do not match.";
  73. return false;
  74. }elseif(!$this->validEmail($info['email'])){
  75. $this->msg = "Error! Please enter a valid e-mail address.";
  76. return false;
  77. }
  78. $sql = "name='$name', email='$email'";
  79. if($info['password']){
  80. $sql .= ", password='$password'";
  81. }
  82. $query = "UPDATE users SET ".$sql." WHERE user_id = '".$this->user_id."'";
  83. $query = $db->query($query);
  84. if($query){
  85. $this->msg = "Info successfully updated.";
  86. $this->ok = true;
  87. $_SESSION['auth_email'] = $email;
  88. if($info['password']) $_SESSION['auth_secret'] = $password;
  89. setcookie("auth_email", $email, time()+60*60*24*30, "/", COOKIE_DOMAIN);
  90. if($info['password']) setcookie("auth_secret", $password, time()+60*60*24*30, "/", COOKIE_DOMAIN);
  91. $this->name = $name;
  92. $this->email = $email;
  93. return true;
  94. }else{
  95. $this->msg = "There was a problem, please try again.";
  96. }
  97. return false;
  98. }
  99. function login($email, $password){
  100. global $db;
  101. $sql = $db->query("SELECT user_id, password, name FROM users WHERE email = '".mysql_real_escape_string($email)."'");
  102. $this->ok = false;
  103. if(!$email || !$password){
  104. $this->msg = "Error! Both E-mail and Password are required to login.";
  105. }
  106. $results = $db->fetch($sql);
  107. if($db->num($sql) == 1)
  108. {
  109. $db_password = $results['password'];
  110. $name = $results['name'];
  111. if(md5($password . PASSWORD_SALT) == $db_password)
  112. {
  113. $_SESSION['auth_email'] = $email;
  114. $_SESSION['auth_secret'] = md5($password . PASSWORD_SALT);
  115. setcookie("auth_email", $email, time()+60*60*24*30, "/", COOKIE_DOMAIN);
  116. setcookie("auth_secret", md5($password . PASSWORD_SALT), time()+60*60*24*30, "/", COOKIE_DOMAIN);
  117. $this->user_id = $results['user_id'];
  118. $this->name = $name;
  119. $this->email = $email;
  120. $this->ok = true;
  121. $this->msg = "Login Successful!";
  122. $this->is_logged = true;
  123. return true;
  124. }else{
  125. $this->msg = "Error! Password is incorrect.";
  126. }
  127. }else{
  128. $this->msg = "Error! User does not exist.";
  129. }
  130. return false;
  131. }
  132. function check($email, $secret){
  133. global $db;
  134. $sql = $db->query("SELECT user_id, password, name FROM users WHERE email = '".mysql_real_escape_string($email)."'");
  135. $results = $db->fetch($sql);
  136. if($db->num($sql) == 1)
  137. {
  138. $db_password = $results['password'];
  139. $name = $results['name'];
  140. if($db_password == $secret) {
  141. $this->user_id = $results['user_id'];
  142. $this->email = $email;
  143. $this->name = $name;
  144. $this->ok = true;
  145. $this->is_logged = true;
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. function is_logged(){
  152. if($this->check($_SESSION['auth_email'], $_SESSION['auth_secret'])) return true;
  153. else return false;
  154. }
  155. function is_admin(){
  156. if($this->is_logged() && $this->get_info('admin') == 1) return true;
  157. else return false;
  158. }
  159. function get_info($field = "*", $email = null){
  160. global $db;
  161. if(!$email) $email = $this->email;
  162. $sql = $db->query("SELECT $field FROM users WHERE email = '$email'");
  163. $info = $db->fetch($sql);
  164. if($field == "*") return $info;
  165. else return $info[$field];
  166. }
  167. function logout(){
  168. $this->user_id = 0;
  169. $this->email = "Guest";
  170. $this->name = "Guest";
  171. $this->ok = true;
  172. $this->msg = "You have been logged out!";
  173. $this->is_logged = false;
  174. $_SESSION['auth_email'] = "";
  175. $_SESSION['auth_secret'] = "";
  176. setcookie("auth_email", "", time() - 3600, "/", COOKIE_DOMAIN);
  177. setcookie("auth_secret", "", time() - 3600, "/", COOKIE_DOMAIN);
  178. }
  179. // Courtesy LinuxJournal.com : http://www.linuxjournal.com/article/9585?page=0,3
  180. function validEmail($email){
  181. $isValid = true;
  182. $atIndex = strrpos($email, "@");
  183. if (is_bool($atIndex) && !$atIndex){
  184. $isValid = false;
  185. }
  186. else{
  187. $domain = substr($email, $atIndex+1);
  188. $local = substr($email, 0, $atIndex);
  189. $localLen = strlen($local);
  190. $domainLen = strlen($domain);
  191. if ($localLen < 1 || $localLen > 64){
  192. $isValid = false;
  193. }else if ($domainLen < 1 || $domainLen > 255){
  194. $isValid = false;
  195. }else if ($local[0] == '.' || $local[$localLen-1] == '.'){
  196. $isValid = false;
  197. }else if (preg_match('/\\.\\./', $local)){
  198. $isValid = false;
  199. }else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)){
  200. $isValid = false;
  201. }else if (preg_match('/\\.\\./', $domain)){
  202. $isValid = false;
  203. }else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))){
  204. if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))){
  205. $isValid = false;
  206. }
  207. }
  208. if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))){
  209. $isValid = false;
  210. }
  211. }
  212. return $isValid;
  213. }
  214. function user_table($all = false, $start = 1, $limit = 20, $sort_by = "user_id DESC"){
  215. global $user, $db;
  216. $query = "SELECT * FROM users";
  217. if(!$all) $query .= " WHERE user_id = '".$user->user_id."'";
  218. $this->total = $db->num($db->query($query));
  219. $query .= " ORDER BY $sort_by LIMIT $start, $limit";
  220. if($this->total >= 1){
  221. $result = $db->query($query);
  222. $str .= '<ul class="order-list"><li class="title"><span class="c2">Name</span><span class="c3">Email</span><span class="c2">Joined On</span><span class="c1">Status</span></li>';
  223. while($row = $db->fetch($result)){
  224. $str .= '<li><span class="c2">'.$row['name'].'</span><span class="c3">'.$row['email'].'</span><span class="c2">'.date("M j Y",strtotime($row['created_on'])).'</span><span class="c1">';
  225. if($all) $str .= '<a href="admin/users/edit/'.$row['user_id'].'">'.$this->status($row['status']).'</a>';
  226. else $str .= $this->status($row['status']);
  227. $str .= '</span></li>';
  228. }
  229. $str .= '</ul>';
  230. }else
  231. $str = "No users found.";
  232. return $str;
  233. }
  234. function status($value){
  235. switch($value){
  236. case 0:
  237. return 'Inactive';
  238. break;
  239. case 1:
  240. return 'Active';
  241. break;
  242. case 2:
  243. return 'Banned';
  244. break;
  245. default:
  246. return 'Inactive';
  247. break;
  248. }
  249. }
  250. function user_info($user_id){
  251. global $user, $db;
  252. $query = "SELECT * FROM users WHERE user_id = '".$user_id."'";
  253. $result = $db->query($query);
  254. $info = $db->fetch($result);
  255. return $info;
  256. }
  257. function user_update($info, $user_id){
  258. global $db;
  259. $query = "UPDATE users SET status='".$info['status']."', name='".$info['name']."', email='".$info['email']."', admin='".$info['admin']."'";
  260. if($info['password']){
  261. $password = md5(mysql_real_escape_string($info['password']) . PASSWORD_SALT);
  262. $query .= ", password='".$password."'";
  263. }
  264. $query .= " WHERE user_id = '".$user_id."'";
  265. if($db->query($query)) return true;
  266. else return false;
  267. }
  268. }