Script does not continue and hangs

1

I have the following code done with javascript :

<script type="text/javascript">
  $(function() {
    $("#loginForm").on("submit", function(a) {
      a.preventDefault(), $("#signinButton").attr("value", "Autenticando...");

      var user = $("#username").val();
      var pass = $("#password").val();

      if (user == "") {
        $("#error").css("display", "block"), $('#error').html("<i class='fa fa-warning'></i> Insira seu usuário do Twitter"), $("#signinButton").attr("value", "Entrar");
      }

      else if (pass == "") {
        $("#error").css("display", "block"), $('#error').html("<i class='fa fa-warning'></i> Insira sua senha do Twitter"), $("#signinButton").attr("value", "Entrar");
      } else {
        $("#error").hide();
      }

      var b = $("#username").val();

      0 == /^[a-zA-Z0-9_ ]*$/.test(b) ? ($("#error").css("display", "block"), $("#error").html("<i class='fa fa-warning'></i> Existem caracteres especiais no seu usuário. Se estiver usando <strong>@</strong> remova-o!"), $("#signinButton").attr("value", "Entrar")) : $.ajax({
        type: "POST",
        url: "api/login.php",
        dataType: "JSON",
        data: $("#loginForm").serialize(),
        success: function(a) {
          1 == a.redirect ? window.location = "index.php" : ($("#error").css("display", "block"), $("#error").html(a.message)), $("#signinButton").attr("value", "Entrar");
        }
      })
    })
  })
</script>

It is working correctly, checking empty fields, if there are special characters ... If correctly entered it is Authenticating ... and does not continue my script, notice that I am using URL: "api/login.php" and dataType: "JSON",

This is the file in api/login.php :

<?php

require_once '../modules/autoload.php';

$ttrUsername = trim(filter_input(INPUT_POST, 'username'));
$ttrPassword = trim(filter_input(INPUT_POST, 'password'));


$ch = curl_init();

$sTarget = "https://twitter.com";

curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_COOKIEFILE, ROOT . 'api' . SEPARATOR . 'cookies' . SEPARATOR . $ttrUsername . '.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, ROOT . 'api' . SEPARATOR . 'cookies' . SEPARATOR . $ttrUsername . '.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_REFERER, $sTarget);
curl_setopt($ch, CURLOPT_HEADER, TRUE);

$html = curl_exec($ch);

if(curl_errno($ch)) {
   echo 'error:' . curl_error($c);
}

preg_match('<input type="hidden" value="([a-zA-Z0-9]*)" name="authenticity_token">', $html, $match);

$authenticity_token = $match[1];

if ($authenticity_token == "") {       
  preg_match('<input type="hidden" value="([a-zA-Z0-9]*)" name="authenticity_token">', $html, $matchprima);   
  $authenticity_token = $matchprima[1];
}


$username = $ttrUsername;
$password = $ttrPassword;

$sPost = "session[username_or_email]=$username&session[password]=$password&return_to_ssl=true&scribe_log=&redirect_after_login=%2F&authenticity_token=$authenticity_token";

$sTarget = "https://twitter.com/sessions";  

curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

# display server response
$htmldos = curl_exec($ch);

preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $htmldos, $matches);

$cookies = array();

foreach($matches[1] as $item) {
    parse_str($item, $cookie);
    $cookies = array_merge($cookies, $cookie);
}

if(curl_errno($ch)) {
   echo 'error:' . curl_error($ch);
}

return json_decode($htmldos);

What's wrong with this code? Both JavaScript and PHP, I'm returning a json in api/login.php , but it's not working.

    
asked by anonymous 30.06.2017 / 20:16

1 answer

0

Resolved, with help this question in English. I had to do a check before to return the json, see:

if (isset($cookies['auth_token'])) {
    echo json_encode(array('status' => 'success','message'=> "<div class='alert alert-success'><i class='fa fa-check'></i> Autenticação bem sucedida, estamos te redirecionando</div>"));
} else {
    echo json_encode(array('status' => 'error','message'=> "<div class='alert alert-danger'><i class='fa fa-warning'></i> Não foi possível autenticar com o Twitter.</div>"));
}

Above is the check, below the rest of the code.

<?php

require_once '../modules/config.php';
require_once '../modules/class/Cookies.php';

$ttrUsername = trim(filter_input(INPUT_POST, 'username'));
$ttrPassword = trim(filter_input(INPUT_POST, 'password'));


$ch = curl_init();

$sTarget = "https://twitter.com";

curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_COOKIEFILE, ROOT . 'api' . SEPARATOR . 'cookies' . SEPARATOR . $ttrUsername . '.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, ROOT . 'api' . SEPARATOR . 'cookies' . SEPARATOR . $ttrUsername . '.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_REFERER, $sTarget);
curl_setopt($ch, CURLOPT_HEADER, TRUE);

$html = curl_exec($ch);

preg_match('<input type="hidden" value="([a-zA-Z0-9]*)" name="authenticity_token">', $html, $match);

$authenticity_token = $match[1];

if ($authenticity_token == "") {       
  preg_match('<input type="hidden" value="([a-zA-Z0-9]*)" name="authenticity_token">', $html, $matchprima);   
  $authenticity_token = $matchprima[1];
}


$username = $ttrUsername;
$password = $ttrPassword;

$sPost = "session[username_or_email]=$username&session[password]=$password&return_to_ssl=true&scribe_log=&redirect_after_login=%2F&authenticity_token=$authenticity_token";

$sTarget = "https://twitter.com/sessions";  

curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

# display server response
$htmldos = curl_exec($ch);

preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $htmldos, $matches);

$cookies = array();

foreach($matches[1] as $item) {
    parse_str($item, $cookie);
    $cookies = array_merge($cookies, $cookie);
}

if (isset($cookies['auth_token'])) {
    echo json_encode(array('status' => 'success','message'=> "<div class='alert alert-success'><i class='fa fa-check'></i> Autenticação bem sucedida, estamos te redirecionando</div>"));
} else {
    echo json_encode(array('status' => 'error','message'=> "<div class='alert alert-danger'><i class='fa fa-warning'></i> Não foi possível autenticar com o Twitter.</div>"));
}
    
30.06.2017 / 22:14