#!/usr/local/bin/perl 

# Send Password script (If user forgot his password)
# Author: Manuel Kanahuati
# Email: mkr@mexicovirtual.net
# Web: http://www.mexicovirtual.net
# Release: 2.0
# Revision: 0
#                                                                                 #

my $INCLUDE_HEADER=($ENV{'DOCUMENT_ROOT'}||"/home/httpd/html");

require "/usr/local/apache/lokad/dbconnect.inc";
use CGI;
$q=new  CGI;
$alias =$q->param('alias');
$email =$q->param('email');
$ok = 0;
if ($q->param('action') eq "dosend") {
  $ok = &dosend;
	if ($ok == 1) {
	  &displaymsg;
	}
	else {
	  &showpassform;
	}
}
else {
  &showpassform;
} 
exit(0);


sub showpassform {

  print "Content-type: text/html\n\n";

  print qq ~
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link href="/lib/members.css" rel="stylesheet" type="text/css"/>
	  <title>IAAPA - Members Area (forgot password)</title>
</head>
<body>
  <div align="center">
	  <div id="main_area">
		  <div id="header_area">
			  <div id="logo_area">
				  <a href="http://www.axisandallies.net"><img src="/images/members/logo_iaapa.gif" alt="IAAPA" width="120" height="50" /></a>
				</div>
				<div id="site_title">
				  <img src="/images/members/logo_site.gif" alt="International Axis And Allies Players Association" width="180" height="50" />
				</div>
				<div id="top_menu">
				  Top Menu
				</div> <!-- top menu bar end -->
      </div><!-- header_area end -->
			<div class="clr"></div>
			  <table width="778" style="color:#fff; background-color: #869eb8; border-bottom: solid 1px #eaeaea;" cellpadding="10" cellspacing="1">
				  <tr>
            <td align="center" valing="middle">
						  <b>&nbsp;</b>
						</td>
          </tr>
        </table>
			<div id="content_area">
			  <div id="content">
				  <div align="center" style="width:350px;">
  				<br />
					<table cellspacing="5"> 
					  <tr>
						  <td valign="middle"><img src="/images/members/iconemail.gif" alt="Login" /></td>
							<td>&nbsp;&nbsp;</td>
							<td align="left" valign="middle">
							  <h3>Send Password by Email</h3>
							  Only active IAAPA members have access to the Members area.
					      <a href="/index.php?option=com_content&task=blogsection&id=7&Itemid=46">Click here</a>
				      	for more information on how to join IAAPA or visit our
								<a href="/index.php">web site</a>.
							</td>
						</tr>
					</table>
					<br />
					<div align="left">
  				&nbsp;&nbsp;&nbsp;&nbsp;If you don't know your user name (alias) or password, you can enter your alias or your 
					registered email address below. &nbsp;&nbsp;In a couple minutes you will be emailed your alias and
					password if you have one in the IAAPA database.
					</div>
					<br />
					<div style="border: solid 1px #c8c8c8;">
          <br />
					<b>Remember the information will be emailed to the address you registered with IAAPA.</b><br />
					If you changed email address, please contact <a href="mailto:help@axisandallies.net">help\@axisandallies.net</a>.
					<br /><br />
					<b>$log_msg</b>
          <form method="POST" action="/cgi-local/members/sendpass.cgi">
					  <input type="hidden" name="action" value="dosend">
						<table>
              <tr>
	              <td align="right">Alias:</td>
								<td align="left"><input class="inputbox" type="text" name="alias" value="$qalias"></td>
							</tr><tr> 
              <tr>
	              <td align="right">Email:</td>
								<td align="left"><input class="inputbox" type="text" name="email" value="$qemail"></td>
							</tr><tr> 
            </table>
						<br />
				    <input class="button" type="submit" value=' Email Password '>&nbsp;&nbsp;
				  </form>
					<br />
					</div>
					</div>
	        <br /><br /><br /><br />					
				</div> <!-- content end -->
				<div id="right_area">
				  <br />
			  </div> <!-- right_area end -->
			</div> <!-- content_area end -->
			<div class="clr"></div>
			<div id="footer_area">
			  &copy; Copyright 2006,Created by the International Axis & Allies Players Association.<br />
				This site is intended to promote the use of Axis and Allies Board Game from Milton Bradley.<br />
				&copy; Axis and Allies is trademark owned by Hasbro Inc, All rights reserved.
			</div>
	  </div><!-- main_area end -->
  </div> <!-- center end -->
</body>
</html>		
  ~;
}

sub dosend {
  my $result = 0;
	
  if (($alias ne "") && ($email ne "")) {
    ($qemail = $email) =~ tr /A-Z/a-z/; 
    $condit = "alias = '$qalias' or emailaddress = '$qemail'";	
	}
	else {
    if ($alias ne "") { 
	    $condit = "alias = '$qalias'";
    }
    elsif ($email ne "") {  
      ($qemail = $email) =~ tr /A-Z/a-z/; 
      $condit = "emailaddress = '$email'";
    }
		else {
		  $log_msg = "Please provide the required information !!!";
		  &showpassform;
		  exit(0);
		}
	}
  $query = "select alias, playercode, emailaddress from players where $condit";
  die "Try to create cursor on players stopped\n" unless ($cursor = $dbh->prepare($query));
  $cursor->execute;
  if  (@row=$cursor->fetchrow) {
    if ( ((@row[0] eq $qalias)||(@row[2] eq $qemail)) && length(@row[2]) != 0 ) {
      $result = 1;
		  if ($email eq "") { $qemail = $row[2]; }
			$dx=localtime(time());
	    @d2=split( " ", $dx);
	    $dagsx=@d2[0].", ".@d2[2]." ".@d2[1]." ".@d2[4]." ".@d2[3]." -0500";
      open(SMAIL,"|/usr/sbin/sendmail -t @row[2]") or die "Could not start sendmail\n";
		  printf(SMAIL "From: help\@axisandallies.net\n");
		  printf(SMAIL "To: %s\n",@row[2]);
		  printf(SMAIL "Subject: Information on your IAAPA Membership\n",$gn);
		  printf(SMAIL "Date: %s\n",$dagsx);
		  printf(SMAIL "MIME-Version: 1.0\n");
		  printf(SMAIL "Content-Type: text/plain; charset=iso-8859-1\n");
		  printf(SMAIL "Content-Transfer-Encoding: 8bit\n\n\n");
		  printf(SMAIL "Here is the information you requested to access IAAPA Members Area.\n");
		  printf(SMAIL "\n\n User Name(Alias): %s \n Password: %s\n\n",@row[0],@row[1]);
		  printf(SMAIL "Make sure you make note of this information for future reference.\n");
      printf(SMAIL "\n\nBest Regards\n\n");
      printf(SMAIL "IAAPA\n\n");
	    close(SMAIL);  
    } 
    else {
		  $log_msg = "The information you provided is not valid, try again !!!";
		}
	}
	else {
    $log_msg = "The information you provided is not valid, try again !!!";
  }
}

sub displaymsg {

  print "Content-type: text/html\n\n";

  print qq ~
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link href="/lib/members.css" rel="stylesheet" type="text/css"/>
	  <title>IAAPA - Members Area (forgot password)</title>
</head>
<body>
  <div align="center">
	  <div id="main_area">
		  <div id="header_area">
			  <div id="logo_area">
				  <a href="http://www.axisandallies.net"><img src="/images/members/logo_iaapa.gif" alt="IAAPA" width="120" height="50" /></a>
				</div>
				<div id="site_title">
				  <img src="/images/members/logo_site.gif" alt="International Axis And Allies Players Association" width="180" height="50" />
				</div>
				<div id="top_menu">
				  Top Menu
				</div> <!-- top menu bar end -->
      </div><!-- header_area end -->
			<div class="clr"></div>
			  <table width="778" style="color:#fff; background-color: #869eb8; border-bottom: solid 1px #eaeaea;" cellpadding="10" cellspacing="1">
				  <tr>
            <td align="center" valing="middle">
						  <b>&nbsp;</b>
						</td>
          </tr>
        </table>
			<div id="content_area">
			  <div id="content">
				  <div align="center" style="width:350px;">
  				<br />
					<table cellspacing="5"> 
					  <tr>
						  <td valign="middle"><img src="/images/members/iconemail.gif" alt="Login" /></td>
							<td>&nbsp;&nbsp;</td>
							<td align="left" valign="middle">
							  <h3>Send Password by Email</h3>
							  Only active IAAPA members have access to the Members area.
					      <a href="/index.php?option=com_content&task=blogsection&id=7&Itemid=46">Click here</a>
				      	for more information on how to join IAAPA or visit our
								<a href="/index.php">web site</a>.
							</td>
						</tr>
					</table>
					<br />
					<div align="left">
  				<br /><br /><br />
					</div>
					<br />
					<div style="border: solid 1px #c8c8c8;">
          <br /><br /><br />
					<b>Shortly your alias and password will be emailed
					to your registered email account.<br /><br />
					Thank You.</b>
					<br /><br />
					<form>
					  <input class="button" type="button" value="Return to the Web Site" onclick="javascript:window.location='/index.php';">
					</form>
					<br />
					</div>
					</div>
	        <br /><br /><br /><br />					
				</div> <!-- content end -->
				<div id="right_area">
				  <br />
			  </div> <!-- right_area end -->
			</div> <!-- content_area end -->
			<div class="clr"></div>
			<div id="footer_area">
			  &copy; Copyright 2006,Created by the International Axis & Allies Players Association.<br />
				This site is intended to promote the use of Axis and Allies Board Game from Milton Bradley.<br />
				&copy; Axis and Allies is trademark owned by Hasbro Inc, All rights reserved.
			</div>
	  </div><!-- main_area end -->
  </div> <!-- center end -->
</body>
</html>		
  ~;

}
