/htdocs/isis-oai-provider/lib/Log.php

https://github.com/bireme/isis-oai-provider · PHP · 159 lines · 105 code · 14 blank · 40 comment · 12 complexity · f462b3afa324cbc3240ae6461eee7a50 MD5 · raw file

  1. <?php
  2. define('LOG_SEPARATOR', ";");
  3. class Log
  4. {
  5. /**
  6. * @var String
  7. * @desc Path do diretorio de logs com permissao de gravacao para usuario web
  8. */
  9. var $directory;
  10. /**
  11. * @var String
  12. * @desc Nome do arquivo de log
  13. */
  14. var $fileName;
  15. var $fields = Array();
  16. /**
  17. * @var Array
  18. * @desc Campos de informacao do log
  19. */
  20. /**
  21. * @desc constructor
  22. */
  23. function log()
  24. {
  25. $this->setDirectory();
  26. if (defined('LOG_FILE')) {
  27. $this->setFileName(LOG_FILE);
  28. }
  29. }
  30. /**
  31. * @desc metodo que seta o diretorio onde os logs deveram ser escritos
  32. * @param string LOG_DIR constante definida como o path para o dir de logs
  33. */
  34. function setDirectory()
  35. {
  36. if (!defined('LOG_DIR')) {
  37. define('LOG_DIR', realpath( dirname(__FILE__) . "/../logs") . "/");
  38. }
  39. if ( is_dir(LOG_DIR) ){
  40. $this->directory = LOG_DIR;
  41. }else{
  42. if ( $this->mkdirs(LOG_DIR,0775) ){
  43. $this->directory = LOG_DIR;
  44. }else{
  45. $this->logError("Unable to create directory " . LOG_DIR);
  46. }
  47. }
  48. }
  49. /**
  50. * @desc metodo que define o nome do arquivo e log
  51. * @param string $fileName nome do arquivo de log
  52. */
  53. function setFileName($fileName)
  54. {
  55. $this->fileName = $fileName;
  56. }
  57. /**
  58. * @desc cria arquivo no diretorio especificado ou abre arquivo se ja tiver sido criado
  59. * @param string
  60. */
  61. function openFileWriter()
  62. {
  63. if ( is_file($this->directory . $this->fileName) ){
  64. $fp = @fopen ($this->directory . $this->fileName, "a+b");
  65. }else{
  66. $fp = @fopen ($this->directory . $this->fileName, "a+b");
  67. $head = "date" . LOG_SEPARATOR . implode(LOG_SEPARATOR, array_keys($this->fields)) . "\r\n";
  68. @chmod($this->directory . $this->fileName,0764);
  69. @fwrite($fp, $head);
  70. }
  71. if ($fp){
  72. return $fp;
  73. }else{
  74. $this->logError("Unable to open log file " . LOG_DIR . $this->fileName);
  75. }
  76. }
  77. /**
  78. * @desc grava no arquivo de log
  79. */
  80. function writeLog()
  81. {
  82. $fp = $this->openFileWriter($this->directory,$this->fileName);
  83. $logLine = implode(LOG_SEPARATOR, $this->fields);
  84. $logInfo = date("Y-m-d H:i:s") . LOG_SEPARATOR . $logLine ."\r\n";
  85. if (!@fwrite($fp, $logInfo)){
  86. $this->logError("Unable to write log file " . LOG_DIR . $this->fileName);
  87. }else{
  88. fclose($fp);
  89. }
  90. }
  91. /**
  92. * @desc le arquivo e coloca em um array
  93. */
  94. function readLog(){
  95. $text = file($this->directory.$this->fileName);
  96. //recupera array retirado do arquivo
  97. foreach ($text as $num_linha=>$linha) {
  98. $leitura.= $linha;
  99. }
  100. echo '<textarea name="textareaLog" rows="30" cols="120">'.$leitura.'</textarea>';
  101. }
  102. /**
  103. * @desc adiciona log de erro
  104. */
  105. function logError($message)
  106. {
  107. $fp = @fopen ($this->directory . "logerror.txt", "a+b");
  108. if ( !$fp ){
  109. error_log ("Unable to open log file for update " . LOG_DIR . "logerror.txt");
  110. }else{
  111. if ( !fwrite($fp, date("Y-m-d H:i:s") . LOG_SEPARATOR . $message . "\r\n") ){
  112. error_log ("Unable to write in log file " . $this->directory . "logerror.txt");
  113. }else{
  114. error_log ("Log error message: " . $message);
  115. }
  116. }
  117. return;
  118. }
  119. function mkdirs($strPath, $mode = "0775")
  120. {
  121. if ( is_dir($strPath) ) {
  122. return true;
  123. }
  124. $pStrPath = dirname($strPath);
  125. if ( ! $this->mkdirs($pStrPath, $mode) ){
  126. return false;
  127. }
  128. $old_umask = umask(0);
  129. $mk = @mkdir($strPath, $mode);
  130. umask($old_umask);
  131. return $mk;
  132. }
  133. /**
  134. * @desc log de acessos ao ADM
  135. */
  136. function logAccess()
  137. {
  138. $this->setFileName("access_log.txt");
  139. $ip = getenv(REMOTE_ADDR); //guarda o endereco ip do host
  140. $host = gethostbyaddr($ip); //guarda o mome do host
  141. $text = "[".date('d/m/Y h:i:s')."] - $host;$ip;$data\n";
  142. $this->writeFile($text);
  143. }
  144. }
  145. ?>