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