fix error
[linksys-wrt3g-tools.git] / Wrt3g.php
1 <?php
2 /**
3  * Part of Linksys WRT3G tools
4  *
5  * PHP version 5
6  *
7  * @category Tools
8  * @package  linksys-wrt3g-tools
9  * @author   Christian Weiske <cweiske@cweiske.de>
10  * @license  AGPL v3
11  * @link     http://cweiske.de/linksys-wrt3g-tools.htm
12  */
13 require_once 'HTTP/Request2.php';
14 require_once 'Wrt3g/HtmlParser.php';
15
16
17 /**
18  * Main class to interact with the router.
19  *
20  * @category Tools
21  * @package  linksys-wrt3g-tools
22  * @author   Christian Weiske <cweiske@cweiske.de>
23  * @license  AGPL v3
24  * @link     http://cweiske.de/linksys-wrt3g-tools.htm
25  */
26 class Wrt3g
27 {
28     /**
29      * Router hostname/IP
30      *
31      * @var string
32      */
33     public $host;
34
35     /**
36      * Name of user with administration privileges
37      *
38      * @var string
39      */
40     public $user;
41
42     /**
43      * Password for $user
44      *
45      * @var string
46      */
47     public $password;
48
49     /**
50      * Logging verbosity
51      * 0 - no debug logging
52      * 1 - show important details (connected URLs)
53      * 2 - show internal details
54      *
55      * @var integer
56      */
57     public $verbosity = 0;
58
59
60
61     /**
62      * Returns the base URL to use for requests that require authentification.
63      * Includes username, password and host.
64      *
65      * @return string URL without trailing slash after the host
66      */
67     protected function getAuthBaseUrl()
68     {
69         return 'http://'
70             . $this->user
71             . ':' . $this->password
72             . '@' . $this->host;
73     }
74
75
76
77     /**
78      * Returns the base URL to use for requests that do
79      * not require authentification.
80      *
81      * @return string URL without trailing slash after the host
82      */
83     protected function getAnonBaseUrl()
84     {
85         return 'http://' . $this->host;
86     }
87
88
89
90     /**
91      * Reboots the router.
92      *
93      * @return HTTP_Request2_Response
94      *
95      * @throws Exception When the router can't be reached, or unauthorized
96      */
97     public function reboot()
98     {
99         $url = $this->getAuthBaseUrl() . '/apply.cgi';
100         $this->log('Connecting to ' . $url);
101
102         $r = new HTTP_Request2();
103         $r->setMethod(HTTP_Request2::METHOD_POST);
104         $r->setUrl($url);
105         $r->addPostParameter('action', 'Reboot');
106         $r->addPostParameter('submit_button', 'Diagnostics');
107         $r->addPostParameter('wait_time', 1);
108
109         $resp = $r->send();
110         return $resp;
111     }//public function reboot()
112
113
114
115     /**
116      * Retrieves status information about the router
117      *
118      * @return array Array with several key-value pairs
119      *               connection => connecting, disconnected, connected
120      *
121      * @throws Exception When the router can't be reached, or unauthorized
122      */
123     public function getConnectionStatus()
124     {
125         $arRetval = array();
126
127         $strUrlBase = $this->getAuthBaseUrl();
128         $parser = new Wrt3g_HtmlParser();
129
130         /**
131          * Connection status
132          */
133         $r = new HTTP_Request2();
134         $r->setMethod(HTTP_Request2::METHOD_GET);
135         $r->setUrl($strUrlBase . '/index_wstatus2.asp');
136         $this->log('Connecting to ' . $strUrlBase . '/index_wstatus2.asp', 1);
137         $resp = $r->send();
138         $this->log($resp->getStatus() . ' ' . $resp->getReasonPhrase(), 1);
139         $arRetval = $parser->index_wstatus2($resp->getBody());
140
141         /**
142         * GPRS/UMTS Status
143         */
144         $r->setUrl($strUrlBase . '/index_wstatus1.asp');
145         $this->log('Connecting to ' . $strUrlBase . '/index_wstatus1.asp', 1);
146         $resp = $r->send();
147         $body = $resp->getBody();
148         $arRetval = array_merge($arRetval, $parser->index_wstatus1($body));
149
150         return $arRetval;
151     }//public function getConnectionStatus()
152
153
154
155     /**
156      * Loads "Status_NoAuth.asp" page and parses the body.
157      *
158      * @return array Associative array with connection status.
159      *
160      * @throws Exception When something goes wrong, i.e. router not
161      *                   reachable
162      */
163     protected function loadStatus_NoAuth()
164     {
165         $strUrlBase = $this->getAnonBaseUrl();
166
167         $url = $strUrlBase . '/Status_NoAuth.asp';
168         $this->log('Connecting to ' . $url, 1);
169
170         $r = new HTTP_Request2();
171         $r->setMethod(HTTP_Request2::METHOD_GET);
172         $r->setUrl($url);
173         $resp = $r->send();
174         $this->log($resp->getStatus() . ' ' . $resp->getReasonPhrase(), 1);
175         //FIXME: check status
176
177         $parser = new Wrt3g_HtmlParser();
178
179         return $parser->status_noauth(
180             $resp->getBody()
181         );
182     }
183
184
185
186     /**
187      * Retrieves pc card/SIM status information
188      *
189      * @return array Array with several key-value pairs
190      *               connection => connecting, disconnected, connected
191      *
192      * @throws Exception When the router can't be reached
193      */
194     public function getCardStatus()
195     {
196         $arRetval = $this->loadStatus_NoAuth();
197
198         return array_intersect_key(
199             $arRetval,
200             array(
201                 'card model'    => 0,
202                 'card revision' => 0,
203                 'card firmware' => 0,
204                 'IMSI'          => 0
205             )
206         );
207     }
208
209
210
211     /**
212      * Retrieves all status information one can get.
213      *
214      * @return array Array with several key-value pairs
215      *               connection => connecting, disconnected, connected
216      *
217      * @throws Exception When the router can't be reached
218      */
219     public function getFullStatus()
220     {
221         return $this->loadStatus_NoAuth();
222     }
223
224
225
226     /**
227      * Log a message to stdout.
228      *
229      * @param string  $msg   Message to display
230      * @param integer $level Log level, see $verbosity
231      *
232      * @return void
233      */
234     public function log($msg, $level = 1)
235     {
236         if ($this->verbosity >= $level) {
237             echo $msg . "\n";
238         }
239     }
240 }
241 ?>