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)
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 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, or unauthorized
155 public function getConnectionStatus()
159 $strUrlBase = $this->getAuthBaseUrl();
160 $parser = new Wrt3g_HtmlParser();
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);
170 $this->checkResponseStatus($resp);
171 $arRetval = $parser->index_wstatus2($resp->getBody());
176 $r->setUrl($strUrlBase . '/index_wstatus1.asp');
177 $this->log('Connecting to ' . $strUrlBase . '/index_wstatus1.asp', 1);
179 $this->checkResponseStatus($resp);
180 $body = $resp->getBody();
181 $arRetval = array_merge($arRetval, $parser->index_wstatus1($body));
184 }//public function getConnectionStatus()
189 * Loads "Status_NoAuth.asp" page and parses the body.
191 * @return array Associative array with connection status.
193 * @throws Exception When something goes wrong, i.e. router not
196 protected function loadStatus_NoAuth()
198 $strUrlBase = $this->getAnonBaseUrl();
200 $url = $strUrlBase . '/Status_NoAuth.asp';
201 $this->log('Connecting to ' . $url, 1);
203 $r = $this->createRequest();
204 $r->setMethod(HTTP_Request2::METHOD_GET);
207 $this->checkResponseStatus($resp);
209 $parser = new Wrt3g_HtmlParser();
211 return $parser->status_noauth(
219 * Checks the HTTP response code and throws an exception if it
220 * is not in the 200 range.
222 * @param HTTP_Request2_Response $resp HTTP response object
226 * @throws Exception When the status is not 200-299
228 protected function checkResponseStatus(HTTP_Request2_Response $resp)
230 $nStatus = $resp->getStatus();
231 $this->log($nStatus . ' ' . $resp->getReasonPhrase(), 1);
232 if (intval($nStatus / 100) == 2) {
238 'HTTP Error: ' . $nStatus . ' ' . $resp->getReasonPhrase()
245 * Retrieves pc card/SIM status information
247 * @return array Array with several key-value pairs
248 * connection => connecting, disconnected, connected
250 * @throws Exception When the router can't be reached
252 public function getCardStatus()
254 $arRetval = $this->loadStatus_NoAuth();
256 return array_intersect_key(
260 'card revision' => 0,
261 'card firmware' => 0,
270 * Retrieves all status information one can get.
272 * @return array Array with several key-value pairs
273 * connection => connecting, disconnected, connected
275 * @throws Exception When the router can't be reached
277 public function getFullStatus()
279 return $this->loadStatus_NoAuth();
285 * Log a message to stdout.
287 * @param string|object $msg Message to display. May even be a response
289 * @param integer $level Log level, see $verbosity
293 public function log($msg, $level = 1)
295 if ($this->verbosity >= $level) {