function checksock() { /* Accept incoming requests and handle them as child processes */ $client = @socket_accept($GLOBALS['sock']); //echo "Client " .$client ."\n"; if (!$client === false) { // Read the input from the client – 1024 bytes //$input = socket_read($client, 1024); $status = @socket_get_status($client); $input = @socket_read($client, 2048); // Strip all white spaces from input echo "RAW " . $input . "\n"; if ($input == '') { break; } //$output = ereg_replace("[ \t\n\r]","",$input).chr(0); //$output = ereg_replace("[ \t\n\r]","",$input); $output = explode(" ", $input); switch (strtolower($output[0])) { case 'white': $response = "Turn on white\n\n"; socket_write($client, $response); socket_close($client); break; case '-get': if (isset($output[1])) { switch (strtolower($output[1])) { case 'sd': $response = "Strobe Duration " . $GLOBALS['strobedelay'] . "\n"; socket_write($client, $response, strlen($response)); socket_close($client); break; } } else { $response = shwhelp(); socket_write($client, $response, strlen($response)); socket_close($client); } break; case '-yard': case '-y': if (isset($output[1])) { yardlight($output[1]); } else { $response = "Missing power number 0-10\n"; socket_write($client, $response); socket_close($client); } break; case '-color': case '-c': echo "In Color Case\n"; echo "OUTPUT 1 " . $output[1] . "\n"; if (isset($output[1])) { echo "COLOR SET\n"; $colorv = explode(",", $output[1]); echo "SIZE COLORV " . sizeof($colorv); if (sizeof($colorv) == 3) { $GLOBALS['rl'] = $colorv[0]; $GLOBALS['gl'] = $colorv[1]; $GLOBALS['bl'] = $colorv[2]; changecolor($colorv[0], $colorv[1], $colorv[2]); $response = "Color R" . $GLOBALS['rl'] . " G" . $GLOBALS['gl'] . " B" . $GLOBALS['bl'] . "\n"; socket_write($client, $response); socket_close($client); } } break; case '-setcolor': echo "In SetColor Case\n"; if (isset($output[1])) { $colorv = explode(",", $output[1]); if (sizeof($colorv) == 3) { $response = "Color Set to\n"; $response .= "R " . $colorv[0] . " G " . $colorv[1] . " B " . $colorv[2]; socket_write($client, $response); socket_close($client); readini($GLOBALS['inifile']); $GLOBALS['rl'] = $colorv[0]; $GLOBALS['gl'] = $colorv[1]; $GLOBALS['bl'] = $colorv[2]; $GLOBALS['ini_array']['color']['r'] = $GLOBALS['rl']; $GLOBALS['ini_array']['color']['g'] = $GLOBALS['gl']; $GLOBALS['ini_array']['color']['b'] = $GLOBALS['bl']; $result = write_ini_file($GLOBALS['ini_array'], $GLOBALS['inifile']); } } break; case '-red': $GLOBALS['rl'] = 10; $GLOBALS['gl'] = 0; $GLOBALS['bl'] = 0; changecolor($GLOBALS['rl'], $GLOBALS['gl'], $GLOBALS['bl']); break; case 'test': $response = "Testing\n\n"; socket_write($client, $response); socket_close($client); looptest(); break; case "-s": socket_write($client, listscene()); socket_close($client); break; case "-snd": socket_write($client, listsound()); socket_close($client); break; case "-sl": if (isset($output[1])) { socket_write($client, showscene($output[1])); } else { socket_write($client, "Need Scene "); } socket_close($client); break; case "-run": if (isset($output[1])) { socket_write($client, runscene($output[1])); socket_close($client); } break; case '-p': case '-P': //try to fork or background process if (isset($output[1])) { /*socket_write($client, $output[1]); socket_close($client); return;*/ $c = getcwd() . '/bp.php ' . $output[1] . ' >bp.txt 2>&1 & echo $!'; //socket_write($client, $c); $GLOBALS['runpid'] = system($c); $status = system('ps aux | grep -i ' . $GLOBALS['runpid']); //echo $status; socket_write($client, $GLOBALS['runpid']); } else { socket_write($client, "Scene Number Required"); } socket_close($client); break; case '-pk': if (isset($output[1])) { $status = system('sudo kill ' . $output[1]); socket_write($client, $status); socket_close($client); } break; case '-ps': if (isset($GLOBALS['runpid'])) { $status = system('ps aux | grep -i ' . $GLOBALS['runpid'] . ' | grep -v grep'); socket_write($client, "PID STATUS " . $GLOBALS['runpid'] . " " . $status . " end"); } else { socket_write($client, "PID NOT SET"); } socket_close($client); break; case '-reset': //we should try a reset process to get all the relays back to of break; case 'kill': $response = "Killing\n\n"; socket_write($client, $response); socket_close($client); socket_close($GLOBALS['sock']); exit; break; case "--help": case "-help": case "--h": case "-h": $response = shwhelp(); socket_write($client, $response, strlen($response)); socket_close($client); break; default: $response = "default\n\n"; socket_write($client, $response); socket_close($client); break; } } // Display output back to client //socket_write($client, $response); // Close the client (child) socket //socket_close($client); }
<?php //Testing the background proccess /*while (true) { echo ".\n"; sleep(1); }*/ if (isset($argv[1])) { fclose(STDIN); fclose(STDOUT); fclose(STDERR); $STDIN = fopen('/dev/null', 'r'); $STDOUT = fopen(getcwd() . "/scene.log", 'wb'); $STDERR = fopen(getcwd() . "/sceneer.log", 'wb'); runscene($argv[1]); } else { echo "NO ARG\n"; } //'******************************************************************************* function runscene($num) { $dir = dir(getcwd() . "/scene"); $farray = array(); $cmdarray = array(); echo "Scene " . $num . " Start " . date('y-m-d H:i:s'); echo "\n"; //List files in directory while (($file = $dir->read()) !== false) { //Make sure it's a .txt file if (strlen($file) < 5 || substr($file, -4) != '.scn') {