device_logs.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /*
  3. Copyright (c) 2019-2023 Mark J Crane <[email protected]>
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
  13. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  16. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  17. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  18. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  19. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  20. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  22. SUCH DAMAGE.
  23. */
  24. //includes files
  25. require_once dirname(__DIR__, 2) . "/resources/require.php";
  26. require_once "resources/check_auth.php";
  27. require_once "resources/paging.php";
  28. //check permissions
  29. if (permission_exists('device_log_view')) {
  30. //access granted
  31. }
  32. else {
  33. echo "access denied";
  34. exit;
  35. }
  36. //add multi-lingual support
  37. $language = new text;
  38. $text = $language->get();
  39. //set additional variables
  40. $search = $_GET["search"] ?? '';
  41. $show = $_GET["show"] ?? '';
  42. //set from session variables
  43. $list_row_edit_button = !empty($_SESSION['theme']['list_row_edit_button']['boolean']) ? $_SESSION['theme']['list_row_edit_button']['boolean'] : 'false';
  44. //get the http post data
  45. if (!empty($_POST['device_logs']) && is_array($_POST['device_logs'])) {
  46. $action = $_POST['action'];
  47. $search = $_POST['search'];
  48. $device_logs = $_POST['device_logs'];
  49. }
  50. //process the http post data by action
  51. if (!empty($action) && is_array($device_logs) && @sizeof($device_logs) != 0) {
  52. switch ($action) {
  53. case 'copy':
  54. if (permission_exists('device_log_add')) {
  55. $obj = new device_logs;
  56. $obj->copy($device_logs);
  57. }
  58. break;
  59. case 'toggle':
  60. if (permission_exists('device_log_edit')) {
  61. $obj = new device_logs;
  62. $obj->toggle($device_logs);
  63. }
  64. break;
  65. case 'delete':
  66. if (permission_exists('device_log_delete')) {
  67. $obj = new device_logs;
  68. $obj->delete($device_logs);
  69. }
  70. break;
  71. }
  72. header('Location: device_logs.php'.($search != '' ? '?search='.urlencode($search) : null));
  73. exit;
  74. }
  75. //get order and order by
  76. $order_by = $_GET["order_by"] ?? '';
  77. $order = $_GET["order"] ?? '';
  78. //set the time zone
  79. if (isset($_SESSION['domain']['time_zone']['name'])) {
  80. $time_zone = $_SESSION['domain']['time_zone']['name'];
  81. }
  82. else {
  83. $time_zone = date_default_timezone_get();
  84. }
  85. //add the search string
  86. if (isset($_GET["search"]) && $_GET["search"] != '') {
  87. $search = strtolower($_GET["search"]);
  88. }
  89. //get the count
  90. $sql = "select count(device_log_uuid) \n";
  91. $sql .= "from v_device_logs \n";
  92. if ($show == "all" && permission_exists('device_log_all')) {
  93. $sql .= "where true \n";
  94. }
  95. else {
  96. $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
  97. $parameters['domain_uuid'] = $domain_uuid;
  98. }
  99. if (isset($search)) {
  100. $sql .= "and (";
  101. $sql .= " lower(device_address) like :search ";
  102. $sql .= " or lower(request_scheme) like :search ";
  103. $sql .= " or lower(http_host) like :search ";
  104. $sql .= " or lower(server_port) like :search ";
  105. $sql .= " or lower(server_protocol) like :search ";
  106. $sql .= " or lower(query_string) like :search ";
  107. $sql .= " or lower(remote_address) like :search ";
  108. $sql .= " or lower(http_user_agent) like :search ";
  109. $sql .= " or lower(http_status) like :search ";
  110. $sql .= " or lower(http_status_code) like :search ";
  111. $sql .= ") ";
  112. $parameters['search'] = '%'.$search.'%';
  113. }
  114. $database = new database;
  115. $num_rows = $database->select($sql, $parameters, 'column');
  116. //prepare to page the results
  117. $rows_per_page = (!empty($_SESSION['domain']['paging']['numeric'])) ? $_SESSION['domain']['paging']['numeric'] : 50;
  118. $param = $search ? "&search=".$search : null;
  119. $param .= ($show == 'all' && permission_exists('device_log_all')) ? "&show=all" : null;
  120. $page = isset($_GET['page']) ? $_GET['page'] : 0;
  121. list($paging_controls, $rows_per_page) = paging($num_rows, $param, $rows_per_page);
  122. list($paging_controls_mini, $rows_per_page) = paging($num_rows, $param, $rows_per_page, true);
  123. $offset = $rows_per_page * $page;
  124. //get the list
  125. $sql = "select \n";
  126. $sql .= "d.domain_uuid, \n";
  127. $sql .= "device_log_uuid, \n";
  128. $sql .= "device_uuid, \n";
  129. $sql .= "d.domain_name, \n";
  130. $sql .= "timestamp, \n";
  131. $sql .= "to_char(timezone(:time_zone, timestamp), 'DD Mon YYYY') as date_formatted, \n";
  132. $sql .= "to_char(timezone(:time_zone, timestamp), 'HH12:MI:SS am') as time_formatted, \n";
  133. $sql .= "device_address, \n";
  134. $sql .= "request_scheme, \n";
  135. $sql .= "http_host, \n";
  136. $sql .= "server_port, \n";
  137. $sql .= "server_protocol, \n";
  138. $sql .= "query_string, \n";
  139. $sql .= "remote_address, \n";
  140. $sql .= "http_user_agent, \n";
  141. $sql .= "http_status, \n";
  142. $sql .= "http_status_code, \n";
  143. $sql .= "http_content_body \n";
  144. $sql .= "from v_device_logs as l, v_domains as d \n";
  145. if ($show == "all" && permission_exists('device_log_all')) {
  146. $sql .= "where true \n";
  147. }
  148. else {
  149. $sql .= "where (l.domain_uuid = :domain_uuid or l.domain_uuid is null) \n";
  150. $parameters['domain_uuid'] = $domain_uuid;
  151. }
  152. $sql .= "and l.domain_uuid = d.domain_uuid \n";
  153. if (isset($search)) {
  154. $sql .= "and ( \n";
  155. $sql .= " lower(device_address) like :search \n";
  156. $sql .= " or lower(request_scheme) like :search \n";
  157. $sql .= " or lower(http_host) like :search \n";
  158. $sql .= " or lower(server_port) like :search \n";
  159. $sql .= " or lower(server_protocol) like :search \n";
  160. $sql .= " or lower(query_string) like :search \n";
  161. $sql .= " or lower(remote_address) like :search \n";
  162. $sql .= " or lower(http_user_agent) like :search \n";
  163. $sql .= " or lower(http_status) like :search \n";
  164. $sql .= " or lower(http_status_code) like :search \n";
  165. $sql .= ") ";
  166. $parameters['search'] = '%'.$search.'%';
  167. }
  168. $parameters['time_zone'] = $time_zone;
  169. $sql .= order_by($order_by, $order, 'timestamp', 'desc');
  170. $sql .= limit_offset($rows_per_page, $offset);
  171. $database = new database;
  172. $device_logs = $database->select($sql, $parameters, 'all');
  173. unset($sql, $parameters);
  174. //create token
  175. $object = new token;
  176. $token = $object->create($_SERVER['PHP_SELF']);
  177. //include the header
  178. $document['title'] = $text['title-device_logs'];
  179. require_once "resources/header.php";
  180. //show the content
  181. echo "<div class='action_bar' id='action_bar'>\n";
  182. echo " <div class='heading'><b>".$text['title-device_logs']." (".$num_rows.")</b></div>\n";
  183. echo " <div class='actions'>\n";
  184. if (permission_exists('device_log_add')) {
  185. echo button::create(['type'=>'button','label'=>$text['button-add'],'icon'=>$_SESSION['theme']['button_icon_add'],'id'=>'btn_add','link'=>'device_log_edit.php']);
  186. }
  187. if (permission_exists('device_log_add') && $device_logs) {
  188. echo button::create(['type'=>'button','label'=>$text['button-copy'],'icon'=>$_SESSION['theme']['button_icon_copy'],'name'=>'btn_copy','onclick'=>"modal_open('modal-copy','btn_copy');"]);
  189. }
  190. if (permission_exists('device_log_edit') && $device_logs) {
  191. echo button::create(['type'=>'button','label'=>$text['button-toggle'],'icon'=>$_SESSION['theme']['button_icon_toggle'],'name'=>'btn_toggle','onclick'=>"modal_open('modal-toggle','btn_toggle');"]);
  192. }
  193. if (permission_exists('device_log_delete') && $device_logs) {
  194. echo button::create(['type'=>'button','label'=>$text['button-delete'],'icon'=>$_SESSION['theme']['button_icon_delete'],'name'=>'btn_delete','onclick'=>"modal_open('modal-delete','btn_delete');"]);
  195. }
  196. echo "<form id='form_search' class='inline' method='get'>\n";
  197. if (permission_exists('device_log_all')) {
  198. if ($show == 'all') {
  199. echo " <input type='hidden' name='show' value='all'>\n";
  200. }
  201. else {
  202. echo button::create(['type'=>'button','label'=>$text['button-show_all'],'icon'=>$_SESSION['theme']['button_icon_all'],'link'=>'?show=all']);
  203. }
  204. }
  205. echo "<input type='text' class='txt list-search' name='search' id='search' value=\"".escape($search)."\" placeholder=\"".$text['label-search']."\" onkeydown='list_search_reset();'>";
  206. echo button::create(['label'=>$text['button-search'],'icon'=>$_SESSION['theme']['button_icon_search'],'type'=>'submit','id'=>'btn_search','style'=>($search != '' ? 'display: none;' : null)]);
  207. echo button::create(['label'=>$text['button-reset'],'icon'=>$_SESSION['theme']['button_icon_reset'],'type'=>'button','id'=>'btn_reset','link'=>'device_logs.php','style'=>($search == '' ? 'display: none;' : null)]);
  208. if (!empty($paging_controls_mini)) {
  209. echo "<span style='margin-left: 15px;'>".$paging_controls_mini."</span>\n";
  210. }
  211. echo " </form>\n";
  212. echo " </div>\n";
  213. echo " <div style='clear: both;'></div>\n";
  214. echo "</div>\n";
  215. if (permission_exists('device_log_add') && $device_logs) {
  216. 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');"])]);
  217. }
  218. if (permission_exists('device_log_edit') && $device_logs) {
  219. 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');"])]);
  220. }
  221. if (permission_exists('device_log_delete') && $device_logs) {
  222. 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');"])]);
  223. }
  224. echo $text['description-device_logs']."\n";
  225. echo "<br /><br />\n";
  226. echo "<form id='form_list' method='post'>\n";
  227. echo "<input type='hidden' id='action' name='action' value=''>\n";
  228. echo "<input type='hidden' name='search' value=\"".escape($search)."\">\n";
  229. echo "<table class='list'>\n";
  230. echo "<tr class='list-header'>\n";
  231. if (permission_exists('device_log_add') || permission_exists('device_log_edit') || permission_exists('device_log_delete')) {
  232. echo " <th class='checkbox'>\n";
  233. echo " <input type='checkbox' id='checkbox_all' name='checkbox_all' onclick='list_all_toggle();' ".($device_logs ?: "style='visibility: hidden;'").">\n";
  234. echo " </th>\n";
  235. }
  236. if ($show == 'all' && permission_exists('device_log_all')) {
  237. echo th_order_by('domain_name', $text['label-domain'], $order_by, $order);
  238. }
  239. echo "<th class='left'>".$text['label-date']."</th>\n";
  240. echo "<th class='left hide-md-dn'>".$text['label-time']."</th>\n";
  241. echo th_order_by('device_address', $text['label-device_address'], $order_by, $order);
  242. echo "<th class='left hide-sm-dn'>".$text['label-request_scheme']."</th>\n";
  243. echo "<th class='left hide-md-dn'>".$text['label-http_host']."</th>\n";
  244. echo "<th class='left hide-md-dn'>".$text['label-server_port']."</th>\n";
  245. echo "<th class='left hide-md-dn'>".$text['label-server_protocol']."</th>\n";
  246. echo "<th class='left hide-md-dn'>".$text['label-query_string']."</th>\n";
  247. echo th_order_by('remote_address', $text['label-remote_address'], $order_by, $order);
  248. echo "<th class='left hide-md-dn'>".$text['label-http_user_agent']."</th>\n";
  249. echo "<th class='left hide-md-dn'>".$text['label-http_status']."</th>\n";
  250. echo "<th class='left hide-md-dn'>".$text['label-http_status_code']."</th>\n";
  251. if (permission_exists('device_log_edit') && $list_row_edit_button == 'true') {
  252. echo " <td class='action-button'>&nbsp;</td>\n";
  253. }
  254. echo "</tr>\n";
  255. if (is_array($device_logs) && @sizeof($device_logs) != 0) {
  256. $x = 0;
  257. foreach ($device_logs as $row) {
  258. if (permission_exists('device_log_edit')) {
  259. $list_row_url = "device_log_edit.php?id=".urlencode($row['device_log_uuid']);
  260. }
  261. echo "<tr class='list-row' href='".$list_row_url."'>\n";
  262. if (permission_exists('device_log_add') || permission_exists('device_log_edit') || permission_exists('device_log_delete')) {
  263. echo " <td class='checkbox'>\n";
  264. echo " <input type='checkbox' name='device_logs[$x][checked]' id='checkbox_".$x."' value='true' onclick=\"if (!this.checked) { document.getElementById('checkbox_all').checked = false; }\">\n";
  265. echo " <input type='hidden' name='device_logs[$x][uuid]' value='".escape($row['device_log_uuid'])."' />\n";
  266. echo " </td>\n";
  267. }
  268. if ($show == 'all' && permission_exists('device_log_all')) {
  269. echo " <td>".escape($_SESSION['domains'][$row['domain_uuid']]['domain_name'])."</td>\n";
  270. }
  271. echo " <td>".escape($row['date_formatted'])."</td>\n";
  272. echo " <td class='left hide-md-dn'>".escape($row['time_formatted'])."</td>\n";
  273. echo " <td>".escape($row['device_address'])."</td>\n";
  274. echo " <td class='left hide-sm-dn'>".escape($row['request_scheme'])."</td>\n";
  275. echo " <td class='left hide-md-dn'>".escape($row['http_host'])."</td>\n";
  276. echo " <td class='left hide-md-dn'>".escape($row['server_port'])."</td>\n";
  277. echo " <td class='left hide-md-dn'>".escape($row['server_protocol'])."</td>\n";
  278. echo " <td class='left hide-md-dn'>".escape($row['query_string'])."</td>\n";
  279. echo " <td>".escape($row['remote_address'])."</td>\n";
  280. echo " <td class='left hide-md-dn'>".escape($row['http_user_agent'])."</td>\n";
  281. echo " <td class='left hide-md-dn'>".escape($row['http_status'])."</td>\n";
  282. echo " <td class='left hide-md-dn'>".escape($row['http_status_code'])."</td>\n";
  283. if (permission_exists('device_log_edit') && $list_row_edit_button == 'true') {
  284. echo " <td class='action-button'>\n";
  285. echo button::create(['type'=>'button','title'=>$text['button-edit'],'icon'=>$_SESSION['theme']['button_icon_edit'],'link'=>$list_row_url]);
  286. echo " </td>\n";
  287. }
  288. echo "</tr>\n";
  289. $x++;
  290. }
  291. }
  292. echo "</table>\n";
  293. echo "<br />\n";
  294. echo "<div align='center'>".$paging_controls."</div>\n";
  295. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  296. echo "</form>\n";
  297. //include the footer
  298. require_once "resources/footer.php";
  299. ?>