add "allstatus" command
[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      * Retrieves pc card/SIM status information
157      *
158      * @return array Array with several key-value pairs
159      *               connection => connecting, disconnected, connected
160      *
161      * @throws Exception When the router can't be reached
162      */
163     public function getCardStatus()
164     {
165         $arRetval = array();
166
167         $strUrlBase = $this->getAnonBaseUrl();
168
169         $url = $strUrlBase . '/Status_NoAuth.asp';
170         $this->log('Connecting to ' . $url, 1);
171
172         $r = new HTTP_Request2();
173         $r->setMethod(HTTP_Request2::METHOD_GET);
174         $r->setUrl($url);
175         $resp = $r->send();
176         $this->log($resp->getStatus() . ' ' . $resp->getReasonPhrase(), 1);
177         //FIXME: check status
178
179         $parser = new Wrt3g_HtmlParser();
180         $arRetval = $parser->status_noauth($resp->getBody());
181
182         return array_intersect_key(
183             $arRetval,
184             array(
185                 'card model'    => 0,
186                 'card revision' => 0,
187                 'card firmware' => 0,
188                 'IMSI'          => 0
189             )
190         );
191     }
192
193
194
195     /**
196      * Retrieves all status information one can get.
197      *
198      * @return array Array with several key-value pairs
199      *               connection => connecting, disconnected, connected
200      *
201      * @throws Exception When the router can't be reached
202      */
203     public function getFullStatus()
204     {
205         $arRetval = array();
206
207         $strUrlBase = $this->getAnonBaseUrl();
208
209         $url = $strUrlBase . '/Status_NoAuth.asp';
210         $this->log('Connecting to ' . $url, 1);
211
212         $r = new HTTP_Request2();
213         $r->setMethod(HTTP_Request2::METHOD_GET);
214         $r->setUrl($url);
215         $resp = $r->send();
216         $this->log($resp->getStatus() . ' ' . $resp->getReasonPhrase(), 1);
217         //FIXME: check status
218
219         $parser = new Wrt3g_HtmlParser();
220         return $parser->status_noauth($resp->getBody());
221     }
222
223
224
225     /**
226      * Log a message to stdout.
227      *
228      * @param string  $msg   Message to display
229      * @param integer $level Log level, see $verbosity
230      *
231      * @return void
232      */
233     public function log($msg, $level = 1)
234     {
235         if ($this->verbosity >= $level) {
236             echo $msg . "\n";
237         }
238     }
239 }
240 ?>