<?php

// +---------------------------------------------+
// |     Copyright  2008 - 2018 weenCompany      |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

if(@file_exists('../install'))
{
	$needDeleteInstall = 1;
}

if(isset($_GET['lang']) AND (!isset($_COOKIE['weenCompany_mplang']) OR $_COOKIE['weenCompany_mplang'] != $_GET['lang']))
{
    $lang = strtolower($_GET['lang']);
	if($lang == 'cn' OR $lang == 'en'){
		setcookie("weenCompany_mplang", $lang, 0, "/");
		Header("Location: $_SERVER[REQUEST_URI]");
		exit();
	}
}

define('IN_ADMIN', true);
define('IN_GETPASS', true);
define('IN_WEENCOMPANY', true);

$rootpath = "./../";

include($rootpath . 'includes/core.php');

if($needDeleteInstall)
{
  $logininfo ='提示: 请在删除系统安装目录(./install/)后继续!';
  $disabled =  ' disabled="disabled" style="cursor:default;"';
}else{
  $inputfocus = 'onLoad="document.forms.wcloginform.useremail.focus()"';
}

$this_full_url = $weenurl . substr(str_replace(dirname(dirname(__FILE__)), '', dirname(__FILE__)), 1);

$email = trim($_POST['useremail']);

if (isset($_POST['Submit']))
{
	if (empty($email))
		$logininfo  = '';
	elseif (!isEmail($email))
		$logininfo  = 'Email地址格式不规范!';
	else
	{
		$user = $DB->query_first("SELECT username, password FROM " . TABLE_PREFIX . "users WHERE email = '$email' ");

		if(!isset($user)){
			$logininfo = 'Email地址不存在!';
		}else{

			$getmainsettings = $DB->query("SELECT varname, value FROM " . TABLE_PREFIX . "mainsettings");
			for($i = 0; $setting = $DB->fetch_array($getmainsettings); $i++)
			{
			  $mainsettings[$setting['varname']] = $setting['value'];
			}

			require_once ('../includes/mail/class.phpmailer.php');

			$mail = new PHPMailer();

			if($mainsettings['email_use_smtp'] == '1')
				$mail->IsSMTP();
			else
				$mail->IsMail();

			$mail->Host =  $mainsettings['email_smtp_host'];
			$mail->Port = $mainsettings['email_smtp_port'];

			if( $mainsettings['email_smtp_require_auth'] == '1' ) {
				$mail->SMTPAuth = true;
				$mail->Username = $mainsettings['email_smtp_user'];
				$mail->Password = $mainsettings['email_smtp_pass'];
			}

			$mail->From      =  $technicalemail;
			$mail->FromName      =  $mainsettings['copyrighttext'];
			$mail->AddReplyTo ($technicalemail, $mainsettings['copyrighttext']);   
			$mail->Sender      =  $mainsettings['email_smtp_from'];
			
			$mail->Subject  = '管理员找回密码';
			$message        = "\r\n请点击以下链接重新设置密码:\r\n\r\n";
			$message       .= "<a href=\"" . $this_full_url . "/forgotpass.php?lang=". $sitedefaultlang ."&key=".base64_encode($email)."&sid=" . md5($user['username'] .WEB_COOKIE_KEY.$user['password']) . "\" target=\"_blank\">" .$this_full_url . "/forgotpass.php?lang=". $sitedefaultlang ."&key=".base64_encode($email)."&sid=" . md5($user['username'] .WEB_COOKIE_KEY.$user['password']) . "</a>\r\n";
			$mail->Body     = nl2br($message);
			$mail->AddAddress($email);

			if($mail->Send()){
				$logininfo = '<font color="#339900"><b>确认重设密码的邮件已发送到您的信箱, 请查收!</b></font>';
			}else{
				$logininfo = $mail->ErrorInfo;
			}

		}
	}
}

if (isset($_GET['sid']) AND isset($_GET['key']))
{
	$email = base64_decode(trim($_GET['key']));

	if(isEmail($email) AND isName(trim($_GET['sid']))){

		$user = $DB->query_first("SELECT userid, username, password FROM " . TABLE_PREFIX . "users WHERE email = '$email' ");

		if(isset($user))
		{
			if(trim($_GET['sid']) == md5($user['username'] .WEB_COOKIE_KEY.$user['password']))
			{

				$pwd = passwdGen(8);

				$result = $DB->query("UPDATE " . TABLE_PREFIX . "users SET password    = '".md5($pwd)."'							 
														   WHERE userid = '$user[userid]' ");

				if (!isset($result)){
					$logininfo = '重设密码时发生错误, 请重试!';
				}else{

					$getmainsettings = $DB->query("SELECT varname, value FROM " . TABLE_PREFIX . "mainsettings");
					for($i = 0; $setting = $DB->fetch_array($getmainsettings); $i++)
					{
					  $mainsettings[$setting['varname']] = $setting['value'];
					}

					require_once ('../includes/mail/class.phpmailer.php');

					$mail = new PHPMailer();

					if($mainsettings['email_use_smtp'] == '1')
						$mail->IsSMTP();
					else
						$mail->IsMail();

					$mail->Host =  $mainsettings['email_smtp_host'];
					$mail->Port = $mainsettings['email_smtp_port'];

					if( $mainsettings['email_smtp_require_auth'] == '1' ) {
						$mail->SMTPAuth = true;
						$mail->Username = $mainsettings['email_smtp_user'];
						$mail->Password = $mainsettings['email_smtp_pass'];
					}

					$mail->From      =  $technicalemail;
					$mail->FromName      =  $mainsettings['copyrighttext'];
					$mail->AddReplyTo ($technicalemail, $mainsettings['copyrighttext']);   
					$mail->Sender      =  $mainsettings['email_smtp_from'];
					
					$mail->Subject  = '管理员新密码';
					$message        = "\r\n用户名: " .$user['username']. "\r\n\r\n";
					$message       .= "密码: " .$pwd. "\r\n";
					$mail->Body     = nl2br($message);
					$mail->AddAddress($email);

					if($mail->Send()){
						$logininfo = '<font color="#339900"><b>新密码已发送到您的邮箱, 请查收!</b></font>';
					}else{
						$logininfo = '<font color="#339900"><b>您的用户名是: '.$user['username']. '&nbsp;&nbsp;密码是: '.$pwd . '</b></font>';
					}

				}

			}else{
				$logininfo = '重设密码时发生错误: 验证码不正确!';
			}

		}else{
			$logininfo = '重设密码时发生错误: 验证码不正确!';
		}

	}else{
		$logininfo = '重设密码时发生错误: 验证码不正确!';
	}

}

echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
	<html>
	<head>

	<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
	<title>weenCompany闻名企业网站系统(CWS)管理登录 - 找回密码</title>
	<link rel=stylesheet href=\'./styles/advanced/login.css\' />
	<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
	<!--
	if(top.location != self.location)
	{
	  top.location.replace(self.location)
	}
	-->
	</SCRIPT>

	</head>

	<body id="page-login" '. $inputfocus .' >
	
	<div class="login-container">
	<div class="login-box">
		<form action="forgotpass.php" method="post" name="wcloginform">
			<fieldset class="login-form">
				<h2>weenCompany闻名企业网站系统 - 找回密码</h2>
				<div id="messages">' . $logininfo . '</div>
				<div class="input-box input-left"><label for="username">管理员Email:</label><br/>
					<input type="text" id="username" name="useremail" value="" class="required-entry input-text" '.$disabled .'/></div>
				<div class="input-box input-right"><label for="login">密码:</label><br/>
					<input type="password" id="login" name="loginpassword" class="required-entry input-text"  value="i forgot" disabled="disabled" style="cursor:default;" /></div>
				<div class="clear"></div>
				<div class="form-buttons" style="margin-right:8px;"><input type="submit" name="Submit" class="form-button" value="找回密码" '.$disabled .'/></div>
				 <div style="margin-left:8px;"><a href="./" target="_parent" class="normal">返回登录?</a>&nbsp;</div>
		   </fieldset>
			<p class="legal" align="right"><a href="http://www.weentech.com" target="_blank" class="normal">闻泰网络</a> <font color=#FF9900>&copy;</font><font class="copyright">'.date("Y").'</font>&nbsp;</p>
		</form>
		<div class="bottom"></div>
	</div>
	</div>
</body>
</html>';


?>