<?php
/**
 * Created by PhpStorm.
 * User: chendeben
 * Date: 2018/8/31
 * Time: 下午9:35
 */

namespace Heanup\App\Script\Controller;


use Cloudflare\Api;
use Cloudflare\Zone;
use Cloudflare\Zone\Dns;
use Heanup\App\Api\Package\Database\Mail;
use Heanup\App\Script\Controller;
use Heanup\Library\Curl;
use Heanup\Library\Vestacp;
use ProfiCloS\VestaCP\ClientException;
use ProfiCloS\VestaCP\ProcessException;

class Server extends Controller
{

    /**
     * 执行逻辑
     */
    protected function main()
    {
        $whereString = "status=1 AND expired_time<:expired_time";
        $where = [
            ':expired_time' => date("Y-m-d H:i:s")
        ];
        $emails = Mail::select([], $where, $whereString);
        if ($emails) {
            foreach ($emails as $email) {
                try {
                    Vestacp::getInstance()->getModuleMail("admin")->deleteAccount($email['domain'], strtolower($email['email']));
                    Mail::update(['status'=>0], ['id'=>$email['id']]);
                    echo sprintf("[%s]\t删除账号成功！[%s]\r\n", date("Y-m-d H:i:s"),$email['email'] . "@" . $email['domain']);
                } catch (ClientException $e) {
                } catch (ProcessException $e) {
                }
            }
        }
        $this->checkDomain();


    }
    public function checkDomain(){
        $key = "093fcd440ed453c2689c42150bdb7d5dbc4fb";
//        $zone_identifier = "89a0fe0d09762c557fd0c7799efb1c9a";
//        $client = new Api('chendeben@qq.com', $key);
        $zone=new Zone('chendeben@qq.com', $key);
        $data=$zone->zones();
        $data=json_decode(json_encode($data),true);
        foreach ($data as $item) {
            $domain=$item['name'];
            $url=sprintf("http://www.%s",$domain);
            file_put_contents(APP_DIR."/curl.log", Curl::get($url),FILE_APPEND);
        }
    }

}