From 54ecc337276f1854b209e4a258a198e5374091cb Mon Sep 17 00:00:00 2001 From: Christos Sidiropoulos Date: Thu, 14 Dec 2023 10:50:39 +0000 Subject: [PATCH] Refactor server URL generation Signed-off-by: Christos Sidiropoulos --- Classes/Controller/PageViewController.php | 28 ++++++++++++++++------- Classes/Plugin/FullTextGenerator.php | 4 ++-- Classes/Plugin/FullTextXMLtools.php | 2 +- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/Classes/Controller/PageViewController.php b/Classes/Controller/PageViewController.php index 17d44d66b..26fa4f1d0 100644 --- a/Classes/Controller/PageViewController.php +++ b/Classes/Controller/PageViewController.php @@ -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"; } } @@ -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. diff --git a/Classes/Plugin/FullTextGenerator.php b/Classes/Plugin/FullTextGenerator.php index 11d7f27eb..6b676bfcc 100644 --- a/Classes/Plugin/FullTextGenerator.php +++ b/Classes/Plugin/FullTextGenerator.php @@ -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 ''; //DEBUG diff --git a/Classes/Plugin/FullTextXMLtools.php b/Classes/Plugin/FullTextXMLtools.php index 44c0e0238..d7e78909c 100644 --- a/Classes/Plugin/FullTextXMLtools.php +++ b/Classes/Plugin/FullTextXMLtools.php @@ -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'); // $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(); }