@@ -62,27 +62,6 @@ public function upsertOnCloudflare(): void
6262 throw new Exception ('Server has no allocation ' );
6363 }
6464
65- // @phpstan-ignore staticMethod.notFound
66- $ response = Http::cloudflare ()->get ("zones/ {$ this ->domain ->cloudflare_id }/dns_records/ $ this ->cloudflare_id " , ['search ' => $ this ->name ])->json ();
67-
68- if ($ response ['success ' ]) {
69- if (!empty ($ response ['result ' ])) {
70- throw new Exception ('A subdomain with that name already exists ' );
71- }
72- } else {
73- if ($ response ['errors ' ] && count ($ response ['errors ' ]) > 0 ) {
74- throw new Exception ($ response ['errors ' ][0 ]['message ' ]);
75- }
76- }
77-
78- $ payload = [
79- 'name ' => $ this ->name ,
80- 'type ' => $ this ->record_type ,
81- 'comment ' => 'Created by Pelican Subdomains plugin ' ,
82- 'content ' => $ this ->server ->allocation ->ip ,
83- 'proxied ' => false ,
84- ];
85-
8665 if ($ this ->record_type === 'SRV ' ) {
8766 $ srvTarget = $ this ->server ->node ->srv_target ; // @phpstan-ignore property.notFound
8867
@@ -96,8 +75,10 @@ public function upsertOnCloudflare(): void
9675 throw new Exception ('Server has no SRV type ' );
9776 }
9877
78+ $ searchName = "$ srvServiceType ->value . $ this ->name " ;
79+
9980 $ payload = [
100- 'name ' => " $ srvServiceType -> value . $ this -> name " ,
81+ 'name ' => $ searchName ,
10182 'type ' => $ this ->record_type ,
10283 'comment ' => 'Created by Pelican Subdomains plugin ' ,
10384 'data ' => [
@@ -112,6 +93,36 @@ public function upsertOnCloudflare(): void
11293 if ($ this ->server ->allocation ->ip === '0.0.0.0 ' || $ this ->server ->allocation ->ip === ':: ' ) {
11394 throw new Exception ('Server has invalid allocation ip (0.0.0.0 or ::) ' );
11495 }
96+
97+ $ searchName = $ this ->name ;
98+
99+ $ payload = [
100+ 'name ' => $ searchName ,
101+ 'type ' => $ this ->record_type ,
102+ 'comment ' => 'Created by Pelican Subdomains plugin ' ,
103+ 'content ' => $ this ->server ->allocation ->ip ,
104+ 'proxied ' => false ,
105+ ];
106+ }
107+
108+ // @phpstan-ignore staticMethod.notFound
109+ $ searchResponse = Http::cloudflare ()->get ("zones/ {$ this ->domain ->cloudflare_id }/dns_records " , [
110+ 'name ' => $ searchName ,
111+ 'type ' => $ this ->record_type ,
112+ ])->json ();
113+
114+ if ($ searchResponse ['success ' ]) {
115+ $ results = $ searchResponse ['result ' ] ?? [];
116+
117+ foreach ($ results as $ record ) {
118+ if ($ record ['id ' ] !== $ this ->cloudflare_id ) {
119+ throw new Exception ('A subdomain with that name already exists ' );
120+ }
121+ }
122+ } else {
123+ if ($ searchResponse ['errors ' ] && count ($ searchResponse ['errors ' ]) > 0 ) {
124+ throw new Exception ($ searchResponse ['errors ' ][0 ]['message ' ]);
125+ }
115126 }
116127
117128 if ($ this ->cloudflare_id ) {
0 commit comments