Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once ('../_model/mysql_connect.php');
- class UserController
- {
- private $connection;
- public function __construct ($connection) { $this->connection = $connection; }
- public function index ()
- {
- $users = User::all ();
- }
- public function create ($data)
- {
- // Nada de novo aqui, query no banco de dados
- session_start();
- $name = $data['username'];
- $email = $data['useremail'];
- $password = MD5($data['userpassword_1']);
- $fone = $data['userfone'];
- $_SESSION["formname"] = $name;
- $_SESSION["formemail"] = $email;
- $_SESSION["formfone"] = $fone;
- /*
- * Começo da validação redundante:
- * Se chegar nesse ponto, e não passar pela validação,
- * quer dizer que o JS foi adulterado. Just in case.
- */
- if(!isset($name) || $name == "")
- {
- echo "<script type=\"text/javascript\"> alert(\"O campo Nome não pode ficar vazio!\"); window.location.href = \"../create_account.php\"</script>";
- die();
- }
- else if(!isset($email) || $email == "")
- {
- echo "<script type=\"text/javascript\"> alert(\"O campo Email não pode ficar vazio!\"); window.location.href = \"../create_account.php\"</script>";
- die();
- }
- else if(!isset($data['userpassword_1'])||$data['userpassword_1']=="")
- {
- echo "<script type=\"text/javascript\"> alert(\"O campo Senha não pode ficar vazio!\"); window.location.href = \"../create_account.php\"</script>";
- die();
- }
- else if($data['userpassword_1'] != $data['userpassword_2'])
- {
- echo "<script type=\"text/javascript\"> alert(\"As senhas devem ser iguais\"); window.location.href = \"../create_account.php\"</script>";
- die();
- }
- $querySelect = "SELECT EMAIL "
- . "FROM Anunciante "
- . "WHERE EMAIL = '".$email."'";
- $resultSelect = mysqli_query ($this->connection, $querySelect);
- if (mysqli_num_rows($resultSelect)===0)
- {
- $query = "INSERT INTO Anunciante (NOME,EMAIL,SENHA,TELEFONE) VALUES ('". $name . "','" . $email . "','".$password."','".$fone."')";
- $result = mysqli_query ($this->connection, $query);
- if (!$result)
- {
- throw new Exception ("Unable to insert into database.");
- }
- }
- else
- {
- echo "<script type=\"text/javascript\"> alert(\"Email já cadastrado!\"); window.location.href = \"../create_account.php\"</script>";
- die();
- }
- $this->login ($data);
- header ('Location: ../index.html');
- die ();
- }
- public function login ($data)
- {
- $email = $data['useremail'];
- $password = MD5($data['userpassword_1']);
- $query = "SELECT * FROM Anunciante WHERE EMAIL = '".$email."' AND SENHA = '".$password."' LIMIT 1";
- $result = mysqli_query ($this->connection, $query);
- if ((mysqli_num_rows($result)) < 1)
- {
- //header('Location: ../index.html');
- echo "<script>alert ('Senha ou e-mail incorreto!');"
- . "window.location.href='http://localhost/login.html'</script>";
- }
- else
- {
- session_start ();
- echo "Iniciou a sessão";
- $rows = mysqli_fetch_assoc ($result);
- $_SESSION['usuarioSessao'] = $rows['NOME'];
- $_SESSION['emailSessao'] = $rows['EMAIL'];
- $_SESSION['idSessao'] = $rows['USER_ID'];
- $_SESSION['telefoneSessao'] = $rows['TELEFONE'];
- $_SESSION['senhaSessao'] = $rows['SENHA'];
- header('Location: ../index.html');
- }
- mysqli_close ($connection);
- die ();
- }
- public function logout ($data)
- {
- session_start ();
- session_destroy ();
- header('Location: ../index.html');
- }
- public function edit ($data)
- {
- // Validação dos dados
- session_start ();
- $passwordDontChange = true;
- $query = "";
- $current_password = false;
- $data['pass_current'] = MD5 ($data['pass_current']);
- $data['userpassword_1'] = MD5 ($data['userpassword_1']);
- $data['userpassword_2'] = MD5 ($data['userpassword_1']);
- if (!(isset($data['username'])))
- {
- echo "Campo de usuário vazio!";
- }
- if ((!(isset($data['pass_current']))) or ($data['pass_current'] != $_SESSION['senhaSessao']))
- {
- echo "<br>Senha Atual:" . $data['pass_current'] . "<br>";
- echo $_SESSION['senhaSessao'];
- echo "Senha atual errada!";
- }
- else
- {
- $current_password = true;
- }
- if (!(isset($data['userfone'])))
- {
- $data['userfone'] = "";
- }
- if (isset($data['userpassword_1']) and (isset($data['userpassword_2'])) and strcmp($data['userpassword_2'],$data['userpassword_1']) )
- {
- $passwordDontChange = false;
- }
- if($current_password)
- {
- if ($passwordDontChange)
- {
- $query = "UPDATE anunciante SET NOME='".$data['username']."', TELEFONE= '".$data['userfone']."' WHERE USER_ID = ".$_SESSION['idSessao'];
- }
- else
- {
- $query = "UPDATE anunciante SET NOME='".$data['username']."', SENHA='".$data['userpassword_1']."',TELEFONE= '".$data['userfone']."' WHERE USER_ID = ".$_SESSION['idSessao'];
- $_SESSION['senhaSessao'] = $data['userpassword_1'];
- }
- $result = mysqli_query ($this->connection, $query);
- if (!$result)
- {
- echo "Query não deu certo!" . "<br>". $query;
- die ();
- }
- $_SESSION['usuarioSessao'] = $data['username'];
- $_SESSION['telefoneSessao'] = $data['userfone'];
- mysqli_close ($connection);
- header ('Location: ../index.html');
- die();
- }
- die ();
- }
- public function search ($data)
- {
- // Usa o email buscado p/ procurar por usuario
- $emailSearch = $data['emailSearch'];
- $sqlAnunciante = "SELECT ID, NOME, EMAIL
- FROM ANUNCIANTE
- WHERE EMAIL = '".$emailSearch."' ";
- $resultAnunciante = mysqli_query($this->conn, $sqlAnunciante) or die(mysql_error());
- //retorna um resultado e armazena em um vetor
- $row = mysqli_affected_rows($this->conn, $resultAnunciante);
- $resultID = $row[0];
- $resultName = $row[1];
- $resultEmail = $row[2];
- //usa ID do usuário para pesquisar anuncios relacionados
- $sqlAnuncio = "SELECT TITULO, DESCRICAO, IMAGEM
- FROM ANUNCIOS
- WHERE USER_ID = '".$resultID."' ";
- $resultAnuncio = mysql_query($sqlAnuncio) or die(mysql_error());
- //retorna vários resultados
- while($rowAnuncio = mysql_affected_rows($resultAnuncio))
- {
- $resultTitulo = $rowAnuncio[0];
- $resultDescrição = $rowAnuncio[1];
- $resultImagem = $rowImagem[2];
- }
- }
- public function delet ($data)
- {
- session_start();
- $passwdConf = MD5 ($data['password']);
- if($passwdConf == $_SESSION['senhaSessao'])
- {
- $queryUser = "DELETE
- FROM Anunciante
- WHERE USER_ID = '".$_SESSION['idSessao']."'";
- $resultUser = mysqli_query ($this->connection, $queryUser);
- $queryAnuncio = "DELETE
- FROM Anuncios
- WHERE USER_ID = '".$_SESSION['idSessao']."'";
- $resultAnuncio = mysqli_query ($this->connection, $queryAnuncio);
- if(!$resultUser)
- {
- echo "<script language='javascript' type='text/javascript'>alert('Impossivel excluir usuário.');window.location.href='../exclui_conta.php';</script>";
- }
- else
- {
- session_destroy();
- header ('Location: ../index.html');
- }
- }
- else
- {
- echo "<script language='javascript' type='text/javascript'>alert('Senha incorreta.');window.location.href='../exclui_conta.php';</script>";
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement