Fork of FusionPBX but with LDAP kinda working
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

719 lines
31 KiB

2 years ago
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <markjcrane@fusionpbx.com>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2022
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <markjcrane@fusionpbx.com>
  20. Luis Daniel Lucio Quiroz <dlucio@okay.com.mx>
  21. */
  22. //includes
  23. require_once "root.php";
  24. require_once "resources/require.php";
  25. require_once "resources/check_auth.php";
  26. //check permissions
  27. if (permission_exists('domain_all') && permission_exists('domain_edit')) {
  28. //access granted
  29. }
  30. else {
  31. echo "access denied";
  32. exit;
  33. }
  34. //add multi-lingual support
  35. $language = new text;
  36. $text = $language->get();
  37. //action add or update
  38. if (!permission_exists('domain_add') || (file_exists($_SERVER["PROJECT_ROOT"]."/app/domains/") && !permission_exists('domain_all'))) {
  39. //admin editing own domain/settings
  40. $domain_uuid = $_SESSION['domain_uuid'];
  41. $action = "update";
  42. }
  43. else {
  44. if (is_uuid($_REQUEST["id"])) {
  45. $action = "update";
  46. $domain_uuid = $_REQUEST["id"];
  47. }
  48. else {
  49. $action = "add";
  50. }
  51. }
  52. //get http post variables and set them to php variables
  53. if (count($_POST) > 0) {
  54. $domain_name = strtolower($_POST["domain_name"]);
  55. $domain_enabled = $_POST["domain_enabled"];
  56. $domain_description = $_POST["domain_description"];
  57. }
  58. //process the data
  59. if (count($_POST) > 0 && strlen($_POST["persistformvar"]) == 0) {
  60. //get the domain_uuid
  61. if ($action == "update" && $_POST["domain_uuid"]) {
  62. $domain_uuid = $_POST["domain_uuid"];
  63. }
  64. //delete the domain
  65. if (permission_exists('domain_delete')) {
  66. if ($_POST['action'] == 'delete' && is_uuid($domain_uuid)) {
  67. //prepare
  68. $array[0]['checked'] = 'true';
  69. $array[0]['uuid'] = $domain_uuid;
  70. //delete
  71. $obj = new domains;
  72. $obj->delete($array);
  73. //redirect
  74. header('Location: domains.php');
  75. exit;
  76. }
  77. }
  78. //validate the token
  79. $token = new token;
  80. if (!$token->validate($_SERVER['PHP_SELF'])) {
  81. message::add($text['message-invalid_token'],'negative');
  82. header('Location: domains.php');
  83. exit;
  84. }
  85. //check for all required data
  86. $msg = '';
  87. if (strlen($domain_name) == 0) { $msg .= $text['message-required'].$text['label-name']."<br>\n"; }
  88. //if (strlen($domain_description) == 0) { $msg .= $text['message-required'].$text['label-description']."<br>\n"; }
  89. if (strlen($msg) > 0 && strlen($_POST["persistformvar"]) == 0) {
  90. require_once "resources/header.php";
  91. require_once "resources/persist_form_var.php";
  92. echo "<div align='center'>\n";
  93. echo "<table><tr><td>\n";
  94. echo $msg."<br />";
  95. echo "</td></tr></table>\n";
  96. persistformvar($_POST);
  97. echo "</div>\n";
  98. require_once "resources/footer.php";
  99. return;
  100. }
  101. //add or update the database
  102. if ($_POST["persistformvar"] != "true") {
  103. if ($action == "add" && permission_exists('domain_add')) {
  104. $sql = "select count(*) from v_domains ";
  105. $sql .= "where lower(domain_name) = :domain_name ";
  106. $parameters['domain_name'] = $domain_name;
  107. $database = new database;
  108. $num_rows = $database->select($sql, $parameters, 'column');
  109. unset($sql, $parameters);
  110. if ($num_rows == 0) {
  111. //add the domain name
  112. $domain_enabled = 'true';
  113. $domain_uuid = uuid();
  114. $array['domains'][0]['domain_uuid'] = $domain_uuid;
  115. $array['domains'][0]['domain_name'] = $domain_name;
  116. $array['domains'][0]['domain_enabled'] = $domain_enabled;
  117. $array['domains'][0]['domain_description'] = $domain_description;
  118. $database = new database;
  119. $database->app_name = 'domains';
  120. $database->app_uuid = '8b91605b-f6d2-42e6-a56d-5d1ded01bb44';
  121. $database->save($array);
  122. //add dialplans to the domain
  123. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/dialplans/app_config.php")) {
  124. //import the dialplans
  125. $dialplan = new dialplan;
  126. $dialplan->import($array['domains']);
  127. unset($array);
  128. //add xml for each dialplan where the dialplan xml is empty
  129. $dialplans = new dialplan;
  130. $dialplans->source = "details";
  131. $dialplans->destination = "database";
  132. $dialplans->context = $domain_name;
  133. $dialplans->is_empty = "dialplan_xml";
  134. $array = $dialplans->xml();
  135. }
  136. //create the recordings directory for the new domain.
  137. if (isset($_SESSION['switch']['recordings']['dir']) && strlen($_SESSION['switch']['recordings']['dir']) > 0) {
  138. if (!file_exists($_SESSION['switch']['recordings']['dir']."/".$domain_name)) {
  139. mkdir($_SESSION['switch']['recordings']['dir']."/".$domain_name, 0770);
  140. }
  141. }
  142. //create the voicemail directory for the new domain.
  143. if (isset($_SESSION['switch']['voicemail']['dir']) && strlen($_SESSION['switch']['voicemail']['dir']) > 0) {
  144. if (!file_exists($_SESSION['switch']['voicemail']['dir']."/default/".$domain_name)) {
  145. mkdir($_SESSION['switch']['voicemail']['dir']."/default/".$domain_name, 0770);
  146. }
  147. }
  148. }
  149. else {
  150. message::add($text['message-domain_exists'],'negative');
  151. header("Location: domains.php");
  152. exit;
  153. }
  154. }
  155. if ($action == "update" && permission_exists('domain_edit')) {
  156. //get original domain name
  157. $sql = "select domain_name from v_domains ";
  158. $sql .= "where domain_uuid = :domain_uuid ";
  159. $parameters['domain_uuid'] = $domain_uuid;
  160. $database = new database;
  161. $original_domain_name = $database->select($sql, $parameters, 'column');
  162. unset($sql, $parameters);
  163. //update domain name, description
  164. $array['domains'][0]['domain_uuid'] = $domain_uuid;
  165. $array['domains'][0]['domain_name'] = $domain_name;
  166. $array['domains'][0]['domain_enabled'] = $domain_enabled;
  167. $array['domains'][0]['domain_description'] = $domain_description;
  168. $database = new database;
  169. $database->app_name = 'domains';
  170. $database->app_uuid = '8b91605b-f6d2-42e6-a56d-5d1ded01bb44';
  171. $database->save($array);
  172. //add dialplans to the domain
  173. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/dialplans/app_config.php")) {
  174. //import the dialplans
  175. $dialplan = new dialplan;
  176. $dialplan->import($array['domains']);
  177. unset($array);
  178. //add xml for each dialplan where the dialplan xml is empty
  179. $dialplans = new dialplan;
  180. $dialplans->source = "details";
  181. $dialplans->destination = "database";
  182. $dialplans->context = $domain_name;
  183. $dialplans->is_empty = "dialplan_xml";
  184. $array = $dialplans->xml();
  185. }
  186. if ($original_domain_name != $domain_name) {
  187. //update dialplans
  188. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/dialplans/app_config.php")) {
  189. $sql = "update v_dialplans set ";
  190. $sql .= "dialplan_context = replace(dialplan_context, :domain_name_old, :domain_name_new), ";
  191. $sql .= "dialplan_xml = replace(dialplan_xml, :domain_name_old, :domain_name_new) ";
  192. $sql .= "where domain_uuid = :domain_uuid ";
  193. $parameters['domain_name_old'] = $original_domain_name;
  194. $parameters['domain_name_new'] = $domain_name;
  195. $parameters['domain_uuid'] = $domain_uuid;
  196. $database = new database;
  197. $database->execute($sql, $parameters);
  198. unset($sql, $parameters);
  199. $sql = "update v_dialplan_details set ";
  200. $sql .= "dialplan_detail_data = replace(dialplan_detail_data, :domain_name_old, :domain_name_new) ";
  201. $sql .= "where domain_uuid = :domain_uuid ";
  202. $parameters['domain_name_old'] = $original_domain_name;
  203. $parameters['domain_name_new'] = $domain_name;
  204. $parameters['domain_uuid'] = $domain_uuid;
  205. $database = new database;
  206. $database->execute($sql, $parameters);
  207. unset($sql, $parameters);
  208. }
  209. //update destinations
  210. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/destinations/app_config.php")) {
  211. $sql = "update v_destinations set ";
  212. $sql .= "destination_data = replace(destination_data, :destination_data_old, :destination_data_new) ";
  213. $sql .= "where domain_uuid = :domain_uuid ";
  214. $parameters['destination_data_old'] = $original_domain_name;
  215. $parameters['destination_data_new'] = $domain_name;
  216. $parameters['domain_uuid'] = $domain_uuid;
  217. $database = new database;
  218. $database->execute($sql, $parameters);
  219. unset($sql, $parameters);
  220. }
  221. //update extensions (accountcode, user_context, dial_domain)
  222. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/extensions/app_config.php")) {
  223. $sql = "update v_extensions set ";
  224. $sql .= "user_context = replace(user_context, :domain_name_old, :domain_name_new), ";
  225. $sql .= "accountcode = replace(accountcode, :domain_name_old, :domain_name_new), ";
  226. $sql .= "dial_domain = replace(dial_domain, :domain_name_old, :domain_name_new) ";
  227. $sql .= "where domain_uuid = :domain_uuid ";
  228. $parameters['domain_name_old'] = $original_domain_name;
  229. $parameters['domain_name_new'] = $domain_name;
  230. $parameters['domain_uuid'] = $domain_uuid;
  231. $database = new database;
  232. $database->execute($sql, $parameters);
  233. unset($sql, $parameters);
  234. }
  235. //update ivr_menus (ivr_menu_context, ivr_menu_greet_long, ivr_menu_greet_short) and ivr_menu_options (ivr_menu_option_param)
  236. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/ivr_menus/app_config.php")) {
  237. $sql = "update v_ivr_menus set ";
  238. $sql .= "ivr_menu_context = replace(ivr_menu_context, :domain_name_old, :domain_name_new), ";
  239. $sql .= "ivr_menu_greet_long = replace(ivr_menu_greet_long, :domain_name_old, :domain_name_new), ";
  240. $sql .= "ivr_menu_greet_short = replace(ivr_menu_greet_short, :domain_name_old, :domain_name_new) ";
  241. $sql .= "where domain_uuid = :domain_uuid ";
  242. $parameters['domain_name_old'] = $original_domain_name;
  243. $parameters['domain_name_new'] = $domain_name;
  244. $parameters['domain_uuid'] = $domain_uuid;
  245. $database = new database;
  246. $database->execute($sql, $parameters);
  247. unset($sql, $parameters);
  248. $sql = "update v_ivr_menu_options set ";
  249. $sql .= "ivr_menu_option_param = replace(ivr_menu_option_param, :domain_name_old, :domain_name_new) ";
  250. $sql .= "where domain_uuid = :domain_uuid ";
  251. $parameters['domain_name_old'] = $original_domain_name;
  252. $parameters['domain_name_new'] = $domain_name;
  253. $parameters['domain_uuid'] = $domain_uuid;
  254. $database = new database;
  255. $database->execute($sql, $parameters);
  256. unset($sql, $parameters);
  257. }
  258. //update ring_groups (ring_group_context, ring_group_forward_destination, ring_group_timeout_data)
  259. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/ring_groups/app_config.php")) {
  260. $sql = "update v_ring_groups set ";
  261. $sql .= "ring_group_context = replace(ring_group_context, :domain_name_old, :domain_name_new), ";
  262. $sql .= "ring_group_forward_destination = replace(ring_group_forward_destination, :domain_name_old, :domain_name_new), ";
  263. $sql .= "ring_group_timeout_data = replace(ring_group_timeout_data, :domain_name_old, :domain_name_new) ";
  264. $sql .= "where domain_uuid = :domain_uuid ";
  265. $parameters['domain_name_old'] = $original_domain_name;
  266. $parameters['domain_name_new'] = $domain_name;
  267. $parameters['domain_uuid'] = $domain_uuid;
  268. $database = new database;
  269. $database->execute($sql, $parameters);
  270. unset($sql, $parameters);
  271. }
  272. //update cdr records (domain_name, context)
  273. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/xml_cdr/app_config.php")){
  274. $sql = "update v_xml_cdr set ";
  275. $sql .= "domain_name = :domain_name_new ";
  276. $sql .= "where domain_name = :domain_name_old ";
  277. $sql .= "and domain_uuid = :domain_uuid ";
  278. $parameters['domain_name_old'] = $original_domain_name;
  279. $parameters['domain_name_new'] = $domain_name;
  280. $parameters['domain_uuid'] = $domain_uuid;
  281. $database = new database;
  282. $database->execute($sql, $parameters);
  283. unset($sql, $parameters);
  284. $sql = "update v_xml_cdr set ";
  285. $sql .= "context = replace(user_context, :context_old, :context_new), ";
  286. $sql .= "where context = :context_old ";
  287. $sql .= "and domain_uuid = :domain_uuid ";
  288. $parameters['context_old'] = $original_domain_name;
  289. $parameters['context_new'] = $domain_name;
  290. $parameters['domain_uuid'] = $domain_uuid;
  291. $database = new database;
  292. $database->execute($sql, $parameters);
  293. unset($sql, $parameters);
  294. }
  295. //update billing, if installed
  296. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/billing/app_config.php")){
  297. $sql = "update v_billings set ";
  298. $sql .= "type_value = :type_value_new ";
  299. $sql .= "where type_value = :type_value_old ";
  300. $sql .= "and domain_uuid = :domain_uuid ";
  301. $parameters['type_value_old'] = $original_domain_name;
  302. $parameters['type_value_new'] = $domain_name;
  303. $parameters['domain_uuid'] = $domain_uuid;
  304. $database = new database;
  305. $database->execute($sql, $parameters);
  306. unset($sql, $parameters);
  307. }
  308. //update conference session recording paths
  309. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/conference_centers/app_config.php")) {
  310. $sql = "update v_conference_sessions set ";
  311. $sql .= "recording = replace(recording, :domain_name_old, :domain_name_new) ";
  312. $sql .= "where domain_uuid = :domain_uuid ";
  313. $parameters['domain_name_old'] = $original_domain_name;
  314. $parameters['domain_name_new'] = $domain_name;
  315. $parameters['domain_uuid'] = $domain_uuid;
  316. $database = new database;
  317. $database->execute($sql, $parameters);
  318. unset($sql, $parameters);
  319. }
  320. //update conference center greetings
  321. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/conference_centers/app_config.php")) {
  322. $sql = "update v_conference_centers set ";
  323. $sql .= "conference_center_greeting = replace(conference_center_greeting, :domain_name_old, :domain_name_new) ";
  324. $sql .= "where domain_uuid = :domain_uuid ";
  325. $parameters['domain_name_old'] = $original_domain_name;
  326. $parameters['domain_name_new'] = $domain_name;
  327. $parameters['domain_uuid'] = $domain_uuid;
  328. $database = new database;
  329. $database->execute($sql, $parameters);
  330. unset($sql, $parameters);
  331. }
  332. //update call center queue record templates
  333. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/call_center/app_config.php")) {
  334. $sql = "update v_call_center_queues set ";
  335. $sql .= "queue_record_template = replace(queue_record_template, :domain_name_old, :domain_name_new) ";
  336. $sql .= "where domain_uuid = :domain_uuid ";
  337. $parameters['domain_name_old'] = $original_domain_name;
  338. $parameters['domain_name_new'] = $domain_name;
  339. $parameters['domain_uuid'] = $domain_uuid;
  340. $database = new database;
  341. $database->execute($sql, $parameters);
  342. unset($sql, $parameters);
  343. }
  344. //update call center agent contacts
  345. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/call_center/app_config.php")) {
  346. $sql = "update v_call_center_agents set ";
  347. $sql .= "agent_contact = replace(agent_contact, :domain_name_old, :domain_name_new) ";
  348. $sql .= "where domain_uuid = :domain_uuid ";
  349. $parameters['domain_name_old'] = $original_domain_name;
  350. $parameters['domain_name_new'] = $domain_name;
  351. $parameters['domain_uuid'] = $domain_uuid;
  352. $database = new database;
  353. $database->execute($sql, $parameters);
  354. unset($sql, $parameters);
  355. }
  356. //update call flows data, alternate-data and contexts
  357. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/call_flows/app_config.php")) {
  358. $sql = "update v_call_flows set ";
  359. $sql .= "call_flow_data = replace(call_flow_data, :domain_name_old, :domain_name_new), ";
  360. $sql .= "call_flow_alternate_data = replace(call_flow_alternate_data, :domain_name_old, :domain_name_new), ";
  361. $sql .= "call_flow_context = replace(call_flow_context, :domain_name_old, :domain_name_new) ";
  362. $sql .= "where domain_uuid = :domain_uuid ";
  363. $parameters['domain_name_old'] = $original_domain_name;
  364. $parameters['domain_name_new'] = $domain_name;
  365. $parameters['domain_uuid'] = $domain_uuid;
  366. $database = new database;
  367. $database->execute($sql, $parameters);
  368. unset($sql, $parameters);
  369. }
  370. //update device lines server_address, server_address_primary, server_address_secondary, outbound_proxy_primary, outbound_proxy_secondary
  371. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/devices/app_config.php")) {
  372. $sql = "update v_device_lines set ";
  373. $sql .= "server_address = replace(server_address, :domain_name_old, :domain_name_new), ";
  374. $sql .= "server_address_primary = replace(server_address_primary, :domain_name_old, :domain_name_new), ";
  375. $sql .= "server_address_secondary = replace(server_address_secondary, :domain_name_old, :domain_name_new), ";
  376. $sql .= "outbound_proxy_primary = replace(outbound_proxy_primary, :domain_name_old, :domain_name_new), ";
  377. $sql .= "outbound_proxy_secondary = replace(outbound_proxy_secondary, :domain_name_old, :domain_name_new) ";
  378. $sql .= "where domain_uuid = :domain_uuid ";
  379. $parameters['domain_name_old'] = $original_domain_name;
  380. $parameters['domain_name_new'] = $domain_name;
  381. $parameters['domain_uuid'] = $domain_uuid;
  382. $database = new database;
  383. $database->execute($sql, $parameters);
  384. unset($sql, $parameters);
  385. }
  386. //rename switch/storage/voicemail/default/[domain] (folder)
  387. if (isset($_SESSION['switch']['voicemail']['dir']) && file_exists($_SESSION['switch']['voicemail']['dir']."/default/".$original_domain_name)) {
  388. @rename($_SESSION['switch']['voicemail']['dir']."/default/".$original_domain_name, $_SESSION['switch']['voicemail']['dir']."/default/".$domain_name); // folder
  389. }
  390. //rename switch/storage/fax/[domain] (folder)
  391. if (isset($_SESSION['switch']['storage']['dir']) && file_exists($_SESSION['switch']['storage']['dir']."/fax/".$original_domain_name)) {
  392. @rename($_SESSION['switch']['storage']['dir']."/fax/".$original_domain_name, $_SESSION['switch']['storage']['dir']."/fax/".$domain_name); // folder
  393. }
  394. //rename switch/conf/dialplan/[domain] (folder/file)
  395. if (isset($_SESSION['switch']['dialplan']['dir'])) {
  396. if (file_exists($_SESSION['switch']['dialplan']['dir']."/".$original_domain_name)) {
  397. @rename($_SESSION['switch']['dialplan']['dir']."/".$original_domain_name, $_SESSION['switch']['dialplan']['dir']."/".$domain_name); // folder
  398. }
  399. if (file_exists($_SESSION['switch']['dialplan']['dir']."/".$original_domain_name.".xml")) {
  400. @rename($_SESSION['switch']['dialplan']['dir']."/".$original_domain_name.".xml", $_SESSION['switch']['dialplan']['dir']."/".$domain_name.".xml"); // file
  401. }
  402. }
  403. //rename switch/conf/dialplan/public/[domain] (folder/file)
  404. if (isset($_SESSION['switch']['dialplan']['dir'])) {
  405. if (file_exists($_SESSION['switch']['dialplan']['dir']."/public/".$original_domain_name)) {
  406. @rename($_SESSION['switch']['dialplan']['dir']."/public/".$original_domain_name, $_SESSION['switch']['dialplan']['dir']."/public/".$domain_name); // folder
  407. }
  408. if (file_exists($_SESSION['switch']['dialplan']['dir']."/public/".$original_domain_name.".xml")) {
  409. @rename($_SESSION['switch']['dialplan']['dir']."/public/".$original_domain_name.".xml", $_SESSION['switch']['dialplan']['dir']."/public/".$domain_name.".xml"); // file
  410. }
  411. }
  412. //rename switch/conf/directory/[domain] (folder/file)
  413. if (isset($_SESSION['switch']['extensions']['dir'])) {
  414. if (file_exists($_SESSION['switch']['extensions']['dir']."/".$original_domain_name)) {
  415. @rename($_SESSION['switch']['extensions']['dir']."/".$original_domain_name, $_SESSION['switch']['extensions']['dir']."/".$domain_name); // folder
  416. }
  417. if (file_exists($_SESSION['switch']['extensions']['dir']."/".$original_domain_name.".xml")) {
  418. @rename($_SESSION['switch']['extensions']['dir']."/".$original_domain_name.".xml", $_SESSION['switch']['extensions']['dir']."/".$domain_name.".xml"); // file
  419. }
  420. }
  421. //rename switch/recordings/[domain] (folder)
  422. if (file_exists($_SESSION['switch']['recordings']['dir']."/".$_SESSION['domain_name'])) {
  423. $switch_recordings_dir = str_replace("/".$_SESSION["domain_name"], "", $_SESSION['switch']['recordings']['dir']."/".$_SESSION['domain_name']);
  424. if (file_exists($switch_recordings_dir."/".$original_domain_name)) {
  425. @rename($switch_recordings_dir."/".$original_domain_name, $switch_recordings_dir."/".$domain_name); // folder
  426. }
  427. }
  428. //update dialplan, dialplan/public xml files
  429. $dialplan_xml = file_get_contents($_SESSION['switch']['dialplan']['dir']."/".$domain_name.".xml");
  430. $dialplan_xml = str_replace($original_domain_name, $domain_name, $dialplan_xml);
  431. file_put_contents($_SESSION['switch']['dialplan']['dir']."/".$domain_name.".xml", $dialplan_xml);
  432. unset($dialplan_xml);
  433. $dialplan_public_xml = file_get_contents($_SESSION['switch']['dialplan']['dir']."/public/".$domain_name.".xml");
  434. $dialplan_public_xml = str_replace($original_domain_name, $domain_name, $dialplan_public_xml);
  435. file_put_contents($_SESSION['switch']['dialplan']['dir']."/public/".$domain_name.".xml", $dialplan_public_xml);
  436. unset($dialplan_public_xml);
  437. //update session domain name
  438. $_SESSION['domains'][$domain_uuid]['domain_name'] = $domain_name;
  439. //recreate dialplan and extension xml files
  440. if (is_readable($_SESSION['switch']['extensions']['dir'])) {
  441. require_once $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/app/extensions/resources/classes/extension.php";
  442. $extension = new extension;
  443. $extension->xml();
  444. }
  445. //if single-tenant and variables exist, update variables > domain value to match new domain
  446. if (count($_SESSION['domains']) == 1 && file_exists($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/app/vars/")) {
  447. $sql = "update v_vars set ";
  448. $sql .= "var_value = :var_value ";
  449. $sql .= "where var_name = 'domain' ";
  450. $parameters['var_value'] = $domain_name;
  451. $database = new database;
  452. $database->app_name = 'domains';
  453. $database->app_uuid = '8b91605b-f6d2-42e6-a56d-5d1ded01bb44';
  454. $database->execute($sql, $parameters);
  455. unset($sql, $parameters);
  456. }
  457. }
  458. }
  459. //clear the cache
  460. $cache = new cache;
  461. $response = $cache->flush();
  462. //clear the domains session array to update it
  463. unset($_SESSION["domains"]);
  464. unset($_SESSION['domain']);
  465. unset($_SESSION['switch']);
  466. //redirect the browser
  467. if ($action == "update") {
  468. message::add($text['message-update']);
  469. if (!permission_exists('domain_add')) { //admin, updating own domain
  470. header("Location: domain_edit.php");
  471. }
  472. else {
  473. header("Location: domains.php"); //superadmin
  474. }
  475. }
  476. if ($action == "add") {
  477. message::add($text['message-add']);
  478. header("Location: domains.php");
  479. }
  480. exit;
  481. }
  482. }
  483. //pre-populate the form (admin won't have domain_add permissions, but domain_uuid will already be set above)
  484. if ((count($_GET) > 0 || (!permission_exists('domain_add') && $domain_uuid != '')) && $_POST["persistformvar"] != "true") {
  485. $sql = "select ";
  486. $sql .= "domain_uuid, ";
  487. $sql .= "domain_name, ";
  488. $sql .= "cast(domain_enabled as text), ";
  489. $sql .= "domain_description ";
  490. $sql .= "from v_domains ";
  491. $sql .= "where domain_uuid = :domain_uuid ";
  492. $parameters['domain_uuid'] = $domain_uuid;
  493. $database = new database;
  494. $row = $database->select($sql, $parameters, 'row');
  495. if (is_array($row) && sizeof($row) != 0) {
  496. $domain_name = strtolower($row["domain_name"]);
  497. $domain_enabled = $row["domain_enabled"];
  498. $domain_description = $row["domain_description"];
  499. }
  500. unset($sql, $parameters, $row);
  501. }
  502. //create token
  503. $object = new token;
  504. $token = $object->create($_SERVER['PHP_SELF']);
  505. //show the header
  506. require_once "resources/header.php";
  507. if ($action == "update") {
  508. $document['title'] = $text['title-domain-edit'];
  509. }
  510. if ($action == "add") {
  511. $document['title'] = $text['title-domain-add'];
  512. }
  513. //copy settings javascript
  514. if (permission_exists("domain_select") && permission_exists("domain_setting_add") && count($_SESSION['domains']) > 1) {
  515. echo "<script language='javascript' type='text/javascript'>\n";
  516. echo " var fade_speed = 400;\n";
  517. echo " function show_domains() {\n";
  518. echo " document.getElementById('action').value = 'copy';\n";
  519. echo " $('#button_copy').fadeOut(fade_speed, function() {\n";
  520. echo " $('#button_back').fadeIn(fade_speed);\n";
  521. echo " $('#target_domain_uuid').fadeIn(fade_speed);\n";
  522. echo " $('#button_paste').fadeIn(fade_speed);\n";
  523. echo " });";
  524. echo " }";
  525. echo " function hide_domains() {\n";
  526. echo " document.getElementById('action').value = '';\n";
  527. echo " $('#button_back').fadeOut(fade_speed);\n";
  528. echo " $('#target_domain_uuid').fadeOut(fade_speed);\n";
  529. echo " $('#button_paste').fadeOut(fade_speed, function() {\n";
  530. echo " $('#button_copy').fadeIn(fade_speed);\n";
  531. echo " document.getElementById('target_domain_uuid').selectedIndex = 0;\n";
  532. echo " });\n";
  533. echo " }\n";
  534. echo "\n";
  535. echo " $(document).ready(function() {\n";
  536. echo " $('#domain_setting_search').trigger('focus');\n";
  537. if ($search == '') {
  538. echo " // scroll to previous category\n";
  539. echo " var category_span_id;\n";
  540. echo " var url = document.location.href;\n";
  541. echo " var hashindex = url.indexOf('#');\n";
  542. echo " if (hashindex == -1) { }\n";
  543. echo " else {\n";
  544. echo " category_span_id = url.substr(hashindex + 1);\n";
  545. echo " }\n";
  546. echo " if (category_span_id) {\n";
  547. echo " $('#page').animate({scrollTop: $('#anchor_'+category_span_id).offset().top - 200}, 'slow');\n";
  548. echo " }\n";
  549. }
  550. echo " });\n";
  551. echo "</script>";
  552. }
  553. //show the content
  554. echo "<form method='post' name='frm' id='frm'>\n";
  555. echo "<div class='action_bar' id='action_bar'>\n";
  556. echo " <div class='heading'>";
  557. if ($action == "update") {
  558. echo "<b>".$text['header-domain']."</b>";
  559. }
  560. if ($action == "add") {
  561. echo "<b>".$text['header-domain-add']."</b>";
  562. }
  563. echo " </div>\n";
  564. echo " <div class='actions'>\n";
  565. if (permission_exists('domain_add')) {
  566. echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','style'=>'margin-right: 15px;','link'=>'domains.php']);
  567. }
  568. if ($action == "update" && permission_exists('domain_setting_view')) {
  569. echo button::create(['type'=>'button','label'=>$text['button-settings'],'icon'=>$_SESSION['theme']['button_icon_settings'],'id'=>'btn_back','style'=>'margin-right: 2px;','link'=>PROJECT_PATH.'/core/domain_settings/domain_settings.php?id='.urlencode($domain_uuid)]);
  570. }
  571. if (permission_exists('domain_delete') && is_array($_SESSION['domains']) && @sizeof($_SESSION['domains']) > 1 && $domain_uuid != $_SESSION['domain_uuid']) {
  572. echo button::create(['type'=>'button','label'=>$text['button-delete'],'icon'=>$_SESSION['theme']['button_icon_delete'],'onclick'=>"modal_open('modal-delete-domain','btn_delete_domain');"]);
  573. }
  574. if (permission_exists("domain_select") && is_array($_SESSION['domains']) && @sizeof($_SESSION['domains']) > 1) {
  575. echo "<select id='domains' class='formfld' style='width: auto;' onchange=\"window.location.href='?id=' + document.getElementById('domains').options[document.getElementById('domains').selectedIndex].value;\">\n";
  576. foreach ($_SESSION['domains'] as $domain) {
  577. $selected = $domain["domain_uuid"] == $domain_uuid ? "selected='selected'" : null;
  578. echo " <option value='".escape($domain["domain_uuid"])."' ".$selected.">".escape($domain["domain_name"])."</option>\n";
  579. }
  580. echo "</select>";
  581. }
  582. if (permission_exists('domain_export')) {
  583. echo button::create(['type'=>'button','label'=>$text['button-export'],'icon'=>$_SESSION['theme']['button_icon_export'],'link'=>PROJECT_PATH."/app/domain_export/index.php?id=".urlencode($domain_uuid)]);
  584. }
  585. echo button::create(['type'=>'submit','label'=>$text['button-save'],'icon'=>$_SESSION['theme']['button_icon_save'],'id'=>'btn_save','style'=>'margin-left: 3px;']);
  586. echo " </div>\n";
  587. echo " <div style='clear: both;'></div>\n";
  588. echo "</div>\n";
  589. if (permission_exists('domain_delete') && is_array($_SESSION['domains']) && @sizeof($_SESSION['domains']) > 1 && $domain_uuid != $_SESSION['domain_uuid']) {
  590. echo modal::create(['id'=>'modal-delete-domain','type'=>'delete','actions'=>button::create(['type'=>'submit','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_delete_domain','style'=>'float: right; margin-left: 15px;','collapse'=>'never','name'=>'action','value'=>'delete','onclick'=>"modal_close();"])]);
  591. }
  592. if ($action == "update") {
  593. echo $text['description-domain-edit']."\n";
  594. }
  595. if ($action == "add") {
  596. echo $text['description-domain-add']."\n";
  597. }
  598. echo "<br /><br />\n";
  599. echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'>\n";
  600. echo "<tr>\n";
  601. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  602. echo " ".$text['label-name']."\n";
  603. echo "</td>\n";
  604. echo "<td class='vtable' align='left'>\n";
  605. echo " <input class='formfld' type='text' name='domain_name' maxlength='255' value=\"".escape($domain_name)."\">\n";
  606. echo "<br />\n";
  607. echo $text['description-name']."\n";
  608. echo "</td>\n";
  609. echo "</tr>\n";
  610. echo "<tr>\n";
  611. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  612. echo " ".$text['label-enabled']."\n";
  613. echo "</td>\n";
  614. echo "<td class='vtable' align='left'>\n";
  615. echo " <select class='formfld' name='domain_enabled'>\n";
  616. echo " <option value='true' ".(($domain_enabled == "true") ? "selected='selected'" : null).">".$text['label-true']."</option>\n";
  617. echo " <option value='false' ".(($domain_enabled == "false") ? "selected='selected'" : null).">".$text['label-false']."</option>\n";
  618. echo " </select>\n";
  619. echo "<br />\n";
  620. echo $text['description-domain_enabled']."\n";
  621. echo "</td>\n";
  622. echo "</tr>\n";
  623. echo "<tr>\n";
  624. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  625. echo " ".$text['label-description']."\n";
  626. echo "</td>\n";
  627. echo "<td class='vtable' align='left'>\n";
  628. echo " <input class='formfld' type='text' name='domain_description' maxlength='255' value=\"".escape($domain_description)."\">\n";
  629. echo "<br />\n";
  630. echo $text['description-description']."\n";
  631. echo "</td>\n";
  632. echo "</tr>\n";
  633. echo "</table>";
  634. echo "<br /><br />";
  635. if ($action == "update") {
  636. echo "<input type='hidden' name='domain_uuid' value='".escape($domain_uuid)."'>\n";
  637. }
  638. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  639. echo "</form>";
  640. //include the footer
  641. require_once "resources/footer.php";
  642. ?>