dummy file for index-wstatus2
[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         $this->checkResponseStatus($resp);
119         return $resp;
120     }//public function reboot()
121
122
123
124     /**
125      * Retrieves status information about the router
126      *
127      * @return array Array with several key-value pairs
128      *               connection => connecting, disconnected, connected
129      *
130      * @throws Exception When the router can't be reached, or unauthorized
131      */
132     public function getConnectionStatus()
133     {
134         $arRetval = array();
135
136         $strUrlBase = $this->getAuthBaseUrl();
137         $parser = new Wrt3g_HtmlParser();
138
139         /**
140          * Connection status
141          */
142         $r = new $this->requestClass();
143         $r->setMethod(HTTP_Request2::METHOD_GET);
144         $r->setUrl($strUrlBase . '/index_wstatus2.asp');
145         $this->log('Connecting to ' . $strUrlBase . '/index_wstatus2.asp', 1);
146         $resp = $r->send();
147         $this->checkResponseStatus($resp);
148         $arRetval = $parser->index_wstatus2($resp->getBody());
149
150         /**
151         * GPRS/UMTS Status
152         */
153         $r->setUrl($strUrlBase . '/index_wstatus1.asp');
154         $this->log('Connecting to ' . $strUrlBase . '/index_wstatus1.asp', 1);
155         $resp = $r->send();
156         $this->checkResponseStatus($resp);
157         $body = $resp->getBody();
158         $arRetval = array_merge($arRetval, $parser->index_wstatus1($body));
159
160         return $arRetval;
161     }//public function getConnectionStatus()
162
163
164
165     /**
166      * Loads "Status_NoAuth.asp" page and parses the body.
167      *
168      * @return array Associative array with connection status.
169      *
170      * @throws Exception When something goes wrong, i.e. router not
171      *                   reachable
172      */
173     protected function loadStatus_NoAuth()
174     {
175         $strUrlBase = $this->getAnonBaseUrl();
176
177         $url = $strUrlBase . '/Status_NoAuth.asp';
178         $this->log('Connecting to ' . $url, 1);
179
180         $r = new $this->requestClass();
181         $r->setMethod(HTTP_Request2::METHOD_GET);
182         $r->setUrl($url);
183         $resp = $r->send();
184         $this->checkResponseStatus($resp);
185
186         $parser = new Wrt3g_HtmlParser();
187
188         return $parser->status_noauth(
189             $resp->getBody()
190         );
191     }
192
193
194
195     /**
196      * Checks the HTTP response code and throws an exception if it
197      * is not in the 200 range.
198      *
199      * @param HTTP_Request2_Response $resp HTTP response object
200      *
201      * @return void
202      *
203      * @throws Exception When the status is not 200-299
204      */
205     protected function checkResponseStatus(HTTP_Request2_Response $resp)
206     {
207         $nStatus = $resp->getStatus();
208         $this->log($nStatus . ' ' . $resp->getReasonPhrase(), 1);
209         if (intval($nStatus / 100) == 2) {
210             //2xx status is fine
211             return;
212         }
213
214         throw new Exception(
215             'HTTP Error: ' . $nStatus . ' ' . $resp->getReasonPhrase()
216         );
217     }
218
219
220
221     /**
222      * Retrieves pc card/SIM status information
223      *
224      * @return array Array with several key-value pairs
225      *               connection => connecting, disconnected, connected
226      *
227      * @throws Exception When the router can't be reached
228      */
229     public function getCardStatus()
230     {
231         $arRetval = $this->loadStatus_NoAuth();
232
233         return array_intersect_key(
234             $arRetval,
235             array(
236                 'card model'    => 0,
237                 'card revision' => 0,
238                 'card firmware' => 0,
239                 'IMSI'          => 0
240             )
241         );
242     }
243
244
245
246     /**
247      * Retrieves all status information one can get.
248      *
249      * @return array Array with several key-value pairs
250      *               connection => connecting, disconnected, connected
251      *
252      * @throws Exception When the router can't be reached
253      */
254     public function getFullStatus()
255     {
256         return $this->loadStatus_NoAuth();
257     }
258
259
260
261     /**
262      * Log a message to stdout.
263      *
264      * @param string  $msg   Message to display
265      * @param integer $level Log level, see $verbosity
266      *
267      * @return void
268      */
269     public function log($msg, $level = 1)
270     {
271         if ($this->verbosity >= $level) {
272             echo $msg . "\n";
273         }
274     }
275 }
276 ?>