HosipLan

Untitled

Jan 31st, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.27 KB | None | 0 0
  1.  
  2. <?php
  3.  
  4. /**
  5.  * This file is part of the Kdyby (http://www.kdyby.org)
  6.  *
  7.  * Copyright (c) 2008, 2011 Filip Procházka ([email protected])
  8.  *
  9.  * @license http://www.kdyby.org/license
  10.  */
  11.  
  12. use Kdyby;
  13. use Kdyby\Browser;
  14. use Nette;
  15. use Nette\Utils\Strings;
  16.  
  17.  
  18.  
  19. /**
  20.  * @author Filip Procházka <[email protected]>
  21.  *
  22.  * @property-read \Kdyby\Browser\BrowserSession $session
  23.  */
  24. class ExampleWeb extends Nette\Object
  25. {
  26.  
  27.     /** @var \Nette\Http\SessionSection */
  28.     private $httpSession;
  29.  
  30.     /** @var array */
  31.     private $credentials = array(
  32.         'username' => NULL,
  33.         'password' => NULL,
  34.     );
  35.  
  36.  
  37.  
  38.     /**
  39.      * @param \Nette\Http\SessionSection $session
  40.      * @param array $credentials
  41.      */
  42.     public function __construct(Nette\Http\SessionSection $session, array $credentials)
  43.     {
  44.         $this->httpSession = $session;
  45.         $this->credentials = $credentials;
  46.     }
  47.  
  48.  
  49.  
  50.     /**
  51.      * @return \Kdyby\Browser\BrowserSession
  52.      */
  53.     public function getSession()
  54.     {
  55.         if (empty($this->httpSession->example)) {
  56.             $browser = new Browser\WebBrowser;
  57.             $browser->setName($_SERVER['HTTP_USER_AGENT']);
  58.             $this->httpSession->example = $browser->createSession();
  59.         }
  60.  
  61.         return $this->httpSession->example;
  62.     }
  63.  
  64.  
  65.  
  66.     /**
  67.      * @return \Kdyby\Browser\WebPage
  68.      */
  69.     public function getLastPage()
  70.     {
  71.         return $this->getSession()->getLastPage();
  72.     }
  73.  
  74.  
  75.  
  76.     /**
  77.      * @return boolean
  78.      */
  79.     public function login()
  80.     {
  81.         $page = $this->getSession()->open('http://www.example.com/login/');
  82.         $form = $page->findForm('div#col-content div.form div.content form');
  83.  
  84.         $form->setValues(array(
  85.             'login-username' => $this->credentials['username'],
  86.             'login-password' => $this->credentials['password'],
  87.             'login-stay-signed' => '1'
  88.         ));
  89.  
  90.         return $this->isLoggedIn($page->submit($form));
  91.     }
  92.  
  93.  
  94.  
  95.     /**
  96.      * @param \Kdyby\Browser\WebPage $loggedIn
  97.      *
  98.      * @return boolean
  99.      */
  100.     public function isLoggedIn(Browser\WebPage $loggedIn = NULL)
  101.     {
  102.         if ($loggedIn === NULL) {
  103.             if (!$loggedIn = $this->getLastPage()) {
  104.                 $loggedIn = $this->getSession()->open('http://www.example.com');
  105.             }
  106.         }
  107.  
  108.         $username = Strings::normalize(trim($loggedIn->findOne('#login-box table a')->textContent));
  109.         return Strings::lower($username) === Strings::lower($this->credentials['username']);
  110.     }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment