fix E_NOTICE when parsing empty html
[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         if (!isset($arMatches[1])) {
106             return array();
107         }
108
109         $strStatus = $arMatches[1];
110         //Connecting
111         //Disconnected
112         //Connected
113         return array('connection' => strtolower($strStatus));
114     }
115
116
117
118     /**
119      * Parses the HTML body of /Status_NoAuth.asp and returns the extracted
120      * values.
121      *
122      * @param string $body HTML document to parse
123      *
124      * @return array Array of key-value pairs with the following keys:
125      *               - type
126      *               - network
127      *               - signal strength
128      *               - connection time
129      *               - session usage (array)
130      *               - card model
131      *               - card revision
132      *               - card firmware
133      *               - IMSI
134      *
135      * @todo FIXME implement "connection" value as index_wstatus2() does it
136      */
137     public function status_noauth($body)
138     {
139         $body = str_replace(
140             array('&nbsp;', '</font>', '<script>', '</script>'),
141             '', $body
142         );
143
144         $doc = new DomDocument();
145         libxml_use_internal_errors(true);//html is broken
146         $doc->loadHtml($body);
147                 $xpath = new DOMXPath($doc);
148                 $trs   = $xpath->query('//table//table/tbody/tr');
149         $arRaw = array();
150         foreach ($trs as $tr) {
151             $titleItems = $xpath->query('td[@width=125]/text()', $tr);
152             $valueItems = $xpath->query('td[@width=296]/*[1]', $tr);
153             if ($titleItems->length && $valueItems->length) {
154                 $title = substr($doc->saveXML($titleItems->item(0)), 8, -2);
155                 $value = $doc->saveXML($valueItems->item(0));
156                 $arRaw[self::$arTranslations[$title]] = $value;
157             }
158         }
159
160         foreach ($arRaw as $key => &$value) {
161             $value = trim(strip_tags($value));
162             if (substr($value, 0, 8) == 'Capture(') {
163                 $value = self::$arTranslations[substr($value, 8, -1)];
164             }
165         }
166         unset($value);
167
168         if (isset($arRaw['connection time'])) {
169             $arRaw['connection time'] = str_replace(
170                 array(
171                     ' Capture(GPRS_MSG.HOURS)',
172                     ' Capture(GPRS_MSG.MINUTE)',
173                     ' Capture(GPRS_MSG.SECOND)',
174                 ),
175                 array(
176                     'h', 'm', 's'
177                 ),
178                 $arRaw['connection time']
179             );
180         }
181
182         if (isset($arRaw['session usage'])) {
183             $arS = explode(
184                 'TX = ',
185                 str_replace('RX = ', '', $arRaw['session usage'])
186             );
187             $arRaw['session usage'] = array(
188                 'received' => $arS[0],
189                 'sent'     => $arS[1]
190             );
191         }
192
193         return $arRaw;
194     }
195 }