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