作者 邓超

1

... ... @@ -33,7 +33,7 @@ define('DB_PASSWORD','pKnXKwhAFRpwcZAM');
// 调试
define('APP_DEBUG',false);
define('APP_DEBUG',true);
... ...
... ... @@ -14,26 +14,30 @@ use Model\emailSql;
*/
abstract class Base {
/**
* 当前登录的邮箱
* @var array[]
*/
protected $login_email;
/**
* 构造
* Home constructor.
* @param string $filed
* @return mixed|null
* @throws \Lib\Err
* @author:dc
* @time 2023/3/10 15:15
*/
public function __construct()
{
// 获取 当前请求的 邮箱
$this->login_email = db()->first(emailSql::firstByToken(token()));
if(!$this->login_email){
app()->e('token_verify_notfound');
protected final function getEmails($filed='*'){
static $data;
if(empty($data)){
$data = db()->all(emailSql::all(web_request_emails()));
if(empty($data)){
app()->e('email_request_required');
}
}
}
if($filed == '*'){
return $data;
}
return array_column($data,$filed);
}
... ...
... ... @@ -27,9 +27,30 @@ class Home extends Base {
$page = app()->request('page',1,'intval');
$page = $page ? $page : 1;
$lists = db()->all(listsSql::lists($this->login_email['id'],$page));
app()->_json(listsPage($lists,100,1,30));
$size = app()->request('size',20,'intval');
$size = $size ? $size : 1;
// 邮件文件夹的id
$folder_id = app()->request('folder_id');
$lists = db()->all(
listsSql::lists(
$this->getEmails('id'),
$page,
$size,
(int) $folder_id
)
);
// 总数
$total = db()->count(
listsSql::listCount(
$this->getEmails('id'),
(int) $folder_id
)
);
app()->_json(listsPage($lists,$total,$page,$size));
}
... ... @@ -57,29 +78,8 @@ class Home extends Base {
* @time 2023/3/10 10:38
*/
public function sync(){
$emails = app()->request('emails');
if (empty($emails)){
$tokens = app()->request('tokens');
$tokens = is_array($tokens) ? $tokens : [$tokens];
foreach ($tokens as $k=>$token){
if(!preg_match('/^[0-9a-zA-Z]{32}$/',$token)){
unset($tokens[$k]);
}
}
if(!empty($tokens)){
$where['token'] = array_values($tokens);
}
}else{
$emails = is_array($emails) ? $emails : [$emails];
foreach ($emails as $k=>$email){
if(!preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',$email)){
unset($emails[$k]);
}
}
if(!empty($emails)){
$where['email'] = array_values($emails);
}
}
$where['email'] = web_request_emails();
if(empty($where)){
app()->e('sync_request_param_error');
... ...
... ... @@ -61,8 +61,6 @@ class Login {
'smtp' => $formData['smtp'],
'status' => 1,
'pwd_error' => 0,
'name' => '',
'token' => md5($formData['email'].$formData['password'].$formData['imap']),
];
if($id){
... ...
... ... @@ -178,18 +178,26 @@ function dbUpdate(array $data):string {
/**
* 获取token
* @return string
* 获取前端提交过来的邮箱
* @return array
* @throws \Lib\Err
* @author:dc
* @time 2023/2/17 14:24
* @time 2023/3/10 14:57
*/
function token():string {
$toekn = app()->request('_token_');
function web_request_emails():array {
$emails = app()->request('emails');
$emails = is_array($emails) ? $emails : [$emails];
foreach ($emails as $k=>$email){
if(!preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',$email)){
unset($emails[$k]);
}
}
if(!preg_match("/^[a-z0-9]{32}$/",$toekn)){
app()->e('token_verify_error');
if(empty($emails)){
app()->e('email_request_required');
}
return $toekn;
return array_values($emails);
}
... ...
... ... @@ -9,6 +9,7 @@ return [
'route_not_found' => '页面不存在',
'email_verify_error' => '邮箱验证失败',
'email_request_required' => '邮箱必须',
'password_verify_error' => '密码必须',
'imap_verify_error' => 'imap服务器地址错误',
'smtp_verify_error' => 'smtp服务器地址错误',
... ...
... ... @@ -24,20 +24,6 @@ class emailSql {
}
/**
* @param $token
* @return array
* @author:dc
* @time 2023/2/17 14:21
*/
public static function firstByToken($token):array {
return [
"select * from `".static::$table."` where `token` = ? limit 1",
[
$token
]
];
}
/**
* 统计邮箱的数量
... ... @@ -79,6 +65,17 @@ class emailSql {
}
/**
* 通过邮箱查询列表
* @param $emails
* @return string
* @author:dc
* @time 2023/3/10 15:02
*/
public static function all($emails){
return "select * from ".static::$table." where ".dbWhere(['email'=>$emails]);
}
}
... ...
... ... @@ -20,20 +20,37 @@ class listsSql {
/**
* 查询列表
* @param $email_id
* @param $p
* @param int|array $email_id
* @param int $p
* @param int $size
* @param int $folder_id
* @return string
* @author:dc
* @time 2023/2/17 14:42
* @time 2023/3/10 15:24
*/
public static function lists($email_id, $p, $folder_id = null){
public static function lists(int|array $email_id, int $p, int $size, int $folder_id = 0){
$where = ['email_id'=>$email_id];
if($folder_id) $where['folder_id'] = $folder_id;
return "select * from `".static::$table."` where ".dbWhere($where)." order by `udate` desc limit 30 offset ".(($p-1)*30);
$filed = '`id`,`uid`,`msgno`,`subject`,`from`,`to`,`date`,`size`,`recent`,`flagged`,`answered`,`deleted`,`seen`,`draft`,`udate`,`folder_id`,`is_file`,`cc`,`bcc`';
return "select {$filed} from `".static::$table."` where ".dbWhere($where)." order by `udate` desc limit {$size} offset ".(($p-1)*30);
}
/**
* 统计列表
* @param int|array $email_id
* @param int $folder_id
* @return string
* @author:dc
* @time 2023/3/10 15:26
*/
public static function listCount(int|array $email_id, int $folder_id = 0){
$where = ['email_id'=>$email_id];
if($folder_id) $where['folder_id'] = $folder_id;
return "select count(*) from `".static::$table."` where ".dbWhere($where);
}
/**
... ...
... ... @@ -13,65 +13,21 @@ return [
'/' => [\Controller\Test::class, 'a'],
't' => [\Controller\Test::class, 'home'],
/**
* 登录操作
* @see \Controller\Login::login()
* @param string email 邮箱
* @param string password 密码
* @param string imap imap服务器地址
* @param string smtp smtp服务器地址
*/
// 登录操作
'login' => [\Controller\Login::class, 'login'],
/**
* 邮件列表
* @see \Controller\Home::lists()
* @param string _token_ token登录凭证
* @param int page 当前页数
*/
'list' => [\Controller\Home::class, 'lists'],
/**
* 邮件文件夹
* @see \Controller\Folder::lists()
* @param string _token_ token登录凭证
*/
// 邮件列表
'lists' => [\Controller\Home::class, 'lists'],
// 邮件文件夹
'folder' => [\Controller\Folder::class, 'lists'],
/**
* 邮件/创建文件夹
* @see \Controller\Folder::create()
* @param string _token_ token登录凭证
*/
// 创建文件夹
'folder/create' => [\Controller\Folder::class, 'create'],
/**
* 邮件/重命名文件夹
* @see \Controller\Folder::rename()
* @param string _token_ token登录凭证
*/
// 重命名文件夹
'folder/rename' => [\Controller\Folder::class, 'rename'],
/**
* 邮件/删除文件夹
* @see \Controller\Folder::delete()
* @param string _token_ token登录凭证
*/
// 删除文件夹
'folder/delete' => [\Controller\Folder::class, 'delete'],
/**
* 发送邮件
* @see \Controller\Home::send_mail()
* @param string _token_ token登录凭证
*/
// 发送邮件
'send' => [\Controller\Home::class, 'send_mail'],
/**
* 同步请求
* @see \Controller\Home::sync()
* @param string _token_ token登录凭证
*/
// 同步请求
'sync' => [\Controller\Home::class, 'sync'],
... ...