<?php

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

// Check if the file is accessed only via index.php if not stop the script from running
if (!defined('JAK_ADMIN_PREVENT_ACCESS')) die('You cannot access this file directly.');

// Check if the user has access to this file
if (!jak_get_access("answers", $jakuser->getVar("permissions"), JAK_MAIN_OP)) jak_redirect(BASE_URL);

// Change for 3.0.3
use JAKWEB\JAKsql;

// All the tables we need for this plugin
$errors = array();
$jaktable = 'answers';
$jaktable1 = 'departments';
$jaktable2 = 'cms_pages';

$forbidden_slug = array(JAK_OPERATOR_LOC, JAK_CLIENT_URL, JAK_BLOG_URL, JAK_FAQ_URL, JAK_SEARCH_URL, JAK_SUPPORT_URL, 'contact', 'start', 'btn', 'quickstart', 'link', 'stop', 'feedback', 'groupchat', 'api', 'check', '404', 'closechat', 'profile', 'chat', 'forgot-password', 'logout');

// Now start with the plugin use a switch to access all pages
switch ($page1) {

  case 'cms':
    switch ($page2) {
      case 'edit':

        // Check if user exists and can be deleted
        if (is_numeric($page3) && jak_row_exist($page3, $opcacheid, $jaktable2)) {

          if ($_SERVER['REQUEST_METHOD'] == 'POST') {
              $jkp = $_POST;
              
            if (empty($jkp['title'])) {
                $errors['e'] = $jkl['e'];
            }
                    
            if (empty($jkp['url_slug']) || !preg_match('/^([a-z-_0-9]||[-_])+$/', $jkp['url_slug']) || ($jkp['jak_prepage'] == "0" && in_array($jkp['url_slug'], $forbidden_slug))) {
                $errors['e1'] = $jkl['hd211'];
            }
                    
            if (count($errors) == 0) {

              if ($jkp['jak_prepage'] != "0") $jkp['jak_prepage'] = $jkp['url_slug'];

                $result = $jakdb->update($jaktable2, ["lang" => $jkp['jak_lang'],
                "title" => $jkp['title'],
                "url_slug" => $jkp['url_slug'],
                "external_url" => $jkp['external_url'],
                "previmg" => $jkp['previmg'],
                "meta_keywords" => $jkp['meta_key'],
                "meta_description" => $jkp['meta_desc'],
                "dorder" => $jkp['order'],
                "showheader" => $jkp['jak_header'],
                "showfooter" => $jkp['jak_footer'],
                "ishome" => $jkp['jak_ishome'],
                "prepage" => $jkp['jak_prepage'],
                "custom" => $jkp['jak_custom'],
                "custom2" => $jkp['jak_custom2'],
                "custom3" => $jkp['jak_custom3'],
                "custom4" => $jkp['jak_custom4'],
                "custom5" => $jkp['jak_custom5'],
                "header_tpl" => $jkp['jak_header_tpl'],
                "main_tpl" => $jkp['jak_main_tpl'],
                "footer_tpl" => $jkp['jak_footer_tpl'],
                "access" => $jkp['jak_membersonly'],
                "hits" => $jkp['hits'],
                "edited" => $jakdb->raw("NOW()"),
                "created" => $jakdb->raw("NOW()")], ["AND" => ["id" => $page3, "opid" => $opcacheid]]);
                
                if (!$result) {
                  $_SESSION["infomsg"] = $jkl['i'];
                  jak_redirect($_SESSION['LCRedirect']);
                } else {

                  // Write the log file each time someone tries to login before
                      JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 37, $page3, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);

                  $_SESSION["successmsg"] = $jkl['g14'];
                  jak_redirect($_SESSION['LCRedirect']);
                }
                
            // Output the errors
            } else {
                
                $errors = $errors;
            }
          }

          // Call the language function
          $lang_files = jak_get_lang_files();

          // Get the page
          $JAK_FORM_DATA = jak_get_data($page3, $opcacheid, $jaktable2);
          
          // Title and Description
          $SECTION_TITLE = $jkl["hd129"];
          $SECTION_DESC = "";

          // Get the different template files.
          $header_tpl = jak_get_templates_files(APP_PATH.'template/'.JAK_FRONT_TEMPLATE.'/tplblocks/header_tpl/');

          if ($JAK_FORM_DATA["prepage"] == JAK_BLOG_URL) {
            $main_tpl = jak_get_templates_files(APP_PATH.'template/'.JAK_FRONT_TEMPLATE.'/tplblocks/blog_tpl/');
          } elseif ($JAK_FORM_DATA["prepage"] == JAK_FAQ_URL) {
            $main_tpl = jak_get_templates_files(APP_PATH.'template/'.JAK_FRONT_TEMPLATE.'/tplblocks/faq_tpl/');
          } elseif ($JAK_FORM_DATA["prepage"] == JAK_SEARCH_URL) {
            $main_tpl = jak_get_templates_files(APP_PATH.'template/'.JAK_FRONT_TEMPLATE.'/tplblocks/search_tpl/');
          } elseif ($JAK_FORM_DATA["prepage"] == JAK_SUPPORT_URL) {
            $main_tpl = jak_get_templates_files(APP_PATH.'template/'.JAK_FRONT_TEMPLATE.'/tplblocks/support_tpl/');
          } elseif ($JAK_FORM_DATA["prepage"] == JAK_CONTACT_URL) {
            $main_tpl = jak_get_templates_files(APP_PATH.'template/'.JAK_FRONT_TEMPLATE.'/tplblocks/contact_tpl/');
          } else {
            $main_tpl = jak_get_templates_files(APP_PATH.'template/'.JAK_FRONT_TEMPLATE.'/tplblocks/page_tpl/');
          }

          $footer_tpl = jak_get_templates_files(APP_PATH.'template/'.JAK_FRONT_TEMPLATE.'/tplblocks/footer_tpl/');

          // Get the custom stuff from the selected template
          $styleconfig = APP_PATH.'template/'.JAK_FRONT_TEMPLATE.'/config.php';
          if (file_exists($styleconfig)) include_once $styleconfig;

          // Include the javascript file for results
          $js_file_footer = 'js_cms.php';
           
          // Call the template
          $template = 'editcmspage.php';

        } else {
            
             $_SESSION["errormsg"] = $jkl['i3'];
            jak_redirect($_SESSION['LCRedirect']);
        }
      break;
      case 'delete':
        // Check if user exists and can be deleted
        if (is_numeric($page3) && jak_row_exist($page3, $opcacheid, $jaktable2)) {
                
          // Now check how many languages are installed and do the dirty work
          $result = $jakdb->delete($jaktable2, ["AND" => ["id" => $page3, "opid" => $opcacheid]]);
        
        if (!$result) {
            $_SESSION["infomsg"] = $jkl['i'];
            jak_redirect($_SESSION['LCRedirect']);
        } else {

          // Write the log file each time someone tries to login before
                JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 30, $page3, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);

            $_SESSION["successmsg"] = $jkl['g14'];
            jak_redirect($_SESSION['LCRedirect']);
        }
            
        } else {
            
             $_SESSION["errormsg"] = $jkl['i3'];
            jak_redirect($_SESSION['LCRedirect']);
        }
      break;
      case 'lock':
        // Set CMS status
        if (is_numeric($page3) && jak_row_exist($page3, $opcacheid, $jaktable2)) {

          // Check what we have to do
          $datausrac = $jakdb->get($jaktable2, "active", ["AND" => ["id" => $page3, "opid" => $opcacheid]]);
          // update the table
          if ($datausrac) {
            $result = $jakdb->update($jaktable2, ["active" => 0], ["AND" => ["id" => $page3, "opid" => $opcacheid]]);
          } else {
            $result = $jakdb->update($jaktable2, ["active" => 1], ["AND" => ["id" => $page3, "opid" => $opcacheid]]);
          }
          
          if (!$result) {
            $_SESSION["infomsg"] = $jkl['i'];
              jak_redirect($_SESSION['LCRedirect']);
          } else {
              $_SESSION["successmsg"] = $jkl['g14'];
              jak_redirect($_SESSION['LCRedirect']);
          }
        
        } else {
             $_SESSION["errormsg"] = $jkl['i3'];
            jak_redirect($_SESSION['LCRedirect']);
        }
      break;
      default:

        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $jkp = $_POST;

            if (isset($jkp['insert_page'])) {
            
              if (empty($jkp['title'])) {
                  $errors['e'] = $jkl['e'];
              }
                  
              if (empty($jkp['url_slug']) || !preg_match('/^([a-z-_0-9]||[-_])+$/', $jkp['url_slug']) || ($jkp['jak_prepage'] == "0" && in_array($jkp['url_slug'], $forbidden_slug))) {
                $errors['e1'] = $jkl['hd211'];
            }

            if ($jkp['jak_prepage'] == "0") {
                $errors['e2'] = $jkl['e1'];
            }
                  
              if (count($errors) == 0) {

                if ($jkp['jak_prepage'] != "0") $jkp['jak_prepage'] = $jkp['url_slug'];

                  // Get the next order
                  $last = $jakdb->get($jaktable2, "dorder", ["opid" => $opcacheid, "ORDER" => ["dorder" => "DESC"]]);
                  $last = $last + 1;

                  $jakdb->insert($jaktable2, ["opid" => $opcacheid,
                    "lang" => $jkp['jak_lang'],
                "title" => $jkp['title'],
                "url_slug" => $jkp['url_slug'],
                "external_url" => $jkp['external_url'],
                "prepage" => $jkp['jak_prepage'],
                "dorder" => $last,
                "edited" => $jakdb->raw("NOW()"),
                "created" => $jakdb->raw("NOW()")]);

              $lastid = $jakdb->id();
              
                if (!$lastid) {
                  $_SESSION["infomsg"] = $jkl['i'];
                  jak_redirect($_SESSION['LCRedirect']);
                } else {

                  // Write the log file each time someone tries to login before
                      JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 29, $lastid, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);

                  $_SESSION["successmsg"] = $jkl['g14'];
                  jak_redirect($_SESSION['LCRedirect']);
                }
              
              // Output the errors
              } else {
              
                  $errors = $errors;
              }
          }

          if (isset($jkp['create_language_pack_cms'])) {

            if (isset($jkp['jak_lang_pack_cms']) && !empty($jkp['jak_lang_pack_cms']) && ($jakdb->has($jaktable2, ["AND" => ["opid" => $opcacheid, "lang[!]" => $jkp['jak_lang_pack_cms']]]) || !$jakdb->get($jaktable2, ["AND" => ["opid" => $opcacheid, "lang[!]" => $jkp['jak_lang_pack_cms']]]))) {

              // Database connection to the main site
                  $jakdb1 = new JAKsql([
                      // required
                      'database_type' => JAKDB_MAIN_DBTYPE,
                      'database_name' => JAKDB_MAIN_NAME,
                      'server' => JAKDB_MAIN_HOST,
                      'username' => JAKDB_MAIN_USER,
                      'password' => JAKDB_MAIN_PASS,
                      'charset' => 'utf8',
                      'port' => JAKDB_MAIN_PORT,
                      'prefix' => JAKDB_MAIN_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]
                      ]);


                // Insert the answers
                      $opa = $jakdb1->select("cms_pages", '*', ["locid" => JAK_MAIN_LOC]);
                      foreach ($opa as $rowcms) {
                          # code...
                          $jakdb->insert($jaktable2, [["opid" => $opcacheid, "lang" => $jkp['jak_lang_pack_cms'], "title" => $rowcms["title"], "url_slug" => $rowcms["url_slug"], "previmg" => $rowcms["ogimg"], "meta_keywords" => $rowcms["meta_keywords"], "meta_description" => $rowcms["meta_description"], "dorder" => $rowcms["dorder"], "showheader" => $rowcms["showheader"], "ishome" => $rowcms["ishome"], "showfooter" => $rowcms["showfooter"], "prepage" => $rowcms["prepage"], "access" => $rowcms["access"], "hits" => $rowcms["hits"], "active" => $rowcms["active"], "edited" => $jakdb->raw("NOW()"), "created" => $jakdb->raw("NOW()")]]);
                      }

              // Now let us delete the user cache file
              $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
              if (file_exists($cachestufffile)) {
                unlink($cachestufffile);
              }

              // Write the log file each time someone tries to login before
              JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 52, 0, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);

              $_SESSION["successmsg"] = $jkl['g14'];
              jak_redirect($_SESSION['LCRedirect']);

            } else {

              $_SESSION["infomsg"] = $jkl['i4'];
              jak_redirect($_SESSION['LCRedirect']);

            }
          }

          if (isset($jkp['change_url_slug'])) {

            if (empty($jkp['url_slug_client']) || (!preg_match('/^([a-z-_0-9]||[-_])+$/', $jkp['url_slug_client']) || in_array($jkp['url_slug_client'], $forbidden_slug)) && $jkp['url_slug_client'] != JAK_CLIENT_URL) {
                $errors['e3'] = $jkl['hd211']."<br>";
            }

            if (empty($jkp['url_slug_blog']) || (!preg_match('/^([a-z-_0-9]||[-_])+$/', $jkp['url_slug_blog']) || in_array($jkp['url_slug_blog'], $forbidden_slug)) && $jkp['url_slug_blog'] != JAK_BLOG_URL) {
                $errors['e4'] = $jkl['hd211']."<br>";
            }

            if (empty($jkp['url_slug_faq']) || (!preg_match('/^([a-z-_0-9]||[-_])+$/', $jkp['url_slug_faq']) || in_array($jkp['url_slug_faq'], $forbidden_slug)) && $jkp['url_slug_faq'] != JAK_FAQ_URL) {
                $errors['e5'] = $jkl['hd211']."<br>";
            }

            if (empty($jkp['url_slug_search']) || (!preg_match('/^([a-z-_0-9]||[-_])+$/', $jkp['url_slug_search']) || in_array($jkp['url_slug_search'], $forbidden_slug)) && $jkp['url_slug_search'] != JAK_SEARCH_URL) {
                $errors['e6'] = $jkl['hd211']."<br>";
            }

            if (empty($jkp['url_slug_support']) || (!preg_match('/^([a-z-_0-9]||[-_])+$/', $jkp['url_slug_support']) || in_array($jkp['url_slug_support'], $forbidden_slug)) && $jkp['url_slug_support'] != JAK_SUPPORT_URL) {
                $errors['e7'] = $jkl['hd211']."<br>";
            }

            if (empty($jkp['url_slug_contact']) || (!preg_match('/^([a-z-_0-9]||[-_])+$/', $jkp['url_slug_contact']) || in_array($jkp['url_slug_contact'], $forbidden_slug)) && $jkp['url_slug_contact'] != JAK_CONTACT_URL) {
                $errors['e8'] = $jkl['hd211']."<br>";
            }
                  
              if (count($errors) == 0) {

              if (JAK_CLIENT_URL != $jkp['url_slug_client']) {
                // We change the main entry in the settings
                $jakdb->update("settings", ["used_value" => $jkp['url_slug_client']], ["AND" => ["opid" => $opcacheid, "varname" => "client_url"]]);
                // We also change the main page
                $jakdb->update($jaktable2, ["prepage" => $jkp['url_slug_client'], "url_slug" => $jkp['url_slug_client']], ["AND" => ["opid" => $opcacheid, "prepage" => JAK_CLIENT_URL]]);
              }

                if (JAK_BLOG_URL != $jkp['url_slug_blog']) {
                // We change the main entry in the settings
                  $jakdb->update("settings", ["used_value" => $jkp['url_slug_blog']], ["AND" => ["opid" => $opcacheid, "varname" => "blog_url"]]);
                  // We also change the main page
                $jakdb->update($jaktable2, ["prepage" => $jkp['url_slug_blog'], "url_slug" => $jkp['url_slug_blog']], ["AND" => ["opid" => $opcacheid, "prepage" => JAK_BLOG_URL]]);
              }

                if (JAK_FAQ_URL != $jkp['url_slug_faq']) {
                // We change the main entry in the settings
                  $jakdb->update("settings", ["used_value" => $jkp['url_slug_faq']], ["AND" => ["opid" => $opcacheid, "varname" => "jak_faq_url"]]);
                  // We also change the main page
                $jakdb->update($jaktable2, ["prepage" => $jkp['url_slug_faq'], "url_slug" => $jkp['url_slug_faq']], ["AND" => ["opid" => $opcacheid, "prepage" => JAK_FAQ_URL]]);
              }

                if (JAK_SEARCH_URL != $jkp['url_slug_search']) {
                // We change the main entry in the settings
                  $jakdb->update("settings", ["used_value" => $jkp['url_slug_search']], ["AND" => ["opid" => $opcacheid, "varname" => "search_url"]]);
                  // We also change the main page
                $jakdb->update($jaktable2, ["prepage" => $jkp['url_slug_search'], "url_slug" => $jkp['url_slug_search']], ["AND" => ["opid" => $opcacheid, "prepage" => JAK_SEARCH_URL]]);
              }

                if (JAK_SUPPORT_URL != $jkp['url_slug_support']) {
                // We change the main entry in the settings
                  $jakdb->update("settings", ["used_value" => $jkp['url_slug_support']], ["AND" => ["opid" => $opcacheid, "varname" => "support_url"]]);
                  // We also change the main page
                $jakdb->update($jaktable2, ["prepage" => $jkp['url_slug_support'], "url_slug" => $jkp['url_slug_support']], ["AND" => ["opid" => $opcacheid, "prepage" => JAK_SUPPORT_URL]]);
              }

                if (JAK_CONTACT_URL != $jkp['url_slug_contact']) {
                // We change the main entry in the settings
                  $jakdb->update("settings", ["used_value" => $jkp['url_slug_contact']], ["AND" => ["opid" => $opcacheid, "varname" => "contact_url"]]);
                  // We also change the main page
                $jakdb->update($jaktable2, ["prepage" => $jkp['url_slug_contact'], "url_slug" => $jkp['url_slug_contact']], ["AND" => ["opid" => $opcacheid, "prepage" => JAK_CONTACT_URL]]);
              }
                

                // We have changed the standard url slugs.
                    JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 109, 1, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);

                    // Now let us delete the user cache file
              $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
              if (file_exists($cachestufffile)) {
                unlink($cachestufffile);
              }

                $_SESSION["successmsg"] = $jkl['g14'];
                jak_redirect($_SESSION['LCRedirect']);
              
              // Output the errors
              } else {
              
                  $errors = $errors;
              }

              // Nothing changed
              $_SESSION["infomsg"] = $jkl['i'];
              jak_redirect($_SESSION['LCRedirect']);
          }
            
        }

        // Call the language function
        $lang_files = jak_get_lang_files();

        // Get all pages
        if (!empty($page2) && in_array($page2, $lang_files)) {
          $PAGES_ALL = $jakdb->select($jaktable2, "*", ["AND" => ["opid" => $opcacheid, "lang" => $page2], "ORDER" => ["dorder" => "ASC"]]);
        } else {
          $PAGES_ALL = $jakdb->select($jaktable2, "*", ["AND" => ["opid" => $opcacheid, "lang" => JAK_LANG], "ORDER" => ["dorder" => "ASC"]]);
        }

        // Get only the not used language files
        $only_used_lang = $jakdb->select($jaktable2, "lang", ["opid" => $opcacheid, "GROUP" => "lang"]);
        $unique_lang = array_diff($lang_files, $only_used_lang);
        
        // Title and Description
        $SECTION_TITLE = $jkl["hd126"];
        $SECTION_DESC = "";

        // Include the javascript file for results
        $js_file_footer = 'js_cms.php';
         
        // Call the template
        $template = 'cms_pages.php';

    }
  break;
  case 'delete':
     
    // Check if user exists and can be deleted
    if (is_numeric($page2)) {
            
      // Now check how many languages are installed and do the dirty work
      $result = $jakdb->delete($jaktable, ["AND" => ["id" => $page2, "opid" => $opcacheid]]);
    
    if (!$result) {
        $_SESSION["infomsg"] = $jkl['i'];
        jak_redirect($_SESSION['LCRedirect']);
    } else {
      
      // Now let us delete the user cache file
      $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
      if (file_exists($cachestufffile)) {
        unlink($cachestufffile);
      }

      // Write the log file each time someone tries to login before
            JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 50, $page2, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);
      
        $_SESSION["successmsg"] = $jkl['g14'];
        jak_redirect($_SESSION['LCRedirect']);
    }
        
    } else {
        
         $_SESSION["errormsg"] = $jkl['i3'];
        jak_redirect($_SESSION['LCRedirect']);
    }
    
  break;
  case 'edit':
  
    // Check if the user exists
    if (is_numeric($page2) && jak_row_exist($page2, $opcacheid, $jaktable)) {
    
      if ($_SERVER['REQUEST_METHOD'] == 'POST') {
          $jkp = $_POST;
      
          if (empty($jkp['title'])) {
              $errors['e'] = $jkl['e2'];
          }
          
          if (empty($jkp['answer'])) {
              $errors['e1'] = $jkl['e1'];
          }
          
          // Let's check if we have a welcome message already in the same language
          if ($jkp['jak_msgtype'] != 1) {

          $rowa = $jakdb->get($jaktable, ["id", "title"], ["AND" => ["id[!]" => $page2, "opid" => $opcacheid, "department" => $jkp['jak_depid'], "lang" => $jkp['jak_lang'], "msgtype" => $jkp['jak_msgtype']]]);
              if ($rowa) {
                $errors['e2'] = sprintf($jkl['e25'], '<a href="'.JAK_rewrite::jakParseurl('answers', 'edit', $rowa["id"]).'">'.$rowa["title"].'</a>');
              }
          }
        
          if (count($errors) == 0) {
      
          $result = $jakdb->update($jaktable, ["department" => $jkp['jak_depid'],
          "lang" => $jkp['jak_lang'],
          "title" => $jkp['title'],
          "message" => jak_clean_safe_userpost($_REQUEST['answer']),
          "fireup" => $jkp['jak_fireup'],
          "msgtype" => $jkp['jak_msgtype']], ["AND" => ["id" => $page2, "opid" => $opcacheid]]);
      
          if (!$result) {
              $_SESSION["infomsg"] = $jkl['i'];
              jak_redirect($_SESSION['LCRedirect']);
          } else {
            
            // Now let us delete the user cache file
            $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
            if (file_exists($cachestufffile)) {
              unlink($cachestufffile);
            }

            // Write the log file each time someone tries to login before
                  JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 51, $page2, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);
            
              $_SESSION["successmsg"] = $jkl['g14'];
              jak_redirect($_SESSION['LCRedirect']);
          }
      
        // Output the errors
        } else {
            $errors = $errors;
        }
      
      }
    
      // Title and Description
      $SECTION_TITLE = $jkl["m21"];
      $SECTION_DESC = "";
      
      // Get all departments
      $JAK_DEPARTMENTS = $jakdb->select($jaktable1, ["id", "title"], ["opid" => $opcacheid, "ORDER" => ["dorder" => "ASC"]]);
      
      // Call the settings function
      $lang_files = jak_get_lang_files();
    
      $JAK_FORM_DATA = jak_get_data($page2, $opcacheid, $jaktable);

      // Include the javascript file for results
      $js_file_footer = 'js_editanswer.php';
      
      // Get the template
      $template = 'editanswer.php';
    
    } else {
        
         $_SESSION["errormsg"] = $jkl['i3'];
         jak_redirect(JAK_rewrite::jakParseurl('answers'));
    }
    
  break;
  default:
    
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $jkp = $_POST;

        if (isset($jkp['insert_answer'])) {
        
          if (empty($jkp['title'])) {
                  $errors['e'] = $jkl['e2'];
              }
              
              if (empty($jkp['answer'])) {
                  $errors['e1'] = $jkl['e1'];
              }
              
              // Let's check if we have a welcome message already in the same language
              if ($jkp['jak_msgtype'] != 1) {

                $rowa = $jakdb->get($jaktable, ["id", "title"], ["AND" => ["opid" => $opcacheid, "department" => $jkp['jak_depid'], "lang" => $jkp['jak_lang'], "msgtype" => $jkp['jak_msgtype']]]);

                if ($rowa) {
                  $errors['e2'] = sprintf($jkl['e25'], '<a href="'.JAK_rewrite::jakParseurl('answers', 'edit', $rowa["id"]).'">'.$rowa["title"].'</a>');
                }
            }
              
              if (count($errors) == 0) {

                $jakdb->insert($jaktable, ["opid" => $opcacheid,
                  "department" => $jkp['jak_depid'],
              "lang" => $jkp['jak_lang'],
              "title" => $jkp['title'],
              "message" => jak_clean_safe_userpost($_REQUEST['answer']),
              "fireup" => $jkp['jak_fireup'],
              "msgtype" => $jkp['jak_msgtype'],
              "created" => $jakdb->raw("NOW()")]);

            $lastid = $jakdb->id();
          
              if (!$lastid) {
                  $_SESSION["infomsg"] = $jkl['i'];
                jak_redirect($_SESSION['LCRedirect']);
              } else {
                
                // Now let us delete the user cache file
              $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
              if (file_exists($cachestufffile)) {
                unlink($cachestufffile);
              }

              // Write the log file each time someone tries to login before
                    JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 49, $lastid, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);
                
                  $_SESSION["successmsg"] = $jkl['g14'];
                jak_redirect($_SESSION['LCRedirect']);
              }
          
          // Output the errors
          } else {
          
              $errors = $errors;
          }
      }
        
        if (isset($jkp['create_language_pack'])) {

          if (isset($jkp['jak_lang_pack']) && !empty($jkp['jak_lang_pack']) && ($jakdb->has($jaktable, ["AND" => ["opid" => $opcacheid, "lang[!]" => $jkp['jak_lang_pack']]]) || !$jakdb->get($jaktable, ["AND" => ["opid" => $opcacheid, "lang[!]" => $jkp['jak_lang_pack']]]))) {

            // Database connection to the main site
              $jakdb1 = new JAKsql([
                  // required
                  'database_type' => JAKDB_MAIN_DBTYPE,
                  'database_name' => JAKDB_MAIN_NAME,
                  'server' => JAKDB_MAIN_HOST,
                  'username' => JAKDB_MAIN_USER,
                  'password' => JAKDB_MAIN_PASS,
                  'charset' => 'utf8',
                  'port' => JAKDB_MAIN_PORT,
                  'prefix' => JAKDB_MAIN_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]
                  ]);


            // Insert the answers
                  $opa = $jakdb1->select("answers", '*', ["locid" => JAK_MAIN_LOC]);
                  foreach ($opa as $rowa) {
                      # code...
                      $jakdb->insert($jaktable, [["opid" => $opcacheid, "department" => 0, "lang" => $jkp['jak_lang_pack'], "title" => $rowa["title"], "message" => $rowa["message"], "fireup" => $rowa["fireup"], "msgtype" => $rowa["msgtype"], "created" => $jakdb->raw("NOW()")]]);
                  }

          // Now let us delete the user cache file
          $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
          if (file_exists($cachestufffile)) {
            unlink($cachestufffile);
          }

          // Write the log file each time someone tries to login before
                JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 52, 0, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);
                
            $_SESSION["successmsg"] = $jkl['g14'];
            jak_redirect($_SESSION['LCRedirect']);

        } else {

          $_SESSION["infomsg"] = $jkl['i4'];
            jak_redirect($_SESSION['LCRedirect']);

        }
        }
    }
     
    // Get all departments
    $JAK_DEPARTMENTS = $jakdb->select($jaktable1, ["id", "title"], ["opid" => $opcacheid, "ORDER" => ["dorder" => "ASC"]]);
    
    // Call the language function
    $lang_files = jak_get_lang_files();

    // Get all answers
    if (!empty($page1) && in_array($page1, $lang_files)) {
      $ANSWERS_ALL = $jakdb->select($jaktable, "*", ["AND" => ["opid" => $opcacheid, "lang" => $page1], "ORDER" => ["id" => "ASC"]]);
    } else {
      $ANSWERS_ALL = $jakdb->select($jaktable, "*", ["AND" => ["opid" => $opcacheid, "lang" => JAK_LANG], "ORDER" => ["id" => "ASC"]]);
    }

    // Get only the not used language files
    $only_used_lang = $jakdb->select($jaktable, "lang", ["opid" => $opcacheid, "GROUP" => "lang"]);
    $unique_lang = array_diff($lang_files, $only_used_lang);
    
    // Title and Description
    $SECTION_TITLE = $jkl["m20"];
    $SECTION_DESC = "";
    
    // Include the javascript file for results
    $js_file_footer = 'js_pages.php';
     
    // Call the template
    $template = 'answers.php';
}
?>
