1: <?php
2: declare(strict_types=1);
3: /**
4: * +------------------------------------------------------------+
5: * | apnscp |
6: * +------------------------------------------------------------+
7: * | Copyright (c) Apis Networks |
8: * +------------------------------------------------------------+
9: * | Licensed under Artistic License 2.0 |
10: * +------------------------------------------------------------+
11: * | Author: Matt Saladna (msaladna@apisnetworks.com) |
12: * +------------------------------------------------------------+
13: */
14:
15: /**
16: * Tomcat functions
17: *
18: * @package core
19: */
20: class Tomcat_Module extends Module_Skeleton implements \Module\Skeleton\Contracts\Hookable
21: {
22: const DEPENDENCY_MAP = ['apache'];
23: const TOMCAT_PORT = 8080;
24:
25: public $exportedFunctions;
26:
27: public function __construct()
28: {
29: parent::__construct();
30: $this->exportedFunctions = array(
31: '*' => PRIVILEGE_SITE,
32: 'version' => PRIVILEGE_ALL,
33: 'system_user' => PRIVILEGE_ALL,
34: 'enabled' => PRIVILEGE_SITE | PRIVILEGE_USER,
35: 'permitted' => PRIVILEGE_SITE | PRIVILEGE_USER
36:
37: );
38: }
39:
40: /**
41: * Current user under which Tomcat operates
42: *
43: * @return null|string username or null if not present on system
44: */
45: public function system_user(): ?string
46: {
47: $user = $this->_getKey();
48:
49: // same user as service name, Helios+ > tomcat
50: return posix_getpwnam($user)['name'] ?? null;
51: }
52:
53: /**
54: * Tomcat service key
55: *
56: * @return string
57: */
58: private function _getKey()
59: {
60: $version = platform_version();
61: if (version_compare($version, '5', '>=')) {
62: // helios
63: return 'tomcat';
64: }
65:
66: return 'tomcat4';
67: }
68:
69: /**
70: * Tomcat is enabled for an account
71: *
72: * @return bool
73: */
74: public function enabled()
75: {
76: $key = $this->_getKey();
77:
78: return (bool)$this->getServiceValue($key, 'enabled');
79: }
80:
81: /**
82: * Confirm Tomcat may be enabled for an account
83: *
84: * @return bool
85: */
86: public function permitted()
87: {
88: $version = platform_version();
89: if (version_compare($version, '4.5', '>=')) {
90: // helios, apollo/aleph
91: $key = $this->_getKey();
92:
93: return (bool)$this->getServiceValue($key, 'permit');
94: }
95:
96: // older platforms with PostgreSQL enabled imply permit
97: return (bool)$this->sql_enabled('pgsql');
98: }
99:
100: /**
101: * Get Tomcat version number
102: *
103: * @return string
104: */
105: public function version()
106: {
107: static $version;
108: if (isset($version) && $version) {
109: return $version;
110: }
111: $cache = Cache_Global::spawn();
112: $key = 'tomcat.version';
113: $version = $cache->get($key);
114: if ($version) {
115: return $version;
116: }
117: /**
118: * couple different strategies here:
119: * run version.sh or scrape localhost:8080
120: */
121: $platformver = platform_version();
122: $prefix = null;
123: if (version_compare($platformver, '4.5', '>=')) {
124: // aleph, helios+
125: $prefix = $this->_getPrefix();
126: }
127: $path = $prefix . '/bin/version.sh';
128:
129: if (file_exists($path)) {
130: $resp = Util_Process::exec($path);
131: if (preg_match(Regex::TOMCAT_VERSION, $resp['output'], $m)) {
132: $version = $m['version'];
133: }
134: }
135:
136: // bin/version.sh failed
137: if (!$version) {
138: $req = new HTTP_Request2('http://127.0.0.1:' . self::TOMCAT_PORT,
139: HTTP_Request2::METHOD_GET, array('use_brackets' => true));
140: $response = $req->send()->getBody();
141: if (preg_match(Regex::TOMCAT_VERSION, $response, $m)) {
142: $version = $m['version'];
143: }
144: }
145: if (!$version) {
146: $version = 'undefined';
147: }
148: $cache->set($key, $version);
149:
150: return $version;
151: }
152:
153: /**
154: * Tomcat installation root
155: *
156: * @return string
157: */
158: private function _getPrefix()
159: {
160: $version = platform_version();
161: if (version_compare($version, '5', '>=')) {
162: // aleph, helios+
163: return '/opt/tomcat';
164: } else {
165: return '/opt/tomcat4';
166: }
167: }
168:
169: /**
170: * General EditVirtDomain hook
171: *
172: * @return bool
173: */
174: public function _edit()
175: {
176: $key = $this->_getKey();
177:
178: $conf_old = $this->getAuthContext()->conf($key, 'old');
179: $conf_new = $this->getAuthContext()->conf($key, 'new');
180:
181: if ($conf_new === $conf_old) {
182: return;
183: }
184: $log = '/var/log/catalina.out';
185: $path = $this->domain_fs_path() . $log;
186: $origlog = $this->_getPrefix() . $log;
187: if (file_exists($path)) {
188: unlink($path);
189: }
190: if ($conf_new['enabled']) {
191: link($origlog, $path);
192: }
193:
194: return true;
195: }
196:
197: public function _verify_conf(\Opcenter\Service\ConfigurationContext $ctx): bool
198: {
199: return true;
200: }
201:
202: public function _create()
203: {
204: // TODO: Implement _create() method.
205: }
206:
207: public function _delete()
208: {
209: // TODO: Implement _delete() method.
210: }
211:
212: public function _create_user(string $user)
213: {
214: // TODO: Implement _create_user() method.
215: }
216:
217: public function _delete_user(string $user)
218: {
219: // TODO: Implement _delete_user() method.
220: }
221:
222: public function _edit_user(string $userold, string $usernew, array $oldpwd)
223: {
224: // TODO: Implement _edit_user() method.
225: }
226:
227:
228: }
229: