PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/e107_plugins/log/consolidate.php

https://github.com/CasperGemini/e107
PHP | 401 lines | 298 code | 67 blank | 36 comment | 36 complexity | 22e29649f8a8b361ca21cbb46e8a03df MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. * e107 website system
  4. *
  5. * Copyright (C) 2008-2009 e107 Inc (e107.org)
  6. * Released under the terms and conditions of the
  7. * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
  8. *
  9. *
  10. *
  11. * $Source: /cvs_backup/e107_0.8/e107_plugins/log/consolidate.php,v $
  12. * $Revision$
  13. * $Date$
  14. * $Author$
  15. */
  16. /* first thing to do is check if the log file is out of date ... */
  17. // $pathtologs = e_PLUGIN."log/logs/";
  18. if (!defined('e107_INIT')){ exit; }
  19. $pathtologs = e_LOG;
  20. $date = date("z.Y", time());
  21. $yesterday = date("z.Y",(time() - 86400)); // This makes sure year wraps round OK
  22. $date2 = date("Y-m-j", (time() -86400)); // Yesterday's date for the database summary
  23. $date3 = date("Y-m", (time() -86400)); // Current month's date for monthly summary (we're working with yesterday's data)
  24. $pfileprev = "logp_".$yesterday.".php"; // Yesterday's log file
  25. $pfile = "logp_".$date.".php"; // Today's log file
  26. $ifileprev = "logi_".$yesterday.".php";
  27. $ifile = "logi_".$date.".php";
  28. if(file_exists($pathtologs.$pfile)) /* log file is up to date, no consolidation required */
  29. {
  30. return;
  31. }
  32. else if(!file_exists($pathtologs.$pfileprev)) // See if any older log files
  33. {
  34. if (($retvalue = check_for_old_files($pathtologs)) === FALSE) /* no logfile found at all - create - this will only ever happen once ... */
  35. {
  36. createLog($pathtologs);
  37. return FALSE;
  38. }
  39. list($pfileprev,$ifileprev,$date2,$tstamp) = explode('|',$retvalue); // ... if we've got files
  40. }
  41. // List of the non-page-based info which is gathered - historically only 'all-time' stats, now we support monthly as well
  42. $stats_list = array('statBrowser','statOs','statScreen','statDomain','statReferer','statQuery');
  43. $qry = "`log_id` IN ('statTotal','statUnique'";
  44. foreach ($stats_list as $s)
  45. {
  46. $qry .= ",'{$s}'"; // Always read the all-time stats
  47. if ($pref[$s] == 2) $qry .= ",'{$s}:{$date3}'"; // Look for monthlys as well as cumulative
  48. }
  49. $qry .= ")";
  50. /* log file is out of date - consolidation required */
  51. /* get existing stats ... */
  52. //if($sql->select("logstats", "*", "log_id='statBrowser' OR log_id='statOs' OR log_id='statScreen' OR log_id='statDomain' OR log_id='statTotal' OR log_id='statUnique' OR log_id='statReferer' OR log_id='statQuery'"))
  53. if($sql->select("logstats", "*", $qry))
  54. { // That's read in all the stats we need to modify
  55. while($row = $sql->fetch())
  56. {
  57. if($row['log_id'] == "statUnique")
  58. {
  59. $statUnique = $row['log_data'];
  60. }
  61. elseif ($row['log_id'] == "statTotal")
  62. {
  63. $statTotal = $row['log_data'];
  64. }
  65. elseif (($pos = strpos($row['log_id'],':')) === FALSE)
  66. { // Its all-time stats
  67. $$row['log_id'] = unserialize($row['log_data']); // $row['log_id'] is the stats type - save in a variable
  68. }
  69. else
  70. { // Its monthly stats
  71. $row['log_id'] = 'mon_'.substr($row['log_id'],0,$pos); // Create a generic variable for each monthly stats
  72. $$row['log_id'] = unserialize($row['log_data']); // $row['log_id'] is the stats type - save in a variable
  73. }
  74. }
  75. }
  76. else
  77. {
  78. // this must be the first time a consolidation has happened - this will only ever happen once ...
  79. $sql->insert("logstats", "0, 'statBrowser', ''");
  80. $sql->insert("logstats", "0, 'statOs', ''");
  81. $sql->insert("logstats", "0, 'statScreen', ''");
  82. $sql->insert("logstats", "0, 'statDomain', ''");
  83. $sql->insert("logstats", "0, 'statReferer', ''");
  84. $sql->insert("logstats", "0, 'statQuery', ''");
  85. $sql->insert("logstats", "0, 'statTotal', '0'");
  86. $sql->insert("logstats", "0, 'statUnique', '0'");
  87. $statBrowser =array();
  88. $statOs =array();
  89. $statScreen =array();
  90. $statDomain =array();
  91. $statReferer =array();
  92. $statQuery =array();
  93. }
  94. foreach ($stats_list as $s)
  95. {
  96. $varname = 'mon_'.$s;
  97. if (!isset($$varname)) $$varname = array(); // Create monthly arrays if they don't exist
  98. }
  99. require_once($pathtologs.$pfileprev); // Yesterday's page accesses - $pageInfo array
  100. require_once($pathtologs.$ifileprev); // Yesterdays browser accesses etc
  101. foreach($browserInfo as $name => $amount)
  102. {
  103. $statBrowser[$name] += $amount;
  104. $mon_statBrowser[$name] += $amount;
  105. }
  106. foreach($osInfo as $name => $amount)
  107. {
  108. $statOs[$name] += $amount;
  109. $mon_statOs[$name] += $amount;
  110. }
  111. foreach($screenInfo as $name => $amount)
  112. {
  113. $statScreen[$name] += $amount;
  114. $mon_statScreen[$name] += $amount;
  115. }
  116. foreach($domainInfo as $name => $amount)
  117. {
  118. if(!is_numeric($name))
  119. {
  120. $statDomain[$name] += $amount;
  121. $mon_statDomain[$name] += $amount;
  122. }
  123. }
  124. foreach($refInfo as $name => $info)
  125. {
  126. $statReferer[$name]['url'] = $info['url'];
  127. $statReferer[$name]['ttl'] += $info['ttl'];
  128. $mon_statReferer[$name]['url'] = $info['url'];
  129. $mon_statReferer[$name]['ttl'] += $info['ttl'];
  130. }
  131. foreach($searchInfo as $name => $amount)
  132. {
  133. $statQuery[$name] += $amount;
  134. $mon_statQuery[$name] += $amount;
  135. }
  136. $browser = serialize($statBrowser);
  137. $os = serialize($statOs);
  138. $screen = serialize($statScreen);
  139. $domain = serialize($statDomain);
  140. $refer = serialize($statReferer);
  141. $squery = serialize($statQuery);
  142. $statTotal += $siteTotal;
  143. $statUnique += $siteUnique;
  144. // Save cumulative results - always keep track of these, even if the $pref doesn't display them
  145. $sql->update("logstats", "log_data='{$browser}' WHERE log_id='statBrowser'");
  146. $sql->update("logstats", "log_data='{$os}' WHERE log_id='statOs'");
  147. $sql->update("logstats", "log_data='{$screen}' WHERE log_id='statScreen'");
  148. $sql->update("logstats", "log_data='{$domain}' WHERE log_id='statDomain'");
  149. $sql->update("logstats", "log_data='{$refer}' WHERE log_id='statReferer'");
  150. $sql->update("logstats", "log_data='{$squery}' WHERE log_id='statQuery'");
  151. $sql->update("logstats", "log_data='".intval($statTotal)."' WHERE log_id='statTotal'");
  152. $sql->update("logstats", "log_data='".intval($statUnique)."' WHERE log_id='statUnique'");
  153. // Now save the relevant monthly results - only where enabled
  154. foreach ($stats_list as $s)
  155. {
  156. if (isset($pref[$s]) && ($pref[$s] > 1))
  157. { // Value 2 requires saving of monthly stats
  158. $srcvar = 'mon_'.$s;
  159. $destvar = 'smon_'.$s;
  160. $$destvar = serialize($$srcvar);
  161. if (!$sql->update("logstats", "log_data='".$$destvar."' WHERE log_id='".$s.":".$date3."'"))
  162. {
  163. $sql->insert("logstats", "0, '".$s.":".$date3."', '".$$destvar."'");
  164. }
  165. }
  166. }
  167. /* get page access monthly info from db */
  168. if($sql->select("logstats", "*", "log_id='{$date3}' "))
  169. {
  170. $tmp = $sql->fetch();
  171. $monthlyInfo = unserialize($tmp['log_data']);
  172. unset($tmp);
  173. $MonthlyExistsFlag = TRUE;
  174. }
  175. foreach($pageInfo as $key => $info)
  176. {
  177. $monthlyInfo['TOTAL']['ttlv'] += $info['ttl'];
  178. $monthlyInfo['TOTAL']['unqv'] += $info['unq'];
  179. $monthlyInfo[$key]['ttlv'] += $info['ttl'];
  180. $monthlyInfo[$key]['unqv'] += $info['unq'];
  181. }
  182. $monthlyinfo = serialize($monthlyInfo);
  183. if($MonthlyExistsFlag)
  184. {
  185. $sql->update("logstats", "log_data='{$monthlyinfo}' WHERE log_id='{$date3}'");
  186. }
  187. else
  188. {
  189. $sql->insert("logstats", "0, '{$date3}', '{$monthlyinfo}'");
  190. }
  191. /* collate page total information */
  192. if($sql->select("logstats", "*", "log_id='pageTotal' "))
  193. {
  194. $tmp = $sql->fetch();
  195. $pageTotal = unserialize($tmp['log_data']);
  196. unset($tmp);
  197. }
  198. else
  199. {
  200. $pageTotal = array();
  201. }
  202. foreach($pageInfo as $key => $info)
  203. {
  204. $pageTotal[$key]['url'] = $info['url'];
  205. $pageTotal[$key]['ttlv'] += $info['ttl'];
  206. $pageTotal[$key]['unqv'] += $info['unq'];
  207. }
  208. $pagetotal = serialize($pageTotal);
  209. if(!$sql->update("logstats", "log_data='{$pagetotal}' WHERE log_id='pageTotal' "))
  210. {
  211. $sql->insert("logstats", "0, 'pageTotal', '{$pagetotal}' ");
  212. }
  213. /* now we need to collate the individual page information into an array ... */
  214. $data = "";
  215. $dailytotal = 0;
  216. $uniquetotal = 0;
  217. foreach($pageInfo as $key => $value)
  218. {
  219. $data .= $value['url']."|".$value['ttl']."|".$value['unq'].chr(1);
  220. $dailytotal += $value['ttl'];
  221. $uniquetotal += $value['unq'];
  222. }
  223. $data = $dailytotal.chr(1).$uniquetotal.chr(1) . $data;
  224. $sql->insert("logstats", "0, '$date2', '".$tp -> toDB($data, true)."'");
  225. /* ok, we're finished with the log file now, we can empty it ... */
  226. if(!unlink($pathtologs.$pfileprev))
  227. {
  228. $data = chr(60)."?php\n". chr(47)."* e107 website system: Log file: ".date("z:Y", time())." *". chr(47)."\n\n\n\n".chr(47)."* THE INFORMATION IN THIS LOG FILE HAS BEEN CONSOLIDATED INTO THE DATABASE - YOU CAN SAFELY DELETE IT. *". chr(47)."\n\n\n?". chr(62);
  229. if ($handle = fopen($pathtologs.$pfileprev, 'w')) {
  230. fwrite($handle, $data);
  231. }
  232. fclose($handle);
  233. }
  234. if(!unlink($pathtologs.$ifileprev))
  235. {
  236. $data = chr(60)."?php\n". chr(47)."* e107 website system: Log file: ".date("z:Y", time())." *". chr(47)."\n\n\n\n".chr(47)."* THE INFORMATION IN THIS LOG INFO FILE HAS BEEN CONSOLIDATED INTO THE DATABASE - YOU CAN SAFELY DELETE IT. *". chr(47)."\n\n\n?". chr(62);
  237. if ($handle = fopen($pathtologs.$ifileprev, 'w')) {
  238. fwrite($handle, $data);
  239. }
  240. fclose($handle);
  241. }
  242. /* and finally, we need to create new logfiles for today ... */
  243. createLog($pathtologs);
  244. /* done! */
  245. function createLog($pathtologs)
  246. {
  247. global $statTotal, $statUnique, $pfile, $ifile;
  248. if(!is_writable($pathtologs))
  249. {
  250. echo "Log directory is not writable - please CHMOD ".e_LOG." to 777";
  251. echo '<br />Path to logs: '.$pathtologs;
  252. return FALSE;
  253. }
  254. $varStart = chr(36);
  255. $quote = chr(34);
  256. $data = chr(60)."?php\n". chr(47)."* e107 website system: Log file: ".date("z:Y", time())." *". chr(47)."\n\n".
  257. $varStart."refererData = ".$quote.$quote.";\n".
  258. $varStart."ipAddresses = ".$quote.$quote.";\n".
  259. $varStart."hosts = ".$quote.$quote.";\n".
  260. $varStart."siteTotal = ".$quote."0".$quote.";\n".
  261. $varStart."siteUnique = ".$quote."0".$quote.";\n".
  262. $varStart."screenInfo = array();\n".
  263. $varStart."browserInfo = array();\n".
  264. $varStart."osInfo = array();\n".
  265. $varStart."pageInfo = array(\n";
  266. $data .= "\n);\n\n?". chr(62);
  267. if(!touch($pathtologs.$pfile)) {
  268. return FALSE;
  269. }
  270. if(!touch($pathtologs.$ifile)) {
  271. return FALSE;
  272. }
  273. if(!is_writable($pathtologs.$pfile)) {
  274. $old = umask(0);
  275. chmod($pathtologs.$pfile, 0777);
  276. umask($old);
  277. // return FALSE;
  278. }
  279. if(!is_writable($pathtologs.$ifile)) {
  280. $old = umask(0);
  281. chmod($pathtologs.$ifile, 0777);
  282. umask($old);
  283. // return FALSE;
  284. }
  285. if ($handle = fopen($pathtologs.$pfile, 'w'))
  286. {
  287. fwrite($handle, $data);
  288. }
  289. fclose($handle);
  290. $data = "<?php
  291. /* e107 website system: Log info file: ".date("z:Y", time())." */
  292. ";
  293. $data .= '$domainInfo'." = array();\n\n";
  294. $data .= '$screenInfo'." = array();\n\n";
  295. $data .= '$browserInfo'." = array();\n\n";
  296. $data .= '$osInfo'." = array();\n\n";
  297. $data .= '$refInfo'." = array();\n\n";
  298. $data .= '$searchInfo'." = array();\n\n";
  299. $data .= '$visitInfo'." = array();\n\n";
  300. $data .= "?>";
  301. if ($handle = fopen($pathtologs.$ifile, 'w'))
  302. {
  303. fwrite($handle, $data);
  304. }
  305. fclose($handle);
  306. return;
  307. }
  308. // Called if both today's and yesterday's log files missing, to see
  309. // if there are any older files we could process. Return FALSE if nothing
  310. // Otherwise return a string of relevant information
  311. function check_for_old_files($pathtologs)
  312. {
  313. $no_files = TRUE;
  314. if ($dir_handle = opendir($pathtologs))
  315. {
  316. while (false !== ($file = readdir($dir_handle)))
  317. {
  318. // Do match on #^logp_(\d{1,3})\.php$#i
  319. if (preg_match('#^logp_(\d{1,3}\.\d{4})\.php$#i',$file,$match) == 1)
  320. { // got a matching file
  321. $yesterday = $match[1]; // Day of year - zero is 1st Jan
  322. $pfileprev = "logp_".$yesterday.".php"; // Yesterday's log file
  323. $ifileprev = "logi_".$yesterday.".php";
  324. list($day,$year) = explode('.',$yesterday);
  325. $tstamp = mktime(0,0,0,1,1,$year) + ($day*86400);
  326. $date2 = date("Y-m-j", $tstamp); // Yesterday's date for the database summary
  327. $temp = array($pfileprev,$ifileprev,$date2,$tstamp);
  328. return implode('|',$temp);
  329. }
  330. }
  331. }
  332. return FALSE;
  333. }
  334. ?>