[ Index ]
 

Code source de e107 0.7.8

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/e107_plugins/log/ -> consolidate.php (source)

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


Généré le : Sun Apr 1 01:23:32 2007 par Balluche grâce à PHPXref 0.7