* copyright 2004-2006 Telartis * version 1.13 (http://www.telartis.nl/xcms/awstats) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place','Suite 330, Boston, MA 02111-1307, USA. */ /** * File contents: * * This file contains functions that read data from files process it and store it * in an array. This concerns library-data as well as stats-data. Language files * are NOT processed here. */ // this file can't be used on its own if (strpos ($_SERVER['PHP_SELF'], 'data.inc.php') !== false) { die ('This file can not be used on its own!'); } /** * Data function: read the data directories this function is called by * baw_match_files() and itself recursively * * @param str $dir directory * @return arr ALL files found in that directory * */ function baw_parse_dir($dir = false) { global $BAW_CONF; if ($dir == false) { $dir = $BAW_CONF['path_data']; } if (!file_exists($dir)) { echo baw_raise_error('datafilesdir', array($dir)); return array(); } // add trailing slash if not exists if (substr($dir, -1) != '/') { $dir .= '/'; } baw_debug('dbg_start_parse_dir', $dir); $files = array(); if ($dh = @opendir($dir)) { while (($file = readdir($dh)) !== false) { if (!preg_match('/^\./s', $file)) { if (is_dir($dir.$file)) { $newdir = $dir.$file.'/'; $files = array_merge($files, baw_parse_dir($newdir)); baw_debug('dbg_found_dir', $dir); } else { $files[] = $dir.$file; baw_debug('dbg_found_file', $file); } } } closedir($dh); } baw_debug('dbg_finished_parse_dir', $dir); return $files; } /* * Data function: find all files that contain the data for the current month and * 12 month before now to write a rolling month data chart. This function is called * by index.php * * fills the gl. arrays: $BAW_CURR['years'], $BAW_CURR['months'], $BAW_DFILES * */ function baw_match_files() { global $BAW_CURR, $BAW_CONF, $BAW_CONF_DIS, $BAW_MES, $BAW_DFILES, $BAW_SERVERS; // read all the files in the directory $dirfiles = baw_parse_dir(); $pat_others = '/awstats(\d{6})\.((.+))\.txt$/'; // go through all files and find matching ones $year_array = array(); $month_array = array(); foreach ($dirfiles as $file) { $filename = explode("/", $file); $filename = $filename[count($filename)-1]; if (preg_match($pat_others, $file, $match)) { $month = substr($filename, 7, 2); $year = substr($filename, 9, 4); $year_array[$year] = $year; $month_array[$month] = $BAW_MES[$month + 59]; // future feature where the admin can tell which sites to read/ exclude // if (!$BAW_CONF['filter_configs'] || in_array($site, $BAW_CONF['filter_configs'])) { $site = $match[2]; $BAW_DFILES[$site][$year.$month] = array( 'file' => $file, 'map' => null ); // add the sites to another array since we need that for the config editor $BAW_SERVERS[$site] = $site; baw_debug("site found for month $month and year $year: $file, SITE: {$site}"); } } if (count($year_array)>0) { $year_array = array_unique($year_array); ksort($year_array); } if (count($month_array)>0) { ksort($month_array); $month_array = array_unique($month_array); } $BAW_CURR['years'] = $year_array; $BAW_CURR['months'] = $month_array; // since we added sites with each file, remove duplicates ksort($BAW_DFILES); ksort($BAW_SERVERS); $BAW_SERVERS = array('show_all' => $BAW_MES['cfg_show_all']) + $BAW_SERVERS; if (!isset($BAW_CURR['site_name'])) { $BAW_CURR['site_name'] = key($BAW_DFILES); // we dont have a selected, take first in line reset($BAW_DFILES); } } /* * Data function: calculate the month-data by summing up days and retrieving * single-value data. This function is called by different display-functions * * @param arr $t TIME data array of the current file * @param arr $g GENERAL data array of the current file * @return arr summed dataset for processing * */ function baw_calc_monthdata($t, $g) { baw_debug("Calculating Month-data"); $pages = 0; $hits = 0; $bandwidth = 0; $not_viewed_pages = 0; $not_viewed_hits = 0; $not_viewed_bandwidth = 0; $num_t = count($t); for ($i=0; $i<$num_t; $i++) { $pages += $t[$i][0]; $hits += $t[$i][1]; $bandwidth += $t[$i][2]; $not_viewed_pages += $t[$i][3]; $not_viewed_hits += $t[$i][4]; $not_viewed_bandwidth += $t[$i][5]; } $result = array( $g['TotalUnique'][0], $g['TotalVisits'][0], $pages, $hits, $bandwidth, $not_viewed_pages, $not_viewed_hits, $not_viewed_bandwidth ); return $result; } /* * Data Function: read data from awstats library files (perl format) * Opens a file, reads the contents and does corrections and makes PHP out of PERL * This function is called by the file library.inc.php * * @param str $file path to the library file * @param str $data the name of the array in the file * @return arr summed dataset for processing * */ function baw_get_library($file, $data) { global $BAW_CONF, $BAW_LOGTYPE; $file = $BAW_CONF['path_lib'] . $file; baw_debug("Trying to read library data ($data) from file: $file"); if (!file_exists($file)) { echo baw_raise_error('libraryfiles', array($file)); exit; } // read the whole file in one go $file_text = file_get_contents($file); // these are to attempt to convert the perl data file to PHP $search = array( "\$LogType eq 'S'", "= ", // remove spaces between = and ( "@", "%", "'=>'", "=(", "=\n(", "=\r(", "=\r\n(", ",,", ' target="_blank"', ' [new window]', ' $line_item) { if (is_numeric($line_item)) { // this could cause trouble in case this was a date, percentage etc. $filedata[$first_element][$no] += $line_item; } } } } fclose($f); baw_debug("data read, file closed"); return $filedata; } /* * Data Function: read the filemap at the begginning of a data file. This function * is called only by baw_data if the data was not read before. * * @param obj &$f file-handler to read * @param str $site the concerned site * @param str $date YYYYMM, date of datafile * adds the data to gl. array $BAW_DFILES * */ function baw_read_filemap(&$f, $site, $date) { global $BAW_DFILES; // $f = fopen($file, 'r'); $str =''; $check = 1; // read this file until we hit the offsets while ($check !== 0) { $str = fgets($f, 20000); // check for XML Data if (strstr($str, ' 1) { baw_debug("data type $type, starts at offset $offset"); $BAW_DFILES[$site][$date]['map'][$type] = $offset; } else { echo baw_raise_error('datafileindex', $err_data); exit; } } baw_debug("map read"); } ?>