diff --git a/composer.json b/composer.json index 3a4f87a..e5303a8 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Configuration.php b/src/Configuration.php index b4ab312..22c246a 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -20,6 +20,7 @@ class Configuration protected $http_authentication = 'digest'; /** @var \PHRETS\Strategies\Strategy */ protected $strategy; + protected $sessionTempDir = null; protected $options = []; public function __construct() @@ -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 @@ -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; diff --git a/src/Parsers/XML.php b/src/Parsers/XML.php index 7309ca1..257d8d1 100644 --- a/src/Parsers/XML.php +++ b/src/Parsers/XML.php @@ -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); } } diff --git a/src/Session.php b/src/Session.php index a99d319..bc18c50 100644 --- a/src/Session.php +++ b/src/Session.php @@ -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')) { @@ -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 @@ -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 */