<?php
/**
 *  Copyright (C) 2021  Double Bastion LLC
 *
 *  This file is part of Roundpin, which is licensed under the
 *  GNU Affero General Public License Version 3.0. The license terms
 *  are detailed in the "LICENSE.txt" file located in the root directory.
 */

session_start();
 // header('Set-Cookie: PHPSESSID= ' . session_id() . '; SameSite=strict; Secure=true; HttpOnly=true;');

if (isset($_POST['s_ajax_call']) && ($_POST['s_ajax_call'] == $_SESSION['validate_s_access']) && isset($_POST['sipusername']) && $_POST['sipusername'] != '' &&
    isset($_POST['sipuserrole']) && $_POST['sipuserrole'] != '') {

    $sipUser = $_POST['sipusername'];
    $sipUserRole = $_POST['sipuserrole'];

    if (isset($_POST['vidconfextension']) && $_POST['vidconfextension'] != '') {

        $vidConfExtensionList = $_POST['vidconfextension'];
        $vidConfExtensionArr = explode(",", $vidConfExtensionList);

    } else { $vidConfExtensionArr = []; }

    if (isset($_POST['textconfextension']) && $_POST['textconfextension'] != '') {

        $textConfExtensionList = $_POST['textconfextension'];
        $textConfExtensionArr = explode(",", $textConfExtensionList);

    } else { $textConfExtensionArr = []; }

    if ($sipUserRole == 'regular_user' || $sipUserRole == 'admin') {

        if (is_dir('../textchat/' . $sipUser . '/uploads')) {

            try { 
              array_map('unlink', glob('../textchat/' . $sipUser . '/uploads/*'));
              if (glob('../textchat/' . $sipUser . '/uploads/*')) { throw new Exception('Directory not empty'); } else { throw new Exception(''); }
            } catch (Exception $e) { if ($e->getMessage() == '') { $cmessage = 'success'; } else { $cmessage = 'failure'; } }

        } else { $cmessage = 'success'; }

        if (is_dir('../textchat/' . $sipUser . 'vconf' . '/uploads')) {

            try { 
              array_map('unlink', glob('../textchat/' . $sipUser . 'vconf' . '/uploads/*'));
              if (glob('../textchat/' . $sipUser . 'vconf' . '/uploads/*')) { throw new Exception('Directory not empty'); } else { throw new Exception(''); }
            } catch (Exception $e) { if ($e->getMessage() == '') { $cmessage = 'success'; } else { $cmessage = 'failure'; } }

        } else { $cmessage = 'success'; }

    } elseif ($sipUserRole == 'superadmin') {

        if (is_dir('../textchat/' . $sipUser . '/uploads')) {

            try { 
              array_map('unlink', glob('../textchat/' . $sipUser . '/uploads/*'));
              if (glob('../textchat/' . $sipUser . '/uploads/*')) { throw new Exception('Directory not empty'); } else { throw new Exception(''); }
            } catch (Exception $e) { if ($e->getMessage() == '') { $partcmessage = 'success'; } else { $partcmessage = 'failure'; } }

        } else { $partcmessage = 'success'; }

        if (is_dir('../textchat/' . $sipUser . 'vconf' . '/uploads')) {

            try { 
              array_map('unlink', glob('../textchat/' . $sipUser . 'vconf' . '/uploads/*'));
              if (glob('../textchat/' . $sipUser . 'vconf' . '/uploads/*')) { throw new Exception('Directory not empty'); } else { throw new Exception(''); }
            } catch (Exception $e) { if ($e->getMessage() == '') { $partcmessage = 'success'; } else { $partcmessage = 'failure'; } }

        } else { $partcmessage = 'success'; }

        if (!empty($vidConfExtensionArr)) {

            $vidcmessagecheck = 0;

            for ($i = 0; $i < count($vidConfExtensionArr); $i++) {

                 if (is_dir('../textchat/' . $vidConfExtensionArr[$i] . '/uploads')) {

		     try {
		       array_map('unlink', glob('../textchat/' . $vidConfExtensionArr[$i] . '/uploads/*'));
		       if (glob('../textchat/' . $vidConfExtensionArr[$i] . '/uploads/*')) { throw new Exception('Directory not empty'); } else { throw new Exception(''); }
		     } catch (Exception $e) { if ($e->getMessage() != '') { ++$vidcmessagecheck; } }
                 }
            }

            if ($vidcmessagecheck == 0) { $vidcmessage = 'success'; } else { $vidcmessage = 'failure'; }

        } else { $vidcmessage = 'success'; }

        if (!empty($textConfExtensionArr)) {

            $textcmessagecheck = 0;

            for ($k = 0; $k < count($textConfExtensionArr); $k++) {

                 if (is_dir('../textchat/' . $textConfExtensionArr[$k] . '/uploads')) {

                     try { 
                       array_map('unlink', glob('../textchat/' . $textConfExtensionArr[$k] . '/uploads/*'));
                       if (glob('../textchat/' . $textConfExtensionArr[$k] . '/uploads/*')) { throw new Exception('Directory not empty'); } else { throw new Exception(''); }
                     } catch (Exception $e) { if ($e->getMessage() != '') { ++$textcmessagecheck; } }
                 }
            }

            if ($textcmessagecheck == 0) { $textcmessage = 'success'; } else { $textcmessage = 'failure'; }

        } else { $textcmessage = 'success'; }

        if ($partcmessage == 'success' && $vidcmessage == 'success' && $textcmessage == 'success') { $cmessage = 'success'; } else { $cmessage = 'failure'; }

    } else { $cmessage = 'failure'; }

    $resresult = ["note" => $cmessage];

    echo json_encode($resresult);

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

?>