Skip to content

Commit

Permalink
Rework the setAdminSettings function at SettingsController.php
Browse files Browse the repository at this point in the history
This rework fixes the error bellow:

Error: array_filter() expects parameter 1 to be array, null given at
/var/www/nextcloud/apps/roundcube/lib/Controller/SettingsController.php#149

This error log happens when the admin tries to save the config without
setting the 'Per email domain RC installations'.

Signed-off-by: Igor Matheus Andrade Torrente <[email protected]>
  • Loading branch information
Igor Matheus Andrade Torrente committed Aug 3, 2021
1 parent d9b9e16 commit ccf68ef
Showing 1 changed file with 37 additions and 44 deletions.
81 changes: 37 additions & 44 deletions roundcube/lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class SettingsController extends Controller
/** @var config */
private $config;

public function __construct(string $AppName, IRequest $request, IConfig $config, IURLGenerator $urlGenerator, IL10N $l) {
public function __construct(string $AppName, IRequest $request, IConfig $config,
IURLGenerator $urlGenerator, IL10N $l)
{
parent::__construct($AppName, $request);
$this->urlGenerator = $urlGenerator;
$this->config = $config;
Expand Down Expand Up @@ -72,66 +74,49 @@ public function setAdminSettings() {
$enableSSLVerify = $req->getParam('enableSSLVerify', null);

// Validate and do a first fix of some values.
$validation = array();
if (!is_string($defaultRCPath) || $defaultRCPath === '')
$validation[] = $l->t("Default RC installation path can't be an empty string.");
elseif (preg_match('/^([a-zA-Z]+:)?\/\//', $defaultRCPath) === 1)
$validation[] = $l->t("Default path must be a url relative to this server.");
else
$defaultRCPath = trim($defaultRCPath);

if(isset($rcDomains) && is_array($rcDomains)) {
foreach ($rcDomains as &$dom) {
if (!is_string($dom) || preg_match('/(@|\/)/', $dom) === 1) {
$validation[] = $l->t("A domain is not valid.");
break;
} else {
$dom = trim($dom);
}
}
return self::error_response($l->t("Default RC installation path can't be an empty string."));
else if (preg_match('/^([a-zA-Z]+:)?\/\//', $defaultRCPath) === 1)
return self::error_response($l->t("Default path must be a url relative to this server."));

$defaultRCPath = ltrim(trim($defaultRCPath));

if (!is_array($rcDomains) || !is_array($rcPaths)) {
$this->config->setAppValue($appName, 'domainPath', array());
goto success_output;
}

foreach ($rcDomains as &$dom) {
if (!is_string($dom) || preg_match('/(@|\/)/', $dom) === 1)
return self::error_response($l->t("A domain is not valid."));
else
$dom = trim($dom);
}

if(isset($rcPaths) && is_array($rcPaths)) foreach ($rcPaths as &$path) {
if (!is_string($path)) {
$validation[] = $l->t("A path is not valid.");
break;
}
foreach ($rcPaths as &$path) {
if (!is_string($path))
return self::error_response($l->t("A path is not valid."));

$path = trim($path);
if (preg_match('/^([a-zA-Z]+:)?\/\//', $path) === 1 || $path === '') {
$validation[] = $l->t("Paths must be urls relative to this server.");
break;
} else {
if (preg_match('/^([a-zA-Z]+:)?\/\//', $path) === 1 || $path === '')
return self::error_response($l->t("Paths must be urls relative to this server."));
else
$path = ltrim($path, " /");
}
}

$rcDomains !== $rcPaths;
if (is_iterable($rcDomains)) {
$validation[] = $l->t("Unpaired domains and paths.");
}

// Won't change anything if validation fails.
if (!empty($validation)) {
return new JSONResponse(array(
'status' => 'error',
'message' => $l->t("Some inputs are not valid."),
'invalid' => $validation
));
}

// Passed validation.
$defaultRCPath = ltrim($defaultRCPath, " /");
$this->config->setAppValue($appName, 'defaultRCPath', $defaultRCPath);
$domainPath = json_encode(array_filter(
array_combine($rcDomains, $rcPaths),
function($v, $k) {
return $k !== '' && $v !== '';
},
ARRAY_FILTER_USE_BOTH
));

$this->config->setAppValue($appName, 'domainPath', $domainPath);

success_output:
$this->config->setAppValue($appName, 'defaultRCPath', $defaultRCPath);

$checkBoxes = array('showTopLine', 'enableSSLVerify');
foreach ($checkBoxes as $c) {
$this->config->setAppValue($appName, $c, $$c !== null);
Expand All @@ -143,4 +128,12 @@ function($v, $k) {
'config' => array('defaultRCPath' => $defaultRCPath)
));
}

protected function error_response(string $validation): JSONResponse {
return new JSONResponse(array(
'status' => 'error',
'message' => $this->l->t("Some inputs are not valid."),
'invalid' => array($validation)
));
}
}

0 comments on commit ccf68ef

Please sign in to comment.