0cd9de0a912587fcda4eb3360830a0557bade12a
[linksys-wrt3g-tools.git] / Wrt3g.php
1 <?php
2 /**
3 * Functions to access the router
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
15
16 class Wrt3g
17 {
18     /**
19      * Router hostname/IP
20      *
21      * @var string
22      */
23     public $host;
24
25     /**
26      * Name of user with administration privileges
27      *
28      * @var string
29      */
30     public $user;
31
32     /**
33      * Password for $user
34      *
35      * @var string
36      */
37     public $password;
38
39
40
41     /**
42      * Returns the base URL to use for requests.
43      * Includes username, password and host.
44      *
45      * @return string URL without trailing slash after the host
46      */
47     protected function getBaseUrl()
48     {
49         return 'http://'
50             . $this->user
51             . ':' . $this->password
52             . '@' . $this->host;
53     }
54
55
56     /**
57     * Reboots the router.
58     *
59     * @return HTTP_Request2_Response
60     *
61     * @throws Exception When the router can't be reached, or unauthorized
62     */
63     public function reboot()
64     {
65         $r = new HTTP_Request2();
66         $r->setMethod(HTTP_Request2::METHOD_POST);
67         $r->setUrl(
68             $this->getBaseUrl() . '/apply.cgi'
69         );
70         $r->addPostParameter('action', 'Reboot');
71         $r->addPostParameter('submit_button', 'Diagnostics');
72         $r->addPostParameter('wait_time', 1);
73
74         $resp = $r->send();
75         return $resp;
76     }//public function reboot()
77
78
79
80     /**
81     * Retrieves status information about the router
82     *
83     * @return array Array with several key-value pairs
84     *               connection => connecting, disconnected, connected
85     *
86     * @throws Exception When the router can't be reached, or unauthorized
87     */
88     public function getStatus()
89     {
90         $arRetval = array();
91
92         $strUrlBase = $this->getBaseUrl();
93
94         /**
95         * Connection status
96         */
97         $r = new HTTP_Request2();
98         $r->setMethod(HTTP_Request2::METHOD_GET);
99         $r->setUrl($strUrlBase . '/index_wstatus2.asp');
100         $resp = $r->send();
101         echo $resp->getStatus() . ' ' . $resp->getReasonPhrase() . "\n";
102         $arRetval = $parser->index_wstatus2($resp->getBody());
103
104         /**
105         * GPRS/UMTS Status
106         */
107         $r->setUrl($strUrlBase . '/index_wstatus1.asp');
108         $resp = $r->send();
109         $body = $resp->getBody();
110         $arRetval = array_merge($arRetval, $parser->index_wstatus1($body));
111
112         return $arRetval;
113     }//public function getStatus()
114 }
115 ?>