success = false; $j->message = 'You must provide the domain you wish to use for the "FQDN" setting.'; CREATE_JSON_RESPONSE($j); exit(0); } else return true; } return false; } // @BRIEF Creates and DUMP of the DNS data to output the DNS Record ID. // @RETURNS Does not return a value. function DUMP(){ try{ // Send the request. $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/" . CF_ZONE_ID . "/dns_records?type=A&name=" . FQDN . "&match=all"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); $headers = array(); $headers[] = "X-Auth-Email: " . CF_API_USERNAME; $headers[] = "X-Auth-Key: " . CF_API_KEY; $headers[] = "Content-Type: application/json"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); if (curl_errno($ch)){ $j->success = false; $j->message = 'cURL encountered a problem.'; $j->cURL_err = curl_error($ch); curl_close ($ch); CREATE_JSON_RESPONSE($j); exit(0); } curl_close ($ch); // Parse the DNS Record ID. $r = json_decode($response, true); if (isset($r['result'][0]['id'])){ $j->success = true; $j->message = 'Please copy the "id" value below into the VALUE for the CF_DNS_RECORD_ID settings. Be sure to copy only the value below within the quotation marks.'; $j->id = $r['result'][0]['id']; CREATE_JSON_RESPONSE($j); exit(0); } else{ $j->success = false; $j->message = 'CloudFlare did not provide a valid response. Please ensure that you have provided correct details for all settings and try again.'; CREATE_JSON_RESPONSE($j); exit(0); } }catch(Exception $e){ $j->success = false; $j->message = 'A fatal exception occurred.'; $j->exception = $e; $j->cURL_err = curl_error($ch); curl_close ($ch); } } // @BRIEF Updates the DNS record with the visitor's IP. // @RETURNS Does not return a value. function DNS_UPDATE($new_ip){ try{ // Send the request. $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/" . CF_ZONE_ID . "/dns_records/" . CF_DNS_RECORD_ID); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // CloudFlare is fucking retarded and bitches about this as "malformed JSON, error 6007". //curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"type\":\"A\",\"name\":\"" . FQDN . "\",\"content\":\"" . $new_ip . "\",\"ttl\":120,\"proxied\":" . USE_CF_REV_PROXY . "}"); if (USE_CF_REV_PROXY) curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"type\":\"A\",\"name\":\"" . FQDN . "\",\"content\":\"" . $new_ip . "\",\"ttl\":120,\"proxied\":true}"); else curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"type\":\"A\",\"name\":\"" . FQDN . "\",\"content\":\"" . $new_ip . "\",\"ttl\":120,\"proxied\":false}"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); $headers = array(); $headers[] = "X-Auth-Email: " . CF_API_USERNAME; $headers[] = "X-Auth-Key: " . CF_API_KEY; $headers[] = "Content-Type: application/json"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); if (curl_errno($ch)){ $j->success = false; $j->message = 'cURL encountered a problem.'; $j->cURL_err = curl_error($ch); curl_close ($ch); CREATE_JSON_RESPONSE($j); exit(0); } curl_close ($ch); // Parse the DNS Record ID. $r = json_decode($response, true); if (isset($r['success'])){ // Uncomment these lines for debugging. // header('Content-Type: application/json'); // echo json_encode($r, JSON_FORCE_OBJECT); // exit(0); if ($r['success'] == true){ $j->success = true; $j->message = 'The [' . FQDN . '] DNS record was successfully updated to point to [' . $new_ip . '].'; CREATE_JSON_RESPONSE($j); exit(0); } $j->success = false; $j->message = 'CloudFlare reported an error. Please ensure that you have provided correct details for all settings and try again.'; // $j->id = $r['result'][0]['id']; CREATE_JSON_RESPONSE($j); exit(0); } else{ $j->success = false; $j->message = 'CloudFlare did not provide a valid response. Please ensure that you have provided correct details for all settings and try again.'; CREATE_JSON_RESPONSE($j); exit(0); } }catch(Exception $e){ $j->success = false; $j->message = 'A fatal exception occurred.'; $j->exception = $e; $j->cURL_err = curl_error($ch); curl_close ($ch); } } // SCRIPT // --------------------------------------------------------- if (CHECK_PASSWORD()){ if (IS_DUMP_REQUEST()){ DUMP(); exit(0); } DNS_UPDATE($visitor_ip); exit(0); } $j->success = false; $j->message = 'The DDNS Password is incorrect. Check the "passwd" value and try again.'; CREATE_JSON_RESPONSE($j); exit(0); // ---------------------------------------------------------