1: <?php
2:
3: if (mb_strtolower(php_sapi_name()) !== 'cli') {
4: die("Must be run from the command line\n");
5: }
6: /* debug mode has to be set to something or include files will die() */
7: define('DEBUG_MODE', false);
8:
9: /* determine current absolute path used for require statements */
10: define('APP_PATH', dirname(dirname(__FILE__)).'/');
11: define('VENDOR_PATH', APP_PATH.'vendor/');
12: define('WEB_ROOT', '');
13:
14: /* get the framework */
15: require APP_PATH.'lib/framework.php';
16:
17: function read_config($source) {
18: if ($source == 'Defaults') {
19: return array();
20: }
21: if ($source == 'hm3.rc') {
22: return @unserialize(file_get_contents('../'.$source));
23: }
24: return parse_ini_file('../'.$source);
25: }
26:
27: function check_php() {
28: $version = phpversion();
29: if (mb_substr($version, 0, 3) >= 8.1) {
30: $version_class = 'yes';
31: }
32: else {
33: $version_class = 'no';
34: }
35: $mb = function_exists('mb_strpos');
36: $curl = function_exists('curl_exec');
37: $pdo = class_exists('PDO');
38: $ssl = function_exists('openssl_random_pseudo_bytes');
39:
40: return '<div class="settings_subtitle"><i class="bi bi-caret-down-fill"></i> PHP Support '.
41: (Hm_Dispatch::is_php_setup() ? '<span class="yes">&#10003;</span>' : '<span class="no">X</span>' ).
42: '</div><div class="config_settings_container">'.
43: '<table class="settings_table config_settings">'.
44: '<tr><td>Version</td><td class="'.$version_class.'">'.$version.'</td></tr>'.
45: '<tr><td>Multibyte string support</td><td class="'.($mb ? 'yes' : 'no').'">'.($mb ? 'yes' : 'no').'</td></tr>'.
46: '<tr><td>Curl support</td><td class="'.($curl ? 'yes' : 'no').'">'.($curl ? 'yes' : 'no').'</td></tr>'.
47: '<tr><td>PDO Database support</td><td class="'.($pdo ? 'yes' : 'no').'">'.($pdo ? 'yes' : 'no').'</td></tr>'.
48: '<tr><td>OpenSSL support</td><td class="'.($ssl ? 'yes' : 'no').'">'.($ssl ? 'yes' : 'no').'</td></tr>'.
49: '</table></div>';
50:
51: }
52:
53: function config_source($selected='hm3.sample.ini') {
54: $opts = array('Defaults', 'hm3.sample.ini', 'hm3.ini', 'hm3.rc');
55: return '<table style="width: 200px; margin: 40px; white-space: nowrap;">'.
56: '<tr><td>'.select_box('source', $selected, $opts).' <input type="submit" value="Update" /></td></tr>'.
57: '</table>';
58: }
59:
60: function session_type_setting($selected=false) {
61: return '<tr><td>Session Type</td><td>'.select_box('session_type', $selected, array('DB', 'PHP')).'</td></tr>';
62: }
63:
64: function select_box($name, $selected, $opts) {
65: $res = '<select name="'.$name.'">';
66: foreach ($opts as $opt) {
67: $res .= '<option ';
68: if ($selected == $opt) {
69: $res .= 'selected="selected" ';
70: }
71: $res .= 'value="'.$opt.'">'.$opt.'</option>';
72: }
73: $res .= '</select>';
74: return $res;
75: }
76:
77: function imap_auth_name_setting($current) {
78: return '<tr><td>IMAP authentication server name</td><td><input type="text" value="'.$current.'" name="imap_auth_name" /></td></tr>';
79: }
80:
81: function imap_auth_port_setting($current) {
82: return '<tr><td>IMAP authentication server port</td><td><input type="number" value="'.$current.'" name="imap_auth_port" /></td></tr>';
83: }
84:
85: function imap_auth_sieve_conf_host_setting($current) {
86: return '<tr><td>Sieve configuration</td><td><input type="text" value="'.$current.'" name="imap_auth_sieve_conf_host" /></td></tr>';
87: }
88: function imap_auth_sieve_tls_mode_setting($current) {
89: return '<tr><td>Sieve configuration mode</td><td><input type="text" value="'.$current.'" name="imap_auth_sieve_tls_mode" /></td></tr>';
90: }
91:
92: function imap_auth_server_setting($current) {
93: return '<tr><td>IMAP authentication server hostname</td><td><input type="text" value="'.$current.'" name="imap_auth_server" /></td></tr>';
94: }
95:
96: function imap_auth_tls_setting($current) {
97: return '<tr><td>IMAP authentication server TLS</td><td><input type="checkbox" '.($current ? 'checked="checked" ' : '').' value="1" '.
98: 'name="imap_auth_tls" /></td></tr>';
99: }
100:
101: function auth_type_setting($selected=false) {
102: return '<tr><td>Authentication Type</td><td>'.select_box('auth_type', $selected, array('DB', 'IMAP')).'</td></tr>';
103: }
104:
105: function default_smtp_name_setting($current) {
106: return '<tr><td>Default SMTP server name</td><td><input type="text" value="'.$current.'" name="default_smtp_name" /></td></tr>';
107: }
108:
109: function default_smtp_server_setting($current) {
110: return '<tr><td>Default SMTP server</td><td><input type="text" value="'.$current.'" name="default_smtp_server" /></td></tr>';
111: }
112:
113: function default_smtp_port_setting($current) {
114: return '<tr><td>Default SMTP server port</td><td><input type="number" value="'.$current.'" name="default_smtp_port" /></td></tr>';
115: }
116:
117: function encrypt_ajax_requests_setting($current) {
118: return '<tr><td>Encrypt AJAX responses</td><td><input type="checkbox" '.($current ? 'checked="checked" ' : '').' value="1" '.
119: 'name="encrypt_ajax_requests" /></td></tr>';
120: }
121:
122: function encrypt_local_storage_setting($current) {
123: return '<tr><td>Encrypt local session storage</td><td><input type="checkbox" '.($current ? 'checked="checked" ' : '').' value="1" '.
124: 'name="encrypt_local_storage" /></td></tr>';
125: }
126:
127: function default_smtp_tls_setting($current) {
128: return '<tr><td>Default SMTP server TLS</td><td><input type="checkbox" '.($current ? 'checked="checked" ' : '').' value="1" '.
129: 'name="default_smtp_tls" /></td></tr>';
130: }
131:
132: function default_language_setting($current) {
133: return '<tr><td>Default language</td><td><input type="text" value="'.$current.'" name="default_language" /></td></tr>';
134: }
135:
136: function user_config_type_setting($selected=false) {
137: return '<tr><td>User Configuration type</td><td>'.select_box('user_config_type', $selected, array('DB', 'file')).'</td></tr>';
138: }
139:
140: function attachment_dir_setting($current) {
141: return '<tr><td>Attachment storage directory</td><td><input type="text" value="'.$current.'" name="attachment_dir" /></td></tr>';
142: }
143:
144: function user_settings_dir_setting($current) {
145: return '<tr><td>User settings storage directory</td><td><input type="text" value="'.$current.'" name="user_settings_dir" /></td></tr>';
146: }
147:
148: function disable_tls_setting($current) {
149: return '<tr><td>Allow non-TLS browser connections</td><td><input type="checkbox" '.($current ? 'checked="checked" ' : '').' value="1" '.
150: 'name="disable_tls" /></td></tr>';
151: }
152:
153: function admin_users_setting($current) {
154: return '<tr><td>Admin user list</td><td><input type="text" value="'.$current.'" name="admin_users" /></td></tr>';
155: }
156:
157: function app_name_setting($current) {
158: return '<tr><td>Application name</td><td><input type="text" value="'.$current.'" name="app_name" /></td></tr>';
159: }
160:
161: function js_compress_setting($current) {
162: return '<tr><td>Javascript compression command</td><td><input type="text" value="'.$current.'" name="js_compress" /></td></tr>';
163: }
164:
165: function css_compress_setting($current) {
166: return '<tr><td>CSS compression command</td><td><input type="text" value="'.$current.'" name="css_compress" /></td></tr>';
167: }
168:
169: function db_host_setting($current) {
170: return '<tr><td>Database hostname</td><td><input type="text" value="'.$current.'" name="db_host" /></td></tr>';
171: }
172:
173: function db_name_setting($current) {
174: return '<tr><td>Database name</td><td><input type="text" value="'.$current.'" name="db_name" /></td></tr>';
175: }
176:
177: function db_user_setting($current) {
178: return '<tr><td>Database username</td><td><input type="text" value="'.$current.'" name="db_user" /></td></tr>';
179: }
180:
181: function db_pass_setting($current) {
182: return '<tr><td>Database password</td><td><input type="password" value="'.$current.'" name="db_pass" /></td></tr>';
183: }
184:
185: function db_driver_setting($current) {
186: $opts = PDO::getAvailableDrivers();
187: return '<tr><td>Databse Type</td><td>'.select_box('auth_type', $selected, $opts).'</td></tr>';
188: }
189:
190: function output_settings($settings) {
191: echo '<div class="settings_subtitle"><i class="bi bi-caret-down-fill"></i> Settings</div>'.
192: '<div class="config_settings_container">'.
193: '<table class="settings_table settings config_settings">';
194: foreach (setting_defaults() as $title => $vals) {
195: echo '<tr><td colspan="2" class="config_subtitle">'.$title.'</td></tr>';
196: foreach ($vals as $setting => $default) {
197: if (function_exists($setting.'_setting')) {
198: if (array_key_exists($setting, $settings)) {
199: $value = $settings[$setting];
200: }
201: else {
202: $value = $default;
203: }
204: $func = $setting.'_setting';
205: echo $func($value);
206: }
207: else {
208: echo '<tr><td class="setting_row">'.$setting.'</td><td>Not supported yet</td></tr>';
209: }
210: }
211: }
212: echo '</table><br style="clear: both;"></div>';
213: }
214:
215: function setting_defaults() {
216: return array(
217: 'General' => array(
218: 'session_type' => 'PHP',
219: 'auth_type' => 'DB',
220: 'default_language' => 'en',
221: 'user_config_type' => 'file',
222: 'user_settings_dir' => '/var/lib/hm3/users',
223: 'attachment_dir' => '/var/lib/hm3/attachments',
224: 'disable_tls' => 'false',
225: 'admin_users' => '',
226: 'app_name' => 'HM3',
227: 'js_compress' => 'false',
228: 'css_compress' => 'false',
229: 'encrypt_ajax_requests' => '',
230: 'encrypt_local_storage' => '',
231: ),
232: 'IMAP' => array(
233: 'imap_auth_name' => 'localhost',
234: 'imap_auth_server' => 'localhost',
235: 'imap_auth_port' => '143',
236: 'imap_auth_tls' => '',
237: 'imap_auth_sieve_conf_host' => '',
238: 'imap_auth_sieve_tls_mode' => false
239: ),
240: 'SMTP' => array(
241: 'default_smtp_name' => '',
242: 'default_smtp_server' => '',
243: 'default_smtp_port' => '',
244: 'default_smtp_tls' => '',
245: ),
246: 'Database' => array(
247: 'db_host' => '127.0.0.1',
248: 'db_name' => 'test',
249: 'db_user' => 'test',
250: 'db_pass' => '123456',
251: 'db_driver' => 'mysql',
252: ),
253: );
254: }
255:
256: function output_modules($settings) {
257: echo '<div class="settings_subtitle"><i class="bi bi-caret-down-fill"></i> Module Sets</div><div class="config_settings_container">'.
258: '<table class="settings_table config_settings">';
259: $mod_str = 'core,contacts,feeds,imap,smtp,site,account,idle_timer,calendar,'.
260: 'themes,nux,developer,github,wordpress,history,saved_searches,nasa';
261: if (array_key_exists('modules', $settings)) {
262: $mod_str = $settings['modules'];
263: }
264: $mods = array('core', 'contacts', 'feeds', 'imap', 'smtp', 'site', 'account',
265: 'idle_timer', 'calendar', 'themes', 'nux,developer', 'github', 'wordpress',
266: 'history', 'saved_searches', 'nasa');
267: foreach ($mods as $mod) {
268: echo '<tr><td>'.ucfirst(str_replace('_', ' ', $mod)).
269: '</td><td><input type="checkbox" value="1" name="'.$mod.'" ';
270: if (mb_strstr($mod_str, $mod)) {
271: echo 'checked="checked" ';
272: }
273: echo '/></td></tr>';
274: }
275: echo '</table></div>';
276: }
277:
278: function page_style() { ?>
279: <style type="text/css">
280: h5 { background-color: #fff; margin: 0px; padding: 20px; font-size: 130%; font-weight: normal; color: #666; }
281: table { max-width: 800px !important; }
282: body, .settings_subtitle { background: none; background-color: #fff; }
283: .config_settings_container { display: none; max-height: 300px; overflow-y: scroll; width: 800px; }
284: p { padding-left: 40px; max-width: 800px; }
285: .settings tr td { padding-left: 55px }
286: .config_settings { border-right: solid 1px #eee; }
287: .config_subtitle { padding-left: 35px !important; background-color: #f9f9f9; border: solid 1px #eee; }
288: .yes { color: green !important; }
289: .no { color: red !important; }
290: .btn { margin-left: 35px; margin-top: 20px; }
291: </style>
292:
293: <?php }
294: function output_page($source) { ?><!DOCTYPE html><html dir="ltr" class="ltr_page" lang=en>
295: <head>
296: <meta charset="utf-8" />
297: <link href="../modules/core/site.css" media="all" rel="stylesheet" type="text/css" />
298: <script type="text/javascript" src="../third_party/cash.min.js"></script>
299: <script type="text/javascript">$(function() {
300: $('.settings_subtitle').click(function() { $(this).next().toggle(); });
301: $('select[name=session_type]').change(function() { console.log('here'); });
302: $('select[name=auth_type]').change(function() { console.log('here'); });
303: });</script>
304: <title>Cypht Setup</title>
305: <?php page_style() ?>
306: </head>
307: <body>
308: <div><h5>Cypht configuration builder</h5></div>
309: <p>
310: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
311: incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
312: nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
313: Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
314: fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
315: culpa qui officia deserunt mollit anim id est laborum.
316: </p>
317: <form method="get">
318: <?php echo config_source($source) ?>
319: </form>
320: <form class="user_settings" method="post">
321: <?php echo check_php() ?>
322: <?php output_settings(read_config($source)) ?>
323: <?php output_modules(read_config($source)) ?>
324: <input type="button" class="btn" name="test_imap" value="Test IMAP" />
325: <input type="button" class="btn" name="test_smtp" value="Test SMTP" /><br />
326: <input type="button" class="btn" name="test_smtp" value="Test Database" />
327: <input type="button" class="btn" name="test_files" value="Test Files" />
328: <input type="submit" class="btn" name="build" value="Build Config" /><br />
329: <input type="reset" class="btn" name="reset" value="Reset" />
330: </form>
331: </body>
332: </html>
333:
334: <?php }
335:
336: if (array_key_exists('source', $_GET) &&
337: in_array($_GET['source'], array('Defaults', 'hm3.sample.ini', 'hm3.ini', 'hm3.rc'), true)) {
338: $source = $_GET['source'];
339: }
340: else {
341: $source = 'hm3.ini';
342: }
343: output_page($source);
344:
345: /*
346: TODO
347: ----
348: - check php for correct support, provide links to php mod installation
349: - curl
350: - mcrypt
351:
352: - read config and setup form from
353: - sample ini file
354: - existing ini file
355: - rc file?
356:
357: - callbacks to generate form elements per key
358: - fallback for unknown/new ini values
359:
360: - check for writable folders
361:
362: - db setup
363: - access test
364: - schema for diffrent db types
365: - postgres
366: - mysql
367: - sqlite
368: - others?
369:
370: - write new config
371: - write to ini file
372: - compile rc file
373:
374: - create required directories? output create commands to be run as root?
375: - symlink production site? output commands to be run as root?
376: */
377: