users.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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-2024
  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. //check permissions
  26. if (permission_exists('user_view')) {
  27. //access granted
  28. }
  29. else {
  30. echo "access denied";
  31. exit;
  32. }
  33. //connect to the database
  34. $database = new database;
  35. //add multi-lingual support
  36. $language = new text;
  37. $text = $language->get();
  38. //get the http post data
  39. if (!empty($_POST['users'])) {
  40. $action = $_POST['action'] ?? '';
  41. $search = $_POST['search'] ?? '';
  42. $users = $_POST['users'] ?? '';
  43. }
  44. //check to see if contact details are in the view
  45. $sql = "select * from view_users ";
  46. $sql .= "where domain_uuid = :domain_uuid ";
  47. $parameters = null;
  48. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  49. $row = $database->select($sql, $parameters, 'row');
  50. if (isset($row['contact_organization'])) {
  51. $show_contact_fields = true;
  52. }
  53. else {
  54. $show_contact_fields = false;
  55. }
  56. unset($parameters);
  57. //process the http post data by action
  58. if (!empty($action) && is_array($users) && @sizeof($users) != 0) {
  59. switch ($action) {
  60. case 'copy':
  61. if (permission_exists('user_add')) {
  62. $obj = new users;
  63. $obj->copy($users);
  64. }
  65. break;
  66. case 'toggle':
  67. if (permission_exists('user_edit')) {
  68. $obj = new users;
  69. $obj->toggle($users);
  70. }
  71. break;
  72. case 'delete':
  73. if (permission_exists('user_delete')) {
  74. $obj = new users;
  75. $obj->delete($users);
  76. }
  77. break;
  78. }
  79. header('Location: users.php'.($search != '' ? '?search='.urlencode($search) : null));
  80. exit;
  81. }
  82. //get order and order by
  83. $order_by = $_GET["order_by"] ?? '';
  84. $order = $_GET["order"] ?? '';
  85. //set additional variables
  86. $context = !empty($_GET["context"]) ? $_GET["context"] : '';
  87. $search = !empty($_GET["search"]) ? $_GET["search"] : '';
  88. $show = !empty($_GET["show"]) ? $_GET["show"] : '';
  89. //set from session variables
  90. $list_row_edit_button = !empty($_SESSION['theme']['list_row_edit_button']['boolean']) ? $_SESSION['theme']['list_row_edit_button']['boolean'] : 'false';
  91. //add the search string
  92. if (!empty($search)) {
  93. $search = strtolower($_GET["search"]);
  94. $sql_search = " (";
  95. $sql_search .= " lower(username) like :search ";
  96. $sql_search .= " or lower(group_names) like :search ";
  97. $sql_search .= " or lower(contact_organization) like :search ";
  98. $sql_search .= " or lower(contact_name) like :search ";
  99. //$sql_search .= " or lower(user_status) like :search ";
  100. $sql_search .= ") ";
  101. $parameters['search'] = '%'.$search.'%';
  102. }
  103. //get the count
  104. $sql = "select count(*) from view_users ";
  105. if ($show == "all" && permission_exists('user_all')) {
  106. if (isset($sql_search)) {
  107. $sql .= "where ".$sql_search;
  108. }
  109. else {
  110. $sql.= "where true ";
  111. }
  112. }
  113. else {
  114. $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
  115. if (!empty($sql_search)) {
  116. $sql .= "and ".$sql_search;
  117. }
  118. $parameters['domain_uuid'] = $domain_uuid;
  119. }
  120. $sql .= "and ( ";
  121. $sql .= " group_level <= :group_level ";
  122. $sql .= " or group_level is null ";
  123. $sql .= ") ";
  124. $parameters['group_level'] = $_SESSION['user']['group_level'];
  125. $num_rows = $database->select($sql, $parameters, 'column');
  126. //prepare to page the results
  127. $rows_per_page = ($_SESSION['domain']['paging']['numeric'] != '') ? $_SESSION['domain']['paging']['numeric'] : 50;
  128. $param = $search ? "&search=".$search : null;
  129. $param .= ($show == 'all' && permission_exists('user_all')) ? "&show=all" : null;
  130. $page = !empty($_GET['page']) ? $_GET['page'] : 0;
  131. list($paging_controls, $rows_per_page) = paging($num_rows, $param, $rows_per_page);
  132. list($paging_controls_mini, $rows_per_page) = paging($num_rows, $param, $rows_per_page, true);
  133. $offset = $rows_per_page * $page;
  134. //get the list
  135. $sql = "select domain_name, domain_uuid, user_uuid, username, group_names, ";
  136. if ($show_contact_fields) {
  137. $sql .= "contact_organization,contact_name, ";
  138. }
  139. $sql .= "cast(user_enabled as text) ";
  140. $sql .= "from view_users ";
  141. if ($show == "all" && permission_exists('user_all')) {
  142. if (isset($sql_search)) {
  143. $sql .= "where ".$sql_search;
  144. }
  145. else {
  146. $sql.= "where true ";
  147. }
  148. }
  149. else {
  150. $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
  151. if (isset($sql_search)) {
  152. $sql .= "and ".$sql_search;
  153. }
  154. $parameters['domain_uuid'] = $domain_uuid;
  155. }
  156. $sql .= "and ( ";
  157. $sql .= " group_level <= :group_level ";
  158. $sql .= " or group_level is null ";
  159. $sql .= ") ";
  160. $parameters['group_level'] = $_SESSION['user']['group_level'];
  161. $sql .= order_by($order_by, $order, 'username', 'asc');
  162. $sql .= limit_offset($rows_per_page, $offset);
  163. $users = $database->select($sql, $parameters, 'all');
  164. unset($sql, $parameters);
  165. //create token
  166. $object = new token;
  167. $token = $object->create($_SERVER['PHP_SELF']);
  168. //include the header
  169. $document['title'] = $text['title-users'];
  170. require_once "resources/header.php";
  171. //show the content
  172. echo "<div class='action_bar' id='action_bar'>\n";
  173. echo " <div class='heading'><b>".$text['title-users']."</b><div class='count'>".number_format($num_rows)."</div></div>\n";
  174. echo " <div class='actions'>\n";
  175. if (permission_exists('user_add')) {
  176. if (!isset($id)) {
  177. echo button::create(['type'=>'button','label'=>$text['button-import'],'icon'=>$_SESSION['theme']['button_icon_import'],'style'=>'margin-right: 15px;','link'=>'user_imports.php']);
  178. }
  179. echo button::create(['type'=>'button','label'=>$text['button-add'],'icon'=>$_SESSION['theme']['button_icon_add'],'id'=>'btn_add','link'=>'user_edit.php']);
  180. }
  181. if (permission_exists('user_add') && $users) {
  182. echo button::create(['type'=>'button','label'=>$text['button-copy'],'icon'=>$_SESSION['theme']['button_icon_copy'],'id'=>'btn_copy','name'=>'btn_copy','style'=>'display: none;','onclick'=>"modal_open('modal-copy','btn_copy');"]);
  183. }
  184. if (permission_exists('user_edit') && $users) {
  185. 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');"]);
  186. }
  187. if (permission_exists('user_delete') && $users) {
  188. 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');"]);
  189. }
  190. echo "<form id='form_search' class='inline' method='get'>\n";
  191. if (permission_exists('user_all')) {
  192. if ($show == 'all') {
  193. echo " <input type='hidden' name='show' value='all'>\n";
  194. }
  195. else {
  196. echo button::create(['type'=>'button','label'=>$text['button-show_all'],'icon'=>$_SESSION['theme']['button_icon_all'],'link'=>'?show=all']);
  197. }
  198. }
  199. echo "<input type='text' class='txt list-search' name='search' id='search' value=\"".escape($search)."\" placeholder=\"".$text['label-search']."\" onkeydown=''>";
  200. echo button::create(['label'=>$text['button-search'],'icon'=>$_SESSION['theme']['button_icon_search'],'type'=>'submit','id'=>'btn_search']);
  201. //echo button::create(['label'=>$text['button-reset'],'icon'=>$_SESSION['theme']['button_icon_reset'],'type'=>'button','id'=>'btn_reset','link'=>'users.php','style'=>($search == '' ? 'display: none;' : null)]);
  202. if ($paging_controls_mini != '') {
  203. echo "<span style='margin-left: 15px;'>".$paging_controls_mini."</span>\n";
  204. }
  205. echo " </form>\n";
  206. echo " </div>\n";
  207. echo " <div style='clear: both;'></div>\n";
  208. echo "</div>\n";
  209. if (permission_exists('user_add') && $users) {
  210. echo modal::create(['id'=>'modal-copy','type'=>'copy','actions'=>button::create(['type'=>'button','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_copy','style'=>'float: right; margin-left: 15px;','collapse'=>'never','onclick'=>"modal_close(); list_action_set('copy'); list_form_submit('form_list');"])]);
  211. }
  212. if (permission_exists('user_edit') && $users) {
  213. 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');"])]);
  214. }
  215. if (permission_exists('user_delete') && $users) {
  216. 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');"])]);
  217. }
  218. echo $text['description-users']."\n";
  219. echo "<br /><br />\n";
  220. echo "<form id='form_list' method='post'>\n";
  221. echo "<input type='hidden' id='action' name='action' value=''>\n";
  222. echo "<input type='hidden' name='search' value=\"".escape($search)."\">\n";
  223. echo "<div class='card'>\n";
  224. echo "<table class='list'>\n";
  225. echo "<tr class='list-header'>\n";
  226. if (permission_exists('user_add') || permission_exists('user_edit') || permission_exists('user_delete')) {
  227. echo " <th class='checkbox'>\n";
  228. echo " <input type='checkbox' id='checkbox_all' name='checkbox_all' onclick='list_all_toggle(); checkbox_on_change(this);' ".(!empty($users) ?: "style='visibility: hidden;'").">\n";
  229. echo " </th>\n";
  230. }
  231. if ($show == 'all' && permission_exists('user_all')) {
  232. echo th_order_by('domain_name', $text['label-domain'], $order_by, $order, null, null, $param);
  233. }
  234. echo th_order_by('username', $text['label-username'], $order_by, $order, null, null, $param);
  235. echo th_order_by('group_names', $text['label-groups'], $order_by, $order, null, null, $param);
  236. if ($show_contact_fields) {
  237. echo th_order_by('contact_organization', $text['label-organization'], $order_by, $order, null, null, $param);
  238. echo th_order_by('contact_name', $text['label-name'], $order_by, $order, null, null, $param);
  239. }
  240. //echo th_order_by('contact_name_family', $text['label-contact_name_family'], $order_by, $order);
  241. //echo th_order_by('user_status', $text['label-user_status'], $order_by, $order);
  242. //echo th_order_by('add_date', $text['label-add_date'], $order_by, $order);
  243. echo th_order_by('user_enabled', $text['label-user_enabled'], $order_by, $order, null, "class='center'", $param);
  244. if (permission_exists('user_edit') && $list_row_edit_button == 'true') {
  245. echo " <td class='action-button'>&nbsp;</td>\n";
  246. }
  247. echo "</tr>\n";
  248. if (is_array($users) && @sizeof($users) != 0) {
  249. $x = 0;
  250. foreach ($users as $row) {
  251. if (permission_exists('user_edit')) {
  252. $list_row_url = "user_edit.php?id=".urlencode($row['user_uuid']);
  253. }
  254. echo "<tr class='list-row' href='".$list_row_url."'>\n";
  255. if (permission_exists('user_add') || permission_exists('user_edit') || permission_exists('user_delete')) {
  256. echo " <td class='checkbox'>\n";
  257. echo " <input type='checkbox' name='users[$x][checked]' id='checkbox_".$x."' value='true' onclick=\"checkbox_on_change(this); if (!this.checked) { document.getElementById('checkbox_all').checked = false; }\">\n";
  258. echo " <input type='hidden' name='users[$x][uuid]' value='".escape($row['user_uuid'])."' />\n";
  259. echo " </td>\n";
  260. }
  261. if ($show == 'all' && permission_exists('user_all')) {
  262. echo " <td>".escape($_SESSION['domains'][$row['domain_uuid']]['domain_name'])."</td>\n";
  263. }
  264. echo " <td>\n";
  265. if (permission_exists('user_edit')) {
  266. echo " <a href='".$list_row_url."' title=\"".$text['button-edit']."\">".escape($row['username'])."</a>\n";
  267. }
  268. else {
  269. echo " ".escape($row['username']);
  270. }
  271. echo " </td>\n";
  272. echo " <td>".escape($row['group_names'])."</td>\n";
  273. if ($show_contact_fields) {
  274. echo " <td>".escape($row['contact_organization'])."</td>\n";
  275. echo " <td>".escape($row['contact_name'])."</td>\n";
  276. }
  277. //echo " <td>".escape($row['contact_name_given'])."</td>\n";
  278. //echo " <td>".escape($row['contact_name_family'])."</td>\n";
  279. //echo " <td>".escape($row['user_status'])."</td>\n";
  280. //echo " <td>".escape($row['add_date'])."</td>\n";
  281. if (permission_exists('user_edit')) {
  282. echo " <td class='no-link center'>\n";
  283. echo button::create(['type'=>'submit','class'=>'link','label'=>$text['label-'.$row['user_enabled']],'title'=>$text['button-toggle'],'onclick'=>"list_self_check('checkbox_".$x."'); list_action_set('toggle'); list_form_submit('form_list')"]);
  284. }
  285. else {
  286. echo " <td class='center'>\n";
  287. echo $text['label-'.$row['user_enabled']];
  288. }
  289. echo " </td>\n";
  290. if (permission_exists('user_edit') && $list_row_edit_button == 'true') {
  291. echo " <td class='action-button'>\n";
  292. echo button::create(['type'=>'button','title'=>$text['button-edit'],'icon'=>$_SESSION['theme']['button_icon_edit'],'link'=>$list_row_url]);
  293. echo " </td>\n";
  294. }
  295. echo "</tr>\n";
  296. $x++;
  297. }
  298. unset($users);
  299. }
  300. echo "</table>\n";
  301. echo "</div>\n";
  302. echo "<br />\n";
  303. echo "<div align='center'>".$paging_controls."</div>\n";
  304. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  305. echo "</form>\n";
  306. //include the footer
  307. require_once "resources/footer.php";
  308. ?>