作者 邓超

1

... ... @@ -115,20 +115,14 @@ function sync($email_id,$worker_id){
return 1;
}
$mailServer = new Lib\Mail\Mail();
$mailServer = new Lib\Mail\Mail($email['email'],base64_decode($email['password']),$email['imap']);
try {
// 登录服务器
$mailServer->login($email['email'],base64_decode($email['password']),$email['imap']);
}catch (Throwable $e){
if($e->getCode() == 403){
// 登录失败了 ,
db()->update(\Model\emailSql::$table,['pwd_error'=>1],dbWhere(['id'=>$email_id]));
}
if(!$mailServer->login()){
return 2;
}
// 同步文件夹
$mailServer->syncFolder($email_id,db());
... ...
... ... @@ -239,21 +239,13 @@ function sync($worker_id=0){
return 1;
}
$mailServer = new Lib\Mail\Mail();
try {
// 登录服务器
$mailServer->login($email['email'],base64_decode($email['password']),$email['imap']);
}catch (Throwable $e){
if($e->getCode() == 403){
// 登录失败了 ,
db()->update(\Model\emailSql::$table,['pwd_error'=>1],dbWhere(['id'=>$id]));
}
$mailServer = new Lib\Mail\Mail($email['email'],base64_decode($email['password']),$email['imap']);
// 登录服务器
if(!$mailServer->login()){
return 2;
}
// 文件夹间隔1天同步一次
if(empty($email['last_sync_time']) || time() > $email['last_sync_time']+86400){
// 同步文件夹
... ...
... ... @@ -7,6 +7,8 @@
"php": "^8.0.2",
"ext-pdo": "*",
"ext-redis": "*",
"ext-mbstring": "*",
"ext-json": "*",
"phpmailer/phpmailer": "^6.7"
},
"require-dev": {
... ...
... ... @@ -37,7 +37,4 @@ define('APP_DEBUG',true);
// 创建 必须 目录
if(!is_dir(LOG_PATH)){
mkdir(LOG_PATH,0777,true);
}
... ...
... ... @@ -46,15 +46,17 @@ class Folder extends Base {
* @time 2023/2/18 17:56
*/
public function create(){
$email = $this->getEmail('id');
$formData = app()->request(['pid','folder']);
// 验证目录 可以输入中文英文数字
if(empty($formData['folder']) || !preg_match("/^[\u4E00-\u9FA5A-Za-z0-9_]+$/",$formData['folder'])){
if(empty($formData['folder'])){
app()->e('folder_create_name_error');
}
// 不为空上级
if(!empty($formData['pid'])){
$parent = db()->first(folderSql::first(['id'=>$formData['pid']]));
if(!$parent || $parent['email_id'] != $this->login_email['id']){
if(!$parent || $parent['email_id'] != $email['id']){
app()->e('folder_parent_not_fount');
}
if($parent['pid']){
... ... @@ -63,11 +65,13 @@ class Folder extends Base {
}
$formData['folder'] = str_replace(["'",'"','/','\\','&','*','(',')'],'',$formData['folder']);
// 判断文件夹是否存在
$has = db()->count(folderSql::has(
[
'email_id' => $this->login_email['id'],
'origin_folder' => $formData['folder']
'email_id' => $email['id'],
'origin_folder' => MailFun::folderEncoding($formData['folder'])
]
));
... ... @@ -76,8 +80,19 @@ class Folder extends Base {
}
// 远程创建
$mail = new Mail();
$mail->login($this->login_email['email'],$this->login_email['password'],$this->login_email['imap']);
$mail = new Mail(
$email['email'],
$email['password'],
$email['imap']
);
if(!$mail->login()){
app()->e('login_error_imap');
}
$folder = $mail->client->folderCreate($formData['folder']);
... ...
... ... @@ -44,14 +44,14 @@ class Login {
}
// 进行远程登录,验证
try{
(new Mail())->login($formData['email'],$formData['password'],$formData['imap']);
}catch (\Exception $e){
if($e->getCode()==403){
app()->e('login_error_imap');
}
$login = (new Mail($formData['email'],$formData['password'],$formData['imap']))->login(false);
if( $login === 0){
app()->e('login_error_imap');
}elseif ($login === -1){
app()->e('server_error');
}
// 是否存在
$id = db()->value(emailSql::hasEmail($formData['email']));
... ...
... ... @@ -50,16 +50,7 @@ function db():\Lib\DbPool{
*/
function logs($message,$filename=null){
$filename = $filename ? $filename : LOG_PATH.'/error.log';
if(!is_dir(dirname($filename))){
@mkdir(dirname($filename),0755,true);
}
@file_put_contents(
$filename,
date('Y-m-d H:i:s ').print_r($message,true).PHP_EOL,
FILE_APPEND
);
\Lib\Log::append($message, $filename);
}
... ...
... ... @@ -299,19 +299,12 @@ class App {
*/
public static function end()
{
// 这里可以做其他事
$app = self::instance();
/**
* 这里写
*/
// end code
$app = self::instance();
// 记录日志
$filename = LOG_PATH.'/'.$app->nowDate().'.log';
if($last_error = error_get_last()){
logs(print_r($last_error,true), $filename);
logs(print_r($last_error,true));
$data['error_message'] = $last_error['message'];
$data['status'] = 502;
... ... @@ -321,15 +314,15 @@ class App {
}else{
if($app->getError()){
logs(
is_string($app->getError()) ? $app->getError() : implode(PHP_EOL,$app->getError()),
$filename
is_string($app->getError()) ? $app->getError() : implode(PHP_EOL,$app->getError())
);
}
self::echo($app->data);
}
// 日志记录
Log::getInstance()->write();
// header("Content-Type:text/event-stream;Charset=UTF-8;");
... ...
... ... @@ -67,11 +67,14 @@ class DbPool {
}
$query = $this->client->prepare($sql);
// todo:: 记录日志,生产请注释
logs(
$params ? [$sql,$params] : $sql,
LOG_PATH.'/sql.log'
);
if(APP_DEBUG){
// todo:: 记录日志,生产请注释
logs(
$params ? [$sql,$params] : $sql,
LOG_PATH.'/sql.log'
);
}
if($query->execute($params)){
return $query;
... ...
<?php
namespace Lib;
/**
* 日志文件
* @author:dc
* @time 2023/3/14 10:26
* Class Log
* @package Lib
*/
class Log {
/**
* @var array
*/
public array $filename = [];
/**
* @var array
*/
public array $message = [];
/**
* @var self
*/
private static self $instance;
/**
* Log constructor.
*/
public function __construct()
{
$this->filename['default'] = LOG_PATH.'/'.app()->nowDate().'.error.log';
}
/**
* 追加日志内容
* @param string $message
* @author:dc
* @time 2023/3/14 10:45
*/
public static function append(string $message, $filename = null){
if($filename){
self::getInstance()->setFilename($filename);
}
self::getInstance()->message[md5($filename)][] = $message;
}
/**
* @return Log
*/
public static function getInstance(): Log
{
if (empty(static::$instance)){
static::$instance = new Log();
}
return self::$instance;
}
/**
* 设置日志文件
* @param $filename
* @author:dc
* @time 2023/3/14 11:11
*/
private function setFilename($filename){
// 创建目录
if(!is_dir(dirname($filename))){
@mkdir(dirname($filename),0755,true);
}
$this->filename[md5($filename)] = $filename;
}
/**
* 写入日志
* @author:dc
* @time 2023/3/14 10:45
*/
public function write(){
foreach ($this->filename as $key=>$fn){
if(!empty($this->message[$key])){
array_unshift($this->message[$key],app()->nowDateTime());
$this->message[$key][] = PHP_EOL;
@file_put_contents(
$fn,
implode(PHP_EOL."\t",$this->message[$key]),
FILE_APPEND
);
$this->message[$key] = [];
}
}
}
}
... ...
... ... @@ -437,10 +437,10 @@ class Imap {
*/
public function folderCreate(string $folder):string {
// 需要转码
$folder = mb_convert_encoding($folder,'UTF7-IMAP','UTF-8');
$folder = MailFun::folderEncoding($folder);
// A003 CREATE owatagusiam 顶级
// A003 CREATE owatagusiam/owatagusiam2 有上下级关系的文件夹
$res = $this->request('CREATE '.$folder);
$res = $this->request("CREATE \"{$folder}\"");
if ($res[0] == 'ok'){
return $folder;
... ...
... ... @@ -21,23 +21,60 @@ class Mail {
*/
public Imap $client;
/**
* @var string
*/
private string $username;
/**
* 登录imap服务器
* @var string
*/
private string $password;
/**
* @var string
*/
private string $server;
/**
* Mail constructor.
* @param string $email
* @param string $password
* @param string $imap
*/
public function __construct(string $email,string $password,string $imap)
{
$this->username = $email;
$this->password = $password;
$this->server = $imap;
}
/**
* 登录imap服务器
* @param bool $pass_err
* @return int
* @author:dc
* @time 2023/2/5 10:46
* @time 2023/3/14 10:03
*/
public function login(string $email,string $password,string $imap) {
public function login($pass_err=true):int {
$this->client = new Imap();
// 是否初始成功
$this->client->login("ssl://{$imap}:993",$email,$password);
try {
// 是否初始成功
$this->client->login("ssl://{$this->server}:993",$this->username,$this->password);
}catch (\Throwable $e){
if($pass_err && $e->getCode() == 403){
// 登录失败了 ,
db()->update(\Model\emailSql::$table,['pwd_error'=>1],dbWhere(['email'=>$this->username]));
}else{
logs($e->getMessage());
}
return true;
return $e->getCode() == 403 ? 0 : -1;
}
return 1;
}
... ...
... ... @@ -201,5 +201,15 @@ class MailFun {
}
/**
* 文件夹名 编译
* @param $folder
* @return string
* @author:dc
* @time 2023/3/14 9:37
*/
public static function folderEncoding($folder):string {
return mb_convert_encoding($folder,'UTF7-IMAP','UTF-8');
}
}
... ...