PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/log.inc.php

https://bitbucket.org/hky/bytehoard2
PHP | 160 lines | 97 code | 29 blank | 34 comment | 18 complexity | b0af9598336a5a423e2700e95192605d MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. * ByteHoard 2.1
  4. * Copyright (c) Andrew Godwin & contributors 2003-2004
  5. *
  6. * Log Functions File
  7. * $Id: log.inc.php,v 1.3 2005/07/28 20:11:47 andrewgodwin Exp $
  8. *
  9. */
  10. /*
  11. This include contains the logging function - pass it a message and the message type and it will do what it needs to do.
  12. */
  13. # Test for include status
  14. if (IN_BH != 1) { header("Location: ../index.php"); die(); }
  15. # Log function
  16. function bh_log($message, $type) {
  17. global $bhlang, $bhcurrent, $bhconfig, $bherrors;
  18. # This is the all-singing, all-dancing logging system.
  19. # First, retrieve all actions matching this type from the database
  20. $matchingactions = select_bhdb("logactions", array("type"=>$type), "");
  21. # Then, see if there are some, and go through them if there are.
  22. if (!empty($matchingactions)) {
  23. foreach ($matchingactions as $matchingaction) {
  24. # Try to match the action to the ones we know about.
  25. switch ($matchingaction['action']) {
  26. case "fileappend":
  27. case "logtofile":
  28. # Append to a file. Check if we have a filename, or just log to the default one.
  29. if (!empty($matchingaction['parameters'])) {
  30. $fn = @fopen($bhconfig['bhfilepath']."/".$matchingaction['parameters'], "a");
  31. @fputs($fn, time().":".$_SERVER['REMOTE_ADDR'].":".$message."\n");
  32. @fclose($fn);
  33. } elseif (!empty($bhconfig['logfile'])) {
  34. $fn = @fopen($bhconfig['bhfilepath']."/".$bhconfig['logfile'], "a");
  35. @fputs($fn, time().":".$_SERVER['REMOTE_ADDR'].":".$message."\n");
  36. @fclose($fn);
  37. } else {
  38. $fn = @fopen($bhconfig['bhfilepath']."/log", "a");
  39. @fputs($fn, time().":".$_SERVER['REMOTE_ADDR'].":".$message."\n");
  40. @fclose($fn);
  41. }
  42. break;
  43. case "email":
  44. # Email it to someone. Check for a specified email address, or fail.
  45. if (!empty($matchingaction['parameters'])) {
  46. $emailobj = new bhemail($matchingaction['parameters']);
  47. $emailobj->subject = "Notification from ByteHoard @ ".$_SERVER['HTTP_HOST'];
  48. $emailobj->sig = "\n\n\nPowered by ByteHoard ".$bhconfig['version']." / Sent at ".date("l dS F Y h:i:s A");
  49. $emailobj->message = $message;
  50. $emailobj->send();
  51. } else {
  52. # Nothing to do. Oh well.
  53. }
  54. break;
  55. case "emailtype":
  56. # Email it to them. Check for a specified type, or fail.
  57. if (!empty($matchingaction['parameters'])) {
  58. $emailobj = new bhemail();
  59. $emailobj->subject = "Notification from ByteHoard @ ".$_SERVER['HTTP_HOST'];
  60. $emailobj->sig = "\n\n\nPowered by ByteHoard ".$bhconfig['version']." / Sent at ".date("l dS F Y h:i:s A");
  61. $emailobj->message = $message;
  62. $emailobj->sendtotype($matchingaction['parameters']);
  63. } else {
  64. # Nothing to do. Oh well.
  65. }
  66. break;
  67. case "onscreen":
  68. # Set error in template thingmywatsit.
  69. $bherrors[] = array('message'=>$message, 'type'=>$matchingaction['parameters']);
  70. break;
  71. }
  72. }
  73. } else {
  74. # Set error in template thingmywatsit.
  75. $bherrors[] = array('message'=>$message, 'type'=>'warning');
  76. }
  77. }
  78. # Sets logging variables to parse lang strings.
  79. function bh_add_logvars($vars) {
  80. global $bhlogvars;
  81. if (is_array($vars)) {
  82. if (is_array($bhlogvars)){
  83. $bhlogvars = array_merge($vars, $bhlogvars);
  84. } else {
  85. $bhlogvars = $vars;
  86. }
  87. }
  88. }
  89. # Set some on script inclusion
  90. bh_add_logvars(array("ip"=>$_SERVER['REMOTE_ADDR']));
  91. # Will parse a string and replace #THESE# with their logvar, if found.
  92. function bh_parse_logvars($string) {
  93. global $bhlogvars;
  94. if (is_array($bhlogvars)) {
  95. foreach ($bhlogvars as $var=>$val) {
  96. $string = str_replace("#".strtoupper($var)."#", $val, $string);
  97. }
  98. }
  99. return $string;
  100. }
  101. # Displays a notice
  102. function bh_add_notice($message) {
  103. global $bherrors, $bhlogvars;
  104. $bherrors[] = array('message'=>bh_parse_logvars($message), 'type'=>'notice');
  105. }
  106. # Displays a warning
  107. function bh_add_warning($message) {
  108. global $bherrors, $bhlogvars;
  109. $bherrors[] = array('message'=>bh_parse_logvars($message), 'type'=>'warning');
  110. }
  111. # Displays an error
  112. function bh_add_error($message) {
  113. global $bherrors, $bhlogvars;
  114. $bherrors[] = array('message'=>bh_parse_logvars($message), 'type'=>'error');
  115. }
  116. function bh_add_log($message, $type) {
  117. global $bhlogvars;
  118. bh_log(bh_parse_logvars($message), $type);
  119. }
  120. # Alias for bh_log
  121. function bh_error($message, $type) {
  122. return bh_log($message, $type);
  123. }
  124. # Internationalised die function
  125. function bh_die($langkey) {
  126. global $bhlang, $bhconfig;
  127. if (empty($bhlang[$langkey])) {$str = "A fatal system error has occured for which there is no explanation. Please contact the ByteHoard team as soon as possible."; } else { $str = $bhlang[$langkey]; }
  128. die("<html><head><title>ByteHoard Error</title></head><body><h1>Error</h1><br>$str<br><hr><i>ByteHoard ".$bhconfig['version']."</i></body></html>");
  129. }