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.FAIR' => 'fair',
24 'GPRS_MSG.GOOD' => 'good',
25 'GPRS_MSG.EXCELLENT' => 'excellent',
26 'GPRS_MSG.NOSIGNAL' => 'no signal',
27 'GPRS_MSG.CTIME' => 'connection time',
28 'GPRS_MSG.CFW' => 'card firmware',
29 'GPRS_MSG.CMOD' => 'card model',
30 'GPRS_MSG.CREV' => 'card revision',
31 'GPRS_MSG.IMSI' => 'IMSI',
32 'GPRS_MSG.SESSIONUSAGE' => 'session usage',
38 * Parses the body of /index_wstatus1.asp and returns extracted values.
39 * If no connection is established, the values are NULL.
41 * @param string $body HTML document to parse
43 * @return array Array of key-value pairs, probably with
44 * - "type" - i.e. "GPRS"
45 * - "network" - Carrier network name, i.e. "BASE DE"
46 * - "signal strength" - good, excellent, poor
48 public function index_wstatus1($body)
50 $doc = new DomDocument();
51 $doc->loadHtml($body);
52 $xpath = new DOMXPath($doc);
53 $entries = $xpath->query('//table/tr/td');
56 foreach ($entries as $entry) {
57 $s = $doc->saveXML($entry);
59 array('<![CDATA[', ']]>'),
63 //some strange utf8 space char
64 $s = str_replace("\xC2\xA0", ' ', $s);
67 array('Capture(', ') :'),
70 if (substr($s, -1) == ')') {
71 $s = substr($s, 0, -1);
80 while (current($arMatches)) {
81 $key = current($arMatches);
82 $value = next($arMatches);
83 if (isset(self::$arTranslations[$key])) {
84 $key = self::$arTranslations[$key];
86 if (isset(self::$arTranslations[$value])) {
87 $value = self::$arTranslations[$value];
89 if ($value == 'aquiring') {
92 $arRetval[$key] = $value;
102 * Parses the body of /index_wstatus2.asp and returns extracted values.
104 * @param string $body HTML document to parse
106 * @return array Array with "connection" as key and one of "connecting",
107 * "connected" or "disconnected" as value.
109 public function index_wstatus2($body)
111 preg_match('/var status2 = "(.+)"/', $body, $arMatches);
112 if (!isset($arMatches[1])) {
116 $strStatus = $arMatches[1];
120 return array('connection' => strtolower($strStatus));
126 * Parses the HTML body of /Status_NoAuth.asp and returns the extracted
129 * @param string $body HTML document to parse
131 * @return array Array of key-value pairs with the following keys:
136 * - session usage (array)
142 public function status_noauth($body)
145 array(' ', '</font>', '<script>', '</script>'),
149 $doc = new DomDocument();
150 libxml_use_internal_errors(true);//html is broken
151 $doc->loadHtml($body);
152 $xpath = new DOMXPath($doc);
153 $trs = $xpath->query('//table//table/tbody/tr');
154 $arRaw = array('connection' => null);
156 foreach ($trs as $tr) {
157 $titleItems = $xpath->query('td[@width=125]/text()', $tr);
158 $valueItems = $xpath->query('td[@width=296]/*[1]', $tr);
159 if ($titleItems->length && $valueItems->length) {
160 $title = substr($doc->saveXML($titleItems->item(0)), 8, -2);
161 $value = $doc->saveXML($valueItems->item(0));
162 $arRaw[self::$arTranslations[$title]] = $value;
166 foreach ($arRaw as $key => &$value) {
167 $value = trim(strip_tags($value));
168 if (substr($value, 0, 8) == 'Capture(') {
169 $value = self::$arTranslations[substr($value, 8, -1)];
170 if ($value == 'aquiring') {
179 $arRaw['connection'] = 'connecting';
180 } else if ($arRaw['type'] != null) {
181 $arRaw['connection'] = 'connected';
183 $arRaw['connection'] = 'disconnected';
186 if (isset($arRaw['connection time'])) {
187 $arRaw['connection time'] = str_replace(
189 ' Capture(GPRS_MSG.HOURS)',
190 ' Capture(GPRS_MSG.MINUTE)',
191 ' Capture(GPRS_MSG.SECOND)',
196 $arRaw['connection time']
200 if (isset($arRaw['session usage'])) {
203 str_replace('RX = ', '', $arRaw['session usage'])
205 if (count($arS) > 1) {
206 $arRaw['session usage'] = array(
207 'received' => $arS[0],
213 if (preg_match('/Capture\\(share.firmwarever\\):(.+?)</', $body, $arMatches)) {
214 $arRaw['router firmware'] = trim($arMatches[1]);