Folder.php 7.4 KB
<?php

namespace Controller;


use Lib\Err;
use Lib\Mail\Mail;
use Lib\Mail\MailFun;
use Model\folderSql;
use Model\listsSql;

/**
 * 文件夹管理
 * @author:dc
 * @time 2023/2/18 17:58
 * Class Folder
 * @package Controller
 */
class Folder extends Base {


    /**
     * 邮箱文件夹
     * @author:dc
     * @time 2023/2/18 10:58
     */
    public function lists(){

        try {
            $emails = $this->getEmails();
        }catch (Err $e){
            $emails = [$this->getEmail()];
        }

        $emails = array_column($emails,'email','id');

        // 查询
        $folders = db()->all(
            \Model\folderSql::all(
                array_keys($emails),
                '`id`,`folder`,`pid`,`exsts`,`unseen`,`email_id`'
            )
        );

        $data = [];
        foreach ($emails as $eid=>$email){
            if (empty($data[$email])){
                $data[$email] = [];
            }

            foreach ($folders as $k=>$folder){
                if($folder['email_id'] == $eid){
                    unset($folder['email_id']);
                    // 总数
                    $folder['exsts'] = db()->count(listsSql::listCount(
                        dbWhere(['folder_id'=> $folder['id'], 'deleted'  =>  0])
                    ));
                    if($folder['folder']=='收件箱'){
                        // 未读
                        $folder['unseen'] = db()->count(listsSql::listCount(
                            dbWhere(['folder_id'=> $folder['id'], 'seen'  =>  0, 'deleted'  =>  0])
                        ));
                    }else{
                        $folder['unseen'] = 0;
                    }

                    $data[$email][$k] = $folder;
                }
            }
            // 没有存在数据
            if(!$data[$email]){
                $data[$email][] =   [
                    'folder'    =>  folderAlias('INBOX'),
                    'id'    =>  0,
                    'exsts'    =>  0,
                    'unseen'    =>  0,
                    'pid'   =>  0
                ];
            }
        }

        $folders = $data;$data = null;

        foreach ($folders as $f=>$folder){
            // 转tree
            $folders[$f] = list_to_tree($folder);
        }


        return $folders;
    }




    /**
     * 创建 目录
     * @author:dc
     * @time 2023/2/18 17:56
     */
    public function create(){
        $email = $this->getEmail();

        $formData = app()->request(['pid','folder']);
        // 验证目录 可以输入中文英文数字
        if(empty($formData['folder'])){
            app()->e('folder_create_name_error');
        }

        $formData['folder'] = str_replace(
            ["'",'"','/','\\','&','*','(',')','{','}','|','$','@','!','#','%','^','<','>','?','`','~','[',']'],
            '',
            $formData['folder']
        );

        // 是否是系统的
        if(folderAlias($formData['folder']) !== $formData['folder']){
            app()->e('folder_is_exist');
        }

        // 加密后的文件夹
        $origin_folder = MailFun::folderEncoding($formData['folder']);

        // 修改
        $id = app()->request('id',0,'intval');
        if($id){
            $folder = db()->first(folderSql::first($id));
            if(!$folder || $folder['email_id'] != $email['id']){
                app()->e('folder_not_exist');
            }

        }else{
            // 不为空上级
            if(!empty($formData['pid'])){
                $parent = db()->first(folderSql::first(['id'=>$formData['pid']]));
                if(!$parent || $parent['email_id'] != $email['id']){
                    app()->e('folder_parent_not_fount');
                }
                if($parent['pid']){
                    app()->e('folder_tree_max_two');
                }

                // 加密后的文件夹
                $origin_folder = $parent['origin_folder'].'/'.$origin_folder;
            }
        }

        // 判断文件夹是否存在
        $has = db()->count(folderSql::has(
            [
                'email_id'  =>  $email['id'],
                'origin_folder' =>  $origin_folder
            ]
        ));

        if($has){
            app()->e('folder_is_exist');
        }

        // 远程创建
        $mail = new Mail(
            $email['email'],
            base64_decode($email['password']),
            $email['imap']
        );

        if($mail->login()!=1){
            app()->e('login_error_imap');
        }

        if($id){
            // 远程创建
            $ret = $mail->client->folderRename($folder['origin_folder'],$origin_folder);

        }else{
            // 远程创建
            $ret = $mail->client->folderCreate($origin_folder);
        }

        if($ret[0] === false){
            app()->e($ret[1]);
        }

        if($id){
            // 更新
            db()->update(folderSql::$table,[
                'folder'    =>  $formData['folder'],
                'origin_folder' =>  $origin_folder,
                'uuid' =>  md5($email['id'].$origin_folder),
            ],dbWhere(['id'=>$id]),false);
        }else{
            // 插入数据,就算插入失败了,也会把文件夹同步回来
            $id = db()->insert(folderSql::$table,[
                'pid'   =>  $parent['id']??0,
                'email_id'  =>  $email['id'],
                'folder'    =>  $formData['folder'],
                'origin_folder' =>  $origin_folder,
                'uuid' =>  md5($email['id'].$origin_folder),
            ],false);

        }

//        if(!$id){
//            app()->e('folder_create_save_error');
//        }

        // 查询
        $folders = db()->all(
            \Model\folderSql::all(
                $email['id'],
                '`id`,`folder`,`pid`,`exsts`,`unseen`,`email_id`'
            )
        );


        return list_to_tree($folders);

    }


    /**
     * 改名
     * @author:dc
     * @time 2023/3/3 17:40
     */
    public function rename(){

    }

    /**
     * 删除
     * @author:dc
     * @time 2023/3/3 17:38
     */
    public function delete(){
        $email = $this->getEmail();

        $folder_id = app()->request('id');

        $folder = db()->first(folderSql::first(['id'=>$folder_id,'email_id'=>$email['id']]));

        if(!$folder){
            app()->e('folder_not_fount');
        }
        // 是否是不可删除的
        if(in_array($folder['folder'],array_values(folderAliasMap()))){
            app()->e('folder_not_allowed_to_delete');
        }
        // 是否存在下级
        if(db()->count(folderSql::first(['pid'=>$folder['id'],'email_id'=>$email['id']]))){
            app()->e('folder_delete_exist_child');
        }

        // 是否存在邮件
        if(db()->count(listsSql::first(dbWhere(['folder_id'=>$folder['id'],'email_id'=>$email['id']])))){
            app()->e('folder_delete_exist_mail');
        }

        // 远程创建
        $mail = new Mail(
            $email['email'],
            base64_decode($email['password']),
            $email['imap']
        );

        if($mail->login()!=1){
            app()->e('login_error_imap');
        }

        // 远程创建
        $ret = $mail->client->folderDelete($folder['origin_folder']);

        $row = db()->delete(folderSql::$table,[
            'id' =>  $folder['id']
        ]);

        if(!$row){
            app()->e('folder_delete_error');
        }

        // 返回数据
        app()->_json($folder);
    }





}