<?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 Users extends \Panel\Vhost {

  /* use this to make query */
  function beforeRoute($f3) {

    parent::beforeRoute($f3);

    if ($f3->exists('PARAMS.username')) {
      $username = $f3->get('PARAMS.username');
      if ($users_array = $f3->call('\Panel::vGet', array("vhost-user-get.sh -u $username -c", FALSE))) {
        if ($users_array[0]['passwd'] == "") {
          $users_array[0]['passwd'] = '(unavailable)';
        }
        $f3->set('users_array', $users_array[0]);
        if ($vhosts_array = $f3->call('\Panel::vGet', array("vhost-get.sh -u $username -c", FALSE))) {
          $f3->set('vhosts_array', $vhosts_array);
        }
      }
    } else {
      if ($users_array = $f3->call('\Panel::vGet', array("vhost-user-get.sh -c", FALSE))) {
        if (is_array($users_array) && count($users_array) > 0) {
          foreach ($users_array as $k=>$user_array) {
            if ($user_array['passwd'] == "") {
              $users_array[$k]['passwd'] = '(unavailable)';
            }
          }
          $f3->set('users_array', $users_array);
        }
      }
    }

  }

  static function get($f3) {

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

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

    } else {

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

    }

  }

}