1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 
<?php declare(strict_types=1);

    /**
     * Copyright (C) Apis Networks, Inc - All Rights Reserved.
     *
     * Unauthorized copying of this file, via any medium, is
     * strictly prohibited without consent. Any dissemination of
     * material herein is prohibited.
     *
     * For licensing inquiries email <licensing@apisnetworks.com>
     *
     * Written by Matt Saladna <matt@apisnetworks.com>, June 2019
     */

    use Opcenter\SiteConfiguration;

    /**
     * Grant access to info/ helpers
     */
    trait AccountInfoTrait {
        use ContextableTrait;


        /**
         * Get all service configuration
         *
         * @return array
         */
        protected function getServices(): array
        {
            return $this->getAuthContext()->conf->conf;
        }

        /**
         * Get unsynchronized configuration
         *
         * @param string $service
         * @return array|null
         */
        protected function getNewServices(string $service = null): ?array
        {
            $s = $this->getAuthContext()->conf->new;
            if ($service) {
                return $s[$service] ?? null;
            }

            return $s;
        }

        /**
         * Get previous configuration
         *
         * @param string $service
         * @return array|null
         */
        protected function getOldServices(string $service = null): ?array
        {
            $s = $this->getAuthContext()->conf->old;
            if ($service) {
                return $s[$service] ?? null;
            }

            return $s;
        }

        /**
         * Get current service configuration
         *
         * @param string|null $svc
         * @return array|null
         */
        protected function getActiveServices(string $svc = null): ?array
        {
            $s = $this->getAuthContext()->conf->cur;
            if ($svc) {
                return $s[$svc] ?? null;
            }

            return $s;
        }

        /**
         * Get service configuration
         *
         * Alias to getServiceValue($service, $parameter)
         *
         * @param string      $service
         * @param string|null $parameter
         * @return mixed
         */
        protected function getConfig(string $service, string $parameter = null)
        {
            return $this->getServiceValue($service, $parameter);
        }

        /**
         * mixed get_service_value(string, string)
         * Get site configuration value from <FST>/info/current
         *
         * @param string $service   service type to look up data
         * @param string $parameter|null name of the service type subset to return data
         * @param mixed  $default   default value
         * @return mixed
         */
        protected function getServiceValue(string $service, string $parameter = null, $default = null)
        {
            if ($this->getAuthContext()->level & PRIVILEGE_ADMIN) {
                return $default;
            }
            $conf = $this->getAuthContext()->conf->cur[$service] ?? $this->getAuthContext()->conf->new[$service] ?? null;

            if (null === $conf) {
                // newer platform remaps
                $remap = array_get(SiteConfiguration::MODULE_REMAP, $service);
                if ($remap && $remap !== $service) {
                    return $this->getServiceValue($remap, $parameter, $default);
                }
                return $default;
            }

            if (!$parameter) {
                return $conf;
            }

            if (!isset($conf[$parameter])) {
                return $default;
            }
            $val = $conf[$parameter];

            return $val;
        }

        /**
         *  Journals data to <site config>/info/new/<$svc_name>
         *  Invokes reconfigure hook when EditVirtDomain is used
         *
         * @param string $service
         * @param string $parameter
         * @param mixed $value
         * @return Auth_Info_Account
         */
        protected function setConfigJournal(string $service, string $parameter, $value): \Auth_Info_Account
        {
            return $this->setConfig($service, $parameter, $value, true);
        }

        /**
         *  Writes out configuration immediately to <site config>/info/current/<$svc_name>
         *  *Does not* invoke reconfigure hook when EditVirtDomain is used
         *
         * @param string $service service
         * @param string $parameter service parameter
         * @param mixed  $value service value
         * @param bool   $journal write to unsynchronized journal (new/)
         * @return Auth_Info_Account
         */
        protected function setConfig(string $service, string $parameter, $value, bool $journal = false): \Auth_Info_Account
        {
            $conf = $this->getAuthContext()->conf;
            // hook into \Auth_Info_Account()
            $conf->change($service, array($parameter => $value), $journal);
            if ($journal) {
                $new = $conf->new;
            } else {
                $new = $conf->cur;
            }
            \DataStream::get($this->getAuthContext())->query('common_save_service_information_backend', $new, $journal);
            return $this->getAuthContext()->reset();
        }
    }