.asp -> .htm
[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     );
25
26
27
28     /**
29     * Parses the body of /index_wstatus1.asp and returns extracted values.
30     *
31     * @param string $body HTML document to parse
32     *
33     * @return array Array of key-value pairs
34     */
35     public function index_wstatus1($body)
36     {
37         $doc = new DomDocument();
38         $doc->loadHtml($body);
39                 $xpath = new DOMXPath($doc);
40                 $entries = $xpath->query('//table/tr/td');
41
42         $arMatches = array();
43                 foreach ($entries as $entry) {
44             $s = $doc->saveXML($entry);
45             $s = str_replace(
46                 array('<![CDATA[', ']]>'),
47                 '', $s
48             );
49             $s = strip_tags($s);
50             //some strange utf8 space char
51             $s = str_replace("\xC2\xA0", ' ', $s);
52             $s = trim($s);
53             $s = str_replace(
54                 array('Capture(', ') :'),
55                 '', $s
56             );
57             if (substr($s, -1) == ')') {
58                 $s = substr($s, 0, -1);
59             }
60             $arMatches[] = $s;
61         }
62
63
64         $arRetval = array();
65         $arData = array();
66         reset($arMatches);
67         while (current($arMatches)) {
68             $key   = current($arMatches);
69             $value = next($arMatches);
70             if (isset(self::$arTranslations[$key])) {
71                 $key = self::$arTranslations[$key];
72             }
73             if (isset(self::$arTranslations[$value])) {
74                 $value = self::$arTranslations[$value];
75             }
76             $arRetval[$key] = $value;
77             next($arMatches);
78         }
79
80         return $arRetval;
81     }
82
83
84
85     /**
86     * Parses the body of /index_wstatus2.asp and returns extracted values.
87     *
88     * @param string $body HTML document to parse
89     *
90     * @return array Array with "connection" as key and one of "connecting",
91     *               "connected" or "disconnected" as value.
92     */
93     public function index_wstatus2($body)
94     {
95         preg_match('/var status2 = "(.+)"/', $body, $arMatches);
96
97         $strStatus = $arMatches[1];
98         //Connecting
99         //Disconnected
100         //Connected
101         return array('connection' => strtolower($strStatus));
102     }
103 }