1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Trixpua\Shipping\TamCargo\Quote; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Exception\RequestException; |
7
|
|
|
|
8
|
|
|
ini_set('max_execution_time', 0); |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class TamCargo |
12
|
|
|
* @author Elizandro Echer <https://github.com/Trixpua> |
13
|
|
|
* @package Trixpua\Shipping |
14
|
|
|
* @version 2.0.0 |
15
|
|
|
*/ |
16
|
|
|
abstract class TamCargoAuth |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** @var Client */ |
20
|
|
|
protected $client; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $urlLogin; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $viewStateLogin; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
private $login; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
private $password; |
33
|
|
|
|
34
|
|
|
/** @var \stdClass */ |
35
|
|
|
protected $result; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* TamCargo constructor. |
39
|
|
|
* @param string $login Define the login registered to access TamCargo services |
40
|
|
|
* @param string $password Define the password registered to access TamCargo services |
41
|
|
|
*/ |
42
|
|
|
protected function __construct(string $login, string $password) |
43
|
|
|
{ |
44
|
|
|
$this->result = new \stdClass(); |
45
|
|
|
$this->setLogin($login); |
46
|
|
|
$this->setPassword($password); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $login Define the login registered to access TamCargo services |
51
|
|
|
*/ |
52
|
|
|
public function setLogin(string $login): void |
53
|
|
|
{ |
54
|
|
|
$this->login = $login; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $password Define the password registered to access TamCargo services |
59
|
|
|
*/ |
60
|
|
|
public function setPassword(string $password): void |
61
|
|
|
{ |
62
|
|
|
$this->password = $password; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Make the login in My Cargo Manager service and set the required parameters needed to proceed |
67
|
|
|
*/ |
68
|
|
|
protected function login(): void |
69
|
|
|
{ |
70
|
|
|
$this->client = new Client(['cookies' => true]); |
71
|
|
|
try { |
72
|
|
|
$promise = $this->client->requestAsync('GET', |
73
|
|
|
'https://mycargomanager.appslatam.com/cas/login?TARGET=https://mycargomanager.appslatam.com/eBusiness-web-1.0-view/private/CreateQuotation.jsf?parameters=LA-pt') |
74
|
|
|
->then(function($response) { |
75
|
|
|
$doc = new \DOMDocument(); |
76
|
|
|
$libxml_previous_state = libxml_use_internal_errors(true); |
77
|
|
|
$doc->loadHTML($response->getBody()); |
78
|
|
|
libxml_use_internal_errors($libxml_previous_state); |
79
|
|
|
$xpath = new \DOMXpath($doc); |
80
|
|
|
$lt = $xpath->query('//input[@name="lt"]') |
81
|
|
|
->item(0) |
82
|
|
|
->getAttribute('value'); |
83
|
|
|
$execution = $xpath->query('//input[@name="execution"]') |
84
|
|
|
->item(0) |
85
|
|
|
->getAttribute('value'); |
86
|
|
|
$this->setViewState($lt, $execution); |
87
|
|
|
}); |
88
|
|
|
$promise->wait(); |
89
|
|
|
} catch (RequestException $e) { |
90
|
|
|
$this->result->status = 'ERROR'; |
91
|
|
|
$this->result->errors[] = 'Curl Error: ' . $e->getMessage(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set view state to use and next request |
97
|
|
|
* @param string $lt |
98
|
|
|
* @param string $execution |
99
|
|
|
*/ |
100
|
|
|
private function setViewState(string $lt, string $execution) |
101
|
|
|
{ |
102
|
|
|
try { |
103
|
|
|
$this->urlLogin = "https://mycargomanager.appslatam.com/cas/login;jsessionid={$this->client->getConfig('cookies')->toArray()[0]['Value']}?TARGET=https://mycargomanager.appslatam.com/eBusiness-web-1.0-view/private/CreateQuotation.jsf?parameters=LA-pt"; |
104
|
|
|
$headers = [ |
105
|
|
|
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36", |
106
|
|
|
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", |
107
|
|
|
"Accept-Language: pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7", |
108
|
|
|
"Accept-Encoding: ", |
109
|
|
|
"Connection: keep-alive", |
110
|
|
|
"Cache-Control: max-age=0", |
111
|
|
|
"Content-Type: application/x-www-form-urlencoded", |
112
|
|
|
"Host: mycargomanager.appslatam.com", |
113
|
|
|
"Origin: https://mycargomanager.appslatam.com", |
114
|
|
|
"Referer: https://mycargomanager.appslatam.com/cas/login", |
115
|
|
|
"Upgrade-Insecure-Requests: 1", |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
$parameters = [ |
119
|
|
|
'username' => $this->login, |
120
|
|
|
'password' => $this->password, |
121
|
|
|
'lt' => $lt, |
122
|
|
|
'execution' => $execution, |
123
|
|
|
'_eventId' => 'submit', |
124
|
|
|
'submit' => 'ENTRAR', |
125
|
|
|
]; |
126
|
|
|
|
127
|
|
|
$promise = $this->client->requestAsync('POST', |
128
|
|
|
$this->urlLogin, [ |
129
|
|
|
'form_params' => $parameters, |
130
|
|
|
'headers' => $headers |
131
|
|
|
]) |
132
|
|
|
->then(function($response) { |
133
|
|
|
$doc = new \DOMDocument(); |
134
|
|
|
$libxml_previous_state = libxml_use_internal_errors(true); |
135
|
|
|
$doc->loadHTML($response->getBody()); |
136
|
|
|
libxml_use_internal_errors($libxml_previous_state); |
137
|
|
|
$xpath = new \DOMXpath($doc); |
138
|
|
|
$this->viewStateLogin = $xpath->query('//*[@name="javax.faces.ViewState"]') |
139
|
|
|
->item(0) |
140
|
|
|
->getAttribute('value'); |
141
|
|
|
$this->result->status = null; |
142
|
|
|
$this->result->errors = []; |
143
|
|
|
}); |
144
|
|
|
$promise->wait(); |
145
|
|
|
} catch (RequestException $e) { |
146
|
|
|
$this->result->status = 'ERROR'; |
147
|
|
|
$this->result->errors[] = 'Curl Error: ' . $e->getMessage(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get the parsed result from the request |
154
|
|
|
* @return \stdClass |
155
|
|
|
*/ |
156
|
|
|
public function getResult(): \stdClass |
157
|
|
|
{ |
158
|
|
|
return $this->result; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
} |