<?php

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

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

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?key=".base64_encode($email)."&sid=" . md5($user['username'] .WEB_COOKIE_KEY.$user['password']) . "\" target=\"_blank\">" .$this_full_url . "/forgotpass.php?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=UTF-8">
	<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>';


?>
