<?php
/**
* Functions to access the router
*
* PHP version 5
*
* @category Tools
* @package  linksys-wrt3g-tools
* @author   Christian Weiske <cweiske@cweiske.de>
* @license  AGPL v3
* @link     http://cweiske.de/linksys-wrt3g-tools.htm
*/
require_once 'HTTP/Request2.php';


class Wrt3g
{
    /**
     * Router hostname/IP
     *
     * @var string
     */
    public $host;

    /**
     * Name of user with administration privileges
     *
     * @var string
     */
    public $user;

    /**
     * Password for $user
     *
     * @var string
     */
    public $password;

    protected static $arTranslations = array(
        'GPRS_MSG.WWBEAR'      => 'type',
        'GPRS_MSG.NNAME'       => 'network',
        'GPRS_MSG.STRENGTH'    => 'signal strength',
        'GPRS_MSG.ACQUIRING'   => 'aquiring',
        'GPRS_MSG.WWBEAR_GPRS' => 'GPRS',
        'GPRS_MSG.WWBEAR_UMTS' => 'UMTS',
    );



    /**
     * Returns the base URL to use for requests.
     * Includes username, password and host.
     *
     * @return string URL without trailing slash after the host
     */
    protected function getBaseUrl()
    {
        return 'http://'
            . $this->user
            . ':' . $this->password
            . '@' . $this->host;
    }


    /**
    * Reboots the router.
    *
    * @return HTTP_Request2_Response
    *
    * @throws Exception When the router can't be reached, or unauthorized
    */
    public function reboot()
    {
        $r = new HTTP_Request2();
        $r->setMethod(HTTP_Request2::METHOD_POST);
        $r->setUrl(
            $this->getBaseUrl() . '/apply.cgi'
        );
        $r->addPostParameter('action', 'Reboot');
        $r->addPostParameter('submit_button', 'Diagnostics');
        $r->addPostParameter('wait_time', 1);

        $resp = $r->send();
        return $resp;
    }//public function reboot()



    /**
    * Retrieves status information about the router
    *
    * @return array Array with several key-value pairs
    *               connection => connecting, disconnected, connected
    *
    * @throws Exception When the router can't be reached, or unauthorized
    */
    public function getStatus()
    {
        $arRetval = array();

        $strUrlBase = $this->getBaseUrl();

        /**
        * Connection status
        */
        $r = new HTTP_Request2();
        $r->setMethod(HTTP_Request2::METHOD_GET);
        $r->setUrl($strUrlBase . '/index_wstatus2.asp');
        $resp = $r->send();
        echo $resp->getStatus() . ' ' . $resp->getReasonPhrase() . "\n";
        $body = $resp->getBody();
        preg_match('/var status2 = "(.+)"/', $body, $arMatches);

        $strStatus = $arMatches[1];
        //Connecting
        //Disconnected
        //Connected
        $arRetval['connection'] = strtolower($strStatus);

        /**
        * GPRS/UMTS Status
        */
        $r->setUrl($strUrlBase . '/index_wstatus1.asp');
        $resp = $r->send();
        $body = $resp->getBody();
        $arRetval = array_merge($arRetval, $this->parseStatus($body));

        return $arRetval;
    }//public function getStatus()



    /**
    * Parses the body and returns extracted values
    *
    * @param string $body HTML document to parse
    *
    * @return array Array of key-value pairs
    */
    protected function parseStatus($body)
    {
        $arRetval = array();
        $arMatches = array();
        preg_match_all('#>Capture\(([^)]+)\)</#', $body, $arMatches);
        $arData = array();
        reset($arMatches[1]);
        while (current($arMatches[1])) {
            $key   = current($arMatches[1]);
            $value = next($arMatches[1]);
            if (isset(self::$arTranslations[$key])) {
                $key = self::$arTranslations[$key];
            }
            if (isset(self::$arTranslations[$value])) {
                $value = self::$arTranslations[$value];
            }
            $arRetval[$key] = $value;
            next($arMatches[1]);
        }

        return $arRetval;
    }
}
?>