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',
24 'GPRS_MSG.CTIME' => 'connection time',
25 'GPRS_MSG.CFW' => 'card firmware',
26 'GPRS_MSG.CMOD' => 'card model',
27 'GPRS_MSG.CREV' => 'card revision',
28 'GPRS_MSG.IMSI' => 'IMSI',
29 'GPRS_MSG.SESSIONUSAGE' => 'session usage',
35 * Parses the body of /index_wstatus1.asp and returns extracted values.
37 * @param string $body HTML document to parse
39 * @return array Array of key-value pairs, probably with "type", "network" and
42 public function index_wstatus1($body)
44 $doc = new DomDocument();
45 $doc->loadHtml($body);
46 $xpath = new DOMXPath($doc);
47 $entries = $xpath->query('//table/tr/td');
50 foreach ($entries as $entry) {
51 $s = $doc->saveXML($entry);
53 array('<![CDATA[', ']]>'),
57 //some strange utf8 space char
58 $s = str_replace("\xC2\xA0", ' ', $s);
61 array('Capture(', ') :'),
64 if (substr($s, -1) == ')') {
65 $s = substr($s, 0, -1);
74 while (current($arMatches)) {
75 $key = current($arMatches);
76 $value = next($arMatches);
77 if (isset(self::$arTranslations[$key])) {
78 $key = self::$arTranslations[$key];
80 if (isset(self::$arTranslations[$value])) {
81 $value = self::$arTranslations[$value];
83 $arRetval[$key] = $value;
93 * Parses the body of /index_wstatus2.asp and returns extracted values.
95 * @param string $body HTML document to parse
97 * @return array Array with "connection" as key and one of "connecting",
98 * "connected" or "disconnected" as value.
100 public function index_wstatus2($body)
102 preg_match('/var status2 = "(.+)"/', $body, $arMatches);
104 $strStatus = $arMatches[1];
108 return array('connection' => strtolower($strStatus));
114 * Parses the HTML body of /Status_NoAuth.asp and returns the extracted
117 * @param string $body HTML document to parse
119 * @return array FIXME
121 public function status_noauth($body)
124 array(' ', '</font>', '<script>', '</script>'),
128 $doc = new DomDocument();
129 libxml_use_internal_errors(true);//html is broken
130 $doc->loadHtml($body);
131 $xpath = new DOMXPath($doc);
132 $trs = $xpath->query('//table//table/tbody/tr');
134 foreach ($trs as $tr) {
135 $titleItems = $xpath->query('td[@width=125]/text()', $tr);
136 $valueItems = $xpath->query('td[@width=296]/*[1]', $tr);
137 if ($titleItems->length && $valueItems->length) {
138 $title = substr($doc->saveXML($titleItems->item(0)), 8, -2);
139 $value = $doc->saveXML($valueItems->item(0));
140 $arRaw[self::$arTranslations[$title]] = $value;
144 foreach ($arRaw as $key => &$value) {
145 $value = trim(strip_tags($value));
146 if (substr($value, 0, 8) == 'Capture(') {
147 $value = self::$arTranslations[substr($value, 8, -1)];
152 if (isset($arRaw['connection time'])) {
153 $arRaw['connection time'] = str_replace(
155 ' Capture(GPRS_MSG.HOURS)',
156 ' Capture(GPRS_MSG.MINUTE)',
157 ' Capture(GPRS_MSG.SECOND)',
162 $arRaw['connection time']
166 if (isset($arRaw['session usage'])) {
169 str_replace('RX = ', '', $arRaw['session usage'])
171 $arRaw['session usage'] = array(
172 'received' => $arS[0],