add support for disconnected state in index_wstatus1
[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.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',
30     );
31
32
33
34     /**
35      * Parses the body of /index_wstatus1.asp and returns extracted values.
36      * If no connection is established, the values are NULL.
37      *
38      * @param string $body HTML document to parse
39      *
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
44      */
45     public function index_wstatus1($body)
46     {
47         $doc = new DomDocument();
48         $doc->loadHtml($body);
49                 $xpath = new DOMXPath($doc);
50                 $entries = $xpath->query('//table/tr/td');
51
52         $arMatches = array();
53                 foreach ($entries as $entry) {
54             $s = $doc->saveXML($entry);
55             $s = str_replace(
56                 array('<![CDATA[', ']]>'),
57                 '', $s
58             );
59             $s = strip_tags($s);
60             //some strange utf8 space char
61             $s = str_replace("\xC2\xA0", ' ', $s);
62             $s = trim($s);
63             $s = str_replace(
64                 array('Capture(', ') :'),
65                 '', $s
66             );
67             if (substr($s, -1) == ')') {
68                 $s = substr($s, 0, -1);
69             }
70             $arMatches[] = $s;
71         }
72
73
74         $arRetval = array();
75         $arData = array();
76         reset($arMatches);
77         while (current($arMatches)) {
78             $key   = current($arMatches);
79             $value = next($arMatches);
80             if (isset(self::$arTranslations[$key])) {
81                 $key = self::$arTranslations[$key];
82             }
83             if (isset(self::$arTranslations[$value])) {
84                 $value = self::$arTranslations[$value];
85             }
86             if ($value == 'aquiring') {
87                 $value = null;
88             }
89             $arRetval[$key] = $value;
90             next($arMatches);
91         }
92
93         return $arRetval;
94     }
95
96
97
98     /**
99      * Parses the body of /index_wstatus2.asp and returns extracted values.
100      *
101      * @param string $body HTML document to parse
102      *
103      * @return array Array with "connection" as key and one of "connecting",
104      *               "connected" or "disconnected" as value.
105      */
106     public function index_wstatus2($body)
107     {
108         preg_match('/var status2 = "(.+)"/', $body, $arMatches);
109         if (!isset($arMatches[1])) {
110             return array();
111         }
112
113         $strStatus = $arMatches[1];
114         //Connecting
115         //Disconnected
116         //Connected
117         return array('connection' => strtolower($strStatus));
118     }
119
120
121
122     /**
123      * Parses the HTML body of /Status_NoAuth.asp and returns the extracted
124      * values.
125      *
126      * @param string $body HTML document to parse
127      *
128      * @return array Array of key-value pairs with the following keys:
129      *               - type
130      *               - network
131      *               - signal strength
132      *               - connection time
133      *               - session usage (array)
134      *               - card model
135      *               - card revision
136      *               - card firmware
137      *               - IMSI
138      *
139      * @todo FIXME implement "connection" value as index_wstatus2() does it
140      */
141     public function status_noauth($body)
142     {
143         $body = str_replace(
144             array('&nbsp;', '</font>', '<script>', '</script>'),
145             '', $body
146         );
147
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');
153         $arRaw = array();
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             }
169         }
170         unset($value);
171
172         if (isset($arRaw['connection time'])) {
173             $arRaw['connection time'] = str_replace(
174                 array(
175                     ' Capture(GPRS_MSG.HOURS)',
176                     ' Capture(GPRS_MSG.MINUTE)',
177                     ' Capture(GPRS_MSG.SECOND)',
178                 ),
179                 array(
180                     'h', 'm', 's'
181                 ),
182                 $arRaw['connection time']
183             );
184         }
185
186         if (isset($arRaw['session usage'])) {
187             $arS = explode(
188                 'TX = ',
189                 str_replace('RX = ', '', $arRaw['session usage'])
190             );
191             $arRaw['session usage'] = array(
192                 'received' => $arS[0],
193                 'sent'     => $arS[1]
194             );
195         }
196
197         return $arRaw;
198     }
199 }