From 32f0456f218d4af4558a1c33c5d9e025818547a1 Mon Sep 17 00:00:00 2001
From: Mark Schouten <mark@tuxis.nl>
Date: Mon, 10 Oct 2016 14:22:31 +0200
Subject: [PATCH] Fix TXT-record quoting

---
 zones.php | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/zones.php b/zones.php
index b3618a0..90c58ed 100644
--- a/zones.php
+++ b/zones.php
@@ -387,6 +387,14 @@ case "createrecord":
         jtable_respond(null, 'error', "Please only use ASCII-characters in your fields");
     }
 
+    if ($type === 'TXT') {
+        # empty TXT records are ok, otherwise require surrounding quotes: "..."
+        if (strlen($content) == 1 || substr($content, 0, 1) !== '"' || substr($content, -1) !== '"') {
+            # fix quoting: first escape all \, then all ", then surround with quotes.
+            $content = '"'.str_replace('"', '\\"', str_replace('\\', '\\\\', $content)).'"';
+        }
+    }
+
     $record = $zone->addRecord($name, $type, $content, $_POST['disabled'], $_POST['ttl'], $_POST['setptr']);
     $api->savezone($zone->export());