domain_edit.php 32 KB

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