d887671fb3e931b6a9a933391b5fd18a6af93364
[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, probably with "type", "network" and
34      *               "signal strength"
35      */
36     public function index_wstatus1($body)
37     {
38         $doc = new DomDocument();
39         $doc->loadHtml($body);
40                 $xpath = new DOMXPath($doc);
41                 $entries = $xpath->query('//table/tr/td');
42
43         $arMatches = array();
44                 foreach ($entries as $entry) {
45             $s = $doc->saveXML($entry);
46             $s = str_replace(
47                 array('<![CDATA[', ']]>'),
48                 '', $s
49             );
50             $s = strip_tags($s);
51             //some strange utf8 space char
52             $s = str_replace("\xC2\xA0", ' ', $s);
53             $s = trim($s);
54             $s = str_replace(
55                 array('Capture(', ') :'),
56                 '', $s
57             );
58             if (substr($s, -1) == ')') {
59                 $s = substr($s, 0, -1);
60             }
61             $arMatches[] = $s;
62         }
63
64
65         $arRetval = array();
66         $arData = array();
67         reset($arMatches);
68         while (current($arMatches)) {
69             $key   = current($arMatches);
70             $value = next($arMatches);
71             if (isset(self::$arTranslations[$key])) {
72                 $key = self::$arTranslations[$key];
73             }
74             if (isset(self::$arTranslations[$value])) {
75                 $value = self::$arTranslations[$value];
76             }
77             $arRetval[$key] = $value;
78             next($arMatches);
79         }
80
81         return $arRetval;
82     }
83
84
85
86     /**
87      * Parses the body of /index_wstatus2.asp and returns extracted values.
88      *
89      * @param string $body HTML document to parse
90      *
91      * @return array Array with "connection" as key and one of "connecting",
92      *               "connected" or "disconnected" as value.
93      */
94     public function index_wstatus2($body)
95     {
96         preg_match('/var status2 = "(.+)"/', $body, $arMatches);
97
98         $strStatus = $arMatches[1];
99         //Connecting
100         //Disconnected
101         //Connected
102         return array('connection' => strtolower($strStatus));
103     }
104
105
106
107     /**
108      * Parses the HTML body of /Status_NoAuth.asp and returns the extracted
109      * values.
110      *
111      * @param string $body HTML document to parse
112      *
113      * @return array FIXME
114      */
115     public function status_noauth($body)
116     {
117         $body = str_replace(
118             array('&nbsp;', '</font>', '<script>', '</script>'),
119             '', $body
120         );
121         //var_dump($body);die();
122         $doc = new DomDocument();
123         libxml_use_internal_errors(true);//html is broken
124         $doc->loadHtml($body);
125                 $xpath = new DOMXPath($doc);
126                 $trs = $xpath->query('//table//table/tbody/tr');
127         foreach ($trs as $tr) {
128             /*
129             var_dump(
130                 '--------',$doc->saveXML($tr),
131                 $xpath->query('td', $tr)->length);
132             */
133             $titleItems = $xpath->query('td[@width=125]/text()', $tr);
134             $valueItems = $xpath->query('td[@width=296]/*[1]', $tr);
135             if ($titleItems->length && $valueItems->length) {
136                 $title = substr($doc->saveXML($titleItems->item(0)), 8, -2);
137                 $value = $doc->saveXML($valueItems->item(0));
138             
139                 var_dump(
140                     $title, $value
141                 );
142             }
143         }
144
145         $arMatches = array();
146     }
147 }