作者 邓超

1

<?php
#echo file_get_contents('http://gold.cnfol.com/mingjiadianjin/20230210/30052311.shtml');
\ No newline at end of file
... ...
... ... @@ -2,6 +2,9 @@
namespace Controller;
use Lib\Mail\Mail;
use Model\email;
/**
* @author:dc
* @time 2023/2/13 11:28
... ... @@ -21,7 +24,7 @@ class Home {
// $mail,$password,$imap,$smtp
$formData = app()->request(['email','password','imap','smtp']);
if(empty($formData['email']) || !preg_match("//",$formData['email'])){
if(empty($formData['email']) || !preg_match("/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/",$formData['email'])){
app()->e('email_verify_error');
}
... ... @@ -38,47 +41,41 @@ class Home {
}
// 进行远程登录,验证
// 查询数据
$model = db()->first(\Model\email::first($formData['email']));
if(!$model){
$model = new Email();
$model->email = $formData['email'];
}
$model->imap = $formData['imap'];
$model->smtp = $formData['smtp'];
$model->status = Email::STATUS_ACTIVE;
$model->password = @base64_encode($formData['password']);
try {
Mail::login($model->email,$model->password,$model->imap);
}catch (\Throwable $e){
return res()
->message($e->getMessage())
->status(400)
->toJson();
// Mail::login($formData['email'],$formData['password'],$formData['imap']);
// 是否存在
$id = db()->value(email::hasEmail($formData['email']));
$data = [
'password' => base64_encode($formData['password']),
'imap' => $formData['imap'],
'smtp' => $formData['smtp'],
'status' => 1,
'pwd_error' => 0,
'name' => '',
'token' => md5($formData['email'].$formData['password'].$formData['imap']),
];
if($id){
// 修改
$ret = db()->update(email::$table,$data,dbWhere(['id'=>$id]));
}else{
// 新增
$ret = db()->insert(email::$table,$data);
}
// 登录成功了,密码验证字段通过
$model->pwd_error = 0;
// 保存好邮箱
$model->save();
// 设置上id,方便后面使用
Mail::$client[$model->email]->setId($model->id);
if($ret){
app()->_json(
[
'token' => $data['token']
],
'login_success'
);
}
// 开始同步文件夹
// $folder = Mail::syncFolder($model->email);
app()->e('login_error');
return res()
->data([
'token' => token_en($model->id.','.$model->email.','.time())
])
->toJson();
}
... ...
... ... @@ -38,7 +38,7 @@ function db():\Lib\DbPool{
function logs($message,$filename=null){
@file_put_contents(
$filename ? $filename : LOG_PATH.'/error.log',
print_r($message,true).PHP_EOL,
date('Y-m-d H:i:s ').print_r($message,true).PHP_EOL,
FILE_APPEND
);
}
... ... @@ -116,3 +116,57 @@ function my_filter($value,$filter=null){
return $value;
}
/**
* db 组合条件
* @param array $where
* @param string $ar
* @return string
* @author:dc
* @time 2023/2/17 10:41
*/
function dbWhere(array $where, string $ar = 'and'):string{
$sql = [];
foreach ($where as $f=>$v){
$sql[] = "`{$f}` = '{$v}'";
}
return implode(' '.$ar.' ',$sql);
}
/**
* db 更新sql
* @param array $data
* @return string
* @author:dc
* @time 2023/2/17 10:43
*/
function dbUpdate(array $data):string {
$sql = [];
foreach ($data as $f=>$v){
$sql[] = "`{$f}` = :{$f}";
}
return implode(' , ',$sql);
}
... ...
... ... @@ -14,4 +14,7 @@ return [
'smtp_verify_error' => 'smtp服务器地址错误',
'server_error' => '服务器异常',
'login_success' => '登录成功',
'login_error' => '登录失败',
];
\ No newline at end of file
... ...
... ... @@ -95,7 +95,7 @@ class App {
}
// 取到路由 控制器
$route = $app->route->get(trim($_SERVER['REQUEST_URI']));
$route = $app->route->get(explode('?',$_SERVER['REQUEST_URI'])[0]);
if(!$route){
$app->e('route_not_found');
}
... ... @@ -114,7 +114,7 @@ class App {
// 记录日志
$filename = LOG_PATH.'/'.$app->nowDate().'.log';
logs(
$app->nowDateTime().' '.$exception->getMessage().PHP_EOL.$exception->getTraceAsString(),
$exception->getMessage().PHP_EOL.$exception->getTraceAsString(),
$filename
);
... ... @@ -133,25 +133,28 @@ class App {
* 请求的参数
* @param string $name
* @param null $default
* @param array|string|null $filter
* @return array|false|float|int|mixed|null
* @author:dc
* @time 2023/2/13 12:00
* @time 2023/2/17 9:37
*/
public function request($name='*',$default=null){
if($name == '*'){
return $this->request;
}
if (is_string($name)){
return $this->request[$name]??$default;
}
if(is_array($name)){
public function request($name='*',$default=null,$filter = null){
$data = [];
if($name === '*'){
$data = $this->request;
} else if (is_string($name)){
$data = $this->request[$name]??$default;
}else if(is_array($name)){
foreach ($this->request as $key=>$value){
if(in_array($key,$name)){
$data[$key] = $value;
}
}
}
if($filter){
$data = my_filter($data,$filter);
}
if($data !== []){
return $data;
}
return null;
... ... @@ -178,7 +181,7 @@ class App {
/**
* 错误
* @param $message
* @param string $message
* @param int $status
* @throws Err
* @author:dc
... ...
... ... @@ -31,18 +31,6 @@ class DbPool {
return $this->client;
}
/**
* 表
* @var string
*/
private string $table;
/**
* 查询条件
* @var string|array
*/
private string | array $where;
public function __construct()
{
... ... @@ -65,46 +53,83 @@ class DbPool {
/**
* 表
* @param $table
* @return $this
* 查询
* @param string|array $sql
* @return false|\PDOStatement
* @author:dc
* @time 2023/2/13 14:36
* @time 2023/2/17 10:01
*/
public function table($table){
$this->table = $table;
return $this;
private function query(string|array $sql){
if(is_array($sql)){
list($sql,$params) = $sql;
}else{
$params = null;
}
$query = $this->client->prepare($sql);
// todo:: 记录日志,生产请注释
logs([$sql,$params]);
if($query->execute($params)){
return $query;
}
return false;
}
/**
* 条件
* @param string|array $where
* @return $this
* 更新数据
* @param string $table
* @param array $data
* @param string $where
* @param bool $timeauto
* @return int
* @author:dc
* @time 2023/2/13 14:38
* @time 2023/2/17 14:03
*/
public function where(string|array $where){
$this->where = $where;
return $this;
public function update(string $table, array $data, string $where, $timeauto = true):int {
if($timeauto){
$data['updated_at'] = empty($data['updated_at']) ? date('Y-m-d H:i:s') : $data['updated_at'];
}
$sql = "update `{$table}` set ".dbUpdate($data). " where ".$where;
$query = $this->query([$sql,$data]);
if($query){
return $query->rowCount();
}
return 0;
}
/**
* @param $sql
* @param null $params
* @return false|\PDOStatement
* 插入数据
* @param string $table
* @param array $data
* @param bool $timeauto
* @return int
* @author:dc
* @time 2023/2/13 14:41
* @time 2023/2/17 14:04
*/
private function query($sql,$params=null){
public function insert(string $table, array $data, $timeauto = true):int {
if($timeauto){
$data['created_at'] = empty($data['created_at']) ? date('Y-m-d H:i:s') : $data['created_at'];
}
$sql = "insert into `{$table}` set ".dbUpdate($data);
public function get($sql){
$query = $this->query([$sql,$data]);
if($query){
return $this->client->lastInsertId();
}
return 0;
}
/**
* 统计数量
* @param string $sql
... ... @@ -112,16 +137,29 @@ class DbPool {
* @author:dc
* @time 2023/2/14 16:19
*/
public function count(string $sql):int{
$query = $this->client->prepare($sql);
if($query->execute()){
public function count(string|array $sql):int{
$query = $this->query($sql);
if($query){
return $query->fetch(\PDO::FETCH_COLUMN);
}
return 0;
}
/**
* 某个值
* @param string|array $sql
* @return mixed|null
* @author:dc
* @time 2023/2/17 11:03
*/
public function value(string|array $sql){
$query = $this->query($sql);
if($query){
return $query->fetch(\PDO::FETCH_COLUMN);
}
return null;
}
/**
* 查询一条数据
... ... @@ -132,20 +170,62 @@ class DbPool {
*/
public function first(string|array $sql){
$query = $this->client->prepare(is_array($sql) ? $sql[0] : $sql);
$query = $this->query($sql);
if($query->execute(is_array($sql) ? $sql[1] : null)){
if($query){
return $query->fetch();
}
return null;
}
/**
* 查询列表
* @param string|array $sql
* @return mixed|null
* @author:dc
* @time 2023/2/13 14:54
*/
public function all(string|array $sql){
public function delete(){
$query = $this->query($sql);
if($query){
return $query->fetchAll();
}
return null;
}
/**
* 事务开启
* @author:dc
* @time 2023/2/17 11:35
*/
public function transaction(){
$this->client->beginTransaction();
}
/**
* 事务回滚
* @author:dc
* @time 2023/2/17 11:35
*/
public function rollBack(){
$this->client->rollBack();
}
/**
* 事务提交
* @author:dc
* @time 2023/2/17 11:35
*/
public function commit(){
$this->client->commit();
}
/**
* @param $cid
... ...
<?php
declare(strict_types=1);
namespace Helper\Mail;
namespace Lib\Mail;
use Helper\Mail\Body;
use Lib\Mail\Body;
/**
... ...
... ... @@ -21,9 +21,7 @@ class Route {
public function __construct()
{
$this->url = [
'login' => [Home::class,'login']
];
$this->url = require_once ROOT_PATH.'/route.php';
}
... ...
... ... @@ -9,17 +9,41 @@ namespace Model;
*/
class email {
public static $table = 'emails';
/**
* 通过email查询
* @param $email
* @return array
* @return string
* @author:dc
* @time 2023/2/13 14:50
*/
public static function first($email):array {
public static function first($email, $filed='*'):string {
return "select {$filed} from `".static::$table."` where `".(is_numeric($email) ? 'id' : 'email')."` = '{$email}' limit 1";
}
/**
* 统计邮箱的数量
* @return string
* @author:dc
* @time 2023/2/14 16:16
*/
public static function count():string {
return "select count(*) from `".static::$table."` limit 1";
}
/**
* 邮箱是否存在的sql
* @param string $email
* @return array
* @author:dc
* @time 2023/2/17 10:15
*/
public static function hasEmail(string $email):array {
return [
"select * from `emails` where `email` = ? limit 1",
"select `id` from `".static::$table."` where `email` = ? limit 1",
[
$email
]
... ... @@ -27,14 +51,31 @@ class email {
}
/**
* 统计邮箱的数量
* @return string
* 更新
* @param $data
* @param $where
* @return array
* @author:dc
* @time 2023/2/14 16:16
* @time 2023/2/17 10:24
*/
public static function count():string {
return "select count(*) from `emails` limit 1";
public static function update($data,$where){
return [
"update `".static::$table."` set ". dbUpdate($data)
." where ".(is_numeric($where) ? '`id` = '.$where : dbWhere($where)),
$data
];
}
public function insert(){
}
}
... ...
<?php
use Controller\Home;
return [
'login' => [Home::class,'login']
];
\ No newline at end of file
... ...