作者 邓超

1

... ... @@ -10,4 +10,4 @@ include_once __DIR__."/../vendor/autoload.php";
//print_r(db()->value(listsSql::lastMsgno(1,89)));
start_now_mail();
//start_now_mail();
... ...
... ... @@ -298,7 +298,7 @@ switch ($argv[1]){
}
case 'stop':{
\Co\run(function (){
echo "正在退出程序...\n";
echo "正在退出程序...\n非必要请不要强制kill掉进程\n";
redis()->set(RUNNING_REDIS_KEY,'stop');
while (true){
$num = exec("ps -ef | grep \"sync_email.php start\" | grep -v grep | wc -l");
... ...
<?php
namespace Controller;
/**
* 基础控制器,根据需求 可以不继承此类
* @author:dc
* @time 2023/2/18 16:34
* Class Base
* @package Controller
*/
abstract class Base {
}
\ No newline at end of file
... ...
... ... @@ -2,7 +2,6 @@
namespace Controller;
use Lib\Mail\Mail;
use Model\emailSql;
use Model\listsSql;
... ... @@ -14,106 +13,62 @@ use Model\listsSql;
*/
class Home {
/**
* 登录操作
* @return int[]
* @author:dc
* @time 2023/2/13 11:50
* 当前登录的邮箱
* @var array[]
*/
public function login(){
// $mail,$password,$imap,$smtp
$formData = app()->request(['email','password','imap','smtp']);
if(empty($formData['email']) || !preg_match("/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/",$formData['email'])){
app()->e('email_verify_error');
}
private $login_email;
if(empty($formData['password'])){
app()->e('password_verify_error');
}
if(empty($formData['imap'])){
app()->e('imap_verify_error');
}
if(empty($formData['smtp'])){
app()->e('smtp_verify_error');
}
// 进行远程登录,验证
Mail::login($formData['email'],$formData['password'],$formData['imap']);
// 是否存在
$id = db()->value(emailSql::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(emailSql::$table,$data,dbWhere(['id'=>$id]));
}else{
// 新增
$ret = db()->insert(emailSql::$table,$data);
if($ret){
// 增加邮件数量
redis()->incr('email_total');
// 立即处理 同步
redis()->lPush('sync_email_lists', $ret);
}
}
if($ret){
app()->_json([
'token' => $data['token']
]);
/**
* 构造
* Home constructor.
*/
public function __construct()
{
// 获取 当前请求的 邮箱
$this->login_email = db()->first(emailSql::firstByToken(token()));
if(!$this->login_email){
app()->e('token_verify_notfound');
}
app()->e('login_error');
}
/**
* 邮件列表
* @author:dc
* @time 2023/2/17 14:12
*/
public function lists(){
$email = db()->first(emailSql::firstByToken(token()));
if(!$email){
app()->e('token_verify_notfound');
}
// 分页 页数
$page = app()->request('page',1,'intval');
$page = $page ? $page : 1;
$lists = db()->all(listsSql::lists($email['id'],$page));
$lists = db()->all(listsSql::lists($this->login_email['id'],$page));
app()->_json(listsPage($lists,100,1,30));
}
/**
* 邮箱文件夹
* @author:dc
* @time 2023/2/18 10:58
*/
public function folder(){
// 查询
$folders = db()->all(
\Model\folderSql::all(
$this->login_email['id'],
'`id`,`folder`,`pid`'
)
);
// 转tree
$folders = list_to_tree($folders);
app()->_json($folders);
}
... ...
<?php
namespace Controller;
use Lib\Mail\Mail;
use Model\emailSql;
/**
* 登录
* @author:dc
* @time 2023/2/18 16:49
* Class Login
* @package Controller
*/
class Login {
/**
* 登录操作
* @return int[]
* @author:dc
* @time 2023/2/13 11:50
*/
public function login(){
// $mail,$password,$imap,$smtp
$formData = app()->request(['email','password','imap','smtp']);
if(empty($formData['email']) || !preg_match("/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/",$formData['email'])){
app()->e('email_verify_error');
}
if(empty($formData['password'])){
app()->e('password_verify_error');
}
if(empty($formData['imap'])){
app()->e('imap_verify_error');
}
if(empty($formData['smtp'])){
app()->e('smtp_verify_error');
}
// 进行远程登录,验证
(new Mail())->login($formData['email'],$formData['password'],$formData['imap']);
// 是否存在
$id = db()->value(emailSql::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(emailSql::$table,$data,dbWhere(['id'=>$id]));
}else{
// 新增
$ret = db()->insert(emailSql::$table,$data);
if($ret){
// 增加邮件数量
redis()->incr('email_total');
// 立即处理 同步
redis()->lPush('sync_email_lists', $ret);
}
}
if($ret){
app()->_json([
'token' => $data['token']
]);
}
app()->e('login_error');
}
}
... ...
<?php
use Model\emailSql;
/**
* 进程pid
... ...
<?php
namespace Helper\Mail;
namespace Lib\Mail;
use Helper\Mail\MailFun;
use Lib\Mail\MailFun;
/**
* 解析邮件body内容
... ...
<?php
namespace Helper\Mail;
namespace Lib\Mail;
/**
* 解码邮件内容
... ...
... ... @@ -20,8 +20,8 @@ class folderSql {
* @author:dc
* @time 2023/2/18 9:22
*/
public static function all(int $email_id):string {
return "select `id`,`folder`,`pid`,`origin_folder`,`last_sync_time`,`folder` from `".static::$table."` where `email_id` = {$email_id} order by `id` asc";
public static function all(int $email_id, $field = '`id`,`folder`,`pid`,`origin_folder`,`last_sync_time`'):string {
return "select {$field} from `".static::$table."` where `email_id` = {$email_id} order by `id` asc";
}
... ...
<?php
use Controller\Home;
/**
* todo::只有post请求
*/
return [
/** 登录操作 @see Home::login() **/
'login' => [Home::class,'login'],
/**
* 登录操作
* @see \Controller\Login::login()
* @param string email 邮箱
* @param string password 密码
* @param string imap imap服务器地址
* @param string smtp smtp服务器地址
*/
'login' => [\Controller\Login::class,'login'],
/** 邮件列表 @see Home::lists() **/
'mail/list' => [Home::class,'lists'],
/**
* 邮件列表
* @see \Controller\Home::lists()
* @param string _token_ token登录凭证
* @param int page 当前页数
*/
'mail/list' => [\Controller\Home::class,'lists'],
/** 邮件文件夹 @see Home::folder() **/
'mail/folder' => [Home::class,'folder'],
/**
* 邮件文件夹
* @see \Controller\Home::folder()
* @param string _token_ token登录凭证
*/
'mail/folder' => [\Controller\Home::class,'folder'],
];
\ No newline at end of file
... ...