<?php namespace Lib\Imap\Request; use Lib\Imap\Imap; use Lib\Imap\Parse\MessageItem; /** * 登录 * @author:dc * @time 2024/9/13 17:09 * Class Login * @package Lib\Imap\Request */ class Folder extends Request{ protected string $folder; /** * 未读数量 有的邮箱是不会返回未读数量的 * @var int */ protected int $unseen = 0; /** * 总数 * @var int */ protected int $total = 0; /** * 最近邮件的数量 * @var int */ protected int $recent = 0; /** * 标签 * @var array */ protected array $flags = []; /** * 是否初始了,就是是否select 文件夹 * @var bool */ private bool $isInit = false; /** * Folder constructor. * @param Imap $imap * @param string $folder */ public function __construct(Imap $imap, string $folder) { parent::__construct($imap); $this->folder = $folder; } /** * 文件夹名称 * @return string * @author:dc * @time 2024/9/26 11:06 */ public function getName(){ return $this->folder; } public function exec(): static { if(!$this->imap->checkedFolder($this->folder)){ $this->cmd('SELECT "%s"',$this->folder); // ["* 128 EXISTS\r\n","* 2 RECENT\r\n","* OK [UIDVALIDITY 1] UIDs valid\r\n","* FLAGS (\\Answered \\Seen \\Deleted \\Draft \\Flagged)\r\n","* OK [PERMANENTFLAGS (\\Answered \\Seen \\Deleted \\Draft \\Flagged)] Limited\r\n","TAG3 OK [READ-WRITE] SELECT completed\r\n"] if($this->isOk()){ $this->isInit = true; foreach ($this->result as $item){ $item = trim($item); // 总数量 if(preg_match("/^\* (\d+) EXISTS$/i",$item,$m)){ $this->total = (int) $m[1]; } // 最近的 elseif (preg_match("/^\* (\d+) RECENT$/i",$item,$m)){ $this->recent = (int) $m[1]; } // 未读 elseif (preg_match("/^\*.*\[UNSEEN (\d+)\]/i",$item,$m)){ $this->unseen = (int) $m[1]; } // tag elseif (preg_match("/^\* FLAGS \((.*)\)$/i",$item,$m)){ $this->flags = explode(' ',$m[1]); } } // 设置 $this->imap->setCheckedFolder($this->folder); } } return $this; } /** * 邮件列表 * @return Msg * @author:dc * @time 2024/9/14 11:36 */ public function msg():Msg { return new Msg($this->imap,$this); } /** * 当前文件夹 可以使用的标签 * @return array */ public function getFlags(): array { return $this->getData('flags'); } /** * 最近收到的邮件 数量 * @return int */ public function getRecent(): int { return $this->getData('recent'); } /** * 当前文件夹有多少未读数量 * @return int */ public function getUnseen(): int { return $this->getData('unseen'); } /** * 读取 文件夹 下面有多少 邮件 总数 * @return int */ public function getTotal(): int { return $this->getData('total'); } /** * 读取文件夹属性 * @param string $name * @return int|array * @author:dc * @time 2024/9/24 9:20 */ public function getData(string $name=''):int|array { if(!$this->isInit){ $this->exec(); } switch ($name){ case 'total':{return $this->total;} case 'unseen':{return $this->unseen;} case 'recent':{return $this->recent;} case 'flags':{return $this->flags;} default:{ return [ 'total'=>$this->total, 'unseen'=>$this->unseen, 'recent'=> $this->recent, 'flags'=> $this->flags ]; } } } /** * 关闭当前目录 一般情况用不到 * @return bool * @author:dc * @time 2024/9/19 14:04 */ public function close():bool { // 必须初始了 并且在当前文件夹下面 if($this->isInit && $this->imap->checkedFolder($this->folder)){ $this->cmd('CLOSE'); return $this->isOk(); } return true; } /** * 创建目录 * @return bool * @author:dc * @time 2024/9/20 13:43 */ public function create():bool { $this->cmd('CREATE "%s"',$this->folder); return $this->isOk(); } /** * 修改目录 * @param string $newFolder 新的文件夹名称 * @param bool $is_utf7 是否是utf7的编码 * @return bool * @author:dc * @time 2024/9/20 13:47 */ public function rename(string $newFolder, bool $is_utf7 = true):bool { // 需要转码 if(!$is_utf7) $newFolder = mb_convert_encoding($newFolder,'UTF7-IMAP','UTF-8'); // RENAME oldfolder newfolder $this->cmd('RENAME "%s" "%s"',$this->folder,$newFolder); return $this->isOk(); } /** * 删除 目录 * @return bool * @author:dc * @time 2024/9/20 13:50 */ public function delete():bool { $this->cmd('DELETE "%s"',$this->folder); return $this->isOk(); } /** * 清除 已标记为删除的邮件 不可逆 * @return bool * @author:dc * @time 2024/3/14 14:12 */ public function expunge():bool { return (new Expunge($this->imap))->isOk(); } /** * 订阅 * @return bool * @author:dc * @time 2024/9/24 10:40 */ public function subscribe(){ $this->cmd('SUBSCRIBE "%s"',$this->folder); return $this->isOk(); } /** * 取消订阅 * @return bool * @author:dc * @time 2024/9/24 10:40 */ public function unsubscribe(){ $this->cmd('UNSUBSCRIBE "%s"',$this->folder); return $this->isOk(); } }