log-functions.php 2.8 KB

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