ニジカもんすたぁ!! トップ・ページ https://nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fetch_data.php 1.8 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. const LOG_PATH = '../log.txt';
  3. header ('Content-Type: application/json; charset=UTF-8');
  4. $log_data = [];
  5. $page = (int) (filter_input (INPUT_POST, 'p') ?? 1);
  6. $length = (int) (filter_input (INPUT_POST, 'max') ?? 20);
  7. $asc = (int) (filter_input (INPUT_POST, 'asc') ?? 0) !== 0;
  8. $keyword = trim (filter_input (INPUT_POST, 'q') ?? '');
  9. $date_start = filter_input (INPUT_POST, 'start');
  10. $date_end = filter_input (INPUT_POST, 'end');
  11. $f = fopen (LOG_PATH, 'r');
  12. if ($f !== false)
  13. {
  14. while (($dt = fgetcsv ($f, 0, "\t")) !== false)
  15. {
  16. $chat_info = json_decode ($dt[1]);
  17. $log_data[] = ['dateTime' => $dt[0],
  18. 'chatIcon' => $chat_info -> author -> imageUrl,
  19. 'chatName' => htmlspecialchars ($chat_info -> author -> name),
  20. 'chatMessage' => htmlspecialchars ($chat_info -> message),
  21. 'answer' => htmlspecialchars ($dt[2])];
  22. }
  23. }
  24. fclose ($f);
  25. unset ($f);
  26. if ($keyword !== '')
  27. {
  28. $log_data = array_filter ($log_data, fn ($row) => (
  29. strpos (($row['chatName'] . "\n" . $row['chatMessage'] . "\n"
  30. . $row['answer']),
  31. $keyword)
  32. !== false));
  33. }
  34. if ($date_start)
  35. {
  36. $log_data = array_filter ($log_data, fn ($row) => (
  37. substr ($row['dateTime'], 0, 10) >= $date_start));
  38. }
  39. if ($date_end)
  40. {
  41. $log_data = array_filter ($log_data, fn ($row) => (
  42. substr ($row['dateTime'], 0, 10) <= $date_end));
  43. }
  44. $pages_max = (int) ((count ($log_data) - 1) / $length) + 1;
  45. if (!($asc))
  46. $log_data = array_reverse ($log_data);
  47. $log_data = array_slice ($log_data, ($page - 1) * $length, $length, false);
  48. echo ('{"data": ' . json_encode ($log_data) . ', "max": ' . $pages_max
  49. . ', "page": ' . $page . '}');