user_logs.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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-2024
  17. the Initial Developer. All Rights Reserved.
  18. */
  19. //includes files
  20. require_once dirname(__DIR__, 2) . "/resources/require.php";
  21. require_once "resources/check_auth.php";
  22. require_once "resources/paging.php";
  23. //check permissions
  24. if (permission_exists('user_log_view')) {
  25. //access granted
  26. }
  27. else {
  28. echo "access denied";
  29. exit;
  30. }
  31. //add multi-lingual support
  32. $language = new text;
  33. $text = $language->get();
  34. //get the http post data
  35. if (!empty($_POST['user_logs']) && is_array($_POST['user_logs'])) {
  36. $action = $_POST['action'];
  37. $search = $_POST['search'];
  38. $user_logs = $_POST['user_logs'];
  39. }
  40. //process the http post data by action
  41. if (!empty($action) && !empty($user_logs) && is_array($user_logs) && @sizeof($user_logs) != 0) {
  42. //validate the token
  43. $token = new token;
  44. if (!$token->validate($_SERVER['PHP_SELF'])) {
  45. message::add($text['message-invalid_token'],'negative');
  46. header('Location: user_logs.php');
  47. exit;
  48. }
  49. //prepare the array
  50. if (!empty($user_logs)) {
  51. foreach ($user_logs as $row) {
  52. $array['user_logs'][$x]['checked'] = $row['checked'];
  53. $array['user_logs'][$x]['user_log_uuid'] = $row['user_log_uuid'];
  54. $x++;
  55. }
  56. }
  57. //prepare the database object
  58. $database = new database;
  59. $database->app_name = 'user_logs';
  60. $database->app_uuid = '582a13cf-7d75-4ea3-b2d9-60914352d76e';
  61. //send the array to the database class
  62. if (!empty($action) && $action == 'delete' && permission_exists('user_log_delete')) {
  63. $database->delete($array);
  64. }
  65. //redirect the user
  66. header('Location: user_logs.php'.($search != '' ? '?search='.urlencode($search) : null));
  67. exit;
  68. }
  69. //get the session path
  70. $session_path = session_save_path();
  71. //get the server hostname
  72. $hostname = gethostname();
  73. //get order and order by
  74. $order_by = $_GET["order_by"] ?? null;
  75. $order = $_GET["order"] ?? null;
  76. //define the variables
  77. $search = '';
  78. $show = '';
  79. //add the search variable
  80. if (!empty($_GET["search"])) {
  81. $search = strtolower($_GET["search"]);
  82. }
  83. //add the show variable
  84. if (!empty($_GET["show"])) {
  85. $show = $_GET["show"];
  86. }
  87. //get the count
  88. $sql = "select count(user_log_uuid) ";
  89. $sql .= "from v_user_logs ";
  90. if (permission_exists('user_log_all') && $show == 'all') {
  91. $sql .= "where true ";
  92. }
  93. else {
  94. $sql .= "where domain_uuid = :domain_uuid ";
  95. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  96. }
  97. if (!empty($search)) {
  98. $sql .= "and ( ";
  99. $sql .= " lower(username) like :search ";
  100. $sql .= " or lower(type) like :search ";
  101. $sql .= " or lower(result) like :search ";
  102. $sql .= " or lower(remote_address) like :search ";
  103. $sql .= " or lower(user_agent) like :search ";
  104. $sql .= ") ";
  105. $parameters['search'] = '%'.$search.'%';
  106. }
  107. $database = new database;
  108. $num_rows = $database->select($sql, $parameters ?? null, 'column');
  109. unset($sql, $parameters);
  110. //prepare to page the results
  111. $rows_per_page = ($_SESSION['domain']['paging']['numeric'] != '') ? $_SESSION['domain']['paging']['numeric'] : 50;
  112. $param = !empty($search) ? "&search=".$search : null;
  113. $param .= (!empty($_GET['page']) && $show == 'all' && permission_exists('user_log_all')) ? "&show=all" : null;
  114. $page = !empty($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 0;
  115. list($paging_controls, $rows_per_page) = paging($num_rows, $param, $rows_per_page);
  116. list($paging_controls_mini, $rows_per_page) = paging($num_rows, $param, $rows_per_page, true);
  117. $offset = $rows_per_page * $page;
  118. //set the time zone
  119. if (isset($_SESSION['domain']['time_zone']['name'])) {
  120. $time_zone = $_SESSION['domain']['time_zone']['name'];
  121. }
  122. else {
  123. $time_zone = date_default_timezone_get();
  124. }
  125. $parameters['time_zone'] = $time_zone;
  126. //get the list
  127. $sql = "select ";
  128. $sql .= "u.user_log_uuid, ";
  129. $sql .= "u.hostname, ";
  130. $sql .= "u.domain_uuid, ";
  131. $sql .= "d.domain_name, ";
  132. $sql .= "to_char(timezone(:time_zone, timestamp), 'DD Mon YYYY') as date_formatted, ";
  133. $sql .= "to_char(timezone(:time_zone, timestamp), 'HH12:MI:SS am') as time_formatted, ";
  134. $sql .= "user_uuid, ";
  135. $sql .= "username, ";
  136. $sql .= "type, ";
  137. $sql .= "result, ";
  138. $sql .= "remote_address, ";
  139. $sql .= "user_agent, ";
  140. $sql .= "session_id ";
  141. $sql .= "from v_user_logs as u, v_domains as d ";
  142. if (permission_exists('user_log_all') && $show == 'all') {
  143. $sql .= "where true ";
  144. }
  145. else {
  146. $sql .= "where u.domain_uuid = :domain_uuid ";
  147. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  148. }
  149. if (!empty($search)) {
  150. $sql .= "and ( ";
  151. $sql .= " lower(username) like :search ";
  152. $sql .= " or lower(type) like :search ";
  153. $sql .= " or lower(result) like :search ";
  154. $sql .= " or lower(remote_address) like :search ";
  155. $sql .= " or lower(user_agent) like :search ";
  156. $sql .= ") ";
  157. $parameters['search'] = '%'.$search.'%';
  158. }
  159. $sql .= "and u.domain_uuid = d.domain_uuid ";
  160. $sql .= order_by($order_by, $order, 'timestamp', 'desc');
  161. $sql .= limit_offset($rows_per_page, $offset);
  162. $database = new database;
  163. $user_logs = $database->select($sql, $parameters ?? null, 'all');
  164. unset($sql, $parameters);
  165. //create token
  166. $object = new token;
  167. $token = $object->create($_SERVER['PHP_SELF']);
  168. //additional includes
  169. $document['title'] = $text['title-user_logs'];
  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-user_logs']."</b><div class='count'>".number_format($num_rows)."</div></div>\n";
  174. echo " <div class='actions'>\n";
  175. if (permission_exists('user_log_delete') && $user_logs) {
  176. 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');"]);
  177. }
  178. echo "<form id='form_search' class='inline' method='get'>\n";
  179. if (permission_exists('user_log_all')) {
  180. if ($show == 'all') {
  181. echo " <input type='hidden' name='show' value='all'>\n";
  182. }
  183. else {
  184. echo button::create(['type'=>'button','label'=>$text['button-show_all'],'icon'=>$_SESSION['theme']['button_icon_all'],'link'=>'?show=all&search='.$search]);
  185. }
  186. }
  187. echo "<input type='text' class='txt list-search' name='search' id='search' value=\"".escape($search)."\" placeholder=\"".$text['label-search']."\" onkeydown=''>";
  188. echo button::create(['label'=>$text['button-search'],'icon'=>$_SESSION['theme']['button_icon_search'],'type'=>'submit','id'=>'btn_search']);
  189. if ($paging_controls_mini != '') {
  190. echo "<span style='margin-left: 15px;'>".$paging_controls_mini."</span>\n";
  191. }
  192. echo " </form>\n";
  193. echo " </div>\n";
  194. echo " <div style='clear: both;'></div>\n";
  195. echo "</div>\n";
  196. if (permission_exists('user_log_delete') && $user_logs) {
  197. 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');"])]);
  198. }
  199. echo $text['title_description-user_logs']."\n";
  200. echo "<br /><br />\n";
  201. echo "<form id='form_list' method='post'>\n";
  202. echo "<input type='hidden' id='action' name='action' value=''>\n";
  203. echo "<input type='hidden' name='search' value=\"".escape($search ?? '')."\">\n";
  204. echo "<div class='card'>\n";
  205. echo "<table class='list'>\n";
  206. echo "<tr class='list-header'>\n";
  207. if (permission_exists('user_log_delete')) {
  208. echo " <th class='checkbox'>\n";
  209. echo " <input type='checkbox' id='checkbox_all' name='checkbox_all' onclick='list_all_toggle(); checkbox_on_change(this);' ".empty($user_logs ? "style='visibility: hidden;'" : null).">\n";
  210. echo " </th>\n";
  211. }
  212. if ($show == 'all' && permission_exists('user_log_all')) {
  213. echo th_order_by('domain_name', $text['label-domain'], $order_by, $order);
  214. }
  215. echo "<th class='left'>".$text['label-date']."</th>\n";
  216. echo "<th class='left hide-md-dn'>".$text['label-time']."</th>\n";
  217. echo "<th class='shrink hide-md-dn'>".$text['label-hostname']."</th>\n";
  218. echo "<th class='right'>".$text['label-status']."</th>\n";
  219. echo th_order_by('username', $text['label-username'], $order_by, $order);
  220. echo th_order_by('type', $text['label-type'], $order_by, $order);
  221. echo th_order_by('result', $text['label-result'], $order_by, $order);
  222. echo th_order_by('remote_address', $text['label-remote_address'], $order_by, $order);
  223. echo th_order_by('user_agent', $text['label-user_agent'], $order_by, $order);
  224. echo "</tr>\n";
  225. if (!empty($user_logs) && is_array($user_logs) && @sizeof($user_logs) != 0) {
  226. $x = 0;
  227. foreach ($user_logs as $row) {
  228. //check the session status
  229. $session_file = 'sess_'.$row['session_id'];
  230. $session_status = (!empty($row['session_id']) && file_exists($session_path.'/'.$session_file)) ? 'active' : 'inactive';
  231. echo "<tr class='list-row'>\n";
  232. if (permission_exists('user_log_delete')) {
  233. echo " <td class='checkbox'>\n";
  234. echo " <input type='checkbox' name='user_logs[$x][checked]' id='checkbox_".$x."' value='true' onclick=\"checkbox_on_change(this); if (!this.checked) { document.getElementById('checkbox_all').checked = false; }\">\n";
  235. echo " <input type='hidden' name='user_logs[$x][user_log_uuid]' value='".escape($row['user_log_uuid'])."' />\n";
  236. echo " </td>\n";
  237. }
  238. if ($show == 'all' && permission_exists('user_log_all')) {
  239. echo " <td>".escape($row['domain_name'])."</td>\n";
  240. }
  241. echo " <td>".escape($row['date_formatted'])."</td>\n";
  242. echo " <td class='left hide-md-dn'>".escape($row['time_formatted'])."</td>\n";
  243. echo " <td class='hide-md-dn'>".escape($row['hostname'])."</td>\n";
  244. echo " <td><div class='list-status-".$session_status."'></div></td>\n";
  245. echo " <td>".escape($row['username'])."</td>\n";
  246. echo " <td>".escape($row['type'])."</td>\n";
  247. echo " <td>".escape($row['result'])."</td>\n";
  248. echo " <td>".escape($row['remote_address'])."</td>\n";
  249. echo " <td>".escape($row['user_agent'])."</td>\n";
  250. echo "</tr>\n";
  251. $x++;
  252. }
  253. unset($user_logs);
  254. }
  255. echo "</table>\n";
  256. echo "</div>\n";
  257. echo "<br />\n";
  258. echo "<div align='center'>".$paging_controls."</div>\n";
  259. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  260. echo "</form>\n";
  261. //include the footer
  262. require_once "resources/footer.php";
  263. ?>