|
@@ -1,16 +1,48 @@
|
1
|
1
|
//
|
2
|
2
|
// Created by robin on 8/9/15.
|
3
|
3
|
//
|
4
|
|
-
|
|
4
|
+#include <string>
|
|
5
|
+#include <sstream>
|
|
6
|
+#include <vector>
|
5
|
7
|
#include "ActionAddHost.h"
|
6
|
8
|
|
|
9
|
+std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
|
|
10
|
+ std::stringstream ss(s);
|
|
11
|
+ std::string item;
|
|
12
|
+ while (std::getline(ss, item, delim)) {
|
|
13
|
+ elems.push_back(item);
|
|
14
|
+ }
|
|
15
|
+ return elems;
|
|
16
|
+}
|
|
17
|
+
|
|
18
|
+std::vector<std::string> split(const std::string &s, char delim) {
|
|
19
|
+ std::vector<std::string> elems;
|
|
20
|
+ split(s, delim, elems);
|
|
21
|
+ return elems;
|
|
22
|
+}
|
|
23
|
+
|
7
|
24
|
ActionAddHost::ActionAddHost()
|
8
|
25
|
{
|
9
|
26
|
}
|
10
|
27
|
|
11
|
28
|
const std::string ActionAddHost::getSql() const
|
12
|
29
|
{
|
13
|
|
- return "";
|
|
30
|
+ auto query = "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date)\n"
|
|
31
|
+ " VALUES(@domain_id, \"" + _host + "." + _domain + "\", \"" + _recordType + "\","
|
|
32
|
+ " \"" + _recordValue + "\", 84600, 0, " + std::to_string(time(nullptr)) + ");\n";
|
|
33
|
+
|
|
34
|
+ if (_reverseEnabled && _recordType == "A")
|
|
35
|
+ {
|
|
36
|
+ auto reversedValue = getReversedValue();
|
|
37
|
+ auto reverseDomain = _reverseDomain.empty() ? "in-addr.arpa" : _reverseDomain + ".in-addr.arpa";
|
|
38
|
+
|
|
39
|
+ query += "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date)\n"
|
|
40
|
+ " VALUES((SELECT get_domain_id(\"" + reverseDomain + "\")), \""
|
|
41
|
+ + reversedValue + "." + reverseDomain + "\", \"PTR\","
|
|
42
|
+ " \"" + _host + "." + _domain + "\", 84600, 0, " + std::to_string(time(nullptr)) + ");\n";
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ return query;
|
14
|
46
|
}
|
15
|
47
|
|
16
|
48
|
const std::string &ActionAddHost::getHost() const
|
|
@@ -71,3 +103,22 @@ void ActionAddHost::setReverseEnabled(bool reverseEnabled)
|
71
|
103
|
{
|
72
|
104
|
_reverseEnabled = reverseEnabled;
|
73
|
105
|
}
|
|
106
|
+
|
|
107
|
+const std::string ActionAddHost::getReversedValue() const
|
|
108
|
+{
|
|
109
|
+ auto splitValue = split(_recordValue, '.');
|
|
110
|
+ int index = 1;
|
|
111
|
+
|
|
112
|
+ for (; index < splitValue.size(); ++index)
|
|
113
|
+ {
|
|
114
|
+ std::string reversedValue;
|
|
115
|
+ for (int i = (int) splitValue.size() - 1 - index; i >= 0; --i)
|
|
116
|
+ reversedValue += splitValue[i] + (i == 0 ? "" : ".");
|
|
117
|
+ if (_reverseDomain == reversedValue)
|
|
118
|
+ break;
|
|
119
|
+ }
|
|
120
|
+ std::string reversedValue;
|
|
121
|
+ for (int i = (int) splitValue.size() - 1; i >= (int) splitValue.size() - index; --i)
|
|
122
|
+ reversedValue += std::string(splitValue[i]) + (i == (int) splitValue.size() - index ? "" : ".");
|
|
123
|
+ return reversedValue;
|
|
124
|
+}
|