3 * Part of Linksys WRT3G tools
8 * @package linksys-wrt3g-tools
9 * @author Christian Weiske <cweiske@cweiske.de>
11 * @link http://cweiske.de/linksys-wrt3g-tools.htm
13 require_once 'HTTP/Request2.php';
14 require_once 'Wrt3g/Config.php';
15 require_once 'Wrt3g/HtmlParser.php';
16 require_once 'Wrt3g/RequestObserver.php';
19 * Main class to interact with the router.
22 * @package linksys-wrt3g-tools
23 * @author Christian Weiske <cweiske@cweiske.de>
25 * @link http://cweiske.de/linksys-wrt3g-tools.htm
31 * 0 - no debug logging
32 * 1 - show important details (connected URLs)
33 * 2 - show internal details
34 * 3 - show HTTP requests and responses
38 public $verbosity = 0;
41 * Class to send HTTP Requests with.
42 * Needs to be compatible with HTTP_Request2
46 public $requestClass = 'HTTP_Request2';
49 * Configuration object
58 * Loads the configuration from file and cmdline options
60 * @param array $options Command line options array
66 public function loadConfig($options = array())
68 $this->config = new Wrt3g_Config($this);
69 $this->config->load($options);
75 * Returns the base URL to use for requests that require authentification.
76 * Includes username, password and host.
78 * @return string URL without trailing slash after the host
80 protected function getAuthBaseUrl()
84 . ':' . $this->config->password
85 . '@' . $this->config->host;
91 * Returns the base URL to use for requests that do
92 * not require authentification.
94 * @return string URL without trailing slash after the host
96 protected function getAnonBaseUrl()
98 return 'http://' . $this->config->host;
104 * Creates a new HTTP_Request2 object, attaches an observer
105 * if necessary and returns it.
107 * @return HTTP_Request2
109 protected function createRequest()
111 $r = new $this->requestClass();
112 if ($this->verbosity >= 3) {
114 $r->attach(new Wrt3g_RequestObserver($this));
122 * Reboots the router.
124 * @return HTTP_Request2_Response
126 * @throws Exception When the router can't be reached, or unauthorized
128 public function reboot()
130 $url = $this->getAuthBaseUrl() . '/apply.cgi';
131 $this->log('Connecting to ' . $url);
133 $r = $this->createRequest();
134 $r->setMethod(HTTP_Request2::METHOD_POST);
136 $r->addPostParameter('action', 'Reboot');
137 $r->addPostParameter('submit_button', 'Diagnostics');
138 $r->addPostParameter('wait_time', 1);
141 $this->checkResponseStatus($resp);
143 }//public function reboot()
148 * Retrieves basic connection status information about the router
150 * @return array Array with several key-value pairs
151 * connection => connecting, disconnected, connected
153 * @throws Exception When the router can't be reached
155 public function getConnectionStatus()
157 return array_intersect_key(
158 $this->loadStatus_NoAuth(),
163 'signal strength' => 0,
164 'connection time' => 0,
173 * Retrieves connection status information about the router.
174 * Uses pages that can only be reached with authentication.
176 * @return array Array with several key-value pairs
177 * connection => connecting, disconnected, connected
179 * @throws Exception When the router can't be reached, or unauthorized
181 public function getConnectionStatusAuth()
185 $strUrlBase = $this->getAuthBaseUrl();
186 $parser = new Wrt3g_HtmlParser();
191 $r = $this->createRequest();
192 $r->setMethod(HTTP_Request2::METHOD_GET);
193 $r->setUrl($strUrlBase . '/index_wstatus2.asp');
194 $this->log('Connecting to ' . $strUrlBase . '/index_wstatus2.asp', 1);
196 $this->checkResponseStatus($resp);
197 $arRetval = $parser->index_wstatus2($resp->getBody());
202 $r->setUrl($strUrlBase . '/index_wstatus1.asp');
203 $this->log('Connecting to ' . $strUrlBase . '/index_wstatus1.asp', 1);
205 $this->checkResponseStatus($resp);
206 $body = $resp->getBody();
207 $arRetval = array_merge($arRetval, $parser->index_wstatus1($body));
210 }//public function getConnectionStatus()
215 * Loads "Status_NoAuth.asp" page and parses the body.
217 * @return array Associative array with connection status.
219 * @throws Exception When something goes wrong, i.e. router not
222 protected function loadStatus_NoAuth()
224 $strUrlBase = $this->getAnonBaseUrl();
226 $url = $strUrlBase . '/Status_NoAuth.asp';
227 $this->log('Connecting to ' . $url, 1);
229 $r = $this->createRequest();
230 $r->setMethod(HTTP_Request2::METHOD_GET);
233 $this->checkResponseStatus($resp);
235 $parser = new Wrt3g_HtmlParser();
237 return $parser->status_noauth(
245 * Checks the HTTP response code and throws an exception if it
246 * is not in the 200 range.
248 * @param HTTP_Request2_Response $resp HTTP response object
252 * @throws Exception When the status is not 200-299
254 protected function checkResponseStatus(HTTP_Request2_Response $resp)
256 $nStatus = $resp->getStatus();
257 $this->log($nStatus . ' ' . $resp->getReasonPhrase(), 1);
258 if (intval($nStatus / 100) == 2) {
264 'HTTP Error: ' . $nStatus . ' ' . $resp->getReasonPhrase()
271 * Retrieves pc card/SIM status information
273 * @return array Array with several key-value pairs
274 * connection => connecting, disconnected, connected
276 * @throws Exception When the router can't be reached
278 public function getCardStatus()
280 $arRetval = $this->loadStatus_NoAuth();
282 return array_intersect_key(
286 'card revision' => 0,
287 'card firmware' => 0,
296 * Retrieves all status information one can get.
298 * @return array Array with several key-value pairs
299 * connection => connecting, disconnected, connected
301 * @throws Exception When the router can't be reached
303 public function getFullStatus()
305 return $this->loadStatus_NoAuth();
311 * Log a message to stdout.
313 * @param string|object $msg Message to display. May even be a response
315 * @param integer $level Log level, see $verbosity
319 public function log($msg, $level = 1)
321 if ($this->verbosity >= $level) {