domains.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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-2023
  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. * delete rows from the database
  56. */
  57. public function delete($records) {
  58. if (permission_exists($this->name.'_delete')) {
  59. //add multi-lingual support
  60. $language = new text;
  61. $text = $language->get();
  62. //validate the token
  63. $token = new token;
  64. if (!$token->validate($_SERVER['PHP_SELF'])) {
  65. message::add($text['message-invalid_token'],'negative');
  66. header('Location: '.$this->location);
  67. exit;
  68. }
  69. //delete multiple records
  70. if (is_array($records) && @sizeof($records) != 0) {
  71. //build the delete array
  72. $d = 0;
  73. foreach ($records as $record) {
  74. //add to the array
  75. if (!empty($record['checked']) && $record['checked'] == 'true' && is_uuid($record['uuid'])) {
  76. //set the uuid
  77. $id = $record['uuid'];
  78. //get the domain using the id
  79. $sql = "select domain_name from v_domains ";
  80. $sql .= "where domain_uuid = :domain_uuid ";
  81. $parameters['domain_uuid'] = $id;
  82. $database = new database;
  83. $domain_name = $database->select($sql, $parameters, 'column');
  84. unset($sql, $parameters);
  85. //get the domain settings
  86. $sql = "select * from v_domain_settings ";
  87. $sql .= "where domain_uuid = :domain_uuid ";
  88. $sql .= "and domain_setting_enabled = 'true' ";
  89. $parameters['domain_uuid'] = $id;
  90. $database = new database;
  91. $result = $database->select($sql, $parameters, 'all');
  92. unset($sql, $parameters);
  93. if (is_array($result) && sizeof($result) != 0) {
  94. foreach ($result as $row) {
  95. $name = $row['domain_setting_name'];
  96. $category = $row['domain_setting_category'];
  97. $subcategory = $row['domain_setting_subcategory'];
  98. if ($subcategory != '') {
  99. if ($name == "array") {
  100. $_SESSION[$category][] = $row['default_setting_value'];
  101. }
  102. else {
  103. $_SESSION[$category][$name] = $row['default_setting_value'];
  104. }
  105. }
  106. else {
  107. if ($name == "array") {
  108. $_SESSION[$category][$subcategory][] = $row['default_setting_value'];
  109. }
  110. else {
  111. $_SESSION[$category][$subcategory]['uuid'] = $row['default_setting_uuid'];
  112. $_SESSION[$category][$subcategory][$name] = $row['default_setting_value'];
  113. }
  114. }
  115. }
  116. }
  117. unset($result, $row);
  118. //get the $apps array from the installed apps from the core and mod directories
  119. $config_list = glob($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/*/*/app_config.php");
  120. $x=0;
  121. if (isset($config_list)) foreach ($config_list as &$config_path) {
  122. include($config_path);
  123. $x++;
  124. }
  125. //delete the domain data from all tables in the database
  126. if (isset($apps)) foreach ($apps as &$app) {
  127. if (isset($app['db'])) foreach ($app['db'] as $row) {
  128. if (is_array($row['table']['name'])) {
  129. $table_name = $row['table']['name']['text'];
  130. echo "<pre>";
  131. print_r($table_name);
  132. echo "<pre>\n";
  133. }
  134. else {
  135. $table_name = $row['table']['name'];
  136. }
  137. if ($table_name !== "v" && isset($row['fields'])) {
  138. foreach ($row['fields'] as $field) {
  139. if ($field['name'] == 'domain_uuid' && $table_name != 'v_domains') {
  140. $sql = "delete from ".$table_name." where domain_uuid = :domain_uuid ";
  141. $parameters['domain_uuid'] = $id;
  142. $database = new database;
  143. $database->app_name = 'domain_settings';
  144. $database->app_uuid = 'b31e723a-bf70-670c-a49b-470d2a232f71';
  145. $database->execute($sql, $parameters);
  146. unset($sql, $parameters);
  147. }
  148. }
  149. }
  150. }
  151. }
  152. //delete the directories
  153. if (!empty($domain_name)) {
  154. //set the needle
  155. if (count($_SESSION["domains"]) > 1) {
  156. $v_needle = 'v_'.$domain_name.'_';
  157. }
  158. else {
  159. $v_needle = 'v_';
  160. }
  161. //delete the dialplan
  162. @unlink($_SESSION['switch']['dialplan']['dir'].'/'.$domain_name.'.xml');
  163. if (!empty($_SESSION['switch']['dialplan']['dir'])) {
  164. system('rm -rf '.$_SESSION['switch']['dialplan']['dir'].'/'.$domain_name);
  165. }
  166. //delete the dialplan public
  167. @unlink($_SESSION['switch']['dialplan']['dir'].'/public/'.$domain_name.'.xml');
  168. if (!empty($_SESSION['switch']['dialplan']['dir'])) {
  169. system('rm -rf '.$_SESSION['switch']['dialplan']['dir'].'/public/'.$domain_name);
  170. }
  171. //delete the extension
  172. @unlink($_SESSION['switch']['extensions']['dir'].'/'.$domain_name.'.xml');
  173. if (!empty($_SESSION['switch']['extensions']['dir'])) {
  174. system('rm -rf '.$_SESSION['switch']['extensions']['dir'].'/'.$domain_name);
  175. }
  176. //delete fax
  177. if (!empty($_SESSION['switch']['storage']['dir'])) {
  178. system('rm -rf '.$_SESSION['switch']['storage']['dir'].'/fax/'.$domain_name);
  179. }
  180. //delete the gateways
  181. if (!empty($_SESSION['switch']['sip_profiles']['dir'])) {
  182. if ($dh = opendir($_SESSION['switch']['sip_profiles']['dir'])) {
  183. $files = Array();
  184. while ($file = readdir($dh)) {
  185. if ($file != "." && $file != ".." && $file[0] != '.') {
  186. if (is_dir($dir . "/" . $file)) {
  187. //this is a directory do nothing
  188. }
  189. else {
  190. //check if file extension is xml
  191. if (strpos($file, $v_needle) !== false && substr($file,-4) == '.xml') {
  192. @unlink($_SESSION['switch']['sip_profiles']['dir']."/".$file);
  193. }
  194. }
  195. }
  196. }
  197. closedir($dh);
  198. }
  199. }
  200. //delete the ivr menu
  201. if (!empty($_SESSION['switch']['conf']['dir'])) {
  202. if ($dh = opendir($_SESSION['switch']['conf']['dir']."/ivr_menus")) {
  203. $files = Array();
  204. while ($file = readdir($dh)) {
  205. if ($file != "." && $file != ".." && $file[0] != '.') {
  206. if (!empty($dir) && !empty($file) && is_dir($dir."/".$file)) {
  207. //this is a directory
  208. }
  209. else {
  210. if (strpos($file, $v_needle) !== false && substr($file,-4) == '.xml') {
  211. @unlink($_SESSION['switch']['conf']['dir']."/ivr_menus/".$file);
  212. }
  213. }
  214. }
  215. }
  216. closedir($dh);
  217. }
  218. }
  219. //delete the recordings
  220. if (!empty($_SESSION['switch']['recordings']['dir'])) {
  221. system('rm -rf '.$_SESSION['switch']['recordings']['dir'].'/'.$_SESSION['domain_name'].'/'.$domain_name);
  222. }
  223. //delete voicemail
  224. if (!empty($_SESSION['switch']['voicemail']['dir'])) {
  225. system('rm -rf '.$_SESSION['switch']['voicemail']['dir'].'/'.$domain_name);
  226. }
  227. }
  228. //apply settings reminder
  229. $_SESSION["reload_xml"] = true;
  230. //clear the domains session array to update it
  231. unset($_SESSION["domains"]);
  232. unset($_SESSION['domain']);
  233. unset($_SESSION['switch']);
  234. //remove the domain and save to transactions
  235. $domain_array['domains'][$d]['domain_uuid'] = $id;
  236. //increment the id
  237. $d++;
  238. }
  239. }
  240. //delete the checked rows
  241. if (is_array($domain_array) && @sizeof($domain_array) != 0) {
  242. //execute delete
  243. $database = new database;
  244. $database->app_name = $this->app_name;
  245. $database->app_uuid = $this->app_uuid;
  246. $database->delete($domain_array);
  247. unset($array);
  248. //set message
  249. message::add($text['message-delete']);
  250. }
  251. unset($records);
  252. }
  253. }
  254. }
  255. /**
  256. * toggle a field between two values
  257. */
  258. public function toggle($records) {
  259. if (permission_exists($this->name.'_edit')) {
  260. //add multi-lingual support
  261. $language = new text;
  262. $text = $language->get();
  263. //validate the token
  264. $token = new token;
  265. if (!$token->validate($_SERVER['PHP_SELF'])) {
  266. message::add($text['message-invalid_token'],'negative');
  267. header('Location: '.$this->location);
  268. exit;
  269. }
  270. //toggle the checked records
  271. if (is_array($records) && @sizeof($records) != 0) {
  272. //get current toggle state
  273. foreach($records as $record) {
  274. if (!empty($record['checked']) && $record['checked'] == 'true' && is_uuid($record['uuid'])) {
  275. $uuids[] = "'".$record['uuid']."'";
  276. }
  277. }
  278. if (is_array($uuids) && @sizeof($uuids) != 0) {
  279. $sql = "select ".$this->name."_uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." ";
  280. $sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  281. $database = new database;
  282. $rows = $database->select($sql, $parameters ?? null, 'all');
  283. if (is_array($rows) && @sizeof($rows) != 0) {
  284. foreach ($rows as $row) {
  285. $states[$row['uuid']] = $row['toggle'];
  286. }
  287. }
  288. unset($sql, $parameters, $rows, $row);
  289. }
  290. //build update array
  291. $x = 0;
  292. foreach($states as $uuid => $state) {
  293. //create the array
  294. $array[$this->table][$x][$this->name.'_uuid'] = $uuid;
  295. $array[$this->table][$x][$this->toggle_field] = $state == $this->toggle_values[0] ? $this->toggle_values[1] : $this->toggle_values[0];
  296. //increment the id
  297. $x++;
  298. }
  299. //save the changes
  300. if (is_array($array) && @sizeof($array) != 0) {
  301. //save the array
  302. $database = new database;
  303. $database->app_name = $this->app_name;
  304. $database->app_uuid = $this->app_uuid;
  305. $database->save($array);
  306. unset($array);
  307. //set message
  308. message::add($text['message-toggle']);
  309. }
  310. unset($records, $states);
  311. }
  312. }
  313. }
  314. /**
  315. * copy rows from the database
  316. */
  317. public function copy($records) {
  318. if (permission_exists($this->name.'_add')) {
  319. //add multi-lingual support
  320. $language = new text;
  321. $text = $language->get();
  322. //validate the token
  323. $token = new token;
  324. if (!$token->validate($_SERVER['PHP_SELF'])) {
  325. message::add($text['message-invalid_token'],'negative');
  326. header('Location: '.$this->location);
  327. exit;
  328. }
  329. //copy the checked records
  330. if (is_array($records) && @sizeof($records) != 0) {
  331. //get checked records
  332. foreach($records as $record) {
  333. if (!empty($record['checked']) && $record['checked'] == 'true' && is_uuid($record['uuid'])) {
  334. $uuids[] = "'".$record['uuid']."'";
  335. }
  336. }
  337. //create the array from existing data
  338. if (is_array($uuids) && @sizeof($uuids) != 0) {
  339. $sql = "select * from v_".$this->table." ";
  340. $sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  341. $database = new database;
  342. $rows = $database->select($sql, $parameters, 'all');
  343. if (is_array($rows) && @sizeof($rows) != 0) {
  344. $x = 0;
  345. foreach ($rows as $row) {
  346. //copy data
  347. $array[$this->table][$x] = $row;
  348. //add copy to the description
  349. $array[$this->table][$x][$this->name.'_uuid'] = uuid();
  350. $array[$this->table][$x][$this->name.'_description'] = trim($row[$this->name.'_description']).' ('.$text['label-copy'].')';
  351. //increment the id
  352. $x++;
  353. }
  354. }
  355. unset($sql, $parameters, $rows, $row);
  356. }
  357. //save the changes and set the message
  358. if (is_array($array) && @sizeof($array) != 0) {
  359. //save the array
  360. $database = new database;
  361. $database->app_name = $this->app_name;
  362. $database->app_uuid = $this->app_uuid;
  363. $database->save($array);
  364. unset($array);
  365. //set message
  366. message::add($text['message-copy']);
  367. }
  368. unset($records);
  369. }
  370. }
  371. }
  372. /**
  373. * add default, domain and user settings to the session array
  374. */
  375. public function set() {
  376. //get previous domain settings
  377. if (isset($_SESSION["previous_domain_uuid"])) {
  378. $sql = "select * from v_domain_settings ";
  379. $sql .= "where domain_uuid = :previous_domain_uuid ";
  380. $sql .= "and domain_setting_enabled = 'true' ";
  381. $sql .= " order by domain_setting_order asc ";
  382. $parameters['previous_domain_uuid'] = $_SESSION["previous_domain_uuid"];
  383. $database = new database;
  384. $result = $database->select($sql, $parameters, 'all');
  385. unset($sql, $parameters);
  386. //unset previous domain settings
  387. foreach ($result as $row) {
  388. if ($row['domain_setting_category'] != 'user') { //skip off-limit categories
  389. unset($_SESSION[$row['domain_setting_category']][$row['domain_setting_subcategory']]);
  390. }
  391. }
  392. unset($_SESSION["previous_domain_uuid"]);
  393. }
  394. //get the default settings
  395. $sql = "select * from v_default_settings ";
  396. $sql .= "order by default_setting_order asc ";
  397. $database = new database;
  398. $result = $database->select($sql, null, 'all');
  399. unset($sql, $parameters);
  400. //unset all settings
  401. foreach ($result as $row) {
  402. if ($row['default_setting_category'] != 'user') { //skip off-limit categories
  403. unset($_SESSION[$row['default_setting_category']][$row['default_setting_subcategory']]);
  404. }
  405. }
  406. //set the enabled settings as a session
  407. foreach ($result as $row) {
  408. if ($row['default_setting_enabled'] == 'true') {
  409. $name = $row['default_setting_name'];
  410. $category = $row['default_setting_category'];
  411. $subcategory = $row['default_setting_subcategory'];
  412. if (empty($subcategory)) {
  413. if ($name == "array") {
  414. $_SESSION[$category][] = $row['default_setting_value'];
  415. }
  416. else {
  417. $_SESSION[$category][$name] = $row['default_setting_value'];
  418. }
  419. }
  420. else {
  421. if ($name == "array") {
  422. $_SESSION[$category][$subcategory][] = $row['default_setting_value'];
  423. }
  424. else {
  425. $_SESSION[$category][$subcategory]['uuid'] = $row['default_setting_uuid'];
  426. $_SESSION[$category][$subcategory][$name] = $row['default_setting_value'];
  427. }
  428. }
  429. }
  430. }
  431. //get the domains settings
  432. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/domains/app_config.php")) {
  433. include "app/domains/resources/settings.php";
  434. }
  435. //get the domains settings
  436. if (!empty($_SESSION["domain_uuid"]) && is_uuid($_SESSION["domain_uuid"])) {
  437. //get settings from the database
  438. $sql = "select * from v_domain_settings ";
  439. $sql .= "where domain_uuid = :domain_uuid ";
  440. $sql .= "and domain_setting_enabled = 'true' ";
  441. $sql .= " order by domain_setting_order asc ";
  442. $parameters['domain_uuid'] = $_SESSION["domain_uuid"];
  443. $database = new database;
  444. $result = $database->select($sql, $parameters, 'all');
  445. unset($sql, $parameters);
  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 (empty($subcategory)) {
  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) && array_key_exists("user_uuid",$_SESSION) && is_uuid($_SESSION["domain_uuid"])) {
  482. $sql = "select * from v_user_settings ";
  483. $sql .= "where domain_uuid = :domain_uuid ";
  484. $sql .= "and user_uuid = :user_uuid ";
  485. $sql .= " order by user_setting_order asc ";
  486. $parameters['domain_uuid'] = $_SESSION["domain_uuid"];
  487. $parameters['user_uuid'] = $_SESSION["user_uuid"];
  488. $database = new database;
  489. $result = $database->select($sql, $parameters, 'all');
  490. if (is_array($result)) {
  491. foreach ($result as $row) {
  492. if ($row['user_setting_enabled'] == 'true') {
  493. $name = $row['user_setting_name'];
  494. $category = $row['user_setting_category'];
  495. $subcategory = $row['user_setting_subcategory'];
  496. if (!empty($row['user_setting_value'])) {
  497. if (empty($subcategory)) {
  498. //$$category[$name] = $row['domain_setting_value'];
  499. if ($name == "array") {
  500. $_SESSION[$category][] = $row['user_setting_value'];
  501. }
  502. else {
  503. $_SESSION[$category][$name] = $row['user_setting_value'];
  504. }
  505. }
  506. else {
  507. //$$category[$subcategory][$name] = $row['domain_setting_value'];
  508. if ($name == "array") {
  509. $_SESSION[$category][$subcategory][] = $row['user_setting_value'];
  510. }
  511. else {
  512. $_SESSION[$category][$subcategory][$name] = $row['user_setting_value'];
  513. }
  514. }
  515. }
  516. }
  517. }
  518. }
  519. }
  520. //set the values from the session variables
  521. if (!empty($_SESSION['domain']['time_zone']['name'])) {
  522. //server time zone
  523. $_SESSION['time_zone']['system'] = date_default_timezone_get();
  524. //domain time zone set in system settings
  525. $_SESSION['time_zone']['domain'] = $_SESSION['domain']['time_zone']['name'];
  526. //set the domain time zone as the default time zone
  527. date_default_timezone_set($_SESSION['domain']['time_zone']['name']);
  528. }
  529. //set the context
  530. if (!empty($_SESSION["domain_name"])) {
  531. $_SESSION["context"] = $_SESSION["domain_name"];
  532. }
  533. }
  534. /**
  535. * upgrade application defaults
  536. */
  537. public function upgrade() {
  538. //includes files
  539. require dirname(__DIR__, 2) . "/resources/require.php";
  540. //get the variables
  541. $config = new config;
  542. $config_path = $config->find();
  543. $config->get();
  544. //check for default settings
  545. $this->settings();
  546. //get the list of installed apps from the core and app directories (note: GLOB_BRACE doesn't work on some systems)
  547. $config_list_1 = glob($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/*/*/app_config.php");
  548. $config_list_2 = glob($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/*/*/app_menu.php");
  549. $config_list = array_merge((array)$config_list_1, (array)$config_list_2);
  550. unset($config_list_1,$config_list_2);
  551. $x=0;
  552. foreach ($config_list as &$config_path) {
  553. $app_path = dirname($config_path);
  554. $app_path = preg_replace('/\A.*(\/.*\/.*)\z/', '$1', $app_path);
  555. include($config_path);
  556. $x++;
  557. }
  558. //get the domains
  559. $sql = "select * from v_domains ";
  560. $database = new database;
  561. $domains = $database->select($sql, null, 'all');
  562. unset($sql);
  563. //get the domain_settings
  564. $sql = "select * from v_domain_settings ";
  565. $sql .= "where domain_setting_enabled = 'true' ";
  566. $database = new database;
  567. $domain_settings = $database->select($sql, null, 'all');
  568. unset($sql);
  569. //get the default settings
  570. $sql = "select * from v_default_settings ";
  571. $sql .= "where default_setting_enabled = 'true' ";
  572. $sql .= "and default_setting_category <> 'switch' ";
  573. $database = new database;
  574. $database_default_settings = $database->select($sql, null, 'all');
  575. unset($sql);
  576. //get the domain_uuid
  577. if (is_array($domains)) {
  578. foreach($domains as $row) {
  579. if (count($domains) == 1) {
  580. $_SESSION["domain_uuid"] = $row["domain_uuid"];
  581. $_SESSION["domain_name"] = $row['domain_name'];
  582. }
  583. else {
  584. if (!empty($domain_array[0]) && (lower_case($row['domain_name']) == lower_case($domain_array[0] ?? '') || lower_case($row['domain_name']) == lower_case('www.'.$domain_array[0] ?? ''))) {
  585. $_SESSION["domain_uuid"] = $row["domain_uuid"];
  586. $_SESSION["domain_name"] = $row['domain_name'];
  587. }
  588. $_SESSION['domains'][$row['domain_uuid']]['domain_uuid'] = $row['domain_uuid'];
  589. $_SESSION['domains'][$row['domain_uuid']]['domain_name'] = $row['domain_name'];
  590. }
  591. }
  592. }
  593. //loop through all domains
  594. $domains_processed = 1;
  595. foreach ($domains as $domain) {
  596. //get the values from database and set them as php variables
  597. $domain_uuid = $domain["domain_uuid"];
  598. $domain_name = $domain["domain_name"];
  599. //get the context
  600. $context = $domain_name;
  601. //get the default settings - this needs to be done to reset the session values back to the defaults for each domain in the loop
  602. foreach($database_default_settings as $row) {
  603. $name = $row['default_setting_name'];
  604. $category = $row['default_setting_category'];
  605. $subcategory = $row['default_setting_subcategory'];
  606. if (empty($subcategory)) {
  607. if (!isset($_SESSION[$category])) {
  608. $_SESSION[$category] = [];
  609. }
  610. if ($name == "array") {
  611. $_SESSION[$category][] = $row['default_setting_value'];
  612. }
  613. else {
  614. $_SESSION[$category][$name] = $row['default_setting_value'];
  615. }
  616. }
  617. else {
  618. if (!isset($_SESSION[$category])) {
  619. $_SESSION[$category] = [];
  620. }
  621. if (!isset($_SESSION[$category][$subcategory])) {
  622. $_SESSION[$category][$subcategory] = [];
  623. }
  624. if ($name == "array") {
  625. $_SESSION[$category][$subcategory][] = $row['default_setting_value'];
  626. }
  627. else {
  628. $_SESSION[$category][$subcategory]['uuid'] = $row['default_setting_uuid'];
  629. $_SESSION[$category][$subcategory][$name] = $row['default_setting_value'];
  630. }
  631. }
  632. }
  633. //get the domains settings for the current domain
  634. foreach($domain_settings as $row) {
  635. if ($row['domain_uuid'] == $domain_uuid) {
  636. $name = $row['domain_setting_name'];
  637. $category = $row['domain_setting_category'];
  638. $subcategory = $row['domain_setting_subcategory'];
  639. if (empty($subcategory)) {
  640. //$$category[$name] = $row['domain_setting_value'];
  641. $_SESSION[$category][$name] = $row['domain_setting_value'];
  642. }
  643. else {
  644. //$$category[$subcategory][$name] = $row['domain_setting_value'];
  645. $_SESSION[$category][$subcategory][$name] = $row['domain_setting_value'];
  646. }
  647. }
  648. }
  649. //get the list of installed apps from the core and mod directories and execute the php code in app_defaults.php
  650. $default_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_defaults.php");
  651. foreach ($default_list as &$default_path) {
  652. include($default_path);
  653. }
  654. //track of the number of domains processed
  655. $domains_processed++;
  656. }
  657. //clear the session variables
  658. unset($_SESSION['domain']);
  659. unset($_SESSION['switch']);
  660. } //end upgrade method
  661. /**
  662. * add missing default settings
  663. * update the uuid for older default settings that were added before the uuids was predefined.
  664. */
  665. public function settings() {
  666. //includes files
  667. include "resources/require.php";
  668. //get an array of the default settings UUIDs
  669. $sql = "select * from v_default_settings ";
  670. $database = new database;
  671. $result = $database->select($sql, null, 'all');
  672. foreach($result as $row) {
  673. $setting[$row['default_setting_uuid']] = 1;
  674. }
  675. unset($sql);
  676. //get the list of default settings
  677. $config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php");
  678. $x=0;
  679. foreach ($config_list as $config_path) {
  680. include($config_path);
  681. $x++;
  682. }
  683. $x = 0;
  684. foreach ($apps as $app) {
  685. if (isset($app['default_settings']) && is_array($app['default_settings'])) {
  686. foreach ($app['default_settings'] as $row) {
  687. if (!isset($setting[$row['default_setting_uuid']])) {
  688. $array['default_settings'][$x] = $row;
  689. $array['default_settings'][$x]['app_uuid'] = $app['uuid'];
  690. $x++;
  691. }
  692. }
  693. }
  694. }
  695. //add the missing default settings
  696. if (isset($array) && is_array($array) && count($array) > 0) {
  697. //grant temporary permissions
  698. $p = new permissions;
  699. $p->add('default_setting_add', 'temp');
  700. //execute insert
  701. $database = new database;
  702. $database->app_name = 'default_settings';
  703. $database->app_uuid = '2c2453c0-1bea-4475-9f44-4d969650de09';
  704. $database->save($array, false);
  705. unset($array);
  706. //revoke temporary permissions
  707. $p->delete('default_setting_add', 'temp');
  708. }
  709. } //end settings method
  710. }
  711. }
  712. ?>