domains.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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-2016
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. sreis
  21. */
  22. /**
  23. * domains class
  24. *
  25. * @method null delete
  26. * @method null toggle
  27. * @method null copy
  28. */
  29. if (!class_exists('domains')) {
  30. class domains {
  31. /**
  32. * declare the variables
  33. */
  34. private $app_name;
  35. private $app_uuid;
  36. private $name;
  37. private $table;
  38. private $toggle_field;
  39. private $toggle_values;
  40. private $location;
  41. /**
  42. * called when the object is created
  43. */
  44. public function __construct() {
  45. //assign the variables
  46. $this->app_name = 'domains';
  47. $this->app_uuid = '8b91605b-f6d2-42e6-a56d-5d1ded01bb44';
  48. $this->name = 'domain';
  49. $this->table = 'domains';
  50. $this->toggle_field = 'domain_enabled';
  51. $this->toggle_values = ['true','false'];
  52. $this->location = 'domains.php';
  53. }
  54. /**
  55. * called when there are no references to a particular object
  56. * unset the variables used in the class
  57. */
  58. public function __destruct() {
  59. foreach ($this as $key => $value) {
  60. unset($this->$key);
  61. }
  62. }
  63. /**
  64. * delete rows from the database
  65. */
  66. public function delete($records) {
  67. if (permission_exists($this->name.'_delete')) {
  68. //add multi-lingual support
  69. $language = new text;
  70. $text = $language->get();
  71. //validate the token
  72. $token = new token;
  73. if (!$token->validate($_SERVER['PHP_SELF'])) {
  74. message::add($text['message-invalid_token'],'negative');
  75. header('Location: '.$this->location);
  76. exit;
  77. }
  78. //delete multiple records
  79. if (is_array($records) && @sizeof($records) != 0) {
  80. //build the delete array
  81. $d = 0;
  82. foreach ($records as $record) {
  83. //add to the array
  84. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  85. //set the uuid
  86. $id = $record['uuid'];
  87. //get the domain using the id
  88. $sql = "select domain_name from v_domains ";
  89. $sql .= "where domain_uuid = :domain_uuid ";
  90. $parameters['domain_uuid'] = $id;
  91. $database = new database;
  92. $domain_name = $database->select($sql, $parameters, 'column');
  93. unset($sql, $parameters);
  94. //get the domain settings
  95. $sql = "select * from v_domain_settings ";
  96. $sql .= "where domain_uuid = :domain_uuid ";
  97. $sql .= "and domain_setting_enabled = 'true' ";
  98. $parameters['domain_uuid'] = $id;
  99. $database = new database;
  100. $result = $database->select($sql, $parameters, 'all');
  101. unset($sql, $parameters);
  102. if (is_array($result) && sizeof($result) != 0) {
  103. foreach ($result as $row) {
  104. $name = $row['domain_setting_name'];
  105. $category = $row['domain_setting_category'];
  106. $subcategory = $row['domain_setting_subcategory'];
  107. if ($subcategory != '') {
  108. if ($name == "array") {
  109. $_SESSION[$category][] = $row['default_setting_value'];
  110. }
  111. else {
  112. $_SESSION[$category][$name] = $row['default_setting_value'];
  113. }
  114. }
  115. else {
  116. if ($name == "array") {
  117. $_SESSION[$category][$subcategory][] = $row['default_setting_value'];
  118. }
  119. else {
  120. $_SESSION[$category][$subcategory]['uuid'] = $row['default_setting_uuid'];
  121. $_SESSION[$category][$subcategory][$name] = $row['default_setting_value'];
  122. }
  123. }
  124. }
  125. }
  126. unset($result, $row);
  127. //get the $apps array from the installed apps from the core and mod directories
  128. $config_list = glob($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/*/*/app_config.php");
  129. $x=0;
  130. if (isset($config_list)) foreach ($config_list as &$config_path) {
  131. include($config_path);
  132. $x++;
  133. }
  134. //delete the domain data from all tables in the database
  135. if (isset($apps)) foreach ($apps as &$app) {
  136. if (isset($app['db'])) foreach ($app['db'] as $row) {
  137. if (is_array($row['table']['name'])) {
  138. $table_name = $row['table']['name']['text'];
  139. echo "<pre>";
  140. print_r($table_name);
  141. echo "<pre>\n";
  142. }
  143. else {
  144. $table_name = $row['table']['name'];
  145. }
  146. if ($table_name !== "v" && isset($row['fields'])) {
  147. foreach ($row['fields'] as $field) {
  148. if ($field['name'] == 'domain_uuid' && $table_name != 'v_domains') {
  149. $sql = "delete from ".$table_name." where domain_uuid = :domain_uuid ";
  150. $parameters['domain_uuid'] = $id;
  151. $database = new database;
  152. $database->app_name = 'domain_settings';
  153. $database->app_uuid = 'b31e723a-bf70-670c-a49b-470d2a232f71';
  154. $database->execute($sql, $parameters);
  155. unset($sql, $parameters);
  156. }
  157. }
  158. }
  159. }
  160. }
  161. //delete the directories
  162. if (strlen($domain_name) > 0) {
  163. //set the needle
  164. if (count($_SESSION["domains"]) > 1) {
  165. $v_needle = 'v_'.$domain_name.'_';
  166. }
  167. else {
  168. $v_needle = 'v_';
  169. }
  170. //delete the dialplan
  171. @unlink($_SESSION['switch']['dialplan']['dir'].'/'.$domain_name.'.xml');
  172. if (strlen($_SESSION['switch']['dialplan']['dir']) > 0) {
  173. system('rm -rf '.$_SESSION['switch']['dialplan']['dir'].'/'.$domain_name);
  174. }
  175. //delete the dialplan public
  176. @unlink($_SESSION['switch']['dialplan']['dir'].'/public/'.$domain_name.'.xml');
  177. if (strlen($_SESSION['switch']['dialplan']['dir']) > 0) {
  178. system('rm -rf '.$_SESSION['switch']['dialplan']['dir'].'/public/'.$domain_name);
  179. }
  180. //delete the extension
  181. @unlink($_SESSION['switch']['extensions']['dir'].'/'.$domain_name.'.xml');
  182. if (strlen($_SESSION['switch']['extensions']['dir']) > 0) {
  183. system('rm -rf '.$_SESSION['switch']['extensions']['dir'].'/'.$domain_name);
  184. }
  185. //delete fax
  186. if (strlen($_SESSION['switch']['storage']['dir']) > 0) {
  187. system('rm -rf '.$_SESSION['switch']['storage']['dir'].'/fax/'.$domain_name);
  188. }
  189. //delete the gateways
  190. if($dh = opendir($_SESSION['switch']['sip_profiles']['dir'])) {
  191. $files = Array();
  192. while($file = readdir($dh)) {
  193. if($file != "." && $file != ".." && $file[0] != '.') {
  194. if(is_dir($dir . "/" . $file)) {
  195. //this is a directory do nothing
  196. } else {
  197. //check if file extension is xml
  198. if (strpos($file, $v_needle) !== false && substr($file,-4) == '.xml') {
  199. @unlink($_SESSION['switch']['sip_profiles']['dir']."/".$file);
  200. }
  201. }
  202. }
  203. }
  204. closedir($dh);
  205. }
  206. //delete the ivr menu
  207. if($dh = opendir($_SESSION['switch']['conf']['dir']."/ivr_menus")) {
  208. $files = Array();
  209. while($file = readdir($dh)) {
  210. if($file != "." && $file != ".." && $file[0] != '.') {
  211. if(is_dir($dir . "/" . $file)) {
  212. //this is a directory
  213. } else {
  214. if (strpos($file, $v_needle) !== false && substr($file,-4) == '.xml') {
  215. @unlink($_SESSION['switch']['conf']['dir']."/ivr_menus/".$file);
  216. }
  217. }
  218. }
  219. }
  220. closedir($dh);
  221. }
  222. //delete the recordings
  223. if (strlen($_SESSION['switch']['recordings']['dir']) > 0) {
  224. system('rm -rf '.$_SESSION['switch']['recordings']['dir'].'/'.$_SESSION['domain_name'].'/'.$domain_name);
  225. }
  226. //delete voicemail
  227. if (strlen($_SESSION['switch']['voicemail']['dir']) > 0) {
  228. system('rm -rf '.$_SESSION['switch']['voicemail']['dir'].'/'.$domain_name);
  229. }
  230. }
  231. //apply settings reminder
  232. $_SESSION["reload_xml"] = true;
  233. //clear the domains session array to update it
  234. unset($_SESSION["domains"]);
  235. unset($_SESSION["domain_uuid"]);
  236. unset($_SESSION["domain_name"]);
  237. unset($_SESSION['domain']);
  238. unset($_SESSION['switch']);
  239. //remove the domain and save to transactions
  240. $domain_array['domains'][$d]['domain_uuid'] = $id;
  241. //increment the id
  242. $d++;
  243. }
  244. }
  245. //delete the checked rows
  246. if (is_array($domain_array) && @sizeof($domain_array) != 0) {
  247. //execute delete
  248. $database = new database;
  249. $database->app_name = $this->app_name;
  250. $database->app_uuid = $this->app_uuid;
  251. $database->delete($domain_array);
  252. unset($array);
  253. //set message
  254. message::add($text['message-delete']);
  255. }
  256. unset($records);
  257. }
  258. }
  259. }
  260. /**
  261. * toggle a field between two values
  262. */
  263. public function toggle($records) {
  264. if (permission_exists($this->name.'_edit')) {
  265. //add multi-lingual support
  266. $language = new text;
  267. $text = $language->get();
  268. //validate the token
  269. $token = new token;
  270. if (!$token->validate($_SERVER['PHP_SELF'])) {
  271. message::add($text['message-invalid_token'],'negative');
  272. header('Location: '.$this->location);
  273. exit;
  274. }
  275. //toggle the checked records
  276. if (is_array($records) && @sizeof($records) != 0) {
  277. //get current toggle state
  278. foreach($records as $record) {
  279. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  280. $uuids[] = "'".$record['uuid']."'";
  281. }
  282. }
  283. if (is_array($uuids) && @sizeof($uuids) != 0) {
  284. $sql = "select ".$this->name."_uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." ";
  285. $sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  286. $database = new database;
  287. $rows = $database->select($sql, $parameters, 'all');
  288. if (is_array($rows) && @sizeof($rows) != 0) {
  289. foreach ($rows as $row) {
  290. $states[$row['uuid']] = $row['toggle'];
  291. }
  292. }
  293. unset($sql, $parameters, $rows, $row);
  294. }
  295. //build update array
  296. $x = 0;
  297. foreach($states as $uuid => $state) {
  298. //create the array
  299. $array[$this->table][$x][$this->name.'_uuid'] = $uuid;
  300. $array[$this->table][$x][$this->toggle_field] = $state == $this->toggle_values[0] ? $this->toggle_values[1] : $this->toggle_values[0];
  301. //increment the id
  302. $x++;
  303. }
  304. //save the changes
  305. if (is_array($array) && @sizeof($array) != 0) {
  306. //save the array
  307. $database = new database;
  308. $database->app_name = $this->app_name;
  309. $database->app_uuid = $this->app_uuid;
  310. $database->save($array);
  311. unset($array);
  312. //set message
  313. message::add($text['message-toggle']);
  314. }
  315. unset($records, $states);
  316. }
  317. }
  318. }
  319. /**
  320. * copy rows from the database
  321. */
  322. public function copy($records) {
  323. if (permission_exists($this->name.'_add')) {
  324. //add multi-lingual support
  325. $language = new text;
  326. $text = $language->get();
  327. //validate the token
  328. $token = new token;
  329. if (!$token->validate($_SERVER['PHP_SELF'])) {
  330. message::add($text['message-invalid_token'],'negative');
  331. header('Location: '.$this->location);
  332. exit;
  333. }
  334. //copy the checked records
  335. if (is_array($records) && @sizeof($records) != 0) {
  336. //get checked records
  337. foreach($records as $record) {
  338. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  339. $uuids[] = "'".$record['uuid']."'";
  340. }
  341. }
  342. //create the array from existing data
  343. if (is_array($uuids) && @sizeof($uuids) != 0) {
  344. $sql = "select * from v_".$this->table." ";
  345. $sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  346. $database = new database;
  347. $rows = $database->select($sql, $parameters, 'all');
  348. if (is_array($rows) && @sizeof($rows) != 0) {
  349. $x = 0;
  350. foreach ($rows as $row) {
  351. //copy data
  352. $array[$this->table][$x] = $row;
  353. //add copy to the description
  354. $array[$this->table][$x][$this->name.'_uuid'] = uuid();
  355. $array[$this->table][$x][$this->name.'_description'] = trim($row[$this->name.'_description']).' ('.$text['label-copy'].')';
  356. //increment the id
  357. $x++;
  358. }
  359. }
  360. unset($sql, $parameters, $rows, $row);
  361. }
  362. //save the changes and set the message
  363. if (is_array($array) && @sizeof($array) != 0) {
  364. //save the array
  365. $database = new database;
  366. $database->app_name = $this->app_name;
  367. $database->app_uuid = $this->app_uuid;
  368. $database->save($array);
  369. unset($array);
  370. //set message
  371. message::add($text['message-copy']);
  372. }
  373. unset($records);
  374. }
  375. }
  376. }
  377. /**
  378. * add the default settings to the session array
  379. */
  380. public function set() {
  381. //connect to the database if not connected
  382. if (!$this->db) {
  383. require_once "resources/classes/database.php";
  384. $database = new database;
  385. $database->connect();
  386. $this->db = $database->db;
  387. }
  388. //set the PDO error mode
  389. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  390. //get the default settings
  391. $sql = "select * from v_default_settings ";
  392. try {
  393. $prep_statement = $this->db->prepare($sql . " order by default_setting_order asc ");
  394. $prep_statement->execute();
  395. }
  396. catch(PDOException $e) {
  397. $prep_statement = $this->db->prepare($sql);
  398. $prep_statement->execute();
  399. }
  400. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  401. //unset all settings
  402. foreach ($result as $row) {
  403. if ($row['default_setting_category'] != 'user') { //skip off-limit categories
  404. unset($_SESSION[$row['default_setting_category']]);
  405. }
  406. }
  407. //set the enabled settings as a session
  408. foreach ($result as $row) {
  409. if ($row['default_setting_enabled'] == 'true') {
  410. $name = $row['default_setting_name'];
  411. $category = $row['default_setting_category'];
  412. $subcategory = $row['default_setting_subcategory'];
  413. if (strlen($subcategory) == 0) {
  414. if ($name == "array") {
  415. $_SESSION[$category][] = $row['default_setting_value'];
  416. }
  417. else {
  418. $_SESSION[$category][$name] = $row['default_setting_value'];
  419. }
  420. }
  421. else {
  422. if ($name == "array") {
  423. $_SESSION[$category][$subcategory][] = $row['default_setting_value'];
  424. }
  425. else {
  426. $_SESSION[$category][$subcategory]['uuid'] = $row['default_setting_uuid'];
  427. $_SESSION[$category][$subcategory][$name] = $row['default_setting_value'];
  428. }
  429. }
  430. }
  431. }
  432. //get the domains settings
  433. if (strlen($_SESSION["domain_uuid"]) > 0) {
  434. $sql = "select * from v_domain_settings ";
  435. $sql .= "where domain_uuid = '" . $_SESSION["domain_uuid"] . "' ";
  436. $sql .= "and domain_setting_enabled = 'true' ";
  437. try {
  438. $prep_statement = $this->db->prepare($sql . " order by domain_setting_order asc ");
  439. $prep_statement->execute();
  440. }
  441. catch(PDOException $e) {
  442. $prep_statement = $this->db->prepare($sql);
  443. $prep_statement->execute();
  444. }
  445. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  446. //unset the arrays that domains are overriding
  447. foreach ($result as $row) {
  448. $name = $row['domain_setting_name'];
  449. $category = $row['domain_setting_category'];
  450. $subcategory = $row['domain_setting_subcategory'];
  451. if ($name == "array") {
  452. unset($_SESSION[$category][$subcategory]);
  453. }
  454. }
  455. //set the enabled settings as a session
  456. foreach ($result as $row) {
  457. $name = $row['domain_setting_name'];
  458. $category = $row['domain_setting_category'];
  459. $subcategory = $row['domain_setting_subcategory'];
  460. if (strlen($subcategory) == 0) {
  461. //$$category[$name] = $row['domain_setting_value'];
  462. if ($name == "array") {
  463. $_SESSION[$category][] = $row['domain_setting_value'];
  464. }
  465. else {
  466. $_SESSION[$category][$name] = $row['domain_setting_value'];
  467. }
  468. }
  469. else {
  470. //$$category[$subcategory][$name] = $row['domain_setting_value'];
  471. if ($name == "array") {
  472. $_SESSION[$category][$subcategory][] = $row['domain_setting_value'];
  473. }
  474. else {
  475. $_SESSION[$category][$subcategory][$name] = $row['domain_setting_value'];
  476. }
  477. }
  478. }
  479. }
  480. //get the user settings
  481. if (array_key_exists("domain_uuid",$_SESSION) and array_key_exists("user_uuid",$_SESSION) and strlen($_SESSION["domain_uuid"]) > 0 && strlen($_SESSION["user_uuid"]) > 0) {
  482. $sql = "select * from v_user_settings ";
  483. $sql .= "where domain_uuid = '" . $_SESSION["domain_uuid"] . "' ";
  484. $sql .= "and user_uuid = '" . $_SESSION["user_uuid"] . "' ";
  485. try {
  486. $prep_statement = $this->db->prepare($sql . " order by user_setting_order asc ");
  487. $prep_statement->execute();
  488. }
  489. catch(PDOException $e) {
  490. $prep_statement = $this->db->prepare($sql);
  491. $prep_statement->execute();
  492. }
  493. if ($prep_statement) {
  494. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  495. foreach ($result as $row) {
  496. if ($row['user_setting_enabled'] == 'true') {
  497. $name = $row['user_setting_name'];
  498. $category = $row['user_setting_category'];
  499. $subcategory = $row['user_setting_subcategory'];
  500. if (strlen($row['user_setting_value']) > 0) {
  501. if (strlen($subcategory) == 0) {
  502. //$$category[$name] = $row['domain_setting_value'];
  503. if ($name == "array") {
  504. $_SESSION[$category][] = $row['user_setting_value'];
  505. }
  506. else {
  507. $_SESSION[$category][$name] = $row['user_setting_value'];
  508. }
  509. }
  510. else {
  511. //$$category[$subcategory][$name] = $row['domain_setting_value'];
  512. if ($name == "array") {
  513. $_SESSION[$category][$subcategory][] = $row['user_setting_value'];
  514. }
  515. else {
  516. $_SESSION[$category][$subcategory][$name] = $row['user_setting_value'];
  517. }
  518. }
  519. }
  520. }
  521. }
  522. }
  523. }
  524. //set the PDO error mode
  525. $this->db->setAttribute(PDO::ATTR_ERRMODE, '');
  526. //set the values from the session variables
  527. if (strlen($_SESSION['domain']['time_zone']['name']) > 0) {
  528. //server time zone
  529. $_SESSION['time_zone']['system'] = date_default_timezone_get();
  530. //domain time zone set in system settings
  531. $_SESSION['time_zone']['domain'] = $_SESSION['domain']['time_zone']['name'];
  532. //set the domain time zone as the default time zone
  533. date_default_timezone_set($_SESSION['domain']['time_zone']['name']);
  534. }
  535. //set the context
  536. $_SESSION["context"] = $_SESSION["domain_name"];
  537. }
  538. /**
  539. * upgrade application defaults
  540. */
  541. public function upgrade() {
  542. //connect to the database if not connected
  543. if (!$this->db) {
  544. require_once "resources/classes/database.php";
  545. $database = new database;
  546. $database->connect();
  547. $this->db = $database->db;
  548. }
  549. //get the variables
  550. $config = new config;
  551. $config_exists = $config->exists();
  552. $config_path = $config->find();
  553. $config->get();
  554. $db_type = $config->db_type;
  555. $db_name = $config->db_name;
  556. $db_username = $config->db_username;
  557. $db_password = $config->db_password;
  558. $db_secure = $config->db_secure;
  559. $db_cert_authority = $config->db_cert_authority;
  560. $db_host = $config->db_host;
  561. $db_path = $config->db_path;
  562. $db_port = $config->db_port;
  563. //get the PROJECT PATH
  564. include "root.php";
  565. //check for default settings
  566. $this->settings();
  567. //get the list of installed apps from the core and app directories (note: GLOB_BRACE doesn't work on some systems)
  568. $config_list_1 = glob($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/*/*/app_config.php");
  569. $config_list_2 = glob($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/*/*/app_menu.php");
  570. $config_list = array_merge((array)$config_list_1, (array)$config_list_2);
  571. unset($config_list_1,$config_list_2);
  572. $db = $this->db;
  573. $x=0;
  574. foreach ($config_list as &$config_path) {
  575. $app_path = dirname($config_path);
  576. $app_path = preg_replace('/\A.*(\/.*\/.*)\z/', '$1', $app_path);
  577. include($config_path);
  578. $x++;
  579. }
  580. //get the domains
  581. $sql = "select * from v_domains ";
  582. $prep_statement = $this->db->prepare($sql);
  583. $prep_statement->execute();
  584. $domains = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  585. unset($prep_statement);
  586. //get the domain_settings
  587. $sql = "select * from v_domain_settings ";
  588. $sql .= "where domain_setting_enabled = 'true' ";
  589. $prep_statement = $this->db->prepare($sql);
  590. $prep_statement->execute();
  591. $domain_settings = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  592. unset($prep_statement);
  593. //get the default settings
  594. $sql = "select * from v_default_settings ";
  595. $sql .= "where default_setting_enabled = 'true' ";
  596. $prep_statement = $this->db->prepare($sql);
  597. $prep_statement->execute();
  598. $database_default_settings = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  599. unset($prep_statement);
  600. //get the domain_uuid
  601. foreach($domains as $row) {
  602. if (count($domains) == 1) {
  603. $_SESSION["domain_uuid"] = $row["domain_uuid"];
  604. $_SESSION["domain_name"] = $row['domain_name'];
  605. }
  606. else {
  607. if (lower_case($row['domain_name']) == lower_case($domain_array[0]) || lower_case($row['domain_name']) == lower_case('www.'.$domain_array[0])) {
  608. $_SESSION["domain_uuid"] = $row["domain_uuid"];
  609. $_SESSION["domain_name"] = $row['domain_name'];
  610. }
  611. $_SESSION['domains'][$row['domain_uuid']]['domain_uuid'] = $row['domain_uuid'];
  612. $_SESSION['domains'][$row['domain_uuid']]['domain_name'] = $row['domain_name'];
  613. }
  614. }
  615. //loop through all domains
  616. $domain_count = count($domains);
  617. $domains_processed = 1;
  618. foreach ($domains as &$row) {
  619. //get the values from database and set them as php variables
  620. $domain_uuid = $row["domain_uuid"];
  621. $domain_name = $row["domain_name"];
  622. //get the context
  623. $context = $domain_name;
  624. //get the default settings - this needs to be done to reset the session values back to the defaults for each domain in the loop
  625. foreach($database_default_settings as $row) {
  626. $name = $row['default_setting_name'];
  627. $category = $row['default_setting_category'];
  628. $subcategory = $row['default_setting_subcategory'];
  629. if (strlen($subcategory) == 0) {
  630. if ($name == "array") {
  631. $_SESSION[$category][] = $row['default_setting_value'];
  632. }
  633. else {
  634. $_SESSION[$category][$name] = $row['default_setting_value'];
  635. }
  636. }
  637. else {
  638. if ($name == "array") {
  639. $_SESSION[$category][$subcategory][] = $row['default_setting_value'];
  640. }
  641. else {
  642. $_SESSION[$category][$subcategory]['uuid'] = $row['default_setting_uuid'];
  643. $_SESSION[$category][$subcategory][$name] = $row['default_setting_value'];
  644. }
  645. }
  646. }
  647. //get the domains settings for the current domain
  648. foreach($domain_settings as $row) {
  649. if ($row['domain_uuid'] == $domain_uuid) {
  650. $name = $row['domain_setting_name'];
  651. $category = $row['domain_setting_category'];
  652. $subcategory = $row['domain_setting_subcategory'];
  653. if (strlen($subcategory) == 0) {
  654. //$$category[$name] = $row['domain_setting_value'];
  655. $_SESSION[$category][$name] = $row['domain_setting_value'];
  656. }
  657. else {
  658. //$$category[$subcategory][$name] = $row['domain_setting_value'];
  659. $_SESSION[$category][$subcategory][$name] = $row['domain_setting_value'];
  660. }
  661. }
  662. }
  663. //get the list of installed apps from the core and mod directories and execute the php code in app_defaults.php
  664. $default_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_defaults.php");
  665. foreach ($default_list as &$default_path) {
  666. include($default_path);
  667. }
  668. //track of the number of domains processed
  669. $domains_processed++;
  670. }
  671. //synchronize the dialplan
  672. if (function_exists('save_dialplan_xml')) {
  673. save_dialplan_xml();
  674. }
  675. //update config.lua
  676. if (file_exists($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/app/scripts/resources/classes/scripts.php')) {
  677. $obj = new scripts;
  678. $obj->write_config();
  679. }
  680. //clear the session variables
  681. unset($_SESSION['domain']);
  682. unset($_SESSION['switch']);
  683. } //end upgrade method
  684. /**
  685. * add missing default settings
  686. * update the uuid for older default settings that were added before the uuids was predefined.
  687. */
  688. public function settings() {
  689. //connect to the database if not connected
  690. if (!$this->db) {
  691. require_once "resources/classes/database.php";
  692. $database = new database;
  693. $database->connect();
  694. $this->db = $database->db;
  695. }
  696. //get the list of installed apps from the core and mod directories
  697. $config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php");
  698. $x=0;
  699. foreach ($config_list as $config_path) {
  700. include($config_path);
  701. $x++;
  702. }
  703. $x = 0;
  704. foreach ($apps as $app) {
  705. if (is_array($app['default_settings'])) {
  706. foreach ($app['default_settings'] as $setting) {
  707. $array[$x] = ($setting);
  708. $array[$x]['app_uuid'] = $app['uuid'];
  709. $x++;
  710. }
  711. }
  712. }
  713. //get an array of the default settings
  714. $sql = "select * from v_default_settings ";
  715. $sql .= "order by default_setting_category asc, default_setting_subcategory asc";
  716. $prep_statement = $this->db->prepare($sql);
  717. $prep_statement->execute();
  718. $default_settings = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  719. unset ($prep_statement, $sql);
  720. //named array
  721. foreach ($default_settings as $row) {
  722. $default_settings[$row['default_setting_category']][$row['default_setting_subcategory']][$row['default_setting_name']]['uuid'] = $row['default_setting_uuid'];
  723. $default_settings[$row['default_setting_category']][$row['default_setting_subcategory']][$row['default_setting_name']]['value'] = $row['default_setting_value'];
  724. $default_settings[$row['default_setting_category']][$row['default_setting_subcategory']][$row['default_setting_name']]['app_uuid'] = $row['app_uuid'];
  725. //echo "[".$row['default_setting_category']."][".$row['default_setting_subcategory']."][".$row['default_setting_name']."] = ".$row['default_setting_value']."\n";
  726. }
  727. //update matching settings with the correct default_setting_uuid and app_uuid and if they exist remove them from the array
  728. $x = 0;
  729. foreach ($array as $row) {
  730. $category = $row['default_setting_category'];
  731. $subcategory = $row['default_setting_subcategory'];
  732. $name = $row['default_setting_name'];
  733. if (isset($default_settings[$category][$subcategory][$name]['value'])) {
  734. //set the variables
  735. $default_setting_uuid = $default_settings[$category][$subcategory][$name]['uuid'];
  736. $app_uuid = $default_settings[$category][$subcategory][$name]['app_uuid'];
  737. //update matching settings
  738. if ($app_uuid == null) {
  739. $sql = "update v_default_settings set ";
  740. if ($default_setting_uuid != $row['default_setting_uuid']) {
  741. $sql .= "default_setting_uuid = '".$row['default_setting_uuid']."', ";
  742. }
  743. $sql .= "app_uuid = '".$row['app_uuid']."' ";
  744. $sql .= "where default_setting_uuid = '".$row['default_setting_uuid']."';";
  745. //echo $category." ".$subcategory." ".$name." ".$app_uuid."\n";
  746. //echo $sql."\n";
  747. $this->db->exec(check_sql($sql));
  748. //echo "\n";
  749. }
  750. //remove settings from the array that were found
  751. unset($array[$x]);
  752. }
  753. $x++;
  754. }
  755. unset($default_settings);
  756. //get the missing count
  757. $array_count = count($array);
  758. //add the missing default settings
  759. if (is_array($array) && count($array) > 0) {
  760. foreach ($array as $row) {
  761. $sql = "insert into v_default_settings (";
  762. $sql .= "default_setting_uuid, ";
  763. $sql .= "default_setting_category, ";
  764. $sql .= "default_setting_subcategory, ";
  765. $sql .= "default_setting_name, ";
  766. $sql .= "default_setting_value, ";
  767. $sql .= "default_setting_enabled, ";
  768. $sql .= "default_setting_description ";
  769. $sql .= ") values \n";
  770. $sql .= "(";
  771. $sql .= "'".check_str($row['default_setting_uuid'])."', ";
  772. $sql .= "'".check_str($row['default_setting_category'])."', ";
  773. $sql .= "'".check_str($row['default_setting_subcategory'])."', ";
  774. $sql .= "'".check_str($row['default_setting_name'])."', ";
  775. $sql .= "'".check_str($row['default_setting_value'])."', ";
  776. $sql .= "'".check_str($row['default_setting_enabled'])."', ";
  777. $sql .= "'".check_str($row['default_setting_description'])."' ";
  778. $sql .= ");";
  779. //echo $sql."\n";
  780. $this->db->exec(check_sql($sql));
  781. unset($array);
  782. }
  783. }
  784. } //end settings method
  785. }
  786. }
  787. ?>