Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/simpletestlib/browser.php
Go to the documentation of this file.
00001 <?php
00012 require_once(dirname(__FILE__) . '/simpletest.php');
00013 require_once(dirname(__FILE__) . '/http.php');
00014 require_once(dirname(__FILE__) . '/encoding.php');
00015 require_once(dirname(__FILE__) . '/page.php');
00016 require_once(dirname(__FILE__) . '/selector.php');
00017 require_once(dirname(__FILE__) . '/frames.php');
00018 require_once(dirname(__FILE__) . '/user_agent.php');
00021 if (!defined('DEFAULT_MAX_NESTED_FRAMES')) {
00022     define('DEFAULT_MAX_NESTED_FRAMES', 3);
00023 }
00024 
00030 class SimpleBrowserHistory {
00031     var $_sequence;
00032     var $_position;
00033 
00038     function SimpleBrowserHistory() {
00039         $this->_sequence = array();
00040         $this->_position = -1;
00041     }
00042 
00048     function _isEmpty() {
00049         return ($this->_position == -1);
00050     }
00051 
00057     function _atBeginning() {
00058         return ($this->_position == 0) && ! $this->_isEmpty();
00059     }
00060 
00066     function _atEnd() {
00067         return ($this->_position + 1 >= count($this->_sequence)) && ! $this->_isEmpty();
00068     }
00069 
00076     function recordEntry($url, $parameters) {
00077         $this->_dropFuture();
00078         array_push(
00079                 $this->_sequence,
00080                 array('url' => $url, 'parameters' => $parameters));
00081         $this->_position++;
00082     }
00083 
00090     function getUrl() {
00091         if ($this->_isEmpty()) {
00092             return false;
00093         }
00094         return $this->_sequence[$this->_position]['url'];
00095     }
00096 
00103     function getParameters() {
00104         if ($this->_isEmpty()) {
00105             return false;
00106         }
00107         return $this->_sequence[$this->_position]['parameters'];
00108     }
00109 
00116     function back() {
00117         if ($this->_isEmpty() || $this->_atBeginning()) {
00118             return false;
00119         }
00120         $this->_position--;
00121         return true;
00122     }
00123 
00130     function forward() {
00131         if ($this->_isEmpty() || $this->_atEnd()) {
00132             return false;
00133         }
00134         $this->_position++;
00135         return true;
00136     }
00137 
00143     function _dropFuture() {
00144         if ($this->_isEmpty()) {
00145             return;
00146         }
00147         while (! $this->_atEnd()) {
00148             array_pop($this->_sequence);
00149         }
00150     }
00151 }
00152 
00160 class SimpleBrowser {
00161     var $_user_agent;
00162     var $_page;
00163     var $_history;
00164     var $_ignore_frames;
00165     var $_maximum_nested_frames;
00166 
00174     function SimpleBrowser() {
00175         $this->_user_agent = &$this->_createUserAgent();
00176         $this->_user_agent->useProxy(
00177                 SimpleTest::getDefaultProxy(),
00178                 SimpleTest::getDefaultProxyUsername(),
00179                 SimpleTest::getDefaultProxyPassword());
00180         $this->_page = new SimplePage();
00181         $this->_history = &$this->_createHistory();
00182         $this->_ignore_frames = false;
00183         $this->_maximum_nested_frames = DEFAULT_MAX_NESTED_FRAMES;
00184     }
00185 
00191     function &_createUserAgent() {
00192         $user_agent = new SimpleUserAgent();
00193         return $user_agent;
00194     }
00195 
00201     function &_createHistory() {
00202         $history = new SimpleBrowserHistory();
00203         return $history;
00204     }
00205 
00211     function ignoreFrames() {
00212         $this->_ignore_frames = true;
00213     }
00214 
00220     function useFrames() {
00221         $this->_ignore_frames = false;
00222     }
00223     
00228     function ignoreCookies() {
00229         $this->_user_agent->ignoreCookies();
00230     }
00231     
00236     function useCookies() {
00237         $this->_user_agent->useCookies();
00238     }
00239 
00248     function &_parse($response, $depth = 0) {
00249         $page = &$this->_buildPage($response);
00250         if ($this->_ignore_frames || ! $page->hasFrames() || ($depth > $this->_maximum_nested_frames)) {
00251             return $page;
00252         }
00253         $frameset = new SimpleFrameset($page);
00254         foreach ($page->getFrameset() as $key => $url) {
00255             $frame = &$this->_fetch($url, new SimpleGetEncoding(), $depth + 1);
00256             $frameset->addFrame($frame, $key);
00257         }
00258         return $frameset;
00259     }
00260     
00269     function &_buildPage($response) {
00270         $builder = new SimplePageBuilder();
00271         $page = &$builder->parse($response);
00272         $builder->free();
00273         unset($builder);
00274         return $page;
00275     }
00276 
00286     function &_fetch($url, $encoding, $depth = 0) {
00287         $response = &$this->_user_agent->fetchResponse($url, $encoding);
00288         if ($response->isError()) {
00289             $page = new SimplePage($response);
00290         } else {
00291             $page = &$this->_parse($response, $depth);
00292         }
00293         return $page;
00294     }
00295 
00304     function _load($url, $parameters) {
00305         $frame = $url->getTarget();
00306         if (! $frame || ! $this->_page->hasFrames() || (strtolower($frame) == '_top')) {
00307             return $this->_loadPage($url, $parameters);
00308         }
00309         return $this->_loadFrame(array($frame), $url, $parameters);
00310     }
00311 
00319     function _loadPage($url, $parameters) {
00320         $this->_page = &$this->_fetch($url, $parameters);
00321         $this->_history->recordEntry(
00322                 $this->_page->getUrl(),
00323                 $this->_page->getRequestData());
00324         return $this->_page->getRaw();
00325     }
00326 
00336     function _loadFrame($frames, $url, $parameters) {
00337         $page = &$this->_fetch($url, $parameters);
00338         $this->_page->setFrame($frames, $page);
00339         return $page->getRaw();
00340     }
00341 
00350     function restart($date = false) {
00351         $this->_user_agent->restart($date);
00352     }
00353 
00360     function addHeader($header) {
00361         $this->_user_agent->addHeader($header);
00362     }
00363 
00369     function ageCookies($interval) {
00370         $this->_user_agent->ageCookies($interval);
00371     }
00372 
00383     function setCookie($name, $value, $host = false, $path = '/', $expiry = false) {
00384         $this->_user_agent->setCookie($name, $value, $host, $path, $expiry);
00385     }
00386 
00397     function getCookieValue($host, $path, $name) {
00398         return $this->_user_agent->getCookieValue($host, $path, $name);
00399     }
00400 
00408     function getCurrentCookieValue($name) {
00409         return $this->_user_agent->getBaseCookieValue($name, $this->_page->getUrl());
00410     }
00411 
00418     function setMaximumRedirects($max) {
00419         $this->_user_agent->setMaximumRedirects($max);
00420     }
00421 
00428     function setMaximumNestedFrames($max) {
00429         $this->_maximum_nested_frames = $max;
00430     }
00431 
00437     function setConnectionTimeout($timeout) {
00438         $this->_user_agent->setConnectionTimeout($timeout);
00439     }
00440 
00450     function useProxy($proxy, $username = false, $password = false) {
00451         $this->_user_agent->useProxy($proxy, $username, $password);
00452     }
00453 
00463     function head($url, $parameters = false) {
00464         if (! is_object($url)) {
00465             $url = new SimpleUrl($url);
00466         }
00467         if ($this->getUrl()) {
00468             $url = $url->makeAbsolute($this->getUrl());
00469         }
00470         $response = &$this->_user_agent->fetchResponse($url, new SimpleHeadEncoding($parameters));
00471         return ! $response->isError();
00472     }
00473 
00482     function get($url, $parameters = false) {
00483         if (! is_object($url)) {
00484             $url = new SimpleUrl($url);
00485         }
00486         if ($this->getUrl()) {
00487             $url = $url->makeAbsolute($this->getUrl());
00488         }
00489         return $this->_load($url, new SimpleGetEncoding($parameters));
00490     }
00491 
00499     function post($url, $parameters = false) {
00500         if (! is_object($url)) {
00501             $url = new SimpleUrl($url);
00502         }
00503         if ($this->getUrl()) {
00504             $url = $url->makeAbsolute($this->getUrl());
00505         }
00506         return $this->_load($url, new SimplePostEncoding($parameters));
00507     }
00508 
00517     function retry() {
00518         $frames = $this->_page->getFrameFocus();
00519         if (count($frames) > 0) {
00520             $this->_loadFrame(
00521                     $frames,
00522                     $this->_page->getUrl(),
00523                     $this->_page->getRequestData());
00524             return $this->_page->getRaw();
00525         }
00526         if ($url = $this->_history->getUrl()) {
00527             $this->_page = &$this->_fetch($url, $this->_history->getParameters());
00528             return $this->_page->getRaw();
00529         }
00530         return false;
00531     }
00532 
00542     function back() {
00543         if (! $this->_history->back()) {
00544             return false;
00545         }
00546         $content = $this->retry();
00547         if (! $content) {
00548             $this->_history->forward();
00549         }
00550         return $content;
00551     }
00552 
00562     function forward() {
00563         if (! $this->_history->forward()) {
00564             return false;
00565         }
00566         $content = $this->retry();
00567         if (! $content) {
00568             $this->_history->back();
00569         }
00570         return $content;
00571     }
00572 
00583     function authenticate($username, $password) {
00584         if (! $this->_page->getRealm()) {
00585             return false;
00586         }
00587         $url = $this->_page->getUrl();
00588         if (! $url) {
00589             return false;
00590         }
00591         $this->_user_agent->setIdentity(
00592                 $url->getHost(),
00593                 $this->_page->getRealm(),
00594                 $username,
00595                 $password);
00596         return $this->retry();
00597     }
00598 
00605     function getFrames() {
00606         return $this->_page->getFrames();
00607     }
00608 
00617     function getFrameFocus() {
00618         return $this->_page->getFrameFocus();
00619     }
00620 
00627     function setFrameFocusByIndex($choice) {
00628         return $this->_page->setFrameFocusByIndex($choice);
00629     }
00630 
00637     function setFrameFocus($name) {
00638         return $this->_page->setFrameFocus($name);
00639     }
00640 
00646     function clearFrameFocus() {
00647         return $this->_page->clearFrameFocus();
00648     }
00649 
00655     function getTransportError() {
00656         return $this->_page->getTransportError();
00657     }
00658 
00664     function getMimeType() {
00665         return $this->_page->getMimeType();
00666     }
00667 
00673     function getResponseCode() {
00674         return $this->_page->getResponseCode();
00675     }
00676 
00683     function getAuthentication() {
00684         return $this->_page->getAuthentication();
00685     }
00686 
00693     function getRealm() {
00694         return $this->_page->getRealm();
00695     }
00696 
00703     function getUrl() {
00704         $url = $this->_page->getUrl();
00705         return $url ? $url->asString() : false;
00706     }
00707 
00712     function getBaseUrl() {
00713         $url = $this->_page->getBaseUrl();
00714         return $url ? $url->asString() : false;
00715     }
00716 
00722     function getRequest() {
00723         return $this->_page->getRequest();
00724     }
00725 
00731     function getHeaders() {
00732         return $this->_page->getHeaders();
00733     }
00734 
00740     function getContent() {
00741         return $this->_page->getRaw();
00742     }
00743 
00749     function getContentAsText() {
00750         return $this->_page->getText();
00751     }
00752 
00758     function getTitle() {
00759         return $this->_page->getTitle();
00760     }
00761 
00768     function getUrls() {
00769         return $this->_page->getUrls();
00770     }
00771 
00779     function setField($label, $value, $position=false) {
00780         return $this->_page->setField(new SimpleByLabelOrName($label), $value, $position);
00781     }
00782 
00791     function setFieldByName($name, $value, $position=false) {
00792         return $this->_page->setField(new SimpleByName($name), $value, $position);
00793     }
00794 
00802     function setFieldById($id, $value) {
00803         return $this->_page->setField(new SimpleById($id), $value);
00804     }
00805 
00815     function getField($label) {
00816         return $this->_page->getField(new SimpleByLabelOrName($label));
00817     }
00818 
00828     function getFieldByName($name) {
00829         return $this->_page->getField(new SimpleByName($name));
00830     }
00831 
00840     function getFieldById($id) {
00841         return $this->_page->getField(new SimpleById($id));
00842     }
00843 
00853     function clickSubmit($label = 'Submit', $additional = false) {
00854         if (! ($form = &$this->_page->getFormBySubmit(new SimpleByLabel($label)))) {
00855             return false;
00856         }
00857         $success = $this->_load(
00858                 $form->getAction(),
00859                 $form->submitButton(new SimpleByLabel($label), $additional));
00860         return ($success ? $this->getContent() : $success);
00861     }
00862 
00871     function clickSubmitByName($name, $additional = false) {
00872         if (! ($form = &$this->_page->getFormBySubmit(new SimpleByName($name)))) {
00873             return false;
00874         }
00875         $success = $this->_load(
00876                 $form->getAction(),
00877                 $form->submitButton(new SimpleByName($name), $additional));
00878         return ($success ? $this->getContent() : $success);
00879     }
00880 
00889     function clickSubmitById($id, $additional = false) {
00890         if (! ($form = &$this->_page->getFormBySubmit(new SimpleById($id)))) {
00891             return false;
00892         }
00893         $success = $this->_load(
00894                 $form->getAction(),
00895                 $form->submitButton(new SimpleById($id), $additional));
00896         return ($success ? $this->getContent() : $success);
00897     }
00898     
00906     function isSubmit($label) {
00907         return (boolean)$this->_page->getFormBySubmit(new SimpleByLabel($label));
00908     }
00909 
00923     function clickImage($label, $x = 1, $y = 1, $additional = false) {
00924         if (! ($form = &$this->_page->getFormByImage(new SimpleByLabel($label)))) {
00925             return false;
00926         }
00927         $success = $this->_load(
00928                 $form->getAction(),
00929                 $form->submitImage(new SimpleByLabel($label), $x, $y, $additional));
00930         return ($success ? $this->getContent() : $success);
00931     }
00932 
00946     function clickImageByName($name, $x = 1, $y = 1, $additional = false) {
00947         if (! ($form = &$this->_page->getFormByImage(new SimpleByName($name)))) {
00948             return false;
00949         }
00950         $success = $this->_load(
00951                 $form->getAction(),
00952                 $form->submitImage(new SimpleByName($name), $x, $y, $additional));
00953         return ($success ? $this->getContent() : $success);
00954     }
00955 
00968     function clickImageById($id, $x = 1, $y = 1, $additional = false) {
00969         if (! ($form = &$this->_page->getFormByImage(new SimpleById($id)))) {
00970             return false;
00971         }
00972         $success = $this->_load(
00973                 $form->getAction(),
00974                 $form->submitImage(new SimpleById($id), $x, $y, $additional));
00975         return ($success ? $this->getContent() : $success);
00976     }
00977     
00985     function isImage($label) {
00986         return (boolean)$this->_page->getFormByImage(new SimpleByLabel($label));
00987     }
00988 
00996     function submitFormById($id) {
00997         if (! ($form = &$this->_page->getFormById($id))) {
00998             return false;
00999         }
01000         $success = $this->_load(
01001                 $form->getAction(),
01002                 $form->submit());
01003         return ($success ? $this->getContent() : $success);
01004     }
01005 
01016     function getLink($label, $index = 0) {
01017         $urls = $this->_page->getUrlsByLabel($label);
01018         if (count($urls) == 0) {
01019             return false;
01020         }
01021         if (count($urls) < $index + 1) {
01022             return false;
01023         }
01024         return $urls[$index];
01025     }
01026 
01037     function clickLink($label, $index = 0) {
01038         $url = $this->getLink($label, $index);
01039         if ($url === false) {
01040             return false;
01041         }
01042         $this->_load($url, new SimpleGetEncoding());
01043         return $this->getContent();
01044     }
01045     
01052     function getLinkById($id) {
01053         return $this->_page->getUrlById($id);
01054     }
01055 
01062     function clickLinkById($id) {
01063         if (! ($url = $this->getLinkById($id))) {
01064             return false;
01065         }
01066         $this->_load($url, new SimpleGetEncoding());
01067         return $this->getContent();
01068     }
01069 
01077     function click($label) {
01078         $raw = $this->clickSubmit($label);
01079         if (! $raw) {
01080             $raw = $this->clickLink($label);
01081         }
01082         if (! $raw) {
01083             $raw = $this->clickImage($label);
01084         }
01085         return $raw;
01086     }
01087 
01094     function isClickable($label) {
01095         return $this->isSubmit($label) || ($this->getLink($label) !== false) || $this->isImage($label);
01096     }
01097 }
01098 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations