domains.php 26 KB

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