<?php

/**
 * @copyright 2024 Double Bastion LLC <www.doublebastion.com>
 *
 * @author Double Bastion LLC
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

session_start();

if ($_SESSION['loggedtorspanel'] == true) {

define('ACCESSCONST', TRUE);

require('db-connect.php');

  if (isset($_POST['newdir'])) {

     $newdirpath = $_POST['newdir'];

     if ($newdirpath != '') {

        if (is_dir($newdirpath)) {

           try {

             $query0 = $mysqli->prepare("UPDATE detectionsdir SET detdir=? WHERE id=1");
             $query0->bind_param("s", $newdirpath);
             $query0->execute();
             $query0->close();

             $result = 'success';
             $messagetosend = "The new directory has been saved !";

           } catch (mysqli_sql_exception $e) {
                $result = 'failure';
                $messagetosend = "An error occurred while processing your request. You can try again after a few moments !";
           }

        } else {
                $result = 'failure';
                $messagetosend = "The directory that you entered doesn't exist on the server. Please enter the full path of an existing directory !";
        }

     } else {
                $result = 'failure';
                $messagetosend = "Please enter the full path of an existing directory !";
     }

     $resp = array('result' => $result, 'messagetosend' => $messagetosend);
     echo json_encode($resp);
  }

} else {
     header("Location: panel-login.php");
}

?>