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