<?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\Vmail;

class DomainsAdd extends \Panel\Vmail {

  function beforeRoute($f3) {

    parent::beforeRoute($f3);

    /* get vm_domains defaults for "add new" form */
    $vm_domains_defaults = $f3->call('\Panel::vGet', array("vmail-defaults-get.sh -c", FALSE));
    $f3->set('vm_domains_defaults', $vm_domains_defaults[0]);

  }

  static function get($f3) {

    echo \Template::instance()->render('vmail/domains-add.html');

  }

  function post($f3) {

    extract($_POST);

    $messages = array();

    /* validate domain */
    if (preg_match('/^[0-9a-z]([-.]?[0-9a-z])*\.[a-z]{2,24}$/i', strtolower($_POST['domain']))) {
      $domain = strtolower($_POST['domain']);
    } else {
      $messages[] = "Invalid domain name.";
    }

    /* validate status */
    if ($_POST['status'] != 0 && $_POST['status'] != 1) {
      $messages[] = "Invalid 'Status'.";
    } else {
      $status = $_POST['status'];
    }

    /* validate mbox_limit */
    if (strtolower($_POST['mbox_limit']) == 'unlimited' || strtolower($_POST['mbox_limit'] == 'null')) {
      $mbox_limit = "NULL";
    } elseif (is_numeric($_POST['mbox_limit'])) {
      /* make sure mbox_limit is a possitive integer */
      $mbox_limit = abs(intval($_POST['mbox_limit']));
      if ($mbox_limit < 1) {
        echo "Mailbox Limit must be a positive number or \"Unlimited\".\n";
        $messages[] = "Mailbox Limit must be a positive number or \"Unlimited\".";
      }
    } else {
      $messages[] = "Mailbox Limit must be a positive number or \"Unlimited\".";
    }

    /* validate mbox_quota_default */
    if (strtolower($_POST['mbox_quota_default']) == 'unlimited' || strtolower($_POST['mbox_quota_default'] == 'null')) {
      $mbox_quota_default = "NULL";
    } elseif (is_numeric($_POST['mbox_quota_default'])) {
      /* make sure mbox_quota_default is a possitive integer */
      $mbox_quota_default = abs(intval($_POST['mbox_quota_default']));
      if ($mbox_quota_default < 1) {
        echo "Default Quota must be a positive number or \"Unlimited\".\n";
        $messages[] = "Default Quota must be a positive number or \"Unlimited\".";
      }
    } else {
      $messages[] = "Default Quota must be a positive number or \"Unlimited\".";
    }

    /* validate mbox_ratelimit_default */
    if (strtolower($_POST['mbox_ratelimit_default']) == 'unlimited' || strtolower($_POST['mbox_ratelimit_default'] == 'null')) {
      $mbox_ratelimit_default = "NULL";
    } elseif (is_numeric($_POST['mbox_ratelimit_default'])) {
      /* make sure mbox_ratelimit_default is a possitive integer */
      $mbox_ratelimit_default = abs(intval($_POST['mbox_ratelimit_default']));
      if ($mbox_ratelimit_default < 1) {
        $messages[] = "Default Rate Limit must be a positive number or \"Unlimited\".";
      }
    } else {
      $messages[] = "Default Rate Limit must be a positive number or \"Unlimited\".";
    }

    /* check for validation errors */
    if (count($messages) > 0) {
      $messages[] = "Please re-submit the form to try again.";
      $f3->set('SESSION.messages', $messages);
      $f3->call('\Panel\Vmail\DomainsAdd::get', $f3);
    } else {
      /* check if vmail domain already exists */
      $domain_array = $f3->call('\Panel::vGet', array("vmail-domains-get.sh -d $domain -c", FALSE));
      if (count($domain_array) == 0) {
        /* add email domain */
        exec("/usr/local/bin/vmail-domains-add.sh -d $domain -l $mbox_limit -q $mbox_quota_default -r $mbox_ratelimit_default -s $status", $output, $result_code);
        if ($result_code == 0) {
          $messages[] = "Email Domain $domain has been added.";
          $f3->set('SESSION.messages', $messages);
          $f3->reroute("/Email/$domain");
        } else {
          if (count($output) > 0) {
            foreach ($output as $k=>$output_message) {
              $messages[] = "$output_message";
            }
          } else {
            $messages[] = "Unknown error adding Email Domain $domain.";
          }
          $f3->set('SESSION.messages', $messages);
          $f3->call('\Panel\Vmail\DomainsAdd::get', $f3);
        }
      } else {
        $messages[] = "Email Domain '$domain' already exists.";
        $f3->set('SESSION.messages', $messages);
        $f3->reroute("/Email/$domain");
      }
    }
  }

}