类更新

This commit is contained in:
筱锋xiao_lfeng 2023-05-02 17:16:50 +08:00
parent 463e20190f
commit 267604fc86
4 changed files with 209 additions and 10 deletions

48
Class/Key.php Normal file
View File

@ -0,0 +1,48 @@
<?php
/*
* Copyright © 2016 - 2023 筱锋xiao_lfeng. All Rights Reserved.
* 开发开源遵循 MIT 许可,若需商用请联系开发者
* https://www.x-lf.com/
*/
class Key
{
/**
* 生成验证码
* 说明:
*
* 1. [type(string)]
* - [Number] 数字验证码
* - [English]英文验证码
* - [Normal] 混合验证码
* @param int $long 验证码长度
* @param string $type 验证码类型
* @return string 返回结果为生成的验证码
*/
public static function Captcha(int $long, string $type = 'Number'): string
{
/** @var string $output */
for ($i = 1; $i <= $long; $i++) {
if ($type == 'Number') {
$output = $output . rand(0, 9);
} elseif ($type == 'English') {
$output = $output . chr(rand(65, 90));
} elseif ($type == 'Normal') {
if (time() % 2 == 0) {
if ($i % 2 == 0) {
$output = $output . chr(rand(65, 90));
} else {
$output = $output . rand(0, 9);
}
} else {
if ($i % 3 != 0) {
$output = $output . chr(rand(65, 90));
} else {
$output = $output . rand(0, 9);
}
}
}
}
return $output;
}
}

112
Class/Mailer/SendMail.php Normal file
View File

@ -0,0 +1,112 @@
<?php
/*
* Copyright © 2016 - 2023 筱锋xiao_lfeng. All Rights Reserved.
* 开发开源遵循 MIT 许可,若需商用请联系开发者
* https://www.x-lf.com/
*/
namespace Mailer;
use Mailer\PHPMailer\Exception;
use Mailer\PHPMailer\PHPMailer;
class SendMail
{
public static int $EmailType;
public static string $EmailReceiver;
private array $ConfigData;
public static int $ExpDate;
private PHPMailer $Mail;
/**
* @return void 导入文件,无具体返回值
*/
protected function __consort()
{
// 文件导入
$Array_ConfigData = null;
$FileData = fopen(dirname(__FILE__, 3) . "/setting.inc.json", 'r');
while (!feof($FileData))
$Array_ConfigData .= fgetc($FileData);
$Array_ConfigData = json_decode($Array_ConfigData, JSON_UNESCAPED_UNICODE);
$this->ConfigData = json_decode($Array_ConfigData, JSON_UNESCAPED_UNICODE)["Smtp"];
fclose($FileData);
// 参数赋予
self::$ExpDate = $Array_ConfigData["Mail"]['ExpDate'];
// 类导入
$this->Mail = new PHPMailer(true);
}
/**
* 检查通信协议是 HTTP 还是 HTTPS
* @param string $Smtp_Type [Port]获取端口值,[Secure]连接模式
* @return mixed|string|null
*/
private function SSLCheck(string $Smtp_Type)
{
if ($Smtp_Type == 'Port')
return $_SERVER['SERVER_PORT'] != '443' ? $this->ConfigData['Port'] : $this->ConfigData['SecurePort'];
elseif ($Smtp_Type == 'Secure')
if ($_SERVER['SERVER_PORT'] != '443')
return 'TLS';
else
return 'ssl';
else
return null;
}
/**
* 发件基础内容(调用)
* 说明:
*
* 1. [EmailType(int)] 邮件发送类型
* - [1] 站点注册邮件
* - [2] 站点邮件登录
* @param string $EmailReceiver 邮件接收方(邮箱地址)
* @param int $EmailType 发送邮件类型
* @return bool 邮件发送成功返回 true 否则返回 false
*/
public function PostMail(string $EmailReceiver, int $EmailType): bool
{
self::$EmailType = $EmailType;
self::$EmailReceiver = $EmailReceiver;
// 尝试邮件发送
try {
// 服务器配置
$this->Mail->CharSet = "UTF-8";
$this->Mail->SMTPDebug = 0;
$this->Mail->isSMTP();
$this->Mail->Host = $this->ConfigData['Host'];
$this->Mail->SMTPAuth = $this->ConfigData['SmtpAuth'];
$this->Mail->Username = $this->ConfigData['Username'];
$this->Mail->Password = $this->ConfigData['Password'];
$this->Mail->SMTPSecure = $this->SSLCheck('Secure');
$this->Mail->Port = $this->SSLCheck('Port');
$this->Mail->setFrom($this->ConfigData['User'], $this->ConfigData['Name']);
$this->Mail->addAddress($EmailReceiver);
// 发件编写
if ($EmailType == 1) $this->EmailRegister();
else if ($EmailType == 2) $this->EmailLogin();
$this->Mail->send();
return true;
} catch (Exception $e) {
//echo '邮件发送失败:', $this->Mail->ErrorInfo;
return false;
}
}
private function EmailRegister(): void
{
$this->Mail->Subject = $this->ConfigData['Name'] . ' - 站点注册'; // 邮箱标题
$this->Mail->Body = MailTemplate::Templates($Input_Code);
}
private function EmailLogin()
{
$this->Mail->Subject = $this->ConfigData['Name'] . ' - 邮箱登录验证码'; // 邮箱标题
$this->Mail->Body = MailTemplate::Templates($Input_Code);
}
}

View File

@ -16,18 +16,31 @@ class Normal
*/
public static function Output(int $gType, array $OtherArray = null)
{
$Json_Data = [
'output' => self::OutputMessage($gType, 0),
'code' => self::OutputMessage($gType, 1),
'data' => [
'message' => self::OutputMessage($gType, 2),
],
];
if (!empty($OtherArray)) {
$Json_Data['data']['data'] = $OtherArray;
if (self::OutputMessage($gType, 1) != null) {
$Json_Data = [
'output' => self::OutputMessage($gType, 0),
'code' => self::OutputMessage($gType, 1),
'data' => [
'statusCode' => $gType,
'message' => self::OutputMessage($gType, 2),
],
];
if (!empty($OtherArray)) {
$Json_Data['data']['data'] = $OtherArray;
}
header(self::HttpStatusCode(self::OutputMessage($gType, 1)));
} else {
$Json_Data = [
'output' => 'DevelopError',
'code' => 0,
'data' => [
'statusCode' => 0,
'message' => "开发错误,请查阅",
],
];
header(self::HttpStatusCode(502));
}
// Json 输出
header(self::HttpStatusCode(self::OutputMessage($gType, 1)));
echo json_encode($Json_Data, JSON_UNESCAPED_UNICODE);
}

View File

@ -60,4 +60,30 @@ public static function INSERT(string $Mysql_Query): bool
else
return false;
}
/**
* MySQL更新库
* @param string $Mysql_Query
* @return bool
*/
public static function UPDATE(string $Mysql_Query): bool
{
if (preg_match('/^UPDATE/', $Mysql_Query))
return mysqli_query(self::MySqlConn(), $Mysql_Query);
else
return false;
}
/**
* MySQL删除库
* @param string $Mysql_Query
* @return bool
*/
public static function DELETE(string $Mysql_Query): bool
{
if (preg_match('/^DELETE/', $Mysql_Query))
return mysqli_query(self::MySqlConn(), $Mysql_Query);
else
return false;
}
}