<html>
<body onLoad="document.theform.firstname.focus();">

<?php

if ($_POST) { // only do stuff if we got stuff to do.

    
extract($_POST);

    
// duh.
    
if ($secret !== "somedumbpassword") {
        echo 
"<p>ERROR: Secret is incorrect! Exiting.";
        exit;
    }

    
//echo "<p>DEBUG got $username for a username. Got $firstname for a first name. Got $lastname for a last name. Got $group for a group. Got $ou for an org. Got $secret for the secret password";

    // Here begins a bunch of rules. Tune to your domain's needs

    
if (strpos($username".") ) { // dont put a dot in the primary username.
        
echo "<P>ERROR: usernames must NOT contain a dot anymore since August 15, 2013. Exiting.";
        exit;
    }    

    if (!
strpos($nickname".") ) { // nickname SHOULD have a dot. 
                
echo "<P>ERROR: nicknames MUST contain a dot. Exiting.";
                exit;
    }

    if (
strpos($username"-") ) { // hyphen in the username? no.
        
echo "<P>ERROR: usernames must NOT contain a hyphen. Exiting.";
        exit;
    }    

        if (
strpos($nickname"-") ) { // hyphen in the nickname? no.
                
echo "<P>ERROR: nicknames must NOT contain a hyphen. Exiting.";
                exit;
        }       

    if (
strpos($username" ") || strpos($username"'") || strpos($username"\n") ) { // username has an illegal char? no.
        
echo "<P>ERROR: usernames MUST NOT contain a space or an apostrophe. Exiting.";
        exit;
    }    

        if (
strpos($nickname" ") || strpos($nickname"'") || strpos($nickname"\n") ) { // nickname has an illegal char? no.
                
echo "<P>ERROR: nicknames MUST NOT contain a space or an apostrophe. Exiting.";
                exit;
        } 

    if (
$firstname == "" || $lastname == "" ) { // no blank names
        
echo "<p>ERROR: Firstname and Lastname must NOT be blank. Exiting.";
        exit;
    }

    if (
strlen($password) < ) { // password too short.
        
echo "<p>ERROR: Password must be at least 8 characters long. Exiting.";
        exit;
    }

    if (
$group == "[CHOOSE]") { // pick a group, dammit
        
echo "<p>ERROR: You MUST choose a group. Exiting.";
        exit;    
    }

    if (
$ou == "[CHOOSE]") { // pick an org, dammit
        
echo "<p>ERROR: You MUST choose an organization. Exiting.";
        exit;
    }

    
// Now check to see if there is already a user with that name.
    
$gam "/usr/bin/python /usr/local/bin/gam/gam.py";
    
$command "$gam info user $username";
    
exec($command,$output,$returned);
    
//echo "<p>DEBUG output is $output and returned is $returned";
    
if ($returned == 0) {
        echo 
"<p>ERROR: Already a user with that name. Exiting.";
                exit;
    }
    else {
        
// Returning anything other than 0 = good to go; do nothing, just proceed.    
    
}

// Okay, dumbass checks passed. 

// K all tests passed, do something?????????

    // Email me whenever this tool is used.
    
$to "youpoorCJ@domain.org";
    
$subject "Google account created";
    
$body "Username: $username Group: $group OU: $ou";
    
$headers 'From: google-acct-maker@domain.org' "\r\n" .
    
'Reply-To: networkadmin@domain.us' "\r\n" .
    
'X-Mailer: PHP/' phpversion();
    
mail($to$subject$body$headers);

    
// Un-escape any apostrophes in the human-readable name.
    
$firstname stripslashes($firstname);
    
$lastname stripslashes($lastname);

    echo 
"<p>Creating user <b>$username</b>...";

    
$command "$gam create user $username firstname \"$firstname\" lastname \"$lastname\" password $password agreedtoterms on org $ou";
//    echo "<br>DEBUG command is $command";
    
passthru($command);

    echo 
"<p>Adding $username to group <b>$group</b>...";
    
$emailaddress $username "@domain.org";
    
$command "$gam update group $group add member $username";
//    echo "<br>DEBUG command is $command";
    
passthru($command);

    
// This command yields its own output, so we don't need an echo statement.
    
$command "$gam create alias $nickname user $username";
//    echo "<br>DEBUG command is $command";
    
passthru($command);

    echo 
"<p>Confirmed; added <b>$firstname $lastname</b> with username <b>$username</b> and email address <b>$emailaddress</b> and nickname <b>$nickname</b> and password <b>$password</b> to group <b>$group</b> and organization <b>$ou</b>. Thank you please drive through.";

}

else { 
// if no POST, show the form.

?>

<form name="theform" id="theform" action="account-maker.php" method="post">

<?php

// Longish list of non-vulgar English nouns
$nouns file('/path/to/a/list/of/nouns.txt');

foreach (
$nouns as $noun) {
        if (
strlen(trim($noun)) > 4) {
                
// we don't want these long nouns; do nothing. Yes I could've just stripped 'em out of the list beforehand but I didn't.
        
}

        else {
                
// if it's long enough, add it to the new array.
                
$shortnouns[] = trim($noun);
        }
}
shuffle($shortnouns);

$randomnumber rand(1000,9999);  // Get a "random" lololol number between 1000 and 9999.
$newpassword trim($shortnouns[0]) . $randomnumber;  // Grab the first element off 
// the array, clean it, and append it to the random number.

?>

<p>Tab between fields and it will try to autofill the right thing. You might need to correct it for spaces, hyphens, apostrophes, etc. although it will try to do this for you.

<p>First name: <input name="firstname" id="firstname" size="25"> Don't leave blank, can have spaces or any punctuation
<br>Last name: <input name="lastname" id="lastname" size="25"> Don't leave blank, can have spaces or any punctuation

<p>Username: <input onfocus="this.value=document.getElementById('firstname').value.toLowerCase() + document.getElementById('lastname').value.toLowerCase()" name="username" id="username" size="50"> Should be of the format "firstnamelastname" NO APOSTROPHES, NO HYPHENS! 

<p>Nickname: <input onfocus="this.value=document.getElementById('firstname').value.toLowerCase() + '.' + document.getElementById('lastname').value.toLowerCase()" name="nickname" id="nickname" size="50"> Should be of the format "firstname.lastname" NO APOSTROPHES, NO HYPHENS!

<p>Password: <input id="password" name="password" value="<?php echo $newpassword;?>"> Must be at least 8 characters. This page auto-generates a new one every time it loads.

<p>PRIMARY Group: <select name="group">
<option selected>[CHOOSE]</option>
<option>staff</option>
<option>students</option>
<option>pets</option>
<option>roadies</option>

</select> You MUST pick a primary group from the list in order to add the user. You can then add the user to additional groups in the main admin interface. 

<p>Organization: <select name="ou">
<option selected>[CHOOSE]</option>
<option>All-Staff</option>
<option>Students</option>
<option>Students/ClassOf2015</option>
<option>Students/ClassOf2016</option>
<option>Students/ClassOf2017</option>
<option>Students/ClassOf2018</option>
<option>Not-Real-People</option>
<option>Not-Really-Employed</option>
</select> If this new account doesn't fit into any of those suborgs, email me. 

<p>Secret key: <input type="password" name="secret"> <input type="submit">
</form>

<?php

// end "if no POST"

?>

</body>
</html>