Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This file is part of the Kdyby (http://www.kdyby.org)
- *
- * Copyright (c) 2008, 2011 Filip Procházka ([email protected])
- *
- * @license http://www.kdyby.org/license
- */
- use Kdyby;
- use Kdyby\Browser;
- use Nette;
- use Nette\Utils\Strings;
- /**
- * @author Filip Procházka <[email protected]>
- *
- * @property-read \Kdyby\Browser\BrowserSession $session
- */
- class ExampleWeb extends Nette\Object
- {
- /** @var \Nette\Http\SessionSection */
- private $httpSession;
- /** @var array */
- private $credentials = array(
- 'username' => NULL,
- 'password' => NULL,
- );
- /**
- * @param \Nette\Http\SessionSection $session
- * @param array $credentials
- */
- public function __construct(Nette\Http\SessionSection $session, array $credentials)
- {
- $this->httpSession = $session;
- $this->credentials = $credentials;
- }
- /**
- * @return \Kdyby\Browser\BrowserSession
- */
- public function getSession()
- {
- if (empty($this->httpSession->example)) {
- $browser = new Browser\WebBrowser;
- $browser->setName($_SERVER['HTTP_USER_AGENT']);
- $this->httpSession->example = $browser->createSession();
- }
- return $this->httpSession->example;
- }
- /**
- * @return \Kdyby\Browser\WebPage
- */
- public function getLastPage()
- {
- return $this->getSession()->getLastPage();
- }
- /**
- * @return boolean
- */
- public function login()
- {
- $page = $this->getSession()->open('http://www.example.com/login/');
- $form = $page->findForm('div#col-content div.form div.content form');
- $form->setValues(array(
- 'login-username' => $this->credentials['username'],
- 'login-password' => $this->credentials['password'],
- 'login-stay-signed' => '1'
- ));
- return $this->isLoggedIn($page->submit($form));
- }
- /**
- * @param \Kdyby\Browser\WebPage $loggedIn
- *
- * @return boolean
- */
- public function isLoggedIn(Browser\WebPage $loggedIn = NULL)
- {
- if ($loggedIn === NULL) {
- if (!$loggedIn = $this->getLastPage()) {
- $loggedIn = $this->getSession()->open('http://www.example.com');
- }
- }
- $username = Strings::normalize(trim($loggedIn->findOne('#login-box table a')->textContent));
- return Strings::lower($username) === Strings::lower($this->credentials['username']);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment