Advertisement
Guest User

lars

a guest
Apr 13th, 2017
105
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 strcmp($data['userpassword_2'],$data['userpassword_1']) )
  140. {
  141. $passwordDontChange = false;
  142. }
  143.  
  144.  
  145. if($current_password)
  146. {
  147.  
  148. if ($passwordDontChange)
  149. {
  150. $query = "UPDATE anunciante SET NOME='".$data['username']."', TELEFONE= '".$data['userfone']."' WHERE USER_ID = ".$_SESSION['idSessao'];
  151. }
  152. else
  153. {
  154. $query = "UPDATE anunciante SET NOME='".$data['username']."', SENHA='".$data['userpassword_1']."',TELEFONE= '".$data['userfone']."' WHERE USER_ID = ".$_SESSION['idSessao'];
  155. $_SESSION['senhaSessao'] = $data['userpassword_1'];
  156. }
  157.  
  158. $result = mysqli_query ($this->connection, $query);
  159.  
  160. if (!$result)
  161. {
  162. echo "Query não deu certo!" . "<br>". $query;
  163. die ();
  164. }
  165.  
  166. $_SESSION['usuarioSessao'] = $data['username'];
  167. $_SESSION['telefoneSessao'] = $data['userfone'];
  168. mysqli_close ($connection);
  169. header ('Location: ../index.html');
  170. die();
  171.  
  172. }
  173.  
  174. die ();
  175.  
  176. }
  177.  
  178. public function search ($data)
  179. {
  180. // Usa o email buscado p/ procurar por usuario
  181. $emailSearch = $data['emailSearch'];
  182. $sqlAnunciante = "SELECT ID, NOME, EMAIL
  183. FROM ANUNCIANTE
  184. WHERE EMAIL = '".$emailSearch."' ";
  185. $resultAnunciante = mysqli_query($this->conn, $sqlAnunciante) or die(mysql_error());
  186.  
  187. //retorna um resultado e armazena em um vetor
  188. $row = mysqli_affected_rows($this->conn, $resultAnunciante);
  189.  
  190. $resultID = $row[0];
  191. $resultName = $row[1];
  192. $resultEmail = $row[2];
  193.  
  194. //usa ID do usuário para pesquisar anuncios relacionados
  195. $sqlAnuncio = "SELECT TITULO, DESCRICAO, IMAGEM
  196. FROM ANUNCIOS
  197. WHERE USER_ID = '".$resultID."' ";
  198. $resultAnuncio = mysql_query($sqlAnuncio) or die(mysql_error());
  199.  
  200. //retorna vários resultados
  201. while($rowAnuncio = mysql_affected_rows($resultAnuncio))
  202. {
  203. $resultTitulo = $rowAnuncio[0];
  204. $resultDescrição = $rowAnuncio[1];
  205. $resultImagem = $rowImagem[2];
  206. }
  207. }
  208.  
  209. public function delet ($data)
  210. {
  211. session_start();
  212. $passwdConf = MD5 ($data['password']);
  213. if($passwdConf == $_SESSION['senhaSessao'])
  214. {
  215. $queryUser = "DELETE
  216. FROM Anunciante
  217. WHERE USER_ID = '".$_SESSION['idSessao']."'";
  218. $resultUser = mysqli_query ($this->connection, $queryUser);
  219. $queryAnuncio = "DELETE
  220. FROM Anuncios
  221. WHERE USER_ID = '".$_SESSION['idSessao']."'";
  222. $resultAnuncio = mysqli_query ($this->connection, $queryAnuncio);
  223. if(!$resultUser)
  224. {
  225. echo "<script language='javascript' type='text/javascript'>alert('Impossivel excluir usuário.');window.location.href='../exclui_conta.php';</script>";
  226. }
  227. else
  228. {
  229. session_destroy();
  230. header ('Location: ../index.html');
  231. }
  232.  
  233. }
  234. else
  235. {
  236. echo "<script language='javascript' type='text/javascript'>alert('Senha incorreta.');window.location.href='../exclui_conta.php';</script>";
  237. }
  238. }
  239. }
  240.  
  241.  
  242. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement