forked from xiaosuo/voipconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.php
executable file
·148 lines (132 loc) · 4.09 KB
/
edit.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
<?php
/**
* View the configuration
*/
require_once("access.php");
require_once("lib/ini.php");
require_once("lib/ext.php");
require_once("lib/validate.php");
function __invalid_entry($model, $focus, $message)
{
$model["focus"] = $focus;
$model["message"] = $message;
render("Edit", "edit", $model);
exit;
}
function invalid_entry($model, $focus)
{
__invalid_entry($model, $focus, "Invalid Entry");
}
$model = $_POST;
if ($_GET["mac"] != "") {
$model["mac"] = $_GET["mac"];
$model["mode"] = "edit";
$model["focus"] = "password";
} else {
$model["mode"] = "add";
$model["focus"] = "mac";
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
/* validate all the entries */
if (valid_mac($model["mac"]) === false)
invalid_entry($model, "mac");
if (valid_username($model["username"]) === false)
invalid_entry($model, "username");
if (valid_password($model["password"]) === false)
invalid_entry($model, "password");
$model["mac"] = strtoupper($model["mac"]);
foreach ($model["switch"] as $i => $switch) {
if (valid_ip($switch["host"]) === false)
invalid_entry($model, "switch[$i][host]");
if (valid_call_limit($switch["call-limit"]) === false)
invalid_entry($model, "switch[$i][call-limit]");
}
foreach ($model["gateway"] as $i => $gateway) {
if (valid_ip($gateway["host"]) === false)
invalid_entry($model, "gateway[$i][host]");
if (valid_port($gateway["port"]) === false)
invalid_entry($model, "gateway[$i][port]");
if (valid_pefix($gateway["prefix"]) === false)
invalid_entry($model, "gateway[$i][prefix]");
}
$chan = new Ini();
$chan->load($g_chan_sync);
foreach ($chan->sections() as $user) {
if ($chan->get($user, "mac") == $model["mac"]) {
if ($model["mode"] == "add")
__invalid_entry($model, "mac", "Duplicate MAC Address");
$chan->deleteSection($user);
$old_user = $user;
break;
}
}
if (in_array($model["username"], $chan->sections()))
__invalid_entry($model, "username", "Duplicate Username");
$chan->add($model["username"], "authname", $model["username"]);
$chan->add($model["username"], "secret", $model["password"]);
$chan->add($model["username"], "host", "dynamic");
$chan->add($model["username"], "disallow", "all");
$chan->add($model["username"], "allow", "g729,g723");
$chan->add($model["username"], "mac", $model["mac"]);
$sip = new Ini();
$sip->load($g_sip);
$hosts = array();
foreach ($sip->sections() as $host) {
if ($sip->get($host, "context") == $old_user)
$sip->deleteSection($host);
else
$hosts[] = $host;
}
foreach ($model["switch"] as $i => $switch) {
if (in_array($switch["host"], $hosts))
__invalid_entry($model, "switch[$i][host]",
"Duplicate Switch Host");
else
$hosts[] = $switch["host"];
$sip->add($switch["host"], "type", "friend");
$sip->add($switch["host"], "host", $switch["host"]);
$sip->add($switch["host"], "context", $model["username"]);
$sip->add($switch["host"], "call-limit", $switch["call-limit"]);
}
$usr = new ExtUsr();
$usr->load($g_ext_usr);
if (isset($old_user))
$usr->delete($old_user);
$prefix = array();
foreach ($model["gateway"] as $i => $gateway) {
if (in_array($gateway["prefix"], $prefix))
__invalid_entry($model, "gateway[$i][prefix]",
"Duplicate Gateway Prefix");
else
$prefix[] = $gateway["prefix"];
$usr->add($model["username"], $gateway);
}
$ael = new ExtAel();
$ael->load($g_ext_ael);
if (isset($old_user))
$ael->delete($old_user);
$ael->add($model["username"]);
$chan->dump($g_chan_sync);
$sip->dump($g_sip);
$usr->dump($g_ext_usr);
$ael->dump($g_ext_ael);
reload_all($g_vpn);
header("Location: " . dirname($_SERVER["PHP_SELF"]) . "/list.php");
exit;
}
// load generic info by mac
$ini = new Ini();
$ini->load($g_chan_sync);
foreach ($ini->sections() as $user) {
if ($ini->get($user, "mac") == $model["mac"]) {
$model["username"] = $ini->get($user, "authname");
$model["password"] = $ini->get($user, "secret");
break;
}
}
// load switch info by username
$model["switch"] = get_switch($g_sip, $model["username"]);
// load gateway info by username
$model["gateway"] = get_gateway($g_ext_usr, $model["username"]);
render("Edit", "edit", $model);
?>