<?php

echo "<tr><th class='vis' colspan='3'>$lvm_create_archive</th></tr>\n";
$rname = Array('vpm', 'topVis', 'topNavOS', 'topOS', 'topNav', 'vph', 'topRef', 'topDom', 'vpj');
@set_time_limit(0);

// ------------------------
// vpj : visitors per day
// vpm : visitors per month
// ------------------------
$data['vpj'] = '';
$data['vpm']  = 0;

for ( $cnt = 1; $cnt <= 31; $cnt++ ) {
	if ( checkdate($month, $cnt, $year) ) {
		$day = sprintf('%4d/%02d/%02d', $year, $month, $cnt);
		$query  = "SELECT COUNT(*) AS C FROM $lvc_table_visitors WHERE DATE LIKE '$day%'";
		$rs = $xoopsDB->query($query);

		if ( $rs && $xoopsDB->getRowsNum($rs) ) {
			list($record) = $xoopsDB->fetchRow($rs);

			if ( $cnt > 1 ) {
				$data['vpj'] .= '+';
			}
			$data['vpj'] .= $cnt.'+'.$record;
            $data['vpm'] += $record;
        }
    }
}

if ( $data['vpm'] == 0 ) {
	echo "<tr><td class='visref' colspan='3' align='center'><br /><a class='error'>&nbsp;&nbsp;".str_replace('{VISITORS_TABLE}', $lvc_table_visitors, $lvm_error_nodata)."&nbsp;&nbsp;</a><br /><br /></td></tr>\n";
} else {
	// ----------------------
    // vph: visitors per hour
    // ----------------------
    $date['vph'] = '';
    for ( $hour = 0; $hour <= 23; $hour++ ) {
		$query2  = "SELECT COUNT(*) AS C FROM $lvc_table_visitors WHERE DATE LIKE '$year/".sprintf('%02d', $month)."/__ ".sprintf('%02d', $hour).":__'";
		$rs2 = $xoopsDB->query($query2);

        if ( $rs2 && $xoopsDB->getRowsNum($rs2) ) {
			list($record) = $xoopsDB->fetchRow($rs2);

            if ( $hour > 0 ) {
				$data['vph'] .= '+';
			}
			$data['vph'] .= $hour.'+'.$record;
        }
    }

    // --------------------
    // topVis: top visitors
    // --------------------
    $query = "SET SQL_BIG_TABLES = 1";
    $xoopsDB->query($query);

    $query  = "SELECT HOST, COUNT(*) AS C FROM $lvc_table_visitors WHERE DATE LIKE '$year/".sprintf('%02d', $month)."/%' GROUP BY HOST ORDER BY C DESC, HOST";

    $data['topVis'] = '';
	$rs = $xoopsDB->query($query, $lvc_nb_top_visitors, 0);
	if ( $rs ) {
		$cnt = 0;
        while ( $record = $xoopsDB->fetchArray($rs) ) {
			if ( $cnt++ > 0 ) {
				$data['topVis'] .= '+';
			}
			$data['topVis'] .= $record['HOST'].'+'.$record['C'];
        }
    }

    // --------------------
    // topRef: top referers
    // --------------------
    $query  = "SELECT REF_HOST, COUNT(*) AS C FROM $lvc_table_visitors WHERE DATE LIKE '$year/".sprintf('%02d', $month)."/%' AND REF_HOST != '' AND REF_HOST != '[unknown origin]' AND REF_HOST != 'bookmarks' GROUP BY REF_HOST ORDER BY C DESC, REF_HOST";

    $data['topRef'] = '';
	$rs = $xoopsDB->query($query, $lvc_nb_top_referer,0);
	if ( $rs ) {
		$cnt = 0;
        while ( $record = $xoopsDB->fetchArray($rs) ) {
			if ( $cnt++ > 0 ) {
				$data['topRef'] .= '+';
			}
			$data['topRef'] .= $record['REF_HOST'].'+'.$record['C'];
        }
    }

    // -------------------
    // topDom: top domains
    // -------------------
    $query  = "SELECT LCASE(SUBSTRING_INDEX(HOST, '.', -1)) AS D, COUNT(*) AS C FROM $lvc_table_visitors WHERE (HOST <> ADDR) AND DATE LIKE '$year/".sprintf('%02d', $month)."/%' GROUP BY D ORDER BY C DESC, D";
	$data['topDom'] = '';
	$rs = $xoopsDB->query($query, $lvc_nb_top_domain, 0);

    if ( $rs ) {
		$cnt = 0;
        while ( $record = $xoopsDB->fetchArray($rs) ) {
			if ( $cnt++ > 0 ) {
				$data['topDom'] .= '+';
			}
			$data['topDom'] .= $record['D'].'+'.$record['C'];
        }
    }

    // ----------------------------------------------------------------------------
    // topNavOs: top agent/OS
    // ----------------------------------------------------------------------------
    $query  = "SELECT AGENT, COUNT(*) AS C FROM $lvc_table_visitors WHERE AGENT <> '' AND DATE LIKE '$year/".sprintf('%02d',$month)."/%' GROUP BY AGENT ORDER BY C DESC, AGENT";

    $data['topNavOS'] = "";
	$rs = $xoopsDB->query($query, $lvc_nb_top_agent_os,0);
    if ( $rs ) {
        $cnt = 0;
        while ( $record = $xoopsDB->fetchArray($rs) ) {
			if ( $cnt++ > 0 ) {
				$data['topNavOS'] .= '+';
			}
			$data['topNavOS'] .= str_replace('+', ' ', $record['AGENT']).'+'.$record['C'];
        }
    }

    // -------------
    // topOS: top OS
    // -------------
    $others = '';
    $total = 0;
    for ( reset($lvc_OS); list($key, $value) = each($lvc_OS); ) {
        $query  = "SELECT COUNT(*) AS C FROM $lvc_table_visitors WHERE DATE LIKE '$year/".sprintf('%02d', $month)."/%' AND AGENT LIKE '%$key%'";
		$rs= $xoopsDB->query($query);
		if ( $rs && $xoopsDB->getRowsNum($rs) != 0 ) {
			list($record) = $xoopsDB->fetchRow($rs);
            
            $cnt_OS[$value] = $record;
            $total += $record;
        }
    }

    // others OS
    $cnt_OS[$lvm_others_os] = $data['vpm'] - $total;

    $data['topOS'] = '';
    $cnt = 0;
    for ( reset($cnt_OS); list($key, $value) = each($cnt_OS); ) {
        if ( $cnt++ > 0 ) {
			$data['topOS'] .= '+';
		}
		$data['topOS'] .= $key.'+'.(($data['vpm'] == 0) ? 0 : round(($value/$data['vpm']) * 1000) / 10);
    }

    // -----------------
    // topNav: top agent
    // -----------------
    $others = '';
    $total = 0;
    for ( reset($lvc_agent); list($key, $value) = each($lvc_agent); ) {
        $query  = "SELECT COUNT(*) AS C FROM $lvc_table_visitors WHERE DATE LIKE '$year/".sprintf('%02d', $month)."/%' AND AGENT LIKE '%$value%'";
		$rs = $xoopsDB->query($query);
		if ( $rs && $xoopsDB->getRowsNum($rs) != 0 ) {
			list($record) = $xoopsDB->fetchRow($rs);
            
            $cnt_agent[$key] = $record;
            $total += $record;
        }
    }

    // other agents
    $cnt_agent[$lvm_others_agent] = $data['vpm'] - $total;

    $data['topNav'] = '';
    $cnt = 0;
    for ( reset($cnt_agent); list($key, $value) = each($cnt_agent); ) {
        if ( $cnt++ > 0 ) {
			$data['topNav'] .= '+';
		}
		$data['topNav'] .= $key.'+'.(($data['vpm'] == 0) ? 0 : round(($value / $data['vpm']) * 1000) / 10);
    }

    // --------------
    // saving archive
    // --------------
    $query  = "SELECT code FROM $lvc_table_archives WHERE annee='$year' AND mois='$month'";
	$rs = $xoopsDB->query($query);

    if ( $rs ) {
        if ( $xoopsDB->getRowsNum($rs) != 0 ) {
            list($record) = $xoopsDB->fetchRow($rs);

            $code = $record;

            $query = "UPDATE $lvc_table_archives SET ";
            for ( $cnt = 0; $cnt < sizeof($rname); $cnt++ ) {
                if ( $cnt > 0 ) {
					$query .= ', ';
				}
                $query .= $rname[$cnt]."='".$data[$rname[$cnt]]."'";
            }
            $query .= "WHERE code='$code'";
        } else {
            $query = "INSERT INTO $lvc_table_archives (mois, annee, ";
            for ( $cnt = 0; $cnt < sizeof($rname); $cnt++ ) {
				if ( $cnt > 0 ) {
					$query .= ', ';
				}
                $query .= $rname[$cnt];
            }
            $query .= ") VALUES ('".(int)$month."','".$year."',";
            for ( $cnt = 0; $cnt < sizeof($rname); $cnt++ ) {
                if ( $cnt > 0 ) {
					$query .= ', ';
				}
                $query .= "'".$data[$rname[$cnt]]."'";
            }
            $query .= ")";
        }
        
        // archive creation
        $xoopsDB->queryF($query);
        //echo "$query";
    }

    // display results
    echo "<tr><td class='visref' colspan='3' align='center'><a class='ok'>&nbsp;&nbsp;&gt;&gt; ".$lvm_archive_created." &lt;&lt;&nbsp;&nbsp;</a></td></tr>\n";
    for ( $cnt = 0; $cnt < sizeof($rname); $cnt++ ) {
		echo "<tr><td class='visref' colspan='2' align='center'>&nbsp;<b>".$rname[$cnt]."</b>&nbsp;</td><td class='vis' align='left'>&nbsp;".str_replace('+', ' + ', htmlspecialchars($data[$rname[$cnt]]))."&nbsp;</td></tr>\n";
    }
}

?>