device_log_edit.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. //check permissions
  28. if (permission_exists('device_log_add') || permission_exists('device_log_edit')) {
  29. //access granted
  30. }
  31. else {
  32. echo "access denied";
  33. exit;
  34. }
  35. //add multi-lingual support
  36. $language = new text;
  37. $text = $language->get();
  38. //action add or update
  39. if (!empty($_REQUEST["id"]) && is_uuid($_REQUEST["id"])) {
  40. $action = "update";
  41. $device_log_uuid = $_REQUEST["id"];
  42. $id = $_REQUEST["id"];
  43. }
  44. else {
  45. $action = "add";
  46. }
  47. //get http post variables and set them to php variables
  48. if (is_array($_POST)) {
  49. $device_log_uuid = $_POST["device_log_uuid"] ?? null;
  50. $device_uuid = $_POST["device_uuid"] ?? null;
  51. $timestamp = $_POST["timestamp"] ?? '';
  52. $device_address = $_POST["device_address"] ?? '';
  53. $request_scheme = $_POST["request_scheme"] ?? '';
  54. $http_host = $_POST["http_host"] ?? '';
  55. $server_port = $_POST["server_port"] ?? '';
  56. $server_protocol = $_POST["server_protocol"] ?? '';
  57. $query_string = $_POST["query_string"] ?? '';
  58. $remote_address = $_POST["remote_address"] ?? '';
  59. $http_user_agent = $_POST["http_user_agent"] ?? '';
  60. $http_status = $_POST["http_status"] ?? '';
  61. $http_status_code = $_POST["http_status_code"] ?? '';
  62. $http_content_body = $_POST["http_content_body"] ?? '';
  63. }
  64. //process the user data and save it to the database
  65. if (count($_POST) > 0 && strlen($_POST["persistformvar"]) == 0) {
  66. //delete the bridge
  67. if (permission_exists('bridge_delete')) {
  68. if ($_POST['action'] == 'delete' && is_uuid($device_log_uuid)) {
  69. //prepare
  70. $array[0]['checked'] = 'true';
  71. $array[0]['uuid'] = $device_log_uuid;
  72. //delete
  73. $obj = new device_logs;
  74. $obj->delete($array);
  75. //redirect
  76. header('Location: device_logs.php');
  77. exit;
  78. }
  79. }
  80. //get the uuid from the POST
  81. if ($action == "update") {
  82. $device_log_uuid = $_POST["device_log_uuid"];
  83. }
  84. //validate the token
  85. $token = new token;
  86. if (!$token->validate($_SERVER['PHP_SELF'])) {
  87. message::add($text['message-invalid_token'],'negative');
  88. header('Location: device_logs.php');
  89. exit;
  90. }
  91. //check for all required data
  92. $msg = '';
  93. //if (strlen($device_uuid) == 0) { $msg .= $text['message-required']." ".$text['label-device_uuid']."<br>\n"; }
  94. if (strlen($timestamp) == 0) { $msg .= $text['message-required']." ".$text['label-timestamp']."<br>\n"; }
  95. if (strlen($device_address) == 0) { $msg .= $text['message-required']." ".$text['label-device_address']."<br>\n"; }
  96. if (strlen($request_scheme) == 0) { $msg .= $text['message-required']." ".$text['label-request_scheme']."<br>\n"; }
  97. if (strlen($http_host) == 0) { $msg .= $text['message-required']." ".$text['label-http_host']."<br>\n"; }
  98. if (strlen($server_port) == 0) { $msg .= $text['message-required']." ".$text['label-server_port']."<br>\n"; }
  99. if (strlen($server_protocol) == 0) { $msg .= $text['message-required']." ".$text['label-server_protocol']."<br>\n"; }
  100. if (strlen($query_string) == 0) { $msg .= $text['message-required']." ".$text['label-query_string']."<br>\n"; }
  101. if (strlen($remote_address) == 0) { $msg .= $text['message-required']." ".$text['label-remote_address']."<br>\n"; }
  102. if (strlen($http_user_agent) == 0) { $msg .= $text['message-required']." ".$text['label-http_user_agent']."<br>\n"; }
  103. if (strlen($http_status) == 0) { $msg .= $text['message-required']." ".$text['label-http_status']."<br>\n"; }
  104. if (strlen($http_status_code) == 0) { $msg .= $text['message-required']." ".$text['label-http_status_code']."<br>\n"; }
  105. //if (strlen($http_content_body) == 0) { $msg .= $text['message-required']." ".$text['label-http_content_body']."<br>\n"; }
  106. if (strlen($msg) > 0 && strlen($_POST["persistformvar"]) == 0) {
  107. require_once "resources/header.php";
  108. require_once "resources/persist_form_var.php";
  109. echo "<div align='center'>\n";
  110. echo "<table><tr><td>\n";
  111. echo $msg."<br />";
  112. echo "</td></tr></table>\n";
  113. persistformvar($_POST);
  114. echo "</div>\n";
  115. require_once "resources/footer.php";
  116. return;
  117. }
  118. //add the device_log_uuid
  119. if (!is_uuid($_POST["device_log_uuid"])) {
  120. $device_log_uuid = uuid();
  121. }
  122. //prepare the array
  123. $array['device_logs'][0]['device_log_uuid'] = $device_log_uuid;
  124. $array['device_logs'][0]['domain_uuid'] = $_SESSION['domain_uuid'];
  125. $array['device_logs'][0]['device_uuid'] = $device_uuid;
  126. $array['device_logs'][0]['timestamp'] = $timestamp;
  127. $array['device_logs'][0]['device_address'] = $device_address;
  128. $array['device_logs'][0]['request_scheme'] = $request_scheme;
  129. $array['device_logs'][0]['http_host'] = $http_host;
  130. $array['device_logs'][0]['server_port'] = $server_port;
  131. $array['device_logs'][0]['server_protocol'] = $server_protocol;
  132. $array['device_logs'][0]['query_string'] = $query_string;
  133. $array['device_logs'][0]['remote_address'] = $remote_address;
  134. $array['device_logs'][0]['http_user_agent'] = $http_user_agent;
  135. $array['device_logs'][0]['http_status'] = $http_status;
  136. $array['device_logs'][0]['http_status_code'] = $http_status_code;
  137. $array['device_logs'][0]['http_content_body'] = $http_content_body;
  138. //save the data
  139. $database = new database;
  140. $database->app_name = 'device logs';
  141. $database->app_uuid = '78b1e5c7-5028-43e7-a05b-a36b44f87087';
  142. $database->save($array);
  143. //$message = $database->message;
  144. //redirect the user
  145. if (isset($action)) {
  146. if ($action == "add") {
  147. $_SESSION["message"] = $text['message-add'];
  148. }
  149. if ($action == "update") {
  150. $_SESSION["message"] = $text['message-update'];
  151. }
  152. header('Location: device_logs.php');
  153. //header('Location: device_log_edit.php?id='.urlencode($device_log_uuid));
  154. return;
  155. }
  156. }
  157. //pre-populate the form
  158. if (is_array($_GET) && empty($_POST["persistformvar"])) {
  159. $device_log_uuid = $_GET["id"] ?? '';
  160. $sql = "select * from v_device_logs ";
  161. $sql .= "where device_log_uuid = :device_log_uuid ";
  162. //$sql .= "and domain_uuid = :domain_uuid ";
  163. //$parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  164. $parameters['device_log_uuid'] = $device_log_uuid;
  165. $database = new database;
  166. $row = $database->select($sql, $parameters, 'row');
  167. if (is_array($row) && sizeof($row) != 0) {
  168. $device_uuid = $row["device_uuid"];
  169. $timestamp = $row["timestamp"];
  170. $device_address = $row["device_address"];
  171. $request_scheme = $row["request_scheme"];
  172. $http_host = $row["http_host"];
  173. $server_port = $row["server_port"];
  174. $server_protocol = $row["server_protocol"];
  175. $query_string = $row["query_string"];
  176. $remote_address = $row["remote_address"];
  177. $http_user_agent = $row["http_user_agent"];
  178. $http_status = $row["http_status"];
  179. $http_status_code = $row["http_status_code"];
  180. $http_content_body = $row["http_content_body"];
  181. }
  182. unset($sql, $parameters, $row);
  183. }
  184. //create token
  185. $object = new token;
  186. $token = $object->create($_SERVER['PHP_SELF']);
  187. //show the header
  188. $document['title'] = $text['title-device_log'];
  189. require_once "resources/header.php";
  190. //show the content
  191. echo "<form name='frm' id='frm' method='post' action=''>\n";
  192. echo "<div class='action_bar' id='action_bar'>\n";
  193. echo " <div class='heading'><b>".$text['title-device_log']."</b></div>\n";
  194. echo " <div class='actions'>\n";
  195. echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','style'=>'margin-right: 15px;','link'=>'device_logs.php']);
  196. if ($action == 'update' && permission_exists('device_log_delete')) {
  197. echo button::create(['type'=>'button','label'=>$text['button-delete'],'icon'=>$_SESSION['theme']['button_icon_delete'],'name'=>'btn_delete','style'=>'margin-right: 15px;','onclick'=>"modal_open('modal-delete','btn_delete');"]);
  198. }
  199. if ($action == 'update' && permission_exists('device_log_copy')) {
  200. echo button::create(['type'=>'button','label'=>$text['button-copy'],'icon'=>$_SESSION['theme']['button_icon_copy'],'name'=>'btn_copy','style'=>'margin-right: 15px;','link'=>'device_log_copy.php']);
  201. }
  202. echo button::create(['type'=>'submit','label'=>$text['button-save'],'icon'=>$_SESSION['theme']['button_icon_save'],'id'=>'btn_save','name'=>'action','value'=>'save']);
  203. echo " </div>\n";
  204. echo " <div style='clear: both;'>".$text['description-device_logs']."</div>\n";
  205. echo "</div>\n";
  206. if ($action == 'update' && permission_exists('device_log_delete')) {
  207. echo modal::create(['id'=>'modal-delete','type'=>'delete','actions'=>button::create(['type'=>'submit','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_delete','style'=>'float: right; margin-left: 15px;','collapse'=>'never','name'=>'action','value'=>'delete','onclick'=>"modal_close();"])]);
  208. }
  209. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  210. echo "<tr>\n";
  211. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  212. echo " ".$text['label-device_uuid']."\n";
  213. echo "</td>\n";
  214. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  215. echo " <input class='formfld' type='text' name='device_uuid' maxlength='255' value='".escape($device_uuid)."'>\n";
  216. echo "<br />\n";
  217. echo $text['description-device_uuid']."\n";
  218. echo "</td>\n";
  219. echo "</tr>\n";
  220. echo "<tr>\n";
  221. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  222. echo " ".$text['label-timestamp']."\n";
  223. echo "</td>\n";
  224. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  225. echo " <input class='formfld' type='text' name='timestamp' maxlength='255' value='".escape($timestamp)."'>\n";
  226. echo "<br />\n";
  227. echo $text['description-timestamp']."\n";
  228. echo "</td>\n";
  229. echo "</tr>\n";
  230. echo "<tr>\n";
  231. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  232. echo " ".$text['label-device_address']."\n";
  233. echo "</td>\n";
  234. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  235. echo " <input class='formfld' type='text' name='device_address' maxlength='255' value='".escape($device_address)."'>\n";
  236. echo "<br />\n";
  237. echo $text['description-device_address']."\n";
  238. echo "</td>\n";
  239. echo "</tr>\n";
  240. echo "<tr>\n";
  241. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  242. echo " ".$text['label-request_scheme']."\n";
  243. echo "</td>\n";
  244. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  245. echo " <input class='formfld' type='text' name='request_scheme' maxlength='255' value='".escape($request_scheme)."'>\n";
  246. echo "<br />\n";
  247. echo $text['description-request_scheme']."\n";
  248. echo "</td>\n";
  249. echo "</tr>\n";
  250. echo "<tr>\n";
  251. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  252. echo " ".$text['label-http_host']."\n";
  253. echo "</td>\n";
  254. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  255. echo " <input class='formfld' type='text' name='http_host' maxlength='255' value='".escape($http_host)."'>\n";
  256. echo "<br />\n";
  257. echo $text['description-http_host']."\n";
  258. echo "</td>\n";
  259. echo "</tr>\n";
  260. echo "<tr>\n";
  261. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  262. echo " ".$text['label-server_port']."\n";
  263. echo "</td>\n";
  264. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  265. echo " <input class='formfld' type='text' name='server_port' maxlength='255' value='".escape($server_port)."'>\n";
  266. echo "<br />\n";
  267. echo $text['description-server_port']."\n";
  268. echo "</td>\n";
  269. echo "</tr>\n";
  270. echo "<tr>\n";
  271. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  272. echo " ".$text['label-server_protocol']."\n";
  273. echo "</td>\n";
  274. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  275. echo " <input class='formfld' type='text' name='server_protocol' maxlength='255' value='".escape($server_protocol)."'>\n";
  276. echo "<br />\n";
  277. echo $text['description-server_protocol']."\n";
  278. echo "</td>\n";
  279. echo "</tr>\n";
  280. echo "<tr>\n";
  281. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  282. echo " ".$text['label-query_string']."\n";
  283. echo "</td>\n";
  284. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  285. echo " <input class='formfld' type='text' name='query_string' maxlength='255' value='".escape($query_string)."'>\n";
  286. echo "<br />\n";
  287. echo $text['description-query_string']."\n";
  288. echo "</td>\n";
  289. echo "</tr>\n";
  290. echo "<tr>\n";
  291. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  292. echo " ".$text['label-remote_address']."\n";
  293. echo "</td>\n";
  294. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  295. echo " <input class='formfld' type='text' name='remote_address' maxlength='255' value='".escape($remote_address)."'>\n";
  296. echo "<br />\n";
  297. echo $text['description-remote_address']."\n";
  298. echo "</td>\n";
  299. echo "</tr>\n";
  300. echo "<tr>\n";
  301. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  302. echo " ".$text['label-http_user_agent']."\n";
  303. echo "</td>\n";
  304. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  305. echo " <input class='formfld' type='text' name='http_user_agent' maxlength='255' value='".escape($http_user_agent)."'>\n";
  306. echo "<br />\n";
  307. echo $text['description-http_user_agent']."\n";
  308. echo "</td>\n";
  309. echo "</tr>\n";
  310. echo "<tr>\n";
  311. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  312. echo " ".$text['label-http_status']."\n";
  313. echo "</td>\n";
  314. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  315. echo " <input class='formfld' type='text' name='http_status' maxlength='255' value='".escape($http_status)."'>\n";
  316. echo "<br />\n";
  317. echo $text['description-http_status']."\n";
  318. echo "</td>\n";
  319. echo "</tr>\n";
  320. echo "<tr>\n";
  321. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  322. echo " ".$text['label-http_status_code']."\n";
  323. echo "</td>\n";
  324. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  325. echo " <input class='formfld' type='text' name='http_status_code' maxlength='255' value='".escape($http_status_code)."'>\n";
  326. echo "<br />\n";
  327. echo $text['description-http_status_code']."\n";
  328. echo "</td>\n";
  329. echo "</tr>\n";
  330. echo "<tr>\n";
  331. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  332. echo " ".$text['label-http_content_body']."\n";
  333. echo "</td>\n";
  334. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  335. echo " <textarea class='formfld' name='http_content_body' style='width: 100%; height: 300px; max-width: 5000px;'>".$http_content_body."</textarea>\n";
  336. echo "<br />\n";
  337. echo $text['description-http_content_body']."\n";
  338. echo "</td>\n";
  339. echo "</tr>\n";
  340. echo "</table>";
  341. echo "<br /><br />";
  342. if ($action == "update") {
  343. echo "<input type='hidden' name='device_log_uuid' value='".escape($device_log_uuid)."'>\n";
  344. }
  345. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  346. echo "</form>";
  347. //include the footer
  348. require_once "resources/footer.php";
  349. ?>