<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>CFTT Federated Testing CD - test a Mobile Device tool</title>

<link rel="stylesheet" type="text/css" href="../css/reset.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../css/main_style.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../css/layout.css" media="screen" />
<script>
function ck_mobile(f)
{
	var r =f.elements['selected_device'];
	if (r[r.length-1].checked) {
		mess = ''
		if (f.model.value == '') mess += '\nYou need to enter phome model'
		if (f.make.value == '') mess += '\nYou need to enter phome make'
		if (f.os_version.value == '') mess += '\nYou need to enter phome OS version'
		if (f.firmware.value == '') mess += '\nYou need to enter phome firmware'
		if (mess == '') return true
		alert (mess)
		return false
	}
	return true
}
function ck_selection(f)
{
#	ok = false
	ok = true
	var r =f.elements['selected_device'];
	for (ix = 0, lx = r.length; ix < lx; ix++){
		if (r[ix].checked) {ok = true}
	}
	if (ok) return true
	alert('No device selected, You have to select a device');
	alert(r)
	return false;
}
function ck_selection1(f)
{
	mess = 'fix this mess';
	if (f.selected_device.value == '')
	{
		alert('You need to select a device');
		return false;
	}
	mess += ':\n'+f;
	mess += ':\n'+f.selected_device[0].value;
	alert(mess+': '+f.selected_device.value);
	return false;
}
function ck_tool (f)
{
	mess = '';
	if (f.toolname.value == '') {mess += 'You need to enter tool name.';};
	if (f.toolversion.value == '') {mess += '\nYou need to enter tool version.';};
	if (mess == '' ) {return true;};
	alert(mess);
	return false;
}
</script>
<style>
	table, th, td {
	   border-collapse: collapse;
	   border: 1px solid black;
	   } 
	tr {
	   border-collapse: collapse;
	   border: 2px solid black;
	   } 
</style>
</head>
<body id="menu1">
	<div id="mainWrapper">
		<div id="topBannerContainer">
			<div id="logoContainer">
  				<img src="../images/banner.png" alt="CFTT Federated Testing - NIST">
			</div>
            <!-- Menu Starts-->
			<div id="menuContainer">
                          <?php
                                include '../include/global.php';
                                print_main_horizontal_menu_contents();
                          ?>

    		</div>
            <!-- Menu Ends-->
		</div>
		<!-- TopBanner Ends-->
	  
		<!-- Main Content Starts-->	
		<div id="subContentContainer">
		  <div id="leftContent">
		    <div id="leftLinks">
<div>
<?php
include '../include/mdt-global.php';
?>
		    </div>
		    </div>
		  </div>			
		  <div id="rightContent">
<!--		    <div id="breadcrumb"><a href="../Federated_Testing_Home_Page.php">Home</a> > Test a disk imaging tool</div>-->
			<div id="breadcrumb"><a href="../Federated_Testing_Home_Page.php" class="nav">Home</a> &gt; <a href="/mobiledevice/mdt.php" class="nav"> Mobile Device Testing Home</a> &gt; Creating/Editing Mobile Device List</div>

<?php

$have_phones = FALSE;
$filename = 'devices.txt';

$continue = 1;

// is_ftlogs_mounted() returns the path to where we should store log files (e.g.,  
// /tmp or /media/FT-LOGS). It also checks if the user has mounted their log drive.
// is_ftlogs_mounted() will return 0/false and print an error message if the 
// user's log drive is not mounted
$path = is_ftlogs_mounted(FALSE);

if (!$path) { 
  // if the user's log drive isn't mounted, don't continue
  $continue = 0;
}

// if the user's log drive isn't mounted, don't continue			  
if ($continue){
	echo '<h1 style="font-size:1.8em">Creating/Editing Mobile Device List</h1>';
	//<!--<h2 style="font-size:1.7em; color:#008000">Update Test Device List</h2>-->			  		  
	$dname = $path."/".$filename;			  

	if (file_exists($dname)){
		$dfile = fopen($dname,"r");
		if ($dfile != FALSE){
			$p_line = fgets($dfile);
			while (!feof($dfile)){
				/*
				printf ("<br>LINE: %s",$p_line);
				*/
				$the_phones [] = $p_line;
				$p_line = fgets($dfile);
				$have_phones = TRUE;
			}
		}
		fclose($dfile);
	}
	extract($_POST);
	$max = 5;
	$maxi = 'phone00';
			/*
	echo "<br>Start of phone list";
	*/
	$work_on = 'none';
	if ($have_phones)
		{ /*echo '<br>have phones';*/
		foreach ($the_phones as $q){
			$p = explode(';',$q);
			/*
			echo "<br>";
			echo $p[0];
			*/
			if ($maxi < $p[0]) $maxi = $p[0];
			/*
			echo ' ';
			echo $p[1];
			printf (" %s %s\n",$p[2],$p[3]);
			*/
			if ($p[0] == $selected_device) $work_on = $q;
		}
	}
			/*
	else { echo '<br>No phones';}
	echo "<br>End of phone list<br>";
	if (isset($_POST)){
		echo "<br>Start of form items list<br>";
		$keys = array_keys($_POST);
		foreach ($keys as $dongxi){
			printf ("<br>DongXI: %s ",$dongxi);
			printf (" Value: [%s] \n",$_POST[$dongxi]);
			printf ("<br>DongXI: %s (%s)\n",$dongxi,$_POST($dongxi));
		}
		echo '<br>DEV: ';
		echo $selected_device;
		echo ' ';
		echo $work_on;
	}
	echo "<br>End of form items list<br>";
			*/

	$open_div = '<div style="padding: 0em 1em .25em .5em; background: #eeeeee url(/images/ui-bg_highlight-soft_100_eeeeee_1x100.png) 50% top repeat-x;">';
	$close_div = '</div>';

	if (isset($new)){
		echo '<h2 style="font-size:1.7em; color:#008000">Adding New Test Device</h2>';
		// try to open devices.txt
		if ($dfile = fopen($dname,"a")){
			// check that the user entered a value for manufacturer on the previous page
			if ($make == ""){
				echo "<p><b>Error:&nbsp;</b> <i><b>No value entered for \"manufacturer.\" Please use the browser's back button to return to the previous page and enter the manufacturer of your test device.</b></i></p>";
				$continue = 0;			
			}
			// check that the user entered a value for model on the previous page
			if ($model == ""){
				echo "<p><b>Error:&nbsp;</b> <i><b>No value entered for \"model and model number.\" Please use the browser's back button to return to the previous page and enter the model and model number of your test device.</b></i></p>";
				$continue = 0;			
			}
			// check that the user entered a value for OS Version on the previous page
			if ($os_version == ""){
				echo "<p><b>Error:&nbsp;</b> <i><b>No value entered for \"OS version.\" Please use the browser's back button to return to the previous page and enter the OS version of your test device.</b></i></p>";
				$continue = 0;			
			}
			// if we're missing information, i.e., the user didn't something, don't continue
			if ($continue){
				$ph = explode('e',$maxi);
				/*
				printf ("<br>This is new %s %s\n",$selected_device,$maxi);
				printf ("<br>This is %s PHONE%02d\n",$selected_device,$ph[1]+1);
				*/
				$phone = sprintf("phone%02d", $ph[1]+1);
				$new_device = sprintf("%s;%s;%s;%s;%s;%s;%s;%s\n",
					$phone,$make,$model,$os,$os_version,$firmware,$network,$has_sim);
				/*
				echo $new_device;
				*/
				//echo "'phone".$ph[1]."' written to '".$path."/".$filename."' <br><br>";
				// write new device description to file
				fwrite ($dfile,$new_device);
				echo "<p><i>Success! New device added!!</i><br></p>";
				echo $open_div; /* ----------------------------------------------------- */
				//printf("<p><i>Success! New device added!!</i><br><br>Manufacturer: %s<br>Model: %s<br>OS: %s<br>OS Version: %s<br>Firmware: %s<br>Network: %s<br>Test SIM/UICC: %s Test</p>", $make,$model,$os,$os_version,$firmware,$network,$has_sim);
				printf("<p><!-- <i>NEW DEVICE DESCRIPTION</i><br> -->Manufacturer: %s<br>Model: %s<br>OS: %s<br>OS Version: %s<br>Firmware: %s<br>Network: %s<br>Test SIM/UICC: %s Test</p>",
					$make,$model,$os,$os_version,$firmware,$network,$has_sim);
				echo $close_div; /* ----------------------------------------------------- */
			}
			fclose($dfile);
		} else { // couldn't open file to write description; fail
			//echo $open_div; /* ----------------------------------------------------- */
			echo "<p><b>Error: <i>".basename($_SERVER['PHP_SELF'])."</i> encountered ";
			echo "an error trying to write to ".$path."/".$filename.". New device ";
			echo "COULD NOT be added.</b></p>";
		}
	} elseif (isset($update)) {
		echo '<h2 style="font-size:1.7em; color:#008000">Updating Test Device</h2>';
		/*
		printf ("<br>This is Update %s\n",$selected_device);
		*/
		// check, did the user select a device to update?
		if (isset($selected_device)){
			// try to open devices.txt
			if ($dfile = fopen($dname,"w")){
				$p_line = fgets($dfile);
				foreach ($the_phones as $a_phone){
					$p = explode(';',$a_phone);
					if ($p[0] != $selected_device){
						/*
						printf ("<br>Keep %s %s\n",$p[0],$selected_device);
						*/
						fwrite ($dfile,$a_phone);
					} else {
						/*
						printf ("<br>%s %s updated\n",$p[0],$p[1]);
						*/
						if ($make == '') $make = $p[1];
						if ($model == '') $model = $p[2];
						if ($os_version == '') $os_version = $p[4];
						if ($firmware == '') $firmware = $p[5];
						if ($has_sim == '') $has_sim = $p[7];
						/*
						printf ("<br>%s %s %s %s %s",$make,$model,$os_version,$firmware,$has_sim);
						*/
						$new_device = sprintf("%s;%s;%s;%s;%s;%s;%s;%s\n",
							$p[0],$make,$model,$os,$os_version,$firmware,$network,$has_sim);
						fwrite ($dfile,$new_device);
					}
				}
				fclose($dfile);
				echo "<p><i>Success! Device update completed!!</i><br></p>";
				echo $open_div; /* ----------------------------------------------------- */	
				//printf("<p>Update Device: %s %s<br>OS: %s OS Version %s<br>Firmware: %s<br>Network: %s<br>Test SIM/UICC: %s Test<br>",$make,$model,$os,$os_version,$firmware,$network,$has_sim);
				printf("<p><!-- <b>Updated Device Description:</b> --><br>Manufacturer: %s<br>Model: %s<br>OS: %s<br>OS Version: %s<br>Firmware: %s<br>Network: %s<br>Test SIM/UICC: %s Test</p>",
				$make,$model,$os,$os_version,$firmware,$network,$has_sim);
				echo $close_div; /* ----------------------------------------------------- */
			} else { // couldn't open devices.txt to write device update; fail
				echo "<p><b>Error: <i>".basename($_SERVER['PHP_SELF'])."</i> encountered ";
				echo "an error trying to write to ".$path."/".$filename.". Device ";
				echo "COULD NOT be updated.</b></p>";						
			}		
		} else { // go back and select a device...
			//echo "<p style=\"font-size:1em;\"><b>Error:</b> <b><i>No device selected. Please use ";
			echo "<p><b>Error:</b> <b><i>No device selected. Please use ";
			echo "the browser's back button to return to the previous page and select the device to ";
			echo "update.</i></b></p>\n";
		}
	} else {
		echo '<h2 style="font-size:1.7em; color:#008000">Deleting Test Device</h2>';
		/*
		printf ("<br>Delete %s\n",$work_on);
		*/
		if (isset($selected_device)){
			// try to open devices.txt
			if ($dfile = fopen($dname,"w")){
				$p_line = fgets($dfile);
				foreach ($the_phones as $a_phone){
					$p = explode(';',$a_phone);
					if ($p[0] != $selected_device){
						/*
						printf ("<br>Keep %s %s\n",$p[0],$selected_device);
						*/
						fwrite ($dfile,$a_phone);
					}
					else {
						echo "<p><i>Success! ".$p[1]." ".$p[2]." deleted!!</i></p>";
					}
				}
				fclose($dfile);
			} else {  // couldn't open devices.txt to write device update; fail
				echo "<p><b>Error: <i>".basename($_SERVER['PHP_SELF'])."</i> encountered ";
				echo "an error trying to write to ".$path."/".$filename.". Device ";
				echo "COULD NOT be deleted.</b></p>";
			}
		} else {
			echo "<p><b>Error:</b> <b><i>No device selected. Please use ";
			echo "the browser's back button to return to the previous page and select the device to ";
			echo "delete.</i></b></p>\n";
		}
	}

	/*<!--
		<p>
		<a href="ft_mdt_get_device.php">
		Return to Create/Edit Devices Page</a><br>
		</p>
		<p>
		<a href="mdt.php">
		Return to Mobile Device Home Page</a><br>
		</p>
	-->*/
	// we can choose not to print these "continuation" links by setting $continue to 0
	if ($continue){
		echo '<br><p>Click <a href="ft_mdt_get_device.php">Create/Edit Mobile Device List</a> '; 
		echo 'to add another device or to review/edit your list of devices.</p>';
		echo '<p>If you are finished creating your list of test devices, click ';
		echo '<a href="ft_mdt_select_setup.php">Record Device Setup</a> to continue to the next ';
		echo 'step and record the setup of your test devices.</p>';
	}
}
?>
		        		        
		</div>
	</div>
    <!-- Footer Starts-->
   <!-- Footer Starts-->
        <?php
     $fn = $_SERVER["SCRIPT_FILENAME"];
          print_footer_contents($fn);
        ?>
    <!-- Footer Ends-->

    <!-- Footer Ends-->
<!--
	<div class="footer"><div class="footerContentsWrapper"><p>The National Institute of Standards and Technology (NIST) is an agency of
 the U.S. Commerce Department.</p><br><br><p class="links"><a href="../Federated_Testing_Home_Page.php">Home</a>
 &nbsp;/&nbsp; <a href="http://localhost/about.php">About</a> &nbsp;/&nbsp;
<a href="mobile-definitions.html">Glossary of Mobile Device Terms</a>
&nbsp;/&nbsp; <a href="http://localhost/FAQ.php">FAQs</a> &nbsp;/&nbsp;
<a href="http://localhost/contacts.php">Contacts</a><br><br>Federated Testing Version:
1.1-3<br>Release Date: --/--/201-</p></div></div>    <!-- Footer Ends-->



</div></body></html>
