I debated trying to do these as functions, or as items that can be called by a ajax script, but decided to go this route (not sure why). My question is, is there a better way I could have written it.
Additionally, how complex is it to have these be able to be performed by an ajax function. I’ve not used ajax as it seems to be somewhat complex as opposed to say jQuery.
Essentially, what I want to do in the grand scheme is when a user clicks an icon, we’ll use RESTART for example, I want the action to be loaded into a div that is hidden, and when completed, show a jquery based notification that the action has been completed. I have the styles and everything, I just need the basic idea of what to do.
Full disclosure, I’m rewriting an older script to use PDO, since when it was abandoned, it was using the mysql extension.
Any Ideas? Feedback would be greatly appreciated.
[php]
if (isset($_GET[‘command’]) && ($_GET[‘command’] == “restart”)) {
$db = new Db();
$gettype = $db->query("SELECT * FROM servers WHERE portnum = ".$_GET[‘port’]);
foreach ($gettype as $svr) {
$type = $svr[‘type’];
break;
}
if ($type == “shoutcast”) {
// kill and restart server
$pidfile = fopen($getDataDirectory."shoutcast/".$_GET['port']."/server.pid", "r") or die("Unable to open file!");
$pid = fread($pidfile,filesize($getDataDirectory."shoutcast/".$_GET['port']."/server.pid"));
fclose($pidfile);
//$pid = shell_exec("nohup ".$config['sc_serv']." ".$config['smi_path']."/servers/shoutcast/".$portbase."/".$srvname.".conf > /dev/null & echo $!");
$cleanpid = trim($pid);
exec("echo $cleanpid > ".$getDataDirectory."shoutcast/".$_GET['port']."/server.pid");
// set alert to show
echo "<script>showNotification('success','restarted');</script>";
} else {
echo “”;
}
}
if (isset($_GET[‘command’]) && ($_GET[‘command’] == “start”)) {
$db = new Db();
$gettype = $db->query("SELECT * FROM servers WHERE portnum = ".$_GET[‘port’]);
foreach ($gettype as $svr) {
$type = $svr[‘type’];
$port = $svr[‘portnum’];
$srvname = $svr[‘servername’];
break;
}
if ($type == “shoutcast”) {
// kill and restart server
$sc_conf = $getDataDirectory."shoutcast/".$port."/server.conf";
// Check that config file exists before trying to launch
$cmdstr = shell_exec($getShoutcastBinary." ".$getDataDirectory."shoutcast/".$port."/server.conf > /dev/null & echo $!");
$pid = shell_exec($cmdstr);
$cleanpid = trim($pid);
// Write PID to file
$fd = fopen($getDataDirectory."shoutcast/".$port."/".$srvname.".pid", "w+");
fputs($fd, $cleanpid);
fclose($fd);
} else {
echo "Icecast";
}
}
if (isset($_GET[‘command’]) && ($_GET[‘command’] == “stop”)) {
$db = new Db();
$gettype = $db->query("SELECT * FROM servers WHERE portnum = ".$_GET[‘port’]);
foreach ($gettype as $svr) {
$type = $svr[‘type’];
$port = $svr[‘portnum’];
$srvname = $svr[‘servername’];
break;
}
if ($type == “shoutcast”) {
// kill and restart server
$pidfile = fopen($getDataDirectory.“shoutcast/”.$_GET[‘port’]."/server.pid", “r”) or die(“Unable to open file!”);
$pid = fread($pidfile,filesize($getDataDirectory.“shoutcast/”.$_GET[‘port’]."/server.pid"));
fclose($pidfile);
$killserver = shell_exec("kill -9 ".$pid);
if ($killserver) {
shell_exec("rm ".$getDataDirectory."shoutcast/".$port."/".$srvname.".pid");
} else {
die("Failed to kill server");
}
} else {
echo “Icecast”;
}
}
[/php]