Location: PHPKode > projects > SCOP Heartbeat Administration interface > scop-2.0/html/scop/config/password.php
<?

// function getgroup($username) { // Open up the group file and check it to find the group a user is in:


include ("../inc/header.inc");
include ("../inc/functions.inc");

$trans="98";
$a98 = "${$l.$trans}";
$trans="99";
$a99 = "${$l.$trans}";
$trans="100";
$a100 = "${$l.$trans}";
$trans="101";
$a101 = "${$l.$trans}";
$trans="102";
$a102 = "${$l.$trans}";


$pass = $_POST['pass'];
if ($pass == "") {$pass="blank";}

if ($action=="remove") {
	$username = $_GET['removeusername'];
	$uid = $_GET['uid'];
} else {
	$username = $_POST['username'];
	$uid = $_POST['uid'];
}
$group = $_POST['group'];
$type = $_POST['type'];

if ($uid=="") { $uid= $_GET['uid']; }




if ($action==edituser) { // Do it straight away then play with the result later to ensure correct MD5 etc.
		exec ("sudo htpasswd -b /etc/scop/passwords $username $pass > /dev/null 2>&1 &");
		SCOP_log("Config : user $username settings changed.");
}



//echo "<table align=\"center\" width=\"60%\"><tr><td>";


# Carry out actions if required




# First get a list of users from /etc/scop/passwords

exec ("sudo chmod 777 /etc/scop/passwords");
if ($arrtext = file("/etc/scop/passwords")) {
} else {
	draw_no_access("/etc/scop/passwords");
}

$trans="4";
$a4 = "${$l.$trans}";
$trans="18a";
$a18a = "${$l.$trans}";
//echo "<h2>$a18a</h2></td></tr>";
//echo "</table>";
?>
<div id="contentArea">
<h1><a href="/scop/index.php?menu=maint&l=<?=$l?>"><?=$a4?></a> &gt; <?=$a18a?></h1>
<div class="border">			
<?

//echo "<table align=\"center\" width=\"60%\"><tr><td>";
?><table border="0" class="datagrid" align="center"><?
?><tr><td colspan="3" class="sep2"></td></tr><?

$max=count($arrtext);
for ( $i=0; $i<$max; $i++) {

	$tok = strtok($arrtext[$i],":\n\t");

	$user1[$i]=$tok;

	$tok = strtok(":\n\t");
	$password[$i]=$tok;
	$trans="19";
	$a19 = "${$l.$trans}";	
	$trans="20";
	$a20 = "${$l.$trans}";	
	$trans="95";
	$a95 = "${$l.$trans}";
	if ($action=="remove" && $uid==$i) {
		#Dont show if we are going to remove it..
	} else {
		echo "<tr><th>$user1[$i]</th><td>[ <a href=\"password.php?action=edit&uid=$i&t=$t\" class=\"action\">$a19</a> ]</td>";
		if ($user1[$i]!="SCOP") {
			echo "<td>[ <a href=\"password.php?action=remove&uid=$i&removeusername=$user1[$i]&t=$t\" class=\"action\" onclick=\"return areyousure('$a95 ($user1[$i])')\">$a20</a> ]</td></tr>";
		}
		?><tr><td colspan="3" class="sep1"></td></tr><?
	}
}

echo "</td></tr></table></br>";
?>
</div>
<div class="bottom"><span>-----</span></div>
</div>
<?

if ($action=="edituser" or $action=="remove") {
	#exec ("sudo htpasswd -b /etc/scop/passwords $username $pass");
	$trans="96";
	$a96 = "${$l.$trans}";
	$trans="97";
	$a97 = "${$l.$trans}";
	exec ("sudo chmod 777 /etc/scop/groups");
	if ($arrtext = file("/etc/scop/groups")) {

	} else {
		draw_no_access("/etc/scop/groups");
	}

	if ($type=="new") {
		echo "<p>$a96</p>";
	}
	if ($action!="remove") {
		echo "<p>$a97</p>";
		if ($pass=="blank") {
			draw_error ("WARNING: NULL Password for $username changed to \"blank\" .");
		}
	}



	if ($username != "SCOP") {
		# find the user in the string if you find it strip it out because we'll add it from scratch
		$username = " " . $username; // Make sure to remove the space as well
		$line0config = str_replace($username,$blank,$arrtext[0]);
		$line0config = str_replace("\n",$blank,$line0config);
		$line1maint = str_replace($username,$blank,$arrtext[1]);
		$line1maint = str_replace("\n",$blank,$line1maint);
		$line2report = str_replace($username,$blank,$arrtext[2]);
		$line2report = str_replace("\n",$blank,$line2report);

		# Copy the whole string and add the user to the end of it
		if ($action=="remove") { // Don't put the user back in if your removing them !
			$username = "";
		}
		if ($group=="config") {
			$line0config = $line0config . $username;
		}
		if ($group=="maint") {
			$line1maint = $line1maint . $username;
		}
		if ($group=="report") {
			$line2report = $line2report . $username;
		}

		# Re-write the file !

			exec ("sudo chmod 777 /etc/scop/groups");
			# open a new file
			if ($file=fopen("/etc/scop/groups","w")) {

				fputs($file,"$line0config\n");
				fputs($file,"$line1maint\n");
				fputs($file,"$line2report\n");
				fclose($file);

			} else {echo "<h3>ERROR : Can't open password file ?";}
			#Copy settings to slave
			$scpgroups=true;

	}

}

if ($action==edit) {

// Setup the java script first
?>
<script language="javascript" type="text/javascript">
<!--
function checkformedituser()
{ // Check the new user fields are correct
	if (document.edituser.username.value =="" )
	{
		alert  ("Please fill in the the username.");
		return false

	}
	if (document.edituser.pass.value =="" )
	{
		alert  ("Please fill in the the password.");
		return false

	}
	return true
}
//-->
</script>


<?

?>
<br />
<div id="contentArea">
<h1><a href="/scop/index.php?menu=maint&l=<?=$l?>"><?=$a4?></a> &gt; <?=$a98?></h1>
<div class="border">			
<?
//echo "<h2>$a98</h2>";
echo "<FORM method=\"post\" action=\"password.php?action=edituser&t=$t\" name=\"edituser\">";
?><table border="0" cellspacing="0" align="center" class="form"><td colspan="3" class="sep2"></td><?
echo "<tr><th>$a100 </th><td> <INPUT type=\"text\" size=20 name=\"username\" value=\"$user1[$uid]\"></td></tr>";
?><td colspan="3" class="sep1"></td><?
echo "<tr class=\"altLine\"><th>$a101 </th><td> <INPUT type=\"password\" size=20 name=\"pass\" ></td></tr>";
?><td colspan="3" class="sep1"></td><?
if ($user1[$uid] != "SCOP") {
	$group  = getgroup($user1[$uid]);
	echo "<tr><th>$a102 </th><td> ";
	echo "<select name=\"group\">";
	echo "<OPTION VALUE=\"report\"  ";
	if ($group=="report") echo  " selected=\"true\" ";
	echo ">report";
	echo "<OPTION VALUE=\"maint\" " ;
	if ($group=="maint") echo  " selected=\"true\" ";
	echo ">maint";
	echo "<OPTION VALUE=\"config\" ";
	if ($group=="config") echo  " selected=\"true\" ";
	echo ">config";
	echo "</select></td></tr>";
?><td colspan="3" class="sep1"></td><?
}


echo "<input type=\"hidden\" name=\"uid\" value=\"$uid\">";
echo "</td></tr></table></br>";
echo "<br /><center><INPUT type=\"submit\" value=\"$a98\" name=\"go\" onclick=\"return checkformedituser()\"></center>";
echo "</form>";
?>
</div>
<div class="bottom"><span>-----</span></div>
</div>
<?
}

// Setup the java script first
?>
<script language="javascript" type="text/javascript">
<!--
function checkformnewuser()
{ // Check the new user fields are correct
	if (document.newuser.pass.value !=="" &&  document.newuser.username.value !=="" )
	{
		return true
	}
	else
	{
		alert  ("Please enter a username AND password.");
		return false
	}
}
//-->
</script>


<?

?>
<br />
<div id="contentArea">
<h1><a href="/scop/index.php?menu=maint&l=<?=$l?>"><?=$a4?></a> &gt; <?=$a99?></h1>
<div class="border">			
<?
//echo "<h2>$a99</h2></br>";
echo "<FORM method=\"post\" action=\"password.php?action=edituser&t=$t\" name=\"newuser\">";
?><table border="0" cellspacing="0" align="center" class="form"><td colspan="3" class="sep2"></td><?
echo "<tr><th>$a100 </th><td> <INPUT type=\"text\" size=20 name=\"username\" ></td></tr>";
?><td colspan="3" class="sep1"></td><?
echo "<tr class=\"altLine\"><th>$a101 </th><td> <INPUT type=\"password\" size=20 name=\"pass\" ></td></tr>";
?><td colspan="3" class="sep1"></td><?
echo "<INPUT type=\"hidden\" name=\"group\" value=\"report\">";
echo "<INPUT type=\"hidden\" name=\"type\" value=\"new\">";
echo "</td></tr></table></br>";
?><td colspan="3" class="sep1"></td><?
echo "<br /><center><INPUT type=\"submit\" value=\"$a99\" name=\"go\" onclick=\"return checkformnewuser()\"></center>";
echo "</form>";

?>
</div>
<div class="bottom"><span>-----</span></div>
</div>
<?
# Dow we need to remove a user ?
if ($action=="remove") {
			$WRITEFILE=TRUE;

			SCOP_log("Config : User $username removed.");
			// Uggh I hate splicing arrays what a mess...
			if ($uid==count($user1)-1) {
				array_splice ($user1, $uid) ;
			}
			else {
				$a = count($user1)-$uid-1;
				array_splice ($user1, $uid, -$a) ;
			}
			// Just to be tidy we'll remove the user from the groups file in a min
		}

# Now write the password file

if ($WRITEFILE==TRUE) {
		exec ("sudo chmod 777 /etc/scop/passwords");
			# open a new file
			if ($file=fopen("/etc/scop/passwords","w")) {



				for ( $i=0; $i<count($user1); $i++) {
					fputs($file,"$user1[$i]:$password[$i]\n");


				}

				fclose($file);
			} else {
				draw_error("ERROR: Can't open password file !!!");
			}
			#Copy settings to slave
			flush();
			exec ("sudo scp /etc/scop/passwords hide@address.com:/etc/scop/passwords > /dev/null 2>&1 &");

}



if ($scpgroups==true) { // Do this at the end to avoid a pause
	#Copy settings to slave
	flush();
	exec ("sudo scp /etc/scop/groups hide@address.com:/etc/scop/groups > /dev/null 2>&1 &");
}

function getgroup($username) {
// Open up the group file and check it to find the group a user is in:
exec ("sudo chmod 777 /etc/scop/groups");
	if ($arrtext = file("/etc/scop/groups")) {

	} else {
		draw_no_access("/etc/scop/groups");
	}

$group="config";
if (strstr($arrtext[0],$username)) $group="config";
if (strstr($arrtext[1],$username)) $group="maint";
if (strstr($arrtext[2],$username)) $group="report";

return $group;
}

include ("../inc/footer.inc");
?>
Return current item: SCOP Heartbeat Administration interface