<?php

/**
 * vpanel-stack
 * https://git.stack-source.com/msb/vpanel-stack
 * Copyright (c) 2022 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
 * GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
 */

namespace Panel\Vhost;

class UsersEdit extends \Panel\Vhost {

  function beforeRoute($f3) {

    parent::beforeRoute($f3);
    $username = $f3->get('PARAMS.username');
    if ($user_array = $f3->call('\Panel::vGet', array("vhost-user-get.sh -u $username -c", FALSE))) {
      if ($user_array[0]['passwd'] == "") {
          $user_array[0]['passwd'] = '(unavailable)';
      }
      $f3->set('user_array', $user_array[0]);
    }

  }

  static public function get($f3) {

    if ($f3->exists('PARAMS.username')) {

      $username = $f3->get('PARAMS.username');
      $f3->set('page_header', "Edit User: $username");
      echo \Template::instance()->render('vhost/users-edit.html');

    } else {

      $f3->set('page_header', "Users");
      echo \Template::instance()->render('vhost/users.html');

    }

  }

  function post($f3) {

    $username = $f3->get('PARAMS.username');
    $action = $_POST['action'];

    if ($action == 'password') {
      $password = $_POST['password'];
      $password_confirm = $_POST['password_confirm'];
      $f3->call('\Panel::validatePassword', array($password, $password_confirm));
      /* check for validation errors */
      if ($f3->exists('SESSION.messages')) {
        $messages = $f3->get('SESSION.messages');
      } else {
        $password = escapeshellarg($password);
        if ($f3->get('WRITEUSERINFO') == '1') {
          exec("/usr/local/bin/vhost-user-mod.sh -u $username -p $password -w", $output, $result_code);
        } else {
          exec("/usr/local/bin/vhost-user-mod.sh -u $username -p $password", $output, $result_code);
        }
        if ($result_code == 0) {
          $messages[] = "Success: Password for $username updated.";
        } else {
          $messages[] = "Error updating password.";
        }
      }
    } elseif ($action == 'jail') {
      exec("/usr/local/bin/vhost-user-jail.sh -u $username >/dev/null 2>/dev/null &", $output, $result_code);
      $messages[] = "User is being jailed. Note: Setting up the jail environment takes about a minute to complete and is run in the background now.";
      $f3->reroute("/Users/$username");
    } elseif ($action == 'fpmmax') {
      $fpmmax = $_POST['fpmmax'];
      exec("/usr/local/bin/vhost-user-mod.sh -u $username -x $fpmmax", $output, $result_code);
      if ($result_code == 0) {
        $messages[] = "Success: PHP Workers updated.";
      } else {
        $messages[] = "Error updating PHP Workers.";
      }
    } else {
      $messages[] = "Unkown edit action.";
    }

    $f3->set('SESSION.messages', $messages);
    $f3->reroute("/Users/$username");


    // /* run mod command here */
    // $mbox = $f3->get('PARAMS.mbox');
    // $domain = $f3->get('PARAMS.domain');
    // $mbox_array = $_POST;
    // $f3->set('mbox_array', $mbox_array);
    // foreach ($mbox_array as $k=>$v) {
    //   if (strtolower($v) == 'unlimited') {
    //     $mbox_array[$k] = 'NULL';
    //   }
    // }
    // extract($mbox_array);
    //
    // if ($password != '') {
    //   $f3->call('\Panel::validatePassword', array($password, $password_confirm));
    // }
    // settype($status, "integer");
    // $f3->call('\Panel::validateEmailStatus', $status);
    // if (strtolower($quota == 'unlimited') || strtolower($quota == 'null')) {
    //   $quota = "NULL";
    // } else {
    //   settype($quota, "integer");
    // }
    // $f3->call('\Panel::validateEmailQuota', $quota);
    // if (strtolower($ratelimit == 'unlimited') || strtolower($ratelimit == 'null')) {
    //   $ratelimit = "NULL";
    // }
    // $f3->call('\Panel::validateEmailRatelimit', $ratelimit);
    // $f3->call('\Panel::validateEmailFiltering', $filter);
    //
    // /* check for validation errors */
    // if ($f3->exists('SESSION.messages')) {
    //   $messages = $f3->get('SESSION.messages');
    //   $messages[] = "Please make changes and re-submit the form to try again.";
    //   $f3->set('SESSION.messages', $messages);
    //   $f3->call('\Panel\Vmail\MboxesEdit::get', $f3);
    // } else {
    //   if ($password != '') {
    //     $password = escapeshellarg($password);
    //     $pword_cmd = "-p $password";
    //   } else {
    //     $pword_cmd = '';
    //   }
    //   exec("/usr/local/bin/vmail-mboxes-mod.sh -e $mbox@$domain $pword_cmd -q $quota -r $ratelimit -s $status -j $filter", $output, $result_code);
    //   if ($result_code == 0) {
    //     $messages[] = "Success: Email account $mbox@$domain updated.";
    //     $f3->set('SESSION.messages', $messages);
    //     $mapping = $f3->get('NAV.mapping');
    //     if ($mapping == 'vmail') {
    //       $f3->reroute("/Accounts/$mbox");
    //     } else {
    //       $f3->reroute("/Email/$domain/Accounts/$mbox");
    //     }
    //   } else {
    //     $messages[] = "Error.";
    //     $f3->set('SESSION.messages', $messages);
    //     $f3->call('\Panel\Vmail\MboxesEdit::get', $f3);
    //   }
    // }

  }

}