PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/kloxo/httpdocs/lib/domain/mmail/mailtrafficlib.php

https://bitbucket.org/Nemcio/kloxo-mr
PHP | 183 lines | 137 code | 37 blank | 9 comment | 13 complexity | cfdc12730c56f7c68f2ae36d5de287f4 MD5 | raw file
  1. <?php
  2. class mailtraffic extends lxclass {
  3. function get() {}
  4. function write() {}
  5. static function generateGraph($oldtime, $newtime)
  6. {
  7. $convertedfile = self::convertfile($oldtime, $newtime);
  8. if (!$convertedfile) {
  9. return;
  10. }
  11. $list = lscandir("__path_mail_root/domains");
  12. foreach($list as $l) {
  13. if (csb($l, "lists.")) {
  14. continue;
  15. }
  16. $total = self::getmail_usage($convertedfile, $l, $oldtime, $newtime);
  17. execRrdSingle("mailtraffic", "ABSOLUTE", $l, $total * 1024 * 1024);
  18. }
  19. lunlink($convertedfile);
  20. }
  21. static function convertfile($oldtime, $newtime)
  22. {
  23. global $gbl, $sgbl, $login, $ghtml;
  24. $file = '/var/log/kloxo/maillog';
  25. if (!lxfile_real($file)) { return null; }
  26. $fp = fopen($file, "r");
  27. $fsize = filesize($file);
  28. $ret = FindRightPosition($fp, $fsize, $oldtime, $newtime, array("mailtraffic", "getTimeFromOriginalQmailString"));
  29. if ($ret < 0) {
  30. dprint("Could not find position\n");
  31. return;
  32. }
  33. $totstring = null;
  34. $count = 0;
  35. while(!feof($fp)) {
  36. $count++;
  37. if ($count > 1000000) {
  38. break;
  39. }
  40. $string = fgets($fp);
  41. $totstring .= $string;
  42. if (self::getTimeFromOriginalQmailString($string) > $newtime) {
  43. break;
  44. }
  45. }
  46. fclose($fp);
  47. $convertfile = tempnam("/tmp/", "mail_log");
  48. $convertfile_source = tempnam("/tmp/", "mail_log_source");
  49. lfile_put_contents($convertfile_source, $totstring);
  50. system("perl {$sgbl->__path_kloxo_httpd_root}/awstats/tools/maillogconvert.pl standard < $convertfile_source > $convertfile");
  51. lunlink($convertfile_source);
  52. return $convertfile;
  53. }
  54. static function findTotalmailQuota($driver, $list, $oldtime, $newtime)
  55. {
  56. global $gbl, $sgbl, $login, $ghtml;
  57. if(!isset($oldtime)) { return null; }
  58. $file = '/var/log/kloxo/maillog';
  59. $processedir = "/var/log/kloxo";
  60. $convertedfile = self::convertfile($oldtime, $newtime);
  61. if (!$convertedfile) {
  62. self::rotateLog($processedir, $file);
  63. return;
  64. }
  65. foreach($list as $d) {
  66. $tlist[$d] = self::getmail_usage($convertedfile, $d, $oldtime, $newtime);
  67. }
  68. lunlink($convertedfile);
  69. self::rotateLog($processedir, $file);
  70. self::rotateLog("/var/log/kloxo/", "/var/log/kloxo/courier");
  71. self::rotateLog("/var/log/kloxo/", "/var/log/kloxo/smtp.log");
  72. return $tlist;
  73. }
  74. static function rotateLog($processedir, $file)
  75. {
  76. $stat = stat($file);
  77. if ($stat['size'] >= 10 * 1024 * 1024) {
  78. lxfile_mv($file, getNotexistingFile($processedir, basename($file)));
  79. createRestartFile("syslog");
  80. }
  81. $list = lscandir_without_dot($processedir);
  82. foreach($list as $k) {
  83. $file = "$processedir/$k";
  84. $stat = stat($file);
  85. if ($stat['mtime'] < time() - 10 * 24 * 3600) {
  86. dprint("deleting old log $file\n");
  87. lxfile_rm($file);
  88. }
  89. }
  90. }
  91. static function getmail_usage($file, $domainname, $oldtime ,$newtime )
  92. {
  93. global $gbl, $sgbl, $login, $ghtml;
  94. $total = self::getEachmailfileqouta($file, $domainname , $oldtime , $newtime);
  95. return $total;
  96. }
  97. static function qmailLogConvertString($line)
  98. {
  99. list($month , $date, $from, $to, $domain, $minus1, $prot, $minu2, $status, $size) = explode(" ", $line);
  100. return $size;
  101. }
  102. static function getTimeFromOriginalQmailString($line)
  103. {
  104. ///2006-03-10 07:00:01
  105. $line = trimSpaces($line);
  106. $year = @ date('y');
  107. list($month, $day, $time) = explode(" ", $line);
  108. $month = get_num_for_month($month);
  109. list($hour , $min , $sec ) = explode(':' , $time);
  110. //$s = mktime($hour , $min , $sec , monthToInt($month), str_pad($day , 2, 0, STR_PAD_LEFT) , $year);
  111. $s = @ mktime($hour, $min, $sec, $month, $day, $year);
  112. //dprint(" $date $time $hour, $min $sec $month, $day , $year, Time: $s\n");
  113. // Return date and size. The size param is not important. Our aim is to find the right position.
  114. return $s;
  115. }
  116. static function getTimeFromString($line)
  117. {
  118. ///2006-03-10 07:00:01
  119. list($date, $time, ) = explode(" ", $line);
  120. list($year, $month, $day) = explode("-", $date);
  121. list($hour , $min , $sec ) = explode(':' , $time);
  122. //$s = mktime($hour , $min , $sec , monthToInt($month), str_pad($day , 2, 0, STR_PAD_LEFT) , $year);
  123. $s = mktime($hour, $min, $sec, $month, $day, $year);
  124. //dprint(" $date $time $hour, $min $sec $month, $day , $year, Time: $s\n");
  125. // Return date and size. The size param is not important. Our aim is to find the right position.
  126. return $s;
  127. }
  128. static function getEachmailfileqouta($file, $domainname, $oldtime, $newtime)
  129. {
  130. dprint("Opening File name is :$file\n");
  131. //error_reporting(0);
  132. $fp = @ fopen($file, "r");
  133. if (!$fp) { return 0; }
  134. while(!feof($fp)) {
  135. $string = fgets($fp);
  136. if (csa($string, $domainname)) {
  137. $total += self::qmailLogConvertString($string);
  138. }
  139. }
  140. fclose($fp);
  141. $total = $total / (1024 * 1024);
  142. $total = round($total, 1);
  143. dprint("Returning Total From OUT SIDE This File: for $domainname $total \n");
  144. return $total;
  145. }
  146. }