<?php
ob_start();

include '../includes/session.php';
include '../includes/constants.php';
include '../includes/functions.php';
include '../includes/allstripslashes.php';
include '../includes/function1.php';

$partners = new partners;
$partners->connection($host, $user, $pass, $db);
$conn = $partners->conn;

$userobj = new adminuser();
$adminUserId = $_SESSION['ADMINUSERID'];

$url = "index.php?Act=options";

switch ($action) {
    case 'Modify Mail':
        $mail = $_POST['mail'];
        if (!$partners->is_email($mail)) {
            $msg = "adopt2000";
        } else {
            if (!$userobj->IsEmailexists($mail, $adminUserId)) {
                $msg = "adopt2014";
                break;
            }

            if ($_SESSION['ADMINUSERID'] == '1') {
                $stmt = $conn->prepare("UPDATE partners_admin SET admin_email = ?");
                $stmt->bind_param("s", $mail);
                $stmt->execute();
                $stmt->close();
            }

            $stmt = $conn->prepare("UPDATE partners_adminusers SET adminusers_email = ? WHERE adminusers_id = ?");
            $stmt->bind_param("si", $mail, $_SESSION['ADMINUSERID']);
            if ($stmt->execute()) {
                $msg = "adopt2001";
                $_SESSION['MAIL'] = $mail;
            } else {
                $msg = "adopt2002";
            }
            $stmt->close();
        }
        break;

    case 'Modify Login':
        $login = stripslashes(trim($_POST['login']));
        if (empty($login) || strlen($login) > 15) {
            $msg = "adopt2003";
        } else {
            if (!$userobj->IsUserNameExists($login, $adminUserId)) {
                $msg = "adopt2015";
                break;
            }

            $login1 = addslashes($login);

            if ($_SESSION['ADMINUSERID'] == '1') {
                $stmt = $conn->prepare("UPDATE partners_admin SET admin_login = ?");
                $stmt->bind_param("s", $login1);
                $stmt->execute();
                $stmt->close();
            }

            $stmt = $conn->prepare("UPDATE partners_adminusers SET adminusers_login = ? WHERE adminusers_id = ?");
            $stmt->bind_param("si", $login1, $_SESSION['ADMINUSERID']);
            if ($stmt->execute()) {
                $msg = "adopt2004";
                $_SESSION['ADMIN'] = stripslashes($login);
            } else {
                $msg = "adopt2002";
            }
            $stmt->close();
        }
        break;

    case 'Modify Password':
        $pass1 = trim($_POST['pass1']);
        $pass2 = trim($_POST['pass2']);
        $pass3 = trim($_POST['pass3']);

        if (empty($pass1) || strlen($pass1) > 15 || empty($pass2) || strlen($pass2) > 15 || empty($pass3) || strlen($pass3) > 15) {
            $msg = "adopt2005";
        } elseif ($pass2 !== $pass3) {
            $msg = "adopt2006";
        } else {
            $stmt = $conn->prepare("SELECT adminusers_password FROM partners_adminusers WHERE adminusers_id = ?");
            $stmt->bind_param("i", $_SESSION['ADMINUSERID']);
            $stmt->execute();
            $stmt->bind_result($hashedPassword);
            $stmt->fetch();
            $stmt->close();

            if (!$hashedPassword || !password_verify($pass1, $hashedPassword)) {
                $msg = "adopt2007";
            } else {
                $newHashedPassword = password_hash($pass2, PASSWORD_DEFAULT);

                if ($_SESSION['ADMINUSERID'] == '1') {
                    $stmt = $conn->prepare("UPDATE partners_admin SET admin_password = ?");
                    $stmt->bind_param("s", $newHashedPassword);
                    $stmt->execute();
                    $stmt->close();
                }

                $stmt = $conn->prepare("UPDATE partners_adminusers SET adminusers_password = ? WHERE adminusers_id = ?");
                $stmt->bind_param("si", $newHashedPassword, $_SESSION['ADMINUSERID']);
                if ($stmt->execute()) {
                    $msg = "adopt2008";
                } else {
                    $msg = "adopt2002";
                }
                $stmt->close();
            }
        }
        break;

    case 'Change Title':
        $new_title = stripslashes(trim($_POST['new_title']));
        if (!empty($new_title)) {
            $filename = "../includes/constants.php";
            $contents = file_get_contents($filename);

            $pattern = '/\$title\s*=\s*[\'"](.*?)[\'"];/';
            $replacement = "\$title = '$new_title';";
            $contents = preg_replace($pattern, $replacement, $contents);

            file_put_contents($filename, $contents);
            $msg = "adopt2009";
        } else {
            $msg = "adopt2010";
        }
        break;

    case 'Change Lines/Page':
        $number = trim($_POST['number']);
        if ($number) {
            if (is_numeric($number)) {
                $filename = "../includes/constants.php";
                $contents = file_get_contents($filename);

                $pattern = '/\$lines\s*=\s*[\'"]?(\d+)[\'"]?;/';
                $replacement = "\$lines = $number;";
                $contents = preg_replace($pattern, $replacement, $contents);

                file_put_contents($filename, $contents);
                $msg = "adopt2011";
            } else {
                $msg = "adopt2012";
            }
        } else {
            $msg = "adopt2010";
        }
        break;

    case 'Error Page Setting':
        $content = stripslashes($_POST['error']);
        if ($content) {
            $filename = "error.htm";
            $content = str_replace(["<?php", "<?", "?>"], "", $content);
            file_put_contents($filename, $content);
            $msg = "adopt2013";
        } else {
            $msg = "adopt2010";
        }
        break;
}

header("Location: $url&msg=$msg");
exit;
?>
