-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.class.php
More file actions
110 lines (106 loc) · 4.17 KB
/
Copy pathUser.class.php
File metadata and controls
110 lines (106 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// 加载邮件发送类
require 'PHPMailer.php';
require 'SMTP.php';
/**
* 用户类
*/
class User
{
private $servername = "localhost";
private $username = "root";
private $password = "123123";
private $dbname = "api";
private $token = '';
protected $ip = '';
protected $connect = null;
function __construct($servername, $username, $password, $dbname)
{
$this->servername = $servername;
$this->username = $username;
$this->password = $password;
$this->dbname = $dbname;
$this->ip = $this->_ip();
$this->_connect();
}
// 添加token
public function addUser($email, $subject, $content, $limitTodayTime=0){
// 生成token
$this->_createToken($email);
// 替换内容中token
$content = str_ireplace('%token%', $this->token, $content);
// 插入数据
$stmt = $this->connect->prepare("INSERT INTO users(`token`, `ip`, `limit_today_time`, `email`)
VALUES (:token, :ip, :limitTodayTime, :email)");
$stmt->bindParam(':token', $this->token);
$stmt->bindParam(':ip', $this->ip);
$stmt->bindParam(':limitTodayTime', $limitTodayTime); // 设置每日调用上限,默认0无限制
$stmt->bindParam(':email', $email);
// 使用 exec() ,没有结果返回
$stmt->execute();
if($stmt->rowCount() > 0){ // 插入成功
if($this->_sendEmail($email, $subject, $content)){ // 发送邮件且成功
return json_encode(array('status' => 1, 'code' => 0, 'message' => '邮件发送成功'), JSON_UNESCAPED_UNICODE);
}else{ // 发送邮件失败
return json_encode(array('status' => 0, 'code' => 6, 'message' => '邮件发送失败'), JSON_UNESCAPED_UNICODE);
}
}else{ // 插入失败
return json_encode(array('status' => 0, 'code' => 1, 'message' => '程序异常'), JSON_UNESCAPED_UNICODE);
}
}
// 生成token
private function _createToken($email){
$this->token = hash('sha256', $email.$this->ip.time());
}
// 发邮件方法
private function _sendEmail($emailTo, $subject, $content){
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
// $mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->CharSet='utf-8';
$mail->Host = 'smtp.qq.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'admin@wwzc.cc'; // SMTP username
$mail->Password = 'vseewawh'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('admin@wwzc.cc');
$mail->addAddress('907553043@qq.com'); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $content;
$mail->send();
return true;
} catch (Exception $e) {
return false;
// echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
// 连接数据库
private function _connect(){
try{
$this->connect = new PDO("mysql:host={$this->servername};dbname={$this->dbname}", $this->username, $this->password);
// 设置 PDO 错误模式,用于抛出异常
$this->connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
die(json_encode(array('status' => 0, 'code' => 1, 'message' => '程序异常'), JSON_UNESCAPED_UNICODE));
}
}
// 获取ip
private function _ip(){
$ip = '1.0.0.1';
if(isset($_SERVER['HTTP_CLIENT_IP'])){
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}elseif(isset($_SERVER['REMOTE_ADDR'])){
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
}
?>