作者 邓超

1

1 <?php 1 <?php
2 2
  3 +#echo file_get_contents('http://gold.cnfol.com/mingjiadianjin/20230210/30052311.shtml');
@@ -2,6 +2,9 @@ @@ -2,6 +2,9 @@
2 2
3 namespace Controller; 3 namespace Controller;
4 4
  5 +use Lib\Mail\Mail;
  6 +use Model\email;
  7 +
5 /** 8 /**
6 * @author:dc 9 * @author:dc
7 * @time 2023/2/13 11:28 10 * @time 2023/2/13 11:28
@@ -21,7 +24,7 @@ class Home { @@ -21,7 +24,7 @@ class Home {
21 // $mail,$password,$imap,$smtp 24 // $mail,$password,$imap,$smtp
22 $formData = app()->request(['email','password','imap','smtp']); 25 $formData = app()->request(['email','password','imap','smtp']);
23 26
24 - if(empty($formData['email']) || !preg_match("//",$formData['email'])){ 27 + if(empty($formData['email']) || !preg_match("/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/",$formData['email'])){
25 app()->e('email_verify_error'); 28 app()->e('email_verify_error');
26 } 29 }
27 30
@@ -38,47 +41,41 @@ class Home { @@ -38,47 +41,41 @@ class Home {
38 } 41 }
39 42
40 // 进行远程登录,验证 43 // 进行远程登录,验证
41 -  
42 -  
43 - // 查询数据  
44 - $model = db()->first(\Model\email::first($formData['email']));  
45 -  
46 -  
47 - if(!$model){  
48 - $model = new Email();  
49 - $model->email = $formData['email'];  
50 - }  
51 -  
52 - $model->imap = $formData['imap'];  
53 - $model->smtp = $formData['smtp'];  
54 - $model->status = Email::STATUS_ACTIVE;  
55 - $model->password = @base64_encode($formData['password']);  
56 -  
57 - try {  
58 - Mail::login($model->email,$model->password,$model->imap);  
59 - }catch (\Throwable $e){  
60 - return res()  
61 - ->message($e->getMessage())  
62 - ->status(400)  
63 - ->toJson(); 44 +// Mail::login($formData['email'],$formData['password'],$formData['imap']);
  45 +
  46 + // 是否存在
  47 + $id = db()->value(email::hasEmail($formData['email']));
  48 +
  49 + $data = [
  50 + 'password' => base64_encode($formData['password']),
  51 + 'imap' => $formData['imap'],
  52 + 'smtp' => $formData['smtp'],
  53 + 'status' => 1,
  54 + 'pwd_error' => 0,
  55 + 'name' => '',
  56 + 'token' => md5($formData['email'].$formData['password'].$formData['imap']),
  57 + ];
  58 +
  59 + if($id){
  60 + // 修改
  61 + $ret = db()->update(email::$table,$data,dbWhere(['id'=>$id]));
  62 + }else{
  63 + // 新增
  64 + $ret = db()->insert(email::$table,$data);
64 } 65 }
65 66
66 - // 登录成功了,密码验证字段通过  
67 - $model->pwd_error = 0;  
68 - // 保存好邮箱  
69 - $model->save();  
70 67
71 - // 设置上id,方便后面使用  
72 - Mail::$client[$model->email]->setId($model->id); 68 + if($ret){
  69 + app()->_json(
  70 + [
  71 + 'token' => $data['token']
  72 + ],
  73 + 'login_success'
  74 + );
  75 + }
73 76
74 - // 开始同步文件夹  
75 -// $folder = Mail::syncFolder($model->email); 77 + app()->e('login_error');
76 78
77 - return res()  
78 - ->data([  
79 - 'token' => token_en($model->id.','.$model->email.','.time())  
80 - ])  
81 - ->toJson();  
82 79
83 } 80 }
84 81
@@ -38,7 +38,7 @@ function db():\Lib\DbPool{ @@ -38,7 +38,7 @@ function db():\Lib\DbPool{
38 function logs($message,$filename=null){ 38 function logs($message,$filename=null){
39 @file_put_contents( 39 @file_put_contents(
40 $filename ? $filename : LOG_PATH.'/error.log', 40 $filename ? $filename : LOG_PATH.'/error.log',
41 - print_r($message,true).PHP_EOL, 41 + date('Y-m-d H:i:s ').print_r($message,true).PHP_EOL,
42 FILE_APPEND 42 FILE_APPEND
43 ); 43 );
44 } 44 }
@@ -116,3 +116,57 @@ function my_filter($value,$filter=null){ @@ -116,3 +116,57 @@ function my_filter($value,$filter=null){
116 116
117 return $value; 117 return $value;
118 } 118 }
  119 +
  120 +/**
  121 + * db 组合条件
  122 + * @param array $where
  123 + * @param string $ar
  124 + * @return string
  125 + * @author:dc
  126 + * @time 2023/2/17 10:41
  127 + */
  128 +function dbWhere(array $where, string $ar = 'and'):string{
  129 + $sql = [];
  130 + foreach ($where as $f=>$v){
  131 + $sql[] = "`{$f}` = '{$v}'";
  132 + }
  133 + return implode(' '.$ar.' ',$sql);
  134 +}
  135 +
  136 +/**
  137 + * db 更新sql
  138 + * @param array $data
  139 + * @return string
  140 + * @author:dc
  141 + * @time 2023/2/17 10:43
  142 + */
  143 +function dbUpdate(array $data):string {
  144 + $sql = [];
  145 + foreach ($data as $f=>$v){
  146 + $sql[] = "`{$f}` = :{$f}";
  147 + }
  148 + return implode(' , ',$sql);
  149 +}
  150 +
  151 +
  152 +
  153 +
  154 +
  155 +
  156 +
  157 +
  158 +
  159 +
  160 +
  161 +
  162 +
  163 +
  164 +
  165 +
  166 +
  167 +
  168 +
  169 +
  170 +
  171 +
  172 +
@@ -14,4 +14,7 @@ return [ @@ -14,4 +14,7 @@ return [
14 'smtp_verify_error' => 'smtp服务器地址错误', 14 'smtp_verify_error' => 'smtp服务器地址错误',
15 15
16 'server_error' => '服务器异常', 16 'server_error' => '服务器异常',
  17 +
  18 + 'login_success' => '登录成功',
  19 + 'login_error' => '登录失败',
17 ]; 20 ];
@@ -95,7 +95,7 @@ class App { @@ -95,7 +95,7 @@ class App {
95 } 95 }
96 96
97 // 取到路由 控制器 97 // 取到路由 控制器
98 - $route = $app->route->get(trim($_SERVER['REQUEST_URI'])); 98 + $route = $app->route->get(explode('?',$_SERVER['REQUEST_URI'])[0]);
99 if(!$route){ 99 if(!$route){
100 $app->e('route_not_found'); 100 $app->e('route_not_found');
101 } 101 }
@@ -114,7 +114,7 @@ class App { @@ -114,7 +114,7 @@ class App {
114 // 记录日志 114 // 记录日志
115 $filename = LOG_PATH.'/'.$app->nowDate().'.log'; 115 $filename = LOG_PATH.'/'.$app->nowDate().'.log';
116 logs( 116 logs(
117 - $app->nowDateTime().' '.$exception->getMessage().PHP_EOL.$exception->getTraceAsString(), 117 + $exception->getMessage().PHP_EOL.$exception->getTraceAsString(),
118 $filename 118 $filename
119 ); 119 );
120 120
@@ -133,25 +133,28 @@ class App { @@ -133,25 +133,28 @@ class App {
133 * 请求的参数 133 * 请求的参数
134 * @param string $name 134 * @param string $name
135 * @param null $default 135 * @param null $default
  136 + * @param array|string|null $filter
136 * @return array|false|float|int|mixed|null 137 * @return array|false|float|int|mixed|null
137 * @author:dc 138 * @author:dc
138 - * @time 2023/2/13 12:00 139 + * @time 2023/2/17 9:37
139 */ 140 */
140 - public function request($name='*',$default=null){  
141 - if($name == '*'){  
142 - return $this->request;  
143 - }  
144 - if (is_string($name)){  
145 - return $this->request[$name]??$default;  
146 - }  
147 -  
148 - if(is_array($name)){ 141 + public function request($name='*',$default=null,$filter = null){
149 $data = []; 142 $data = [];
  143 + if($name === '*'){
  144 + $data = $this->request;
  145 + } else if (is_string($name)){
  146 + $data = $this->request[$name]??$default;
  147 + }else if(is_array($name)){
150 foreach ($this->request as $key=>$value){ 148 foreach ($this->request as $key=>$value){
151 if(in_array($key,$name)){ 149 if(in_array($key,$name)){
152 $data[$key] = $value; 150 $data[$key] = $value;
153 } 151 }
154 } 152 }
  153 + }
  154 + if($filter){
  155 + $data = my_filter($data,$filter);
  156 + }
  157 + if($data !== []){
155 return $data; 158 return $data;
156 } 159 }
157 return null; 160 return null;
@@ -178,7 +181,7 @@ class App { @@ -178,7 +181,7 @@ class App {
178 181
179 /** 182 /**
180 * 错误 183 * 错误
181 - * @param $message 184 + * @param string $message
182 * @param int $status 185 * @param int $status
183 * @throws Err 186 * @throws Err
184 * @author:dc 187 * @author:dc
@@ -31,18 +31,6 @@ class DbPool { @@ -31,18 +31,6 @@ class DbPool {
31 return $this->client; 31 return $this->client;
32 } 32 }
33 33
34 - /**  
35 - * 表  
36 - * @var string  
37 - */  
38 - private string $table;  
39 -  
40 - /**  
41 - * 查询条件  
42 - * @var string|array  
43 - */  
44 - private string | array $where;  
45 -  
46 34
47 public function __construct() 35 public function __construct()
48 { 36 {
@@ -65,46 +53,83 @@ class DbPool { @@ -65,46 +53,83 @@ class DbPool {
65 53
66 54
67 /** 55 /**
68 - * 表  
69 - * @param $table  
70 - * @return $this 56 + * 查询
  57 + * @param string|array $sql
  58 + * @return false|\PDOStatement
71 * @author:dc 59 * @author:dc
72 - * @time 2023/2/13 14:36 60 + * @time 2023/2/17 10:01
73 */ 61 */
74 - public function table($table){  
75 - $this->table = $table;  
76 - return $this; 62 + private function query(string|array $sql){
  63 + if(is_array($sql)){
  64 + list($sql,$params) = $sql;
  65 + }else{
  66 + $params = null;
77 } 67 }
  68 + $query = $this->client->prepare($sql);
  69 +
  70 + // todo:: 记录日志,生产请注释
  71 + logs([$sql,$params]);
  72 +
  73 + if($query->execute($params)){
  74 + return $query;
  75 + }
  76 +
  77 + return false;
  78 + }
  79 +
78 80
79 /** 81 /**
80 - * 条件  
81 - * @param string|array $where  
82 - * @return $this 82 + * 更新数据
  83 + * @param string $table
  84 + * @param array $data
  85 + * @param string $where
  86 + * @param bool $timeauto
  87 + * @return int
83 * @author:dc 88 * @author:dc
84 - * @time 2023/2/13 14:38 89 + * @time 2023/2/17 14:03
85 */ 90 */
86 - public function where(string|array $where){  
87 - $this->where = $where;  
88 - return $this; 91 + public function update(string $table, array $data, string $where, $timeauto = true):int {
  92 +
  93 + if($timeauto){
  94 + $data['updated_at'] = empty($data['updated_at']) ? date('Y-m-d H:i:s') : $data['updated_at'];
  95 + }
  96 +
  97 + $sql = "update `{$table}` set ".dbUpdate($data). " where ".$where;
  98 + $query = $this->query([$sql,$data]);
  99 + if($query){
  100 + return $query->rowCount();
  101 + }
  102 + return 0;
89 } 103 }
90 104
91 105
92 /** 106 /**
93 - * @param $sql  
94 - * @param null $params  
95 - * @return false|\PDOStatement 107 + * 插入数据
  108 + * @param string $table
  109 + * @param array $data
  110 + * @param bool $timeauto
  111 + * @return int
96 * @author:dc 112 * @author:dc
97 - * @time 2023/2/13 14:41 113 + * @time 2023/2/17 14:04
98 */ 114 */
99 - private function query($sql,$params=null){ 115 + public function insert(string $table, array $data, $timeauto = true):int {
100 116
  117 + if($timeauto){
  118 + $data['created_at'] = empty($data['created_at']) ? date('Y-m-d H:i:s') : $data['created_at'];
101 } 119 }
102 120
  121 + $sql = "insert into `{$table}` set ".dbUpdate($data);
103 122
104 - public function get($sql){ 123 + $query = $this->query([$sql,$data]);
  124 +
  125 + if($query){
  126 + return $this->client->lastInsertId();
  127 + }
105 128
  129 + return 0;
106 } 130 }
107 131
  132 +
108 /** 133 /**
109 * 统计数量 134 * 统计数量
110 * @param string $sql 135 * @param string $sql
@@ -112,16 +137,29 @@ class DbPool { @@ -112,16 +137,29 @@ class DbPool {
112 * @author:dc 137 * @author:dc
113 * @time 2023/2/14 16:19 138 * @time 2023/2/14 16:19
114 */ 139 */
115 - public function count(string $sql):int{  
116 - $query = $this->client->prepare($sql);  
117 -  
118 - if($query->execute()){ 140 + public function count(string|array $sql):int{
  141 + $query = $this->query($sql);
  142 + if($query){
119 return $query->fetch(\PDO::FETCH_COLUMN); 143 return $query->fetch(\PDO::FETCH_COLUMN);
120 } 144 }
121 -  
122 return 0; 145 return 0;
123 } 146 }
124 147
  148 + /**
  149 + * 某个值
  150 + * @param string|array $sql
  151 + * @return mixed|null
  152 + * @author:dc
  153 + * @time 2023/2/17 11:03
  154 + */
  155 + public function value(string|array $sql){
  156 + $query = $this->query($sql);
  157 + if($query){
  158 + return $query->fetch(\PDO::FETCH_COLUMN);
  159 + }
  160 + return null;
  161 + }
  162 +
125 163
126 /** 164 /**
127 * 查询一条数据 165 * 查询一条数据
@@ -132,20 +170,62 @@ class DbPool { @@ -132,20 +170,62 @@ class DbPool {
132 */ 170 */
133 public function first(string|array $sql){ 171 public function first(string|array $sql){
134 172
135 - $query = $this->client->prepare(is_array($sql) ? $sql[0] : $sql); 173 + $query = $this->query($sql);
136 174
137 - if($query->execute(is_array($sql) ? $sql[1] : null)){ 175 + if($query){
138 return $query->fetch(); 176 return $query->fetch();
139 } 177 }
140 178
141 return null; 179 return null;
142 } 180 }
143 181
  182 + /**
  183 + * 查询列表
  184 + * @param string|array $sql
  185 + * @return mixed|null
  186 + * @author:dc
  187 + * @time 2023/2/13 14:54
  188 + */
  189 + public function all(string|array $sql){
144 190
145 - public function delete(){ 191 + $query = $this->query($sql);
146 192
  193 + if($query){
  194 + return $query->fetchAll();
147 } 195 }
148 196
  197 + return null;
  198 + }
  199 +
  200 +
  201 + /**
  202 + * 事务开启
  203 + * @author:dc
  204 + * @time 2023/2/17 11:35
  205 + */
  206 + public function transaction(){
  207 + $this->client->beginTransaction();
  208 + }
  209 +
  210 + /**
  211 + * 事务回滚
  212 + * @author:dc
  213 + * @time 2023/2/17 11:35
  214 + */
  215 + public function rollBack(){
  216 + $this->client->rollBack();
  217 + }
  218 +
  219 + /**
  220 + * 事务提交
  221 + * @author:dc
  222 + * @time 2023/2/17 11:35
  223 + */
  224 + public function commit(){
  225 + $this->client->commit();
  226 + }
  227 +
  228 +
149 229
150 /** 230 /**
151 * @param $cid 231 * @param $cid
1 <?php 1 <?php
2 declare(strict_types=1); 2 declare(strict_types=1);
3 3
4 -namespace Helper\Mail; 4 +namespace Lib\Mail;
5 5
6 -use Helper\Mail\Body; 6 +use Lib\Mail\Body;
7 7
8 8
9 /** 9 /**
@@ -21,9 +21,7 @@ class Route { @@ -21,9 +21,7 @@ class Route {
21 public function __construct() 21 public function __construct()
22 { 22 {
23 23
24 - $this->url = [  
25 - 'login' => [Home::class,'login']  
26 - ]; 24 + $this->url = require_once ROOT_PATH.'/route.php';
27 25
28 } 26 }
29 27
@@ -9,17 +9,41 @@ namespace Model; @@ -9,17 +9,41 @@ namespace Model;
9 */ 9 */
10 class email { 10 class email {
11 11
  12 + public static $table = 'emails';
  13 +
12 14
13 /** 15 /**
14 * 通过email查询 16 * 通过email查询
15 * @param $email 17 * @param $email
16 - * @return array 18 + * @return string
17 * @author:dc 19 * @author:dc
18 * @time 2023/2/13 14:50 20 * @time 2023/2/13 14:50
19 */ 21 */
20 - public static function first($email):array { 22 + public static function first($email, $filed='*'):string {
  23 + return "select {$filed} from `".static::$table."` where `".(is_numeric($email) ? 'id' : 'email')."` = '{$email}' limit 1";
  24 + }
  25 +
  26 + /**
  27 + * 统计邮箱的数量
  28 + * @return string
  29 + * @author:dc
  30 + * @time 2023/2/14 16:16
  31 + */
  32 + public static function count():string {
  33 + return "select count(*) from `".static::$table."` limit 1";
  34 + }
  35 +
  36 +
  37 + /**
  38 + * 邮箱是否存在的sql
  39 + * @param string $email
  40 + * @return array
  41 + * @author:dc
  42 + * @time 2023/2/17 10:15
  43 + */
  44 + public static function hasEmail(string $email):array {
21 return [ 45 return [
22 - "select * from `emails` where `email` = ? limit 1", 46 + "select `id` from `".static::$table."` where `email` = ? limit 1",
23 [ 47 [
24 $email 48 $email
25 ] 49 ]
@@ -27,14 +51,31 @@ class email { @@ -27,14 +51,31 @@ class email {
27 } 51 }
28 52
29 /** 53 /**
30 - * 统计邮箱的数量  
31 - * @return string 54 + * 更新
  55 + * @param $data
  56 + * @param $where
  57 + * @return array
32 * @author:dc 58 * @author:dc
33 - * @time 2023/2/14 16:16 59 + * @time 2023/2/17 10:24
34 */ 60 */
35 - public static function count():string {  
36 - return "select count(*) from `emails` limit 1"; 61 + public static function update($data,$where){
  62 +
  63 + return [
  64 + "update `".static::$table."` set ". dbUpdate($data)
  65 + ." where ".(is_numeric($where) ? '`id` = '.$where : dbWhere($where)),
  66 + $data
  67 + ];
  68 +
  69 + }
  70 +
  71 +
  72 + public function insert(){
  73 +
37 } 74 }
38 75
39 76
  77 +
  78 +
  79 +
  80 +
40 } 81 }
  1 +<?php
  2 +
  3 +use Controller\Home;
  4 +
  5 +
  6 +return [
  7 + 'login' => [Home::class,'login']
  8 +];