log-functions.php 2.6 KB

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