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.None' => null,
23 'GPRS_MSG.GOOD' => 'good',
24 'GPRS_MSG.EXCELLENT' => 'excellent',
25 'GPRS_MSG.CTIME' => 'connection time',
26 'GPRS_MSG.CFW' => 'card firmware',
27 'GPRS_MSG.CMOD' => 'card model',
28 'GPRS_MSG.CREV' => 'card revision',
29 'GPRS_MSG.IMSI' => 'IMSI',
30 'GPRS_MSG.SESSIONUSAGE' => 'session usage',
36 * Parses the body of /index_wstatus1.asp and returns extracted values.
37 * If no connection is established, the values are NULL.
39 * @param string $body HTML document to parse
41 * @return array Array of key-value pairs, probably with
42 * - "type" - i.e. "GPRS"
43 * - "network" - Carrier network name, i.e. "BASE DE"
44 * - "signal strength" - good, excellent, poor
46 public function index_wstatus1($body)
48 $doc = new DomDocument();
49 $doc->loadHtml($body);
50 $xpath = new DOMXPath($doc);
51 $entries = $xpath->query('//table/tr/td');
54 foreach ($entries as $entry) {
55 $s = $doc->saveXML($entry);
57 array('<![CDATA[', ']]>'),
61 //some strange utf8 space char
62 $s = str_replace("\xC2\xA0", ' ', $s);
65 array('Capture(', ') :'),
68 if (substr($s, -1) == ')') {
69 $s = substr($s, 0, -1);
78 while (current($arMatches)) {
79 $key = current($arMatches);
80 $value = next($arMatches);
81 if (isset(self::$arTranslations[$key])) {
82 $key = self::$arTranslations[$key];
84 if (isset(self::$arTranslations[$value])) {
85 $value = self::$arTranslations[$value];
87 if ($value == 'aquiring') {
90 $arRetval[$key] = $value;
100 * Parses the body of /index_wstatus2.asp and returns extracted values.
102 * @param string $body HTML document to parse
104 * @return array Array with "connection" as key and one of "connecting",
105 * "connected" or "disconnected" as value.
107 public function index_wstatus2($body)
109 preg_match('/var status2 = "(.+)"/', $body, $arMatches);
110 if (!isset($arMatches[1])) {
114 $strStatus = $arMatches[1];
118 return array('connection' => strtolower($strStatus));
124 * Parses the HTML body of /Status_NoAuth.asp and returns the extracted
127 * @param string $body HTML document to parse
129 * @return array Array of key-value pairs with the following keys:
134 * - session usage (array)
140 public function status_noauth($body)
143 array(' ', '</font>', '<script>', '</script>'),
147 $doc = new DomDocument();
148 libxml_use_internal_errors(true);//html is broken
149 $doc->loadHtml($body);
150 $xpath = new DOMXPath($doc);
151 $trs = $xpath->query('//table//table/tbody/tr');
152 $arRaw = array('connection' => null);
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)];
168 if ($value == 'aquiring') {
177 $arRaw['connection'] = 'connecting';
178 } else if ($arRaw['type'] != null) {
179 $arRaw['connection'] = 'connected';
181 $arRaw['connection'] = 'disconnected';
184 if (isset($arRaw['connection time'])) {
185 $arRaw['connection time'] = str_replace(
187 ' Capture(GPRS_MSG.HOURS)',
188 ' Capture(GPRS_MSG.MINUTE)',
189 ' Capture(GPRS_MSG.SECOND)',
194 $arRaw['connection time']
198 if (isset($arRaw['session usage'])) {
201 str_replace('RX = ', '', $arRaw['session usage'])
203 if (count($arS) > 1) {
204 $arRaw['session usage'] = array(
205 'received' => $arS[0],
211 if (preg_match('/Capture\\(share.firmwarever\\):(.+?)</', $body, $arMatches)) {
212 $arRaw['router firmware'] = trim($arMatches[1]);