#include <maincolors.style>
#include <service.style>

<page title="Change your passwd" func="Main Menu">

<?
  $htpasswd = "/etc/apache/.htpasswd";

  function checkuser ($file, $user)
  {
    $f = fopen ($file, "r");
    if ($f) {
      while (!feof ($f)) {
        $line = fgets ($f, 255);
	$foo = explode (":", $line);

	if (! strcmp($user, $foo[0])) {
	  fclose ($f);
	  return 1;
	}
      }
      fclose ($f);
    }
    return 0;
  }

  function errmsg ($message, $help)
  {
    printf ("<p>%s.", $message);
    if ($help) {
      printf ("<p>Talk to Joey to find out what went wrong.", $message);
    }
  }

?>

<? if ($REMOTE_USER) {
     if (checkuser ($htpasswd, $REMOTE_USER)) {
       if (strlen ($passwd) > 0 && strlen ($control) > 0) {
         if (! strcmp ($passwd, $control)) {
?>

<blockquote>

<p><strong>User</strong>: <? echo $REMOTE_USER ?>

<?
  $passwd = escapeshellcmd($passwd);
  $cmdline = sprintf ("htpasswd -b \"%s\" \"%s\" \"%s\"", $htpasswd, $REMOTE_USER, $passwd);
  system ($cmdline);
?>

<p><strong>Password changed</strong>

<p>You'll need to log in again.

</blockquote>

<?
        } else {
          errmsg ("You mistyped the passwd, please repeat.", 0);
        }
      } else {
        errmsg ("Sorry, but empty passwords are not permitted.", 0);
      }
    } else {
      errmsg ("Sorry, but the user is not registrated.", 1);
    }
  } else {
    errmsg ("Sorry, but I cannot change the passwd for an invisible user.", 1);
  }
?>
 
</page>

# Local variables:
# mode: indented-text
# mode: auto-fill
# end:
