function master_table()
{
    $q = new mysql_squid_builder();
    $tpl = new templates();
    $page = CurrentPageName();
    $year = date("Y");
    $week = intval(date('W'));
    if ($_GET["table"] == null) {
        $table = "{$year}{$week}_week";
    } else {
        $table = $_GET["table"];
    }
    $array = array();
    $tables = $q->LIST_TABLES_WEEKS();
    while (list($index, $tablez) = each($tables)) {
        $array[$tablez] = $q->WEEK_TITLE_FROM_TABLENAME($tablez);
    }
    $MasterTitle = $q->WEEK_TITLE_FROM_TABLENAME($table);
    $field = Field_array_Hash($array, "table-query-{$t}", $table, "ChangeWeekPanel{$t}()", null, 0, "font-size:12px");
    $array = array();
    $field = "<table style='width:100%'>\n\t<tbody>\n\t<tr><td width=100%' style='font-size:16px;font-weight:bold'>{$MasterTitle}</td>\n\t<td width=1%>\n\t<table><tbody><tr><td class=legend>{week}:</td>{$field}</td></tr></table>\n\t</td>\n\t</tr>\n\t</tbody>\n\t</table>";
    $html = "\n\t<div id='master-{$t}'>\n\t{$field}<table style='width:100%'>\n\t<tbody>\n\t<tr>\n\t\t<td width='33.33%' valign='top'><div id='panel-left-top'></div></td>\n\t\t<td width='33.33%' valign='top'><div id='panel-middle-top'></div></td>\n\t\t<td width='33.33%' valign='top'><div id='panel-right-top'></div></td>\n\t</tr>\n\t</tbody>\n\t</table>\n\t</div>\n\t<script>\n\t\tLoadAjax('panel-left-top','{$page}?panel-categories-week=yes&table={$_GET["table"]}');\n\t\t\n\t\tfunction ChangeWeekPanel{$t}(){\n\t\t\t\$('master-{$t}').remove();\t\n\t\t\tvar sdate=document.getElementById('table-query-{$t}').value;\n\t\t\tLoadAjax('panel-start-point','{$page}?master=yes&table='+sdate);\n\t\t}\n\t</script>\n\t";
    echo $tpl->_ENGINE_parse_body($html);
}
function change_week_popup()
{
    $page = CurrentPageName();
    $tpl = new templates();
    $q = new mysql_squid_builder();
    $t = $_GET["t"];
    $members = $tpl->_ENGINE_parse_body("{members}");
    $week = $tpl->_ENGINE_parse_body("{week}");
    $array = array();
    $tables = $q->LIST_TABLES_WEEKS();
    while (list($index, $tablez) = each($tables)) {
        $array[$tablez] = $q->WEEK_TITLE_FROM_TABLENAME($tablez);
    }
    $field = Field_array_Hash($array, "table-query-{$t}", $table, " DayMemberChangeWeekPanel{$t}()", null, 0, "font-size:14px");
    $array = array();
    $html = "\n\t<table style='width:99%' class=form>\n\t\t<tbody>\n\t\t\t<tr>\n\t\t\t\t<td class=legend style='font-size:16px;'>{week}:</td>\n\t\t\t\t<td>{$field}</td>\n\t\t\t</tr>\n\t</table>\n\n\t<script>\n\t\tfunction DayMemberChangeWeekPanel{$t}(){\n\t\t\tvar xday=document.getElementById('table-query-{$t}').value;\n\t\t\t\$('.ftitle').html('{$members}&raquo;{$week}&raquo;table:'+xday);\n\t\t\t\$('#{$t}').flexOptions({url: '{$page}?list-week=yes&week='+xday,title:'{$members}'+xday}).flexReload();\n\t\t\n\t\t}\n\t\n\t</script>";
    echo $tpl->_ENGINE_parse_body($html);
}
function sitenames()
{
    $page = CurrentPageName();
    $tpl = new templates();
    $field = $_GET["field"];
    $value = $_GET["value"];
    $MyTableMonth = date("Ym") . "_day";
    $MyMonthText = date("{F}");
    $q = new mysql_squid_builder();
    $tableQuery = $_GET["table"];
    if (isset($_GET["table"])) {
        $MyTableMonth = $_GET["table"];
    }
    if (!$q->TABLE_EXISTS($MyTableMonth)) {
        echo FATAL_ERROR_SHOW_128("&laquo;{$MyTableMonth}&raquo; {table_does_not_exists}");
        return;
    }
    if (preg_match("#_week#", $_GET["table"])) {
        $title_add = "&raquo;" . $tpl->_ENGINE_parse_body($q->WEEK_TITLE_FROM_TABLENAME($_GET["table"]));
    }
    if (preg_match("#_day\$#", $_GET["table"])) {
        $title_add = "&raquo;" . $tpl->_ENGINE_parse_body($q->MONTH_TITLE_FROM_TABLENAME($_GET["table"]));
    }
    if (preg_match("#_hour\$#", $_GET["table"])) {
        $title_add = "&raquo;" . $tpl->_ENGINE_parse_body($q->DAY_TITLE_FROM_TABLENAME($_GET["table"]));
    }
    if ($field == "ipaddr") {
        $field = "client";
    }
    $title = $tpl->_ENGINE_parse_body("{where} ? &raquo;&raquo;{{$field}}::{$value} {$title_add}");
    $t = time();
    $sitename = $tpl->_ENGINE_parse_body("{sitename}");
    $category = $tpl->_ENGINE_parse_body("{category}");
    $hits = $tpl->_ENGINE_parse_body("{hits}");
    $size = $tpl->_ENGINE_parse_body("{size}");
    $sitename = $tpl->_ENGINE_parse_body("{sitename}");
    $ipaddr = $tpl->_ENGINE_parse_body("{ipaddr}");
    $members = $tpl->_ENGINE_parse_body("{members}");
    $hostname = $tpl->_ENGINE_parse_body("{hostname}");
    $mac = $tpl->_ENGINE_parse_body("{MAC}");
    $week = $tpl->_ENGINE_parse_body("{week}");
    $month = $tpl->_ENGINE_parse_body("{month}");
    $TB_WIDTH = 550;
    $t = time();
    $buttons = "\n\tbuttons : [\n\t{name: '<b>{$day}</b>', bclass: 'Calendar', onpress : ChangeDay{$t}},\n\t{name: '<b>{$week}</b>', bclass: 'Calendar', onpress : ChangeWeek{$t}},\n\t{name: '<b>{$month}</b>', bclass: 'Calendar', onpress : ChangeMonth{$t}},\n\t\n\t\t],";
    $buttons = null;
    $html = "\n\t<table class='{$t}' style='display: none' id='{$t}' style='width:99%'></table>\n\n<script>\n\n\$(document).ready(function(){\n\$('#{$t}').flexigrid({\n\turl: '{$page}?sitenames-items=yes&field={$field}&value={$value}&table={$_GET["table"]}&familysite={$_GET["familysite"]}',\n\tdataType: 'json',\n\tcolModel : [\n\t\t{display: '{$sitename}', name : 'sitename', width : 181, sortable : true, align: 'left'},\n\t\t{display: '{$category}', name : 'category', width : 245, sortable : true, align: 'left'},\n\t\t{display: '{$size}', name : 'size', width : 109, sortable : true, align: 'left'},\n\t\t{display: '{$hits}', name : 'hits', width : 94, sortable : true, align: 'left'},\n\n\t\t\n\t\t\n\t],{$buttons}\n\tsearchitems : [\n\t\t{display: '{$sitename}', name : 'sitename'},\n\t\t{display: '{$category}', name : 'category'},\n\t\t],\n\tsortname: 'size',\n\tsortorder: 'desc',\n\tusepager: true,\n\ttitle: '{$title}',\n\tuseRp: true,\n\trp: 15,\n\tshowTableToggleBtn: false,\n\twidth: 700,\n\theight: 450,\n\tsingleSelect: true\n\t\n\t});\n});\n</script>";
    echo $tpl->_ENGINE_parse_body($html);
}
function synthesis()
{
    $page = CurrentPageName();
    $tpl = new templates();
    $q = new mysql_squid_builder();
    $table = $_GET["table"];
    $user = $_GET["user"];
    $field = $_GET["field"];
    $t = $_GET["divkey"];
    $titleW = $q->WEEK_TITLE_FROM_TABLENAME($table);
    $TITLE_PAGE = "{$user} ({{$field}}) <br>{$titleW}";
    if ($field == "MAC") {
        $userText = $q->UID_FROM_MAC($user);
        if ($userText != null) {
            $TITLE_PAGE = "{$userText} ({$user}) <br>{$titleW}";
        }
    }
    $html = "\n\t\n\t<table style='width:99%' class=form>\n\t<tbody>\n\t<tr>\n\t\t<td width=1%><img src='img/postmaster-48.png'></td>\n\t\t<td><div style='width:100%;font-size:16px;font-weight:bold'>{$TITLE_PAGE}</div></td>\n\t</tr>\n\t</tbody>\n\t</table>\n\t<table style='width:100%'>\n\t<tbody>\n\t\t<tr>\n\t\t\t<td width=50% valign='top'><div id='paragraphe1'></div></td>\n\t\t\t<td width=50% valign='top'><div id='paragraphe2'></div></td>\n\t\t</tr>\n\t</tbody>\n\t</table>\n\t<script>\n\t\tfunction LoadUserSearchSection(){\n\t\t\tif(!document.getElementById('week-search-users-list')){alert('week-search-users-list no such id');}\n\t\t\tLoadAjaxTiny('week-search-users-list','{$page}?users-form=yes&table={$table}&user={$user}&field={$field}');\n\t\t}\n\t\t\n\t\tLoadUserSearchSection();\n\t</script>\n\t\t\t\n\t\n\t";
    echo $tpl->_ENGINE_parse_body($html);
}
function what_popup()
{
    $q = new mysql_squid_builder();
    $tpl = new templates();
    $tableQuery = $_GET["table"];
    $tpl = new templates();
    $page = 1;
    $FORCE_FILTER = null;
    $total = 0;
    $field = $_GET["field"];
    $value = $_GET["value"];
    if (preg_match("#quotamonth#", $tableQuery)) {
        $xtime = $q->TIME_FROM_QUOTAMONTH_TABLE($tableQuery);
        $tableQuery = date("Ym", $xtime) . "_day";
    }
    if ($tableQuery == null) {
        $MyTableMonth = date("Ym") . "_day";
        $tableQuery = $MyTableMonth;
    }
    if (preg_match("#_week#", $_GET["table"])) {
        $title_add = "&raquo;" . $tpl->_ENGINE_parse_body($q->WEEK_TITLE_FROM_TABLENAME($_GET["table"]));
    }
    if (preg_match("#_day\$#", $_GET["table"])) {
        $title_add = "&raquo;" . $tpl->_ENGINE_parse_body($q->MONTH_TITLE_FROM_TABLENAME($_GET["table"]));
    }
    if (preg_match("#_hour\$#", $_GET["table"])) {
        $title_add = "&raquo;" . $tpl->_ENGINE_parse_body($q->DAY_TITLE_FROM_TABLENAME($_GET["table"]));
    }
    if ($_GET["field"] == "ipaddr") {
        $_GET["field"] = "client";
    }
    $sql = "SELECT familysite,{$_GET["field"]},SUM(hits) as hits,category FROM {$tableQuery}\n\tWHERE {$_GET["field"]}='{$_GET["value"]}' GROUP BY familysite,{$_GET["field"]} ORDER BY hits DESC LIMIT 0,10";
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        echo $q->mysql_error_html();
    }
    if ($GLOBALS["VERBOSE"]) {
        echo mysql_num_rows($results) . " entries <br>";
    }
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        if (strpos($ligne["category"], ",") > 0) {
            $tp = explode(",", $ligne["category"]);
            while (list($index, $cat) = each($tp)) {
                if (isset($valsz[$cat])) {
                    continue;
                }
                $valsz[$cat] = $ligne["hits"];
            }
            continue;
        }
        $valsz[$cat] = $ligne["hits"];
    }
    while (list($cat, $count) = each($valsz)) {
        $xdata[] = $count;
        $ydata[] = $cat;
    }
    $targetedfilePie = "ressources/logs/" . basename(__FILE__) . "." . __FUNCTION__ . "." . md5($sql) . ".pie.png";
    $gp = new artica_graphs($targetedfilePie);
    $gp->xdata = $xdata;
    $gp->ydata = $ydata;
    $gp->width = 750;
    $gp->height = 550;
    $gp->ViewValues = true;
    $gp->x_title = $tpl->_ENGINE_parse_body("{what} ? {$MyMonthText}");
    $gp->pie();
    if (!is_file($targetedfilePie)) {
        $html = "<center>{$targetedfilePie} no such file</center>";
        writelogs("Fatal \"{$targetedfilePie}\" no such file!", __FUNCTION__, __FILE__, __LINE__);
    } else {
        $html = $html . "\n\t\t<center>\n\t\t\t<div style='width:99%' class=form>\n\t\t\t\t<div style='font-size:18px;margin:8px'>&laquo;{$value}&raquo;&nbsp;{what} {$title_add}</div>\n\t\t\t\t<img src='{$targetedfilePie}'>\n\t\t\t</div>\n\t\t\t\n\t\t\t\n\t\t</center>\n\t\t\n\t\t";
    }
    echo $tpl->_ENGINE_parse_body($html);
}
function synthesis()
{
    $page = CurrentPageName();
    $tpl = new templates();
    $q = new mysql_squid_builder();
    $table = $_GET["table"];
    $user = $_GET["user"];
    $field = $_GET["field"];
    $t = $_GET["divkey"];
    $category = $_GET["category"];
    $titleW = $q->WEEK_TITLE_FROM_TABLENAME($table);
    $html = "\n\t\n\t<table style='width:99%' class=form>\n\t<tbody>\n\t<tr>\n\t\t<td width=1%><img src='img/64-categories-loupe.png'></td>\n\t\t<td><div style='width:100%;font-size:16px;font-weight:bold'>{category} {$category} <br>{$titleW}</div></td>\n\t</tr>\n\t</tbody>\n\t</table>\n\t<table style='width:100%'>\n\t<tbody>\n\t\t<tr>\n\t\t\t<td width=50% valign='top'><div id='paragraphecat1'></div></td>\n\t\t\t<td width=50% valign='top'><div id='paragraphecat2'></div></td>\n\t\t</tr>\n\t</tbody>\n\t</table>\n\t<script>\n\t\tfunction LoadUserSearchSection(){\n\t\t\tLoadAjaxTiny('week-search-users-list-{$t}','{$page}?form=yes&table={$table}&user={$user}&field={$field}&category={$category}');\n\t\t}\n\t\t\n\t\tLoadUserSearchSection();\n\t</script>\n\t\t\t\n\t\n\t";
    echo $tpl->_ENGINE_parse_body($html);
}
function popup1()
{
    $page = CurrentPageName();
    $tpl = new templates();
    $q = new mysql_squid_builder();
    $table = $_GET["table"];
    $user = $_GET["user"];
    $field = $_GET["field"];
    $category = $_GET["category"];
    $t = $_GET["divkey"];
    $titleW = $q->WEEK_TITLE_FROM_TABLENAME($table);
    $weeksd = array(1 => "Sunday", 2 => "Monday", 3 => "Tuesday", 4 => "Wednesday", 5 => "Thursday", 6 => "Friday", 7 => "Saturday");
    $sql = "SELECT SUM(hits) as thits,SUM(size) as tsize,familysite,\n\tcategory,{$field} FROM {$table} GROUP BY familysite,category,{$field} HAVING category='{$category}' AND {$field}='{$user}' ORDER BY thits DESC LIMIT 0,10";
    $results = $q->QUERY_SQL($sql);
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $ligne["tsize"] = FormatBytes($ligne["tsize"] / 1024);
        $ydata[] = "{$ligne["familysite"]}";
        $xdata[] = $ligne["thits"];
    }
    $targetedfile = "ressources/logs/" . basename(__FILE__) . "." . __FUNCTION__ . "." . time() . ".png";
    $gp = new artica_graphs($targetedfile);
    $gp->xdata = $xdata;
    $gp->ydata = $ydata;
    $gp->width = 250;
    $gp->height = 400;
    $gp->ViewValues = true;
    //$gp->PieLegendHide=true;
    $gp->x_title = $tpl->_ENGINE_parse_body("{cache}");
    $gp->pie();
    $xdata = array();
    $ydata = array();
    $sql = "SELECT SUM(hits) as thits,SUM(size) as tsize,`day`,category,{$field} FROM {$table} GROUP BY `day`,category,{$field} \n\tHAVING category='{$category}' AND {$field}='{$user}' ORDER BY `day`";
    $results = $q->QUERY_SQL($sql);
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $ligne["tsize"] = FormatBytes($ligne["tsize"] / 1024);
        $xdata[] = $tpl->_ENGINE_parse_body("{{$weeksd[$ligne["day"]]}}");
        $ydata[] = $ligne["thits"];
        $tr2[] = "\n\t\t<tr>\n\t\t\t<td width=1%><img src='img/calendar.gif'></td>\n\t\t\t<td style='font-size:12px;font-weight:bold' width=1%>" . $tpl->_ENGINE_parse_body("{{$weeksd[$ligne["day"]]}}") . "</td>\n\t\t\t<td style='font-size:12px;font-weight:bold' width=99% nowrap>{$ligne["thits"]} {hits} - {$ligne["tsize"]}</td>\n\t\t</tr>\n\t\t\t\n\t\t";
    }
    $targetedfile2 = "ressources/logs/" . basename(__FILE__) . "." . __FUNCTION__ . ".2." . time() . ".png";
    $gp = new artica_graphs();
    $gp->width = 270;
    $gp->height = 150;
    $gp->filename = "{$targetedfile2}";
    $gp->xdata = $xdata;
    $gp->ydata = $ydata;
    $gp->y_title = null;
    $gp->x_title = $tpl->_ENGINE_parse_body("{days}");
    $gp->title = null;
    $gp->margin0 = true;
    $gp->Fillcolor = "blue@0.9";
    $gp->color = "146497";
    $gp->line_green();
    if (!is_file($targetedfile2)) {
        $targetedfile2 = "img/nograph-000.png";
    }
    $html = "<table style='width:99%' class=form>\n\t<tbody>\n\t<tr>\n\t\t<td width=1%><img src='img/postmaster-48.png'></td>\n\t\t<td><div style='width:100%;font-size:16px;font-weight:bold'>\n\t\t\t{$user} ({{$field}}) <br>{$titleW}<br>{category}:{$category}\n\t\t</div></td>\n\t</tr>\n\t</tbody>\n\t</table>\n\t<table style='width:99%' class=form>\n\t<tbody>\n\t<tr>\n\t<td valign='top' width=50%>\n\t\t<div style='font-size:13px;font-weight:bold;color: rgb(207, 23, 23);margin-top:15px'>{$category}:{top_visited_websites}:</div>\n\t\t<p>&nbsp;</p>\n\t\t<center><img src='{$targetedfile}'></center>\n\t</td>\n\t<td valign='top' width=50%>\n\t\t<div style='font-size:13px;font-weight:bold;color: rgb(207, 23, 23);margin-top:15px'>{$category}:{hits} {byday}:</div>\n\t\t<p>&nbsp;</p>\n\t\t<center><img src='{$targetedfile2}'>\n\t\t<table style='width:50%;border:1px solid #CCCCCC'><tbody>" . @implode("\n", $tr2) . "</tbody></table>\n\t\t</center>\n\t</td>\n\t</tr>\n\t</tbody>\n\t</table>\n\t\n\t";
    echo $tpl->_ENGINE_parse_body($html);
}
function websites()
{
    $page = CurrentPageName();
    $tpl = new templates();
    $q = new mysql_squid_builder();
    $table = $_GET["table"];
    if (preg_match("#[0-9]+_hour\$#", $table)) {
        $year = substr($table, 0, 4);
        $month = substr($table, 4, 2);
        $day = substr($table, 6, 2);
        $date = "{$year}-{$month}-{$day}";
        $timeS = strtotime($date);
        $table = date("YW", $timeS) . "_week";
    }
    $www = $_GET["www"];
    $field = $_GET["field"];
    $titleW = $q->WEEK_TITLE_FROM_TABLENAME($table);
    $weeksd = array(1 => "Sunday", 2 => "Monday", 3 => "Tuesday", 4 => "Wednesday", 5 => "Thursday", 6 => "Friday", 7 => "Saturday");
    $hits = "hits";
    $hits_title = "{members}";
    $ipaddr = $tpl->_ENGINE_parse_body("{ipaddr}");
    $ComputerMacAddress = $tpl->_ENGINE_parse_body("{ComputerMacAddress}");
    $hostname = $tpl->_ENGINE_parse_body("{hostname}");
    $websites = $tpl->_ENGINE_parse_body("{websites}");
    $size = $tpl->_ENGINE_parse_body("{size}");
    $html = "<div id='title' style='font-size:16px;font-weight:bold'>{website}: {$www} {$titleW}</div>\n\t<center style='font-size:16px;font-weight:bold;margin-top:15px;width:80%;margin:10px'>\n\t<div style='border-top:1px solid #CCCCCC;border-bottom:1px solid #CCCCCC;padding:20px;'>\n\t{$www} {websites}\n\t</div></center>\n\t<table class='events-table-{$t}' style='display: none' id='events-table-{$t}' style='width:99%'></table>\n<script>\n\n\$(document).ready(function(){\n\$('#events-table-{$t}').flexigrid({\n\turl: '{$page}?websites-search=yes&table={$_GET["table"]}&www={$_GET["www"]}&field={$_GET["field"]}',\n\tdataType: 'json',\n\tcolModel : [\n\t\t{display: '{$hits}', name : 'hits', width :90, sortable : false, align: 'left'},\n\t\t{display: '{$size}', name : 'size', width :90, sortable : false, align: 'left'},\n\t\t{display: '{$websites}', name : 'sitename', width :600, sortable : true, align: 'left'},\n\t\t\n\t],\n\n\tsearchitems : [\n\t\t{display: '{$websites}', name : 'sitename'},\n\t\t\n\t\t\n\t\t],\n\tsortname: 'hits',\n\tsortorder: 'desc',\n\tusepager: true,\n\ttitle: '',\n\tuseRp: true,\n\trp: 50,\n\tshowTableToggleBtn: false,\n\twidth: 833,\n\theight: 170,\n\tsingleSelect: true\n\t\n\t});   \n});\n\n</script>";
    echo $tpl->_ENGINE_parse_body($html);
}