update docblock
[linksys-wrt3g-tools.git] / Wrt3g / HtmlParser.php
index 1cd5373ff476c115e6c7cab7a5a046235beca5bf..ac4db350a799090b5698fba62f5e1c350c6c3630 100644 (file)
@@ -1,15 +1,15 @@
 <?php
 /**
-* HTML parsing for the linksys router HTML pages
-*
-* 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
-*/
+ * HTML parsing for the linksys router HTML pages
+ *
+ * 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
+ */
 class Wrt3g_HtmlParser
 {
     protected static $arTranslations = array(
@@ -21,18 +21,26 @@ class Wrt3g_HtmlParser
         'GPRS_MSG.WWBEAR_UMTS' => 'UMTS',
         'GPRS_MSG.GOOD'        => 'good',
         'GPRS_MSG.EXCELLENT'   => 'excellent',
+        'GPRS_MSG.CTIME'       => 'connection time',
+        'GPRS_MSG.CFW'         => 'card firmware',
+        'GPRS_MSG.CMOD'        => 'card model',
+        'GPRS_MSG.CREV'        => 'card revision',
+        'GPRS_MSG.IMSI'        => 'IMSI',
+        'GPRS_MSG.SESSIONUSAGE' => 'session usage',
     );
 
 
 
     /**
-    * Parses the body of /index_wstatus1.asp and returns extracted values.
-    *
-    * @param string $body HTML document to parse
-    *
-    * @return array Array of key-value pairs, probably with "type", "network" and
-    *               "signal strength"
-    */
+     * Parses the body of /index_wstatus1.asp and returns extracted values.
+     *
+     * @param string $body HTML document to parse
+     *
+     * @return array Array of key-value pairs, probably with
+     *               - "type"
+     *               - "network"
+     *               - "signal strength"
+     */
     public function index_wstatus1($body)
     {
         $doc = new DomDocument();
@@ -84,16 +92,19 @@ class Wrt3g_HtmlParser
 
 
     /**
-    * Parses the body of /index_wstatus2.asp and returns extracted values.
-    *
-    * @param string $body HTML document to parse
-    *
-    * @return array Array with "connection" as key and one of "connecting",
-    *               "connected" or "disconnected" as value.
-    */
+     * Parses the body of /index_wstatus2.asp and returns extracted values.
+     *
+     * @param string $body HTML document to parse
+     *
+     * @return array Array with "connection" as key and one of "connecting",
+     *               "connected" or "disconnected" as value.
+     */
     public function index_wstatus2($body)
     {
         preg_match('/var status2 = "(.+)"/', $body, $arMatches);
+        if (!isset($arMatches[1])) {
+            return array();
+        }
 
         $strStatus = $arMatches[1];
         //Connecting
@@ -101,4 +112,84 @@ class Wrt3g_HtmlParser
         //Connected
         return array('connection' => strtolower($strStatus));
     }
+
+
+
+    /**
+     * Parses the HTML body of /Status_NoAuth.asp and returns the extracted
+     * values.
+     *
+     * @param string $body HTML document to parse
+     *
+     * @return array Array of key-value pairs with the following keys:
+     *               - type
+     *               - network
+     *               - signal strength
+     *               - connection time
+     *               - session usage (array)
+     *               - card model
+     *               - card revision
+     *               - card firmware
+     *               - IMSI
+     *
+     * @todo FIXME implement "connection" value as index_wstatus2() does it
+     */
+    public function status_noauth($body)
+    {
+        $body = str_replace(
+            array('&nbsp;', '</font>', '<script>', '</script>'),
+            '', $body
+        );
+
+        $doc = new DomDocument();
+        libxml_use_internal_errors(true);//html is broken
+        $doc->loadHtml($body);
+               $xpath = new DOMXPath($doc);
+               $trs   = $xpath->query('//table//table/tbody/tr');
+        $arRaw = array();
+        foreach ($trs as $tr) {
+            $titleItems = $xpath->query('td[@width=125]/text()', $tr);
+            $valueItems = $xpath->query('td[@width=296]/*[1]', $tr);
+            if ($titleItems->length && $valueItems->length) {
+                $title = substr($doc->saveXML($titleItems->item(0)), 8, -2);
+                $value = $doc->saveXML($valueItems->item(0));
+                $arRaw[self::$arTranslations[$title]] = $value;
+            }
+        }
+
+        foreach ($arRaw as $key => &$value) {
+            $value = trim(strip_tags($value));
+            if (substr($value, 0, 8) == 'Capture(') {
+                $value = self::$arTranslations[substr($value, 8, -1)];
+            }
+        }
+        unset($value);
+
+        if (isset($arRaw['connection time'])) {
+            $arRaw['connection time'] = str_replace(
+                array(
+                    ' Capture(GPRS_MSG.HOURS)',
+                    ' Capture(GPRS_MSG.MINUTE)',
+                    ' Capture(GPRS_MSG.SECOND)',
+                ),
+                array(
+                    'h', 'm', 's'
+                ),
+                $arRaw['connection time']
+            );
+        }
+
+        if (isset($arRaw['session usage'])) {
+            $arS = explode(
+                'TX = ',
+                str_replace('RX = ', '', $arRaw['session usage'])
+            );
+            $arRaw['session usage'] = array(
+                'received' => $arS[0],
+                'sent'     => $arS[1]
+            );
+        }
+
+        return $arRaw;
+    }
 }
\ No newline at end of file