* @license AGPL v3 * @link http://cweiske.de/linksys-wrt3g-tools.htm */ class Wrt3g_HtmlParser { protected static $arTranslations = array( 'GPRS_MSG.WWBEAR' => 'type', 'GPRS_MSG.NNAME' => 'network', 'GPRS_MSG.STRENGTH' => 'signal strength', 'GPRS_MSG.ACQUIRING' => 'aquiring', 'GPRS_MSG.WWBEAR_GPRS' => 'GPRS', 'GPRS_MSG.WWBEAR_UMTS' => 'UMTS', 'GPRS_MSG.GOOD' => 'good', 'GPRS_MSG.EXCELLENT' => 'excellent', ); /** * Parses the body of /index_wstatus1.asp and returns extracted values. * * @param string $body HTML document to parse * * @return array Array of key-value pairs, probably with "type", "network" and * "signal strength" */ public function index_wstatus1($body) { $doc = new DomDocument(); $doc->loadHtml($body); $xpath = new DOMXPath($doc); $entries = $xpath->query('//table/tr/td'); $arMatches = array(); foreach ($entries as $entry) { $s = $doc->saveXML($entry); $s = str_replace( array(''), '', $s ); $s = strip_tags($s); //some strange utf8 space char $s = str_replace("\xC2\xA0", ' ', $s); $s = trim($s); $s = str_replace( array('Capture(', ') :'), '', $s ); if (substr($s, -1) == ')') { $s = substr($s, 0, -1); } $arMatches[] = $s; } $arRetval = array(); $arData = array(); reset($arMatches); while (current($arMatches)) { $key = current($arMatches); $value = next($arMatches); if (isset(self::$arTranslations[$key])) { $key = self::$arTranslations[$key]; } if (isset(self::$arTranslations[$value])) { $value = self::$arTranslations[$value]; } $arRetval[$key] = $value; next($arMatches); } return $arRetval; } /** * Parses the body of /index_wstatus2.asp and returns extracted values. * * @param string $body HTML document to parse * * @return array Array with "connection" as key and one of "connecting", * "connected" or "disconnected" as value. */ public function index_wstatus2($body) { preg_match('/var status2 = "(.+)"/', $body, $arMatches); $strStatus = $arMatches[1]; //Connecting //Disconnected //Connected return array('connection' => strtolower($strStatus)); } /** * Parses the HTML body of /Status_NoAuth.asp and returns the extracted * values. * * @param string $body HTML document to parse * * @return array FIXME */ public function status_noauth($body) { $body = str_replace( array(' ', '', ''), '', $body ); //var_dump($body);die(); $doc = new DomDocument(); libxml_use_internal_errors(true);//html is broken $doc->loadHtml($body); $xpath = new DOMXPath($doc); $trs = $xpath->query('//table//table/tbody/tr'); foreach ($trs as $tr) { /* var_dump( '--------',$doc->saveXML($tr), $xpath->query('td', $tr)->length); */ $titleItems = $xpath->query('td[@width=125]/text()', $tr); $valueItems = $xpath->query('td[@width=296]/*[1]', $tr); if ($titleItems->length && $valueItems->length) { $title = $titleItems->item(0); $value = $valueItems->item(0); var_dump( $doc->saveXML($title) . "\n" . $doc->saveXML($value) . "\n\n" ); } } $arMatches = array(); } }