command aliases
[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
51     /**
52      * Returns the base URL to use for requests that require authentification.
53      * Includes username, password and host.
54      *
55      * @return string URL without trailing slash after the host
56      */
57     protected function getAuthBaseUrl()
58     {
59         return 'http://'
60             . $this->user
61             . ':' . $this->password
62             . '@' . $this->host;
63     }
64
65
66
67     /**
68      * Returns the base URL to use for requests that do
69      * not require authentification.
70      *
71      * @return string URL without trailing slash after the host
72      */
73     protected function getAnonBaseUrl()
74     {
75         return 'http://' . $this->host;
76     }
77
78
79
80     /**
81     * Reboots the router.
82     *
83     * @return HTTP_Request2_Response
84     *
85     * @throws Exception When the router can't be reached, or unauthorized
86     */
87     public function reboot()
88     {
89         $r = new HTTP_Request2();
90         $r->setMethod(HTTP_Request2::METHOD_POST);
91         $r->setUrl(
92             $this->getAuthBaseUrl() . '/apply.cgi'
93         );
94         $r->addPostParameter('action', 'Reboot');
95         $r->addPostParameter('submit_button', 'Diagnostics');
96         $r->addPostParameter('wait_time', 1);
97
98         $resp = $r->send();
99         return $resp;
100     }//public function reboot()
101
102
103
104     /**
105     * Retrieves status information about the router
106     *
107     * @return array Array with several key-value pairs
108     *               connection => connecting, disconnected, connected
109     *
110     * @throws Exception When the router can't be reached, or unauthorized
111     */
112     public function getStatus()
113     {
114         $arRetval = array();
115
116         $strUrlBase = $this->getAuthBaseUrl();
117         $parser = new Wrt3g_HtmlParser();
118
119         /**
120         * Connection status
121         */
122         $r = new HTTP_Request2();
123         $r->setMethod(HTTP_Request2::METHOD_GET);
124         $r->setUrl($strUrlBase . '/index_wstatus2.asp');
125         $resp = $r->send();
126         echo $resp->getStatus() . ' ' . $resp->getReasonPhrase() . "\n";
127         $arRetval = $parser->index_wstatus2($resp->getBody());
128
129         /**
130         * GPRS/UMTS Status
131         */
132         $r->setUrl($strUrlBase . '/index_wstatus1.asp');
133         $resp = $r->send();
134         $body = $resp->getBody();
135         $arRetval = array_merge($arRetval, $parser->index_wstatus1($body));
136
137         return $arRetval;
138     }//public function getStatus()
139 }
140 ?>