9574cd032ac6c2bd14448d199f626614ac4caabb
[linksys-wrt3g-tools.git] / Wrt3g / HtmlParser.php
1 <?php
2 /**
3  * HTML parsing for the linksys router HTML pages
4  *
5  * PHP version 5
6  *
7  * @category Tools
8  * @package  linksys-wrt3g-tools
9  * @author   Christian Weiske <cweiske@cweiske.de>
10  * @license  AGPL v3
11  * @link     http://cweiske.de/linksys-wrt3g-tools.htm
12  */
13 class Wrt3g_HtmlParser
14 {
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',
31     );
32
33
34
35     /**
36      * Parses the body of /index_wstatus1.asp and returns extracted values.
37      * If no connection is established, the values are NULL.
38      *
39      * @param string $body HTML document to parse
40      *
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
45      */
46     public function index_wstatus1($body)
47     {
48         $doc = new DomDocument();
49         $doc->loadHtml($body);
50                 $xpath = new DOMXPath($doc);
51                 $entries = $xpath->query('//table/tr/td');
52
53         $arMatches = array();
54                 foreach ($entries as $entry) {
55             $s = $doc->saveXML($entry);
56             $s = str_replace(
57                 array('<![CDATA[', ']]>'),
58                 '', $s
59             );
60             $s = strip_tags($s);
61             //some strange utf8 space char
62             $s = str_replace("\xC2\xA0", ' ', $s);
63             $s = trim($s);
64             $s = str_replace(
65                 array('Capture(', ') :'),
66                 '', $s
67             );
68             if (substr($s, -1) == ')') {
69                 $s = substr($s, 0, -1);
70             }
71             $arMatches[] = $s;
72         }
73
74
75         $arRetval = array();
76         $arData = array();
77         reset($arMatches);
78         while (current($arMatches)) {
79             $key   = current($arMatches);
80             $value = next($arMatches);
81             if (isset(self::$arTranslations[$key])) {
82                 $key = self::$arTranslations[$key];
83             }
84             if (isset(self::$arTranslations[$value])) {
85                 $value = self::$arTranslations[$value];
86             }
87             if ($value == 'aquiring') {
88                 $value = null;
89             }
90             $arRetval[$key] = $value;
91             next($arMatches);
92         }
93
94         return $arRetval;
95     }
96
97
98
99     /**
100      * Parses the body of /index_wstatus2.asp and returns extracted values.
101      *
102      * @param string $body HTML document to parse
103      *
104      * @return array Array with "connection" as key and one of "connecting",
105      *               "connected" or "disconnected" as value.
106      */
107     public function index_wstatus2($body)
108     {
109         preg_match('/var status2 = "(.+)"/', $body, $arMatches);
110         if (!isset($arMatches[1])) {
111             return array();
112         }
113
114         $strStatus = $arMatches[1];
115         //Connecting
116         //Disconnected
117         //Connected
118         return array('connection' => strtolower($strStatus));
119     }
120
121
122
123     /**
124      * Parses the HTML body of /Status_NoAuth.asp and returns the extracted
125      * values.
126      *
127      * @param string $body HTML document to parse
128      *
129      * @return array Array of key-value pairs with the following keys:
130      *               - type
131      *               - network
132      *               - signal strength
133      *               - connection time
134      *               - session usage (array)
135      *               - card model
136      *               - card revision
137      *               - card firmware
138      *               - IMSI
139      */
140     public function status_noauth($body)
141     {
142         $body = str_replace(
143             array('&nbsp;', '</font>', '<script>', '</script>'),
144             '', $body
145         );
146
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);
153         $bAquiring = false;
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;
161             }
162         }
163
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') {
169                     $bAquiring = true;
170                     $value = null;
171                 }
172             }
173         }
174         unset($value);
175
176         if ($bAquiring) {
177             $arRaw['connection'] = 'connecting';
178         } else if ($arRaw['type'] != null) {
179             $arRaw['connection'] = 'connected';
180         } else {
181             $arRaw['connection'] = 'disconnected';
182         }
183
184         if (isset($arRaw['connection time'])) {
185             $arRaw['connection time'] = str_replace(
186                 array(
187                     ' Capture(GPRS_MSG.HOURS)',
188                     ' Capture(GPRS_MSG.MINUTE)',
189                     ' Capture(GPRS_MSG.SECOND)',
190                 ),
191                 array(
192                     'h', 'm', 's'
193                 ),
194                 $arRaw['connection time']
195             );
196         }
197
198         if (isset($arRaw['session usage'])) {
199             $arS = explode(
200                 'TX = ',
201                 str_replace('RX = ', '', $arRaw['session usage'])
202             );
203             if (count($arS) > 1) {
204                 $arRaw['session usage'] = array(
205                     'received' => $arS[0],
206                     'sent'     => $arS[1]
207                 );
208             }
209         }
210
211         if (preg_match('/Capture\\(share.firmwarever\\):(.+?)</', $body, $arMatches)) {
212             $arRaw['router firmware'] = trim($arMatches[1]);
213         }
214
215         return $arRaw;
216     }
217 }