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

class Certs extends \Panel\Cert {

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

    parent::beforeRoute($f3);

    if ($f3->exists('PARAMS.cert')) {
      $cert = $f3->get('PARAMS.cert');
      if ($cert_array = $f3->call('\Panel::vGet', array("letsencrypt-get.sh -d $cert -c", FALSE))) {
        $f3->set('cert_array', $cert_array[0]);
      }
    } else {
      if ($certs_array = $f3->call('\Panel::vGet', array("letsencrypt-get.sh -c", FALSE))) {
        $f3->set('certs_array', $certs_array);
      }
    }

  }

  static function get($f3) {

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

      $cert_array = $f3->get('cert_array');
      /* remove time from creation date */
      $start =  $cert_array['start'];
      $start_array = explode(' ', $start);
      unset($start_array[2]);
      $start = implode(' ', $start_array);
      $cert_array['start'] = $start;
      /* remove time from expiration date */
      $end =  $cert_array['end'];
      $end_array = explode(' ', $end);
      unset($end_array[2]);
      $end = implode(' ', $end_array);
      $cert_array['end'] = $end;
      /* remove main cert name from alternatives */
      // $common =  $cert_array['common'];
      $alternative = $cert_array['alternative'];
      // $alternative = preg_replace("/^$common/", '', $alternative);
      // $alternative = trim($alternative);
      $alternative = preg_replace('/ /', '<br>', $alternative);
      $cert_array['alternative'] = $alternative;
      $f3->set('cert_array', $cert_array);

      $cert = $f3->get('PARAMS.cert');
      $f3->set('page_header', "Certificate Details for $cert");
      echo \Template::instance()->render('cert/certs-cert.html');

    } else {

      $certs_array = $f3->get('certs_array');
      if (is_array($certs_array) && count($certs_array) > 0) {
        foreach ($certs_array as $k=>$cert_array) {
          /* remove time from expiration date */
          $end =  $cert_array['end'];
          $end_array = explode(' ', $end);
          unset($end_array[2]);
          $end = implode(' ', $end_array);
          $cert_array['end'] = $end;
          /* remove main cert name from alternatives */
          // $common =  $cert_array['common'];
          $alternative = $cert_array['alternative'];
          // $alternative = preg_replace("/^$common/", '', $alternative);
          // $alternative = trim($alternative);
          $alternative = preg_replace('/ /', '<br>', $alternative);
          $cert_array['alternative'] = $alternative;
          $certs_array[$k] = $cert_array;
        }
        $f3->set('certs_array', $certs_array);
      }

      $f3->set('page_header', "Certificates");
      echo \Template::instance()->render('cert/certs.html');

    }

  }

}