12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094 |
- <?php
-
-
-
- require_once(dirname(__FILE__) . '/simpletest.php');
- require_once(dirname(__FILE__) . '/http.php');
- require_once(dirname(__FILE__) . '/encoding.php');
- require_once(dirname(__FILE__) . '/page.php');
- require_once(dirname(__FILE__) . '/selector.php');
- require_once(dirname(__FILE__) . '/frames.php');
- require_once(dirname(__FILE__) . '/user_agent.php');
-
-
- if (!defined('DEFAULT_MAX_NESTED_FRAMES')) {
- define('DEFAULT_MAX_NESTED_FRAMES', 3);
- }
-
-
- class SimpleBrowserHistory {
- private $sequence;
- private $position;
-
-
-
- function __construct() {
- $this->sequence = array();
- $this->position = -1;
- }
-
-
-
- protected function isEmpty() {
- return ($this->position == -1);
- }
-
-
-
- protected function atBeginning() {
- return ($this->position == 0) && ! $this->isEmpty();
- }
-
-
-
- protected function atEnd() {
- return ($this->position + 1 >= count($this->sequence)) && ! $this->isEmpty();
- }
-
-
-
- function recordEntry($url, $parameters) {
- $this->dropFuture();
- array_push(
- $this->sequence,
- array('url' => $url, 'parameters' => $parameters));
- $this->position++;
- }
-
-
-
- function getUrl() {
- if ($this->isEmpty()) {
- return false;
- }
- return $this->sequence[$this->position]['url'];
- }
-
-
-
- function getParameters() {
- if ($this->isEmpty()) {
- return false;
- }
- return $this->sequence[$this->position]['parameters'];
- }
-
-
-
- function back() {
- if ($this->isEmpty() || $this->atBeginning()) {
- return false;
- }
- $this->position--;
- return true;
- }
-
-
-
- function forward() {
- if ($this->isEmpty() || $this->atEnd()) {
- return false;
- }
- $this->position++;
- return true;
- }
-
-
-
- protected function dropFuture() {
- if ($this->isEmpty()) {
- return;
- }
- while (! $this->atEnd()) {
- array_pop($this->sequence);
- }
- }
- }
-
-
- class SimpleBrowser {
- private $user_agent;
- private $page;
- private $history;
- private $ignore_frames;
- private $maximum_nested_frames;
-
-
-
- function __construct() {
- $this->user_agent = $this->createUserAgent();
- $this->user_agent->useProxy(
- SimpleTest::getDefaultProxy(),
- SimpleTest::getDefaultProxyUsername(),
- SimpleTest::getDefaultProxyPassword());
- $this->page = new SimplePage();
- $this->history = $this->createHistory();
- $this->ignore_frames = false;
- $this->maximum_nested_frames = DEFAULT_MAX_NESTED_FRAMES;
- }
-
-
-
- protected function createUserAgent() {
- return new SimpleUserAgent();
- }
-
-
-
- protected function createHistory() {
- return new SimpleBrowserHistory();
- }
-
-
-
- function ignoreFrames() {
- $this->ignore_frames = true;
- }
-
-
-
- function useFrames() {
- $this->ignore_frames = false;
- }
-
-
-
- function ignoreCookies() {
- $this->user_agent->ignoreCookies();
- }
-
-
-
- function useCookies() {
- $this->user_agent->useCookies();
- }
-
-
-
- protected function parse($response, $depth = 0) {
- $page = $this->buildPage($response);
- if ($this->ignore_frames || ! $page->hasFrames() || ($depth > $this->maximum_nested_frames)) {
- return $page;
- }
- $frameset = new SimpleFrameset($page);
- foreach ($page->getFrameset() as $key => $url) {
- $frame = $this->fetch($url, new SimpleGetEncoding(), $depth + 1);
- $frameset->addFrame($frame, $key);
- }
- return $frameset;
- }
-
-
-
- protected function buildPage($response) {
- $builder = new SimplePageBuilder();
- $page = $builder->parse($response);
- $builder->free();
- unset($builder);
- return $page;
- }
-
-
-
- protected function fetch($url, $encoding, $depth = 0) {
- $response = $this->user_agent->fetchResponse($url, $encoding);
- if ($response->isError()) {
- return new SimplePage($response);
- }
- return $this->parse($response, $depth);
- }
-
-
-
- protected function load($url, $parameters) {
- $frame = $url->getTarget();
- if (! $frame || ! $this->page->hasFrames() || (strtolower($frame) == '_top')) {
- return $this->loadPage($url, $parameters);
- }
- return $this->loadFrame(array($frame), $url, $parameters);
- }
-
-
-
- protected function loadPage($url, $parameters) {
- $this->page = $this->fetch($url, $parameters);
- $this->history->recordEntry(
- $this->page->getUrl(),
- $this->page->getRequestData());
- return $this->page->getRaw();
- }
-
-
-
- protected function loadFrame($frames, $url, $parameters) {
- $page = $this->fetch($url, $parameters);
- $this->page->setFrame($frames, $page);
- return $page->getRaw();
- }
-
-
-
- function restart($date = false) {
- $this->user_agent->restart($date);
- }
-
-
-
- function addHeader($header) {
- $this->user_agent->addHeader($header);
- }
-
-
-
- function ageCookies($interval) {
- $this->user_agent->ageCookies($interval);
- }
-
-
-
- function setCookie($name, $value, $host = false, $path = '/', $expiry = false) {
- $this->user_agent->setCookie($name, $value, $host, $path, $expiry);
- }
-
-
-
- function getCookieValue($host, $path, $name) {
- return $this->user_agent->getCookieValue($host, $path, $name);
- }
-
-
-
- function getCurrentCookieValue($name) {
- return $this->user_agent->getBaseCookieValue($name, $this->page->getUrl());
- }
-
-
-
- function setMaximumRedirects($max) {
- $this->user_agent->setMaximumRedirects($max);
- }
-
-
-
- function setMaximumNestedFrames($max) {
- $this->maximum_nested_frames = $max;
- }
-
-
-
- function setConnectionTimeout($timeout) {
- $this->user_agent->setConnectionTimeout($timeout);
- }
-
-
-
- function useProxy($proxy, $username = false, $password = false) {
- $this->user_agent->useProxy($proxy, $username, $password);
- }
-
-
-
- function head($url, $parameters = false) {
- if (! is_object($url)) {
- $url = new SimpleUrl($url);
- }
- if ($this->getUrl()) {
- $url = $url->makeAbsolute($this->getUrl());
- }
- $response = &$this->user_agent->fetchResponse($url, new SimpleHeadEncoding($parameters));
- return ! $response->isError();
- }
-
-
-
- function get($url, $parameters = false) {
- if (! is_object($url)) {
- $url = new SimpleUrl($url);
- }
- if ($this->getUrl()) {
- $url = $url->makeAbsolute($this->getUrl());
- }
- return $this->load($url, new SimpleGetEncoding($parameters));
- }
-
-
-
- function post($url, $parameters = false) {
- if (! is_object($url)) {
- $url = new SimpleUrl($url);
- }
- if ($this->getUrl()) {
- $url = $url->makeAbsolute($this->getUrl());
- }
- return $this->load($url, new SimplePostEncoding($parameters));
- }
-
-
-
- function retry() {
- $frames = $this->page->getFrameFocus();
- if (count($frames) > 0) {
- $this->loadFrame(
- $frames,
- $this->page->getUrl(),
- $this->page->getRequestData());
- return $this->page->getRaw();
- }
- if ($url = $this->history->getUrl()) {
- $this->page = $this->fetch($url, $this->history->getParameters());
- return $this->page->getRaw();
- }
- return false;
- }
-
-
-
- function back() {
- if (! $this->history->back()) {
- return false;
- }
- $content = $this->retry();
- if (! $content) {
- $this->history->forward();
- }
- return $content;
- }
-
-
-
- function forward() {
- if (! $this->history->forward()) {
- return false;
- }
- $content = $this->retry();
- if (! $content) {
- $this->history->back();
- }
- return $content;
- }
-
-
-
- function authenticate($username, $password) {
- if (! $this->page->getRealm()) {
- return false;
- }
- $url = $this->page->getUrl();
- if (! $url) {
- return false;
- }
- $this->user_agent->setIdentity(
- $url->getHost(),
- $this->page->getRealm(),
- $username,
- $password);
- return $this->retry();
- }
-
-
-
- function getFrames() {
- return $this->page->getFrames();
- }
-
-
-
- function getFrameFocus() {
- return $this->page->getFrameFocus();
- }
-
-
-
- function setFrameFocusByIndex($choice) {
- return $this->page->setFrameFocusByIndex($choice);
- }
-
-
-
- function setFrameFocus($name) {
- return $this->page->setFrameFocus($name);
- }
-
-
-
- function clearFrameFocus() {
- return $this->page->clearFrameFocus();
- }
-
-
-
- function getTransportError() {
- return $this->page->getTransportError();
- }
-
-
-
- function getMimeType() {
- return $this->page->getMimeType();
- }
-
-
-
- function getResponseCode() {
- return $this->page->getResponseCode();
- }
-
-
-
- function getAuthentication() {
- return $this->page->getAuthentication();
- }
-
-
-
- function getRealm() {
- return $this->page->getRealm();
- }
-
-
-
- function getUrl() {
- $url = $this->page->getUrl();
- return $url ? $url->asString() : false;
- }
-
-
-
- function getBaseUrl() {
- $url = $this->page->getBaseUrl();
- return $url ? $url->asString() : false;
- }
-
-
-
- function getRequest() {
- return $this->page->getRequest();
- }
-
-
-
- function getHeaders() {
- return $this->page->getHeaders();
- }
-
-
-
- function getContent() {
- return $this->page->getRaw();
- }
-
-
-
- function getContentAsText() {
- return $this->page->getText();
- }
-
-
-
- function getTitle() {
- return $this->page->getTitle();
- }
-
-
-
- function getUrls() {
- return $this->page->getUrls();
- }
-
-
-
- function setField($label, $value, $position=false) {
- return $this->page->setField(new SimpleByLabelOrName($label), $value, $position);
- }
-
-
-
- function setFieldByName($name, $value, $position=false) {
- return $this->page->setField(new SimpleByName($name), $value, $position);
- }
-
-
-
- function setFieldById($id, $value) {
- return $this->page->setField(new SimpleById($id), $value);
- }
-
-
-
- function getField($label) {
- return $this->page->getField(new SimpleByLabelOrName($label));
- }
-
-
-
- function getFieldByName($name) {
- return $this->page->getField(new SimpleByName($name));
- }
-
-
-
- function getFieldById($id) {
- return $this->page->getField(new SimpleById($id));
- }
-
-
-
- function clickSubmit($label = 'Submit', $additional = false) {
- if (! ($form = $this->page->getFormBySubmit(new SimpleByLabel($label)))) {
- return false;
- }
- $success = $this->load(
- $form->getAction(),
- $form->submitButton(new SimpleByLabel($label), $additional));
- return ($success ? $this->getContent() : $success);
- }
-
-
-
- function clickSubmitByName($name, $additional = false) {
- if (! ($form = $this->page->getFormBySubmit(new SimpleByName($name)))) {
- return false;
- }
- $success = $this->load(
- $form->getAction(),
- $form->submitButton(new SimpleByName($name), $additional));
- return ($success ? $this->getContent() : $success);
- }
-
-
-
- function clickSubmitById($id, $additional = false) {
- if (! ($form = $this->page->getFormBySubmit(new SimpleById($id)))) {
- return false;
- }
- $success = $this->load(
- $form->getAction(),
- $form->submitButton(new SimpleById($id), $additional));
- return ($success ? $this->getContent() : $success);
- }
-
-
-
- function isSubmit($label) {
- return (boolean)$this->page->getFormBySubmit(new SimpleByLabel($label));
- }
-
-
-
- function clickImage($label, $x = 1, $y = 1, $additional = false) {
- if (! ($form = $this->page->getFormByImage(new SimpleByLabel($label)))) {
- return false;
- }
- $success = $this->load(
- $form->getAction(),
- $form->submitImage(new SimpleByLabel($label), $x, $y, $additional));
- return ($success ? $this->getContent() : $success);
- }
-
-
-
- function clickImageByName($name, $x = 1, $y = 1, $additional = false) {
- if (! ($form = $this->page->getFormByImage(new SimpleByName($name)))) {
- return false;
- }
- $success = $this->load(
- $form->getAction(),
- $form->submitImage(new SimpleByName($name), $x, $y, $additional));
- return ($success ? $this->getContent() : $success);
- }
-
-
-
- function clickImageById($id, $x = 1, $y = 1, $additional = false) {
- if (! ($form = $this->page->getFormByImage(new SimpleById($id)))) {
- return false;
- }
- $success = $this->load(
- $form->getAction(),
- $form->submitImage(new SimpleById($id), $x, $y, $additional));
- return ($success ? $this->getContent() : $success);
- }
-
-
-
- function isImage($label) {
- return (boolean)$this->page->getFormByImage(new SimpleByLabel($label));
- }
-
-
-
- function submitFormById($id) {
- if (! ($form = $this->page->getFormById($id))) {
- return false;
- }
- $success = $this->load(
- $form->getAction(),
- $form->submit());
- return ($success ? $this->getContent() : $success);
- }
-
-
-
- function getLink($label, $index = 0) {
- $urls = $this->page->getUrlsByLabel($label);
- if (count($urls) == 0) {
- return false;
- }
- if (count($urls) < $index + 1) {
- return false;
- }
- return $urls[$index];
- }
-
-
-
- function clickLink($label, $index = 0) {
- $url = $this->getLink($label, $index);
- if ($url === false) {
- return false;
- }
- $this->load($url, new SimpleGetEncoding());
- return $this->getContent();
- }
-
-
-
- function getLinkById($id) {
- return $this->page->getUrlById($id);
- }
-
-
-
- function clickLinkById($id) {
- if (! ($url = $this->getLinkById($id))) {
- return false;
- }
- $this->load($url, new SimpleGetEncoding());
- return $this->getContent();
- }
-
-
-
- function click($label) {
- $raw = $this->clickSubmit($label);
- if (! $raw) {
- $raw = $this->clickLink($label);
- }
- if (! $raw) {
- $raw = $this->clickImage($label);
- }
- return $raw;
- }
-
-
-
- function isClickable($label) {
- return $this->isSubmit($label) || ($this->getLink($label) !== false) || $this->isImage($label);
- }
- }
- ?>
|