$isadminsetting = $people['isadmin'];
$lastaction = date("Ymdhis");
$startdate = date("Ymd");
if (isset($UNTRUSTED['reset'])) {
    $query = "SELECT user_id,sessionid,camefrom,firstdepartment FROM livehelp_users WHERE isoperator='N' AND status!='chat'";
    $sth = $mydatabase->query($query);
    while ($row = $sth->fetchRow(DB_FETCHMODE_ORDERED)) {
        $user_id = $row[0];
        $sessionid = $row[1];
        $camefrom = $row[2];
        $firstdepartment = $old_user[3];
        // if not txt-db-api and $CSLH_Config['tracking'] == "Y" insert visitor and referer information:
        if ($dbtype != "txt-db-api") {
            if (!empty($camefrom) && $CSLH_Config['reftracking'] == "Y") {
                archivepage('livehelp_referers_daily', $camefrom, date("Ymd"), $firstdepartment);
                archivepage('livehelp_referers_monthly', $camefrom, date("Ym"), $firstdepartment);
            }
            if ($CSLH_Config['tracking'] == "Y") {
                archivefootsteps($sessionid);
            }
        }
        archiveuser($sessionid);
    }
    print "Database reset...";
    print "<SCRIPT type=\"text/javascript\"> window.location.replace(\"live.php\");</script>";
    print "<a href=live.php>click here</a>";
    exit;
}
if (!$serversession) {
    $mydatabase->close_connect();
}
/**
 * Given a sessionID this function archives off the footsteps of a given user.
 * counting their page visits only once.
 *
 * @param string SessionID 
 */
function archivefootsteps($sessionid)
{
    global $mydatabase;
    $dateof = date("Ym");
    // move all old visit data into visit tracking database counting each page once.
    $sqlquery = "SELECT location FROM livehelp_visit_track WHERE sessionid='" . filter_sql($sessionid) . "' ORDER BY whendone ";
    $footsteps = $mydatabase->query($sqlquery);
    $previousrecno = 0;
    //id of previous page recno.
    $arrayofurls = array();
    //array of urls visited already
    $sqlquery = "SELECT user_id,sessionid,camefrom,firstdepartment FROM livehelp_users WHERE sessionid='" . filter_sql($sessionid) . "' ";
    $old_people = $mydatabase->query($sqlquery);
    $old_user = $old_people->fetchRow(DB_FETCHMODE_ORDERED);
    $user_id = $old_user[0];
    $sessionid = $old_user[1];
    $camefrom = $old_user[2];
    $firstdepartment = $old_user[3];
    while ($foot = $footsteps->fetchRow(DB_FETCHMODE_ORDERED)) {
        $pageurl = $foot[0];
        // location
        $firstvisit = false;
        // flag to say if we have visited this or not.
        if (!in_array($pageurl, $arrayofurls)) {
            $arrayofurls[] = $pageurl;
            $firstvisit = true;
            archivepage('livehelp_visits_daily', $pageurl, date("Ymd"), $firstdepartment);
            archivepage('livehelp_visits_monthly', $pageurl, date("Ym"), $firstdepartment);
        }
        // simplify the url:
        // separate out querystring from page url
        $pathstuff = explode("?", $pageurl);
        $pageurl = $pathstuff[0];
        $querystring = "";
        if (!empty($pathstuff[1])) {
            $querystring = $pathstuff[1];
        }
        // change the size to max 250 and remove quotes and www.
        $pageurl = str_replace("'", "", $pageurl);
        $pageurl = str_replace("www.", "", $pageurl);
        $pageurl = substr($pageurl, 0, 250);
        // chop off ending slash
        $lastchar = substr($pageurl, -1);
        if ($lastchar == "/") {
            $pageurl = substr($pageurl, 0, -1);
        }
        // aquire the page visit monthly id:
        $sqlquery = "SELECT recno FROM livehelp_visits_monthly WHERE pageurl='" . $pageurl . "' AND dateof={$dateof} LIMIT 1";
        $referers_res = $mydatabase->query($sqlquery);
        $result = $referers_res->fetchRow(DB_FETCHMODE_ORDERED);
        $recno = intval($result[0]);
        // Skip if we are still in the same place we were:
        if ($previousrecno != $recno && $recno != 0) {
            // First time visiting page Paths:
            if ($firstvisit == true) {
                // see if we have this path in the database:
                $q = "SELECT id,visits FROM livehelp_paths_firsts WHERE visit_recno='{$previousrecno}' AND exit_recno='{$recno}' AND dateof='" . date("Ym") . "'";
                $referers_res = $mydatabase->query($q);
                if ($referers_res->numrows() == 0) {
                    $q = "INSERT INTO livehelp_paths_firsts (visit_recno,exit_recno,dateof,visits) VALUES ('{$previousrecno}','{$recno}','" . date("Ym") . "','1')";
                    $mydatabase->query($q);
                } else {
                    $result = $referers_res->fetchRow(DB_FETCHMODE_ORDERED);
                    $id = $result[0];
                    $visits = $result[1];
                    $visits++;
                    $q = "UPDATE livehelp_paths_firsts SET visits='{$visits}' WHERE id='{$id}'";
                    $mydatabase->query($q);
                }
            }
            // All time visiting page Paths.
            $q = "SELECT id,visits FROM livehelp_paths_monthly WHERE visit_recno='{$previousrecno}' AND exit_recno='{$recno}' AND dateof='" . date("Ym") . "'";
            $referers_res = $mydatabase->query($q);
            if ($referers_res->numrows() == 0) {
                $q = "INSERT INTO livehelp_paths_monthly (visit_recno,exit_recno,dateof,visits) VALUES ('{$previousrecno}','{$recno}','" . date("Ym") . "','1')";
                $mydatabase->query($q);
            } else {
                $result = $referers_res->fetchRow(DB_FETCHMODE_ORDERED);
                $id = $result[0];
                $visits = $result[1];
                $visits++;
                $q = "UPDATE livehelp_paths_monthly SET visits='{$visits}' WHERE id='{$id}'";
                $mydatabase->query($q);
            }
        }
        $previousrecno = $recno;
    }
    // end while more page visits
    // Record END of session Page:
    $recno = 0;
    // Skip if we are still in the same place we were:
    if ($previousrecno != $recno && $previousrecno != 0) {
        // First time visiting page Paths:
        if ($firstvisit == true) {
            // see if we have this path in the database:
            $q = "SELECT id,visits FROM livehelp_paths_firsts WHERE visit_recno='{$previousrecno}' AND exit_recno='{$recno}' AND dateof='" . date("Ym") . "'";
            $referers_res = $mydatabase->query($q);
            if ($referers_res->numrows() == 0) {
                $q = "INSERT INTO livehelp_paths_firsts (visit_recno,exit_recno,dateof,visits) VALUES ('{$previousrecno}','{$recno}','" . date("Ym") . "','1')";
                $mydatabase->query($q);
            } else {
                $result = $referers_res->fetchRow(DB_FETCHMODE_ORDERED);
                $id = $result[0];
                $visits = $result[1];
                $visits++;
                $q = "UPDATE livehelp_paths_firsts SET visits='{$visits}' WHERE id='{$id}'";
                $mydatabase->query($q);
            }
        }
        // All time visiting page Paths.
        $q = "SELECT id,visits FROM livehelp_paths_monthly WHERE visit_recno='{$previousrecno}' AND exit_recno='{$recno}' AND dateof='" . date("Ym") . "'";
        $referers_res = $mydatabase->query($q);
        if ($referers_res->numrows() == 0) {
            $q = "INSERT INTO livehelp_paths_monthly (visit_recno,exit_recno,dateof,visits) VALUES ('{$previousrecno}','{$recno}','" . date("Ym") . "','1')";
            $mydatabase->query($q);
        } else {
            $result = $referers_res->fetchRow(DB_FETCHMODE_ORDERED);
            $id = $result[0];
            $visits = $result[1];
            $visits++;
            $q = "UPDATE livehelp_paths_monthly SET visits='{$visits}' WHERE id='{$id}'";
            $mydatabase->query($q);
        }
    }
    // let get rid of the temp data..
    $sqlquery = "DELETE FROM livehelp_visit_track WHERE sessionid='" . filter_sql($sessionid) . "'";
    $mydatabase->query($sqlquery);
}