/** * Save cookies to the client session */ public function save() { $json = []; foreach ($this as $cookie) { /** @var SetCookie $cookie */ if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) { $json[] = $cookie->toArray(); } } $_SESSION[$this->sessionKey] = json_encode($json); }
/** * Parse the cookies in the response headers and store them in the cookie jar. */ protected function parseCookies() { if (!$this->cookieJar) { $this->cookieJar = new CookieJar(); } if (isset($this->respHeaders['set-cookie'])) { $url = parse_url($this->getFinalUrl()); foreach ($this->respHeaders['set-cookie'] as $cookie) { $this->cookieJar->parseCookieResponseHeader($cookie, $url['host']); } } }
/** * Saves the cookies to a file. * * @param string $filename File to save * @throws \RuntimeException if the file cannot be found or created */ public function save($filename) { $json = []; foreach ($this as $cookie) { /** @var SetCookie $cookie */ if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) { $json[] = $cookie->toArray(); } } if (false === file_put_contents($filename, json_encode($json))) { throw new \RuntimeException("Unable to save file {$filename}"); } }
/** * Check we can remove cookies and we can access their original values */ public function testForceExpiry() { //load an existing cookie $cookieJar = new CookieJar(array('cookieExisting' => 'i woz here')); //make sure it's available $this->assertEquals('i woz here', $cookieJar->get('cookieExisting')); //remove the cookie $cookieJar->forceExpiry('cookieExisting'); //check it's gone $this->assertEmpty($cookieJar->get('cookieExisting')); //check we can get it's original value $this->assertEquals('i woz here', $cookieJar->get('cookieExisting', false)); //check we can add a new cookie and remove it and it doesn't leave any phantom values $cookieJar->set('newCookie', 'i am new'); //check it's set by not received $this->assertEquals('i am new', $cookieJar->get('newCookie')); $this->assertEmpty($cookieJar->get('newCookie', false)); //remove it $cookieJar->forceExpiry('newCookie'); //check it's neither set nor received $this->assertEmpty($cookieJar->get('newCookie')); $this->assertEmpty($cookieJar->get('newCookie', false)); }
/** * Parse the cookies in the response headers and store them in the cookie jar. */ protected function parseCookies() { wfProfileIn( __METHOD__ ); if ( !$this->cookieJar ) { $this->cookieJar = new CookieJar; } if ( isset( $this->respHeaders['set-cookie'] ) ) { $url = parse_url( $this->getFinalUrl() ); foreach ( $this->respHeaders['set-cookie'] as $cookie ) { $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] ); } } wfProfileOut( __METHOD__ ); }
function testParseResponseHeader() { $cj = new CookieJar(); $h[] = "Set-Cookie: name4=value; domain=.example.com; path=/; expires=Mon, 09-Dec-2029 13:46:00 GMT"; $cj->parseCookieResponseHeader($h[0], "www.example.com"); $this->assertEquals("name4=value", $cj->serializeToHttpRequest("/", "www.example.com")); $h[] = "name4=value2; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT"; $cj->parseCookieResponseHeader($h[1], "www.example.com"); $this->assertEquals("", $cj->serializeToHttpRequest("/", "www.example.com")); $this->assertEquals("name4=value2", $cj->serializeToHttpRequest("/path/", "www.example.com")); $h[] = "name5=value3; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT"; $cj->parseCookieResponseHeader($h[2], "www.example.com"); $this->assertEquals("name4=value2; name5=value3", $cj->serializeToHttpRequest("/path/", "www.example.com")); $h[] = "name6=value3; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT"; $cj->parseCookieResponseHeader($h[3], "www.example.com"); $this->assertEquals("", $cj->serializeToHttpRequest("/path/", "www.example.net")); $h[] = "name6=value0; domain=.example.net; path=/path/; expires=Mon, 09-Dec-1999 13:46:00 GMT"; $cj->parseCookieResponseHeader($h[4], "www.example.net"); $this->assertEquals("", $cj->serializeToHttpRequest("/path/", "www.example.net")); $h[] = "name6=value4; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT"; $cj->parseCookieResponseHeader($h[5], "www.example.net"); $this->assertEquals("name6=value4", $cj->serializeToHttpRequest("/path/", "www.example.net")); }
throw new ErrorException($errstr, 0, $errno, $errfile, $errline); } set_error_handler("handleErrors"); include "library/init.php"; // well, why not... include "apps/default/controllers/abstract.php"; $mode = getenv("PROJECT_MODE") !== false ? getenv("PROJECT_MODE") : "live"; //session_cache_limiter(false); try { // make sure a request object is available as soon as possible $request = JaossRequest::getInstance(); Settings::setMode($mode); include "library/boot.php"; include "library/load_apps.php"; if (Settings::getValue("date", "allow_from_cookie", false)) { $date = CookieJar::getInstance()->getCookie("test_date"); if ($date !== null) { Utils::setCurrentDate($date); } } $request->dispatch(); $response = $request->getResponse(); /* $response->setIfNoneMatch( $request->getHeader('If-None-Match') ); */ $response->send(); } catch (Exception $e) { $handler = new ErrorHandler(); $handler->setRequest($request);
function testCookieClearByDate() { $jar = new CookieJar(); $jar->setCookie(new SimpleCookie("a", "abc", "/", "Wed, 25-Dec-02 04:24:21 GMT")); $jar->setCookie(new SimpleCookie("a", "def", "/", "Wed, 25-Dec-02 04:24:19 GMT")); $cookies = $jar->getValidCookies(false, "/"); $this->assertIdentical($cookies[0]->getValue(), "def"); $jar->restartSession("Wed, 25-Dec-02 04:24:20 GMT"); $this->assertEqual(count($jar->getValidCookies(false, "/")), 0); }
function testCookieClearing() { $jar = new CookieJar(); $jar->setCookie(new SimpleCookie("a", "abc", "/")); $jar->setCookie(new SimpleCookie("a", "", "/")); $this->assertEqual(count($cookies = $jar->getValidCookies(false, "/")), 1); $this->assertIdentical($cookies[0]->getValue(), ""); }
public function testOne() { $jar = new CookieJar(); $this->assertEquals(0, $jar->count()); $jar->add(array('name' => 'foo', 'value' => 'dingbat', "domain" => '.client9.com', 'path' => '/')); $this->assertEquals(1, $jar->count()); $jar->add(array('name' => 'foo', 'value' => 'dingbat', "domain" => '.sdfs.com', 'path' => '/')); $this->assertEquals(2, $jar->count()); $jar->add(array('name' => 'foo', 'value' => 'asterisk', "domain" => '.client9.com', 'path' => '/')); $this->assertEquals(2, $jar->count()); $matches = $jar->get('www.client9.com', '/', FALSE); $this->assertEquals(1, count($matches)); $m = array_shift($matches); $this->assertEquals('foo', $m['name']); $this->assertEquals('asterisk', $m['value']); $jar->clearByDomain('.client9.com'); $this->assertEquals(1, $jar->count()); // do nothing $jar->clearByDomain('junk'); $this->assertEquals(1, $jar->count()); // do nothing $jar->clearByName('junk'); $this->assertEquals(1, $jar->count()); // all gone $jar->clearSessions(); $this->assertEquals(0, $jar->count()); $jar->add(array('name' => 'foo', 'value' => 'dingbat', "domain" => '.client9.com', 'path' => '/', 'expires' => 'Fri, 31-Dec-2010 23:59:59 GMT')); $this->assertEquals(1, $jar->count()); $jar->clearSessions(); $this->assertEquals(1, $jar->count()); $jar->clearExpired(); $this->assertEquals(0, $jar->count()); }