<?php 
require("/home/httpd/lib/defaults.inc");
require(
"$LibDir/rsvp/defaults.rsvp.inc");
#Warning="";
$User=array();
$Invite=array();
$Event=array();

if(
$code)
{
    list(
$Action,$e_eid,$u_uid,$u_password)=split("\|",$code,4);
    
$code="";
}

#Do some standard lookup stuff most Actions will need
if(!$Action)
{
    
#No lookups
}
elseif(!
$u_uid || !$u_password)
{
    
$Warning .= "You must specify a user ID and password.  ";
    
$BadInvite=1;
}
elseif(!(
$User=FindUser($u_uid,$u_password)) || !$User[uid])
{
    
$Warning .= "User $u_uid not found or incorrect password!  ";
    
$BadInvite=1;
}
elseif(!
$e_eid)
{
    
$Warning .= "You must choose an event.  ";
    
$BadInvite=1;
}
elseif(!(
$Event=FindEvent($e_eid)) || !$Event[eid])
{
    
$Warning .= "Event '$e_eid' not found!  ";
}
else
{
    
#We have a valid user and event, now let's see if they're invited
    
FindInvite($e_eid,$u_uid);
    if(
$Invite[uid])
    {
    
$FoundInvite=1;
    }
    else
    {
    
#Not invited.  Is this an open event?
    
if($Event[openinvite]=="no")
    {
        
$Warning .= "Sorry, this is a closed event!  ";
        
$BadInvite=1;
    }
    else
    {
        
$Invite[eid]=$Event[eid];
        
$Invite[uid]=$User[uid];
        
$Invite[status]="invited";
        
$Invite[maxguests]=$Event[maxguests];
        
$NewInvite=1;
    }
    }
    
$Owner=IsOwner($e_eid,$u_uid);
}


#Branch based on $BadInvite and $Action
if($BadInvite)
{
    
#Don't do any actions if BadInvite
}
elseif(
$Action=="Reply")
{
    require(
"$LLibDir/top.inc");



if(
$Event[eid]=='pegahol2000')
{
    print(
'
<script language="Javascript">

<!--
// please keep these lines on when you copy the source
// made by: Nicolas - http://www.javascript-page.com

alert("Note: The holiday party registration is now being handled by another system.\nYou will be brought there when you click on the OK button. ('
.$Action.')");

location = "http://trinity/cgi-bin/admit.cgi?pname='
.$User[uid].'"
exit;
//-->
</script>
'
);
}






    if(
file_exists("$EventDir/$Event[eid]/reply.phtml"))
    {
    require(
"$EventDir/$Event[eid]/reply.phtml");
    }
    else
    {
    require(
"$EventDir/default/reply.phtml");
    }
}
elseif(
$Action=="Submit reply")
{
    if(
$FoundInvite)
    {
    if(!
UpdateRow("rsvpinvite",$InviteFields,"i_","eid=\"$e_eid\" and uid=\"$u_uid\""))
    {
        
$Warning .= "Reply could not be saved! Please contact david@thekramers.net.";
    }
    else
    {
        
$Warning .= "Your reply has been updated.  ";
    }
    }
    else
    {
    if(!
InsertRow("rsvpinvite",$InviteFields,"i_"))
    {
        
$Warning .= "Reply could not be saved! Please contact david@thekramers.net.  ";
    }
    else
    {
        
$Warning .= "Your reply has been sent.  ";
    }
    }
    if(
$i_status=='no')
    {
    
$Warning .= 'So sorry we won\'t see you there.  ';
    }
    elseif(
$i_status=='yes')
    {
    
$Warning .= 'See you there!  ';
    }
}
elseif(
$Action=="ShowInvite")
{
    if(
$Owner!=$u_uid)
    {
    
$Warning .= "You must be admin to do this!  ";
    
$BadInvite=1;
    }
    elseif(!(
$User=FindUser($guest_uid)) || !$User[uid])
    {
    
$Warning .= "User ($guest_uid) ($user[uid]) not found!  ";
    
$BadInvite=1;
    }
    elseif(!
FindInvite($e_eid,$guest_uid) || !$Invite[uid])
    {
    
$Warning .= "Invite for $guest_uid to $e_eid not found!  ";
    
$BadInvite=1;
    }
    else
    {
    require(
"$LLibDir/top.inc");
    
$Mode="reply";
    if(
file_exists("$EventDir/$Event[eid]/$Mode.phtml"))
    {
        require(
"$EventDir/$Event[eid]/$Mode.phtml");
    }
    else
    {
        require(
"$EventDir/default/$Mode.phtml");
    }
    }
}
elseif(
$Action=='Add event')
{
}
elseif(
$Action=='Administer event')
{
}
elseif(
$Action=="Invite summary")
{
    if(
$Owner!=$u_uid && $Event[openguests] != 'yes')
    {
    
$Warning .= "You must be admin to do this!  ";
    
$BadInvite=1;
    }
    elseif(
file_exists("$EventDir/$Event[eid]/adminsummary.phtml"))
    {
    require(
"$LLibDir/top.inc");
    require(
"$EventDir/$Event[eid]/adminsummary.phtml");
    }
    else
    {
    require(
"$LLibDir/top.inc");
    require(
"$EventDir/default/adminsummary.phtml");
    }
}
elseif(
$Action=="Special report")
{
    if(
$Owner!=$u_uid && $Event[openguests] != 'yes')
    {
    
$Warning .= "You must be admin to do this!  ";
    
$BadInvite=1;
    }
    elseif(
file_exists("$EventDir/$Event[eid]/$Report.phtml"))
    {
    require(
"$LLibDir/top.inc");
    require(
"$EventDir/$Event[eid]/$Report.phtml");
    }
    elseif(
file_exists("$EventDir/default/$Report.phtml"))
    {
    require(
"$LLibDir/top.inc");
    require(
"$EventDir/default/$Report.phtml");
    }
    else
    {
    
$Warning .= "Report $Report not found!";
    require(
"$LLibDir/top.inc");
    }
}
elseif(
$Action=='Email invites')
{
    if(
$Owner!=$u_uid)
    {
    
$Warning .= "You must be admin to do this!  ";
    }
    else
    {
    require(
"$LibDir/email.inc");
    
$From="RSVP by David Kramer <david@thekramers.net>";
    
$Subject="Your invite to \"$Event[descr]\"";
    
$Bodyjoin (''file ("$EventDir/$Event[eid]/invite.email"));

    
#If no user specified, email everyone.
    
$Sql="select uid from rsvpinvite where eid='$Event[eid]'";
    if(
$guest_uid)
    {
        
$Sql .= " and uid='$guest_uid'";
    }
    else
    {
        
$Sql .=  " and status='uninvited'";
    }
    
$QueryH=mysql_query($Sql,$DbH)
        or die(
"Invalid query '$Sql':".mysql_error());
    while(
$EventUid=mysql_fetch_array($QueryH))
    {
        if(!(
$User=FindUser($EventUid[uid])) || !$User[uid])
        {
        
$Warning .= "User $EventUid[uid] not found!  ";
        }
        elseif(!(
FindInvite($e_eid,$User[uid])) || !$Invite[uid])
        {
        
$Warning .= "Invite for $EventUid[uid] to $e_eid not found!  ";
        
        }
        else
        {
        
EmailForm($From,$User[email],$Subject,"",$Body,array_merge($Event,$User,$Invite));
        
$Warning .= "$User[uid] sent  ";
        
#Set status to 'invited' here
        
$Sql="update rsvpinvite set status='invited' where eid='$Event[eid]' and uid='$User[uid]' and status='uninvited'";
        
mysql_query($Sql,$DbH)
            or die(
"Invalid query '$Sql':".mysql_error());
        }
        
#If sending to a single user, we're done so clear this so the loop exits.
        
if($guest_uid)
        {
        
$guest_uid="";
        }
    }
    }
    
#We want to redisplay the main page, so set this even if it isn't accurate.
    
$BadInvite=1;
}

if(!
$Action || $BadInvite)
{
    require(
"$LLibDir/top.inc");
    require(
"mainform.phtml");
}

if(!
$TopPrinted)
{
    require(
"$LLibDir/top.inc");
}
require(
"$LLibDir/bottom.inc");
    
?>