From a9630e7f8e50c598ae263b7093f2fee6da5f7d02 Mon Sep 17 00:00:00 2001 From: e107steved Date: Sun, 4 Nov 2007 09:24:59 +0000 Subject: [PATCH] Stats - get rid of deprecated function; extra display options --- e107_plugins/log/stats.php | 89 +++++++-- e107_plugins/log/stats_csv.php | 343 +++++++++++++++++++++++++++++++++ 2 files changed, 418 insertions(+), 14 deletions(-) create mode 100644 e107_plugins/log/stats_csv.php diff --git a/e107_plugins/log/stats.php b/e107_plugins/log/stats.php index 50b0d21d0..0f32f0ed1 100644 --- a/e107_plugins/log/stats.php +++ b/e107_plugins/log/stats.php @@ -11,8 +11,8 @@ | GNU General Public License (http://gnu.org). | | $Source: /cvs_backup/e107_0.8/e107_plugins/log/stats.php,v $ -| $Revision: 1.5 $ -| $Date: 2007-11-01 20:28:21 $ +| $Revision: 1.6 $ +| $Date: 2007-11-04 09:24:59 $ | $Author: e107steved $ +----------------------------------------------------------------------------+ */ @@ -22,6 +22,14 @@ require_once("../../class2.php"); @include_once(e_PLUGIN."log/languages/English.php"); +$bar = (file_exists(THEME."images/bar.png") ? THEME."images/bar.png" : e_IMAGE."generic/bar.png"); +$eplug_css[] = ""; + +/* function core_head() { $bar = (file_exists(THEME."images/bar.png") ? THEME."images/bar.png" : e_IMAGE."generic/bar.png"); return ""; } +*/ require_once(HEADERF); @@ -65,6 +74,20 @@ if($stat -> error) exit; } + +$oses_map = array ( +"Windows" => "windows", +"Mac" => "mac", +"Linux" => "linux", +"BeOS" => "beos", +"FreeBSD" => "freebsd", +"NetBSD" => "netbsd", +"Unspecified"=> "unspecified", +"OpenBSD" => "openbsd", +"Unix" => "unix", +"Spiders" => "spiders", +); + $browser_map = array ( 'Netcaptor' => "netcaptor", 'Internet Explorer' => "explorer", @@ -439,8 +462,8 @@ switch($action) if (ADMIN == TRUE) $text .= $stat -> renderAlltimeVisits(TRUE); break; - case 3 : - case 14 : + case 3 : // 'Normal' render + case 14 : // 'Consolidated' render if($pref['statBrowser']) { $text .= $stat -> renderBrowsers(display_pars($pref['statBrowser']), $action==3); @@ -450,10 +473,11 @@ switch($action) $text .= ADSTAT_L7; } break; - case 4: + case 4: // 'Normal' render + case 15 : // 'Consolidated' render if($pref['statOs']) { - $text .= $stat -> renderOses(display_pars($pref['statOs'])); + $text .= $stat -> renderOses(display_pars($pref['statOs']), $action==4); } else { @@ -537,6 +561,7 @@ switch($action) 12 - Today's error page visits 13 - All-time error page visits 14 - Consolidated browser view (not listed as a menu option) +15 - Consolidated OS view (not listed as a menu option) */ $path = e_PLUGIN."log/stats.php"; @@ -831,7 +856,7 @@ class siteStats /* -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ // List browsers. $selection is an array of the info required - '2' = current month's stats, '1' = all-time stats (default) - // If $show_version is FALSE, browsers are consolidated across versions - e.g. 1 line for Firefox + // If $show_version is FALSE, browsers are consolidated across versions - e.g. 1 line for Firefox using info from $browser_map function renderBrowsers($selection, $show_version=TRUE) { global $sql, $browser_map; @@ -839,7 +864,7 @@ class siteStats if (!is_array($selection)) $selection = array(1); $text = ''; - echo "Show browsers; expanded = ".($show_version ? 'TRUE' : 'FALSE')."
"; +// echo "Show browsers; expanded = ".($show_version ? 'TRUE' : 'FALSE')."
"; foreach ($selection as $act) { unset($statBrowser); @@ -871,7 +896,7 @@ class siteStats $b_type = ''; foreach ($browser_map as $name => $file) { - if(stristr($b_full, $name) === 0) + if(stripos($b_full, $name) === 0) { // Match here $b_type = $name; break; @@ -879,7 +904,7 @@ class siteStats } if (!$b_type) $b_type = $b_full; // Default is an unsupported browser - use the whole name - if (array_key_exists($temp_array,$b_type)) + if (array_key_exists($b_type,$temp_array)) { $temp_array[$b_type] += $v; } @@ -949,15 +974,19 @@ class siteStats return $text; } - /* -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ - - function renderOses($selection) + /* ------------------------------------------------------- + Show operating systems. Only show different versions of the operating system if $show_version == TRUE + Uses $oses_map + -----------------------------------------------------------*/ + function renderOses($selection, $show_version=TRUE) { - global $sql; + global $sql, $oses_map; if (!$selection) $selection = array(1); if (!is_array($selection)) $selection = array(1); $text = ''; +// echo "Show OSes; expanded = ".($show_version ? 'TRUE' : 'FALSE')."
"; + $statOs = array(); foreach ($selection as $act) { @@ -979,6 +1008,38 @@ class siteStats } } + + if ($show_version == FALSE) + { + $temp_array = array(); + foreach ($statOs as $b_full=>$v) + { + $b_type = ''; + foreach ($oses_map as $name => $file) + { + if(stripos($b_full, $name) === 0) + { // Match here + $b_type = $name; + break; + } + } + if (!$b_type) $b_type = $b_full; // Default is an unsupported browser - use the whole name + + if (array_key_exists($b_type,$temp_array)) + { + $temp_array[$b_type] += $v; + } + else + { + $temp_array[$b_type] = $v; // New browser found + } + } + $statOs = $temp_array; + unset($temp_array); + } + + + if($this -> order) { ksort($statOs); diff --git a/e107_plugins/log/stats_csv.php b/e107_plugins/log/stats_csv.php new file mode 100644 index 000000000..4d6b9d79a --- /dev/null +++ b/e107_plugins/log/stats_csv.php @@ -0,0 +1,343 @@ +ADSTAT_L6,'statOs'=>ADSTAT_L7,'statScreen'=>ADSTAT_L8,'statDomain'=>ADSTAT_L9,'statReferer'=>ADSTAT_L10,'statQuery'=>ADSTAT_L11); + +$separator_char = array(1 => ',', 2 => '|'); +$quote_char = array(1 => '', 2 => "'", 3 => '"'); + + +//--------------------------------------------- +// Export data file +//--------------------------------------------- +$export_filter = ''; // can be 'LIKE', 'REGEX', or simple equality +$export_type = $tp->toDB(varset($_POST['export_type'],'page')); // Page data or one of the other bits of info +$export_date = intval(varset($_POST['export_date'],1)); +$export2_date = intval(varset($_POST['export2_date'],3)); +$export_year = intval(varset($_POST['export_year'],date('Y'))); +$export_month = intval(varset($_POST['export_month'],date('m'))); +$export_day = intval(varset($_POST['export_day'],date('j'))); +$export_char = varset($_POST['export_char'], 1); +$export_quote = varset($_POST['export_quote'], 1); +$export_stripurl = varset($_POST['export_stripurl'], 0); + +if (isset($_POST['create_export']) && $action == 'export') +{ + $first_date = 0; + $last_date = 0; + $date_error = FALSE; + if ($export_type == 'page') + { + switch ($export_date) + { + case '1' : // Single day + $first_date = gmmktime(0,0,0,$export_month,$export_day,$export_year); + $last_date = $first_date+86399; + $export_filter = " `log_id`='".date("Y-m-j",$first_date)."'"; + break; + case '2' : // Daily for a month + $first_date = gmmktime(0,0,0,$export_month,1,$export_year); + $last_date = gmmktime(0,0,0,$export_month+1,1,$export_year) - 1; + $export_filter = " LEFT(`log_id`,8)='".gmstrftime("%Y-%m-",$first_date)."'"; + break; + case '3' : // Monthly for a Year + $first_date = gmmktime(0,0,0,1,1,$export_year); + $last_date = gmmktime(0,0,0,1,1,$export_year+1) - 1; + $export_filter = " LENGTH(`log_id`)=7 AND LEFT(`log_id`,5)='".gmstrftime("%Y-",$first_date)."'"; + break; + case '4' : // Accumulated + case '5' : + $export_filter = "`log_id`='pageTotal'"; + $date_error = 'ignore'; + break; + } + } + else + { // Calculate strings for non-page sources + $prefix_len = 0; + $export_date = $export2_date; + if (isset($stats_list[$export_type])) + { + $prefix_len = strlen($export_type) + 1; + switch ($export2_date) + { + case '3' : // Monthly for a Year + if ($prefix_len > 0) + { + $first_date = gmmktime(0,0,0,1,1,$export_year); + $last_date = gmmktime(0,0,0,1,1,$export_year+1) - 1; + $export_filter = " LENGTH(`log_id`)='".($prefix_len + 7)."' AND LEFT(`log_id`,".($prefix_len + 5).")='".$export_type.":".gmstrftime("%Y-",$first_date)."'"; + } + break; + case '4' : // Accumulated + $export_filter = " `log_id`='".$export_type."'"; + $date_error = 'ignore'; + break; + } + } + else + { + $message = ADSTAT_L54; + } + } + if (($date_error != 'ignore') && (($first_date == 0) || ($last_date == 0) || $date_error)) + { + $message = ADSTAT_L47; + } + else + { // Actually do export + $message = export_stats($export_type, $export_date, $export_filter, $first_date, $last_date, $separator_char[$export_char], $quote_char[$export_quote], $export_stripurl); + } +} + + +if (isset($message) && $message) +{ + require_once(e_ADMIN."auth.php"); + $ns->tablerender("", "
".$message."
"); + require_once(e_ADMIN."footer.php"); +} + +//--------------------------------------------- +// Generate export statistics +//--------------------------------------------- +// All but page data returns a single level array +// $export_type - base type of stats - 'page' for page data, or one of the 'generic' types +// $export_date - date range/interval type - 1 = single day, 2 = daily for a month, 3 = monthly for a year, 4 = 'all-time' +// $export_filter - DB filter to return the required records +// $first_date, $last_date - date range +// $separator = ',', - character placed between entries +// $quote_char = character placed around entries +// $strip_url - if true, just outputs page names +// $target='' +function export_stats($export_type, $export_date, $export_filter, $first_date, $last_date, $separator = ',', $quote_char = '', $strip_url = 0, $target='') +{ + $stat_types = array( 1 => 'day', 2 => 'month', 3 => 'year', 4 => 'alltime', 5 => 'all_detail'); + global $sql, $e107; + $sql -> db_Select("logstats", "*", "{$export_filter} "); + + $export_text = ''; // Accumulate output string + $export_array = array(); + $process_mode = 0; + $values_per_entry = 1; + $values_per_row = 1; + $empty_data = 0; + + $replace_url = $e107->http_path; + if (strpos($e107->http_path,'www.')) + { + $replace_url = array($e107->http_path,str_replace('www.','',$e107->http_path)); + } + + if ($export_type == 'page') + { + $values_per_entry = 2; + $empty_data = array('ttlv' => 0, 'unqv' => 0); + } + else + { + $strip_url = 0; // Only get URLs in page data! + } + $filename = $stat_types[$export_date]."_"; + switch ($export_date) + { + case 1 : + $filename .= $export_type.'_day_'.date('Ymd',$first_date); + break; + case 2 : + $filename .= $export_type.'_mon_'.date('Ymd',$first_date).'_'.date('Ymd',$last_date); + $values_per_row = 31; + break; + case 3 : $filename .= $export_type.'_year_'.date('Ym',$first_date).'_'.date('Ym',$last_date); + $values_per_row = 12; + break; + case 4 : +// $filename .= $export_type.'_alltime'; + $filename .= $export_type; + break; + case 5 : +// $filename .= $export_type.'_alltime'; + $filename .= $export_type; + break; + } + $filename .= '.csv'; + if (defined('CSV_DEBUG')) $export_text .= "export stats to {$filename}
"; + + while($row = $sql -> db_Fetch()) + { // Process one DB entry + $date_id = substr($row['log_id'],strrpos($row['log_id'],'-')+1); // Numeric value of date being processed (not always valid) + if (!is_numeric($date_id)) $date_id = 0; + if (defined('CSV_DEBUG')) $export_text .= "Reading: ".$row['log_id']." Date value: {$date_id}
"; + if (($export_type == 'page') && (($export_date == 1) || ($export_date == 2))) + { // The daily page data files have a different format to the rest + list($daily, $unique, $db_data) = explode(chr(1),$row['log_data']); + $db_data = explode(chr(1),$db_data); // Individual entries + $process_mode = 1; + } + else + { + $db_data = unserialize($row['log_data']); + } + + foreach ($db_data as $k => $db_v) + { + if ($process_mode == 1) + { + list($url, $total, $unique) = explode('|', $db_v); + } + elseif ($export_type == 'page') + { + if ($export_date == 4) + { + $url = $k; // - the key here is the page URL without any query part + } + else + { + $url = $db_v['url']; + } + $total = $db_v['ttlv']; + $unique = $db_v['unqv']; + } + else + { + $url = $k; // Will actually be browser type or similar + $total = $db_v; + $unique = 0; + } + + if ($strip_url) + { + $url = str_replace($replace_url,"",$url); // We really just want a relative path. Strip out the root bit + } + // At this point we've identified a URL (or browser type, etc) and one or two values (two values for page data, one for other?). Add to an array + // Monthly stats: + // For the page data, each array entry has a key of the url and a value which is an array with two keys - ['ttlv'] (total accesses) and ['unqv'] (unique accesses) + // the first entry has the url 'TOTAL' + // All-time stats: + // Page data has array entries with three keys: 'url', 'ttlv', 'unqv' + +// Work with an array where each entry is an array with up to 31 values. The key of each entry is page name, browser type etc. Within the value we have numeric keys corresponding +// to months 1..12, or days 1..31, or zero for all-time stats and single day stats. For most stats the value will be within these keys; for page data we have a further level +// of arrays with keys ['ttlv'] and ['unqv'] +// echo $total.", ".$unique.", ".$url."
"; + if (!isset($export_array[$url][$date_id])) + { // Need to create an array + if ($values_per_row == 1) + { + $export_array[$url][$date_id] = $empty_data; + } + else + { + for ($i = 1; $i <= $values_per_row; $i++) + { + $export_array[$url][$i] = $empty_data; + } + } + } + + if ($values_per_entry == 1) + { + $export_array[$url][$date_id] = $total; + } + else + { + $export_array[$url][$date_id]['ttlv'] = $total; + $export_array[$url][$date_id]['unqv'] = $unique; + } + } + } + + foreach ($export_array as $url => $data) + { + $export_text .= $quote_char.$url.$quote_char; + foreach ($data as $day => $values) + { + if (is_array($values)) + { + foreach ($values as $info => $val) + { + if (!($val)) $val = 0; + $export_text .= $separator.$quote_char.$val.$quote_char; + } + } + else + { + if (!($val)) $val = 0; + $export_text .= $separator.$quote_char.$values.$quote_char; + } + } + $export_text .= "\n"; + } + + if (defined('CSV_DEBUG')) + { + $export_text .= "Summary data:
"; + echo str_replace("\n","
",$export_text); + } + else + { + if(headers_sent()) + { + return "Cannot output file - some data sent

"; + } + + //Secure https check + if (isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT']=='contype') header('Pragma: public'); + if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')) + header('Content-Type: application/force-download'); + else + header('Content-Type: application/octet-stream'); + header('Content-Length: '.strlen($export_text)); + header('Content-disposition: attachment; filename="'.$filename.'"'); + echo $export_text; + } + + return; +} + + +?>