Support uplink network management when two wlan devices are available
[infodrom/musiikki-web.git] / class / ajax.class.php
index ec34f5e..45bf06e 100644 (file)
@@ -69,6 +69,66 @@ class AJAX {
        return $ok;
     }
 
+    public static function getnetworkAction()
+    {
+       $name = NULL;
+       $conf = '/etc/network/interfaces.d/uplink.conf';
+       if (file_exists($conf)) {
+           $dest = readlink($conf);
+           $name = substr($dest, 9, -5);
+       }
+
+       $upstream = [];
+       foreach (new DirectoryIterator(dirname($conf)) as $fileInfo) {
+           if (substr($fileInfo->getBasename(), 0, 9) != 'upstream_') continue;
+           $upstream[] = substr($fileInfo->getBasename('.conf'), 9);
+       }
+       sort($upstream);
+
+       $ip = NULL;
+       $lines = [];
+       $f = popen("ip -f inet addr show wlan0", 'r');
+       while (($line = fgets($f)) !== false)
+           $lines[] = rtrim($line);
+       pclose($f);
+       if (count($lines)) {
+           if (preg_match('/inet (\d+\.\d+\.\d+\.\d+)\/.*/', $lines[1], $matches))
+               $ip = $matches[1];
+       }
+
+       $networks = [];
+       $f = popen("iwlist wlan0 scan", 'r');
+       while (($line = fgets($f)) !== false) {
+           if (preg_match('/ESSID:"(.*?)"/', $line, $matches))
+               if (strlen($matches[1]))
+                   $networks[] = $matches[1];
+       }
+       pclose($f);
+       sort($networks);
+
+       return ['name' => $name,
+               'ip' => $ip,
+               'networks' => $networks,
+               'upstream' => $upstream];
+    }
+
+    public static function networkAction()
+    {
+       $ok = self::callCommand("super musiikki-wlan", array($_POST['wlan']));
+
+       return $ok;
+    }
+
+    public static function essidAction()
+    {
+       if (!preg_match('/^[A-Za-z0-9_-]+$/', $_POST['name']))
+           return;
+
+       $ok = self::callCommand("super musiikki-essid", array($_POST['name'], $_POST['essid'], $_POST['psk']));
+
+       return $ok;
+    }
+
     public static function getsharesAction()
     {
        return array('ro_name' => '\\\\' . $_SERVER['SERVER_ADDR'] .'\\'. Config::SHARE_READ,
@@ -110,6 +170,7 @@ class AJAX {
 
     public static function haltAction()
     {
+       $ok = self::callCommand("super musiikki-wlan", array(''));
        $ok = self::callCommand("super musiikki-halt");
 
        return $ok;