3 * HTML parsing for the linksys router HTML pages
8 * @package linksys-wrt3g-tools
9 * @author Christian Weiske <cweiske@cweiske.de>
11 * @link http://cweiske.de/linksys-wrt3g-tools.htm
13 class Wrt3g_HtmlParser
15 protected static $arTranslations = array(
16 'GPRS_MSG.WWBEAR' => 'type',
17 'GPRS_MSG.NNAME' => 'network',
18 'GPRS_MSG.STRENGTH' => 'signal strength',
19 'GPRS_MSG.ACQUIRING' => 'aquiring',
20 'GPRS_MSG.WWBEAR_GPRS' => 'GPRS',
21 'GPRS_MSG.WWBEAR_UMTS' => 'UMTS',
22 'GPRS_MSG.GOOD' => 'good',
23 'GPRS_MSG.EXCELLENT' => 'excellent',
29 * Parses the body of /index_wstatus1.asp and returns extracted values.
31 * @param string $body HTML document to parse
33 * @return array Array of key-value pairs, probably with "type", "network" and
36 public function index_wstatus1($body)
38 $doc = new DomDocument();
39 $doc->loadHtml($body);
40 $xpath = new DOMXPath($doc);
41 $entries = $xpath->query('//table/tr/td');
44 foreach ($entries as $entry) {
45 $s = $doc->saveXML($entry);
47 array('<![CDATA[', ']]>'),
51 //some strange utf8 space char
52 $s = str_replace("\xC2\xA0", ' ', $s);
55 array('Capture(', ') :'),
58 if (substr($s, -1) == ')') {
59 $s = substr($s, 0, -1);
68 while (current($arMatches)) {
69 $key = current($arMatches);
70 $value = next($arMatches);
71 if (isset(self::$arTranslations[$key])) {
72 $key = self::$arTranslations[$key];
74 if (isset(self::$arTranslations[$value])) {
75 $value = self::$arTranslations[$value];
77 $arRetval[$key] = $value;
87 * Parses the body of /index_wstatus2.asp and returns extracted values.
89 * @param string $body HTML document to parse
91 * @return array Array with "connection" as key and one of "connecting",
92 * "connected" or "disconnected" as value.
94 public function index_wstatus2($body)
96 preg_match('/var status2 = "(.+)"/', $body, $arMatches);
98 $strStatus = $arMatches[1];
102 return array('connection' => strtolower($strStatus));
108 * Parses the HTML body of /Status_NoAuth.asp and returns the extracted
111 * @param string $body HTML document to parse
113 * @return array FIXME
115 public function status_noauth($body)
118 array(' ', '</font>', '<script>', '</script>'),
121 //var_dump($body);die();
122 $doc = new DomDocument();
123 libxml_use_internal_errors(true);//html is broken
124 $doc->loadHtml($body);
125 $xpath = new DOMXPath($doc);
126 $trs = $xpath->query('//table//table/tbody/tr');
127 foreach ($trs as $tr) {
130 '--------',$doc->saveXML($tr),
131 $xpath->query('td', $tr)->length);
133 $titleItems = $xpath->query('td[@width=125]/text()', $tr);
134 $valueItems = $xpath->query('td[@width=296]/*[1]', $tr);
135 if ($titleItems->length && $valueItems->length) {
136 $title = substr($doc->saveXML($titleItems->item(0)), 8, -2);
137 $value = $doc->saveXML($valueItems->item(0));
145 $arMatches = array();