Guest User

class.user.php

a guest
Nov 2nd, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 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. $stmt = $this->conn->prepare("INSERT INTO tbl_users(userName,userEmail,userPass, tokenCode, phone, street_address, street_address_2 , city , state , zip_code , country , portfolio)
  36. VALUES(:user_name, :user_mail, :user_pass, :active_code, :phone , :street_address, :street_address_2 , :city , :state , :zip_code , :country, :portfolio) ;");
  37. $stmt->execute(array(
  38. ":user_name" => $uname,
  39. ":user_mail" => $email,
  40. ":user_pass" => $password,
  41. ":active_code" => $code,
  42. ":phone" => $phone,
  43. ":street_address" => $street_address,
  44. ":street_address_2" => $street_address_2,
  45. ":city" => $city,
  46. ":state" => $state,
  47. ":zip_code" => $zip_code,
  48. ":country" => $country,
  49. ":portfolio" => $portfolio
  50. ));
  51. return $stmt;
  52. } catch (PDOException $ex) {
  53. echo $ex->getMessage();
  54. }
  55. }
  56.  
  57. /* php */
  58.  
  59. public function update($uname, $email, $phone, $street_address, $street_address_2, $city, $state, $zip_code, $country, $sold_by,
  60. $portfolio, $paypal_email_id, $account_holder_name, $account_number, $branch_name, $bank_name, $ifsc_code)
  61. {
  62. try {
  63. $stmt = $this->conn->prepare('UPDATE tbl_users SET userName = ?, userEmail = ?, phone = ?, street_address = ? , street_address_2 = ?
  64. , city = ? , state = ? , zip_code = ? , country = ? , sold_by = ? , portfolio = ? , paypal_email_id = ? , account_holder_name = ? ,
  65. account_number = ?, branch_name = ? , bank_name =? , ifsc_code =? WHERE userID = ? ');
  66. return $stmt->execute(array($uname, $email, $phone, $street_address, $street_address_2, $city, $state, $zip_code, $country, $sold_by,
  67. $portfolio, $paypal_email_id, $account_holder_name, $account_number, $branch_name, $bank_name, $ifsc_code, $_SESSION['userSession']));
  68. } catch (PDOException $e) {
  69. echo '<p class="bg-danger">' . $e->getMessage() . '</p>';
  70. }
  71. }
  72.  
  73. /*php end */
  74.  
  75.  
  76. const PATH_PHOTOS = '/var/www/html/sbdev2/php/site3/upload/';
  77. const BASE_URL = 'http://sbdev2.kidsdial.com:81/php/site3/';
  78.  
  79. public function add_photo($file)
  80. {
  81. $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
  82. $file['new_name'] = uniqid(rand(), true) . ".$ext";
  83. if (!$this->_upload_file($file))
  84. return false;
  85. return $this->_remove_previous_photo()->_add_file_to_db(self::PATH_PHOTOS . basename($file['new_name']));
  86. }
  87.  
  88. protected function _remove_previous_photo()
  89. {
  90. $photo = $this->get_photo();
  91. if ($photo)
  92. unlink($photo);
  93. return $this;
  94. }
  95.  
  96. public function get_photo()
  97. {
  98. global $_SESSION;
  99. $stmt = $this->conn->prepare('SELECT photo FROM tbl_users WHERE userID = ? ');
  100. $stmt->execute(array($_SESSION['userSession']));
  101. $result = $stmt->fetch();
  102. return reset($result);
  103. }
  104.  
  105. public function get_photo_url()
  106. {
  107. $pathInfo = pathinfo($this->get_photo());
  108. $last_dir = end(explode(DIRECTORY_SEPARATOR, $pathInfo['dirname']));
  109. return self::BASE_URL . "$last_dir/" . basename($this->get_photo());
  110. }
  111.  
  112. protected function _upload_file($file)
  113. {
  114. $uploadfile = self::PATH_PHOTOS . $file['new_name'];
  115. return move_uploaded_file($file['tmp_name'], $uploadfile);
  116. }
  117.  
  118. protected function _add_file_to_db($file_path)
  119. {
  120. try {
  121. $stmt = $this->conn->prepare('UPDATE tbl_users SET photo = ? WHERE userID = ? ');
  122. return $stmt->execute(array($file_path, $_SESSION['userSession']));
  123. } catch (PDOException $e) {
  124. echo '<p class="bg-danger">' . $e->getMessage() . '</p>';
  125. }
  126. }
  127.  
  128.  
  129. public function login($email, $upass)
  130. {
  131. try {
  132. $stmt = $this->conn->prepare("SELECT * FROM tbl_users WHERE userEmail=:email_id");
  133. $stmt->execute(array(":email_id" => $email));
  134. $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
  135.  
  136. if ($stmt->rowCount() == 1) {
  137. if ($userRow['userStatus'] == "Y") {
  138. if ($userRow['userPass'] == md5($upass)) {
  139. $_SESSION['userSession'] = $userRow['userID'];
  140. return true;
  141. } else {
  142. header("Location: index.php?error");
  143. exit;
  144. }
  145. } else {
  146. header("Location: index.php?inactive");
  147. exit;
  148. }
  149. } else {
  150. header("Location: index.php?error");
  151. exit;
  152. }
  153. } catch (PDOException $ex) {
  154. echo $ex->getMessage();
  155. }
  156. }
  157.  
  158.  
  159. public function is_logged_in()
  160. {
  161. if (isset($_SESSION['userSession'])) {
  162. return true;
  163. }
  164. }
  165.  
  166. public function redirect($url)
  167. {
  168. header("Location: $url");
  169. }
  170.  
  171. public function logout()
  172. {
  173. session_destroy();
  174. $_SESSION['userSession'] = false;
  175. }
  176.  
  177. function send_mail($email, $message, $subject)
  178. {
  179. require_once('mailer/class.phpmailer.php');
  180. $mail = new PHPMailer();
  181. $mail->IsSMTP();
  182. $mail->SMTPDebug = 0;
  183. $mail->SMTPAuth = true;
  184. $mail->SMTPSecure = "ssl";
  185. $mail->Host = "smtp.gmail.com";
  186. $mail->Port = 465;
  187. $mail->AddAddress($email);
  188. $mail->Username = "kidsdial5@gmail.com";
  189. $mail->Password = "5dialkids";
  190. $mail->SetFrom('kidsdial5@gmail.com', 'stylebaby1');
  191. $mail->AddReplyTo("kidsdial5@gmail.com", "stylebaby2");
  192. $mail->Subject = $subject;
  193. $mail->MsgHTML($message);
  194. $mail->Send();
  195. }
  196. }
Add Comment
Please, Sign In to add comment