From ef80cba5610b8acae8fc11d3f0a441c1f5a03735 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Tue, 3 Sep 2024 09:49:01 +0900 Subject: [PATCH] Add new functions: get IPs, strip UTF8 BOM from text, text updates Add the following new static methods Convert\Strings::stripUTF8BomBytes: removes the UTF8 BOM bytes from the beginning of a line Used for CSV files created in Excel for the first header entry (line 0/row 0) Get\Systen::getIpAddresses: gets all IP addresses for the the current access user and returns an array Moved the frontend folder detection from the first load config to the config.path.php Cleaned up the translations JS scripts --- 4dev/bin/create_mo.sh | 9 +-- 4dev/bin/mo_to_js.sh | 63 ++++++++++--------- www/configs/config.path.php | 16 ++++- www/configs/config.php | 15 +---- www/lib/CoreLibs/Convert/Strings.php | 12 ++++ www/lib/CoreLibs/DB/IO.php | 2 +- www/lib/CoreLibs/Get/System.php | 23 +++++++ .../CoreLibs/Template/HtmlBuilder/Block.php | 37 +++++++---- 8 files changed, 118 insertions(+), 59 deletions(-) diff --git a/4dev/bin/create_mo.sh b/4dev/bin/create_mo.sh index 3748abcc..57b92ca9 100755 --- a/4dev/bin/create_mo.sh +++ b/4dev/bin/create_mo.sh @@ -1,19 +1,20 @@ #!/usr/bin/env bash -BASE_FOLDER=$(dirname $(readlink -f $0))"/"; +BASE_FOLDER=$(dirname "$(readlink -f "$0")")"/"; # Assume script is in 4dev/bin base_folder="${BASE_FOLDER}../../www/"; # locale gettext po to mo translator master -for file in $(ls -1 ${base_folder}../4dev/locale/*.po); do - file=$(basename $file .po); +for file in "${base_folder}"../4dev/locale/*.po; do + [[ -e "$file" ]] || break + file=$(basename "$file" .po); locale=$(echo "${file}" | cut -d "-" -f 1); domain=$(echo "${file}" | cut -d "-" -f 2); echo "- Translate language file '${file}' for locale '${locale}' and domain '${domain}':"; if [ ! -d "${base_folder}/includes/locale/${locale}/LC_MESSAGES/" ]; then mkdir -p "${base_folder}/includes/locale/${locale}/LC_MESSAGES/"; fi; - msgfmt -o ${base_folder}/includes/locale/${locale}/LC_MESSAGES/${domain}.mo ${base_folder}../4dev/locale/${locale}-${domain}.po; + msgfmt -o "${base_folder}/includes/locale/${locale}/LC_MESSAGES/${domain}.mo" "${base_folder}../4dev/locale/${locale}-${domain}.po"; done; # __END__ diff --git a/4dev/bin/mo_to_js.sh b/4dev/bin/mo_to_js.sh index 34472ab6..78274e11 100755 --- a/4dev/bin/mo_to_js.sh +++ b/4dev/bin/mo_to_js.sh @@ -2,16 +2,18 @@ # read source mo files and writes target js files in object form -# check for ARG 1 is "mv" -# then move the files directly and don't do manual check (don't create temp files) -FILE_MOVE=0; -if [ "${1}" = "mv" ]; then +# check for ARG 1 is "no-move" +# then do not move the files directly for manual check +FILE_MOVE=1; +if [ "${1}" = "no-move" ]; then + echo "+++ CREATE TEMPORARY FILES +++"; + FILE_MOVE=0; +else echo "*** Direct write ***"; - FILE_MOVE=1; fi; target=''; -BASE_FOLDER=$(dirname $(readlink -f $0))"/"; +BASE_FOLDER=$(dirname "$(readlink -f "$0")")"/"; # Assume script is in 4dev/bin base_folder="${BASE_FOLDER}../../www/"; po_folder='../4dev/locale/' @@ -26,7 +28,7 @@ if [ "${target}" == '' ]; then echo "*** Non smarty ***"; TEXTDOMAINDIR=${base_folder}${mo_folder}. # default is admin - TEXTDOMAIN=admin; + TEXTDOMAIN="admin"; fi; js_folder="${TEXTDOMAIN}/layout/javascript/"; @@ -44,15 +46,16 @@ if [ ${error} -eq 1 ]; then fi; # locale gettext po to mo translator master -for file in $(ls -1 ${base_folder}../4dev/locale/*.po); do - file=$(basename $file .po); - echo "Translate language ${file}"; +for file in "${base_folder}"../4dev/locale/*.po; do + [[ -e "$file" ]] || break + file=$(basename "$file" .po); locale=$(echo "${file}" | cut -d "-" -f 1); domain=$(echo "${file}" | cut -d "-" -f 2); + echo "- Translate language file '${file}' for locale '${locale}' and domain '${domain}':"; if [ ! -d "${base_folder}/includes/locale/${locale}/LC_MESSAGES/" ]; then mkdir -p "${base_folder}/includes/locale/${locale}/LC_MESSAGES/"; fi; - msgfmt -o ${base_folder}/includes/locale/${locale}/LC_MESSAGES/${domain}.mo ${base_folder}${po_folder}${locale}-${domain}.po; + msgfmt -o "${base_folder}/includes/locale/${locale}/LC_MESSAGES/${domain}.mo" "${base_folder}${po_folder}${locale}-${domain}.po"; done; rx_msgid_empty="^msgid \"\""; @@ -62,7 +65,7 @@ rx_msgstr="^msgstr \"" # quick copy string at the end quick_copy=''; -for language in ${language_list[*]}; do +for language in "${language_list[@]}"; do # I don't know which one must be set, but I think at least LANGUAGE case ${language} in ja) @@ -79,7 +82,8 @@ for language in ${language_list[*]}; do esac; # write only one for language and then symlink files template_file=$(echo ${template_file_stump} | sed -e "s/##SUFFIX##//" | sed -e "s/##LANGUAGE##/${LANG}/"); - original_file=$(echo ${template_file} | sed -e 's/\.TMP//g'); + # original_file=$(echo ${template_file} | sed -e 's/\.TMP//g'); + original_file=${template_file//.TMP/}; if [ "${FILE_MOVE}" -eq 0 ]; then file=${target_folder}${template_file}; else @@ -88,16 +92,18 @@ for language in ${language_list[*]}; do echo "===> Write translation file ${file}"; echo ". = normal, : = escape, x = skip"; # init line [aka don't touch this file] - echo "// AUTO FILL, changes will be overwritten" > $file; - echo "// source: ${suffix}, language: ${language}" >> $file; - echo "// Translation strings in the format" >> $file; - echo "// \"Original\":\"Translated\""$'\n' >> $file; - echo "var i18n = {" >> $file; + echo "// AUTO FILL, changes will be overwritten" > "$file"; + { + echo "// source: ${suffix}, language: ${language}"; + echo "// Translation strings in the format"; + echo "// \"Original\":\"Translated\""$'\n' + echo "var i18n = {" + } >> "$file" # translations stuff # read the po file pos=0; # do we add a , for the next line cat "${base_folder}${po_folder}${language}-${TEXTDOMAIN}.po" | - while read str; do + while read -r str; do # echo "S: ${str}"; # skip empty if [[ "${str}" =~ ${rx_msgid_empty} ]]; then @@ -112,12 +118,13 @@ for language in ${language_list[*]}; do str_source=$(echo "${str}" | sed -e "s/^msgid \"//" | sed -e "s/\"$//"); # close right side, if not last add , if [ "${pos}" -eq 1 ]; then - echo -n "," >> $file; + echo -n "," >> "$file"; fi; # all " inside string need to be escaped - str_source=$(echo "${str_source}" | sed -e 's/"/\\"/g'); + # str_source=$(echo "${str_source}" | sed -e 's/"/\\"/g'); + str_source=${str_source//\"/\\\"} # fix with proper layout - echo -n "\"$str_source\":\"$(TEXTDOMAINDIR=${TEXTDOMAINDIR} LANGUAGE=${language} LANG=${LANG} gettext ${TEXTDOMAIN} "${str_source}")\"" >> $file; + echo -n "\"$str_source\":\"$(TEXTDOMAINDIR=${TEXTDOMAINDIR} LANGUAGE=${language} LANG=${LANG} gettext "${TEXTDOMAIN}" "${str_source}")\"" >> "$file"; pos=1; elif [[ "${str}" =~ ${rx_msgstr} ]]; then # open right side (ignore) @@ -128,8 +135,8 @@ for language in ${language_list[*]}; do fi; done; - echo "" >> $file; - echo "};" >> $file; + echo "" >> "$file"; + echo "};" >> "$file"; echo " [DONE]"; # on no move @@ -140,19 +147,19 @@ for language in ${language_list[*]}; do fi; # symlink to master file - for suffix in ${source_list[*]}; do + for suffix in "${source_list[@]}"; do # symlink with full lang name symlink_file[0]=$(echo ${template_file_stump} | sed -e "s/##SUFFIX##/${suffix}_/" | sed -e "s/##LANGUAGE##/${LANG}/" | sed -e 's/\.TMP//g'); # create second one with lang (no country) + encoding symlink_file[1]=$(echo ${template_file_stump} | sed -e "s/##SUFFIX##/${suffix}_/" | sed -e "s/##LANGUAGE##/${LANGUAGE}\.${ENCODING}/" | sed -e 's/\.TMP//g'); - for template_file in ${symlink_file[@]}; do + for template_file in "${symlink_file[@]}"; do # if this is not symlink, create them if [ ! -h "${template_file}" ]; then echo "Create symlink: ${template_file}"; # symlik to original - cd "${target_folder}"; + cd "${target_folder}" || exit; ln -sf "${original_file}" "${template_file}"; - cd - >/dev/null; + cd - >/dev/null || exit; fi; done; done; diff --git a/www/configs/config.path.php b/www/configs/config.path.php index 34abe19e..69a78d00 100644 --- a/www/configs/config.path.php +++ b/www/configs/config.path.php @@ -1,4 +1,4 @@ - false, // when fetch array or cache read returns false - // in loop read that means dbReturn retuns false without erro + // in loop read that means dbReturn retuns false without error 'finished' => false, // read from cache/db (pos == rows) 'read_finished' => false, diff --git a/www/lib/CoreLibs/Get/System.php b/www/lib/CoreLibs/Get/System.php index 690b43b8..25196b63 100644 --- a/www/lib/CoreLibs/Get/System.php +++ b/www/lib/CoreLibs/Get/System.php @@ -116,6 +116,29 @@ class System 3 ) === 'cli' ? true : false; } + + /** + * Collect all IP addresses + * REMOTE_ADDR, HTTP_X_FORWARD_FOR, CLIENT_IP + * and retuns them in an array with index of io source + * if address source has addresses with "," will add "-array" with these as array block + * + * @return array + */ + public static function getIpAddresses(): array + { + $ip_addr = []; + foreach (['REMOTE_ADDR', 'HTTP_X_FORWARDED_FOR', 'CLIENT_IP'] as $_ip_source) { + if (!empty($_SERVER[$_ip_source])) { + $ip_addr[$_ip_source] = $_SERVER[$_ip_source]; + // same level as ARRAY IF there is a , inside + if (strstr($_SERVER[$_ip_source], ',') !== false) { + $ip_addr[$_ip_source . '-array'] = explode(',', $_SERVER[$_ip_source]); + } + } + } + return $ip_addr; + } } // __END__ diff --git a/www/lib/CoreLibs/Template/HtmlBuilder/Block.php b/www/lib/CoreLibs/Template/HtmlBuilder/Block.php index 2015280e..2ef8549c 100644 --- a/www/lib/CoreLibs/Template/HtmlBuilder/Block.php +++ b/www/lib/CoreLibs/Template/HtmlBuilder/Block.php @@ -19,7 +19,7 @@ use CoreLibs\Template\HtmlBuilder\General\HtmlBuilderExcpetion; class Block { /** - * Undocumented function + * Create Element * * @param string $tag * @param string $id @@ -86,7 +86,7 @@ class Block } /** - * Undocumented function + * Add multiple elements to the base element * * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} $base * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} ...$attach @@ -101,7 +101,7 @@ class Block } /** - * Undocumented function + * Add multiple sub elements to the base element * * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} $element * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} $sub @@ -117,7 +117,7 @@ class Block } /** - * Undocumented function + * Remove all sub element entries * * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} $element * @return array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} @@ -131,7 +131,7 @@ class Block // CSS Elements /** - * Undocumented function + * Add css entry to the css entries * * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} $element * @param string ...$css @@ -144,7 +144,7 @@ class Block } /** - * Undocumented function + * Remove a css entry entry from the css array * * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} $element * @param string ...$css @@ -157,7 +157,7 @@ class Block } /** - * Undocumented function + * Switch CSS entries * scssel (switch) is not supported * use rcssel -> acssel * @@ -175,7 +175,7 @@ class Block } /** - * Undocumented function + * Build HTML from the content tree * alias phfo * * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} $tree @@ -231,7 +231,23 @@ class Block } /** - * Undocumented function + * Alias for phfo + * + * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} $tree + * @param bool $add_nl [default=false] + * @return string + * + * @param array $tree + * @param bool $add_nl + * @return string + */ + public static function phfo(array $tree, bool $add_nl = false): string + { + return self::buildHtml($tree, $add_nl); + } + + /** + * Build HTML elements from an array of elements * alias phfa * * @param array,options:array,sub:array}> $list @@ -248,8 +264,7 @@ class Block } /** - * Undocumented function - * wrapper for buildHtmlFromList + * alias for buildHtmlFromList * * @param array,options:array,sub:array}> $list array of Elements to build string from * @param bool $add_nl [default=false] Optional output string line break