LogController.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace Controller;
  3. class LogController
  4. {
  5. const FIELDS = 4;
  6. public function downloadFileLog($startDate, $stopDate, $header)
  7. {
  8. $file = \Core\Log\FileLog::getFile();
  9. header("Content-Type: text/csv");
  10. header("Content-Disposition: attachment; filename=" . basename(str_replace('.log', '.csv', $file)));
  11. header("Cache-Control: no-cache, no-store, must-revalidate");
  12. header("Pragma: no-cache");
  13. header("Expires: 0");
  14. $output = fopen("php://output", "w");
  15. if (file_exists($file)) {
  16. $f = fopen($file, "r");
  17. fputcsv($output, $header);
  18. while(!feof($f)) {
  19. $row = explode(\Core\Log\FileLog::DATA_SEPARATOR, fgets($f));
  20. $rowDate = date('Y-m-d', strtotime($row[0]));
  21. if(
  22. count($row) == self::FIELDS &&
  23. strtotime($rowDate) >= strtotime($startDate) &&
  24. strtotime($rowDate) <= strtotime($stopDate)
  25. )
  26. {
  27. $row[0] = date('d.m.Y H:i:s', strtotime($row[0]));
  28. fputcsv($output, array_slice($row, 0, self::FIELDS - 1));
  29. }
  30. }
  31. fclose($output);
  32. fclose($f);
  33. exit;
  34. } else {
  35. throw new \Exception('Error');
  36. }
  37. }
  38. public function clearFileLog()
  39. {
  40. $file = \Core\Log\FileLog::getFile();
  41. if (file_exists($file) && filesize($file) > 0) {
  42. $f = fopen($file, "w+");
  43. fclose($f);
  44. } else {
  45. throw new \Exception('Error');
  46. }
  47. }
  48. }