log-functions.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. function writeLoginLog($username, $authType)
  3. {
  4. if (file_exists($GLOBALS['organizrLoginLog'])) {
  5. $getLog = str_replace("\r\ndate", "date", file_get_contents($GLOBALS['organizrLoginLog']));
  6. $gotLog = json_decode($getLog, true);
  7. }
  8. $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)));
  9. $logEntry = array('date' => date("Y-m-d H:i:s"), 'utc_date' => $GLOBALS['currentTime'], 'username' => $username, 'ip' => userIP(), 'auth_type' => $authType);
  10. if (isset($gotLog)) {
  11. array_push($gotLog["auth"], $logEntry);
  12. $writeFailLog = str_replace("date", "\r\ndate", json_encode($gotLog));
  13. } else {
  14. $writeFailLog = str_replace("date", "\r\ndate", json_encode($logEntryFirst));
  15. }
  16. file_put_contents($GLOBALS['organizrLoginLog'], $writeFailLog);
  17. }
  18. function writeLog($type = 'error', $message, $username = null)
  19. {
  20. $username = ($username) ? $username : $GLOBALS['organizrUser']['username'];
  21. if (file_exists($GLOBALS['organizrLog'])) {
  22. $getLog = str_replace("\r\ndate", "date", file_get_contents($GLOBALS['organizrLog']));
  23. $gotLog = json_decode($getLog, true);
  24. }
  25. $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)));
  26. $logEntry = array('date' => date("Y-m-d H:i:s"), 'utc_date' => $GLOBALS['currentTime'], 'type' => $type, 'username' => $username, 'ip' => userIP(), 'message' => $message);
  27. if (isset($gotLog)) {
  28. array_push($gotLog["log_items"], $logEntry);
  29. $writeFailLog = str_replace("date", "\r\ndate", json_encode($gotLog));
  30. } else {
  31. $writeFailLog = str_replace("date", "\r\ndate", json_encode($logEntryFirst));
  32. }
  33. file_put_contents($GLOBALS['organizrLog'], $writeFailLog);
  34. }
  35. function getLog($type, $reverse = true)
  36. {
  37. switch ($type) {
  38. case 'login':
  39. case 'loginLog':
  40. $file = $GLOBALS['organizrLoginLog'];
  41. $parent = 'auth';
  42. break;
  43. case 'org':
  44. case 'organizrLog':
  45. $file = $GLOBALS['organizrLog'];
  46. $parent = 'log_items';
  47. // no break
  48. default:
  49. break;
  50. }
  51. if (!file_exists($file)) {
  52. return false;
  53. }
  54. $getLog = str_replace("\r\ndate", "date", file_get_contents($file));
  55. $gotLog = json_decode($getLog, true);
  56. return ($reverse) ? array_reverse($gotLog[$parent]) : $gotLog[$parent];
  57. }