include_once("db_connection.php5");
include_once("query_object.php5");
include_once("account_manager.php5");
session_start();
$ACCESS_CODES_FILE = "access_codes.txt";
function get_access_codes($file) {
$access_codes = array();
$in = fopen($file,"r");
while($line = fgets($in)) {
$line = trim($line);
if($line != "") {
$parts = explode(" = ",$line);
$user_type = strtolower($parts[0]);
$access_codes[$user_type] = $parts[1];
}
}
print_r($access_codes);
return $access_codes;
}
$function = trim($_POST["function"]);
if($function == "") {
$function = $_GET["function"];
}
if(!array_key_exists("project",$_SESSION)) {
$project = $_POST["project"];
if($project == "") {
$project == $_GET["project"];
}
if($project == "") {
die("NO PROJECT SPECIFIED");
}
else {
$_SESSION["project"] = $project;
}
}
$ma = new account_manager();
if($function == "create_account") {
$username = $_POST["username"];
$password = $_POST["password"];
$access_code = $_POST["access_code"];
$access_codes = get_access_codes($ACCESS_CODES_FILE);
if(in_array($access_code,$access_codes)) {
$result = $ma->create_account($_POST,$access_codes);
if($result == $ma->USERNAME_TAKEN) {
echo "Username $username is taken.
";
$in = fopen("../nahuatl/html_php5/login.html","r");
while($line = fgets($in)) {
echo $line;
}
fclose($in);
}
else {
if($result == $ma->CREATED_ADMIN) {
$_SESSION["admin"] = $username;
}
$_SESSION["username"] = $username;
header("Location: ../nahuatl/html_php5/mydictionary_search.html");
}
}
else {
echo "I'm sorry, you do not have permission to create an account. Please email ";
echo "Jonathan Amith to get the access code.";
}
}
else if($function == "login") {
$username = $_POST["username"];
$password = $_POST["password"];
$result = $ma->verify_password($username,$password);
if($result == $ma->INVALID_PASSWORD) {
echo "The password you have entered is not valid. Please try again.
";
$in = fopen("../nahuatl/html_php5/login.html","r");
while($line = fgets($in)) {
echo $line;
}
fclose($in);
}
else if ($result == $ma->INVALID_USERNAME) {
echo "No user with username '$username' exists. Please try a different user name.
";
$in = fopen("../nahuatl/html_php5/login.html","r");
while($line = fgets($in)) {
echo $line;
}
fclose($in);
}
else {
if($result == $ma->VALID_ADMIN) {
$_SESSION["admin"] = $username;
}
$_SESSION["username"] = $username;
$ma->update_login_info($username);
header("Location: ../nahuatl/html_php5/mydictionary_search.html");
}
}
else if($function == "logout") {
$username = $_SESSION["username"];
if(array_key_exists("admin",$_SESSION)) {
$admin = $_SESSION["admin"];
if($username != $admin) {
$_SESSION["username"] = $admin;
header("Location: ../nahuatl/html_php5/mydictionary_search.html");
}
else {
unset($_SESSION["admin"]);
$_SESSION["username"] = "";
header("Location: ../nahuatl/html_php5/nahuatl_search.html");
}
}
else {
$_SESSION["username"] = "";
header("Location: ../nahuatl/html_php5/nahuatl_search.html");
}
}
else if($function == "list_accounts") {
if(array_key_exists("admin",$_SESSION)) {
$admin = $_SESSION["admin"];
$_SESSION["username"] = $admin;
$ma->list_accounts("../user/account_manager.xsl");
}
else {
echo "Access Denied!";
}
}
else if($function == "delete_accounts") {
if(array_key_exists("admin",$_SESSION)) {
$admin = $_SESSION["admin"];
$_SESSION["username"] = $admin;
$usernames = $_POST["usernames"];
$ma->delete_accounts($usernames);
$ma->list_accounts("../user/account_manager.xsl");
}
else {
echo "Access Denied!";
}
}
?>