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';
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
57 public $verbosity = 0;
60 * Class to send HTTP Requests with.
61 * Needs to be compatible with HTTP_Request2
65 public $requestClass = 'HTTP_Request2';
70 * Returns the base URL to use for requests that require authentification.
71 * Includes username, password and host.
73 * @return string URL without trailing slash after the host
75 protected function getAuthBaseUrl()
79 . ':' . $this->password
86 * Returns the base URL to use for requests that do
87 * not require authentification.
89 * @return string URL without trailing slash after the host
91 protected function getAnonBaseUrl()
93 return 'http://' . $this->host;
101 * @return HTTP_Request2_Response
103 * @throws Exception When the router can't be reached, or unauthorized
105 public function reboot()
107 $url = $this->getAuthBaseUrl() . '/apply.cgi';
108 $this->log('Connecting to ' . $url);
110 $r = new $this->requestClass();
111 $r->setMethod(HTTP_Request2::METHOD_POST);
113 $r->addPostParameter('action', 'Reboot');
114 $r->addPostParameter('submit_button', 'Diagnostics');
115 $r->addPostParameter('wait_time', 1);
118 $this->checkResponseStatus($resp);
120 }//public function reboot()
125 * Retrieves status information about the router
127 * @return array Array with several key-value pairs
128 * connection => connecting, disconnected, connected
130 * @throws Exception When the router can't be reached, or unauthorized
132 public function getConnectionStatus()
136 $strUrlBase = $this->getAuthBaseUrl();
137 $parser = new Wrt3g_HtmlParser();
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);
147 $this->checkResponseStatus($resp);
148 $arRetval = $parser->index_wstatus2($resp->getBody());
153 $r->setUrl($strUrlBase . '/index_wstatus1.asp');
154 $this->log('Connecting to ' . $strUrlBase . '/index_wstatus1.asp', 1);
156 $this->checkResponseStatus($resp);
157 $body = $resp->getBody();
158 $arRetval = array_merge($arRetval, $parser->index_wstatus1($body));
161 }//public function getConnectionStatus()
166 * Loads "Status_NoAuth.asp" page and parses the body.
168 * @return array Associative array with connection status.
170 * @throws Exception When something goes wrong, i.e. router not
173 protected function loadStatus_NoAuth()
175 $strUrlBase = $this->getAnonBaseUrl();
177 $url = $strUrlBase . '/Status_NoAuth.asp';
178 $this->log('Connecting to ' . $url, 1);
180 $r = new $this->requestClass();
181 $r->setMethod(HTTP_Request2::METHOD_GET);
184 $this->checkResponseStatus($resp);
186 $parser = new Wrt3g_HtmlParser();
188 return $parser->status_noauth(
196 * Checks the HTTP response code and throws an exception if it
197 * is not in the 200 range.
199 * @param HTTP_Request2_Response $resp HTTP response object
203 * @throws Exception When the status is not 200-299
205 protected function checkResponseStatus(HTTP_Request2_Response $resp)
207 $nStatus = $resp->getStatus();
208 $this->log($nStatus . ' ' . $resp->getReasonPhrase(), 1);
209 if (intval($nStatus / 100) == 2) {
215 'HTTP Error: ' . $nStatus . ' ' . $resp->getReasonPhrase()
222 * Retrieves pc card/SIM status information
224 * @return array Array with several key-value pairs
225 * connection => connecting, disconnected, connected
227 * @throws Exception When the router can't be reached
229 public function getCardStatus()
231 $arRetval = $this->loadStatus_NoAuth();
233 return array_intersect_key(
237 'card revision' => 0,
238 'card firmware' => 0,
247 * Retrieves all status information one can get.
249 * @return array Array with several key-value pairs
250 * connection => connecting, disconnected, connected
252 * @throws Exception When the router can't be reached
254 public function getFullStatus()
256 return $this->loadStatus_NoAuth();
262 * Log a message to stdout.
264 * @param string $msg Message to display
265 * @param integer $level Log level, see $verbosity
269 public function log($msg, $level = 1)
271 if ($this->verbosity >= $level) {