make munin script work again
[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     protected static $arTranslations = array(
40         'GPRS_MSG.WWBEAR'      => 'type',
41         'GPRS_MSG.NNAME'       => 'network',
42         'GPRS_MSG.STRENGTH'    => 'signal strength',
43         'GPRS_MSG.ACQUIRING'   => 'aquiring',
44         'GPRS_MSG.WWBEAR_GPRS' => 'GPRS',
45         'GPRS_MSG.WWBEAR_UMTS' => 'UMTS',
46     );
47
48     /**
49     * Reboots the router.
50     *
51     * @return HTTP_Request2_Response
52     *
53     * @throws Exception When the router can't be reached, or unauthorized
54     */
55     function reboot()
56     {
57         $r = new HTTP_Request2();
58         $r->setMethod(HTTP_Request2::METHOD_POST);
59         $r->setUrl(
60             'http://'
61             . $this->user
62             . ':' . $this->password
63             . '@' . $this->host
64             . '/apply.cgi'
65         );
66         $r->addPostParameter('action', 'Reboot');
67         $r->addPostParameter('submit_button', 'Diagnostics');
68         $r->addPostParameter('wait_time', 1);
69
70         $resp = $r->send();
71         return $resp;
72     }//function reboot()
73
74
75
76     /**
77     * Retrieves status information about the router
78     *
79     * @return array Array with several key-value pairs
80     *               connection => connecting, disconnected, connected
81     *
82     * @throws Exception When the router can't be reached, or unauthorized
83     */
84     function getStatus()
85     {
86         $arRetval = array();
87
88         $strUrlBase = 'http://'
89             . $this->user
90             . ':' . $this->password
91             . '@' . $this->host;
92
93         /**
94         * Connection status
95         */
96         $r = new HTTP_Request2();
97         $r->setMethod(HTTP_Request2::METHOD_GET);
98         $r->setUrl($strUrlBase . '/index_wstatus2.asp');
99         $resp = $r->send();
100         echo $resp->getStatus() . ' ' . $resp->getReasonPhrase() . "\n";
101         $body = $resp->getBody();
102         preg_match('/var status2 = "(.+)"/', $body, $arMatches);
103
104         $strStatus = $arMatches[1];
105         //Connecting
106         //Disconnected
107         //Connected
108         $arRetval['connection'] = strtolower($strStatus);
109
110         /**
111         * GPRS/UMTS Status
112         */
113         $r->setUrl($strUrlBase . '/index_wstatus1.asp');
114         $resp = $r->send();
115         $body = $resp->getBody();
116         $arRetval = array_merge($arRetval, $this->parseStatus($body));
117
118         return $arRetval;
119     }//function getStatus()
120
121
122
123     /**
124     * Parses the body and returns extracted values
125     *
126     * @param string $body HTML document to parse
127     *
128     * @return array Array of key-value pairs
129     */
130     function parseStatus($body)
131     {
132         $arRetval = array();
133         $arMatches = array();
134         preg_match_all('#>Capture\(([^)]+)\)</#', $body, $arMatches);
135         $arData = array();
136         reset($arMatches[1]);
137         while (current($arMatches[1])) {
138             $key   = current($arMatches[1]);
139             $value = next($arMatches[1]);
140             if (isset(self::$arTranslations[$key])) {
141                 $key = self::$arTranslations[$key];
142             }
143             if (isset(self::$arTranslations[$value])) {
144                 $value = self::$arTranslations[$value];
145             }
146             $arRetval[$key] = $value;
147             next($arMatches[1]);
148         }
149
150         return $arRetval;
151     }
152 }
153 ?>