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/HtmlParser.php';
15 require_once 'Wrt3g/RequestObserver.php';
18 * Main class to interact with the router.
21 * @package linksys-wrt3g-tools
22 * @author Christian Weiske <cweiske@cweiske.de>
24 * @link http://cweiske.de/linksys-wrt3g-tools.htm
36 * Name of user with administration privileges
51 * 0 - no debug logging
52 * 1 - show important details (connected URLs)
53 * 2 - show internal details
54 * 3 - show HTTP requests and responses
58 public $verbosity = 0;
61 * Class to send HTTP Requests with.
62 * Needs to be compatible with HTTP_Request2
66 public $requestClass = 'HTTP_Request2';
71 * Returns the base URL to use for requests that require authentification.
72 * Includes username, password and host.
74 * @return string URL without trailing slash after the host
76 protected function getAuthBaseUrl()
80 . ':' . $this->password
87 * Returns the base URL to use for requests that do
88 * not require authentification.
90 * @return string URL without trailing slash after the host
92 protected function getAnonBaseUrl()
94 return 'http://' . $this->host;
100 * Creates a new HTTP_Request2 object, attaches an observer
101 * if necessary and returns it.
103 * @return HTTP_Request2
105 protected function createRequest()
107 $r = new $this->requestClass();
108 if ($this->verbosity >= 3) {
110 $r->attach(new Wrt3g_RequestObserver($this));
118 * Reboots the router.
120 * @return HTTP_Request2_Response
122 * @throws Exception When the router can't be reached, or unauthorized
124 public function reboot()
126 $url = $this->getAuthBaseUrl() . '/apply.cgi';
127 $this->log('Connecting to ' . $url);
129 $r = $this->createRequest();
130 $r->setMethod(HTTP_Request2::METHOD_POST);
132 $r->addPostParameter('action', 'Reboot');
133 $r->addPostParameter('submit_button', 'Diagnostics');
134 $r->addPostParameter('wait_time', 1);
137 $this->checkResponseStatus($resp);
139 }//public function reboot()
144 * Retrieves status information about the router
146 * @return array Array with several key-value pairs
147 * connection => connecting, disconnected, connected
149 * @throws Exception When the router can't be reached, or unauthorized
151 public function getConnectionStatus()
155 $strUrlBase = $this->getAuthBaseUrl();
156 $parser = new Wrt3g_HtmlParser();
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);
166 $this->checkResponseStatus($resp);
167 $arRetval = $parser->index_wstatus2($resp->getBody());
172 $r->setUrl($strUrlBase . '/index_wstatus1.asp');
173 $this->log('Connecting to ' . $strUrlBase . '/index_wstatus1.asp', 1);
175 $this->checkResponseStatus($resp);
176 $body = $resp->getBody();
177 $arRetval = array_merge($arRetval, $parser->index_wstatus1($body));
180 }//public function getConnectionStatus()
185 * Loads "Status_NoAuth.asp" page and parses the body.
187 * @return array Associative array with connection status.
189 * @throws Exception When something goes wrong, i.e. router not
192 protected function loadStatus_NoAuth()
194 $strUrlBase = $this->getAnonBaseUrl();
196 $url = $strUrlBase . '/Status_NoAuth.asp';
197 $this->log('Connecting to ' . $url, 1);
199 $r = $this->createRequest();
200 $r->setMethod(HTTP_Request2::METHOD_GET);
203 $this->checkResponseStatus($resp);
205 $parser = new Wrt3g_HtmlParser();
207 return $parser->status_noauth(
215 * Checks the HTTP response code and throws an exception if it
216 * is not in the 200 range.
218 * @param HTTP_Request2_Response $resp HTTP response object
222 * @throws Exception When the status is not 200-299
224 protected function checkResponseStatus(HTTP_Request2_Response $resp)
226 $nStatus = $resp->getStatus();
227 $this->log($nStatus . ' ' . $resp->getReasonPhrase(), 1);
228 if (intval($nStatus / 100) == 2) {
234 'HTTP Error: ' . $nStatus . ' ' . $resp->getReasonPhrase()
241 * Retrieves pc card/SIM status information
243 * @return array Array with several key-value pairs
244 * connection => connecting, disconnected, connected
246 * @throws Exception When the router can't be reached
248 public function getCardStatus()
250 $arRetval = $this->loadStatus_NoAuth();
252 return array_intersect_key(
256 'card revision' => 0,
257 'card firmware' => 0,
266 * Retrieves all status information one can get.
268 * @return array Array with several key-value pairs
269 * connection => connecting, disconnected, connected
271 * @throws Exception When the router can't be reached
273 public function getFullStatus()
275 return $this->loadStatus_NoAuth();
281 * Log a message to stdout.
283 * @param string|object $msg Message to display. May even be a response
285 * @param integer $level Log level, see $verbosity
289 public function log($msg, $level = 1)
291 if ($this->verbosity >= $level) {