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: 
<?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\Definition\MODULE30;
    use Module\Skeleton\Contracts\Proxied;

    /**
     * Class Module_Skeleton
     */
    abstract class Standard implements MODULE30
    {
        use \apnscpFunctionInterceptorTrait;
        use \FilesystemPathTrait;
        use \AccountInfoTrait;

        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;

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

        private function initializeUser()
        {
            if ($this->inContext()) {
                return $this->setUserParameters($this->getAuthContext());
            }
            if (!IS_CLI && $_SESSION) {
                if (!isset($_SESSION['username'])) {
                    \apnscpSession::init()->destroy(\session_id());
                    fatal("Session corruption - %s", \session_id());
                }
                $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;
            }
        }

        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);
        }

        /**
         * 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 __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();
        }

        /**
         * 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();
        }

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

        /** {{{ 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;
        }

        /**
         * 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;
        }
    }