<?php

/*===============================================*\
|| ############################################# ||
|| # JAKWEB.CH / Version 2.1.4                 # ||
|| # ----------------------------------------- # ||
|| # Copyright 2023 JAKWEB All Rights Reserved # ||
|| ############################################# ||
\*===============================================*/

// Error reporting:
error_reporting(E_ALL^E_NOTICE);

// The DB connections data
require_once 'include/db.php';

if (!JAK_CACHE_DIRECTORY) die('Please define a cache directory in the db.php.');

// Start the session
$cookie_secure = true; // if you only want to receive the cookie over HTTPS
$cookie_httponly = true; // prevent JavaScript access to session cookie
$cookie_samesite = 'None';
if(PHP_VERSION_ID < 70300) {
    session_set_cookie_params(JAK_COOKIE_TIME, JAK_COOKIE_PATH.'; samesite='.$cookie_samesite, $_SERVER['HTTP_HOST'], $cookie_secure, $cookie_httponly);
} else {
    session_set_cookie_params([
        'lifetime' => JAK_COOKIE_TIME,
        'path' => JAK_COOKIE_PATH,
        'secure' => $cookie_secure,
        'httponly' => $cookie_httponly,
        'samesite' => $cookie_samesite
    ]);
}
session_start();

// Don't call some update twice
if (!isset($_SESSION['jkwio'])) $_SESSION['jkwio'] = false;

// Absolute Path
define('APP_PATH', dirname(__file__) . DIRECTORY_SEPARATOR);

if (isset($_SERVER['SCRIPT_NAME'])) {

    # on Windows _APP_MAIN_DIR becomes \ and abs url would look something like HTTP_HOST\/restOfUrl, so \ should be trimed too
    # @modified Chis Florinel <chis.florinel@candoo.ro>
    $app_main_dir = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\');
    define('_APP_MAIN_DIR', $app_main_dir);
} else {
    define('_APP_MAIN_DIR', SITE_SUBFOLDER);
}

// Get the DB class
require_once 'class/class.db.php';

// Change for 3.0.3
use JAKWEB\JAKsql;

// Database connection
$jakdb = new JAKsql([
    // required
    'database_type' => JAKDB_DBTYPE,
    'database_name' => JAKDB_NAME,
    'server' => JAKDB_HOST,
    'username' => JAKDB_USER,
    'password' => JAKDB_PASS,
    'charset' => 'utf8',
    'port' => JAKDB_PORT,
    'prefix' => JAKDB_PREFIX,
 
    // [optional] driver_option for connection, read more from http://www.php.net/manual/en/pdo.setattribute.php
    'option' => [PDO::ATTR_CASE => PDO::CASE_NATURAL]
    ]);

// All important files
include_once 'include/functions.php';
include_once 'class/class.browser.php';
include_once 'class/class.jakbase.php';
include_once 'class/class.userlogin.php';
include_once 'class/class.user.php';
include_once 'class/class.clientlogin.php';
include_once 'class/class.client.php';
require_once 'vendor/autoload.php';

// Windows Fix if !isset REQUEST_URI
if (!isset($_SERVER['REQUEST_URI']))
{
    $_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'],1 );
    if (isset($_SERVER['QUERY_STRING'])) { $_SERVER['REQUEST_URI'].='?'.$_SERVER['QUERY_STRING']; }
}

// Now launch the rewrite class, depending on the settings in db.
$_SERVER['REQUEST_URI'] = htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES);
$getURL = New JAK_rewrite($_SERVER['REQUEST_URI']);

// We are not using apache so take the ugly urls
$tempp = $getURL->jakGetseg(0);
$tempp1 = $getURL->jakGetseg(1);
$tempp2 = $getURL->jakGetseg(2);
$tempp3 = $getURL->jakGetseg(3);
$tempp4 = $getURL->jakGetseg(4);
$tempp5 = $getURL->jakGetseg(5);
$tempp6 = $getURL->jakGetseg(6);
$tempp7 = $getURL->jakGetseg(7);

// Check if we want caching
if (!is_dir(APP_PATH.JAK_CACHE_DIRECTORY)) mkdir(APP_PATH.JAK_CACHE_DIRECTORY, 0755);

// define file better for caching
$cachedefinefile = APP_PATH.JAK_CACHE_DIRECTORY.'/define.php';

if (!file_exists($cachedefinefile)) {

$allsettings = "<?php\n";

// Get the general settings out the database
$datasett = $jakdb->select("settings",["varname", "used_value"], ["opid" => 0]);
if (isset($datasett) && !empty($datasett)) foreach ($datasett as $row) {
    // Now check if sting contains html and do something about it!
    if (strlen($row['used_value']) != strlen(filter_var($row['used_value'], FILTER_SANITIZE_FULL_SPECIAL_CHARS))) {
        $defvar  = 'htmlspecialchars_decode("'.htmlspecialchars($row['used_value']).'")';
    } else {
        $defvar = "'".$row["used_value"]."'";
    }
        
    $allsettings .= "define('JAK_".strtoupper($row['varname'])."', ".$defvar.");\n";
}
    
$allsettings .= "?>";
        
JAK_base::jakWriteinCache($cachedefinefile, $allsettings, '');

}

// Get the users ip address
$ipa = get_ip_address();

// Set all users to not logged in
$userislogged = $mainop = false;
$getuid = $getcid = $opcacheid = 0;
// Get the language file if different from settings

// Check if operator is logged in
$jakuserlogin = new JAK_userlogin();
$jakuserrow = $jakuserlogin->jakChecklogged();
$jakuser = new JAK_user($jakuserrow);
if (is_numeric($jakuser->getVar("id"))) {
    $getuid = $jakuser->getVar("id");
    $jakuserlogin->jakUpdatelastactivity($getuid);
    $userislogged = true;
    // Get the operator language
    if ($jakuser->getVar("language")) $BT_LANGUAGE = $jakuser->getVar("language");

    // Now we get the siblings sorted
    $opcacheid = $jakuser->getVar("id");

    // Check if a sibling has logged in
    if ($jakuser->getVar("opid") != 0) {
        $opcacheid = $jakuser->getVar("opid");
    } else {
        $mainop = true;
    }

    $clienteditorpath = APP_PATH.JAK_EDITOR_PATH.$opcacheid;
                        
    if (!is_dir($clienteditorpath)) {
        mkdir($clienteditorpath, 0755);
        copy(APP_PATH.JAK_FILES_DIRECTORY."/index.html", $clienteditorpath."/index.html");

        mkdir($clienteditorpath.'/header/', 0755);
        $clientsrc = APP_PATH.JAK_EDITOR_PATH.'/header/';
        $clientdst = $clienteditorpath.'/header/';
        $clientheader = glob($clientsrc."*.*");
        foreach($clientheader as $h){
            $header_to_go = str_replace($clientsrc, $clientdst, $h);
            copy($h, $header_to_go);
        }

        $clienteditorpatht = APP_PATH.JAK_EDITOR_PATH_THUMBS.$opcacheid;
        mkdir($clienteditorpatht, 0755);
        mkdir($clienteditorpatht.'/header/', 0755);
        $clientsrct = APP_PATH.JAK_EDITOR_PATH_THUMBS.'/header/';
        $clientdstt = $clienteditorpatht.'/header/';
        $clientheadert = glob($clientsrct."*.*");
        foreach($clientheadert as $ht){
            $header_to_got = str_replace($clientsrct, $clientdstt, $ht);
            copy($ht, $header_to_got);
        }

    }
}
define('JAK_USERID', $getuid);

// Only the SuperAdmin in the config file see everything
if (JAK_USERID && $jakuser->jakSuperadminaccess(JAK_USERID)) {
    define('JAK_SUPERADMINACCESS', true);
} else {
    define('JAK_SUPERADMINACCESS', false);
}

// Check if client is logged in when no operator is logged
if (!$userislogged) {
    $jakclientlogin = new JAK_clientlogin();
    $jakclientrow = $jakclientlogin->jakChecklogged();
    $jakclient = new JAK_client($jakclientrow);
    if (is_numeric($jakclient->getVar("id"))) {
        $getcid = $jakclient->getVar("id");
        $jakclientlogin->jakUpdatelastactivity($getcid);
        $userislogged = true;
        // Get the client language
        if ($jakclient->getVar("language")) $BT_LANGUAGE = $jakclient->getVar("language");

        // Get the opid for this client
        $opcacheid = $jakclient->getVar("opid");
    }
}
define('JAK_CLIENTID', $getcid);

// At last set define to true for user is logged in
define('JAK_USERISLOGGED', $userislogged);

// All the pages
$page = ($tempp ? jak_url_input_filter($tempp) : '');
$page1 = ($tempp1 ? jak_url_input_filter($tempp1) : '');
$page2 = ($tempp2 ? jak_url_input_filter($tempp2) : '');
$page3 = ($tempp3 ? jak_url_input_filter($tempp3) : '');
$page4 = ($tempp4 ? jak_url_input_filter($tempp4) : '');
$page5 = ($tempp5 ? jak_url_input_filter($tempp5) : '');
$page6 = ($tempp6 ? jak_url_input_filter($tempp6) : '');
$page7 = ($tempp7 ? jak_url_input_filter($tempp7) : '');

// Default
if (!isset($widgetid)) $widgetid = 0;
$widgetlang = "";
$maincmspage = true;
// We have the main chat call
if (isset($page) && $page == 'lc') {
    // Write the chat widget id
    if (isset($page2) && is_numeric($page2)) $widgetid = $page2;
    $opcacheid = $jakdb->get("chatwidget", "opid", ["id" => $page2]);
    // Write the chat language
    if (isset($page3) && !empty($page3)) $widgetlang = $page3;
    $maincmspage = false;
}

// Ok we have a link, set the sessions.
if (isset($page) && $page == 'link') {
    // Write the chat widget id
    if (isset($page1) && is_numeric($page1)) $widgetid = $page1;
    $opcacheid = $jakdb->get("chatwidget", "opid", ["id" => $page1]);
    // Write the chat language
    if (isset($page2) && !empty($page2)) $widgetlang = $page2;
    $maincmspage = false;
}

// Set the group chat language
if (isset($page) && $page == 'groupchat') {
    // Write the chat language
    if (isset($page2) && !empty($page2)) $widgetlang = $page2;
    // Set the opcacheid
    $opcacheid = $jakdb->get("groupchat", "opid", ["id" => $page1]);
    // Set the groupchat session id
    $_SESSION['groupchatid'] = $page1;
    $maincmspage = false;
}

// Now lets check for direct linking of articles.
$individualpage = $confirmaccount = false;
if ($page == 'support' && $page1 == 't' && is_numeric($page2)) {
    // Get the operator id
    $opcacheid = $jakdb->get("support_tickets", "opid", ["id" => $page2]);
    $individualpage = true;
} elseif ($page == 'faq' && $page1 == 'a' && is_numeric($page2)) {
    $opcacheid = $jakdb->get("faq_article", "opid", ["id" => $page2]);
    $individualpage = true;
} elseif ($page == 'blog' && $page1 == 'a' && is_numeric($page2)) {
    $opcacheid = $jakdb->get("blog", "opid", ["id" => $page2]);
    $individualpage = true;
} elseif ($page == 'confirm' && is_numeric($page1) && is_numeric($page2)) {
    $confirmaccount = true;
}

// Now let's check if we have a subdomain
$urlgoeswrong = true;
if (JAK_WILDCARD_SUBDOMAIN) {

    // Get the host
    $urlorign = filter_var($_SERVER["HTTP_HOST"], FILTER_SANITIZE_ENCODED);

    // Do we have something?
    $custom_domain = $jakdb->has("subscriptions", ["custom_domain" => $urlorign]);

    // Let's check if we have a custom domain
    if (!empty($custom_domain)) {

        // It looks like we we found a third party domain
        $opcacheid = $jakdb->get("subscriptions", "opid", ["custom_domain" => $urlorign]);

        // We will need to define the URL
        define('BASE_URL', (JAK_SITEHTTPS ? 'https://' : 'http://') . $urlorign . _APP_MAIN_DIR . '/');
        $urlgoeswrong = false;

    } else {
        // Get the subdomain
        $subdomain = str_replace(FULL_SITE_DOMAIN, "", $urlorign);

        // We do have a subdomain
        if (isset($subdomain) && !empty($subdomain)) {

            // Ok, we do have a sub domain
            $subdomain = filter_var(str_replace(".", "", $subdomain), FILTER_SANITIZE_ENCODED);

            if ($jakdb->has("subscriptions", ["business" => $subdomain])) {

                // It looks like we found a subdomain
                $opcacheid = $jakdb->get("subscriptions", "opid", ["business" => $subdomain]);

                // We will need to define the URL
                define('BASE_URL', (JAK_SITEHTTPS ? 'https://' : 'http://') . $subdomain .".". FULL_SITE_DOMAIN . _APP_MAIN_DIR . '/');
                $urlgoeswrong = false;

            }
        }
    }

    // We do have wildcard enabled but are at the wrong place or not having a base_url yet.
    if ($urlgoeswrong && $jakdb->has("subscriptions", ["opid" => $opcacheid])) {

        // We need the business name
        $subdomain = $jakdb->get("subscriptions", ["business", "custom_domain"], ["opid" => $opcacheid]);

        if (isset($subdomain) && !empty($subdomain)) {

            // We will need to define the URL for the custom domain
            if (!empty($subdomain["custom_domain"])) {
                define('BASE_URL', (JAK_SITEHTTPS ? 'https://' : 'http://') . $subdomain["custom_domain"] . '/');
            } else {
                // No custom domain, let's go for the sub domain
                define('BASE_URL', (JAK_SITEHTTPS ? 'https://' : 'http://') . $subdomain["business"] .".". FULL_SITE_DOMAIN . _APP_MAIN_DIR . '/');
            }

            // We do have an individual page so we will redirect to the correct one
            if ($individualpage) {

                // Redirect to the correct URL
                jak_redirect(JAK_rewrite::jakParseurl($page, $page1, $page2, $page3, $page4, $page5, $page6, $page7));

            }

        }

    } elseif ($urlgoeswrong && !empty($page3) && $confirmaccount) {

        // We have no wildcard subdomain, but to confirm the account, let's go.
        define('BASE_URL', (JAK_SITEHTTPS ? 'https://' : 'http://') . $page3 .".". FULL_SITE_DOMAIN . _APP_MAIN_DIR . '/');

    } elseif ($urlgoeswrong) {

        // We do not have the correct page nor we have a opcacheid let's forward it to the sign up page
        jak_redirect(SIGN_UP_URL);

    }

    // We have no wildcard subdomain or could not find it, just go normal
    // defined('BASE_URL') or define('BASE_URL', (JAK_SITEHTTPS ? 'https://' : 'http://') . FULL_SITE_DOMAIN . _APP_MAIN_DIR . '/');

} else {

    // Check if we have a business change
    if (isset($page) && $page == 'bs' && $jakdb->has("subscriptions", ["business" => $page1])) {
        
        $opid = $jakdb->get("subscriptions", "opid", ["business" => $page1]);

        $_SESSION['opid'] = $opid;
        $_SESSION['business'] = $page1;
        jak_redirect((JAK_SITEHTTPS ? 'https://' : 'http://') . FULL_SITE_DOMAIN . _APP_MAIN_DIR . '/');

    }

    // Check if we have a business change on the operator panel side
    if (isset($page) && isset($page1) && $page == JAK_OPERATOR_LOC && $page1 == 'bs' && $jakdb->has("subscriptions", ["business" => $page2])) {
        
        $opid = $jakdb->get("subscriptions", "opid", ["business" => $page2]);

        $_SESSION['opid'] = $opid;
        $_SESSION['business'] = $page1;
        jak_redirect((JAK_SITEHTTPS ? 'https://' : 'http://') . FULL_SITE_DOMAIN . _APP_MAIN_DIR . '/'.JAK_OPERATOR_LOC);

    }

    // We need to set the opcacheid
    if (isset($_SESSION['opid']) && isset($_SESSION['business'])) {
        $opcacheid = $_SESSION['opid'];
    }

    // Ok, we now set the defined url
    define('BASE_URL', (JAK_SITEHTTPS ? 'https://' : 'http://') . FULL_SITE_DOMAIN . _APP_MAIN_DIR . '/');

}

// Get the operator settings
if (isset($opcacheid) && $opcacheid != 0) {

    // We still set a session for local use
    $_SESSION['opid'] = $opcacheid;

    $cacheopid = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';

    if (!file_exists($cacheopid)) {

        $opsett = "<?php\n";

        $datasett = $jakdb->select("settings",["varname", "used_value"], ["opid" => $opcacheid]);
        if (isset($datasett) && !empty($datasett)) foreach ($datasett as $row) {
            // Now check if sting contains html and do something about it!
            if (isset($row['used_value']) && strlen($row['used_value']) != strlen(filter_var($row['used_value'], FILTER_SANITIZE_FULL_SPECIAL_CHARS))) {
                $defvar = 'htmlspecialchars_decode("'.htmlspecialchars($row['used_value']).'")';
            } else {
                $defvar = "'".$row["used_value"]."'";
            }
                
            $opsett .= "define('JAK_".strtoupper($row['varname'])."', ".$defvar.");\n";
        }

        $datasettmain = $jakdb->select("settings",["varname", "used_value"], ["varname" => ["allowedo_files", "allowed_files", "updated", "version", "native_app_token", "native_app_key", "o_number", "live_online_status", "client_expired", "client_left", "push_reminder", "proactive_time", "recap_client", "recap_server"]]);
        if (isset($datasettmain) && !empty($datasettmain)) foreach ($datasettmain as $row) {
            // Now check if sting contains html and do something about it!
            if (strlen($row['used_value']) != strlen(filter_var($row['used_value'], FILTER_SANITIZE_FULL_SPECIAL_CHARS))) {
                $defvar  = 'htmlspecialchars_decode("'.htmlspecialchars($row['used_value']).'")';
            } else {
                $defvar = "'".$row["used_value"]."'";
            }
                
            $opsett .= "define('JAK_".strtoupper($row['varname'])."', ".$defvar.");\n";
        }

        // Get the one chat widget for the basic ID
        $chatwidgetidop = $jakdb->get("chatwidget", "id", ["opid" => $opcacheid, "ORDER" => ["id" => "ASC"]]);

        if (isset($chatwidgetidop) && is_numeric($chatwidgetidop)) $opsett .= "define('JAK_CHATWIDGETIDOP', ".$chatwidgetidop.");\n";

        $opsett .= "\n\$jakwidget = array();\n";

        // Get the chat widget out the database
        $reswidgarray = $jakdb->select("chatwidget", "*", ["opid" => $opcacheid]);
        if (isset($reswidgarray) && !empty($reswidgarray)) foreach ($reswidgarray as $reswidg) {

            $opsett .= "\$jakwidget['".$reswidg['id']."']['id'] = ".$reswidg['id'].";\n\$jakwidget['".$reswidg['id']."']['opid'] = ".$reswidg['opid'].";\n\$jakwidget['".$reswidg['id']."']['title'] = '".addslashes($reswidg['title'])."';\n\$jakwidget['".$reswidg['id']."']['depid'] = '".$reswidg['depid']."';\n\$jakwidget['".$reswidg['id']."']['singleopid'] = ".$reswidg['singleopid'].";\n\$jakwidget['".$reswidg['id']."']['lang'] = '".stripcslashes($reswidg['lang'])."';\n\$jakwidget['".$reswidg['id']."']['hidewhenoff'] = ".$reswidg['hidewhenoff'].";\n\$jakwidget['".$reswidg['id']."']['dsgvo'] = '".(isset($reswidg['dsgvo']) ? addslashes($reswidg['dsgvo']) : "")."';\n\$jakwidget['".$reswidg['id']."']['redirect_url'] = '".(isset($reswidg['redirect_url']) ? stripcslashes($reswidg['redirect_url']) : "")."';\n\$jakwidget['".$reswidg['id']."']['redirect_active'] = '".$reswidg['redirect_active']."';\n\$jakwidget['".$reswidg['id']."']['redirect_after'] = '".$reswidg['redirect_after']."';\n\$jakwidget['".$reswidg['id']."']['feedback'] = '".$reswidg['feedback']."';\n\$jakwidget['".$reswidg['id']."']['hidewhenoff'] = '".$reswidg['hidewhenoff']."';\n\$jakwidget['".$reswidg['id']."']['onlymembers'] = '".$reswidg['onlymembers']."';\n\$jakwidget['".$reswidg['id']."']['chatgpt'] = '".$reswidg['chatgpt']."';\n\$jakwidget['".$reswidg['id']."']['chatgpt_helpful'] = '".stripcslashes($reswidg['chatgpt_helpful'])."';\n\$jakwidget['".$reswidg['id']."']['template'] = '".stripcslashes($reswidg['template'])."';\n\$jakwidget['".$reswidg['id']."']['avatarset'] = '".stripcslashes($reswidg['avatarset'])."';\n\$jakwidget['".$reswidg['id']."']['btn_tpl'] = '".stripcslashes($reswidg['btn_tpl'])."';\n\$jakwidget['".$reswidg['id']."']['start_tpl'] = '".stripcslashes($reswidg['start_tpl'])."';\n\$jakwidget['".$reswidg['id']."']['chat_tpl'] = '".stripcslashes($reswidg['chat_tpl'])."';\n\$jakwidget['".$reswidg['id']."']['contact_tpl'] = '".stripcslashes($reswidg['contact_tpl'])."';\n\$jakwidget['".$reswidg['id']."']['profile_tpl'] = '".stripcslashes($reswidg['profile_tpl'])."';\n\$jakwidget['".$reswidg['id']."']['feedback_tpl'] = '".stripcslashes($reswidg['feedback_tpl'])."';\n";

            // Get the chat settings for this widget
            $reswidg2 = $jakdb->select("chatsettings", ["lang", "settname", "settvalue"], ["AND" => ["widgetid" => $reswidg["id"], "opid" => $reswidg['opid']]]);
            if (isset($reswidg2) && !empty($reswidg2)) {

                foreach ($reswidg2 as $row3) {

                    $opsett .= "\$widgetsettings['".$reswidg['id']."']['".$row3['settname']."'] = '".addslashes($row3['settvalue'])."';\n";

                }

            }

        }

        // Get the subscription out the database
        $opsett .= "\n\$jakosub = array();\n";
        $dataosub = $jakdb->get("subscriptions", "*", ["opid" => $opcacheid]);
        if (isset($dataosub) && !empty($dataosub)) {

            $opsett .= "\$jakosub['id'] = ".$dataosub['id'].";\n\$jakosub['packageid'] = ".$dataosub['packageid'].";\n\$jakosub['opid'] = ".$dataosub['opid'].";\n\$jakosub['operators'] = ".$dataosub['operators'].";\n\$jakosub['extraoperators'] = ".$dataosub['extraoperators'].";\n\$jakosub['business'] = '".stripcslashes($dataosub['business'])."';\n\$jakosub['departments'] = ".$dataosub['departments'].";\n\$jakosub['files'] = ".$dataosub['files'].";\n\$jakosub['tickets'] = ".$dataosub['tickets'].";\n\$jakosub['blog'] = ".$dataosub['blog'].";\n\$jakosub['faq'] = ".$dataosub['faq'].";\n\$jakosub['chatwidgets'] = ".$dataosub['chatwidgets'].";\n\$jakosub['groupchats'] = ".$dataosub['groupchats'].";\n\$jakosub['operatorchat'] = ".$dataosub['operatorchat'].";\n\$jakosub['copyfree'] = ".$dataosub['copyfree'].";\n\$jakosub['activechats'] = ".$dataosub['activechats'].";\n\$jakosub['phpimap'] = ".$dataosub['phpimap'].";\n\$jakosub['clients'] = ".$dataosub['clients'].";\n\$jakosub['validfor'] = ".$dataosub['validfor'].";\n\$jakosub['trial'] = ".$dataosub['trial'].";\n\$jakosub['active'] = ".$dataosub['active'].";\n\$jakosub['paidtill'] = '".$dataosub['paidtill']."';\n";
        }

        // empty vars
        $answergrid = $responsegrid = $autoproactivegrid = array();

        $opsett .= "\n";

        // Get the general settings out the database
        $datafiles = $jakdb->select("files",["id", "path", "name"], ["opid" => $opcacheid]);
        if (isset($datafiles) && !empty($datafiles)) foreach ($datafiles as $rowf) {
            $filesgrid[] = $rowf;
        }
            
        // Get the answers out the database
        $dataansw = $jakdb->select("answers", ["id", "opid", "department", "lang", "message", "fireup", "msgtype"], ["opid" => $reswidg['opid']]);
        if (isset($dataansw) && !empty($dataansw)) foreach ($dataansw as $rowa) {
            $answergrid[] = $rowa;
        }

        // Get the url black list
        $databl = $jakdb->select("urlblacklist", "path", ["opid" => $opcacheid]);
        if (isset($databl) && !empty($databl)) foreach ($databl as $rowb) {
            $blacklistgrid[] = $rowb;
        }
            
        // Get the responses settings out the database
        $datares = $jakdb->select("responses", ["id", "opid", "department", "title", "short_code", "message"], ["opid" => $opcacheid]);
        if (isset($datares) && !empty($datares)) foreach ($datares as $rowr) {
            $responsegrid[] = $rowr;
        }

        // Get the responses settings out the database
        $datarest = $jakdb->select("support_responses", ["id", "depid", "title", "message"], ["opid" => $opcacheid]);
        if (isset($datarest) && !empty($datarest)) foreach ($datarest as $rowrt) {
            $responsetgrid[] = $rowrt;
        }

        // Get the support status settings out the database
        $datasupstat = $jakdb->select("support_status", ["id", "title", "class", "oponly", "reminder", "closed"], ["opid" => $opcacheid, "ORDER" => ["dorder" => "ASC"]]);
        if (isset($datasupstat) && !empty($datasupstat)) foreach ($datasupstat as $rowst) {
            $supportstatgrid[] = $rowst;
        }

        // Get the chat bot out of the database
        $databot = $jakdb->select("bot_question", ["id", "opid", "widgetids", "depid", "lang", "question", "answer"], ["AND" => ["active" => 1, "opid" => $opcacheid]]);
        if (isset($databot) && !empty($databot)) foreach ($databot as $rowba) {
            $botgrid[] = $rowba;
        }

        // Get the departments
        $datadep = $jakdb->select("departments", ["id", "title", "email", "guesta", "faq_url", "credits"], ["AND" => ["opid" => $opcacheid, "active" => 1], "ORDER" => ["dorder" => "ASC"]]);
        if (isset($datadep) && !empty($datadep)) foreach ($datadep as $rowd) {
            $departmentgrid[] = $rowd;
        }

        // Get the support departments
        $datadeps = $jakdb->select("support_departments", ["id", "title", "email", "faq_url", "credits", "guesta"], ["AND" => ["opid" => $opcacheid, "active" => 1], "ORDER" => ["dorder" => "ASC"]]);
        if (isset($datadeps) && !empty($datadeps)) foreach ($datadeps as $rowds) {
            $departmentsgrid[] = $rowds;
        }

        $datafaq = $jakdb->select("faq_categories", ["id", "subcat", "class", "title", "description", "email", "guesta", "articles"], ["AND" => ["opid" => $opcacheid, "active" => 1], "ORDER" => ["dorder" => "ASC"]]);
        if (isset($datafaq) && !empty($datafaq)) foreach ($datafaq as $rowfaq) {
            $faqgrid[] = $rowfaq;
        }
            
        // Get the auto proactive out the database
        $dataproact = $jakdb->select("autoproactive", ["opid", "path", "title", "imgpath", "message", "btn_confirm", "btn_cancel", "showalert", "soundalert", "timeonsite", "visitedsites"], ["opid" => $opcacheid]);
        if (isset($dataproact) && !empty($dataproact)) foreach ($dataproact as $rowap) {
            $autoproactivegrid[] = $rowap;
        }
            
        if (!empty($answergrid)) $opsett .= "\$answergserialize = '".base64_encode(gzcompress(serialize($answergrid)))."';\n\n\$HD_ANSWERS = unserialize(gzuncompress(base64_decode(\$answergserialize)));\n";

        if (!empty($blacklistgrid)) $opsett .= "\$blacklistserialize = '".base64_encode(gzcompress(serialize($blacklistgrid)))."';\n\n\$HD_BLACKLIST = unserialize(gzuncompress(base64_decode(\$blacklistserialize)));\n";
            
        if (!empty($responsegrid)) $opsett .= "\$responsegserialize = '".base64_encode(gzcompress(serialize($responsegrid)))."';\n\n\$HD_RESPONSES = unserialize(gzuncompress(base64_decode(\$responsegserialize)));\n";

        if (!empty($responsetgrid)) $opsett .= "\$responsetgserialize = '".base64_encode(gzcompress(serialize($responsetgrid)))."';\n\n\$HD_RESPONSEST = unserialize(gzuncompress(base64_decode(\$responsetgserialize)));\n\n";

        if (!empty($supportstatgrid)) $opsett .= "\$supportstatserialize = '".base64_encode(gzcompress(serialize($supportstatgrid)))."';\n\n\$HD_SUPPORT_STATUS = unserialize(gzuncompress(base64_decode(\$supportstatserialize)));\n\n";

        if (!empty($botgrid)) $opsett .= "\$botserialize = '".base64_encode(gzcompress(serialize($botgrid)))."';\n\n\$HD_BOT_ANSWER = unserialize(gzuncompress(base64_decode(\$botserialize)));\n";

        if (!empty($filesgrid)) $opsett .= "\$filesgserialize = '".base64_encode(gzcompress(serialize($filesgrid)))."';\n\n\$HD_FILES = unserialize(gzuncompress(base64_decode(\$filesgserialize)));\n";

        if (!empty($departmentgrid)) $opsett .= "\$departmentgserialize = '".base64_encode(gzcompress(serialize($departmentgrid)))."';\n\n\$HD_DEPARTMENTS = unserialize(gzuncompress(base64_decode(\$departmentgserialize)));\n";

        if (!empty($departmentsgrid)) $opsett .= "\$departmentsgserialize = '".base64_encode(gzcompress(serialize($departmentsgrid)))."';\n\n\$HD_SUPPORT_DEPARTMENTS = unserialize(gzuncompress(base64_decode(\$departmentsgserialize)));\n\n";

        if (!empty($faqgrid)) $opsett .= "\$faqgserialize = '".base64_encode(gzcompress(serialize($faqgrid)))."';\n\n\$HD_FAQ_CATEGORIES = unserialize(gzuncompress(base64_decode(\$faqgserialize)));\n\n";

        if (!empty($autoproactivegrid)) $opsett .= "\$autoproactiveserialize = '".base64_encode(gzcompress(serialize($autoproactivegrid)))."';\n\n\$HD_PROACTIVE = unserialize(gzuncompress(base64_decode(\$autoproactiveserialize)));\n";

        // Get the general settings out the database
        $resgc = $jakdb->select("groupchat", "*", ["AND" => ["opid" => $opcacheid, "active" => 1]]);
        if (isset($resgc) && !empty($resgc)) {
            $opsett .= "\n\$groupchat = array();\n";

            foreach ($resgc as $rowgc) {

                $opsett .= "\$groupchat['".$rowgc['id']."']['id'] = '".$rowgc['id']."';\n\$groupchat['".$rowgc['id']."']['opid'] = '".stripcslashes($rowgc['opid'])."';\n\$groupchat['".$rowgc['id']."']['password'] = '".$rowgc['password']."';\n\$groupchat['".$rowgc['id']."']['title'] = '".addslashes($rowgc['title'])."';\n\$groupchat['".$rowgc['id']."']['description'] = '".(!empty($rowgc['description']) ? stripcslashes($rowgc['description']) : "")."';\n\$groupchat['".$rowgc['id']."']['opids'] = '".stripcslashes($rowgc['opids'])."';\n\$groupchat['".$rowgc['id']."']['maxclients'] = ".$rowgc['maxclients'].";\n\$groupchat['".$rowgc['id']."']['lang'] = '".stripcslashes($rowgc['lang'])."';\n\$groupchat['".$rowgc['id']."']['buttonimg'] = '".stripcslashes($rowgc['buttonimg'])."';\n\$groupchat['".$rowgc['id']."']['floatpopup'] = ".$rowgc['floatpopup'].";\n\$groupchat['".$rowgc['id']."']['floatcss'] = '".(!empty($rowgc['floatcss']) ? stripcslashes($rowgc['floatcss']) : "")."';\n\$groupchat['".$rowgc['id']."']['active'] = ".$rowgc['active'].";\n";
               
            }

        }

        // Finally close the cache file
        $opsett .= "?>";

        JAK_base::jakWriteinCache($cacheopid, $opsett, '');

    }

    // Now include the created definefile
    include_once $cacheopid;

    if (defined('JAK_LANG') && !isset($BT_LANGUAGE)) $BT_LANGUAGE = JAK_LANG;
    // Define the MAIN_OP
    define('JAK_MAIN_OP', $mainop);

    // Your copyright link
    $JAK_PCOPYRIGHT_LINK = '<a href="https://jakweb.ch/cloud-desk-3">CD3</a> / ';
    // Remove the copyright when customer has paid for
    if (isset($jakosub['copyfree']) && $jakosub['copyfree'] == 1) $JAK_PCOPYRIGHT_LINK = "";
    // Copyright do only remove or change with a valid copyright free link license
    define('JAK_COPYRIGHT_LINK', $JAK_PCOPYRIGHT_LINK.'<a href="https://jakweb.ch/cloud-desk-3">Powered by Cloud Desk 3</a>');

    // For the file and image manager
    $_SESSION["RF"]["subfolder"] = $opcacheid.'/';

} else {
    // Now include the created definefile
    // include_once $cachedefinefile;
    // die("Please access your site with the correct URL.");
    if ($confirmaccount || defined('BASE_URL')) {
        include_once $cachedefinefile;
    } else {
        jak_redirect(SIGN_UP_URL);
    }
}

// timezone from server
if (defined('JAK_TIMEZONESERVER')) date_default_timezone_set(JAK_TIMEZONESERVER);
$jakdb->query('SET time_zone = "'.date("P").'"');
?>
