Finish framework, include W3.css build web site via templates and jQuery
[infodrom/musiikki-web.git] / class / ajax.class.php
1 <?php
2
3 class AJAX {
4     protected static function callCommand($cmd, array $data=array())
5     {
6         if (($pipe = popen($cmd, 'w')) === false)
7             return false;
8
9         foreach ($data as $value)
10             fwrite($pipe, $value . "\n");
11
12         pclose($pipe);
13         return true;
14     }
15
16     public static function getdatetimeAction()
17     {
18         return array('year' => date('Y'),
19                      'month' => date('m'),
20                      'day' => date('d'),
21                      'hour' => date('H'),
22                      'minute' => date('i'),
23                      'second' => date('s'));
24     }
25
26     public static function datetimeAction()
27     {
28         $date = sprintf("%04d-%02d-%02d", $_POST['year'], $_POST['month'], $_POST['day']);
29         $time = sprintf("%02d:%02d:%02d", $_POST['hour'], $_POST['minute'], $_POST['second']);
30
31         $ok = self::callCommand("super musiikki-datetime", array($date, $time));
32
33         return $ok;
34     }
35
36     public static function getwifiAction()
37     {
38         if (!file_exists('/etc/hostapd/hostapd.conf'))
39             return array();
40
41         if (($f = fopen('/etc/hostapd/hostapd.conf', 'r')) === false)
42             return array();
43
44         $essid = '';
45         $password = '';
46
47         while (($line = fgets($f)) !== false) {
48             $line = trim($line);
49             if (substr($line,0,1) == '#') continue;
50             if (!strlen($line)) continue;
51
52             $parts = explode('=', $line, 2);
53             if ($parts[0] == 'ssid')
54                 $essid = $parts[1];
55             elseif ($parts[0] == 'wpa_passphrase')
56                 $password = $parts[1];
57         }
58
59         fclose($f);
60
61         return array('essid' => $essid,
62                      'password' => $password);
63     }
64
65     public static function wifiAction()
66     {
67         $ok = self::callCommand("super musiikki-wifi", array($_POST['essid'], $_POST['password']));
68
69         return $ok;
70     }
71
72     public static function getsharesAction()
73     {
74         return array('ro_name' => '\\\\' . $_SERVER['SERVER_ADDR'] .'\\'. Config::SHARE_READ,
75                      'ro_user' => Config::SHARE_READ,
76                      'ro_password' => '',
77                      'rw_name' => '\\\\' . $_SERVER['SERVER_ADDR'] .'\\'. Config::SHARE_WRITE,
78                      'rw_user' => Config::SHARE_WRITE,
79                      'rw_password' => '');
80     }
81
82     public static function sharesAction()
83     {
84         $ok = self::callCommand("super musiikki-shares", array($_POST['ro_password'], $_POST['rw_password']));
85
86         return $ok;
87     }
88
89     public static function gethaltAction()
90     {
91         return array();
92     }
93
94     public static function haltAction()
95     {
96         $ok = self::callCommand("super musiikki-halt");
97
98         return $ok;
99     }
100 }