make the request observer work correctly, remove response logging in main class
[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 require_once 'Wrt3g/RequestObserver.php';
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      * 3 - show HTTP requests and responses
55      *
56      * @var integer
57      */
58     public $verbosity = 0;
59
60     /**
61      * Class to send HTTP Requests with.
62      * Needs to be compatible with HTTP_Request2
63      *
64      * @var string
65      */
66     public $requestClass = 'HTTP_Request2';
67
68
69
70     /**
71      * Returns the base URL to use for requests that require authentification.
72      * Includes username, password and host.
73      *
74      * @return string URL without trailing slash after the host
75      */
76     protected function getAuthBaseUrl()
77     {
78         return 'http://'
79             . $this->user
80             . ':' . $this->password
81             . '@' . $this->host;
82     }
83
84
85
86     /**
87      * Returns the base URL to use for requests that do
88      * not require authentification.
89      *
90      * @return string URL without trailing slash after the host
91      */
92     protected function getAnonBaseUrl()
93     {
94         return 'http://' . $this->host;
95     }
96
97
98
99     /**
100      * Creates a new HTTP_Request2 object, attaches an observer
101      * if necessary and returns it.
102      *
103      * @return HTTP_Request2
104      */
105     protected function createRequest()
106     {
107         $r = new $this->requestClass();
108         if ($this->verbosity >= 3) {
109             //register observer
110             $r->attach(new Wrt3g_RequestObserver($this));
111         }
112         return $r;
113     }
114
115
116
117     /**
118      * Reboots the router.
119      *
120      * @return HTTP_Request2_Response
121      *
122      * @throws Exception When the router can't be reached, or unauthorized
123      */
124     public function reboot()
125     {
126         $url = $this->getAuthBaseUrl() . '/apply.cgi';
127         $this->log('Connecting to ' . $url);
128
129         $r = $this->createRequest();
130         $r->setMethod(HTTP_Request2::METHOD_POST);
131         $r->setUrl($url);
132         $r->addPostParameter('action', 'Reboot');
133         $r->addPostParameter('submit_button', 'Diagnostics');
134         $r->addPostParameter('wait_time', 1);
135
136         $resp = $r->send();
137         $this->checkResponseStatus($resp);
138         return $resp;
139     }//public function reboot()
140
141
142
143     /**
144      * Retrieves status information about the router
145      *
146      * @return array Array with several key-value pairs
147      *               connection => connecting, disconnected, connected
148      *
149      * @throws Exception When the router can't be reached, or unauthorized
150      */
151     public function getConnectionStatus()
152     {
153         $arRetval = array();
154
155         $strUrlBase = $this->getAuthBaseUrl();
156         $parser = new Wrt3g_HtmlParser();
157
158         /**
159          * Connection status
160          */
161         $r = $this->createRequest();
162         $r->setMethod(HTTP_Request2::METHOD_GET);
163         $r->setUrl($strUrlBase . '/index_wstatus2.asp');
164         $this->log('Connecting to ' . $strUrlBase . '/index_wstatus2.asp', 1);
165         $resp = $r->send();
166         $this->checkResponseStatus($resp);
167         $arRetval = $parser->index_wstatus2($resp->getBody());
168
169         /**
170         * GPRS/UMTS Status
171         */
172         $r->setUrl($strUrlBase . '/index_wstatus1.asp');
173         $this->log('Connecting to ' . $strUrlBase . '/index_wstatus1.asp', 1);
174         $resp = $r->send();
175         $this->checkResponseStatus($resp);
176         $body = $resp->getBody();
177         $arRetval = array_merge($arRetval, $parser->index_wstatus1($body));
178
179         return $arRetval;
180     }//public function getConnectionStatus()
181
182
183
184     /**
185      * Loads "Status_NoAuth.asp" page and parses the body.
186      *
187      * @return array Associative array with connection status.
188      *
189      * @throws Exception When something goes wrong, i.e. router not
190      *                   reachable
191      */
192     protected function loadStatus_NoAuth()
193     {
194         $strUrlBase = $this->getAnonBaseUrl();
195
196         $url = $strUrlBase . '/Status_NoAuth.asp';
197         $this->log('Connecting to ' . $url, 1);
198
199         $r = $this->createRequest();
200         $r->setMethod(HTTP_Request2::METHOD_GET);
201         $r->setUrl($url);
202         $resp = $r->send();
203         $this->checkResponseStatus($resp);
204
205         $parser = new Wrt3g_HtmlParser();
206
207         return $parser->status_noauth(
208             $resp->getBody()
209         );
210     }
211
212
213
214     /**
215      * Checks the HTTP response code and throws an exception if it
216      * is not in the 200 range.
217      *
218      * @param HTTP_Request2_Response $resp HTTP response object
219      *
220      * @return void
221      *
222      * @throws Exception When the status is not 200-299
223      */
224     protected function checkResponseStatus(HTTP_Request2_Response $resp)
225     {
226         $nStatus = $resp->getStatus();
227         $this->log($nStatus . ' ' . $resp->getReasonPhrase(), 1);
228         if (intval($nStatus / 100) == 2) {
229             //2xx status is fine
230             return;
231         }
232
233         throw new Exception(
234             'HTTP Error: ' . $nStatus . ' ' . $resp->getReasonPhrase()
235         );
236     }
237
238
239
240     /**
241      * Retrieves pc card/SIM status information
242      *
243      * @return array Array with several key-value pairs
244      *               connection => connecting, disconnected, connected
245      *
246      * @throws Exception When the router can't be reached
247      */
248     public function getCardStatus()
249     {
250         $arRetval = $this->loadStatus_NoAuth();
251
252         return array_intersect_key(
253             $arRetval,
254             array(
255                 'card model'    => 0,
256                 'card revision' => 0,
257                 'card firmware' => 0,
258                 'IMSI'          => 0
259             )
260         );
261     }
262
263
264
265     /**
266      * Retrieves all status information one can get.
267      *
268      * @return array Array with several key-value pairs
269      *               connection => connecting, disconnected, connected
270      *
271      * @throws Exception When the router can't be reached
272      */
273     public function getFullStatus()
274     {
275         return $this->loadStatus_NoAuth();
276     }
277
278
279
280     /**
281      * Log a message to stdout.
282      *
283      * @param string|object  $msg   Message to display. May even be a response
284      *                              object
285      * @param integer        $level Log level, see $verbosity
286      *
287      * @return void
288      */
289     public function log($msg, $level = 1)
290     {
291         if ($this->verbosity >= $level) {
292             echo $msg . "\n";
293         }
294     }
295 }
296 ?>