-
Notifications
You must be signed in to change notification settings - Fork 1
/
stationcontrol.php
92 lines (56 loc) · 1.86 KB
/
stationcontrol.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
$Name = $_POST['name'];
$first = $_POST['first'];
if (isset($first)){
$result= runExternal("echo '$Name' > ~/.config/pianobar/ctl",$code);
}else{
$result= runExternal("echo -n 's' > ~/.config/pianobar/ctl ;",$code);
$result= runExternal("echo '$Name' > ~/.config/pianobar/ctl ",$code);
}
/*
if ($first === "s"){
$result= runExternal("echo '$Name' > ~/.config/pianobar/ctl",$code);
}else{
$result= runExternal("echo 's' > ~/.config/pianobar/ctl ;",$code);
$result= runExternal("echo '$Name' > ~/.config/pianobar/ctl ",$code);
}
*/
print "<pre>";
print $result;
print "</pre>\n";
print "<b>code: $code</b>\n";
function runExternal($cmd,&$code) {
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a file to write to
);
$pipes= array();
$process = proc_open($cmd, $descriptorspec, $pipes);
$output= "";
if (!is_resource($process)) return false;
#close child's input imidiately
fclose($pipes[0]);
stream_set_blocking($pipes[1],false);
stream_set_blocking($pipes[2],false);
$todo= array($pipes[1],$pipes[2]);
while( true ) {
$read= array();
if( !feof($pipes[1]) ) $read[]= $pipes[1];
if( !feof($pipes[2]) ) $read[]= $pipes[2];
if (!$read) break;
$ready= stream_select($read, $write=NULL, $ex= NULL, 2);
if ($ready === false) {
break; #should never happen - something died
}
foreach ($read as $r) {
$s= fread($r,1024);
$output.= $s;
}
}
fclose($pipes[1]);
fclose($pipes[2]);
$code= proc_close($process);
return $output;
}
?>