summaryrefslogtreecommitdiff
path: root/init.php
blob: 4ca1f1ab812124410b5d1baa10f5ae6e2b72d9e6 (plain)
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
<?php
class Prefs_Effective_Config extends Plugin
{
	private const CONFIG_KEYS_TO_MASK = ['DB_PASS'];
	private const PARAM_TYPE_TO_NAME = [
		Config::T_BOOL => 'boolean',
		Config::T_STRING => 'string',
		Config::T_INT => 'integer',
	];

	/** @return array<null|float|string|bool> */
	function about()
	{
		return [
			null, // version
			'Shows your effective tt-rss config @ Preferences --> System', // description
			'wn', // author
			false, // is system
			'https://dev.tt-rss.org/fox/ttrss-prefs-effective-config', // more info URL
		];
	}

	/** @return int */
	function api_version()
	{
		return 2;
	}

	/**
	 * @param PluginHost $host
	 *
	 * @return void
	 * */
	function init($host)
	{
		$host->add_hook($host::HOOK_PREFS_TAB, $this);
	}

	/**
	 * @param string $tab
	 * @return void
	 */
	function hook_prefs_tab($tab)
	{
		if ($tab != 'prefSystem' || !self::is_admin()) {
			return;
		}
?>
		<div dojoType='dijit.layout.AccordionPane' title='<i class="material-icons">subject</i> <?= __('Effective Config') ?>'>
			<script type='dojo/method' event='onSelected' args='evt'>
				if (!this.domNode.querySelector('.loading')) {
					return;
				}

				window.setTimeout(() => {
					xhr.json('backend.php', {
						op: 'PluginHandler',
						plugin: 'prefs_effective_config',
						method: 'get_effective_config'
					}, (reply) => {
						this.attr('content', `
                <style type='text/css'>
                  #config-items-list { text-align: left; border-spacing: 0; }

                  #config-items-list .envvar_prefix { opacity: 0.4; }

                  #config-items-list th { border-bottom: 1px solid #000; }
                  #config-items-list tbody tr:hover { background: #eee; }
                  #config-items-list tbody td { padding: 5px; }
                  #config-items-list tbody td.redacted { color: rgba(0, 0, 0, 0.4); }
                  #config-items-list tbody td.green { background-color: rgba(0, 255, 0, 0.1); }
                  #config-items-list tbody td.red { background-color: rgba(255, 0, 0, 0.1); }
                  #config-items-list tbody td.gray { background-color: rgba(128, 128, 128, 0.1); }
                  #config-items-list td:not(:last-child) { border-right: 1px solid #ccc; }
                </style>

                <p>
                  ${__('Configuration item descriptions may be found in %s.')
                    .replace('%s',
                             '<a target="_blank" rel="noreferrer noopener" href="https://dev.tt-rss.org/tt-rss/tt-rss/src/branch/master/classes/config.php">classes/config.php</a>')
                   }
                </p>

                <table id='config-items-list'>
                  <thead>
                    <tr>
                      <th>${__('Name')}</th>
                      <th>${__('Effective Value')}</th>
                      <th>${__('Environment Variable Value')}</th>
                      <th>${__('Default Value')}</th>
                      <th>${__('Type Hint')}</th>
                    </tr>
                  </thead>
                  <tbody>
                  ${
                    reply.params.map(param => `
                      <tr>
                        <td><span class="envvar_prefix">${reply.envvar_prefix}</span>${param.name}</td>
                        ${param.should_redact ? `
                          <td class='redacted gray'>redacted</td>
                          <td class='redacted gray'>${param.env_val}</td>
                          <td class='redacted gray'>${param.default_val}</td>
                        ` : `
                          <td class='${[param.env_val, param.default_val].includes(param.effective_val) ? 'green' : 'red'}'>${param.effective_val}</td>
                          <td class='${param.effective_val == param.env_val ? 'green' : 'red'}'>${param.env_val}</td>
                          <td class='${param.effective_val == param.default_val ? 'green' : 'red'}'>${param.default_val}</td>
                        `}
                        <td>${param.type_hint}</td>
                      </tr>
                    `).join('')
                  }
                  </tbody>
                </table>
                `);
					});
				}, 200);
			</script>
			<span class='loading'><?= __('Loading, please wait...') ?></span>
		</div>
<?php
	}

	function get_effective_config(): void
	{
		if (!self::is_admin()) {
			print Errors::to_json(Errors::E_UNAUTHORIZED);
			return;
		}

		$cfg_instance = new Config();
		$cfg_rc = new ReflectionClass($cfg_instance);

		$envvar_prefix = $cfg_rc->getConstant('_ENVVAR_PREFIX');
		$defaults = $cfg_rc->getConstant('_DEFAULTS');

		$params_rc = $cfg_rc->getProperty('params');
		$params_rc->setAccessible(true);

		$params = [];

		foreach ($params_rc->getValue($cfg_instance) as $p => $v) {
			list($pval, $ptype) = $v;
			$env_val = getenv($envvar_prefix . $p);
			list($defval, $deftype) = $defaults[$cfg_rc->getConstant($p)];
			$should_redact = in_array($p, self::CONFIG_KEYS_TO_MASK);

			$params[] = [
				'name' => $p,
				'should_redact' => $should_redact,
				'effective_val' => $should_redact ? 'redacted' : strval(self::maybe_bool_to_str($pval)),
				'env_val' => $env_val ? ($should_redact ? 'redacted' : $env_val) : '',
				'default_val' => strval($defval),
				'type_hint' => self::PARAM_TYPE_TO_NAME[$ptype],
			];
		}

		print json_encode([
			'envvar_prefix' => $envvar_prefix,
			'params' => $params,
		]);
	}

	private static function is_admin(): bool
	{
		return ($_SESSION['access_level'] ?? UserHelper::ACCESS_LEVEL_DISABLED) >= UserHelper::ACCESS_LEVEL_ADMIN;
	}

	/**
	 * @param bool|int|string $val
	 *
	 * @return int|string
	 */
	private static function maybe_bool_to_str($val)
	{
		return $val === true ? 'true' : ($val === false ? 'false' : $val);
	}
}