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
44 public function index_wstatus1($body)
46 $doc = new DomDocument();
47 $doc->loadHtml($body);
48 $xpath = new DOMXPath($doc);
49 $entries = $xpath->query('//table/tr/td');
52 foreach ($entries as $entry) {
53 $s = $doc->saveXML($entry);
55 array('<![CDATA[', ']]>'),
59 //some strange utf8 space char
60 $s = str_replace("\xC2\xA0", ' ', $s);
63 array('Capture(', ') :'),
66 if (substr($s, -1) == ')') {
67 $s = substr($s, 0, -1);
76 while (current($arMatches)) {
77 $key = current($arMatches);
78 $value = next($arMatches);
79 if (isset(self::$arTranslations[$key])) {
80 $key = self::$arTranslations[$key];
82 if (isset(self::$arTranslations[$value])) {
83 $value = self::$arTranslations[$value];
85 $arRetval[$key] = $value;
95 * Parses the body of /index_wstatus2.asp and returns extracted values.
97 * @param string $body HTML document to parse
99 * @return array Array with "connection" as key and one of "connecting",
100 * "connected" or "disconnected" as value.
102 public function index_wstatus2($body)
104 preg_match('/var status2 = "(.+)"/', $body, $arMatches);
106 $strStatus = $arMatches[1];
110 return array('connection' => strtolower($strStatus));
116 * Parses the HTML body of /Status_NoAuth.asp and returns the extracted
119 * @param string $body HTML document to parse
121 * @return array Array of key-value pairs with the following keys:
126 * - session usage (array)
132 * @todo FIXME implement "connection" value as index_wstatus2() does it
134 public function status_noauth($body)
137 array(' ', '</font>', '<script>', '</script>'),
141 $doc = new DomDocument();
142 libxml_use_internal_errors(true);//html is broken
143 $doc->loadHtml($body);
144 $xpath = new DOMXPath($doc);
145 $trs = $xpath->query('//table//table/tbody/tr');
147 foreach ($trs as $tr) {
148 $titleItems = $xpath->query('td[@width=125]/text()', $tr);
149 $valueItems = $xpath->query('td[@width=296]/*[1]', $tr);
150 if ($titleItems->length && $valueItems->length) {
151 $title = substr($doc->saveXML($titleItems->item(0)), 8, -2);
152 $value = $doc->saveXML($valueItems->item(0));
153 $arRaw[self::$arTranslations[$title]] = $value;
157 foreach ($arRaw as $key => &$value) {
158 $value = trim(strip_tags($value));
159 if (substr($value, 0, 8) == 'Capture(') {
160 $value = self::$arTranslations[substr($value, 8, -1)];
165 if (isset($arRaw['connection time'])) {
166 $arRaw['connection time'] = str_replace(
168 ' Capture(GPRS_MSG.HOURS)',
169 ' Capture(GPRS_MSG.MINUTE)',
170 ' Capture(GPRS_MSG.SECOND)',
175 $arRaw['connection time']
179 if (isset($arRaw['session usage'])) {
182 str_replace('RX = ', '', $arRaw['session usage'])
184 $arRaw['session usage'] = array(
185 'received' => $arS[0],