domains.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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) 2018 - 2022
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //includes files
  22. require_once dirname(__DIR__, 2) . "/resources/require.php";
  23. require_once "resources/check_auth.php";
  24. require_once "resources/paging.php";
  25. //redirect admin to app instead
  26. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/domains/app_config.php") && !permission_exists('domain_all') && !is_cli()) {
  27. header("Location: ".PROJECT_PATH."/app/domains/domains.php");
  28. exit;
  29. }
  30. //change the domain
  31. if (!empty($_GET["domain_uuid"]) && is_uuid($_GET["domain_uuid"]) && $_GET["domain_change"] == "true") {
  32. if (permission_exists('domain_select')) {
  33. //get the domain_uuid
  34. $sql = "select * from v_domains ";
  35. $sql .= "order by domain_name asc ";
  36. $database = new database;
  37. $result = $database->select($sql, null, 'all');
  38. if (!empty($result)) {
  39. foreach($result as $row) {
  40. if (count($result) == 0) {
  41. $_SESSION["domain_uuid"] = $row["domain_uuid"];
  42. $_SESSION["domain_name"] = $row['domain_name'];
  43. }
  44. else {
  45. if (!empty($domain_array) && ($row['domain_name'] == $domain_array[0] || $row['domain_name'] == 'www.'.$domain_array[0])) {
  46. $_SESSION["domain_uuid"] = $row["domain_uuid"];
  47. $_SESSION["domain_name"] = $row['domain_name'];
  48. }
  49. }
  50. }
  51. }
  52. unset($sql, $result);
  53. //update the domain session variables
  54. $domain_uuid = $_GET["domain_uuid"];
  55. $_SESSION["previous_domain_uuid"] = $_SESSION['domain_uuid'];
  56. $_SESSION['domain_uuid'] = $domain_uuid;
  57. $_SESSION["domain_name"] = $_SESSION['domains'][$domain_uuid]['domain_name'];
  58. $_SESSION['domain']['template']['name'] = $_SESSION['domains'][$domain_uuid]['template_name'] ?? null;
  59. $_SESSION["context"] = $_SESSION["domain_name"];
  60. //clear the extension array so that it is regenerated for the selected domain
  61. unset($_SESSION['extension_array']);
  62. //set the setting arrays
  63. $domain = new domains();
  64. $domain->set();
  65. //redirect the user
  66. if (!empty($_SESSION["login"]["destination"])) {
  67. // to default, or domain specific, login destination
  68. header("Location: ".PROJECT_PATH.$_SESSION["login"]["destination"]["url"]);
  69. }
  70. else {
  71. header("Location: ".PROJECT_PATH."/core/dashboard/");
  72. }
  73. exit;
  74. }
  75. }
  76. //check permission
  77. if (permission_exists('domain_all') && permission_exists('domain_view')) {
  78. //access granted
  79. }
  80. else {
  81. echo "access denied";
  82. exit;
  83. }
  84. //add multi-lingual support
  85. $language = new text;
  86. $text = $language->get();
  87. //get the http post data
  88. if (!empty($_POST['domains'])) {
  89. $action = $_POST['action'] ?? '';
  90. $search = $_POST['search'] ?? '';
  91. $domains = $_POST['domains'] ?? '';
  92. }
  93. //process the http post data by action
  94. if (!empty($action) && !empty($domains)) {
  95. switch ($action) {
  96. case 'copy':
  97. if (permission_exists('domain_add')) {
  98. $obj = new domains;
  99. $obj->copy($domains);
  100. }
  101. break;
  102. case 'toggle':
  103. if (permission_exists('domain_edit')) {
  104. $obj = new domains;
  105. $obj->toggle($domains);
  106. }
  107. break;
  108. case 'delete':
  109. if (permission_exists('domain_delete')) {
  110. $obj = new domains;
  111. $obj->delete($domains);
  112. }
  113. break;
  114. }
  115. header('Location: domains.php'.(!empty($search) ? '?search='.urlencode($search) : null));
  116. exit;
  117. }
  118. //get order and order by and sanitize the values
  119. $order_by = $_GET["order_by"] ?? '';
  120. $order = $_GET["order"] ?? '';
  121. //set additional variables
  122. $search = $_GET["search"] ?? '';
  123. $show = $_GET["show"] ?? '';
  124. //set from session variables
  125. $list_row_edit_button = !empty($_SESSION['theme']['list_row_edit_button']['boolean']) ? $_SESSION['theme']['list_row_edit_button']['boolean'] : 'false';
  126. //add the search string
  127. if (!empty($search)) {
  128. $search = strtolower($_GET["search"]);
  129. $sql_search = " (";
  130. $sql_search .= " lower(domain_name) like :search ";
  131. $sql_search .= " or lower(domain_description) like :search ";
  132. $sql_search .= ") ";
  133. $parameters['search'] = '%'.$search.'%';
  134. }
  135. //get the count
  136. $sql = "select count(domain_uuid) from v_domains ";
  137. if (!empty($sql_search)) {
  138. $sql .= "where ".$sql_search;
  139. }
  140. $database = new database;
  141. $num_rows = $database->select($sql, $parameters ?? null, 'column');
  142. //prepare to page the results
  143. $rows_per_page = (!empty($_SESSION['domain']['paging']['numeric'])) ? $_SESSION['domain']['paging']['numeric'] : 50;
  144. $param = $search ? "&search=".$search : null;
  145. $page = !empty($_GET['page']) ? $_GET['page'] : 0;
  146. list($paging_controls, $rows_per_page) = paging($num_rows, $param, $rows_per_page);
  147. list($paging_controls_mini, $rows_per_page) = paging($num_rows, $param, $rows_per_page, true);
  148. $offset = $rows_per_page * $page;
  149. //get the list
  150. $sql = "select domain_uuid, domain_name, cast(domain_enabled as text), domain_description ";
  151. $sql .= "from v_domains ";
  152. if (!empty($sql_search)) {
  153. $sql .= "where ".$sql_search;
  154. }
  155. $sql .= order_by($order_by, $order, 'domain_name', 'asc');
  156. $sql .= limit_offset($rows_per_page, $offset);
  157. $database = new database;
  158. $domains = $database->select($sql, $parameters ?? null, 'all');
  159. unset($sql, $parameters);
  160. //create token
  161. $object = new token;
  162. $token = $object->create($_SERVER['PHP_SELF']);
  163. //include the header
  164. $document['title'] = $text['title-domains'];
  165. require_once "resources/header.php";
  166. //show the content
  167. echo "<div class='action_bar' id='action_bar'>\n";
  168. echo " <div class='heading'><b>".$text['title-domains']." (".$num_rows.")</b></div>\n";
  169. echo " <div class='actions'>\n";
  170. if (permission_exists('domain_add')) {
  171. echo button::create(['type'=>'button','label'=>$text['button-add'],'icon'=>$_SESSION['theme']['button_icon_add'],'id'=>'btn_add','link'=>'domain_edit.php']);
  172. }
  173. if (permission_exists('domain_edit') && $domains) {
  174. echo button::create(['type'=>'button','label'=>$text['button-toggle'],'icon'=>$_SESSION['theme']['button_icon_toggle'],'id'=>'btn_toggle','name'=>'btn_toggle','style'=>'display: none;','onclick'=>"modal_open('modal-toggle','btn_toggle');"]);
  175. }
  176. if (permission_exists('domain_delete') && $domains) {
  177. echo button::create(['type'=>'button','label'=>$text['button-delete'],'icon'=>$_SESSION['theme']['button_icon_delete'],'id'=>'btn_delete','name'=>'btn_delete','style'=>'display: none;','onclick'=>"modal_open('modal-delete','btn_delete_domain');"]);
  178. }
  179. echo "<form id='form_search' class='inline' method='get'>\n";
  180. echo "<input type='text' class='txt list-search' name='search' id='search' value=\"".escape($search)."\" placeholder=\"".$text['label-search']."\" onkeydown=''>";
  181. echo button::create(['label'=>$text['button-search'],'icon'=>$_SESSION['theme']['button_icon_search'],'type'=>'submit','id'=>'btn_search']);
  182. //echo button::create(['label'=>$text['button-reset'],'icon'=>$_SESSION['theme']['button_icon_reset'],'type'=>'button','id'=>'btn_reset','link'=>'domains.php','style'=>($search == '' ? 'display: none;' : null)]);
  183. if (!empty($paging_controls_mini)) {
  184. echo "<span style='margin-left: 15px;'>".$paging_controls_mini."</span>\n";
  185. }
  186. echo " </form>\n";
  187. echo " </div>\n";
  188. echo " <div style='clear: both;'></div>\n";
  189. echo "</div>\n";
  190. if (permission_exists('domain_edit') && !empty($domains)) {
  191. echo modal::create(['id'=>'modal-toggle','type'=>'toggle','actions'=>button::create(['type'=>'button','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_toggle','style'=>'float: right; margin-left: 15px;','collapse'=>'never','onclick'=>"modal_close(); list_action_set('toggle'); list_form_submit('form_list');"])]);
  192. }
  193. if (permission_exists('domain_delete') && !empty($domains)) {
  194. echo modal::create(['id'=>'modal-delete','type'=>'delete','actions'=>button::create(['type'=>'button','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_delete','style'=>'float: right; margin-left: 15px;','collapse'=>'never','onclick'=>"modal_close(); list_action_set('delete'); list_form_submit('form_list');"])]);
  195. }
  196. echo $text['description-domains']."\n";
  197. echo "<br /><br />\n";
  198. echo "<form id='form_list' method='post'>\n";
  199. echo "<input type='hidden' id='action' name='action' value=''>\n";
  200. echo "<input type='hidden' name='search' value=\"".escape($search)."\">\n";
  201. echo "<table class='list'>\n";
  202. echo "<tr class='list-header'>\n";
  203. if (permission_exists('domain_edit') || permission_exists('domain_delete')) {
  204. echo " <th class='checkbox'>\n";
  205. echo " <input type='checkbox' id='checkbox_all' name='checkbox_all' onclick='list_all_toggle(); checkbox_on_change(this);' ".(!empty($domains) ?: "style='visibility: hidden;'").">\n";
  206. echo " </th>\n";
  207. }
  208. if ($show == 'all' && permission_exists('domain_all')) {
  209. echo th_order_by('domain_name', $text['label-domain'], $order_by, $order);
  210. }
  211. echo th_order_by('domain_name', $text['label-domain_name'], $order_by, $order);
  212. echo "<th class='center'>".$text['label-tools']."</th>";
  213. echo th_order_by('domain_enabled', $text['label-domain_enabled'], $order_by, $order, null, "class='center'");
  214. echo " <th class='hide-sm-dn'>".$text['label-domain_description']."</th>\n";
  215. if (permission_exists('domain_edit') && $list_row_edit_button == 'true') {
  216. echo " <td class='action-button'>&nbsp;</td>\n";
  217. }
  218. echo "</tr>\n";
  219. if (!empty($domains)) {
  220. $x = 0;
  221. foreach ($domains as $row) {
  222. $list_row_url = '';
  223. if (permission_exists('domain_edit')) {
  224. $list_row_url = "domain_edit.php?id=".urlencode($row['domain_uuid']);
  225. }
  226. echo "<tr class='list-row' href='".$list_row_url."'>\n";
  227. if (permission_exists('domain_edit') || permission_exists('domain_delete')) {
  228. echo " <td class='checkbox'>\n";
  229. echo " <input type='checkbox' name='domains[$x][checked]' id='checkbox_".$x."' value='true' onclick=\"checkbox_on_change(this); if (!this.checked) { document.getElementById('checkbox_all').checked = false; }\">\n";
  230. echo " <input type='hidden' name='domains[$x][uuid]' value='".escape($row['domain_uuid'])."' />\n";
  231. echo " </td>\n";
  232. }
  233. if ($show == 'all' && permission_exists('domain_all')) {
  234. echo " <td>".escape($_SESSION['domains'][$row['domain_uuid']]['domain_name'])."</td>\n";
  235. }
  236. echo " <td>\n";
  237. if (permission_exists('domain_edit')) {
  238. echo " <a href='".$list_row_url."' title=\"".$text['button-edit']."\">".escape($row['domain_name'])."</a>\n";
  239. }
  240. else {
  241. echo " ".escape($row['domain_name']);
  242. }
  243. echo " </td>\n";
  244. echo " <td class='no-link center'>\n";
  245. echo " <a href='".PROJECT_PATH."/core/domains/domains.php?domain_uuid=".escape($row['domain_uuid'])."&domain_change=true'>".$text['label-manage']."</a>";
  246. if (permission_exists('domain_setting_view')) {
  247. $list_setting_url = PROJECT_PATH."/core/domain_settings/domain_settings.php?id=".urlencode($row['domain_uuid']);
  248. echo "&nbsp;&nbsp; <a href='".$list_setting_url."'\">".$text['button-settings'];
  249. }
  250. echo " </td>\n";
  251. if (permission_exists('domain_edit')) {
  252. echo " <td class='no-link center'>\n";
  253. echo button::create(['type'=>'submit','class'=>'link','label'=>$text['label-'.$row['domain_enabled']],'title'=>$text['button-toggle'],'onclick'=>"list_self_check('checkbox_".$x."'); list_action_set('toggle'); list_form_submit('form_list')"]);
  254. echo " </td>\n";
  255. }
  256. else {
  257. echo " <td class='center'>\n";
  258. echo $text['label-'.$row['domain_enabled']];
  259. echo " </td>\n";
  260. }
  261. echo " <td class='description overflow hide-sm-dn'>".escape($row['domain_description'])."</td>\n";
  262. if (permission_exists('domain_edit') && $list_row_edit_button == 'true') {
  263. echo " <td class='action-button'>\n";
  264. echo button::create(['type'=>'button','title'=>$text['button-edit'],'icon'=>$_SESSION['theme']['button_icon_edit'],'link'=>$list_row_url]);
  265. echo " </td>\n";
  266. }
  267. echo "</tr>\n";
  268. $x++;
  269. }
  270. unset($domains);
  271. }
  272. echo "</table>\n";
  273. echo "<br />\n";
  274. echo "<div align='center'>".$paging_controls."</div>\n";
  275. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  276. echo "</form>\n";
  277. //include the footer
  278. require_once "resources/footer.php";
  279. ?>