domains.php 26 KB

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