test skeleton for status_noauth
[linksys-wrt3g-tools.git] / Wrt3g / HtmlParser.php
index 03ac5742bd1b11090bfe9d3f5aa9945fc7143f95..fe81c586ef1421d25eab8243574bfd0011ac9ca1 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(
@@ -19,27 +19,55 @@ class Wrt3g_HtmlParser
         'GPRS_MSG.ACQUIRING'   => 'aquiring',
         'GPRS_MSG.WWBEAR_GPRS' => 'GPRS',
         'GPRS_MSG.WWBEAR_UMTS' => 'UMTS',
+        'GPRS_MSG.GOOD'        => 'good',
+        'GPRS_MSG.EXCELLENT'   => 'excellent',
     );
 
 
 
     /**
-    * 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
-    */
+     * 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"
+     */
     public function index_wstatus1($body)
     {
-        $arRetval = array();
+        $doc = new DomDocument();
+        $doc->loadHtml($body);
+               $xpath = new DOMXPath($doc);
+               $entries = $xpath->query('//table/tr/td');
+
         $arMatches = array();
-        preg_match_all('#>Capture\(([^)]+)\)</#', $body, $arMatches);
+               foreach ($entries as $entry) {
+            $s = $doc->saveXML($entry);
+            $s = str_replace(
+                array('<![CDATA[', ']]>'),
+                '', $s
+            );
+            $s = strip_tags($s);
+            //some strange utf8 space char
+            $s = str_replace("\xC2\xA0", ' ', $s);
+            $s = trim($s);
+            $s = str_replace(
+                array('Capture(', ') :'),
+                '', $s
+            );
+            if (substr($s, -1) == ')') {
+                $s = substr($s, 0, -1);
+            }
+            $arMatches[] = $s;
+        }
+
+
+        $arRetval = array();
         $arData = array();
-        reset($arMatches[1]);
-        while (current($arMatches[1])) {
-            $key   = current($arMatches[1]);
-            $value = next($arMatches[1]);
+        reset($arMatches);
+        while (current($arMatches)) {
+            $key   = current($arMatches);
+            $value = next($arMatches);
             if (isset(self::$arTranslations[$key])) {
                 $key = self::$arTranslations[$key];
             }
@@ -47,7 +75,7 @@ class Wrt3g_HtmlParser
                 $value = self::$arTranslations[$value];
             }
             $arRetval[$key] = $value;
-            next($arMatches[1]);
+            next($arMatches);
         }
 
         return $arRetval;
@@ -56,13 +84,13 @@ 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);
@@ -73,4 +101,18 @@ 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 FIXME
+     */
+    public function status_noauth($body)
+    {
+    }
 }
\ No newline at end of file