<?php
/*
 * Engsoc PHP Registry
 *
 * Copyright 2001 Philip Steinke and Kevin Everets 
 * Licensed under the GNU General Public License
 *
 */

// Initialize the session, etc.
require_once ('includes/header.inc.php'); 

// Authenticate any pending logins.
if (isset($HTTP_POST_VARS['handle']) && !$webuser->is_authenticated()) {
    
$HTTP_POST_VARS['handle'] = trim(strtolower($HTTP_POST_VARS['handle']));
    if (!
$webuser->login($HTTP_POST_VARS['handle'], 
                         
$HTTP_POST_VARS['password'], $regdb))  { 
        
$failed_login true;
    }
}

// Display HTML headers.
disp_header("Engsoc Registry");

// Check if user is authenticated.
if (!check_auth()) {
    
// user is on main page, not logged in.  A few possibilities are possible
    // here:    - they forgot their password and want a reminder
    //            - they've entered an invalid login/password
    //            - they're looking to register
    //            - they're looking to log in
    // note that all of these present the login prompt.  Only the text and
    // actions before displaying it are different.
    
if (isset($forgot_pwd)) { 
        
// they've requested their password be emailed to them.  Right now
        // this uses the preferred account, but we could either ask them at
        // the time or just send it to both.
        
$msg_password $regdb->getOne("select password from people 
                                        where handle='$handle'"
);
        
$message "You wanted a reminder that for handle ";
        
$message .= "$handle you need to know $msg_password.\n";
        
$pwd_email get_preferred_email($handle);
        if (!empty(
$msg_password)) {
            
mail ($pwd_email"Engsoc Registry Password",
                  
$message"From: $maint_mail");
            print 
"<p>Your password has been emailed to you at $pwd_email.</p>";
        } else {
            print 
"<p>No entries were found for that handle ($handle).</p>\n";
            print 
"<p>Are you sure you <a href=\"add.php\">signed up</a>?</p>\n";
        }
    } elseif (isset(
$failed_login)) {
        
// we've detected an invalid handle/password.  Let them know and
        // present a couple options.
        
print "<p class=\"error\">\n";
        print 
"\tThe handle and password you entered are incorrect.\n</p>\n";
        print 
"<ul>\n\t<li>Forgot your password?\t";
        print 
"\t    <a href=\"$PHP_SELF?forgot_pwd=1&amp;handle=$handle\">";
        print 
"Email it to yourself</a>.\n";
        print 
"\t    If you still can't get in, contact the ";
        print 
mailus("maintainers") . ".\n";
        print 
"\t    Please <em>do not</em> create a new account.\n";
        print 
"\t</li>\n\t<li>New to the registry?  Please use the 'sign up'";
        print 
" button to add yourself to the registry.\n";
        print 
"\t</li>\n</ul>\n";
    } else { 
        
// normal new login ?>
<p>
    Welcome to the <a href="http://engsoc.queensu.ca/">Queen's Engineering
    Society</a> Registry.
</p>

<p>
    The registry is a way for alumni to keep in touch with friends, find fellow
    engineers, and help others contact you.  If this is your first time
    visiting us, please follow the &quot;sign up&quot; link to create an account.  
    If you are already listed, please enter your handle and password below. 
</p>

<?php
    
// end if failed logon detected 
    // Display the login prompt. 
?>
<form class="auth" action="<?php print $PHP_SELF ?>" method="post">
<table class="auth" width="100%">
<tr>
    <td class="auth">Handle: </td>
    <td class="auth"><input class="auth" type="text" name="handle" size="30" /></td>
    <td class="authnew" rowspan="2">Webmail</td>
    <td class="authnew" rowspan="2">New Users</td>
</tr>
<tr>
    <td class="auth">Password: </td>
    <td class="auth"><input class="auth" type="password" name="password" size="30" /></td>
</tr>
<tr>
    <td colspan="2" class="authbut">
    <input class="authbut" type="image" src="images/buttons/login.png"
           alt="login" value="Login" name="login" 
           <?php if ($using_ns4) print 'border="0" '?>/></td>
    <td class="authbut"><a href="squirrelmail/">SquirrelMail</a></td>
    <td class="authbut">
    <a href="add.php"><img src="images/buttons/signup.png" alt="sign up" /></a></td>
</tr>
<!-- <tr>
    <td></td>
    <td></td>
    <td class="authbut"><a href="imp/">IMP</a></td>
    <td></td>
</tr>
<tr>
    <td></td>
    <td></td>
    <td class="authbut"><a href="aeromail/">AeroMail</a></td>
    <td></td>
</tr> -->
</table></form>
<table class="publink">
<tr>
    <td id="publeft">Looking for the pub?<br />Try here:</td>
    <td id="pubright"><a href="http://engsoc.queensu.ca/clark/">
    <img class="publink" src="images/smallclark.png" alt="CHP" /></a></td>
</tr>
</table><?php
    
include ('footer.inc.php');
    exit();
}

// User is authenticated, begin main page code.
// Display navigation bar.
disp_navbar();

// if user is an administrator, check to see if they have anyone pending and
// provide a link to the admin page.
$admin_class $webuser->is_admin();
if (
$admin_class) {
    
// check for pending users
    
if ($admin_class == 'master') {
        
$pending $regdb->query("SELECT handle FROM people 
                                  WHERE newuser='true'"
);
    } else {
        
$pending $regdb->query("SELECT handle FROM people 
                                  WHERE newuser='true' 
                                  AND class='$admin_class'"
);
    }
    
// count pending users
    
while ($row $pending->fetchRow()) {
        if (
$row[0]) $numPending++;
    }
    
$pending->free();
    unset(
$pending);

    
// display pending message
    
if ($numPending) {
        print 
"<p>$numPending user";
        if (
$numPending 1) print "s";
        print 
" pending.  Please check the <a href=\"useradmin.php\">";
        print 
"user administration</a> page.</p>\n";
    }
}

// Display a welcome message and any important announcements. ?>
<p>
    Welcome to the Engsoc Registry, <?php print $webuser->first_name ?>.<br />
</p>
<p>
    <b>New!</b> - <a href="email_help.php">Help on using your registry
    email</a>.
</p>
<p>
    Recent and upcoming birthdays:
</p>
<ul>
<?php
// Some PostgreSQL stuff has been used here to do a better birthday
// calculation.  This WHERE clause picks the birthdays within a month of
// four weeks ago <grin>.. so that gives a month's worth of birthdays,
// starting at (usually) a couple days in the past and going to about 4
// weeks in the future.
$bdayquery "SELECT first_name, last_name, class, birthdate,
              date_part('month', birthdate) as birthmonth,
              date_part('day', birthdate) as birthday,
              to_char(date_part('month', birthdate), '00') ||
              to_char(date_part('day', birthdate), '00') as birthsort
              FROM people 
              WHERE date_part('month', age(birthdate-28))=0 AND "
;
#              class='$webuser->class'
$bdayquery .= build_generation_query($webuser->class);
$bdayquery .= " ORDER BY birthsort, class, last_name, first_name";
$bdayresult $regdb->query($bdayquery);
// Step through the upcoming birthdays.
while ($row $bdayresult->fetchRow(DB_FETCHMODE_ASSOC)) {
    
// Not using birthmonth and birthday from DB as they have issues around
    // daylight savings time changes (april & oct).
    
list($byear$bmonth$bday) = explode('-'$row['birthdate']);
    
// Get a UNIX timestamp for their upcoming birthday.
    
$current_bday mktime(date("G"), date("i"), date("s"), 

#                           $row['birthmonth'], $row['birthday'], date("Y"));
                           
$bmonth$bdaydate("Y"));
    
// The 259200 is 3 days in seconds.  This keeps the weekdays in the
    // current year for people whose birthdays were only 3 days ago, as
    // opposed to those in the upcoming year.
    
if ($current_bday < (time() - 259200)) {
        
// Their upcoming birthday is in the next year, change the timestamp
        // to reflect that so we get the right day of the week.
        
$current_bday mktime(000,
                               
$bmonth$bdaydate("Y")+1);
    }
    print 
"\t<li>$row[first_name] $row[last_name] (Sci'$row[class]) - ";
     print 
date("l F jS"$current_bday) . "</li>\n";
}
$bdayresult->free();
unset(
$bdayresult);
if (!isset(
$current_bday)) {
    print 
"\t<li>No birthdays this month</li>\n";
}
print 
"</ul>\n";

include (
'footer.inc.php'); 
// End of index.php ?>