/** * Fix problems PHP DOM parser have with ais-generated html. * * The major problem is <script> tag inside which are the table data. * Here, we fix these <script> tags replacing them as <div> tags. * * @param Trace $trace * @param string $html html code to fix * * @returns string fixed html code ready for DOM parsing. */ public function fixProblematicTags(Trace $trace, $html) { Preconditions::checkIsString($html); $html = str_replace("<!--", "", $html); $html = str_replace("-->", "", $html); $html = str_replace("script", "div", $html); $trace->tlogVariable("Fixed html", $html); return $html; }
public function getOptions(Trace $trace, DOMElement $element) { $options = array(); $list = $element->getElementsByTagName('option'); foreach ($list as $node) { $value = $this->fixNbsp($node->textContent); $options[] = $value; } $trace->tlogVariable("options", $options); return $options; }
/** * Parse a line of proxy file in a string * * @param Trace $trace trace object * @param string $line not including line termination characters * @returns array service, value and domain from parsed line, indexed by * those strings or false if this is not correct proxy line */ private function parseString(Trace $trace, $line) { Preconditions::checkIsString($line, '$line should be string.'); $matches = array(); if (!preg_match(self::PROXY_LINE_PATTERN, $line, $matches)) { $trace->tlog('Line did not match'); $trace->tlogVariable('line', $line); throw new ParseException('Proxy file line does not match'); } try { return new CosignServiceCookie($matches[1], $matches[2], $matches[3]); } catch (InvalidArgumentException $e) { throw new ParseException('Proxy file arguments are invalid', null, $e); } }
private function prepareResponse(Trace $trace, $html) { // fixing html code, so DOMDocumet it can parse Preconditions::checkIsString($html); $html = str_replace("<!--", "", $html); $html = str_replace("-->", "", $html); // just first javascript code contain data which we want $count = 1; $html = str_replace("type='application/javascript'", "id='init-data'", $html, $count); $html = str_replace("script", "div", $html); $html = $this->fixNbsp($html); $trace->tlogVariable("Fixed html", $html); // creating DOMDocument $dom = new DOMDocument(); $trace->tlog("Loading html to DOM"); $loaded = @$dom->loadHTML($html); if (!$loaded) { throw new ParseException("Problem parsing html to DOM."); } //fixing id atributes $trace->tlog('Fixing id attributes in the DOM'); $xpath = new DOMXPath($dom); $nodes = $xpath->query("//*[@id]"); foreach ($nodes as $node) { // Note: do not erase next line. @see // http://www.navioo.com/php/docs/function.dom-domelement-setidattribute.php // for explanation! $node->setIdAttribute('id', false); $node->setIdAttribute('id', true); } return $dom; }
/** * Add a serial number to the xml request and make this requset happen * * @param DOMDocument $action xml request withou serial number */ public function doActionRequest(Trace $trace, $action) { $finalRequest = new DOMDocument(); $request = $finalRequest->createElement('request'); $serialNumber = $this->requestBuilder->newSerial(); $serial = $finalRequest->createElement('serial', $serialNumber); $request->appendChild($serial); $finalRequest->appendChild($request); foreach ($action->childNodes as $element) { $element = $finalRequest->importNode($element, true); $finalRequest->documentElement->appendChild($element); } $data = array('xml_spec' => $finalRequest->saveXML($finalRequest->documentElement)); $trace->tlogVariable('xml req', $data['xml_spec']); return $this->connection->request($trace, $this->getRequestUrl(), $data); }
/** * Returns user's full name as reported by ais. * * @returns string */ public function getFullUserName(Trace $trace) { $userNameParser = new AIS2UserNameParser(); $simpleConn = $this->connection->getSimpleConnection(); $urlMap = $this->connection->getUrlMap(); $html = $simpleConn->request($trace->addChild('requesting AIS2 main page'), $urlMap->getStartPageUrl()); $html = $this->convertEncoding($html); $username = $userNameParser->parseUserNameFromMainPage($html); $trace->tlogVariable('username', $username); return $username; }
private function exec(Trace $trace) { // read cookie file $this->_curlSetOption(CURLOPT_COOKIEFILE, $this->cookieFile); $output = curl_exec($this->curl); $response_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); $content_type = curl_getinfo($this->curl, CURLINFO_CONTENT_TYPE); $tags = array('http-response-code' => $response_code, 'content-type' => $content_type); $trace->tlogVariable("Response", $output, $tags); $this->processStatistics($this->curl); if (curl_errno($this->curl)) { $trace->tlog("There was an error receiving data"); throw new Exception("Chyba pri nadväzovaní spojenia:" . curl_error($this->curl)); } // Do not forget to save current file content $this->_curlSetOption(CURLOPT_COOKIEJAR, $this->cookieFile); return $output; }
public function getTableData(Trace $trace, DOMDocument $dom) { $data = array(); $trace->tlog("finding tbody element"); $element = $dom->getElementById('dataTabBody0'); if ($element == null) { throw new ParseException("Can't find table data"); } foreach ($element->childNodes as $aisRow) { assert($aisRow->tagName == "tr"); assert($aisRow->hasAttribute("id")); assert($aisRow->hasChildNodes()); // TODO: asserty prerobit na exceptiony $row = array(); $rowId = $aisRow->getAttribute("id"); $index = StrUtil::match('@^row_([0-9]+)$@', $rowId); if ($index === false) { throw new ParseException("Unexpected row id format"); } foreach ($aisRow->childNodes as $ais_td) { assert($ais_td->tagName == "td"); $row[] = $this->getCellContent($ais_td); } $data[$index] = $row; } $trace->tlogVariable("data", $data); return $data; }