diff --git a/src/Configuration.php b/src/Configuration.php index d2e9bb9..f222489 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/Session.php b/src/Session.php index 63ba0a5..e85aea2 100644 --- a/src/Session.php +++ b/src/Session.php @@ -339,6 +339,8 @@ protected function request($capability, $options = []) $response = new \PHRETS\Http\Response($response); + $this->removeSessionCookieFile($options); + $this->last_response = $response; if ($response->getHeader('Set-Cookie')) { @@ -349,7 +351,7 @@ protected function request($capability, $options = []) } } } - + if (preg_match('/text\/xml/', $response->getHeader('Content-Type')) and $capability != 'GetObject') { $parser = $this->grab(Strategy::PARSER_XML); $xml = $parser->parse($response); @@ -481,7 +483,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 @@ -492,6 +494,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 */