Guest User

class.user.php

a guest
Nov 2nd, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. <?php
  2.  
  3. require_once 'dbconfig.php';
  4. const PATH_PHOTOS = '/var/www/html/sbdev2/php/site3/upload/';
  5. global $_FILES;
  6.  
  7. class USER
  8. {
  9.  
  10. private $conn;
  11.  
  12. public function __construct()
  13. {
  14. $database = new Database();
  15. $db = $database->dbConnection();
  16. $this->conn = $db;
  17. }
  18.  
  19. public function runQuery($sql)
  20. {
  21. $stmt = $this->conn->prepare($sql);
  22. return $stmt;
  23. }
  24.  
  25. public function lasdID()
  26. {
  27. $stmt = $this->conn->lastInsertId();
  28. return $stmt;
  29. }
  30.  
  31. public function register($uname, $email, $upass, $code, $phone, $street_address, $street_address_2, $city, $state, $zip_code, $country , $portfolio)
  32. {
  33. try {
  34. // $password = md5($upass);
  35. $password = $_POST["upass"];
  36. // $hash = password_hash($upass, PASSWORD_DEFAULT);
  37. $password = password_hash('upass', PASSWORD_DEFAULT);
  38. $stmt = $this->conn->prepare("INSERT INTO tbl_users(userName,userEmail,userPass, tokenCode, phone, street_address, street_address_2 , city , state , zip_code , country , portfolio)
  39. VALUES(:user_name, :user_mail, :user_pass, :active_code, :phone , :street_address, :street_address_2 , :city , :state , :zip_code , :country, :portfolio) ;");
  40. $stmt->execute(array(
  41. ":user_name" => $uname,
  42. ":user_mail" => $email,
  43. ":user_pass" => $password,
  44. ":active_code" => $code,
  45. ":phone" => $phone,
  46. ":street_address" => $street_address,
  47. ":street_address_2" => $street_address_2,
  48. ":city" => $city,
  49. ":state" => $state,
  50. ":zip_code" => $zip_code,
  51. ":country" => $country,
  52. ":portfolio" => $portfolio
  53. ));
  54. return $stmt;
  55. } catch (PDOException $ex) {
  56. echo $ex->getMessage();
  57. }
  58. }
  59.  
  60. /* php */
  61.  
  62. public function update($uname, $email, $phone, $street_address, $street_address_2, $city, $state, $zip_code, $country, $sold_by,
  63. $portfolio, $paypal_email_id, $account_holder_name, $account_number, $branch_name, $bank_name, $ifsc_code)
  64. {
  65. try {
  66. $stmt = $this->conn->prepare('UPDATE tbl_users SET userName = ?, userEmail = ?, phone = ?, street_address = ? , street_address_2 = ?
  67. , city = ? , state = ? , zip_code = ? , country = ? , sold_by = ? , portfolio = ? , paypal_email_id = ? , account_holder_name = ? ,
  68. account_number = ?, branch_name = ? , bank_name =? , ifsc_code =? WHERE userID = ? ');
  69. return $stmt->execute(array($uname, $email, $phone, $street_address, $street_address_2, $city, $state, $zip_code, $country, $sold_by,
  70. $portfolio, $paypal_email_id, $account_holder_name, $account_number, $branch_name, $bank_name, $ifsc_code, $_SESSION['userSession']));
  71. } catch (PDOException $e) {
  72. echo '<p class="bg-danger">' . $e->getMessage() . '</p>';
  73. }
  74. }
  75.  
  76. /*php end */
  77.  
  78.  
  79. const PATH_PHOTOS = '/var/www/html/sbdev2/php/site3/upload/';
  80. const BASE_URL = 'http://sbdev2.kidsdial.com:81/php/site3/';
  81.  
  82. public function add_photo($file)
  83. {
  84. $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
  85. $file['new_name'] = uniqid(rand(), true) . ".$ext";
  86. if (!$this->_upload_file($file))
  87. return false;
  88. return $this->_remove_previous_photo()->_add_file_to_db(self::PATH_PHOTOS . basename($file['new_name']));
  89. }
  90.  
  91. protected function _remove_previous_photo()
  92. {
  93. $photo = $this->get_photo();
  94. if ($photo)
  95. unlink($photo);
  96. return $this;
  97. }
  98.  
  99. public function get_photo()
  100. {
  101. global $_SESSION;
  102. $stmt = $this->conn->prepare('SELECT photo FROM tbl_users WHERE userID = ? ');
  103. $stmt->execute(array($_SESSION['userSession']));
  104. $result = $stmt->fetch();
  105. return reset($result);
  106. }
  107.  
  108. public function get_photo_url()
  109. {
  110. $pathInfo = pathinfo($this->get_photo());
  111. $last_dir = end(explode(DIRECTORY_SEPARATOR, $pathInfo['dirname']));
  112. return self::BASE_URL . "$last_dir/" . basename($this->get_photo());
  113. }
  114.  
  115. protected function _upload_file($file)
  116. {
  117. $uploadfile = self::PATH_PHOTOS . $file['new_name'];
  118. return move_uploaded_file($file['tmp_name'], $uploadfile);
  119. }
  120.  
  121. protected function _add_file_to_db($file_path)
  122. {
  123. try {
  124. $stmt = $this->conn->prepare('UPDATE tbl_users SET photo = ? WHERE userID = ? ');
  125. return $stmt->execute(array($file_path, $_SESSION['userSession']));
  126. } catch (PDOException $e) {
  127. echo '<p class="bg-danger">' . $e->getMessage() . '</p>';
  128. }
  129. }
  130.  
  131.  
  132. public function login($email, $upass)
  133. {
  134. try {
  135. $stmt = $this->conn->prepare("SELECT * FROM tbl_users WHERE userEmail=:email_id");
  136. $stmt->execute(array(":email_id" => $email));
  137. $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
  138. $password = password_hash('upass', PASSWORD_DEFAULT);
  139. if ($stmt->rowCount() == 1) {
  140. if ($userRow['userStatus'] == "Y") {
  141. // if ($userRow['userPass'] == md5($upass)) {
  142. if ($userRow['userPass'] == $_POST["upass"])
  143.  
  144.  
  145. {
  146.  
  147. $_SESSION['userSession'] = $userRow['userID'];
  148. return true;
  149. } else {
  150. header("Location: index.php?error");
  151. exit;
  152. }
  153. } else {
  154. header("Location: index.php?inactive");
  155. exit;
  156. }
  157. } else {
  158. header("Location: index.php?error");
  159. exit;
  160. }
  161. } catch (PDOException $ex) {
  162. echo $ex->getMessage();
  163. }
  164. }
  165.  
  166.  
  167. public function checkCredentials($email, $upass)
  168. {
  169. $user = $this->getUserByUsername($email);
  170. if (!$email) {
  171. // No user found with provided username
  172. return false;
  173. }
  174. if (!password_verify($password, $email['password'])) {
  175. // Password does not match
  176. return false;
  177. }
  178.  
  179. if (password_needs_rehash($email['password'], PASSWORD_DEFAULT)) {
  180. // This password was hashed using an older algorithm, update with new hash.
  181. $this->updatePassword($email['id'], $password);
  182. }
  183. // The password is no longer needed from the user data
  184. unset($email['password']);
  185. return $email;
  186. }
  187.  
  188.  
  189. public function is_logged_in()
  190. {
  191. if (isset($_SESSION['userSession'])) {
  192. return true;
  193. }
  194. }
  195.  
  196. public function redirect($url)
  197. {
  198. header("Location: $url");
  199. }
  200.  
  201. public function logout()
  202. {
  203. session_destroy();
  204. $_SESSION['userSession'] = false;
  205. }
  206.  
  207. function send_mail($email, $message, $subject)
  208. {
  209. require_once('mailer/class.phpmailer.php');
  210. $mail = new PHPMailer();
  211. $mail->IsSMTP();
  212. $mail->SMTPDebug = 0;
  213. $mail->SMTPAuth = true;
  214. $mail->SMTPSecure = "ssl";
  215. $mail->Host = "smtp.gmail.com";
  216. $mail->Port = 465;
  217. $mail->AddAddress($email);
  218. $mail->Username = "kidsdial5@gmail.com";
  219. $mail->Password = "5dialkids";
  220. $mail->SetFrom('kidsdial5@gmail.com', 'stylebaby1');
  221. $mail->AddReplyTo("kidsdial5@gmail.com", "stylebaby2");
  222. $mail->Subject = $subject;
  223. $mail->MsgHTML($message);
  224. $mail->Send();
  225. }
  226. }
Add Comment
Please, Sign In to add comment