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: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 
<?php
    /**
     * 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>, May 2017
     */

    namespace Module\Skeleton;

    use Module\Skeleton\Contracts\Proxied;

    /**
     * Class Module_Skeleton
     */
    abstract class Standard implements \Module\Definition\MODULE30
    {
        use \apnscpFunctionInterceptorTrait;
        use \FilesystemPathTrait;
        use \ContextableTrait;
        const DEPENDENCY_MAP = ['siteinfo'];

        protected $exportedFunctions = array();

        protected $username;
        protected $password;
        protected $domain;
        protected $user_id;
        protected $group_id;
        protected $permission_level;
        protected $session_id;
        protected $site;
        protected $site_id;
        protected $pgsql;
        protected $mysql;

        public function __construct()
        {
            $this->initializeUser();
        }

        public function __wakeup()
        {
            // refresh account meta if necessary
            // failure to do so in contexted instances will yield stale metadata
            if (\Auth::profile()->id === $this->session_id) {
                if (\session_id() !== $this->session_id) {
                    fatal("session logic mismatch");
                }
                $this->setContext(\Auth::profile());
            } else if ($this->inContext()) {
                $this->getAuthContext()->reset();
                $this->setContext($this->getAuthContext());
            }
            $this->initializeUser();
        }

        private function initializeUser()
        {
            if ($this->inContext()) {
                return $this->setUserParameters($this->getAuthContext(), \apnscpFunctionInterceptor::factory($this->getAuthContext()));
            }
            if (!IS_CLI && $_SESSION) {
                $this->group_id = (int)$_SESSION['group_id'];
                $this->user_id =  (int)$_SESSION['user_id'];
                $this->username = $_SESSION['username'];
                $this->password = $_SESSION['password'] ?? null;
                $this->session_id = session_id();
                $this->domain = $_SESSION['domain'];
                $this->permission_level = $_SESSION['level'];
                /** we'll need to fix up handling this information */
                $this->site_id = $_SESSION['site_id']; 
                $this->site = 'site' . $this->site_id;
            }
        }

        /**
         * Autoload instance using context
         *
         * @param \Auth_Info_User $context
         * @return Standard
         */
        public static function autoloadModule(\Auth_Info_User $context): self
        {
            $c = static::instantiateContexted($context);
            if (\in_array(Proxied::class, class_implements($c), true)) {
                return $c->_proxy();
            }
            return $c;
        }

        public function setUserParameters(\Auth_Info_User $auth)
        {
            if ($auth->id === $this->session_id) {
                return true;
            }

            if (!$auth->id) {
                fatal("no session id");
            }

            $this->domain = $auth->domain;
            $this->site_id = $auth->site_id;
            $this->site = $auth->site;
            $this->username = $auth->username;
            $this->permission_level = $auth->level;
            $this->group_id = $auth->group_id;
            $this->user_id = $auth->user_id;
            $this->session_id = $auth->id;
            $this->setContext($auth);
        }


        // backend session setup

        /** {{{ void clean_user_parameters
         * Cleans the user-specific parameters; necessary to invoke after calling
         * a backend call
         */
        public function cleanUserParameters()
        {
            unset(
                $this->username, $this->domain,
                $this->password, $this->session_id, $this->user_id,
                $this->group_id, $this->permission_level, $this->authContext
            );

            return $this;
        }

        public function setSessionId($id): void
        {
            //@xxx debugging
            \assert($this->session_id === $id);
            $this->session_id = $id;
        }

        /**
         * Call method on module
         *
         * @param string $function
         * @param mixed  $args
         * @return mixed
         */
        public function _invoke($function, $args)
        {
            return $this->$function(...$args);
        }

        public function __debugInfo()
        {
            return [
                'session_id' => $this->session_id,
                'username' => $this->username,
                'site' => $this->site
            ];
        }


        public function getExportedFunctions(): array
        {
            return $this->exportedFunctions;
        }

        /**
         * Account reset hook
         *
         * @param \Util_Account_Editor|null $editor
         * @return array
         */
        public function _reset(\Util_Account_Editor &$editor = null)
        {
            return array();
        }

        protected function getServices()
        {
            return $this->getAuthContext()->conf->conf;
        }

        protected function getNewServices($svc = null)
        {
            $s = $this->getAuthContext()->conf->new;
            if ($svc) {
                return $s[$svc];
            }

            return $s;
        }

        protected function getActiveServices($svc = null)
        {
            $s = $this->getAuthContext()->conf->cur;
            if ($svc) {
                return $s[$svc];
            }

            return $s;
        }

        protected function getConfig($svc_name, $svc_type = null)
        {
            return $this->getServiceValue($svc_name, $svc_type);
        }

        /**
         * mixed get_service_value(string, string)
         * Get site configuration value from <FST>/info/current
         *
         * @param string $svc_name service type to look up data
         * @param string $svc_type name of the service type subset to return data
         * @param mixed  $default  default value
         * @return mixed
         */
        protected function getServiceValue($svc_name, $svc_type = null, $default = null)
        {
            if ($this->permission_level & PRIVILEGE_ADMIN) {
                return $default;
            }
            $conf = $this->getAuthContext()->conf->cur ?? ($this->getAuthContext()->conf->new ?? []);
            if (!isset($conf[$svc_name])) {
                // newer platform remaps
                $svc_name = array_get(\Opcenter\SiteConfiguration::MODULE_REMAP, $svc_name);
                if (null === $svc_name) {
                    return $default;
                }
            }
            $val = $conf[$svc_name];
            if ($svc_type) {
                if (!isset($val[$svc_type])) {
                    return $default;
                }
                $val = $val[$svc_type];
                if (\is_array($val) && \count($val) < 2) {
                    $val = array_pop($val);
                }
            }
            return $val;
        }

        /**
         *  Journals data to <site config>/info/new/<$svc_name>
         *  Invokes reconfigure hook when EditVirtDomain is used
         */
        protected function setConfigJournal($svc_name, $svc_param, $svc_val)
        {
            return $this->setConfig($svc_name, $svc_param, $svc_val, 1);
        }

        /**
         *  Writes out configuration immediately to <site config>/info/current/<$svc_name>
         *  *Does not* invoke reconfigure hook when EditVirtDomain is used
         */
        protected function setConfig($svc_name, $svc_param, $svc_val, $journal = false)
        {
            $conf = $this->getAuthContext()->conf;
            // hook into \Auth_Info_Account()
            $conf->change($svc_name, array($svc_param => $svc_val), $journal);
            if ($journal) {
                $new = $conf->new;
            } else {
                $new = $conf->cur;
            }
            $this->query('common_save_service_information_backend', $new, $journal);
            return $this->getAuthContext()->reset();
        }

        /**
         * Elevate permissions + query backend
         *
         * @param string $cmd
         * @param mixed  $args
         * @return mixed
         */
        public function query($cmd, ...$args)
        {
            if (IS_CLI) {
                return $this->__call($cmd, $args);
            }
            $ret = \DataStream::get($this->getAuthContext())->query($cmd, ...$args);

            return $ret;
        }

        public function _resetModule() {
            $this->authContext = null;
            $this->cleanUserParameters();
        }
    }