Jump to content

Switch.. IF statements.. oy.


Wafflemann

Recommended Posts

Hello, I'm currently working on just a quick little frontend for  VirtualBox and I'm just running into a little bit of a problem that I can't find my way around. Now this is my first project in php. I'm familiar with programming concepts (well except this one apparently  :-\ ) and by familiar were talking I like to tinker with small languages.. no formal training.

Anyway, here we go.

 

So heres the main page. Pretty simple, you type the argument that goes with the following command and check the box next to the command you'd like to execute.

 

<HTML>
<HEAD>
<TITLE>LCCP ver1</TITLE>
</HEAD>

<BODY>
<?php include "header.php"; ?>
<form method="post" action="lccp_ver1.php">
<p>cmd="createvm"<br>
        Enter the name of the Virtual Guest you'd like to create:
        <input type="text" name="id">
        <input type="checkbox" name="createvm">
        </p>
<p>cmd="showvrtinfo"<br>
Enter the name of the Virtual Machine you'd like to view:
<input type="text" name="id2">
<input type="checkbox" name="showvminfo">	
</p>
<p>cmd="unregistervm"<br>
Enter the name of the Virtual Guest you'd like to delete:
<input type="text" name="id3">
<input type="checkbox" name="unregistervm">
</p>
<p>cmd="startvm"<br>
Enter the name of the Virtual Machine you'd like to start:
<input type="text" name="id4">
<input type="checkbox" name="startvm">
</p>
<p>cmd="controlvm"<br>
Enter the name of the Virtual Machine you'd like to control:
<input type="text" name="id5">
<input type="checkbox" name="controlvm">
</p>
<p>Then.. would you like to...</p>
<p>arg="pause" <input type="checkbox" name="controlvm_pause"></p>
<p>arg="resume" <input type="checkbox" name="controlvm_resume"></p>
<p>arg="reset" <input type="checkbox" name="controlvm_reset"></p>
<p>arg="poweroff" <input type="checkbox" name="controlvm_poweroff"></p>
<p>arg="savestate" <input type="checkbox" name="controlvm_savestate"></p>
<input type="submit" name="Submit" value="Submit">
</form>
</BODY>
</HTML>

 

And then here is the execution page

<HTML>
<HEAD>
<TITLE>LCCP command 
<?php 
function ServerLoad()
{

$stats = exec('uptime');

preg_match('/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/', $stats, $regs);

return ($regs[1].', '.$regs[2].', '.$regs[3]);
}
echo ServerLoad(); 

?>
</TITLE>
</HEAD>

<BODY>
<?php include "header.php"; ?>
<?php
	function createvm($id) {
                        $output = shell_exec("VBoxManage createvm --name " . $id . " --register");
                        return $output;
                        }
	function showvminfo($id) {
		$output = shell_exec("VBoxManage showvminfo " . $id);
		return $output;
		}
	function unregistervm($id) {
		$output = shell_exec("VBoxManage unregistervm ". $id ." --delete");
		return $output;
		}
	function startvm($id) {
		$output = shell_exec("VBoxManage startvm ". $id ." --type headless");
		return $output;
		}
	function controlvm($id,$arg) {
		$output = shell_exec("VBoxManage controlvm ". $id ." ". $arg);
		return $output;
		}
	function print_output($output) {
		 echo'
		 <pre>'. $output . '
	         </pre>';
		}
//******************exec main**********************

        if (isset($_POST['createvm'])) {
//              createvm($_POST['id2']);
                print_output(createvm($_POST['id']));
                }
if (isset($_POST['showvminfo'])) {
//		showvminfo($_POST['id']);
	print_output(showvminfo($_POST['id2']));
	}
if (isset($_POST['unregistervm'])) {
//		unregistervm($_POST['id3');
	print_output(unregistervm($_POST['id3']));
	}
if (isset($_POST['startvm'])) {
//		startvm($_POST['id4']);
	print_output(startvm($_POST['id4']));
	}
	if ((isset($_POST['controlvm'])) AND (isset($_POST['controlvm_pause'))) {
		//controlvm($_POST['id5']," pause");
		print_output(controlvm($_POST['id5']," pause"));
		}
	if ((isset($_POST['controlvm'])) AND (isset($_POST['controlvm_resume'))) {
		//controlvm($_POST['id5']," resume");
		print_output(controlvm($_POST['id5']," resume"));
		}
	if ((isset($_POST['controlvm'])) AND (isset($_POST['controlvm_reset'))) {
		//controlvm($_POST['id5']," reset");
		print_output(controlvm($_POST['id5']," reset"));
		}
	if ((isset($_POST['controlvm'])) AND (isset($_POST['controlvm_poweroff'))) {
		//controlvm($_POST['id5']," poweroff");
		print_output(controlvm($_POST['id5']," poweroff"));
		}
	if ((isset($_POST['controlvm'])) AND (isset($_POST['controlvm_savestate'))) {
		//controlvm($_POST['id5']," savestate");
		print_output(controlvm($_POST['id5']," savestate"));
		}
	//**********working on***************
	//teleport?

?>
</BODY>
</HTML>

 

Now my question is with the if statements. Well, number one I guess is there a more efficient way of doing this that I'm overlooking? I looked into switches but I don't know how I'd pull that off with checkboxes without some complicated loop and somehow converting the checkboxes "yes" or "nos" into numbers or something. If statements just seemed like less of a headache but I may be finding otherwise haha.

 

Number two here is how do I get the "controlvm" command to work? When I comment out that last indented lines of if statements the program runs perfect (and thats with the controlvm function as well. so.. the problem isn't there). Basically whats the appropriate way to do this. I'm stuck and my limited program knowledge and Google searching is starting to run thin.

 

Thanks in advance for the help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.