log-functions.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. function checkLog($path)
  3. {
  4. if (file_exists($path)) {
  5. if (filesize($path) > 500000) {
  6. rename($path, $path . '[' . date('Y-m-d') . '].json');
  7. return false;
  8. }
  9. return true;
  10. } else {
  11. return false;
  12. }
  13. }
  14. function writeLoginLog($username, $authType)
  15. {
  16. $username = htmlspecialchars($username, ENT_QUOTES);
  17. if (checkLog($GLOBALS['organizrLoginLog'])) {
  18. $getLog = str_replace("\r\ndate", "date", file_get_contents($GLOBALS['organizrLoginLog']));
  19. $gotLog = json_decode($getLog, true);
  20. }
  21. $logEntryFirst = array('logType' => 'login_log', 'auth' => array(array('date' => date("Y-m-d H:i:s"), 'utc_date' => $GLOBALS['currentTime'], 'username' => $username, 'ip' => userIP(), 'auth_type' => $authType)));
  22. $logEntry = array('date' => date("Y-m-d H:i:s"), 'utc_date' => $GLOBALS['currentTime'], 'username' => $username, 'ip' => userIP(), 'auth_type' => $authType);
  23. if (isset($gotLog)) {
  24. array_push($gotLog["auth"], $logEntry);
  25. $writeFailLog = str_replace("date", "\r\ndate", json_encode($gotLog));
  26. } else {
  27. $writeFailLog = str_replace("date", "\r\ndate", json_encode($logEntryFirst));
  28. }
  29. file_put_contents($GLOBALS['organizrLoginLog'], $writeFailLog);
  30. }
  31. function writeLog($type = 'error', $message, $username = null)
  32. {
  33. $GLOBALS['timeExecution'] = timeExecution($GLOBALS['timeExecution']);
  34. $message = $message . ' [Execution Time: ' . formatSeconds($GLOBALS['timeExecution']) . ']';
  35. $username = ($username) ? htmlspecialchars($username, ENT_QUOTES) : $GLOBALS['organizrUser']['username'];
  36. if (checkLog($GLOBALS['organizrLog'])) {
  37. $getLog = str_replace("\r\ndate", "date", file_get_contents($GLOBALS['organizrLog']));
  38. $gotLog = json_decode($getLog, true);
  39. }
  40. $logEntryFirst = array('logType' => 'organizr_log', 'log_items' => array(array('date' => date("Y-m-d H:i:s"), 'utc_date' => $GLOBALS['currentTime'], 'type' => $type, 'username' => $username, 'ip' => userIP(), 'message' => $message)));
  41. $logEntry = array('date' => date("Y-m-d H:i:s"), 'utc_date' => $GLOBALS['currentTime'], 'type' => $type, 'username' => $username, 'ip' => userIP(), 'message' => $message);
  42. if (isset($gotLog)) {
  43. array_push($gotLog["log_items"], $logEntry);
  44. $writeFailLog = str_replace("date", "\r\ndate", json_encode($gotLog));
  45. } else {
  46. $writeFailLog = str_replace("date", "\r\ndate", json_encode($logEntryFirst));
  47. }
  48. file_put_contents($GLOBALS['organizrLog'], $writeFailLog);
  49. }
  50. function getLog($type, $reverse = true)
  51. {
  52. switch ($type) {
  53. case 'login':
  54. case 'loginLog':
  55. $file = $GLOBALS['organizrLoginLog'];
  56. $parent = 'auth';
  57. break;
  58. case 'org':
  59. case 'organizrLog':
  60. $file = $GLOBALS['organizrLog'];
  61. $parent = 'log_items';
  62. // no break
  63. default:
  64. break;
  65. }
  66. if (!file_exists($file)) {
  67. return false;
  68. }
  69. $getLog = str_replace("\r\ndate", "date", file_get_contents($file));
  70. $gotLog = json_decode($getLog, true);
  71. return ($reverse) ? array_reverse($gotLog[$parent]) : $gotLog[$parent];
  72. }