log-functions.php 2.6 KB

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