fix E_NOTICE when parsing empty html
[linksys-wrt3g-tools.git] / Wrt3g.php
1 <?php
2 /**
3  * Part of Linksys WRT3G tools
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 require_once 'HTTP/Request2.php';
14 require_once 'Wrt3g/HtmlParser.php';
15
16
17 /**
18  * Main class to interact with the router.
19  *
20  * @category Tools
21  * @package  linksys-wrt3g-tools
22  * @author   Christian Weiske <cweiske@cweiske.de>
23  * @license  AGPL v3
24  * @link     http://cweiske.de/linksys-wrt3g-tools.htm
25  */
26 class Wrt3g
27 {
28     /**
29      * Router hostname/IP
30      *
31      * @var string
32      */
33     public $host;
34
35     /**
36      * Name of user with administration privileges
37      *
38      * @var string
39      */
40     public $user;
41
42     /**
43      * Password for $user
44      *
45      * @var string
46      */
47     public $password;
48
49     /**
50      * Logging verbosity
51      * 0 - no debug logging
52      * 1 - show important details (connected URLs)
53      * 2 - show internal details
54      *
55      * @var integer
56      */
57     public $verbosity = 0;
58
59     /**
60      * Class to send HTTP Requests with.
61      * Needs to be compatible with HTTP_Request2
62      *
63      * @var string
64      */
65     public $requestClass = 'HTTP_Request2';
66
67
68
69     /**
70      * Returns the base URL to use for requests that require authentification.
71      * Includes username, password and host.
72      *
73      * @return string URL without trailing slash after the host
74      */
75     protected function getAuthBaseUrl()
76     {
77         return 'http://'
78             . $this->user
79             . ':' . $this->password
80             . '@' . $this->host;
81     }
82
83
84
85     /**
86      * Returns the base URL to use for requests that do
87      * not require authentification.
88      *
89      * @return string URL without trailing slash after the host
90      */
91     protected function getAnonBaseUrl()
92     {
93         return 'http://' . $this->host;
94     }
95
96
97
98     /**
99      * Reboots the router.
100      *
101      * @return HTTP_Request2_Response
102      *
103      * @throws Exception When the router can't be reached, or unauthorized
104      */
105     public function reboot()
106     {
107         $url = $this->getAuthBaseUrl() . '/apply.cgi';
108         $this->log('Connecting to ' . $url);
109
110         $r = new $this->requestClass();
111         $r->setMethod(HTTP_Request2::METHOD_POST);
112         $r->setUrl($url);
113         $r->addPostParameter('action', 'Reboot');
114         $r->addPostParameter('submit_button', 'Diagnostics');
115         $r->addPostParameter('wait_time', 1);
116
117         $resp = $r->send();
118         return $resp;
119     }//public function reboot()
120
121
122
123     /**
124      * Retrieves status information about the router
125      *
126      * @return array Array with several key-value pairs
127      *               connection => connecting, disconnected, connected
128      *
129      * @throws Exception When the router can't be reached, or unauthorized
130      */
131     public function getConnectionStatus()
132     {
133         $arRetval = array();
134
135         $strUrlBase = $this->getAuthBaseUrl();
136         $parser = new Wrt3g_HtmlParser();
137
138         /**
139          * Connection status
140          */
141         $r = new $this->requestClass();
142         $r->setMethod(HTTP_Request2::METHOD_GET);
143         $r->setUrl($strUrlBase . '/index_wstatus2.asp');
144         $this->log('Connecting to ' . $strUrlBase . '/index_wstatus2.asp', 1);
145         $resp = $r->send();
146         $this->log($resp->getStatus() . ' ' . $resp->getReasonPhrase(), 1);
147         $arRetval = $parser->index_wstatus2($resp->getBody());
148
149         /**
150         * GPRS/UMTS Status
151         */
152         $r->setUrl($strUrlBase . '/index_wstatus1.asp');
153         $this->log('Connecting to ' . $strUrlBase . '/index_wstatus1.asp', 1);
154         $resp = $r->send();
155         $body = $resp->getBody();
156         $arRetval = array_merge($arRetval, $parser->index_wstatus1($body));
157
158         return $arRetval;
159     }//public function getConnectionStatus()
160
161
162
163     /**
164      * Loads "Status_NoAuth.asp" page and parses the body.
165      *
166      * @return array Associative array with connection status.
167      *
168      * @throws Exception When something goes wrong, i.e. router not
169      *                   reachable
170      */
171     protected function loadStatus_NoAuth()
172     {
173         $strUrlBase = $this->getAnonBaseUrl();
174
175         $url = $strUrlBase . '/Status_NoAuth.asp';
176         $this->log('Connecting to ' . $url, 1);
177
178         $r = new $this->requestClass();
179         $r->setMethod(HTTP_Request2::METHOD_GET);
180         $r->setUrl($url);
181         $resp = $r->send();
182         $this->log($resp->getStatus() . ' ' . $resp->getReasonPhrase(), 1);
183         //FIXME: check status
184
185         $parser = new Wrt3g_HtmlParser();
186
187         return $parser->status_noauth(
188             $resp->getBody()
189         );
190     }
191
192
193
194     /**
195      * Retrieves pc card/SIM status information
196      *
197      * @return array Array with several key-value pairs
198      *               connection => connecting, disconnected, connected
199      *
200      * @throws Exception When the router can't be reached
201      */
202     public function getCardStatus()
203     {
204         $arRetval = $this->loadStatus_NoAuth();
205
206         return array_intersect_key(
207             $arRetval,
208             array(
209                 'card model'    => 0,
210                 'card revision' => 0,
211                 'card firmware' => 0,
212                 'IMSI'          => 0
213             )
214         );
215     }
216
217
218
219     /**
220      * Retrieves all status information one can get.
221      *
222      * @return array Array with several key-value pairs
223      *               connection => connecting, disconnected, connected
224      *
225      * @throws Exception When the router can't be reached
226      */
227     public function getFullStatus()
228     {
229         return $this->loadStatus_NoAuth();
230     }
231
232
233
234     /**
235      * Log a message to stdout.
236      *
237      * @param string  $msg   Message to display
238      * @param integer $level Log level, see $verbosity
239      *
240      * @return void
241      */
242     public function log($msg, $level = 1)
243     {
244         if ($this->verbosity >= $level) {
245             echo $msg . "\n";
246         }
247     }
248 }
249 ?>