Skip to content

Commit

Permalink
Refactor server URL generation
Browse files Browse the repository at this point in the history
Signed-off-by: Christos Sidiropoulos <[email protected]>
  • Loading branch information
csidirop committed Dec 14, 2023
1 parent 820ec5f commit 54ecc33
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
28 changes: 20 additions & 8 deletions Classes/Controller/PageViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,7 @@ protected function getFulltext($page)
//check if local fulltext exists:
if (PageViewController::getOCRengine(Doc::$extKey) != "originalremote") {
if (FullTextGenerator::checkLocal(Doc::$extKey, $this->document, $page)) { //fulltext is locally present
//check server protocol (https://stackoverflow.com/a/14270161):
if ( isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1)
|| isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$protocol = 'https://';
} else {
$protocol = 'http://';
}
$fulltext['url'] = $protocol . $_SERVER['HTTP_HOST'] . "/" . FullTextGenerator::getPageLocalPath(Doc::$extKey, $this->document, $page);
$fulltext['url'] = PageViewController::getServerUrl() . "/" . FullTextGenerator::getPageLocalPath(Doc::$extKey, $this->document, $page);
$fulltext['mimetype'] = "text/xml";
}
}
Expand Down Expand Up @@ -435,6 +428,25 @@ protected function generateFullText():void {
FullTextGenerator::createPageFullText(Doc::$extKey, $this->document, $this->getImage($this->requestData['page'])["url"], $this->requestData['page'], $engine);
}

/**
* Returns the server URL (including networkprotocol: http or https)
* eg. https://www.example.com
*
* @access public
*
* @return string The server URL
*/
public static function getServerUrl():string {
//check server protocol (parts from https://stackoverflow.com/a/14270161):
if (GeneralUtility::getIndpEnv('TYPO3_SSL') == true
|| isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1)
|| isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
return 'https://' . $_SERVER['HTTP_HOST'];
} else {
return 'http://'. $_SERVER['HTTP_HOST'];
}
}

/**
* This function is a workaround to circumvent TYPO3s disturbing caching.
* It clears the stored page cache (for presentations viewer only!) on calling.
Expand Down
4 changes: 2 additions & 2 deletions Classes/Plugin/FullTextGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,10 @@ protected static function generatePageOCR(string $extKey, array $conf, Document
//Determine if the image should be downloaded. Than use remote URL ($imageUrl) or local PATH ($tmpImagePath):
if ($conf['ocrDwnlTempImage']){ //download image
$imageDownloadCommand = "wget $imageUrl -O $tmpImagePath"; //wget image and save to $tmpImagePath
$ocrShellCommand .= self::genShellCommand($ocrEnginePath, $tmpImagePath, $tmpOutputPath, $outputPath, $tmpImagePath, $pageId, $conf['ocrPlaceholderText'], "http://".$_SERVER['HTTP_HOST']."/".$outputPath, $conf['ocrUpdateMets'], $conf['ocrIndexMets']);
$ocrShellCommand .= self::genShellCommand($ocrEnginePath, $tmpImagePath, $tmpOutputPath, $outputPath, $tmpImagePath, $pageId, $conf['ocrPlaceholderText'], PageViewController::getServerUrl()."/".$outputPath, $conf['ocrUpdateMets'], $conf['ocrIndexMets']);
$ocrShellCommand .= " && rm $tmpImagePath"; // Remove used image
} else { //do not download image, pass URL to the engine
$ocrShellCommand .= self::genShellCommand($ocrEnginePath, $imageUrl, $tmpOutputPath, $outputPath, $tmpImagePath, $pageId, $pageNum, $conf['ocrPlaceholderText'], "http://".$_SERVER['HTTP_HOST']."/".$outputPath, $conf['ocrUpdateMets'], $conf['ocrIndexMets']);
$ocrShellCommand .= self::genShellCommand($ocrEnginePath, $imageUrl, $tmpOutputPath, $outputPath, $tmpImagePath, $pageId, $pageNum, $conf['ocrPlaceholderText'], PageViewController::getServerUrl()."/".$outputPath, $conf['ocrUpdateMets'], $conf['ocrIndexMets']);
}

/* DEBUG */ if($conf['ocrDebug']) echo '<script>alert("'.$ocrShellCommand.'")</script>'; //DEBUG
Expand Down
2 changes: 1 addition & 1 deletion Classes/Plugin/FullTextXMLtools.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ protected static function updateMetsNode(XMLWriter $writer, string $alto_path, s
$writer->writeAttribute('SOFTWARE', "DFG-Viewer-5-OCR-$ocr_script");
$writer->startElement('mets:FLocat'); // <mets:FLocat LOCTYPE="URL" xlink:href="https://digi.bib.uni-mannheim.de/fileadmin/digi/log59088/alto/log59088_431.xml"/>
$writer->writeAttribute('LOCTYPE', 'URL');
$writer->writeAttribute('xlink:href', "http://".$_SERVER['HTTP_HOST']."/".$alto_path);
$writer->writeAttribute('xlink:href', PageViewController::getServerUrl()."/".$alto_path);
$writer->endElement();
$writer->endElement();
}
Expand Down

0 comments on commit 54ecc33

Please sign in to comment.