@@ -131,6 +131,14 @@ public function getZones()
131131 return $ this ->call ("GET " , "client/v4/zones?per_page= " . self ::ZONES_PER_PAGE . "&status=active " );
132132 }
133133
134+ public function getZoneByName ($ zoneName )
135+ {
136+ return $ this ->call (
137+ "GET " ,
138+ "client/v4/zones?name= " . rawurlencode ($ zoneName ) . "&status=active "
139+ );
140+ }
141+
134142 /**
135143 * @link https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-list-dns-records
136144 * @throws Exception
@@ -341,44 +349,66 @@ function updateDnsRecords()
341349 private function matchHostnameWithZone ($ hostnameList = [])
342350 {
343351 try {
344- $ zoneList = $ this ->cloudflareAPI ->getZones ();
345- $ zoneList = $ zoneList ['result ' ];
346- foreach ($ zoneList as $ zone ) {
347- $ zoneId = $ zone ['id ' ];
348- $ zoneName = $ zone ['name ' ];
349- foreach ($ hostnameList as $ hostname ) {
350- // Check if the hostname ends with the zone name
351- if ($ hostname === $ zoneName || substr ($ hostname , -strlen ('. ' . $ zoneName )) === '. ' . $ zoneName ) {
352- // Add an IPv4 DNS record for each hostname that matches a zone
353- $ this ->dnsRecordList [] = new DnsRecordEntity (
354- '' ,
355- 'A ' ,
356- $ hostname ,
357- $ this ->ipv4 ,
358- $ zoneId ,
359- '' ,
360- ''
361- );
362- if (isset ($ this ->ipv6 )) {
363- // Add an IPv6 DNS record if an IPv6 address is available
364- $ this ->dnsRecordList [] = new DnsRecordEntity (
365- '' ,
366- 'AAAA ' ,
367- $ hostname ,
368- $ this ->ipv6 ,
369- $ zoneId ,
370- '' ,
371- ''
372- );
373- }
374- }
352+ foreach ($ hostnameList as $ hostname ) {
353+ $ zoneId = $ this ->findZoneIdByHostname ($ hostname );
354+
355+ if (!$ zoneId ) {
356+ continue ;
375357 }
358+
359+ $ this ->dnsRecordList [] = new DnsRecordEntity (
360+ '' ,
361+ 'A ' ,
362+ $ hostname ,
363+ $ this ->ipv4 ,
364+ $ zoneId ,
365+ '' ,
366+ ''
367+ );
368+
369+ if (isset ($ this ->ipv6 )) {
370+ $ this ->dnsRecordList [] = new DnsRecordEntity (
371+ '' ,
372+ 'AAAA ' ,
373+ $ hostname ,
374+ $ this ->ipv6 ,
375+ $ zoneId ,
376+ '' ,
377+ ''
378+ );
379+ }
380+ }
381+
382+ if (empty ($ this ->dnsRecordList )) {
383+ $ this ->exitWithSynologyMsg (SynologyOutput::NO_HOSTNAME );
376384 }
377385 } catch (Exception $ e ) {
378386 $ this ->exitWithSynologyMsg (SynologyOutput::NO_HOSTNAME );
379387 }
380388 }
381389
390+ /**
391+ * Summary of findZoneIdByHostname
392+ * @param mixed $hostname
393+ */
394+ private function findZoneIdByHostname ($ hostname )
395+ {
396+ $ hostname = strtolower (rtrim (trim ($ hostname ), '. ' ));
397+ $ labels = explode ('. ' , $ hostname );
398+ $ count = count ($ labels );
399+
400+ for ($ i = 0 ; $ i <= $ count - 2 ; $ i ++) {
401+ $ candidateZone = implode ('. ' , array_slice ($ labels , $ i ));
402+ $ zone = $ this ->cloudflareAPI ->getZoneByName ($ candidateZone );
403+
404+ if (!empty ($ zone ['result ' ]) && !empty ($ zone ['result ' ][0 ]['id ' ])) {
405+ return $ zone ['result ' ][0 ]['id ' ];
406+ }
407+ }
408+
409+ return null ;
410+ }
411+
382412 /**
383413 * Extracts valid hostnames from a given string of hostnames separated by pipes (|).
384414 *
0 commit comments