Location: PHPKode > projects > Stuffed Tracker - Web Traffic Monitoring & Analysis > stuffed-tracker-web-traffic-monitoring-analysis/actions/admin.host_pages.php
<?
if (!defined("NS_TRACKER_INDEX")) exit;
/////////////////////////////////////////////
///////// permission check here
if (!$nsUser->Logged()) $nsProduct->Redir("login", "", "admin");


/////////////////////////////////////////////
///////// require libraries here
require_once SYS."/system/lib/validate.func.php";

require_once SELF."/lib/company.func.php";
require_once SELF."/lib/delete.func.php";


/////////////////////////////////////////////
///////// prepare any variables
$nsLang->TplInc("inc/top_panel.inc");
$nsLang->TplInc("inc/menu");
$MenuSection="settings";

$nsSession->start();

$EditId=V($_GP['EditId']);
$EditArr=V($_GP['EditArr']);
$HostId=V($_GP['HostId']);
$GrpId=V($_GP['GrpId'],0);
$FilterClicked=V($_GP['FilterClicked']);
$Filter=V($_GP['Filter'],  ($FilterClicked)?"":V($_SESSION['__HFilter'])   );
$DeleteGrpId=V($_GP['DeleteGrpId']);

$MultiplePageAction=V($_GP['MultiplePageAction']);
$NewPageGrp=V($_GP['NewPageGrp']);
$MPageAction=V($_GP['MPageAction'],array());
$MoveToGrp=V($_GP['MoveToGrp']);
$EditGrpName=V($_GP['EditGrpName']);
$EditRegExp=V($_GP['EditRegExp']);
$RegExp=V($_GP['RegExp']);

$_SESSION['__HFilter']=$Filter;

$SiteArr=array();
$Query = "SELECT * FROM ?_site WHERE COMPANY_ID=?";
$Sql = new Query($Query, false, $CpId);
while ($Row=$Sql->Row()) {
    if (!$HostId) $HostId=$Row->ID;
    $SiteArr[]=$Row;
}

if (!$HostId) $nsProduct->Redir("company", "EditId=$CpId");
$EditHost=$Db->Select("SELECT * FROM ?_site WHERE ID = $HostId");
if (!$EditHost) $nsProduct->Redir("company", "EditId=$CpId");

$PageTitle=$Lang['PagesList'];

$Client=$Db->Select("SELECT * FROM ?_client WHERE ID=?", false, $EditHost->COMPANY_ID);

$ProgPath[0]['Name']=$Lang['MSettings'];
$ProgPath[0]['Url']=getURL("settings", "CpId=$CpId", "admin");
$ProgPath[1]['Name']=$Lang['Title'];
$ProgPath[1]['Url']=getURL("host_pages", "", "admin");
$ProgPath[2]['Name']=$EditHost->HOST;
$ProgPath[2]['Url']=getURL("host_pages", "HostId=".$EditHost->ID, "admin");


if (!$nsUser->DEMO) {
    if ($EditId=="new"&&ValidArr($EditArr)) AddSitePage($HostId, $EditArr);
    if (ValidId($EditId)&&ValidArr($EditArr)) UpdatePage($EditId, $EditArr);
    if (ValidId($DeleteId)) DeletePage($DeleteId);  
    
    if ($MultiplePageAction=="new" && !empty($MPageAction) && $NewPageGrp) CreateNewPageGrp($MPageAction, $NewPageGrp);
    if ($MultiplePageAction=="move" && !empty($MPageAction) && $MoveToGrp) MovePageToGrp($MPageAction, $MoveToGrp);
    if ($MultiplePageAction=="from" && !empty($MPageAction)) MovePageToGrp($MPageAction, false);

    if (ValidId($DeleteGrpId)) DeletePageGrp($DeleteGrpId);
    if ($GrpId && $EditGrpName) UpdatePageGrpName($GrpId, $EditGrpName,$EditRegExp); 
}





// pages list
if (!$EditId) {

    $PagesList=array();
    $GroupArr=array();
    
    $Query = "SELECT * FROM ?_site_page_grp WHERE SITE_ID=? ORDER BY NAME ASC";
    $Sql = new Query($Query, false, $HostId);
    while ($Row=$Sql->Row()) {
        if ($Row->ID==$GrpId) {
            $EditGrpName=$Row->NAME;
            $EditRegExp=$Row->REG_EXP;
        }
        $GroupArr[]=$Row;
    }
    
        $FilterStr="";
        $RegStr="";
        if ($Filter) $FilterStr="AND CONCAT(SP.PATH, IF(Q.QUERY_STRING IS NOT NULL,?sel,''), IF(Q.QUERY_STRING IS NOT NULL,Q.QUERY_STRING,'')) LIKE '%".addslashes($Filter)."%' ";
        if ($RegExp) {
            //$RegExp=str_replace("'","",$RegExp);
            //$RegExp=str_replace("\"","",$RegExp);
            $RegStr="AND CONCAT(SP.PATH, IF(Q.QUERY_STRING IS NOT NULL,?where,''), IF(Q.QUERY_STRING IS NOT NULL,Q.QUERY_STRING,'')) REGEXP ?reg ";
        }

        if ($FilterStr || $RegStr) {
            $Query = "
                SELECT COUNT(*)
                FROM ?_page_query PQ
                    INNER JOIN ?_site_page SP
                        ON SP.ID=PQ.PAGE_ID
                    LEFT JOIN ?_query Q
                        ON Q.ID=PQ.QUERY_ID
                WHERE PQ.SITE_ID=?site AND PQ.GRP_ID=?grp
                $FilterStr
                $RegStr
            ";
        }
        else $Query = "SELECT COUNT(*) FROM ?_page_query PQ WHERE PQ.SITE_ID=?site AND PQ.GRP_ID=?grp";
        $Cnt=$Db->ReturnValue($Query,array("site"=>$HostId,"grp"=>$GrpId,"sel"=>"?","where"=>"?","reg"=>$RegExp));
        if ($Cnt<1000) include_once SELF."/class/pagenums2.class.php";
        else include_once SELF."/class/pagenums2.class.php";
        $Pages = new PageNums($Cnt,50);
        $Pages->Args="&HostId=$HostId&GrpId=$GrpId&Filter=$Filter&RegExp=$RegExp&";
        
        if ($Cnt>0) {
            $OrderBy=($FilterStr||$RegStr||$Cnt<5000)?"ORDER BY SH.HOST, SP.PATH ASC, Q.QUERY_STRING ASC ":"";
            $Query = "
                SELECT
                    SP.PATH, Q.QUERY_STRING, SH.HOST,
                    PQ.GRP_ID, PQ.QUERY_ID,PQ.ID AS SELECT_ID, 
                    SP.ID, PT.TITLE AS NAME
                    
                FROM ?_page_query PQ
                    INNER JOIN ?_site_page SP
                        ON SP.ID=PQ.PAGE_ID
                    INNER JOIN ?_site_host SH
                        ON SH.ID=PQ.SITE_HOST_ID
                    LEFT JOIN ?_page_title PT
                        ON PT.ID = PQ.TITLE_ID
                    LEFT JOIN ?_query Q
                        ON Q.ID=PQ.QUERY_ID
                WHERE PQ.SITE_ID=?site AND PQ.GRP_ID=?grp
                $FilterStr 
                $RegStr
                $OrderBy
                LIMIT $Pages->PageStart,$Pages->Limit 
            ";
            
            $Sql = new Query($Query, false, array("site"=>$HostId,"grp"=>$GrpId,"sel"=>"?","where"=>"?","reg"=>$RegExp));
            while ($Row=$Sql->Row()) {
                $Row->PATH=htmlspecialchars($Row->PATH);
                if ($Row->QUERY_STRING) $Row->PATH.="?".htmlspecialchars($Row->QUERY_STRING);
                if ($Row->HOST) $Row->PATH=$Row->HOST.$Row->PATH;
                $Row->NAME=_htmlspecialchars($Row->NAME);
                $PagesList[]=$Row;
            }
            $Pages->Calculate();
        }
  
    include $nsTemplate->Inc();
}







//// new page 
if ($EditId=="new") {
    $TableCaption=$Lang['CaptionNew'];
    if (!ValidArr($EditArr)) {
        $EditArr['Path']="";
        $EditArr['PageIgnore']=0;
    }
    $SubMenu[0]['Name']=$Lang['BackToList'];
    $SubMenu[0]['Link']=getURL("host_pages", "HostId=$HostId");

    include $nsTemplate->Inc("admin.page_edit");
}

//// edit page 
if (ValidId($EditId)) {
    $EditPage=GetPage($EditId);
    $EditArr['Path']=ValidVar($EditArr['Path'],$EditPage->PATH);
    $EditArr['PageIgnore']=ValidVar($EditArr['PageIgnore'],$EditPage->IGNORE_PAGE);
    
    $SubMenu[0]['Name']=$Lang['BackToList'];
    $SubMenu[0]['Link']=getURL("host_pages", "HostId=$HostId");
    $ProgPath[3]['Name']=$EditPage->PATH;
    $ProgPath[3]['Url']=getURL("host_pages", "HostId=$HostId&EditId=$EditId", "admin");

    $TableCaption=$Lang['CaptionEdit'].stripslashes($EditPage->PATH);
    include $nsTemplate->Inc("admin.page_edit");
}














////////////////////////////////////





function AddSitePage($SiteId, $Arr)
{
    global $Db, $Logs, $Lang, $nsProduct, $EditCompany;
    extract($Arr);
    if(!$Path) $ErrArr['Path']=$Lang['MustFill'];
    if (!isset($PageIgnore)) $PageIgnore=0;
    if (isset($ErrArr)) {$Logs->Msg($Lang['FormErr']); $GLOBALS['ErrArr']=$ErrArr; return;}
    $Query = "INSERT INTO ?_site_page (SITE_ID, PATH, IGNORE_PAGE) VALUES (?, ?, ?)";
    $Db->Query($Query,$SiteId,$Path,$PageIgnore);
    $nsProduct->Redir("host_pages", "RCrt=1&HostId=$SiteId&EditId=".$Db->LastInsertId);
}

function UpdatePage($Id, $Arr)
{
    global $Db, $Logs, $Lang, $nsProduct,$HostId;
    extract($Arr);
    if(!$Path) $ErrArr['Path']=$Lang['MustFill'];
    if (!isset($PageIgnore)) $PageIgnore=0;
    if (isset($ErrArr)) {$Logs->Msg($Lang['FormErr']); $GLOBALS['ErrArr']=$ErrArr; return;}
    $Query = "UPDATE ?_site_page SET PATH = ?, IGNORE_PAGE=? WHERE ID = ?";
    $Db->Query($Query, $Path, $PageIgnore, $Id);
    $nsProduct->Redir("host_pages", "RUpd=1&HostId=$HostId&EditId=$Id");
}


function DeletePage($Id)
{
    $EditPage=GetPage($Id);
    global $Db, $nsProduct;
    $Query = "DELETE FROM ?_site_page WHERE ID = ?";
    $Db->Query($Query, $Id);
    $nsProduct->Redir("host_pages", "RDlt=1&HostId=".$EditPage->SITE_ID);
}


function CreateNewPageGrp($Arr, $Grp) 
{
    global $HostId, $Db, $nsProduct, $Filter;
    $Query = "INSERT INTO ?_site_page_grp (SITE_ID, NAME) VALUES (?, ?) ";
    $Db->Query($Query, $HostId, $Grp);
    $GrpId=$Db->LastInsertId;
    if (!$GrpId) return false;
    MovePageToGrp($Arr, $GrpId);
}


function MovePageToGrp($Arr, $NewGrpId)
{
    global $HostId, $Db, $nsProduct, $GrpId, $Filter;
    foreach ($Arr as $PqId) {
        if ($NewGrpId) {
            $Query = "UPDATE ?_page_query SET GRP_ID=? WHERE ID=?";
            $Db->Query($Query, $NewGrpId, $PqId);
        }
        else {
            $Query = "UPDATE ?_page_query SET GRP_ID=0 WHERE ID=?";
            $Db->Query($Query,$PqId);
        }
    }
    $nsProduct->Redir("host_pages", "RUpd=1&HostId=$HostId&Filter=".urlencode($Filter));
}

function UpdatePageGrpName($Id, $Name, $EditRegExp) {
    global $HostId, $Db, $nsProduct;
    $Query = "UPDATE ?_site_page_grp SET NAME=?, REG_EXP=? WHERE ID = ?";
    $Db->Query($Query, $Name, $EditRegExp, $Id);
    $nsProduct->Redir("host_pages", "RUpd=1&GrpId=$Id&HostId=$HostId");
}

function DeletePageGrp($Id)
{
    global $HostId, $Db, $nsProduct;
    $Query = "DELETE FROM ?_site_page_grp WHERE ID = ?";
    $Db->Query($Query, $Id);
    $Query = "UPDATE  ?_page_query SET GRP_ID=0 WHERE GRP_ID = ?";
    $Db->Query($Query, $Id);
    $nsProduct->Redir("host_pages", "RUpd=1&HostId=$HostId");
}

?>
Return current item: Stuffed Tracker - Web Traffic Monitoring & Analysis