<?php

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}

class cloudflare
{

    /**
     * 构造函数
     *
     * @access  public
     * @param   string       $table       数据库表名
     * @param   dbobject     $db          aodb的对象
     * @param   string       $id          数据表主键字段名
     * @param   string       $name        数据表重要段名
     *
     * @return void
     */
    function __construct()
    {
		global $_CFG;
		
        $this->domain    = $_CFG['cf_domain'];
        $this->zoneid    = $_CFG['cf_site_id'];
        $this->apikey    = $_CFG['cf_api_key'];
        $this->apiemail  = $_CFG['cf_email'];
        $this->apiip  	 = $_CFG['cf_ip'];
        $this->error_msg = '';
    }
    
    function add_domain($dm){
		$real_param =[ 
			'type' 			=> 'A',
			'name' 			=> $dm,
			'content' 		=> $this->apiip,
			'ttl' 			=> 1,
			'priority' 		=> 0,
			'proxied' 		=> true,
		];

		$header[] = 'X-Auth-Key: '.$this->apikey;
		$header[] = 'X-Auth-Email: '.$this->apiemail ;
		$header[] = 'Content-Type: application/json ' ;

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/zones/'.$this->zoneid.'/dns_records');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($real_param));
		
		$t_result = curl_exec($ch);
		curl_close($ch);
		$result=json_decode($t_result,true);
		if($result['success']){
			return $result['result']['id'];
		}
		print_r($t_result);
		return false;
    	    	
    }

    function del_domain($iden){

		$header[] = 'X-Auth-Key: '.$this->apikey;
		$header[] = 'X-Auth-Email: '.$this->apiemail ;
		$header[] = 'Content-Type: application/json ' ;

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/zones/'.$this->zoneid.'/dns_records/'.$iden);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
 		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		
		$t_result = curl_exec($ch);
		curl_close($ch);
		$result=json_decode($t_result,true);
		if($result['success']){
			return true;
		}
		return false;
    	    	
    }

    function update_domain($iden ,$dm){
		$real_param =[ 
			'type' 			=> 'A',
			'name' 			=> $dm,
			'content' 		=> $this->apiip,
			'ttl' 			=> 1,
			'proxied' 		=> true,
		];

		
		$header[] = 'X-Auth-Key: '.$this->apikey;
		$header[] = 'X-Auth-Email: '.$this->apiemail ;
		$header[] = 'Content-Type: application/json ' ;

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/zones/'.$this->zoneid.'/dns_records/'.$iden);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
 		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($real_param));
		
		$t_result = curl_exec($ch);
		curl_close($ch);
		$result=json_decode($t_result,true);
		if($result['success']){
			return true;
		}
		return false;
    	    	
    }

    function get_record($name){

		$header[] = 'X-Auth-Key: '.$this->apikey;
		$header[] = 'X-Auth-Email: '.$this->apiemail ;
		$header[] = 'Content-Type: application/json ' ;

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/zones/'.$this->zoneid.'/dns_records');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		
		$t_result = curl_exec($ch);
		curl_close($ch);
		$result=json_decode($t_result,true);
		print_r($result);
		foreach($result['result'] as $key=>$val){
			if($val['name']==$name.'.'.$this->domain){
				$id=$val['id'];
				break;
			}
		}
		if(strlen($id)>0){
			return $id;			
		}
		return false;
    	    	
    }

}

?>
