123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983 |
- <?php
- require_once(dirname(__FILE__) . '/http.php');
- require_once(dirname(__FILE__) . '/parser.php');
- require_once(dirname(__FILE__) . '/tag.php');
- require_once(dirname(__FILE__) . '/form.php');
- require_once(dirname(__FILE__) . '/selector.php');
- class SimpleTagBuilder {
-
- function createTag($name, $attributes) {
- static $map = array(
- 'a' => 'SimpleAnchorTag',
- 'title' => 'SimpleTitleTag',
- 'base' => 'SimpleBaseTag',
- 'button' => 'SimpleButtonTag',
- 'textarea' => 'SimpleTextAreaTag',
- 'option' => 'SimpleOptionTag',
- 'label' => 'SimpleLabelTag',
- 'form' => 'SimpleFormTag',
- 'frame' => 'SimpleFrameTag');
- $attributes = $this->_keysToLowerCase($attributes);
- if (array_key_exists($name, $map)) {
- $tag_class = $map[$name];
- return new $tag_class($attributes);
- } elseif ($name == 'select') {
- return $this->_createSelectionTag($attributes);
- } elseif ($name == 'input') {
- return $this->_createInputTag($attributes);
- }
- return new SimpleTag($name, $attributes);
- }
-
- function _createSelectionTag($attributes) {
- if (isset($attributes['multiple'])) {
- return new MultipleSelectionTag($attributes);
- }
- return new SimpleSelectionTag($attributes);
- }
-
- function _createInputTag($attributes) {
- if (! isset($attributes['type'])) {
- return new SimpleTextTag($attributes);
- }
- $type = strtolower(trim($attributes['type']));
- $map = array(
- 'submit' => 'SimpleSubmitTag',
- 'image' => 'SimpleImageSubmitTag',
- 'checkbox' => 'SimpleCheckboxTag',
- 'radio' => 'SimpleRadioButtonTag',
- 'text' => 'SimpleTextTag',
- 'hidden' => 'SimpleTextTag',
- 'password' => 'SimpleTextTag',
- 'file' => 'SimpleUploadTag');
- if (array_key_exists($type, $map)) {
- $tag_class = $map[$type];
- return new $tag_class($attributes);
- }
- return false;
- }
-
- function _keysToLowerCase($map) {
- $lower = array();
- foreach ($map as $key => $value) {
- $lower[strtolower($key)] = $value;
- }
- return $lower;
- }
- }
- class SimplePageBuilder extends SimpleSaxListener {
- var $_tags;
- var $_page;
- var $_private_content_tag;
-
- function SimplePageBuilder() {
- $this->SimpleSaxListener();
- }
-
- function free() {
- unset($this->_tags);
- unset($this->_page);
- unset($this->_private_content_tags);
- }
-
- function &parse($response) {
- $this->_tags = array();
- $this->_page = &$this->_createPage($response);
- $parser = &$this->_createParser($this);
- $parser->parse($response->getContent());
- $this->_page->acceptPageEnd();
- return $this->_page;
- }
-
- function &_createPage($response) {
- $page = &new SimplePage($response);
- return $page;
- }
-
- function &_createParser(&$listener) {
- $parser = &new SimpleHtmlSaxParser($listener);
- return $parser;
- }
-
- function startElement($name, $attributes) {
- $factory = &new SimpleTagBuilder();
- $tag = $factory->createTag($name, $attributes);
- if (! $tag) {
- return true;
- }
- if ($tag->getTagName() == 'label') {
- $this->_page->acceptLabelStart($tag);
- $this->_openTag($tag);
- return true;
- }
- if ($tag->getTagName() == 'form') {
- $this->_page->acceptFormStart($tag);
- return true;
- }
- if ($tag->getTagName() == 'frameset') {
- $this->_page->acceptFramesetStart($tag);
- return true;
- }
- if ($tag->getTagName() == 'frame') {
- $this->_page->acceptFrame($tag);
- return true;
- }
- if ($tag->isPrivateContent() && ! isset($this->_private_content_tag)) {
- $this->_private_content_tag = &$tag;
- }
- if ($tag->expectEndTag()) {
- $this->_openTag($tag);
- return true;
- }
- $this->_page->acceptTag($tag);
- return true;
- }
-
- function endElement($name) {
- if ($name == 'label') {
- $this->_page->acceptLabelEnd();
- return true;
- }
- if ($name == 'form') {
- $this->_page->acceptFormEnd();
- return true;
- }
- if ($name == 'frameset') {
- $this->_page->acceptFramesetEnd();
- return true;
- }
- if ($this->_hasNamedTagOnOpenTagStack($name)) {
- $tag = array_pop($this->_tags[$name]);
- if ($tag->isPrivateContent() && $this->_private_content_tag->getTagName() == $name) {
- unset($this->_private_content_tag);
- }
- $this->_addContentTagToOpenTags($tag);
- $this->_page->acceptTag($tag);
- return true;
- }
- return true;
- }
-
- function _hasNamedTagOnOpenTagStack($name) {
- return isset($this->_tags[$name]) && (count($this->_tags[$name]) > 0);
- }
-
- function addContent($text) {
- if (isset($this->_private_content_tag)) {
- $this->_private_content_tag->addContent($text);
- } else {
- $this->_addContentToAllOpenTags($text);
- }
- return true;
- }
-
- function _addContentToAllOpenTags($text) {
- foreach (array_keys($this->_tags) as $name) {
- for ($i = 0, $count = count($this->_tags[$name]); $i < $count; $i++) {
- $this->_tags[$name][$i]->addContent($text);
- }
- }
- }
-
- function _addContentTagToOpenTags(&$tag) {
- if ($tag->getTagName() != 'option') {
- return;
- }
- foreach (array_keys($this->_tags) as $name) {
- for ($i = 0, $count = count($this->_tags[$name]); $i < $count; $i++) {
- $this->_tags[$name][$i]->addTag($tag);
- }
- }
- }
-
- function _openTag(&$tag) {
- $name = $tag->getTagName();
- if (! in_array($name, array_keys($this->_tags))) {
- $this->_tags[$name] = array();
- }
- $this->_tags[$name][] = &$tag;
- }
- }
- class SimplePage {
- var $_links;
- var $_title;
- var $_last_widget;
- var $_label;
- var $_left_over_labels;
- var $_open_forms;
- var $_complete_forms;
- var $_frameset;
- var $_frames;
- var $_frameset_nesting_level;
- var $_transport_error;
- var $_raw;
- var $_text;
- var $_sent;
- var $_headers;
- var $_method;
- var $_url;
- var $_base = false;
- var $_request_data;
-
- function SimplePage($response = false) {
- $this->_links = array();
- $this->_title = false;
- $this->_left_over_labels = array();
- $this->_open_forms = array();
- $this->_complete_forms = array();
- $this->_frameset = false;
- $this->_frames = array();
- $this->_frameset_nesting_level = 0;
- $this->_text = false;
- if ($response) {
- $this->_extractResponse($response);
- } else {
- $this->_noResponse();
- }
- }
-
- function _extractResponse($response) {
- $this->_transport_error = $response->getError();
- $this->_raw = $response->getContent();
- $this->_sent = $response->getSent();
- $this->_headers = $response->getHeaders();
- $this->_method = $response->getMethod();
- $this->_url = $response->getUrl();
- $this->_request_data = $response->getRequestData();
- }
-
- function _noResponse() {
- $this->_transport_error = 'No page fetched yet';
- $this->_raw = false;
- $this->_sent = false;
- $this->_headers = false;
- $this->_method = 'GET';
- $this->_url = false;
- $this->_request_data = false;
- }
-
- function getRequest() {
- return $this->_sent;
- }
-
- function getRaw() {
- return $this->_raw;
- }
-
- function getText() {
- if (! $this->_text) {
- $this->_text = SimpleHtmlSaxParser::normalise($this->_raw);
- }
- return $this->_text;
- }
-
- function getHeaders() {
- if ($this->_headers) {
- return $this->_headers->getRaw();
- }
- return false;
- }
-
- function getMethod() {
- return $this->_method;
- }
-
- function getUrl() {
- return $this->_url;
- }
-
- function getBaseUrl() {
- return $this->_base;
- }
-
- function getRequestData() {
- return $this->_request_data;
- }
-
- function getTransportError() {
- return $this->_transport_error;
- }
-
- function getMimeType() {
- if ($this->_headers) {
- return $this->_headers->getMimeType();
- }
- return false;
- }
-
- function getResponseCode() {
- if ($this->_headers) {
- return $this->_headers->getResponseCode();
- }
- return false;
- }
-
- function getAuthentication() {
- if ($this->_headers) {
- return $this->_headers->getAuthentication();
- }
- return false;
- }
-
- function getRealm() {
- if ($this->_headers) {
- return $this->_headers->getRealm();
- }
- return false;
- }
-
- function getFrameFocus() {
- return array();
- }
-
- function setFrameFocusByIndex($choice) {
- return false;
- }
-
- function setFrameFocus($name) {
- return false;
- }
-
- function clearFrameFocus() {
- }
-
- function acceptTag(&$tag) {
- if ($tag->getTagName() == "a") {
- $this->_addLink($tag);
- } elseif ($tag->getTagName() == "base") {
- $this->_setBase($tag);
- } elseif ($tag->getTagName() == "title") {
- $this->_setTitle($tag);
- } elseif ($this->_isFormElement($tag->getTagName())) {
- for ($i = 0; $i < count($this->_open_forms); $i++) {
- $this->_open_forms[$i]->addWidget($tag);
- }
- $this->_last_widget = &$tag;
- }
- }
-
- function acceptLabelStart(&$tag) {
- $this->_label = &$tag;
- unset($this->_last_widget);
- }
-
- function acceptLabelEnd() {
- if (isset($this->_label)) {
- if (isset($this->_last_widget)) {
- $this->_last_widget->setLabel($this->_label->getText());
- unset($this->_last_widget);
- } else {
- $this->_left_over_labels[] = SimpleTestCompatibility::copy($this->_label);
- }
- unset($this->_label);
- }
- }
-
- function _isFormElement($name) {
- return in_array($name, array('input', 'button', 'textarea', 'select'));
- }
-
- function acceptFormStart(&$tag) {
- $this->_open_forms[] = &new SimpleForm($tag, $this);
- }
-
- function acceptFormEnd() {
- if (count($this->_open_forms)) {
- $this->_complete_forms[] = array_pop($this->_open_forms);
- }
- }
-
- function acceptFramesetStart(&$tag) {
- if (! $this->_isLoadingFrames()) {
- $this->_frameset = &$tag;
- }
- $this->_frameset_nesting_level++;
- }
-
- function acceptFramesetEnd() {
- if ($this->_isLoadingFrames()) {
- $this->_frameset_nesting_level--;
- }
- }
-
- function acceptFrame(&$tag) {
- if ($this->_isLoadingFrames()) {
- if ($tag->getAttribute('src')) {
- $this->_frames[] = &$tag;
- }
- }
- }
-
- function _isLoadingFrames() {
- if (! $this->_frameset) {
- return false;
- }
- return ($this->_frameset_nesting_level > 0);
- }
-
- function _linkIsAbsolute($url) {
- $parsed = new SimpleUrl($url);
- return (boolean)($parsed->getScheme() && $parsed->getHost());
- }
-
- function _addLink($tag) {
- $this->_links[] = $tag;
- }
-
- function acceptPageEnd() {
- while (count($this->_open_forms)) {
- $this->_complete_forms[] = array_pop($this->_open_forms);
- }
- foreach ($this->_left_over_labels as $label) {
- for ($i = 0, $count = count($this->_complete_forms); $i < $count; $i++) {
- $this->_complete_forms[$i]->attachLabelBySelector(
- new SimpleById($label->getFor()),
- $label->getText());
- }
- }
- }
-
- function hasFrames() {
- return (boolean)$this->_frameset;
- }
-
- function getFrameset() {
- if (! $this->_frameset) {
- return false;
- }
- $urls = array();
- for ($i = 0; $i < count($this->_frames); $i++) {
- $name = $this->_frames[$i]->getAttribute('name');
- $url = new SimpleUrl($this->_frames[$i]->getAttribute('src'));
- $urls[$name ? $name : $i + 1] = $this->expandUrl($url);
- }
- return $urls;
- }
-
- function getFrames() {
- $url = $this->expandUrl($this->getUrl());
- return $url->asString();
- }
-
- function getUrls() {
- $all = array();
- foreach ($this->_links as $link) {
- $url = $this->_getUrlFromLink($link);
- $all[] = $url->asString();
- }
- return $all;
- }
-
- function getUrlsByLabel($label) {
- $matches = array();
- foreach ($this->_links as $link) {
- if ($link->getText() == $label) {
- $matches[] = $this->_getUrlFromLink($link);
- }
- }
- return $matches;
- }
-
- function getUrlById($id) {
- foreach ($this->_links as $link) {
- if ($link->getAttribute('id') === (string)$id) {
- return $this->_getUrlFromLink($link);
- }
- }
- return false;
- }
-
- function _getUrlFromLink($link) {
- $url = $this->expandUrl($link->getHref());
- if ($link->getAttribute('target')) {
- $url->setTarget($link->getAttribute('target'));
- }
- return $url;
- }
-
- function expandUrl($url) {
- if (! is_object($url)) {
- $url = new SimpleUrl($url);
- }
- $location = $this->getBaseUrl() ? $this->getBaseUrl() : new SimpleUrl();
- return $url->makeAbsolute($location->makeAbsolute($this->getUrl()));
- }
-
- function _setBase(&$tag) {
- $url = $tag->getAttribute('href');
- $this->_base = new SimpleUrl($url);
- }
-
- function _setTitle(&$tag) {
- $this->_title = &$tag;
- }
-
- function getTitle() {
- if ($this->_title) {
- return $this->_title->getText();
- }
- return false;
- }
-
- function &getFormBySubmit($selector) {
- for ($i = 0; $i < count($this->_complete_forms); $i++) {
- if ($this->_complete_forms[$i]->hasSubmit($selector)) {
- return $this->_complete_forms[$i];
- }
- }
- $null = null;
- return $null;
- }
-
- function &getFormByImage($selector) {
- for ($i = 0; $i < count($this->_complete_forms); $i++) {
- if ($this->_complete_forms[$i]->hasImage($selector)) {
- return $this->_complete_forms[$i];
- }
- }
- $null = null;
- return $null;
- }
-
- function &getFormById($id) {
- for ($i = 0; $i < count($this->_complete_forms); $i++) {
- if ($this->_complete_forms[$i]->getId() == $id) {
- return $this->_complete_forms[$i];
- }
- }
- $null = null;
- return $null;
- }
-
- function setField($selector, $value, $position=false) {
- $is_set = false;
- for ($i = 0; $i < count($this->_complete_forms); $i++) {
- if ($this->_complete_forms[$i]->setField($selector, $value, $position)) {
- $is_set = true;
- }
- }
- return $is_set;
- }
-
- function getField($selector) {
- for ($i = 0; $i < count($this->_complete_forms); $i++) {
- $value = $this->_complete_forms[$i]->getValue($selector);
- if (isset($value)) {
- return $value;
- }
- }
- return null;
- }
- }
- ?>
|