Migrate function file to PHP7. Code Cleanup.

This commit is contained in:
Joshua Ramon Enslin 2019-08-15 00:10:48 +02:00 committed by Stefan Rohde-Enslin
parent 28b8874110
commit 6e5b8ec72e

View File

@ -8,14 +8,14 @@
/** /**
* Browsersprache ermitteln * Browsersprache ermitteln
* *
* @param array $allowed_languages Array of allowed languages * @param array $allowed_languages Array of allowed languages.
* @param string $default_language Language code of the default language * @param string $default_language Language code of the default language.
* @param string $lang_variable Already set language variable * @param string|null $lang_variable Already set language variable.
* @param bool $strict_mode Whether to demand de (false) or de-de * @param boolean $strict_mode Whether to demand de (false) or de-de.
* *
* @return string * @return string
*/ */
function lang_getfrombrowser($allowed_languages, $default_language, $lang_variable = null, $strict_mode = true) { function lang_getfrombrowser(array $allowed_languages, string $default_language, $lang_variable = null, bool $strict_mode = true) {
// $_SERVER['HTTP_ACCEPT_LANGUAGE'] verwenden, wenn keine Sprachvariable mitgegeben wurde // $_SERVER['HTTP_ACCEPT_LANGUAGE'] verwenden, wenn keine Sprachvariable mitgegeben wurde
if ($lang_variable === null) { if ($lang_variable === null) {
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) $lang_variable = $_SERVER['HTTP_ACCEPT_LANGUAGE']; if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) $lang_variable = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
@ -37,8 +37,7 @@ function lang_getfrombrowser($allowed_languages, $default_language, $lang_variab
// Nun alle mitgegebenen Sprachen abarbeiten // Nun alle mitgegebenen Sprachen abarbeiten
foreach ($accepted_languages as $accepted_language) { foreach ($accepted_languages as $accepted_language) {
// Alle Infos über diese Sprache rausholen // Alle Infos über diese Sprache rausholen
$res = preg_match('/^([a-z]{1,8}(?:-[a-z]{1,8})*)'. $res = preg_match('/^([a-z]{1,8}(?:-[a-z]{1,8})*)' . '(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i', $accepted_language, $matches
'(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i', $accepted_language, $matches
); );
// war die Syntax gültig? // war die Syntax gültig?
@ -93,7 +92,7 @@ function lang_getfrombrowser($allowed_languages, $default_language, $lang_variab
* *
* @return void * @return void
*/ */
function ensureDir($filepath) { function ensureDir(string $filepath) {
if (!is_dir($filepath)) mkdir($filepath); if (!is_dir($filepath)) mkdir($filepath);
} }
@ -104,7 +103,7 @@ function ensureDir($filepath) {
* *
* @return void * @return void
*/ */
function ensureJson($filepath) { function ensureJson(string $filepath) {
if (!file_exists($filepath) or filesize($filepath) < 2) { if (!file_exists($filepath) or filesize($filepath) < 2) {
file_put_contents($filepath, "[]"); file_put_contents($filepath, "[]");
@ -134,21 +133,21 @@ function ensureEnvironment() {
/** /**
* Function transform escapes a string for printing in TeX * Function transform escapes a string for printing in TeX
* *
* @param string $transform String to transform * @param string $transform String to transform.
* *
* @return string * @return string
*/ */
function transform($transform) { function transform(string $transform):string {
$transform = preg_replace('#<br>#', ' ', $transform); $transform = preg_replace('#<br>#', ' ', $transform);
$transform = preg_replace('#</br>#', ' ', $transform); $transform = preg_replace('#</br>#', ' ', $transform);
$transform = preg_replace('#<br />#', ' ', $transform); $transform = preg_replace('#<br />#', ' ', $transform);
$transform = preg_replace('#&rsquo;#', '\'', $transform); $transform = preg_replace('#&rsquo;#', '\'', $transform);
$transform = preg_replace('#'.chr(11).'#', ' ', $transform); $transform = preg_replace('#' . chr(11) . '#', ' ', $transform);
$transform = preg_replace('#&quot;#', '"', $transform); $transform = preg_replace('#&quot;#', '"', $transform);
$transform = preg_replace('#&amp;#', '&', $transform); $transform = preg_replace('#&amp;#', '&', $transform);
$transform = preg_replace('#•#', '&#8226;', $transform); $transform = preg_replace('#•#', '&#8226;', $transform);
$transform = preg_replace('#'.chr(9).'#', '', $transform); $transform = preg_replace('#' . chr(9) . '#', '', $transform);
$transform = str_replace('&', '\\&', $transform); $transform = str_replace('&', '\\&', $transform);
$transform = str_replace('{', '\\{', $transform); $transform = str_replace('{', '\\{', $transform);
$transform = str_replace('}', '\\}', $transform); $transform = str_replace('}', '\\}', $transform);
@ -179,7 +178,7 @@ function transform($transform) {
$transform = preg_replace('#è#', '&#232;', $transform); $transform = preg_replace('#è#', '&#232;', $transform);
foreach (array("[", "]") as $toescape) foreach (array("[", "]") as $toescape)
$transform = str_replace ($toescape, "$".$toescape."$", $transform); $transform = str_replace ($toescape, "$" . $toescape . "$", $transform);
$transform = html_entity_decode($transform, ENT_QUOTES, "utf-8" ); $transform = html_entity_decode($transform, ENT_QUOTES, "utf-8" );
return $transform; return $transform;
@ -192,7 +191,7 @@ function transform($transform) {
* *
* @return string[] * @return string[]
*/ */
function scanDirConts($folder) { function scanDirConts(string $folder) {
return array_values(array_diff(scandir($folder), [".", "..", ".git"])); return array_values(array_diff(scandir($folder), [".", "..", ".git"]));
} }
@ -206,8 +205,8 @@ function scanDirConts($folder) {
* *
* @return void * @return void
*/ */
function copyFolder($from, $to) { function copyFolder(string $from, string $to) {
mkdir ($to); mkdir($to);
foreach (array_diff(scandir("$from"), [".", ".."]) as $file) { foreach (array_diff(scandir("$from"), [".", ".."]) as $file) {
copy("$from/$file", "$to/$file"); copy("$from/$file", "$to/$file");
} }
@ -220,7 +219,7 @@ function copyFolder($from, $to) {
* *
* @return void * @return void
*/ */
function removeFolder($folder) { function removeFolder(string $folder) {
foreach (array_diff(scandir("$folder"), [".", ".."]) as $file) { foreach (array_diff(scandir("$folder"), [".", ".."]) as $file) {
if (is_dir("$folder/$file")) removeFolder("$folder/$file"); if (is_dir("$folder/$file")) removeFolder("$folder/$file");
else unlink("$folder/$file"); else unlink("$folder/$file");
@ -237,7 +236,7 @@ function removeFolder($folder) {
* *
* @return boolean * @return boolean
*/ */
function removeFolderGently($folder, $exclude) { function removeFolderGently(string $folder, string $exclude) {
foreach (array_diff(scandir("$folder"), [".", ".."]) as $file) { foreach (array_diff(scandir("$folder"), [".", ".."]) as $file) {
if (time() - filemtime("$folder/$file") < 10 and $file != $exclude) continue; if (time() - filemtime("$folder/$file") < 10 and $file != $exclude) continue;
if (is_dir("$folder/$file")) $output = removeFolder("$folder/$file"); if (is_dir("$folder/$file")) $output = removeFolder("$folder/$file");
@ -254,13 +253,13 @@ function removeFolderGently($folder, $exclude) {
* @param string $folder Determines the folder (say, template) to use for PDF generation. * @param string $folder Determines the folder (say, template) to use for PDF generation.
* @param string $texFilename Name of the TeX file. Used without its ending. * @param string $texFilename Name of the TeX file. Used without its ending.
* @param string $filename Filename as it should be shown to the user. * @param string $filename Filename as it should be shown to the user.
* @param boolean $pdfSettings Additional parameters to use \def for. * @param array $pdfSettings Additional parameters to use \def for.
* @param boolean $lang Language. Optional. * @param string $lang Language. Optional.
* @param boolean $debug Enables/disables debug mode. Optional. * @param boolean $debug Enables/disables debug mode. Optional.
* *
* @return void * @return void
*/ */
function runPDF($folder, $texFilename, $filename, $pdfSettings, $lang = "de", $debug = false) { function runPDF(string $folder, string $texFilename, string $filename, array $pdfSettings, string $lang = "de", bool $debug = false) {
if (!is_dir("./tmp")) mkdir("./tmp"); if (!is_dir("./tmp")) mkdir("./tmp");
@ -316,14 +315,14 @@ function runPDF($folder, $texFilename, $filename, $pdfSettings, $lang = "de", $d
* *
* @param string $title Title of the page. * @param string $title Title of the page.
* @param string $contents Page contents. * @param string $contents Page contents.
* @param string $lang Language. Optional * @param string $lang Language. Optional.
* @param array $availableLangs Page description. Optional. * @param array $availableLangs Page description. Optional.
* @param string $descriptions Page description. Optional. * @param string $descriptions Page description. Optional.
* @param string $pdfLink Link to brochure. Optional. * @param string $pdfLink Link to brochure. Optional.
* *
* @return string * @return string
*/ */
function printHTMLPage(string $title, string $contents, string $lang = "en", array $availableLangs = ["en"], string $descriptions = "", $pdfLink = "brochure") { function printHTMLPage(string $title, string $contents, string $lang = "en", array $availableLangs = ["en"], string $descriptions = "", string $pdfLink = "brochure"):string {
$output = '<!DOCTYPE html> $output = '<!DOCTYPE html>
<html lang="' . $lang . '" id="' . str_replace(":", "", str_replace(" ", "", $title)) . '"> <html lang="' . $lang . '" id="' . str_replace(":", "", str_replace(" ", "", $title)) . '">
@ -336,9 +335,9 @@ function printHTMLPage(string $title, string $contents, string $lang = "en", arr
<link rel="shortcut icon" sizes="16x16 32x32" href="./media/mdlogo-32px.png" /> <link rel="shortcut icon" sizes="16x16 32x32" href="./media/mdlogo-32px.png" />
<link rel="shortcut icon" sizes="64x64" href="./media/mdlogo-64px.png" /><link rel="stylesheet" type="text/css" href="css/main.css"> <link rel="shortcut icon" sizes="64x64" href="./media/mdlogo-64px.png" /><link rel="stylesheet" type="text/css" href="css/main.css">
<meta name="twitter:title" content="' . $title. '" /> <meta name="twitter:title" content="' . $title . '" />
<meta property="og:title" content="' . $title. '" /> <meta property="og:title" content="' . $title . '" />
<title>' . $title. '</title> <title>' . $title . '</title>
<meta name="twitter:card" content="summary" /> <meta name="twitter:card" content="summary" />
<meta name="twitter:site" content="@museumdigital" /> <meta name="twitter:site" content="@museumdigital" />