aboutsummaryrefslogtreecommitdiff
path: root/cli/_cli.php
blob: 9ba6ec006ba38827fec6dcf55e8a76b16b812125 (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
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
<?php
declare(strict_types=1);

if (php_sapi_name() !== 'cli') {
	die('FreshRSS error: This PHP script may only be invoked from command line!');
}

const EXIT_CODE_ALREADY_EXISTS = 3;
const REGEX_INPUT_OPTIONS = '/^--/';
const REGEX_PARAM_OPTIONS = '/:*$/';

require(__DIR__ . '/../constants.php');
require(LIB_PATH . '/lib_rss.php');	//Includes class autoloader
require(LIB_PATH . '/lib_install.php');

Minz_Session::init('FreshRSS', true);
FreshRSS_Context::initSystem();
Minz_ExtensionManager::init();
Minz_Translate::init('en');

FreshRSS_Context::$isCli = true;

/** @return never */
function fail(string $message, int $exitCode = 1) {
	fwrite(STDERR, $message . "\n");
	die($exitCode);
}

function cliInitUser(string $username): string {
	if (!FreshRSS_user_Controller::checkUsername($username)) {
		fail('FreshRSS error: invalid username: ' . $username . "\n");
	}

	if (!FreshRSS_user_Controller::userExists($username)) {
		fail('FreshRSS error: user not found: ' . $username . "\n");
	}

	FreshRSS_Context::initUser($username);
	if (!FreshRSS_Context::hasUserConf()) {
		fail('FreshRSS error: invalid configuration for user: ' . $username . "\n");
	}

	$ext_list = FreshRSS_Context::userConf()->extensions_enabled;
	Minz_ExtensionManager::enableByList($ext_list, 'user');

	return $username;
}

function accessRights(): void {
	echo 'ℹ️ Remember to re-apply the appropriate access rights, such as:',
		"\t", 'sudo cli/access-permissions.sh', "\n";
}

/** @return never */
function done(bool $ok = true) {
	if (!$ok) {
		fwrite(STDERR, (empty($_SERVER['argv'][0]) ? 'Process' : basename($_SERVER['argv'][0])) . ' failed!' . "\n");
	}
	exit($ok ? 0 : 1);
}

function performRequirementCheck(string $databaseType): void {
	$requirements = checkRequirements($databaseType);
	if ($requirements['all'] !== 'ok') {
		$message = 'FreshRSS failed requirements:' . "\n";
		foreach ($requirements as $requirement => $check) {
			if ($check !== 'ok' && !in_array($requirement, ['all', 'pdo', 'message'], true)) {
				$message .= '• ' . $requirement . "\n";
			}
		}
		if (!empty($requirements['message']) && $requirements['message'] !== 'ok') {
			$message .= '• ' . $requirements['message'] . "\n";
		}
		fail($message);
	}
}

/**
 * Parses parameters used with FreshRSS' CLI commands.
 * @param array{'valid':array<string,string>,'deprecated':array<string,string>} $parameters An array of 'valid': An
 * array of parameters as keys and their respective getopt() notations as values. 'deprecated' An array with
 * replacement parameters as keys and their respective deprecated parameters as values.
 * @return array{'valid':array<string,string|bool>,'invalid':array<string>} An array of 'valid': an array of all
 * known parameters used and their respective options and 'invalid': an array of all unknown parameters used.
 */
function parseCliParams(array $parameters): array {
	global $argv;
	$cliParams = [];

	foreach ($parameters['valid'] as $param => $getopt_val) {
		$cliParams[] = $param . $getopt_val;
	}
	foreach ($parameters['deprecated'] as $param => $deprecatedParam) {
		$cliParams[] = $deprecatedParam . $parameters['valid'][$param];
	}

	$opts = getopt('', $cliParams);

	/** @var array<string,string|bool> $valid */
	$valid = is_array($opts) ? $opts : [];

	array_walk($valid, static fn(&$option) => $option = $option === false ? true : $option);

	if (checkforDeprecatedParameterUse(array_keys($valid), $parameters['deprecated'])) {
		$valid = updateDeprecatedParameters($valid, $parameters['deprecated']);
	}

	$invalid = findInvalidOptions(
		$argv,
		array_merge(array_keys($parameters['valid']), array_values($parameters['deprecated']))
	);

	return [
		'valid' => $valid,
		'invalid' => $invalid
	];
}

/**
 * @param array<string> $options
 * @return array<string>
 */
function getLongOptions(array $options, string $regex): array {
	$longOptions = array_filter($options, static function (string $a) use ($regex) {
		return preg_match($regex, $a) === 1;
	});
	return array_map(static function (string $a) use ($regex) {
		return preg_replace($regex, '', $a) ?? '';
	}, $longOptions);
}

/**
 * @param array<string> $input
 * @param array<string> $params
 */
function validateOptions(array $input, array $params): bool {
	$sanitizeInput = getLongOptions($input, REGEX_INPUT_OPTIONS);
	$sanitizeParams = getLongOptions($params, REGEX_PARAM_OPTIONS);
	$unknownOptions = array_diff($sanitizeInput, $sanitizeParams);

	if (0 === count($unknownOptions)) {
		return true;
	}

	fwrite(STDERR, sprintf("FreshRSS error: unknown options: %s\n", implode (', ', $unknownOptions)));
	return false;
}

/**
 * Checks for use of unknown parameters with FreshRSS' CLI commands.
 * @param array<string> $input An array of parameters to check for validity.
 * @param array<string> $params An array of valid parameters to check against.
 * @return array<string> Returns an array of all unknown parameters found.
 */
function findInvalidOptions(array $input, array $params): array {
	$sanitizeInput = getLongOptions($input, REGEX_INPUT_OPTIONS);
	$unknownOptions = array_diff($sanitizeInput, $params);

	if (0 === count($unknownOptions)) {
		return [];
	}

	fwrite(STDERR, sprintf("FreshRSS error: unknown options: %s\n", implode (', ', $unknownOptions)));
	return $unknownOptions;
}

/**
 * Checks for use of deprecated parameters with FreshRSS' CLI commands.
 * @param array<string> $options User inputs to check for deprecated parameter use.
 * @param array<string,string> $params An array with replacement parameters as keys and their respective deprecated
 * parameters as values.
 * @return bool Returns TRUE and generates a deprecation warning if deprecated parameters
 * have been used, FALSE otherwise.
 */
function checkforDeprecatedParameterUse(array $options, array $params): bool {
	$deprecatedOptions = array_intersect($options, $params);
	$replacements = array_map(static fn($option) => array_search($option, $params, true), $deprecatedOptions);

	if (0 === count($deprecatedOptions)) {
		return false;
	}

	fwrite(STDERR, "FreshRSS deprecation warning: the CLI option(s): " . implode(', ', $deprecatedOptions) .
		" are deprecated and will be removed in a future release. Use: "
		. implode(', ', $replacements) . " instead\n");
	return true;
}

/**
 * Switches all used deprecated parameters to their replacements if they have one.
 *
 * @template T
 *
 * @param array<string,T> $options User inputs.
 * @param array<string,string> $params An array with replacement parameters as keys and their respective deprecated
 * parameters as values.
 * @return array<string,T>  Returns $options with deprications replaced.
 */
function updateDeprecatedParameters(array $options, array $params): array {
	$updatedOptions = [];

	foreach ($options as $param => $option) {
		$replacement = array_search($param, $params, true);
		if (is_string($replacement)) {
			$updatedOptions[$replacement] = $option;
		} else {
			$updatedOptions[$param] = $option;
		}
	}

	return $updatedOptions;
}