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