device_logs.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. //connect to the database
  40. $database = new database;
  41. //set additional variables
  42. $search = $_GET["search"] ?? '';
  43. $show = $_GET["show"] ?? '';
  44. //set from session variables
  45. $list_row_edit_button = !empty($_SESSION['theme']['list_row_edit_button']['boolean']) ? $_SESSION['theme']['list_row_edit_button']['boolean'] : 'false';
  46. //get the http post data
  47. if (!empty($_POST['device_logs']) && is_array($_POST['device_logs'])) {
  48. $action = $_POST['action'];
  49. $search = $_POST['search'];
  50. $device_logs = $_POST['device_logs'];
  51. }
  52. //process the http post data by action
  53. if (!empty($action) && is_array($device_logs) && @sizeof($device_logs) != 0) {
  54. switch ($action) {
  55. case 'copy':
  56. if (permission_exists('device_log_add')) {
  57. $obj = new device_logs;
  58. $obj->copy($device_logs);
  59. }
  60. break;
  61. case 'toggle':
  62. if (permission_exists('device_log_edit')) {
  63. $obj = new device_logs;
  64. $obj->toggle($device_logs);
  65. }
  66. break;
  67. case 'delete':
  68. if (permission_exists('device_log_delete')) {
  69. $obj = new device_logs;
  70. $obj->delete($device_logs);
  71. }
  72. break;
  73. }
  74. header('Location: device_logs.php'.($search != '' ? '?search='.urlencode($search) : null));
  75. exit;
  76. }
  77. //get order and order by
  78. $order_by = $_GET["order_by"] ?? '';
  79. $order = $_GET["order"] ?? '';
  80. //set the time zone
  81. if (isset($_SESSION['domain']['time_zone']['name'])) {
  82. $time_zone = $_SESSION['domain']['time_zone']['name'];
  83. }
  84. else {
  85. $time_zone = date_default_timezone_get();
  86. }
  87. //add the search string
  88. if (isset($_GET["search"]) && !empty($_GET["search"])) {
  89. $search = strtolower($_GET["search"]);
  90. }
  91. //get the count
  92. $sql = "select count(device_log_uuid) \n";
  93. $sql .= "FROM v_device_logs AS l \n";
  94. $sql .= "LEFT JOIN v_domains AS d ON l.domain_uuid = d.domain_uuid \n";
  95. if ($show == "all" && permission_exists('device_log_all')) {
  96. $sql .= "WHERE true \n";
  97. }
  98. else {
  99. $sql .= "WHERE (l.domain_uuid = :domain_uuid) \n";
  100. $parameters['domain_uuid'] = $domain_uuid;
  101. }
  102. if (!empty($search)) {
  103. $sql .= "and (";
  104. $sql .= " lower(device_address) like :search ";
  105. $sql .= " or lower(request_scheme) like :search ";
  106. $sql .= " or lower(http_host) like :search ";
  107. $sql .= " or lower(server_port) like :search ";
  108. $sql .= " or lower(server_protocol) like :search ";
  109. $sql .= " or lower(query_string) like :search ";
  110. $sql .= " or lower(remote_address) like :search ";
  111. $sql .= " or lower(http_user_agent) like :search ";
  112. $sql .= " or lower(http_status) like :search ";
  113. $sql .= " or lower(http_status_code) like :search ";
  114. $sql .= ") ";
  115. $parameters['search'] = '%'.$search.'%';
  116. }
  117. $num_rows = $database->select($sql, $parameters, 'column');
  118. //prepare to page the results
  119. $rows_per_page = (!empty($_SESSION['domain']['paging']['numeric'])) ? $_SESSION['domain']['paging']['numeric'] : 50;
  120. $param = $search ? "&search=".$search : null;
  121. $param .= ($show == 'all' && permission_exists('device_log_all')) ? "&show=all" : null;
  122. $page = isset($_GET['page']) ? $_GET['page'] : 0;
  123. list($paging_controls, $rows_per_page) = paging($num_rows, $param, $rows_per_page);
  124. list($paging_controls_mini, $rows_per_page) = paging($num_rows, $param, $rows_per_page, true);
  125. $offset = $rows_per_page * $page;
  126. //get the list
  127. $sql = "SELECT \n";
  128. $sql .= "d.domain_uuid, \n";
  129. $sql .= "device_log_uuid, \n";
  130. $sql .= "device_uuid, \n";
  131. $sql .= "d.domain_name, \n";
  132. $sql .= "timestamp, \n";
  133. $sql .= "to_char(timezone(:time_zone, timestamp), 'DD Mon YYYY') as date_formatted, \n";
  134. $sql .= "to_char(timezone(:time_zone, timestamp), 'HH12:MI:SS am') as time_formatted, \n";
  135. $sql .= "device_address, \n";
  136. $sql .= "request_scheme, \n";
  137. $sql .= "http_host, \n";
  138. $sql .= "server_port, \n";
  139. $sql .= "server_protocol, \n";
  140. $sql .= "query_string, \n";
  141. $sql .= "remote_address, \n";
  142. $sql .= "http_user_agent, \n";
  143. $sql .= "http_status, \n";
  144. $sql .= "http_status_code, \n";
  145. $sql .= "http_content_body \n";
  146. $sql .= "FROM v_device_logs AS l \n";
  147. $sql .= "LEFT JOIN v_domains AS d ON l.domain_uuid = d.domain_uuid \n";
  148. if ($show == "all" && permission_exists('device_log_all')) {
  149. $sql .= "WHERE true \n";
  150. }
  151. else {
  152. $sql .= "WHERE (l.domain_uuid = :domain_uuid) \n";
  153. $parameters['domain_uuid'] = $domain_uuid;
  154. }
  155. if (!empty($search)) {
  156. $sql .= "AND ( \n";
  157. $sql .= " lower(device_address) like :search \n";
  158. $sql .= " or lower(request_scheme) like :search \n";
  159. $sql .= " or lower(http_host) like :search \n";
  160. $sql .= " or lower(server_port) like :search \n";
  161. $sql .= " or lower(server_protocol) like :search \n";
  162. $sql .= " or lower(query_string) like :search \n";
  163. $sql .= " or lower(remote_address) like :search \n";
  164. $sql .= " or lower(http_user_agent) like :search \n";
  165. $sql .= " or lower(http_status) like :search \n";
  166. $sql .= " or lower(http_status_code) like :search \n";
  167. $sql .= ") ";
  168. $parameters['search'] = '%'.$search.'%';
  169. }
  170. $parameters['time_zone'] = $time_zone;
  171. $sql .= order_by($order_by, $order, 'timestamp', 'desc');
  172. $sql .= limit_offset($rows_per_page, $offset);
  173. $device_logs = $database->select($sql, $parameters, 'all');
  174. unset($sql, $parameters);
  175. //create token
  176. $object = new token;
  177. $token = $object->create($_SERVER['PHP_SELF']);
  178. //include the header
  179. $document['title'] = $text['title-device_logs'];
  180. require_once "resources/header.php";
  181. //show the content
  182. echo "<div class='action_bar' id='action_bar'>\n";
  183. echo " <div class='heading'><b>".$text['title-device_logs']."</b><div class='count'>".number_format($num_rows)."</div></div>\n";
  184. echo " <div class='actions'>\n";
  185. if (permission_exists('device_log_add')) {
  186. echo button::create(['type'=>'button','label'=>$text['button-add'],'icon'=>$_SESSION['theme']['button_icon_add'],'id'=>'btn_add','link'=>'device_log_edit.php']);
  187. }
  188. if (permission_exists('device_log_delete') && $device_logs) {
  189. 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');"]);
  190. }
  191. echo "<form id='form_search' class='inline' method='get'>\n";
  192. if (permission_exists('device_log_all')) {
  193. if ($show == 'all') {
  194. echo " <input type='hidden' name='show' value='all'>\n";
  195. }
  196. else {
  197. echo button::create(['type'=>'button','label'=>$text['button-show_all'],'icon'=>$_SESSION['theme']['button_icon_all'],'link'=>'?show=all']);
  198. }
  199. }
  200. echo "<input type='text' class='txt list-search' name='search' id='search' value=\"".escape($search)."\" placeholder=\"".$text['label-search']."\" onkeydown='list_search_reset();'>";
  201. echo button::create(['label'=>$text['button-search'],'icon'=>$_SESSION['theme']['button_icon_search'],'type'=>'submit','id'=>'btn_search','style'=>($search != '' ? 'display: none;' : null)]);
  202. 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)]);
  203. if (!empty($paging_controls_mini)) {
  204. echo "<span style='margin-left: 15px;'>".$paging_controls_mini."</span>\n";
  205. }
  206. echo " </form>\n";
  207. echo " </div>\n";
  208. echo " <div style='clear: both;'></div>\n";
  209. echo "</div>\n";
  210. if (permission_exists('device_log_add') && $device_logs) {
  211. 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');"])]);
  212. }
  213. if (permission_exists('device_log_edit') && $device_logs) {
  214. 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');"])]);
  215. }
  216. if (permission_exists('device_log_delete') && $device_logs) {
  217. 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');"])]);
  218. }
  219. echo $text['description-device_logs']."\n";
  220. echo "<br /><br />\n";
  221. echo "<form id='form_list' method='post'>\n";
  222. echo "<input type='hidden' id='action' name='action' value=''>\n";
  223. echo "<input type='hidden' name='search' value=\"".escape($search)."\">\n";
  224. echo "<div class='card'>\n";
  225. echo "<table class='list'>\n";
  226. echo "<tr class='list-header'>\n";
  227. if (permission_exists('device_log_add') || permission_exists('device_log_edit') || permission_exists('device_log_delete')) {
  228. echo " <th class='checkbox'>\n";
  229. echo " <input type='checkbox' id='checkbox_all' name='checkbox_all' onclick='list_all_toggle();' ".(!empty($device_logs) ?: "style='visibility: hidden;'").">\n";
  230. echo " </th>\n";
  231. }
  232. if ($show == 'all' && permission_exists('device_log_all')) {
  233. echo th_order_by('domain_name', $text['label-domain'], $order_by, $order);
  234. }
  235. echo "<th class='left'>".$text['label-date']."</th>\n";
  236. echo "<th class='left hide-md-dn'>".$text['label-time']."</th>\n";
  237. echo th_order_by('device_address', $text['label-device_address'], $order_by, $order);
  238. echo "<th class='left hide-sm-dn'>".$text['label-request_scheme']."</th>\n";
  239. echo "<th class='left hide-md-dn'>".$text['label-http_host']."</th>\n";
  240. echo "<th class='left hide-md-dn'>".$text['label-server_port']."</th>\n";
  241. echo "<th class='left hide-md-dn'>".$text['label-server_protocol']."</th>\n";
  242. echo "<th class='left hide-md-dn'>".$text['label-query_string']."</th>\n";
  243. echo th_order_by('remote_address', $text['label-remote_address'], $order_by, $order);
  244. echo "<th class='left hide-md-dn'>".$text['label-http_user_agent']."</th>\n";
  245. echo "<th class='left hide-md-dn'>".$text['label-http_status']."</th>\n";
  246. echo "<th class='left hide-md-dn'>".$text['label-http_status_code']."</th>\n";
  247. if (permission_exists('device_log_edit') && $list_row_edit_button == 'true') {
  248. echo " <td class='action-button'>&nbsp;</td>\n";
  249. }
  250. echo "</tr>\n";
  251. if (is_array($device_logs) && @sizeof($device_logs) != 0) {
  252. $x = 0;
  253. foreach ($device_logs as $row) {
  254. if (permission_exists('device_log_edit')) {
  255. $list_row_url = "device_log_edit.php?id=".urlencode($row['device_log_uuid']);
  256. }
  257. echo "<tr class='list-row' href='".$list_row_url."'>\n";
  258. if (permission_exists('device_log_add') || permission_exists('device_log_edit') || permission_exists('device_log_delete')) {
  259. echo " <td class='checkbox'>\n";
  260. 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";
  261. echo " <input type='hidden' name='device_logs[$x][uuid]' value='".escape($row['device_log_uuid'])."' />\n";
  262. echo " </td>\n";
  263. }
  264. if ($show == 'all' && permission_exists('device_log_all')) {
  265. echo " <td>".escape($_SESSION['domains'][$row['domain_uuid']]['domain_name'])."</td>\n";
  266. }
  267. echo " <td>".escape($row['date_formatted'])."</td>\n";
  268. echo " <td class='left hide-md-dn'>".escape($row['time_formatted'])."</td>\n";
  269. echo " <td>".escape($row['device_address'])."</td>\n";
  270. echo " <td class='left hide-sm-dn'>".escape($row['request_scheme'])."</td>\n";
  271. echo " <td class='left hide-md-dn'>".escape($row['http_host'])."</td>\n";
  272. echo " <td class='left hide-md-dn'>".escape($row['server_port'])."</td>\n";
  273. echo " <td class='left hide-md-dn'>".escape($row['server_protocol'])."</td>\n";
  274. echo " <td class='left hide-md-dn'>".escape($row['query_string'])."</td>\n";
  275. echo " <td>".escape($row['remote_address'])."</td>\n";
  276. echo " <td class='left hide-md-dn'>".escape($row['http_user_agent'])."</td>\n";
  277. echo " <td class='left hide-md-dn'>".escape($row['http_status'])."</td>\n";
  278. echo " <td class='left hide-md-dn'>".escape($row['http_status_code'])."</td>\n";
  279. if (permission_exists('device_log_edit') && $list_row_edit_button == 'true') {
  280. echo " <td class='action-button'>\n";
  281. echo button::create(['type'=>'button','title'=>$text['button-edit'],'icon'=>$_SESSION['theme']['button_icon_edit'],'link'=>$list_row_url]);
  282. echo " </td>\n";
  283. }
  284. echo "</tr>\n";
  285. $x++;
  286. }
  287. }
  288. echo "</table>\n";
  289. echo "</div>\n";
  290. echo "<br />\n";
  291. echo "<div align='center'>".$paging_controls."</div>\n";
  292. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  293. echo "</form>\n";
  294. //include the footer
  295. require_once "resources/footer.php";
  296. ?>