<?PHP
#
#   FILE:  ResendAccountActivation.php
#
#   Part of the Collection Workflow Integration System (CWIS)
#   Copyright 2006-2013 Edward Almasy and Internet Scout Research Group
#   http://scout.wisc.edu/cwis/
#

PageTitle("");


# ----- CONFIGURATION  -------------------------------------------------------

# ----- EXPORTED FUNCTIONS ---------------------------------------------------
# (functions intended for use in corresponding HTML file)

# ----- LOCAL FUNCTIONS ------------------------------------------------------
# (functions intended for use only within this file)

# ----- MAIN -----------------------------------------------------------------

# retrieve user name or e-mail address from URL or form
if (isset($_GET["UN"]) && strlen(trim($_GET["UN"])))
{
    $G_UserName = $_GET["UN"];
}
elseif (isset($_POST["F_UserName"])
        && strlen(trim($_POST["F_UserName"])))
{
    $G_UserName = $_POST["F_UserName"];
}
if (isset($_GET["EM"]) && strlen(trim($_GET["EM"])))
{
    $G_EMailAddress = $_GET["EM"];
}
elseif (isset($_POST["F_EMailAddress"])
        && strlen(trim($_POST["F_EMailAddress"])))
{
    $G_EMailAddress = $_POST["F_EMailAddress"];
}

# values intended for use in HTML
$G_EMailAddressUsed = FALSE;
$G_UserFound = FALSE;
$G_EMailSent = FALSE;
$G_AccountAlreadyActivated = FALSE;

# if user name was supplied
$TargetUser = NULL;
if (strlen($G_UserName))
{
    # attempt to find user with specified name
    $G_UserName = User::NormalizeUserName($G_UserName);
    $TargetUser = new SPTUser($G_UserName);
}
# else if e-mail address was supplied
elseif (strlen($G_EMailAddress))
{
    # attempt to find user with specified e-mail address
    $Factory = new CWUserFactory();
    $G_EMailAddress = User::NormalizeEMailAddress($G_EMailAddress);
    $TargetUser = $Factory->GetMatchingUsers($G_EMailAddress, "EMail");

    # set flag indicating that we used e-mail address
    $G_EMailAddressUsed = TRUE;
}

# if requested user was found
if ($TargetUser && ($TargetUser->Status() == U_OKAY))
{
    # set flag indicating that user was found
    $G_UserFound = TRUE;

    # if user account was already activated
    if ($TargetUser->IsActivated())
    {
        # set flag indicating account already activated
        $G_AccountAlreadyActivated = TRUE;
    }
    else
    {
        # send activation e-mail
        $Protocol = isset($_SERVER["HTTPS"]) ? "https://" : "http://";
        $ActivationUrlParameters =
                "?UN=".urlencode($TargetUser->Get("UserName"))
                ."&AC=".$TargetUser->GetActivationCode();
        $ActivationUrl = $Protocol.$_SERVER["SERVER_NAME"]
                .dirname($_SERVER["SCRIPT_NAME"])
                ."/index.php".$ActivationUrlParameters."&P=ActivateAccount";
        $ManualActivationUrl = $Protocol.$_SERVER["SERVER_NAME"]
                .dirname($_SERVER["SCRIPT_NAME"])
                ."/index.php?P=ManuallyActivateAccount";
        $OurSubstitutions = array(
                "X-PORTALNAME-X" => $SysConfig->PortalName(),
                "X-ACTIVATIONURL-X" => $ActivationUrl,
                "X-ACTIVATIONPARAMETERS-X" => $ActivationUrlParameters,
                "X-MANUALACTIVATIONURL-X" => $ManualActivationUrl,
                );
        $MailTemplate = $SysConfig->PasswordMailSubject()."\n"
                .$SysConfig->PasswordMailBody();
        $FromAddress = $SysConfig->PortalName()." <".$SysConfig->AdminEmail().">";
        $TargetUser->SendEMail($MailTemplate, $FromAddress, $OurSubstitutions);
        $G_EMailErrorCode = $TargetUser->Status();
        $G_EMailSent = ($G_EMailErrorCode == U_OKAY) ? TRUE : FALSE;
    }
}
