Skip to content

Commit

Permalink
[MAINTENANCE] Sanitize settings from FlexForm (#1324)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Meyer <[email protected]>
  • Loading branch information
beatrycze-volk and sebastian-meyer authored Sep 9, 2024
1 parent d27b894 commit 31a6e84
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 31 deletions.
51 changes: 50 additions & 1 deletion Classes/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,55 @@ protected function sanitizeRequestData(): void
$this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'] ?? 0, 0, 1);
}

/**
* Sanitize settings from FlexForm.
*
* @access protected
*
* @return void
*/
protected function sanitizeSettings(): void
{
$this->setDefaultIntSetting('storagePid', 0);

if ($this instanceof MetadataController) {
$this->setDefaultIntSetting('rootline', 0);
$this->setDefaultIntSetting('originalIiifMetadata', 0);
$this->setDefaultIntSetting('displayIiifDescription', 1);
$this->setDefaultIntSetting('displayIiifRights', 1);
$this->setDefaultIntSetting('displayIiifLinks', 1);
}

if ($this instanceof OaiPmhController) {
$this->setDefaultIntSetting('limit', 5);
$this->setDefaultIntSetting('solr_limit', 50000);
}

if ($this instanceof PageViewController) {
$this->setDefaultIntSetting('useInternalProxy', 0);
}
}

/**
* Sets default value for setting if not yet set.
*
* @access protected
*
* @param string $setting name of setting
* @param int $value for being set if empty
*
* @return void
*/
protected function setDefaultIntSetting(string $setting, int $value): void
{
if (empty($this->settings[$setting])) {
$this->settings[$setting] = $value;
$this->logger->warning('Setting "' . $setting . '" not set, using default value "' . $value . '". Probably FlexForm for controller "' . get_class($this) . '" is not read.');
} else {
$this->settings[$setting] = (int) $this->settings[$setting];
}
}

/**
* Sets page value.
*
Expand Down Expand Up @@ -533,7 +582,7 @@ protected function getDocumentByUrl(string $documentId)

// Make sure configuration PID is set when applicable
if ($doc->cPid == 0) {
$doc->cPid = max((int) $this->settings['storagePid'], 0);
$doc->cPid = max($this->settings['storagePid'], 0);
}

$this->document->setLocation($documentId);
Expand Down
27 changes: 2 additions & 25 deletions Classes/Controller/MetadataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,10 @@ public function mainAction(): void
if ($this->isDocMissing()) {
// Quit without doing anything if required variables are not set.
return;
} else {
// Set default values if not set.
$this->setDefault('rootline', 0);
$this->setDefault('originalIiifMetadata', 0);
$this->setDefault('displayIiifDescription', 1);
$this->setDefault('displayIiifRights', 1);
$this->setDefault('displayIiifLinks', 1);
$this->setPage();
}

$this->setPage();

$this->currentDocument = $this->document->getCurrentDocument();
$this->useOriginalIiifManifestMetadata = $this->settings['originalIiifMetadata'] == 1 && $this->currentDocument instanceof IiifManifest;

Expand Down Expand Up @@ -529,21 +523,4 @@ private function getMetadataForIds(array $id, array $metadata): array
}
return $metadata;
}

/**
* Sets default value for setting if not yet set.
*
* @access private
*
* @param string $setting name of setting
* @param int $value 0 or 1
*
* @return void
*/
private function setDefault(string $setting, int $value): void
{
if (!isset($this->settings[$setting])) {
$this->settings[$setting] = $value;
}
}
}
6 changes: 3 additions & 3 deletions Classes/Controller/OaiPmhController.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,8 @@ protected function fetchDocumentSet(): array
$this->logger->error('Apache Solr not available');
return $documentSet;
}
if ((int) $this->settings['solr_limit'] > 0) {
$solr->limit = (int) $this->settings['solr_limit'];
if ($this->settings['solr_limit'] > 0) {
$solr->limit = $this->settings['solr_limit'];
}
// We only care about the UID in the results and want them sorted
$parameters = [
Expand Down Expand Up @@ -785,7 +785,7 @@ private function checkGranularity(): void
*/
protected function generateOutputForDocumentList(array $documentListSet)
{
$documentsToProcess = array_splice($documentListSet['elements'], 0, (int) $this->settings['limit']);
$documentsToProcess = array_splice($documentListSet['elements'], 0, $this->settings['limit']);
if (empty($documentsToProcess)) {
$this->error = 'noRecordsMatch';
return [];
Expand Down
4 changes: 2 additions & 2 deletions Classes/Controller/PageViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ protected function addViewerJS(): void
'score' => $docScore,
'annotationContainers' => $docAnnotationContainers,
'measureCoords' => $docMeasures['measureCoordsCurrentSite'],
'useInternalProxy' => $this->settings['useInternalProxy'] ? 1 : 0,
'useInternalProxy' => $this->settings['useInternalProxy'],
'currentMeasureId' => $currentMeasureId,
'measureIdLinks' => $docMeasures['measureLinks']
];
Expand Down Expand Up @@ -539,7 +539,7 @@ protected function addViewerJS(): void
'score' => $this->scores,
'annotationContainers' => $this->annotationContainers,
'measureCoords' => $docMeasures['measureCoordsCurrentSite'],
'useInternalProxy' => $this->settings['useInternalProxy'] ? 1 : 0,
'useInternalProxy' => $this->settings['useInternalProxy'],
'verovioAnnotations' => $this->verovioAnnotations,
'currentMeasureId' => $currentMeasureId,
'measureIdLinks' => $docMeasures['measureLinks']
Expand Down

0 comments on commit 31a6e84

Please sign in to comment.