<?php

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 6 May 1998 03:10:00 GMT");

/*===============================================*\
|| ############################################# ||
|| # JAKWEB.CH / Version 1.2                   # ||
|| # ----------------------------------------- # ||
|| # Copyright 2021 JAKWEB All Rights Reserved # ||
|| ############################################# ||
\*===============================================*/

if (!file_exists('config.php')) die('rest_api config.php not exist');
require_once 'config.php';

$userid = $loginhash = $contactid = "";
$errors = $rowi = array();
$sendform = false;
if (isset($_REQUEST['userid']) && !empty($_REQUEST['userid']) && is_numeric($_REQUEST['userid'])) $userid = $_REQUEST['userid'];
if (isset($_REQUEST['loginhash']) && !empty($_REQUEST['loginhash'])) $loginhash = $_REQUEST['loginhash'];
if (isset($_REQUEST['contactid']) && !empty($_REQUEST['contactid'])) $contactid = $_REQUEST['contactid'];
if (isset($_REQUEST['sendform']) && !empty($_REQUEST['sendform'])) $sendform = $_REQUEST['sendform'];

if (!empty($userid) && !empty($loginhash)) {

  // Let's check if we are logged in
  $usr = $jakuserlogin->jakCheckrestlogged($userid, $loginhash);

  if ($usr) {

    // Get the user fields
    $jakuser = new JAK_user($usr);

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

    // Cache stuff
    if (file_exists(APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php')) include_once APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';

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

    if (!empty($contactid)) $rowi = $jakdb->get("contacts", ["name", "email", "message", "latitude", "longitude"], ["id" => $contactid]);

    if ($sendform && !empty($contactid)) {

      if (empty($_REQUEST['subject'])) {
          $errors['subject'] = true;
      }

      if (empty($_REQUEST['message'])) {
          $errors['message'] = true;
      }

      if (count($errors) == 0) {

        // Ok, we send the email // email address, cc email address, reply to, subject, message, attachment
            if (jak_send_email($rowi['email'], "", "", trim($_REQUEST['subject']), trim(nl2br($_REQUEST['message'])), "")) {

            // Get the operator details
            $jakuser = new JAK_user($usr);
            
              // Insert the stuff into the database
              $jakdb->insert("contactsreply", [ 
              "contactid" => $contactid,
              "operatorid" => $jakuser->getVar("id"),
              "operatorname" => $jakuser->getVar("username"),
              "subject" => trim($_REQUEST['subject']),
              "message" => trim($_REQUEST['message']),
              "sent" => $jakdb->raw("NOW()")]);
              
              $jakdb->update("contacts", ["reply" => 1, "answered" => $jakdb->raw("NOW()")], ["id" => $contactid]);

              // Write the log file
                  JAK_base::jakWhatslog('', $opcacheid, $userid, 0, 32, $lastid, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);

              // Form has been sent, let's send the success status
          die(json_encode(array('status' => true)));

          }

      } else {
        die(json_encode(array('status' => false, 'errors' => $errors)));
      }

    }

    // Display the message content and location from the client
    die(json_encode(array('status' => true, 'data' => $rowi)));

  } else {
    die(json_encode(array('status' => false, 'errorcode' => 1, 'errorcode' => false)));
  }
}

die(json_encode(array('status' => false, 'errorcode' => 7, 'errorcode' => false)));
?>
