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: 
<?php
    declare(strict_types=1);
    /**
     *  +------------------------------------------------------------+
     *  | apnscp                                                     |
     *  +------------------------------------------------------------+
     *  | Copyright (c) Apis Networks                                |
     *  +------------------------------------------------------------+
     *  | Licensed under Artistic License 2.0                        |
     *  +------------------------------------------------------------+
     *  | Author: Matt Saladna (msaladna@apisnetworks.com)           |
     *  +------------------------------------------------------------+
     */

    /**
     * Tomcat functions
     *
     * @package core
     */
    class Tomcat_Module extends Module_Skeleton implements \Opcenter\Contracts\Hookable
    {
        const DEPENDENCY_MAP = ['apache'];
        const TOMCAT_PORT = 8080;

        public $exportedFunctions;

        public function __construct()
        {
            parent::__construct();
            $this->exportedFunctions = array(
                '*'           => PRIVILEGE_SITE,
                'version'     => PRIVILEGE_ALL,
                'system_user' => PRIVILEGE_ALL,
                'enabled'     => PRIVILEGE_SITE | PRIVILEGE_USER,
                'permitted'   => PRIVILEGE_SITE | PRIVILEGE_USER

            );
        }

        /**
         * Current user under which Tomcat operates
         *
         * @return null|string username or null if not present on system
         */
        public function system_user(): ?string
        {
            $user = $this->_getKey();

            // same user as service name, Helios+ > tomcat
            return posix_getpwnam($user)['name'] ?? null;
        }

        /**
         * Tomcat service key
         *
         * @return string
         */
        private function _getKey()
        {
            $version = platform_version();
            if (version_compare($version, '5', '>=')) {
                // helios
                return 'tomcat';
            }

            return 'tomcat4';
        }

        /**
         * Tomcat is enabled for an account
         *
         * @return bool
         */
        public function enabled()
        {
            $key = $this->_getKey();

            return (bool)$this->getServiceValue($key, 'enabled');
        }

        /**
         * Confirm Tomcat may be enabled for an account
         *
         * @return bool
         */
        public function permitted()
        {
            $version = platform_version();
            if (version_compare($version, '4.5', '>=')) {
                // helios, apollo/aleph
                $key = $this->_getKey();

                return (bool)$this->getServiceValue($key, 'permit');
            }

            // older platforms with PostgreSQL enabled imply permit
            return (bool)$this->sql_enabled('pgsql');
        }

        /**
         * Get Tomcat version number
         *
         * @return string
         */
        public function version()
        {
            static $version;
            if (isset($version) && $version) {
                return $version;
            }
            $cache = Cache_Global::spawn();
            $key = 'tomcat.version';
            $version = $cache->get($key);
            if ($version) {
                return $version;
            }
            /**
             * couple different strategies here:
             * run version.sh or scrape localhost:8080
             */
            $platformver = platform_version();
            $prefix = null;
            if (version_compare($platformver, '4.5', '>=')) {
                // aleph, helios+
                $prefix = $this->_getPrefix();
            }
            $path = $prefix . '/bin/version.sh';

            if (file_exists($path)) {
                $resp = Util_Process::exec($path);
                if (preg_match(Regex::TOMCAT_VERSION, $resp['output'], $m)) {
                    $version = $m['version'];
                }
            }

            // bin/version.sh failed
            if (!$version) {
                $req = new HTTP_Request2('http://127.0.0.1:' . self::TOMCAT_PORT,
                    HTTP_Request2::METHOD_GET, array('use_brackets' => true));
                $response = $req->send()->getBody();
                if (preg_match(Regex::TOMCAT_VERSION, $response, $m)) {
                    $version = $m['version'];
                }
            }
            if (!$version) {
                $version = 'undefined';
            }
            $cache->set($key, $version);

            return $version;
        }

        /**
         * Tomcat installation root
         *
         * @return string
         */
        private function _getPrefix()
        {
            $version = platform_version();
            if (version_compare($version, '5', '>=')) {
                // aleph, helios+
                return '/opt/tomcat';
            } else {
                return '/opt/tomcat4';
            }
        }

        /**
         * General EditVirtDomain hook
         *
         * @return bool
         */
        public function _edit()
        {
            $key = $this->_getKey();

            $conf_old = $this->getAuthContext()->conf($key, 'old');
            $conf_new = $this->getAuthContext()->conf($key, 'new');

            if ($conf_new === $conf_old) {
                return;
            }
            $log = '/var/log/catalina.out';
            $path = $this->domain_fs_path() . $log;
            $origlog = $this->_getPrefix() . $log;
            if (file_exists($path)) {
                unlink($path);
            }
            if ($conf_new['enabled']) {
                link($origlog, $path);
            }

            return true;
        }

        public function _verify_conf(\Opcenter\Service\ConfigurationContext $ctx): bool
        {
            return true;
        }

        public function _create()
        {
            // TODO: Implement _create() method.
        }

        public function _delete()
        {
            // TODO: Implement _delete() method.
        }

        public function _create_user(string $user)
        {
            // TODO: Implement _create_user() method.
        }

        public function _delete_user(string $user)
        {
            // TODO: Implement _delete_user() method.
        }

        public function _edit_user(string $userold, string $usernew, array $oldpwd)
        {
            // TODO: Implement _edit_user() method.
        }


    }