PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/kloxo/bin/gettraffic.php

https://bitbucket.org/Nemcio/kloxo-mr
PHP | 245 lines | 197 code | 37 blank | 11 comment | 21 complexity | 32ad3e63c34725cc00f200dbdbcbc553 MD5 | raw file
  1. <?php
  2. include_once "htmllib/lib/displayinclude.php";
  3. print_time("gettraffic");
  4. gettraffic_main();
  5. $val = print_time("gettraffic", "Get Traffic ");
  6. log_log("get_traffic", $val);
  7. function gettraffic_main()
  8. {
  9. global $argc, $argv;
  10. $list = parse_opt($argv);
  11. if (isset($list['delete-table']) && $list['delete-table'] === 'yes') {
  12. print("clearing Traffic Table\n");
  13. clearTrafficTable();
  14. filltraffictable();
  15. } else {
  16. filltraffictable();
  17. }
  18. }
  19. //testFunc();
  20. function clearTrafficTable()
  21. {
  22. $sql = new Sqlite(null, "domaintraffic");
  23. $sql->rawquery("delete from domaintraffic;");
  24. }
  25. function filltraffictable()
  26. {
  27. global $gbl, $login, $ghtml;
  28. initProgram('admin');
  29. $login->loadAllObjects('client');
  30. $clist = $login->getList('client');
  31. $t="";
  32. // Fake domain to store the time the last stats finding was done.
  33. $laccessdom = new Domain(null, null, '__last_access_domain_');
  34. try {
  35. $laccess = $laccessdom->getFromList('domaintraffic', '__last_access_domain_');
  36. } catch (exception $e) {
  37. dprint("not getting\n");
  38. $laccess = null;
  39. }
  40. if (!$laccess) {
  41. $laccess = new Domaintraffic(null, null, '__last_access_domain_');
  42. $oldtime = 0;
  43. $laccess->parent_clname = 'domain-__last_access_domain_';
  44. $laccess->dbaction = 'add';
  45. } else {
  46. $oldtime = $laccess->timestamp;
  47. }
  48. if ($oldtime && ((time() - $oldtime) > 12 * 3600 * 24) ) {
  49. $oldtime = time() - 12 * 3600 * 24;
  50. $laccess->timestamp = $oldtime;
  51. $laccess->setUpdateSubaction();
  52. $laccess->write();
  53. }
  54. foreach($clist as $c) {
  55. $domlist = $c->getList('domain');
  56. foreach((array)$domlist as $domain) {
  57. if (!$domain->isDomainVirtual()) {
  58. continue;
  59. }
  60. $web = $domain->getObject('web');
  61. $mmail= $domain->getObject('mmail');
  62. $globaldomlist[$domain->nname] = $domain;
  63. $weblist[$web->syncserver][] = $domain;
  64. $mmaillist[$mmail->syncserver][] = $mmail;
  65. }
  66. }
  67. $flag = 0;
  68. if($oldtime == 0) {
  69. // 8 days back
  70. $oldtime = @ mktime(00, 01, 00, date("n"), date("j") - 10, date("Y"));
  71. // Start of Jan
  72. //$oldtime = mktime( 00 , 01, 00 , 1 ,1, date("Y"));
  73. $flag = 1;
  74. }
  75. // $newtime = mktime( 00 , 01, 00 , date("n") , date("j") ,date("Y"));
  76. $newtime = time();
  77. $old = $oldtime;
  78. $new = $newtime;
  79. if(($newtime - $oldtime) >= (19 * 60 * 60)) {
  80. if($flag == 1) {
  81. $old = @ mktime(00, 01, 00, @ date("n"), @ date("j"), @ date("Y"));
  82. $timearray[0] = $newtime . "-" . $old;
  83. $newtime = $old;
  84. }
  85. $j=0;
  86. for($i = $newtime; $i >= $oldtime ; $i-= (24 * 60 * 60)) {
  87. if($j > 0) {
  88. $timearray[] = $new . "-" . $i ;
  89. }
  90. $new = $i;
  91. $j++;
  92. }
  93. if($flag != 1) {
  94. $timearray[] = "$new-$oldtime";
  95. }
  96. $timearray = array_reverse($timearray);
  97. foreach($timearray as $t1) {
  98. $t = explode("-", $t1);
  99. $newtime = $t[0];
  100. $oldtime = $t[1];
  101. if ($newtime - $oldtime < 4 * 60 * 60) { continue; }
  102. $o = @ strftime("%c", $oldtime);
  103. $n = @ strftime("%c", $newtime);
  104. print("\n\n$o to ... $n\n\n");
  105. findtraffic($weblist, $mmaillist, $globaldomlist, $oldtime, $newtime);
  106. $laccess->timestamp = $newtime;
  107. $laccess->setUpdateSubaction();
  108. $laccess->write();
  109. }
  110. // This is the time at which this was run last time.
  111. $laccess->timestamp = time();
  112. $laccess->setUpdateSubaction();
  113. $laccess->write();
  114. } else {
  115. dprint("Less than a day:");
  116. dprint("\n\n\n\n");
  117. }
  118. }
  119. function get_mailaccountlist($m)
  120. {
  121. $list = array();
  122. foreach($m as $v) {
  123. $mac = $v->getList('mailaccount');
  124. if ($mac) {
  125. $list = array_merge($list, get_namelist_from_objectlist($mac));
  126. }
  127. }
  128. return $list;
  129. }
  130. function findtraffic($weblist, $mmaillist, $globaldomlist, $oldtime, $newtime)
  131. {
  132. global $gbl, $login, $ghtml;
  133. $web_usage = null;
  134. $gen = $login->getObject('general')->generalmisc_b;
  135. $webstatsprog = $gen->webstatisticsprogram;
  136. if (!$webstatsprog) { $webstatsprog = "awstats"; }
  137. foreach($weblist as $k => $dlist) {
  138. $list = null;
  139. foreach($dlist as $d) {
  140. $rt = new Remote();
  141. $rt->iisid = $d->getObject('web')->iisid;
  142. $rt->nname = $d->nname;
  143. $rt->customer_name = $d->getRealClientParentO()->getPathFromName('nname');
  144. $rt->priv = new Priv(null, null, $d->nname);
  145. $rt->priv->awstats_flag = $d->priv->awstats_flag;
  146. $rt->stats_username = $d->getObject('web')->stats_username;
  147. $rt->stats_password = $d->getObject('web')->stats_password;
  148. $rt->remove_processed_stats = $d->getObject('web')->remove_processed_stats;
  149. $list[$d->nname] = $rt;
  150. }
  151. $driverapp = $gbl->getSyncClass(null, $k, 'web');
  152. $web_usaget = rl_exec_get(null, $k, array("webtraffic", 'findTotaltrafficwebUsage'), array($driverapp, $webstatsprog, $list, $oldtime, $newtime));
  153. $web_usage = lx_array_merge(array($web_usage , $web_usaget));
  154. }
  155. foreach($mmaillist as $k => $m) {
  156. $mlist = get_namelist_from_objectlist($m);
  157. $driverapp = $gbl->getSyncClass(null, $k, 'mmail');
  158. $mailusaget = rl_exec_get(null, $k, array("mailtraffic", 'findTotalmailQuota'), array($driverapp, $mlist, $oldtime, $newtime));
  159. $mailusage = lx_array_merge(array($mailusaget, $mailusage));
  160. }
  161. try {
  162. foreach($weblist as $k => $w) {
  163. $wlist = get_namelist_from_objectlist($w);
  164. $driverapp = $gbl->getSyncClass(null, $k, 'ftpuser');
  165. $ftpusaget = rl_exec_get(null, $k, array("ftpusertraffic__$driverapp", 'findTotalQuota'), array($wlist, $oldtime, $newtime));
  166. $ftpusage = lx_array_merge(array($ftpusaget, $ftpusage));
  167. }
  168. } catch (exception $e) {
  169. print($e->getMessage(). "\n");
  170. }
  171. //dprintr($web_usage);
  172. //dprintr($mailusage);
  173. //dprintr($ftpusage);
  174. $res="";
  175. foreach($globaldomlist as $d) {
  176. $res['nname'] = "$d->nname:$oldtime:$newtime";
  177. $domt = new Domaintraffic(null, null, $res['nname']);
  178. $res['ddate'] = time();
  179. $res['timestamp'] = strftime("%c", $newtime);
  180. $res['oldtimestamp'] = strftime("%c", $oldtime);
  181. $res['comment'] = null;
  182. $res['parent_list'] = null;
  183. $res['parent_clname'] = $d->getClName();
  184. $res['webtraffic_usage'] = $web_usage[$d->nname];
  185. $res['mailtraffic_usage'] = $mailusage[$d->nname];
  186. $res['ftptraffic_usage'] = $ftpusage[$d->nname];
  187. $res['traffic_usage'] = $res['ftptraffic_usage'] + $res['webtraffic_usage'] + $res['mailtraffic_usage'];
  188. // print_r($res);
  189. $domt->create($res);
  190. $domt->was();
  191. }
  192. }
  193. function testFunc()
  194. {
  195. $inittime = time();
  196. $hour = 0;
  197. $min = 0;
  198. $second = 0;
  199. $month = 'Jun';
  200. $date = 21;
  201. $year = 2005;
  202. $s1 = mktime($hour, $min, $second, monthToInt($month), $date, $year);
  203. $date = 22;
  204. $s2 = mktime($hour, $min, $second, monthToInt($month), $date, $year);
  205. $res = getEachwebfilequota("/home/root/paid-my-traffic.com-custom_log", $s1, $s2);
  206. $total = time() - $inittime;
  207. dprint("Total Time $total seconds \n");
  208. }