/libs/Atmail/Log.php

https://bitbucket.org/tduarte/atmailopen · PHP · 216 lines · 130 code · 46 blank · 40 comment · 17 complexity · 79bfd8d838e658ad4101b077b440d4cb MD5 · raw file

  1. <?php
  2. // +----------------------------------------------------------------+
  3. // | Log.php |
  4. // +----------------------------------------------------------------+
  5. // | AtMail Open - Licensed under the Apache 2.0 Open-source License|
  6. // | http://opensource.org/licenses/apache2.0.php |
  7. // +----------------------------------------------------------------+
  8. // | Date: Febuary 2005 |
  9. // +----------------------------------------------------------------+
  10. require_once('header.php');
  11. define ('QUERY_ERROR', 'Error in SQL Query');
  12. require_once('Config.php');
  13. class Log
  14. {
  15. var $account;
  16. var $errtype;
  17. var $msg;
  18. var $logtype;
  19. var $db;
  20. var $tables = array('Error', 'Login', 'RecvMail', 'SMS', 'SendMail', 'Spam', 'Virus');
  21. function Log($args=null)
  22. {
  23. if (is_array($args))
  24. {
  25. foreach ($args as $k => $v)
  26. $this->$k = $v;
  27. }
  28. global $atmail, $pref;
  29. // can't establish db instance
  30. //if (is_a($atmail->db, 'SQL'))
  31. // $this->db =& $atmail->db;
  32. //else
  33. //{
  34. //require_once('libs/Atmail/SQL.php');
  35. $this->db = mysql_connect($pref['sql_host'], $pref['sql_user'], $pref['sql_pass'], $pref['sql_table']);
  36. mysql_select_db($pref['sql_table'], $this->db);
  37. //}
  38. }
  39. function write_log($type, $msg=null)
  40. {
  41. if ($this->is_log_type($type))
  42. $this->errtype = $type;
  43. else
  44. return -1;
  45. if (!empty($msg))
  46. {
  47. $this->msg = $msg;
  48. $msg = mysql_real_escape_string($msg);
  49. }
  50. if ($this->Account)
  51. {
  52. $account = mysql_real_escape_string($this->Account);
  53. }
  54. else
  55. $account = 'System';
  56. //$query = "INSERT INTO Log_{$this->errtype} (Account, LogMsg, LogDate) VALUES (?, ?, NOW())";
  57. //$data = array($account, $this->msg);
  58. //$this->db->sqldo($query, $data);
  59. $query = sprintf("INSERT INTO Log_{$this->errtype} (Account, LogMsg, LogDate) VALUES ('%s', '%s', NOW())", $account, $msg);
  60. mysql_ping($this->db);
  61. $res = mysql_query($query, $this->db);
  62. }
  63. // Raw log into the database for the log-daemon, use mysql_ping() if insert fails
  64. function log_raw($type, $msg=null)
  65. {
  66. return $this->write_log($type, $msg);
  67. if ($type)
  68. $this->errtype = $type;
  69. else
  70. return -1;
  71. if (!empty($msg))
  72. {
  73. $this->msg = $msg;
  74. $msg = mysql_real_escape_string($msg);
  75. }
  76. if ($this->Account)
  77. {
  78. $account = mysql_real_escape_string($this->Account);
  79. }
  80. else
  81. $account = 'System';
  82. //$query = "INSERT INTO Log_{$this->errtype} (Account, LogMsg, LogDate) VALUES (?, ?, NOW())";
  83. //$data = array($account, $this->msg);
  84. //$this->db->sqldo($query, $data);
  85. $query = sprintf("INSERT INTO Log_{$this->errtype} (Account, LogMsg, LogDate) VALUES ('%s', '%s', NOW())", $account, $msg);
  86. mysql_ping($this->db);
  87. $res = mysql_query($query, $this->db);
  88. }
  89. function logcheck($type, $ip=null)
  90. {
  91. $time = date('Ymd');
  92. if (!$this->is_log_type($type))
  93. return -1;
  94. if (isset($ip))
  95. {
  96. $ip = '%' . $ip . '%';
  97. //if ($ip && $this->account && !preg_match('/New Account/i', $ip) ) {
  98. $query = sprintf("select count(id) from Log_$type where LogMsg like '%s' and LogDate > DATE_SUB(NOW(), INTERVAL 24 HOUR) and Account='%s'", $ip, $this->Account);
  99. $res = mysql_query($query);
  100. if ($res === false)
  101. return -1;
  102. return mysql_result($res, 0);
  103. // } else {
  104. // return $this->db->getvalue( "select count(id) from Log_$type where LogMsg like ? and LogDate > DATE_SUB(NOW(), INTERVAL 24 HOUR)", array($ip) );
  105. // }
  106. }
  107. // $query = "select id from Log_$type where LogMsg like '%$ip%' and LogDate like '$time?%' ";
  108. // return $this->db->getvalue($query);
  109. // }
  110. return -2;
  111. }
  112. function logError($message, $file, $line, $type)
  113. {
  114. global $pref;
  115. if (!$pref['error_log'])
  116. return;
  117. $date = date('D M d H:i:s Y');
  118. if ($fh = fopen($pref['error_log'], 'a'))
  119. {
  120. fwrite($fh, "[$date] $type: $message in $file on line $line\n");
  121. fclose($fh);
  122. }
  123. }
  124. function addrelay($ip)
  125. {
  126. global $pref;
  127. if (!preg_match('/\d+\.\d+\.\d+\.\d+/', $ip))
  128. return 1;
  129. // Delete any entries older than 1 hour from the "user" table
  130. $query = sprintf("delete from MailRelay where Account='User' and DateAdded < DATE_SUB(NOW(), INTERVAL %d MINUTE)", $pref['smtp_popimaprelay_timeout']);
  131. mysql_query($query);
  132. // Add the new IP into the database, only if we do not exist
  133. $ip = mysql_real_escape($ip);
  134. $query = sprintf("select IPaddress from MailRelay where IPaddress='%s'", $ip);
  135. $res = mysql_query($query);
  136. if ($res === false)
  137. return -1;
  138. if (mysql_result($res, 0)) {
  139. $query = sprintf("INSERT INTO MailRelay (IPaddress, Account, DateAdded) VALUES('%s', 'User', NOW() )", $ip);
  140. $res = mysql_query($query);
  141. } else {
  142. // Update the DateAdded to now, so user always in sync to relay
  143. $query = sprintf("UPDATE MailRelay set DateAdded=NOW() where IPaddress='%s' and Account='User'", $ip);
  144. $res2 = mysql_query($query);
  145. }
  146. return;
  147. }
  148. function updatelastlogin($user)
  149. {
  150. // Return if no user supplied or account invalid
  151. if (!strpos($user, '@'))
  152. return -1;
  153. $time = time();
  154. // Update the users last login date
  155. $query = sprintf("update UserSession set LastLogin='%d' where Account='%s'", $time, $user);
  156. mysql_query($query);
  157. return 1;
  158. }
  159. /**
  160. * Check for valid Log table name
  161. *
  162. * @param string $name
  163. * @return bool True if valid, false if not
  164. */
  165. function is_log_type($name)
  166. {
  167. return in_array($name, $this->tables);
  168. }
  169. }//end Log
  170. ?>