1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/Ninja/DNS/Cloudflare.php
David Bomba 19881bd41f
Cloudflare DNS Zone Updates (#1733)
* Add subdomain to cloudflare

* Fire subdomain event when saving client portal settings

* Refactor to include zones

* JSON decode to array

* Proxy requests
2017-11-06 20:58:53 +11:00

50 lines
1.4 KiB
PHP

<?php
namespace App\Ninja\DNS;
use App\Libraries\Utils;
use App\Models\Account;
class Cloudflare
{
public static function addDNSRecord(Account $account){
$zones = json_decode(env('CLOUDFLARE_ZONE_IDS',''), true);
foreach($zones as $zone)
{
$curl = curl_init();
$jsonEncodedData = json_encode(['type'=>'A', 'name'=>$account->subdomain, 'content'=>env('CLOUDFLARE_TARGET_IP_ADDRESS',''),'proxied'=>true]);
$opts = [
CURLOPT_URL => 'https://api.cloudflare.com/client/v4/zones/'.$zone.'/dns_records',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $jsonEncodedData,
CURLOPT_HTTPHEADER => [ 'Content-Type: application/json',
'Content-Length: '.strlen($jsonEncodedData),
'X-Auth-Email: '.env('CLOUDFLARE_EMAIL', ''),
'X-Auth-Key: '.env('CLOUDFLARE_API_KEY', '')
],
];
curl_setopt_array($curl, $opts);
$result = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($status != 200)
Utils::logError('unable to update subdomain ' . $account->subdomain . ' @ Cloudflare - '.$result);
}
}
}