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