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