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);
119 }//public function reboot()
124 * Retrieves status information about the router
126 * @return array Array with several key-value pairs
127 * connection => connecting, disconnected, connected
129 * @throws Exception When the router can't be reached, or unauthorized
131 public function getConnectionStatus()
135 $strUrlBase = $this->getAuthBaseUrl();
136 $parser = new Wrt3g_HtmlParser();
141 $r = new $this->requestClass();
142 $r->setMethod(HTTP_Request2::METHOD_GET);
143 $r->setUrl($strUrlBase . '/index_wstatus2.asp');
144 $this->log('Connecting to ' . $strUrlBase . '/index_wstatus2.asp', 1);
146 $this->log($resp->getStatus() . ' ' . $resp->getReasonPhrase(), 1);
147 $arRetval = $parser->index_wstatus2($resp->getBody());
152 $r->setUrl($strUrlBase . '/index_wstatus1.asp');
153 $this->log('Connecting to ' . $strUrlBase . '/index_wstatus1.asp', 1);
155 $body = $resp->getBody();
156 $arRetval = array_merge($arRetval, $parser->index_wstatus1($body));
159 }//public function getConnectionStatus()
164 * Loads "Status_NoAuth.asp" page and parses the body.
166 * @return array Associative array with connection status.
168 * @throws Exception When something goes wrong, i.e. router not
171 protected function loadStatus_NoAuth()
173 $strUrlBase = $this->getAnonBaseUrl();
175 $url = $strUrlBase . '/Status_NoAuth.asp';
176 $this->log('Connecting to ' . $url, 1);
178 $r = new $this->requestClass();
179 $r->setMethod(HTTP_Request2::METHOD_GET);
182 $this->log($resp->getStatus() . ' ' . $resp->getReasonPhrase(), 1);
183 //FIXME: check status
185 $parser = new Wrt3g_HtmlParser();
187 return $parser->status_noauth(
195 * Retrieves pc card/SIM status information
197 * @return array Array with several key-value pairs
198 * connection => connecting, disconnected, connected
200 * @throws Exception When the router can't be reached
202 public function getCardStatus()
204 $arRetval = $this->loadStatus_NoAuth();
206 return array_intersect_key(
210 'card revision' => 0,
211 'card firmware' => 0,
220 * Retrieves all status information one can get.
222 * @return array Array with several key-value pairs
223 * connection => connecting, disconnected, connected
225 * @throws Exception When the router can't be reached
227 public function getFullStatus()
229 return $this->loadStatus_NoAuth();
235 * Log a message to stdout.
237 * @param string $msg Message to display
238 * @param integer $level Log level, see $verbosity
242 public function log($msg, $level = 1)
244 if ($this->verbosity >= $level) {