Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
],
"require": {
"php": ">=5.5.0",
"ext-iconv": "*",
"ext-mbstring": "*",
"guzzlehttp/guzzle": ">=6.0",
"illuminate/container": ">=4.2.0",
"illuminate/support": ">=4.2.0",
Expand Down
21 changes: 20 additions & 1 deletion src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Configuration
protected $http_authentication = 'digest';
/** @var \PHRETS\Strategies\Strategy */
protected $strategy;
protected $sessionTempDir = null;
protected $options = [];

public function __construct()
Expand Down Expand Up @@ -135,6 +136,24 @@ public function setUsername($username)
return $this;
}

/**
* @param string $username
* @return $this
*/
public function setSessionTempDir($sessionTempDir)
{
$this->sessionTempdDir = $sessionTempDir;
return $this;
}

/**
* @return mixed
*/
public function getSessionTempDir()
{
return $this->sessionTempDir;
}

/**
* @param $name
* @param $value
Expand Down Expand Up @@ -240,7 +259,7 @@ public function userAgentDigestHash(Session $session)
*/
public function setHttpAuthenticationMethod($auth_method)
{
if (!in_array($auth_method, [self::AUTH_BASIC, self::AUTH_DIGEST])) {
if ($auth_method && !in_array($auth_method, [self::AUTH_BASIC, self::AUTH_DIGEST])) {
throw new \InvalidArgumentException("Given authentication method is invalid. Must be 'basic' or 'digest'");
}
$this->http_authentication = $auth_method;
Expand Down
16 changes: 15 additions & 1 deletion src/Parsers/XML.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ public function parse($string)
$string = $string->getBody()->__toString();
}

return new \SimpleXMLElement((string) $string);
$string = (string) $string;

/**
* Some rets provider(s) return invalid XML data.
* Here we make sure the returned data are UTF-8 valid chars.
* I've seen so far:
* - windows carriage return (^M at the end of each lines)
* - trade mark sign not converted to ™ and I guess we can have other sigh like this.
*
* NOTE:
* //IGNORE will discard the invalid UTF-8 chars.
*/
$string = iconv(mb_detect_encoding($string), 'UTF-8//IGNORE', $string);

return new \SimpleXMLElement($string);
}
}
19 changes: 18 additions & 1 deletion src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ protected function request($capability, $options = [], $is_retry = false)

$response = new \PHRETS\Http\Response($response);

$this->removeSessionCookieFile($options);

$this->last_response = $response;

if ($response->getHeader('Set-Cookie')) {
Expand Down Expand Up @@ -543,7 +545,7 @@ public function getDefaultOptions()
'Accept-Encoding' => 'gzip',
'Accept' => '*/*',
],
'curl' => [ CURLOPT_COOKIEFILE => tempnam('/tmp', 'phrets') ]
'curl' => [ CURLOPT_COOKIEFILE => tempnam($this->configuration->getSessionTempDir(), 'phrets') ]
];

// disable following 'Location' header (redirects) automatically
Expand All @@ -554,6 +556,21 @@ public function getDefaultOptions()
return $defaults;
}

/**
* If the session cookie file have beend created, we will remove it.
*
* @param array $options Assoc array that came from the method getDefaultOptions.
*
* @return void
*/
private function removeSessionCookieFile(array $options)
{
$tmpFile = $options['curl'][CURLOPT_COOKIEFILE];
if (file_exists($tmpFile)) {
unlink($tmpFile);
}
}

public function setParser($parser_name, $parser_object)
{
/** @var Container $container */
Expand Down