-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.php
166 lines (152 loc) · 5.09 KB
/
install.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
if (!defined("FREEPBX_IS_AUTH")) {
die("No direct script access allowed");
}
$module = "dundi";
require_once dirname(__FILE__) . "/functions.inc.php";
$result = $db->getOne("SELECT * FROM dundi_config");
$first_run = (DB::IsError($result));
out(_("Creating database tables..."));
$query = <<< SQL
CREATE TABLE IF NOT EXISTS dundi_config (
setting VARCHAR(80) NOT NULL,
value VARCHAR(80) NOT NULL,
UNIQUE INDEX idx_setting (setting)
)
SQL;
$result = $db->query($query);
if (DB::IsError($result)) {
die_freepbx(_("Can not create table `%s`: %s"), "dundi_config", $result->getMessage());
}
$query = <<< SQL
CREATE TABLE IF NOT EXISTS dundi_mappings (
name VARCHAR(80) NOT NULL,
context VARCHAR(80) NOT NULL,
weight VARCHAR(512) NOT NULL,
tech VARCHAR(8) NOT NULL,
dest VARCHAR(512) NOT NULL,
flags SET(
"nounsolicited",
"nocomunsolicit",
"residential",
"commercial",
"mobile",
"nopartial"
) NOT NULL DEFAULT "",
descr VARCHAR(80) NOT NULL
)
SQL;
$result = $db->query($query);
if (DB::IsError($result)) {
die_freepbx(_("Can not create table `%s`: %s"), "dundi_mappings", $result->getMessage());
}
$query = <<< SQL
CREATE TABLE IF NOT EXISTS dundi_peers (
peerid VARCHAR(17) NOT NULL PRIMARY KEY,
inkey VARCHAR(80) NOT NULL,
outkey VARCHAR(80) NOT NULL,
host VARCHAR(80) NOT NULL,
port INT NOT NULL DEFAULT 4520,
qualify VARCHAR(8) NOT NULL,
ord VARCHAR(80) NOT NULL,
include VARCHAR(80) NOT NULL,
noinclude VARCHAR(80) NOT NULL,
permit VARCHAR(80) NOT NULL,
deny VARCHAR(80) NOT NULL,
model ENUM(
"",
"inbound",
"outbound",
"symmetric",
"none"
) NOT NULL,
precache ENUM(
"",
"inbound",
"outbound",
"symmetric",
"none"
) NOT NULL,
descr VARCHAR(80) NOT NULL
)
SQL;
$result = $db->query($query);
if (DB::IsError($result)) {
die_freepbx(_("Can not create table `%s`: %s"), "dundi_peers", $result->getMessage());
}
$query = <<< SQL
CREATE TABLE IF NOT EXISTS dundi_contexts (
name VARCHAR(80) PRIMARY KEY,
descr VARCHAR(80) NOT NULL
)
SQL;
$result = $db->query($query);
if (DB::IsError($result)) {
die_freepbx(_("Can not create table `%s`: %s"), "dundi_contexts", $result->getMessage());
}
$query = <<< SQL
CREATE TABLE IF NOT EXISTS dundi_context_entries (
context VARCHAR(80) NOT NULL,
extension VARCHAR(80) NOT NULL,
UNIQUE INDEX idx_ext (context, extension)
)
SQL;
$result = $db->query($query);
if (DB::IsError($result)) {
die_freepbx(_("Can not create table `%s`: %s"), "dundi_context_entries", $result->getMessage());
}
$config_file = $amp_conf["ASTETCDIR"] . "/" . Dundi::$config_file;
if (file_exists($config_file) && $first_run) {
out(_("Importing existing DUNDi configuration…"));
$config = Dundi::parse_conf_file($config_file);
$fields = array();
$query = "INSERT INTO dundi_config (setting, value) VALUES (?, ?)";
$stmt = $db->prepare($query);
foreach ($config["general"] as $k=>$v) {
$fields[] = array($k, $v);
}
$result = $db->executeMultiple($stmt, $fields);
if (DB::IsError($result)) {
error_log("Database error: $query");
error_log(var_dump($fields, true));
die_freepbx(sprintf(_("Can not insert value from [%s] section: %s"), "general", $result->getMessage()));
}
unset($config["general"]);
$fields = array();
$query = "INSERT INTO dundi_mappings (name, context, weight, tech, dest, flags) VALUES (?, ?, ?, ?, ?, ?)";
$stmt = $db->prepare($query);
foreach ($config["mappings"] as $k=>$v) {
$v = explode(",", $v, 5);
$v[0] = str_replace("> ", "", $v[0]);
$fields[] = array($k, $v[0], $v[1], $v[2], $v[3], $v[4]);
}
$result = $db->executeMultiple($stmt, $fields);
if (DB::IsError($result)) {
error_log("Database error: $query");
error_log(var_dump($fields, true));
die_freepbx(sprintf(_("Can not insert value from [%s] section: %s"), "mappings", $result->getMessage()));
}
unset($config["mappings"]);
$fields = array();
$query = "INSERT INTO dundi_peers (peerid, inkey, outkey, host, port, qualify, ord, include, noinclude, permit, deny, model, precache) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";
$stmt = $db->prepare($query);
foreach($config as $k=>$v) {
//ensure no unset values
$v = array_merge(Dundi::default_peer(), $v);
$fields[] = array($k, $v["inkey"], $v["outkey"], $v["host"], $v["port"], $v["qualify"], $v["order"], $v["include"], $v["noinclude"], $v["permit"], $v["deny"], $v["model"], $v["precache"]);
}
$result = $db->executeMultiple($stmt, $fields);
if (DB::IsError($result)) {
error_log("Database error: $query");
error_log(var_dump($fields, true));
die_freepbx(sprintf(_("Can not insert value from [%s] section: %s"), "peers", $result->getMessage()));
}
if (!file_exists("$config_file.0")) {
out(sprintf(_("Backing up previous configuration file as %s."), "$config_file.0"));
rename($config_file, "$config_file.0");
} else {
out(sprintf(_("<strong>A file named %s already exists, %s must be manually removed and backed up!</strong>"), "$config_file.0", $config_file));
}
}
touch($config_file);
?>