Advertisement
Guest User

lars

a guest
Apr 12th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1. <?php
  2.  
  3. require_once ('../_model/mysql_connect.php');
  4.  
  5. class UserController
  6. {
  7. private $connection;
  8.  
  9. public function __construct ($connection) { $this->connection = $connection; }
  10.  
  11. public function index ()
  12. {
  13. $users = User::all ();
  14. }
  15.  
  16. public function create ($data)
  17. {
  18. // Nada de novo aqui, query no banco de dados
  19. session_start();
  20. $name = $data['username'];
  21. $email = $data['useremail'];
  22. $password = MD5($data['userpassword_1']);
  23. $fone = $data['userfone'];
  24. $_SESSION["formname"] = $name;
  25. $_SESSION["formemail"] = $email;
  26. $_SESSION["formfone"] = $fone;
  27. /*
  28. * Começo da validação redundante:
  29. * Se chegar nesse ponto, e não passar pela validação,
  30. * quer dizer que o JS foi adulterado. Just in case.
  31. */
  32. if(!isset($name) || $name == "")
  33. {
  34. echo "<script type=\"text/javascript\"> alert(\"O campo Nome não pode ficar vazio!\"); window.location.href = \"../create_account.php\"</script>";
  35. die();
  36. }
  37. else if(!isset($email) || $email == "")
  38. {
  39. echo "<script type=\"text/javascript\"> alert(\"O campo Email não pode ficar vazio!\"); window.location.href = \"../create_account.php\"</script>";
  40. die();
  41. }
  42. else if(!isset($data['userpassword_1'])||$data['userpassword_1']=="")
  43. {
  44. echo "<script type=\"text/javascript\"> alert(\"O campo Senha não pode ficar vazio!\"); window.location.href = \"../create_account.php\"</script>";
  45. die();
  46. }
  47. else if($data['userpassword_1'] != $data['userpassword_2'])
  48. {
  49. echo "<script type=\"text/javascript\"> alert(\"As senhas devem ser iguais\"); window.location.href = \"../create_account.php\"</script>";
  50. die();
  51. }
  52. $querySelect = "SELECT EMAIL "
  53. . "FROM Anunciante "
  54. . "WHERE EMAIL = '".$email."'";
  55. $resultSelect = mysqli_query ($this->connection, $querySelect);
  56. if (mysqli_num_rows($resultSelect)===0)
  57. {
  58. $query = "INSERT INTO Anunciante (NOME,EMAIL,SENHA,TELEFONE) VALUES ('". $name . "','" . $email . "','".$password."','".$fone."')";
  59. $result = mysqli_query ($this->connection, $query);
  60. if (!$result)
  61. {
  62. throw new Exception ("Unable to insert into database.");
  63. }
  64. }
  65. else
  66. {
  67.  
  68. echo "<script type=\"text/javascript\"> alert(\"Email já cadastrado!\"); window.location.href = \"../create_account.php\"</script>";
  69. die();
  70. }
  71. $this->login ($data);
  72. header ('Location: ../index.html');
  73. die ();
  74. }
  75.  
  76. public function login ($data)
  77. {
  78. $email = $data['useremail'];
  79. $password = MD5($data['userpassword_1']);
  80. $query = "SELECT * FROM Anunciante WHERE EMAIL = '".$email."' AND SENHA = '".$password."' LIMIT 1";
  81. $result = mysqli_query ($this->connection, $query);
  82. if ((mysqli_num_rows($result)) < 1)
  83. {
  84. //header('Location: ../index.html');
  85. echo "<script>alert ('Senha ou e-mail incorreto!');"
  86. . "window.location.href='http://localhost/login.html'</script>";
  87. }
  88. else
  89. {
  90. session_start ();
  91. echo "Iniciou a sessão";
  92. $rows = mysqli_fetch_assoc ($result);
  93. $_SESSION['usuarioSessao'] = $rows['NOME'];
  94. $_SESSION['emailSessao'] = $rows['EMAIL'];
  95. $_SESSION['idSessao'] = $rows['USER_ID'];
  96. $_SESSION['telefoneSessao'] = $rows['TELEFONE'];
  97. $_SESSION['senhaSessao'] = $rows['SENHA'];
  98. header('Location: ../index.html');
  99. }
  100. mysqli_close ($connection);
  101. die ();
  102. }
  103.  
  104. public function logout ($data)
  105. {
  106. session_start ();
  107. session_destroy ();
  108. header('Location: ../index.html');
  109. }
  110.  
  111. public function edit ($data)
  112. {
  113. // Validação dos dados
  114. session_start ();
  115. $passwordDontChange = true;
  116. $query = "";
  117. $current_password = false;
  118. $data['pass_current'] = MD5 ($data['pass_current']);
  119. $data['userpassword_1'] = MD5 ($data['userpassword_1']);
  120. $data['userpassword_2'] = MD5 ($data['userpassword_1']);
  121. if (!(isset($data['username'])))
  122. {
  123. echo "Campo de usuário vazio!";
  124. }
  125. if ((!(isset($data['pass_current']))) or ($data['pass_current'] != $_SESSION['senhaSessao']))
  126. {
  127. echo "<br>Senha Atual:" . $data['pass_current'] . "<br>";
  128. echo $_SESSION['senhaSessao'];
  129. echo "Senha atual errada!";
  130. }
  131. else
  132. {
  133. $current_password = true;
  134. }
  135. if (!(isset($data['userfone'])))
  136. {
  137. $data['userfone'] = "";
  138. }
  139. if (((isset($data['userpassword_1'])) and (isset($data['userpassword_2']))) and (($data['userpassword_2']) == ($data['userpassword_1'])) )
  140. {
  141. $passwordDontChange = false;
  142. }
  143.  
  144. if($current_password)
  145. {
  146.  
  147. if ($passwordDontChange)
  148. {
  149. $query = "UPDATE anunciante SET NOME='".$data['username']."', TELEFONE= '".$data['userfone']."' WHERE USER_ID = ".$_SESSION['idSessao'];
  150. }
  151. else
  152. {
  153. $query = "UPDATE anunciante SET NOME='".$data['username']."', SENHA='".$data['userpassword_1']."',TELEFONE= '".$data['userfone']."' WHERE USER_ID = ".$_SESSION['idSessao'];
  154. }
  155.  
  156. $result = mysqli_query ($this->connection, $query);
  157.  
  158. if (!$result)
  159. {
  160. echo "Query não deu certo!" . "<br>". $query;
  161. die ();
  162. }
  163.  
  164. $_SESSION['usuarioSessao'] = $data['username'];
  165. $_SESSION['telefoneSessao'] = $data['userfone'];
  166. $_SESSION['senhaSessao'] = $data['userpassword_1'];
  167. mysqli_close ($connection);
  168. header ('Location: ../index.html');
  169. die();
  170.  
  171. }
  172.  
  173. die ();
  174.  
  175. }
  176.  
  177. public function search ($data)
  178. {
  179. // Usa o email buscado p/ procurar por usuario
  180. $emailSearch = $data['emailSearch'];
  181. $sqlAnunciante = "SELECT ID, NOME, EMAIL
  182. FROM ANUNCIANTE
  183. WHERE EMAIL = '".$emailSearch."' ";
  184. $resultAnunciante = mysqli_query($this->conn, $sqlAnunciante) or die(mysql_error());
  185.  
  186. //retorna um resultado e armazena em um vetor
  187. $row = mysqli_affected_rows($this->conn, $resultAnunciante);
  188.  
  189. $resultID = $row[0];
  190. $resultName = $row[1];
  191. $resultEmail = $row[2];
  192.  
  193. //usa ID do usuário para pesquisar anuncios relacionados
  194. $sqlAnuncio = "SELECT TITULO, DESCRICAO, IMAGEM
  195. FROM ANUNCIOS
  196. WHERE USER_ID = '".$resultID."' ";
  197. $resultAnuncio = mysql_query($sqlAnuncio) or die(mysql_error());
  198.  
  199. //retorna vários resultados
  200. while($rowAnuncio = mysql_affected_rows($resultAnuncio))
  201. {
  202. $resultTitulo = $rowAnuncio[0];
  203. $resultDescrição = $rowAnuncio[1];
  204. $resultImagem = $rowImagem[2];
  205. }
  206. }
  207.  
  208. public function delet ($data)
  209. {
  210. session_start();
  211. $passwdConf = MD5 ($data['password']);
  212. if($passwdConf == $_SESSION['senhaSessao'])
  213. {
  214. $queryUser = "DELETE
  215. FROM Anunciante
  216. WHERE USER_ID = '".$_SESSION['idSessao']."'";
  217. $resultUser = mysqli_query ($this->connection, $queryUser);
  218. $queryAnuncio = "DELETE
  219. FROM Anuncios
  220. WHERE USER_ID = '".$_SESSION['idSessao']."'";
  221. $resultAnuncio = mysqli_query ($this->connection, $queryAnuncio);
  222. if(!$resultUser)
  223. {
  224. echo "<script language='javascript' type='text/javascript'>alert('Impossivel excluir usuário.');window.location.href='../exclui_conta.php';</script>";
  225. }
  226. else
  227. {
  228. session_destroy();
  229. header ('Location: ../index.html');
  230. }
  231.  
  232. }
  233. else
  234. {
  235. echo "<script language='javascript' type='text/javascript'>alert('Senha incorreta.');window.location.href='../exclui_conta.php';</script>";
  236. }
  237. }
  238. }
  239.  
  240.  
  241. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement