Advertisement
koki2000

CloudFlare library

Feb 22nd, 2021
1,550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.27 KB | None | 0 0
  1. <?php
  2. require(__DIR__ . '/vendor/autoload.php');
  3. class MycloudFlare {
  4.    
  5.    
  6.     private $key = null;
  7.     private $adapter = null;
  8.     private $user = null;
  9.     private $zones = null;
  10.  
  11.     public function __construct()
  12.     {
  13.         $this->key = new \Cloudflare\API\Auth\APIKey('loginemail', 'apitoken');
  14.         $this->adapter = new Cloudflare\API\Adapter\Guzzle($this->key);
  15.         $this->user = new \Cloudflare\API\Endpoints\User($this->adapter);
  16.         $this->zones = new \Cloudflare\API\Endpoints\Zones($this->adapter);
  17.     }
  18.  
  19.     public function getUserID()
  20.     {
  21.         return $this->user->getUserID();
  22.     }
  23.  
  24.  
  25. /**
  26.  * Imported methods from other project;
  27.  */
  28.     public function listzones()
  29.     {
  30.         $zones = array();
  31.         foreach ($this->zones->listZones()->result as $zone) {
  32.             $zones[$zone->name] = $zone->plan->name;
  33.         }
  34.         return $zones;
  35.     }
  36.  
  37.     /**
  38.      * return string
  39.      * ex: Cache purge for zone: successful | failed
  40.      */
  41.  
  42.     public function cachepurge()
  43.     {
  44.         $resultdata = array();
  45.  
  46.         foreach ($this->zones->listZones()->result as $zone) {
  47.             //echo "Cache purge for " . $zone->name . ": ";
  48.             $resultdata[ $this->zones->cachePurgeEverything($zone->id) == true ? "successful" : "failed"][] = $zone->name;
  49.             return $resultdata;
  50.         }
  51.     }
  52.  
  53.     /**
  54.      * Creating Page Rules (kimaradt, nézz utána)
  55.      */
  56.    
  57.     /**
  58.      * add dns records
  59.      * param string zoneid ex: zone
  60.      * param string TYPE ex:A, CNAME
  61.      * param string name (subdomain name)
  62.      * param string content ex: zone
  63.      * param int TTL 0|1
  64.      * param bool proxied
  65.      */
  66.  
  67.     public function addrecord($zone_id = '', $type = '', $name = '', $content = '', $ttl = 1, $proxied = true)
  68.     {
  69.         $resultdata = array();
  70.         $zoneID = $this->zones->getZoneID($zone_id);
  71.         $dns = new \Cloudflare\API\Endpoints\DNS($this->adapter);
  72.         $resultdata[ $dns->addRecord($zoneID, $type, $name, $content, $ttl, $proxied)? "successful" : "failed"][] = $name.$zone_id;
  73.         return $resultdata;
  74.     }
  75.  
  76.     /**
  77.      * Return string  (record identification)
  78.      * param string domain
  79.      * param type ex: A, CNAME, TXT
  80.      * return string
  81.      */
  82.     public function getrecordid(string $zone_id = '', string $type = '', string $name = '')
  83.     {
  84.         $zoneID = $this->zones->getZoneID($zone_id, $type, $name);
  85.  
  86.         $dns = new \Cloudflare\API\Endpoints\DNS($this->adapter);
  87.  
  88.         foreach ($dns->listRecords($zoneID, $type, $name.'.'.$zone_id)->result as $record)
  89.         {
  90.             return $record->id;
  91.         }
  92.     }
  93.  
  94.     public function delrecord($zone_id = '', $recordID = '')
  95.     {
  96.         $zoneID = $this->zones->getZoneID($zone_id);
  97.         $recordID = $this->getrecordid();
  98.  
  99.         try {
  100.             $user = $this->adapter->delete('zones/' . $zoneID . '/dns_records/' . $recordID);
  101.  
  102.             $this->body = json_decode($user->getBody());
  103.  
  104.             if (isset($this->body->result->id)) {
  105.                 echo "TÖRÖLVE";
  106.                 return true;
  107.             }
  108.             echo "Nincs törölve";
  109.  
  110.             return false;
  111.         } catch (Exception $e) {
  112.             echo $e->getmessage();
  113.         }
  114.     }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement