aboutsummaryrefslogtreecommitdiff
path: root/Wrt3g/HtmlParser.php
blob: bd613acbcc7f6b467631a8647a9e479f1924f425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
 * HTML parsing for the linksys router HTML pages
 *
 * PHP version 5
 *
 * @category Tools
 * @package  linksys-wrt3g-tools
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @license  AGPL v3
 * @link     http://cweiske.de/linksys-wrt3g-tools.htm
 */
class Wrt3g_HtmlParser
{
    protected static $arTranslations = array(
        'GPRS_MSG.WWBEAR'      => 'type',
        'GPRS_MSG.NNAME'       => 'network',
        'GPRS_MSG.STRENGTH'    => 'signal strength',
        'GPRS_MSG.ACQUIRING'   => 'aquiring',
        'GPRS_MSG.WWBEAR_GPRS' => 'GPRS',
        'GPRS_MSG.WWBEAR_UMTS' => 'UMTS',
        'GPRS_MSG.None'        => null,
        'GPRS_MSG.FAIR'        => 'fair',
        'GPRS_MSG.GOOD'        => 'good',
        'GPRS_MSG.EXCELLENT'   => 'excellent',
        'GPRS_MSG.NOSIGNAL'    => 'no signal',
        'GPRS_MSG.CTIME'       => 'connection time',
        'GPRS_MSG.CFW'         => 'card firmware',
        'GPRS_MSG.CMOD'        => 'card model',
        'GPRS_MSG.CREV'        => 'card revision',
        'GPRS_MSG.IMSI'        => 'IMSI',
        'GPRS_MSG.SESSIONUSAGE' => 'session usage',
    );



    /**
     * Parses the body of /index_wstatus1.asp and returns extracted values.
     * If no connection is established, the values are NULL.
     *
     * @param string $body HTML document to parse
     *
     * @return array Array of key-value pairs, probably with
     *               - "type"            - i.e. "GPRS"
     *               - "network"         - Carrier network name, i.e. "BASE DE"
     *               - "signal strength" - good, excellent, poor
     */
    public function index_wstatus1($body)
    {
        $doc = new DomDocument();
        $doc->loadHtml($body);
		$xpath = new DOMXPath($doc);
		$entries = $xpath->query('//table/tr/td');

        $arMatches = array();
		foreach ($entries as $entry) {
            $s = $doc->saveXML($entry);
            $s = str_replace(
                array('<![CDATA[', ']]>'),
                '', $s
            );
            $s = strip_tags($s);
            //some strange utf8 space char
            $s = str_replace("\xC2\xA0", ' ', $s);
            $s = trim($s);
            $s = str_replace(
                array('Capture(', ') :'),
                '', $s
            );
            if (substr($s, -1) == ')') {
                $s = substr($s, 0, -1);
            }
            $arMatches[] = $s;
        }


        $arRetval = array();
        $arData = array();
        reset($arMatches);
        while (current($arMatches)) {
            $key   = current($arMatches);
            $value = next($arMatches);
            if (isset(self::$arTranslations[$key])) {
                $key = self::$arTranslations[$key];
            }
            if (isset(self::$arTranslations[$value])) {
                $value = self::$arTranslations[$value];
            }
            if ($value == 'aquiring') {
                $value = null;
            }
            $arRetval[$key] = $value;
            next($arMatches);
        }

        return $arRetval;
    }



    /**
     * Parses the body of /index_wstatus2.asp and returns extracted values.
     *
     * @param string $body HTML document to parse
     *
     * @return array Array with "connection" as key and one of "connecting",
     *               "connected" or "disconnected" as value.
     */
    public function index_wstatus2($body)
    {
        preg_match('/var status2 = "(.+)"/', $body, $arMatches);
        if (!isset($arMatches[1])) {
            return array();
        }

        $strStatus = $arMatches[1];
        //Connecting
        //Disconnected
        //Connected
        return array('connection' => strtolower($strStatus));
    }



    /**
     * Parses the HTML body of /Status_NoAuth.asp and returns the extracted
     * values.
     *
     * @param string $body HTML document to parse
     *
     * @return array Array of key-value pairs with the following keys:
     *               - type
     *               - network
     *               - signal strength
     *               - connection time
     *               - session usage (array)
     *               - card model
     *               - card revision
     *               - card firmware
     *               - IMSI
     */
    public function status_noauth($body)
    {
        $body = str_replace(
            array('&nbsp;', '</font>', '<script>', '</script>'),
            '', $body
        );

        $doc = new DomDocument();
        libxml_use_internal_errors(true);//html is broken
        $doc->loadHtml($body);
		$xpath = new DOMXPath($doc);
		$trs   = $xpath->query('//table//table/tbody/tr');
        $arRaw = array('connection' => null);
        $bAquiring = false;
        foreach ($trs as $tr) {
            $titleItems = $xpath->query('td[@width=125]/text()', $tr);
            $valueItems = $xpath->query('td[@width=296]/*[1]', $tr);
            if ($titleItems->length && $valueItems->length) {
                $title = substr($doc->saveXML($titleItems->item(0)), 8, -2);
                $value = $doc->saveXML($valueItems->item(0));
                $arRaw[self::$arTranslations[$title]] = $value;
            }
        }

        foreach ($arRaw as $key => &$value) {
            $value = trim(strip_tags($value));
            if (substr($value, 0, 8) == 'Capture(') {
                $value = self::$arTranslations[substr($value, 8, -1)];
                if ($value == 'aquiring') {
                    $bAquiring = true;
                    $value = null;
                }
            }
        }
        unset($value);

        if ($bAquiring) {
            $arRaw['connection'] = 'connecting';
        } else if ($arRaw['type'] != null) {
            $arRaw['connection'] = 'connected';
        } else {
            $arRaw['connection'] = 'disconnected';
        }

        if (isset($arRaw['connection time'])) {
            $arRaw['connection time'] = str_replace(
                array(
                    ' Capture(GPRS_MSG.HOURS)',
                    ' Capture(GPRS_MSG.MINUTE)',
                    ' Capture(GPRS_MSG.SECOND)',
                ),
                array(
                    'h', 'm', 's'
                ),
                $arRaw['connection time']
            );
        }

        if (isset($arRaw['session usage'])) {
            $arS = explode(
                'TX = ',
                str_replace('RX = ', '', $arRaw['session usage'])
            );
            if (count($arS) > 1) {
                $arRaw['session usage'] = array(
                    'received' => $arS[0],
                    'sent'     => $arS[1]
                );
            }
        }

        if (preg_match('/Capture\\(share.firmwarever\\):(.+?)</', $body, $arMatches)) {
            $arRaw['router firmware'] = trim($arMatches[1]);
        }

        return $arRaw;
    }
}