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.
36 * If no connection is established, the values are NULL.
38 * @param string $body HTML document to parse
40 * @return array Array of key-value pairs, probably with
41 * - "type" - i.e. "GPRS"
42 * - "network" - Carrier network name, i.e. "BASE DE"
43 * - "signal strength" - good, excellent, poor
45 public function index_wstatus1($body)
47 $doc = new DomDocument();
48 $doc->loadHtml($body);
49 $xpath = new DOMXPath($doc);
50 $entries = $xpath->query('//table/tr/td');
53 foreach ($entries as $entry) {
54 $s = $doc->saveXML($entry);
56 array('<![CDATA[', ']]>'),
60 //some strange utf8 space char
61 $s = str_replace("\xC2\xA0", ' ', $s);
64 array('Capture(', ') :'),
67 if (substr($s, -1) == ')') {
68 $s = substr($s, 0, -1);
77 while (current($arMatches)) {
78 $key = current($arMatches);
79 $value = next($arMatches);
80 if (isset(self::$arTranslations[$key])) {
81 $key = self::$arTranslations[$key];
83 if (isset(self::$arTranslations[$value])) {
84 $value = self::$arTranslations[$value];
86 if ($value == 'aquiring') {
89 $arRetval[$key] = $value;
99 * Parses the body of /index_wstatus2.asp and returns extracted values.
101 * @param string $body HTML document to parse
103 * @return array Array with "connection" as key and one of "connecting",
104 * "connected" or "disconnected" as value.
106 public function index_wstatus2($body)
108 preg_match('/var status2 = "(.+)"/', $body, $arMatches);
109 if (!isset($arMatches[1])) {
113 $strStatus = $arMatches[1];
117 return array('connection' => strtolower($strStatus));
123 * Parses the HTML body of /Status_NoAuth.asp and returns the extracted
126 * @param string $body HTML document to parse
128 * @return array Array of key-value pairs with the following keys:
133 * - session usage (array)
139 * @todo FIXME implement "connection" value as index_wstatus2() does it
141 public function status_noauth($body)
144 array(' ', '</font>', '<script>', '</script>'),
148 $doc = new DomDocument();
149 libxml_use_internal_errors(true);//html is broken
150 $doc->loadHtml($body);
151 $xpath = new DOMXPath($doc);
152 $trs = $xpath->query('//table//table/tbody/tr');
154 foreach ($trs as $tr) {
155 $titleItems = $xpath->query('td[@width=125]/text()', $tr);
156 $valueItems = $xpath->query('td[@width=296]/*[1]', $tr);
157 if ($titleItems->length && $valueItems->length) {
158 $title = substr($doc->saveXML($titleItems->item(0)), 8, -2);
159 $value = $doc->saveXML($valueItems->item(0));
160 $arRaw[self::$arTranslations[$title]] = $value;
164 foreach ($arRaw as $key => &$value) {
165 $value = trim(strip_tags($value));
166 if (substr($value, 0, 8) == 'Capture(') {
167 $value = self::$arTranslations[substr($value, 8, -1)];
172 if (isset($arRaw['connection time'])) {
173 $arRaw['connection time'] = str_replace(
175 ' Capture(GPRS_MSG.HOURS)',
176 ' Capture(GPRS_MSG.MINUTE)',
177 ' Capture(GPRS_MSG.SECOND)',
182 $arRaw['connection time']
186 if (isset($arRaw['session usage'])) {
189 str_replace('RX = ', '', $arRaw['session usage'])
191 $arRaw['session usage'] = array(
192 'received' => $arS[0],