<?php
preg_match("/^agrobr-app02\.*/", gethostname(), $matches);
$env = parse_ini_file(dirname(__DIR__).'/application/config/application.ini');

if (count($matches)>0) {
    set_include_path(get_include_path().':/var/www/html/library/');
} else {
    set_include_path(get_include_path().':/usr/share/php/:../library/');
}

require_once('Zend/Loader.php');
require_once('Zend/Http/Client.php');
require_once('Zend/Http/Client/Adapter/Curl.php');

if (count($matches>0)) {
    $curl_options = array(
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_SSL_VERIFYPEER => true,
    );
} else {
    $curl_options = array(
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_SSL_VERIFYPEER => true,
        CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1,
        CURLOPT_SSL_CIPHER_LIST => 'TLSv1'
    );
}

$config = array(
    'adapter' => 'Zend_Http_Client_Adapter_Curl',
    'ssltransport' => 'tls',
    'strict' => false,
    'persistent' => true,
    'curloptions' => $curl_options
);

if (count($matches)>0) {
    $host = 'agronet.agrobrasilseguros.com.br';
    $proto = 'https';
} else {
    $host = 'tonismar.agrobrasilseguros.com.br';
    $proto = 'http';
}

$url = $proto.'://'.$host.'/login';
$uri = $proto.'://'.$host.'/endosso/proposta/duplicarlote/id/'.$argv[1].'/nr_parcelas/'.$argv[2];

$client = new Zend_Http_Client($url, $config);
$postArray = array('usuario' => $env['constants.SYSTEM_USERNAME'], 'senha' => $env['constants.SYSTEM_PASSWORD']);
$client->setParameterPost($postArray);

$client->setHeaders(array(
    'Host: '.$host,
    'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0',
    'Accept: text/javascript, application/javascript, */*',
    'Accept-Language: en-gb,en;q=5.0',
    'Accept-Encoding: gzip, deflate',
    'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
    'X-Requested-With: XMLHttpRequest',
    'Referer: '.$proto.'://'.$host
));

$client->setCookie('__utma', '174057141.1507422797.1392357517.1392698281.1392702703.3');
$client->setCookie('__utmz', '174057141.1392357517.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)');
$client->setCookie('__atuvc', '58');
$client->setCookie('PHPSESSID', '0a12ket5d8d7otlo6uh66p658a5');
$response = $client->request('POST');

$client->setParameterPost(array());
$client->setUri($uri);
$client->setHeaders(array(
    'Host: '.$host,
    'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0',
    'Accept: text/javascript, application/javascript, */*',
    'Accept-Language: en-gb,en;q=5.0',
    'Accept-Encoding: gzip, deflate',
    'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
    'X-Requested-With: XMLHttpRequest',
    'Referer: '.$proto.'://'.$host
));

$response = $client->request('POST');
$result = json_decode($response->getBody());

if ($result->id) {
    $id = $result->id;
    $uri = $uri = $proto.'://'.$host.'/endosso/ajax/finaliza/id_proposta/'.$result->id.'/status/AGUARDANDO_ENVIO_SEG/tp_endosso/1/fl_lote/true';

    $client->setUri($uri);
    $client->setHeaders(array(
        'Host: '.$host,
        'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0',
        'Accept: text/javascript, application/javascript, */*',
        'Accept-Language: en-gb,en;q=5.0',
        'Accept-Encoding: gzip, deflate',
        'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
        'X-Requested-With: XMLHttpRequest',
        'Referer: '.$proto.'://'.$host
    ));

    $response = $client->request('POST');
    $result = json_decode($response->getBody());

    if ($result->message) {
        $message = $result->message;

        $uri = $uri = $proto.'://'.$host.'/default/gerarboleto/gerarboleto/id_proposta/'.$id;

        $client->setUri($uri);
        $client->setHeaders(array(
            'Host: '.$host,
            'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0',
            'Accept: text/javascript, application/javascript, */*',
            'Accept-Language: en-gb,en;q=5.0',
            'Accept-Encoding: gzip, deflate',
            'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
            'X-Requested-With: XMLHttpRequest',
            'Referer: '.$proto.'://'.$host
        ));

        $response = $client->request('POST');
        $result = json_decode($response->getBody());

        if ($result->status) {
            $boleto = 'Boleto gerado com sucesso';
        } else {
            $boleto = 'Erro ao gerar o boleto';
        }
    }
}

$linha = $argv[1].';'.$id.';'.$message.';'.$boleto;

print_r($linha);
