Compare commits
65 Commits
v9.31.0
...
composerLi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
40e6444c2a | ||
|
|
4b015505ff | ||
|
|
e90387c1fc | ||
|
|
3fb7169531 | ||
|
|
e4dd73d0e9 | ||
|
|
e71c53b887 | ||
|
|
16addc4f4b | ||
|
|
7e845d3954 | ||
|
|
6fcb7a44e0 | ||
|
|
e82e4f6079 | ||
|
|
231897cf5b | ||
|
|
17da804073 | ||
|
|
96ad4b0f48 | ||
|
|
1386afb552 | ||
|
|
86a9ad8789 | ||
|
|
0c4c018ffa | ||
|
|
8b36807a2e | ||
|
|
8daef88e5e | ||
|
|
a59fa7a2c9 | ||
|
|
14c8197a7f | ||
|
|
79bd7fa256 | ||
|
|
9ba09b22f5 | ||
|
|
e88ad00d7f | ||
|
|
9b80fde0d7 | ||
|
|
f99e8bb70c | ||
|
|
b48894d000 | ||
|
|
5ebe7dc06c | ||
|
|
2e6b7b2f5b | ||
|
|
897406456a | ||
|
|
edb0620308 | ||
|
|
90edcbf8c8 | ||
|
|
40f267f3dd | ||
|
|
c8aee19deb | ||
|
|
74c8b8d71e | ||
|
|
c0db3be770 | ||
|
|
e74bd04d6f | ||
|
|
3662b1ab1b | ||
|
|
7abce87653 | ||
|
|
b3e35b5d94 | ||
|
|
6429b77bda | ||
|
|
3c8bdab8fa | ||
|
|
3cf6fee548 | ||
|
|
f5a9757ae3 | ||
|
|
3d6b461b20 | ||
|
|
513b115d57 | ||
|
|
eb16f433e8 | ||
|
|
8f94201478 | ||
|
|
1b2359a934 | ||
|
|
d6187005f4 | ||
|
|
f0e6b5b8e9 | ||
|
|
6b400978ac | ||
|
|
2754a718fa | ||
|
|
37c3b6afeb | ||
|
|
516b11f2f1 | ||
|
|
75a42558fd | ||
|
|
48271a8659 | ||
|
|
35d3032df5 | ||
|
|
7be8bb06c9 | ||
|
|
2aab94a842 | ||
|
|
1cbe4e5c06 | ||
|
|
137fb9a986 | ||
|
|
e1357f5d39 | ||
|
|
8766d4db77 | ||
|
|
b696338324 | ||
|
|
43e66edfd1 |
8
.gitignore
vendored
8
.gitignore
vendored
@@ -1,9 +1,3 @@
|
||||
.libs
|
||||
node_modules/
|
||||
composer.lock
|
||||
vendor/
|
||||
tools/
|
||||
www/composer.lock
|
||||
www/vendor
|
||||
**/.env
|
||||
**/.target
|
||||
.env
|
||||
|
||||
@@ -27,7 +27,6 @@ use Phan\Config;
|
||||
|
||||
return [
|
||||
// "target_php_version" => "8.2",
|
||||
"minimum_target_php_version" => "8.2",
|
||||
// turn color on (-C)
|
||||
"color_issue_messages_if_supported" => true,
|
||||
// If true, missing properties will be created when
|
||||
@@ -96,6 +95,8 @@ return [
|
||||
"exclude_analysis_directory_list" => [
|
||||
'www/vendor',
|
||||
'www/tests',
|
||||
'www/lib/Smarty',
|
||||
'www/lib/smarty-4.3.0',
|
||||
'www/templates_c',
|
||||
'www/log',
|
||||
'www/tmp',
|
||||
@@ -116,6 +117,10 @@ return [
|
||||
// ignore the old qq tests
|
||||
'www/admin/qq_file_upload_front.php',
|
||||
'www/admin/qq_file_upload_ajax.php',
|
||||
// symlink ignore
|
||||
'www/lib/smarty-4.3.0/libs/Smarty.class.php',
|
||||
// legacy edit base (until removal)
|
||||
'www/includes/edit_base.LEGACY.php'
|
||||
],
|
||||
|
||||
// what not to show as problem
|
||||
@@ -128,12 +133,7 @@ return [
|
||||
'PhanWriteOnlyPublicProperty',
|
||||
'PhanUnreferencedConstant',
|
||||
'PhanWriteOnlyPublicProperty',
|
||||
'PhanReadOnlyPublicProperty',
|
||||
// start ignore annotations
|
||||
'PhanUnextractableAnnotationElementName',
|
||||
'PhanUnextractableAnnotationSuffix',
|
||||
// enum problems in comments
|
||||
'PhanCommentObjectInClassConstantType'
|
||||
'PhanReadOnlyPublicProperty'
|
||||
],
|
||||
|
||||
// Override to hardcode existence and types of (non-builtin) globals in the global scope.
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phive xmlns="https://phar.io/phive">
|
||||
<phar name="phpunit" version="^10.3.5" installed="10.3.5" location="./tools/phpunit" copy="false"/>
|
||||
<phar name="phpcbf" version="^3.7.2" installed="3.10.3" location="./tools/phpcbf" copy="false"/>
|
||||
<phar name="phpcs" version="^3.10.3" installed="3.10.3" location="./tools/phpcs" copy="false"/>
|
||||
<phar name="phpstan" version="^2.0" installed="2.0.4" location="./tools/phpstan" copy="false"/>
|
||||
<phar name="phan" version="^5.4.3" installed="5.4.3" location="./tools/phan" copy="false"/>
|
||||
<phar name="psalm" version="^5.15.0" installed="5.24.0" location="./tools/psalm" copy="false"/>
|
||||
<phar name="phpdox" version="^0.12.0" installed="0.12.0" location="./tools/phpdox" copy="false"/>
|
||||
<phar name="phpdocumentor" version="^3.4.2" installed="3.4.3" location="./tools/phpDocumentor" copy="false"/>
|
||||
<phar name="php-cs-fixer" version="^3.34.1" installed="3.57.2" location="./tools/php-cs-fixer" copy="false"/>
|
||||
</phive>
|
||||
@@ -1,2 +0,0 @@
|
||||
shell=bash
|
||||
external-sources=true
|
||||
@@ -1,20 +1,19 @@
|
||||
#!/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 "${base_folder}"../4dev/locale/*.po; do
|
||||
[[ -e "$file" ]] || break
|
||||
file=$(basename "$file" .po);
|
||||
for file in $(ls -1 ${base_folder}../4dev/locale/*.po); do
|
||||
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__
|
||||
|
||||
@@ -2,18 +2,16 @@
|
||||
|
||||
# read source mo files and writes target js files in object form
|
||||
|
||||
# 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
|
||||
# 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
|
||||
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/'
|
||||
@@ -28,9 +26,9 @@ if [ "${target}" == '' ]; then
|
||||
echo "*** Non smarty ***";
|
||||
TEXTDOMAINDIR=${base_folder}${mo_folder}.
|
||||
# default is admin
|
||||
TEXTDOMAIN="admin";
|
||||
TEXTDOMAIN=admin;
|
||||
fi;
|
||||
js_folder="${TEXTDOMAIN}/layout/javascript/";
|
||||
js_folder="layout/${TEXTDOMAIN}/javascript/";
|
||||
|
||||
error=0;
|
||||
# this checks if the TEXTDOMAIN target actually exists
|
||||
@@ -46,16 +44,15 @@ if [ ${error} -eq 1 ]; then
|
||||
fi;
|
||||
|
||||
# locale gettext po to mo translator master
|
||||
for file in "${base_folder}"../4dev/locale/*.po; do
|
||||
[[ -e "$file" ]] || break
|
||||
file=$(basename "$file" .po);
|
||||
for file in $(ls -1 ${base_folder}../4dev/locale/*.po); do
|
||||
file=$(basename $file .po);
|
||||
echo "Translate language ${file}";
|
||||
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 \"\"";
|
||||
@@ -65,7 +62,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)
|
||||
@@ -82,8 +79,7 @@ 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=${template_file//.TMP/};
|
||||
original_file=$(echo ${template_file} | sed -e 's/\.TMP//g');
|
||||
if [ "${FILE_MOVE}" -eq 0 ]; then
|
||||
file=${target_folder}${template_file};
|
||||
else
|
||||
@@ -92,18 +88,16 @@ 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}";
|
||||
echo "// Translation strings in the format";
|
||||
echo "// \"Original\":\"Translated\""$'\n'
|
||||
echo "var i18n = {"
|
||||
} >> "$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;
|
||||
# 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 -r str; do
|
||||
while read str; do
|
||||
# echo "S: ${str}";
|
||||
# skip empty
|
||||
if [[ "${str}" =~ ${rx_msgid_empty} ]]; then
|
||||
@@ -118,13 +112,12 @@ 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=${str_source//\"/\\\"}
|
||||
str_source=$(echo "${str_source}" | sed -e 's/"/\\"/g');
|
||||
# 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)
|
||||
@@ -135,8 +128,8 @@ for language in "${language_list[@]}"; do
|
||||
fi;
|
||||
done;
|
||||
|
||||
echo "" >> "$file";
|
||||
echo "};" >> "$file";
|
||||
echo "" >> $file;
|
||||
echo "};" >> $file;
|
||||
echo " [DONE]";
|
||||
|
||||
# on no move
|
||||
@@ -147,19 +140,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}" || exit;
|
||||
cd "${target_folder}";
|
||||
ln -sf "${original_file}" "${template_file}";
|
||||
cd - >/dev/null || exit;
|
||||
cd - >/dev/null;
|
||||
fi;
|
||||
done;
|
||||
done;
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
base="/storage/var/www/html/developers/clemens/core_data/php_libraries/trunk/";
|
||||
# must be run in ${base}
|
||||
cd $base || exit;
|
||||
${base}tools/phan --progress-bar -C --analyze-twice;
|
||||
cd ~ || exit;
|
||||
# must be run in ${base}www/
|
||||
phan --progress-bar -C --analyze-twice
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
base="/storage/var/www/html/developers/clemens/core_data/php_libraries/trunk/";
|
||||
# must be run in ${base}
|
||||
cd $base || exit;
|
||||
${base}tools/phpstan;
|
||||
cd ~ || exit;
|
||||
# must be run in ${base}www/
|
||||
phpstan
|
||||
|
||||
@@ -1,96 +1,44 @@
|
||||
#!/bin/env bash
|
||||
|
||||
function error() {
|
||||
if [ -t 1 ]; then echo "[MAK] ERROR: $*" >&2; fi; exit 0;
|
||||
}
|
||||
|
||||
usage() {
|
||||
cat <<EOF
|
||||
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-t] [-v] [-p VERSION]
|
||||
|
||||
Runs all the PHP unit tests.
|
||||
|
||||
If -p is not set, the default intalled PHP is used.
|
||||
|
||||
Available options:
|
||||
|
||||
-h, --help Print this help and exit
|
||||
-t, --testdox Enable testdox output for phpunit
|
||||
-v, --verbose Enable verbose output for PHPunit
|
||||
-p, --php VERSION Chose PHP version in the form of "N.N", if not found will exit
|
||||
EOF
|
||||
exit
|
||||
}
|
||||
|
||||
# set base variables
|
||||
BASE_PATH="/storage/var/www/html/developers/clemens/core_data/php_libraries/trunk/";
|
||||
PHPUNIT_CONFIG="${BASE_PATH}phpunit.xml";
|
||||
PHP_BIN_PATH=$(which php);
|
||||
if [ -z "${PHP_BIN_PATH}" ]; then
|
||||
echo "Cannot find php binary";
|
||||
exit;
|
||||
fi;
|
||||
DEFAULT_PHP_VERSION=$(${PHP_BIN_PATH} -r "echo PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION;");
|
||||
if [ -z "${DEFAULT_PHP_VERSION}" ]; then
|
||||
echo "Cannot set default PHP version";
|
||||
exit;
|
||||
fi;
|
||||
base="/storage/var/www/html/developers/clemens/core_data/php_libraries/trunk/";
|
||||
# -c phpunit.xml
|
||||
# --testdox
|
||||
# call with "-tt" to give verbose testdox output
|
||||
# call with "t" to give verbose testdox output
|
||||
# SUPPORTED: https://www.php.net/supported-versions.php
|
||||
# call with -p <php version number> to force a certain php version
|
||||
# call with 7.4, 8.0, 8.1 to force a certain php version
|
||||
|
||||
opt_testdox="";
|
||||
opt_verbose="";
|
||||
php_version="";
|
||||
no_php_version=0;
|
||||
while [ -n "${1-}" ]; do
|
||||
if [ "${1}" = "t" ] || [ "${2}" = "t" ]; then
|
||||
opt_testdox="--testdox";
|
||||
fi;
|
||||
php_bin="";
|
||||
if [ ! -z "${1}" ]; then
|
||||
case "${1}" in
|
||||
-t | --testdox)
|
||||
opt_testdox="--testdox";
|
||||
;;
|
||||
-v | --verbose)
|
||||
opt_verbose="--verbose";
|
||||
;;
|
||||
-p | --php)
|
||||
php_version="${2-}";
|
||||
shift
|
||||
;;
|
||||
-h | --help)
|
||||
usage
|
||||
;;
|
||||
# invalid option
|
||||
-?*)
|
||||
error "[!] Unknown option: '$1'."
|
||||
;;
|
||||
esac
|
||||
shift;
|
||||
done;
|
||||
|
||||
if [ -z "${php_version}" ]; then
|
||||
php_version="${DEFAULT_PHP_VERSION}";
|
||||
no_php_version=1;
|
||||
# "7.3") php_bin="/usr/bin/php7.3 "; ;;
|
||||
"7.4") php_bin="/usr/bin/php7.4 "; ;;
|
||||
"8.0") php_bin="/usr/bin/php8.0 "; ;;
|
||||
"8.1") php_bin="/usr/bin/php8.1 "; ;;
|
||||
"8.2") php_bin="/usr/bin/php8.2 "; ;;
|
||||
*) echo "Not support PHP: ${1}"; exit; ;;
|
||||
esac;
|
||||
fi;
|
||||
php_bin="${PHP_BIN_PATH}${php_version}";
|
||||
echo "Use PHP Version: ${php_version}";
|
||||
|
||||
if [ ! -f "${php_bin}" ]; then
|
||||
echo "Set php ${php_bin} does not exist";
|
||||
exit;
|
||||
if [ ! -z "${2}" ] && [ -z "${php_bin}" ]; then
|
||||
case "${2}" in
|
||||
# "7.3") php_bin="/usr/bin/php7.3 "; ;;
|
||||
"7.4") php_bin="/usr/bin/php7.4 "; ;;
|
||||
"8.0") php_bin="/usr/bin/php8.0 "; ;;
|
||||
"8.1") php_bin="/usr/bin/php8.1 "; ;;
|
||||
"8.2") php_bin="/usr/bin/php8.2 "; ;;
|
||||
*) echo "Not support PHP: ${1}"; exit; ;;
|
||||
esac;
|
||||
fi;
|
||||
php_bin="${php_bin} ";
|
||||
|
||||
# Note 4dev/tests/bootstrap.php has to be set as bootstrap file in phpunit.xml
|
||||
phpunit_call="${php_bin}${BASE_PATH}vendor/bin/phpunit ${opt_testdox} ${opt_verbose} -c ${PHPUNIT_CONFIG} ${BASE_PATH}4dev/tests/";
|
||||
phpunit_call="${php_bin}${base}www/vendor/bin/phpunit ${opt_testdox} -c ${base}phpunit.xml ${base}4dev/tests/";
|
||||
|
||||
${phpunit_call};
|
||||
|
||||
echo -e "\nPHPUnit Config: ${PHPUNIT_CONFIG}";
|
||||
if [ "${no_php_version}" -eq 0 ]; then
|
||||
echo "CALLED WITH PHP: ${php_bin}$(${php_bin} --version)";
|
||||
if [ ! -z "${php_bin}" ]; then
|
||||
echo "CALLED WITH PHP: ${php_bin}"$(${php_bin} --version);
|
||||
else
|
||||
echo "Default PHP used: $(php --version)";
|
||||
echo "Default PHP used: "$(php --version);
|
||||
fi;
|
||||
|
||||
# __END__
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
#!/bin/env bash
|
||||
|
||||
# syncs
|
||||
# 4dev/tests/
|
||||
# www/lib/CoreLibs/
|
||||
#
|
||||
# to the composer corelibs all repo
|
||||
|
||||
GO="${1}";
|
||||
DRY_RUN="";
|
||||
if [ "${GO}" != "go" ]; then
|
||||
DRY_RUN="-n ";
|
||||
fi;
|
||||
|
||||
BASE="/storage/var/www/html/developers/clemens/core_data/";
|
||||
SOURCE="${BASE}php_libraries/master/"
|
||||
TARGET="${BASE}composer-packages/CoreLibs-Composer-All/"
|
||||
|
||||
rsync ${DRY_RUN}-Plzvrupt --stats --delete ${SOURCE}4dev/tests/ ${TARGET}test/phpunit/
|
||||
rsync ${DRY_RUN}-Plzvrupt --stats --delete ${SOURCE}www/lib/CoreLibs/ ${TARGET}src/
|
||||
|
||||
# __END__
|
||||
@@ -5,9 +5,9 @@ RETURNS TRIGGER AS
|
||||
$$
|
||||
BEGIN
|
||||
IF TG_OP = 'INSERT' THEN
|
||||
NEW.date_created := clock_timestamp();
|
||||
NEW.date_created := 'now';
|
||||
ELSIF TG_OP = 'UPDATE' THEN
|
||||
NEW.date_updated := clock_timestamp();
|
||||
NEW.date_updated := 'now';
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
|
||||
@@ -7,11 +7,10 @@ DECLARE
|
||||
random_length INT = 25; -- that should be long enough
|
||||
BEGIN
|
||||
IF TG_OP = 'INSERT' THEN
|
||||
NEW.date_created := clock_timestamp();
|
||||
NEW.date_created := 'now';
|
||||
NEW.cuid := random_string(random_length);
|
||||
NEW.cuuid := gen_random_uuid();
|
||||
ELSIF TG_OP = 'UPDATE' THEN
|
||||
NEW.date_updated := clock_timestamp();
|
||||
NEW.date_updated := 'now';
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
|
||||
@@ -8,12 +8,12 @@ DECLARE
|
||||
random_length INT = 32; -- long for massive data
|
||||
BEGIN
|
||||
IF TG_OP = 'INSERT' THEN
|
||||
NEW.date_created := clock_timestamp();
|
||||
NEW.date_created := 'now';
|
||||
IF NEW.uid IS NULL THEN
|
||||
NEW.uid := random_string(random_length);
|
||||
END IF;
|
||||
ELSIF TG_OP = 'UPDATE' THEN
|
||||
NEW.date_updated := clock_timestamp();
|
||||
NEW.date_updated := 'now';
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
|
||||
19
4dev/database/function/update_function.sql
Normal file
19
4dev/database/function/update_function.sql
Normal file
@@ -0,0 +1,19 @@
|
||||
-- adds the created or updated date tags
|
||||
|
||||
-- OLD, DEPRECATED, use set_generic.sql
|
||||
|
||||
-- CREATE OR REPLACE FUNCTION set_generic()
|
||||
-- RETURNS TRIGGER AS
|
||||
-- $$
|
||||
-- BEGIN
|
||||
-- IF TG_OP = 'INSERT' THEN
|
||||
-- NEW.date_created := clock_timestamp();
|
||||
-- NEW.user_created := current_user;
|
||||
-- ELSIF TG_OP = 'UPDATE' THEN
|
||||
-- NEW.date_updated := clock_timestamp();
|
||||
-- NEW.user_updated := current_user;
|
||||
-- END IF;
|
||||
-- RETURN NEW;
|
||||
-- END;
|
||||
-- $$
|
||||
-- LANGUAGE 'plpgsql';
|
||||
@@ -1,110 +0,0 @@
|
||||
-- Upgrade serial to identity type
|
||||
--
|
||||
-- Original: https://www.enterprisedb.com/blog/postgresql-10-identity-columns-explained#section-6
|
||||
--
|
||||
-- @param reclass tbl The table where the column is located, prefix with 'schema.' if different schema
|
||||
-- @param name col The column to be changed
|
||||
-- @param varchar identity_type [default=a] Allowed a, d, assigned, default
|
||||
-- @param varchar col_type [default=''] Allowed smallint, int, bigint, int2, int4, int8
|
||||
-- @returns varchar status tring
|
||||
-- @raises EXCEPTON on column not found, no linked sequence, more than one linked sequence found, invalid col type
|
||||
--
|
||||
CREATE OR REPLACE FUNCTION upgrade_serial_to_identity(
|
||||
tbl regclass,
|
||||
col name,
|
||||
identity_type varchar = 'a',
|
||||
col_type varchar = ''
|
||||
)
|
||||
RETURNS varchar
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
DECLARE
|
||||
colnum SMALLINT;
|
||||
seqid OID;
|
||||
count INT;
|
||||
col_type_oid INT;
|
||||
col_type_len INT;
|
||||
current_col_atttypid OID;
|
||||
current_col_attlen INT;
|
||||
status_string VARCHAR;
|
||||
BEGIN
|
||||
-- switch between always (default) or default identiy type
|
||||
IF identity_type NOT IN ('a', 'd', 'assigned', 'default') THEN
|
||||
identity_type := 'a';
|
||||
ELSE
|
||||
IF identity_type = 'default' THEN
|
||||
identity_type := 'd';
|
||||
ELSIF identity_type = 'assigned' THEN
|
||||
identity_type := 'a';
|
||||
END IF;
|
||||
END IF;
|
||||
-- find column number, attribute oid and attribute len
|
||||
SELECT attnum, atttypid, attlen
|
||||
INTO colnum, current_col_atttypid, current_col_attlen
|
||||
FROM pg_attribute
|
||||
WHERE attrelid = tbl AND attname = col;
|
||||
IF NOT FOUND THEN
|
||||
RAISE EXCEPTION 'column does not exist';
|
||||
END IF;
|
||||
|
||||
-- find sequence
|
||||
SELECT INTO seqid objid
|
||||
FROM pg_depend
|
||||
WHERE (refclassid, refobjid, refobjsubid) = ('pg_class'::regclass, tbl, colnum)
|
||||
AND classid = 'pg_class'::regclass AND objsubid = 0
|
||||
AND deptype = 'a';
|
||||
|
||||
GET DIAGNOSTICS count = ROW_COUNT;
|
||||
IF count < 1 THEN
|
||||
RAISE EXCEPTION 'no linked sequence found';
|
||||
ELSIF count > 1 THEN
|
||||
RAISE EXCEPTION 'more than one linked sequence found';
|
||||
END IF;
|
||||
|
||||
IF col_type <> '' AND col_type NOT IN ('smallint', 'int', 'bigint', 'int2', 'int4', 'int8') THEN
|
||||
RAISE EXCEPTION 'Invalid col type: %', col_type;
|
||||
END IF;
|
||||
|
||||
-- drop the default
|
||||
EXECUTE 'ALTER TABLE ' || tbl || ' ALTER COLUMN ' || quote_ident(col) || ' DROP DEFAULT';
|
||||
|
||||
-- change the dependency between column and sequence to internal
|
||||
UPDATE pg_depend
|
||||
SET deptype = 'i'
|
||||
WHERE (classid, objid, objsubid) = ('pg_class'::regclass, seqid, 0)
|
||||
AND deptype = 'a';
|
||||
|
||||
-- mark the column as identity column
|
||||
UPDATE pg_attribute
|
||||
-- set to 'd' for default
|
||||
SET attidentity = identity_type
|
||||
WHERE attrelid = tbl
|
||||
AND attname = col;
|
||||
status_string := 'Updated to identity for table "' || tbl || '" and columen "' || col || '" with type "' || identity_type || '"';
|
||||
|
||||
-- set type if requested and not empty
|
||||
IF col_type <> '' THEN
|
||||
-- rewrite smallint, int, bigint
|
||||
IF col_type = 'smallint' THEN
|
||||
col_type := 'int2';
|
||||
ELSIF col_type = 'int' THEN
|
||||
col_type := 'int4';
|
||||
ELSIF col_type = 'bigint' THEN
|
||||
col_type := 'int8';
|
||||
END IF;
|
||||
-- get the length and oid for selected
|
||||
SELECT oid, typlen INTO col_type_oid, col_type_len FROM pg_type WHERE typname = col_type;
|
||||
-- set only if diff or hight
|
||||
IF current_col_atttypid <> col_type_oid AND col_type_len > current_col_attlen THEN
|
||||
status_string := status_string || '. Change col type: ' || col_type;
|
||||
-- update type
|
||||
UPDATE pg_attribute
|
||||
SET
|
||||
atttypid = col_type_oid, attlen = col_type_len
|
||||
WHERE attrelid = tbl
|
||||
AND attname = col;
|
||||
END IF;
|
||||
END IF;
|
||||
RETURN status_string;
|
||||
END;
|
||||
$$;
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_access;
|
||||
CREATE TABLE edit_access (
|
||||
edit_access_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_access_id SERIAL PRIMARY KEY,
|
||||
enabled SMALLINT NOT NULL DEFAULT 0,
|
||||
protected SMALLINT DEFAULT 0,
|
||||
deleted SMALLINT DEFAULT 0,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_access_data;
|
||||
CREATE TABLE edit_access_data (
|
||||
edit_access_data_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_access_data_id SERIAL PRIMARY KEY,
|
||||
edit_access_id INT NOT NULL,
|
||||
FOREIGN KEY (edit_access_id) REFERENCES edit_access (edit_access_id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
enabled SMALLINT NOT NULL DEFAULT 0,
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
-- DROP TABLE edit_access_right;
|
||||
CREATE TABLE edit_access_right (
|
||||
edit_access_right_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_access_right_id SERIAL PRIMARY KEY,
|
||||
name VARCHAR,
|
||||
level SMALLINT,
|
||||
type VARCHAR,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_access_user;
|
||||
CREATE TABLE edit_access_user (
|
||||
edit_access_user_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_access_user_id SERIAL PRIMARY KEY,
|
||||
edit_access_id INT NOT NULL,
|
||||
FOREIGN KEY (edit_access_id) REFERENCES edit_access (edit_access_id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
edit_user_id INT NOT NULL,
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
-- DROP TABLE edit_generic;
|
||||
CREATE TABLE edit_generic (
|
||||
cuid VARCHAR,
|
||||
cuuid UUID DEFAULT gen_random_uuid(),
|
||||
date_created TIMESTAMP WITHOUT TIME ZONE DEFAULT clock_timestamp(),
|
||||
date_updated TIMESTAMP WITHOUT TIME ZONE
|
||||
);
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_group;
|
||||
CREATE TABLE edit_group (
|
||||
edit_group_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_group_id SERIAL PRIMARY KEY,
|
||||
edit_scheme_id INT,
|
||||
FOREIGN KEY (edit_scheme_id) REFERENCES edit_scheme (edit_scheme_id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
edit_access_right_id INT NOT NULL,
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
-- DROP TABLE edit_language;
|
||||
CREATE TABLE edit_language (
|
||||
edit_language_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_language_id SERIAL PRIMARY KEY,
|
||||
enabled SMALLINT NOT NULL DEFAULT 0,
|
||||
lang_default SMALLINT NOT NULL DEFAULT 0,
|
||||
long_name VARCHAR,
|
||||
|
||||
@@ -7,54 +7,35 @@
|
||||
|
||||
-- DROP TABLE edit_log;
|
||||
CREATE TABLE edit_log (
|
||||
edit_log_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_log_id SERIAL PRIMARY KEY,
|
||||
euid INT, -- this is a foreign key, but I don't nedd to reference to it
|
||||
FOREIGN KEY (euid) REFERENCES edit_user (edit_user_id) MATCH FULL ON UPDATE CASCADE ON DELETE SET NULL,
|
||||
eucuid VARCHAR,
|
||||
eucuuid UUID, -- this is the one we want to use, full UUIDv4 from the edit user table
|
||||
-- date_created equal, but can be overridden
|
||||
event_date TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||
-- session ID if set
|
||||
session_id VARCHAR,
|
||||
-- username
|
||||
username VARCHAR,
|
||||
-- DEPRECATED [password]
|
||||
password VARCHAR,
|
||||
ip_address JSONB, -- REMOTE_IP and all other IPs (X_FORWARD, etc) as JSON block
|
||||
-- DEPRECATED [ip]
|
||||
ip VARCHAR, -- just the REMOTE_IP, full set see ip_address
|
||||
-- string blocks, general
|
||||
event_date TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||
ip VARCHAR,
|
||||
error TEXT,
|
||||
event TEXT,
|
||||
-- bytea or string type storage of any data
|
||||
data_binary BYTEA,
|
||||
data TEXT,
|
||||
-- set page name only
|
||||
page VARCHAR,
|
||||
-- various info data sets
|
||||
action VARCHAR,
|
||||
action_id VARCHAR,
|
||||
action_yes VARCHAR,
|
||||
action_flag VARCHAR,
|
||||
action_menu VARCHAR,
|
||||
action_loaded VARCHAR,
|
||||
action_value VARCHAR,
|
||||
action_type VARCHAR,
|
||||
action_error VARCHAR,
|
||||
user_agent VARCHAR,
|
||||
referer VARCHAR,
|
||||
script_name VARCHAR,
|
||||
query_string VARCHAR,
|
||||
request_scheme VARCHAR, -- http or https
|
||||
server_name VARCHAR,
|
||||
http_host VARCHAR,
|
||||
http_data JSONB,
|
||||
-- DEPRECATED [http*]
|
||||
http_accept VARCHAR, -- in http_data
|
||||
http_accept_charset VARCHAR, -- in http_data
|
||||
http_accept_encoding VARCHAR, -- in http_data
|
||||
-- any action var, -> same set in action_data as JSON
|
||||
action_data JSONB,
|
||||
-- DEPRECATED [action*]
|
||||
action VARCHAR, -- in action_data
|
||||
action_id VARCHAR, -- in action_data
|
||||
action_sub_id VARCHAR, -- in action_data
|
||||
action_yes VARCHAR, -- in action_data
|
||||
action_flag VARCHAR, -- in action_data
|
||||
action_menu VARCHAR, -- in action_data
|
||||
action_loaded VARCHAR, -- in action_data
|
||||
action_value VARCHAR, -- in action_data
|
||||
action_type VARCHAR, -- in action_data
|
||||
action_error VARCHAR -- in action_data
|
||||
http_accept VARCHAR,
|
||||
http_accept_charset VARCHAR,
|
||||
http_accept_encoding VARCHAR,
|
||||
session_id VARCHAR
|
||||
) INHERITS (edit_generic) WITHOUT OIDS;
|
||||
|
||||
@@ -7,8 +7,10 @@
|
||||
|
||||
-- DROP TABLE edit_menu_group;
|
||||
CREATE TABLE edit_menu_group (
|
||||
edit_menu_group_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_menu_group_id SERIAL PRIMARY KEY,
|
||||
name VARCHAR,
|
||||
flag VARCHAR,
|
||||
order_number INT NOT NULL
|
||||
) INHERITS (edit_generic) WITHOUT OIDS;
|
||||
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_page;
|
||||
CREATE TABLE edit_page (
|
||||
edit_page_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_page_id SERIAL PRIMARY KEY,
|
||||
content_alias_edit_page_id INT, -- alias for page content, if the page content is defined on a different page, ege for ajax backend pages
|
||||
FOREIGN KEY (content_alias_edit_page_id) REFERENCES edit_page (edit_page_id) MATCH FULL ON DELETE RESTRICT ON UPDATE CASCADE,
|
||||
filename VARCHAR,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_page_access;
|
||||
CREATE TABLE edit_page_access (
|
||||
edit_page_access_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_page_access_id SERIAL PRIMARY KEY,
|
||||
edit_group_id INT NOT NULL,
|
||||
FOREIGN KEY (edit_group_id) REFERENCES edit_group (edit_group_id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
edit_page_id INT NOT NULL,
|
||||
@@ -16,3 +16,5 @@ CREATE TABLE edit_page_access (
|
||||
FOREIGN KEY (edit_access_right_id) REFERENCES edit_access_right (edit_access_right_id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
enabled SMALLINT NOT NULL DEFAULT 0
|
||||
) INHERITS (edit_generic) WITHOUT OIDS;
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
-- DROP TABLE edit_page_content;
|
||||
CREATE TABLE edit_page_content (
|
||||
edit_page_content_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_page_content_id SERIAL PRIMARY KEY,
|
||||
edit_page_id INT NOT NULL,
|
||||
FOREIGN KEY (edit_page_id) REFERENCES edit_page (edit_page_id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
edit_access_right_id INT NOT NULL,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_query_string;
|
||||
CREATE TABLE edit_query_string (
|
||||
edit_query_string_id SERIAINT GENERATED ALWAYS AS IDENTITYL PRIMARY KEY,
|
||||
edit_query_string_id SERIAL PRIMARY KEY,
|
||||
edit_page_id INT NOT NULL,
|
||||
FOREIGN KEY (edit_page_id) REFERENCES edit_page (edit_page_id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
enabled SMALLINT NOT NULL DEFAULT 0,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_scheme;
|
||||
CREATE TABLE edit_scheme (
|
||||
edit_scheme_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_scheme_id SERIAL PRIMARY KEY,
|
||||
enabled SMALLINT NOT NULL DEFAULT 0,
|
||||
name VARCHAR,
|
||||
header_color VARCHAR,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_user;
|
||||
CREATE TABLE edit_user (
|
||||
edit_user_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_user_id SERIAL PRIMARY KEY,
|
||||
connect_edit_user_id INT, -- possible reference to other user
|
||||
FOREIGN KEY (connect_edit_user_id) REFERENCES edit_user (edit_user_id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
edit_language_id INT NOT NULL,
|
||||
@@ -35,10 +35,11 @@ CREATE TABLE edit_user (
|
||||
strict SMALLINT DEFAULT 0,
|
||||
locked SMALLINT DEFAULT 0,
|
||||
protected SMALLINT NOT NULL DEFAULT 0,
|
||||
-- legacy, debug flags
|
||||
debug SMALLINT NOT NULL DEFAULT 0,
|
||||
db_debug SMALLINT NOT NULL DEFAULT 0,
|
||||
-- is admin user
|
||||
admin SMALLINT NOT NULL DEFAULT 0,
|
||||
-- force lgout counter
|
||||
force_logout INT DEFAULT 0,
|
||||
-- last login log
|
||||
last_login TIMESTAMP WITHOUT TIME ZONE,
|
||||
-- login error
|
||||
@@ -75,8 +76,9 @@ COMMENT ON COLUMN edit_user.deleted IS 'Login is deleted (master switch), overri
|
||||
COMMENT ON COLUMN edit_user.strict IS 'If too many failed logins user will be locked, default off';
|
||||
COMMENT ON COLUMN edit_user.locked IS 'Locked from too many wrong password logins';
|
||||
COMMENT ON COLUMN edit_user.protected IS 'User can only be chnaged by admin user';
|
||||
COMMENT ON COLUMN edit_user.debug IS 'Turn debug flag on (legacy)';
|
||||
COMMENT ON COLUMN edit_user.db_debug IS 'Turn DB debug flag on (legacy)';
|
||||
COMMENT ON COLUMN edit_user.admin IS 'If set, this user is SUPER admin';
|
||||
COMMENT ON COLUMN edit_user.force_logout IS 'Counter for forced log out, if this one is higher than the session set one the session gets terminated';
|
||||
COMMENT ON COLUMN edit_user.last_login IS 'Last succesfull login tiemstamp';
|
||||
COMMENT ON COLUMN edit_user.login_error_count IS 'Number of failed logins, reset on successful login';
|
||||
COMMENT ON COLUMN edit_user.login_error_date_last IS 'Last login error date';
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_visible_group;
|
||||
CREATE TABLE edit_visible_group (
|
||||
edit_visible_group_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_visible_group_id SERIAL PRIMARY KEY,
|
||||
name VARCHAR,
|
||||
flag VARCHAR
|
||||
) INHERITS (edit_generic) WITHOUT OIDS;
|
||||
|
||||
2213
4dev/deprecated/CoreLibs/ACL/Login.php
Normal file
2213
4dev/deprecated/CoreLibs/ACL/Login.php
Normal file
File diff suppressed because it is too large
Load Diff
622
4dev/deprecated/CoreLibs/Admin/Backend.php
Normal file
622
4dev/deprecated/CoreLibs/Admin/Backend.php
Normal file
@@ -0,0 +1,622 @@
|
||||
<?php
|
||||
|
||||
/*********************************************************************
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2006/08/15
|
||||
* VERSION: 1.0.0
|
||||
* RELEASED LICENSE: GNU GPL 3
|
||||
* DESCRIPTION
|
||||
* Basic Admin interface backend
|
||||
* - sets action flags
|
||||
* - menu creation
|
||||
* - array vars for smarty
|
||||
*
|
||||
* CHANGE PLAN:
|
||||
* loads DB\IO + Logger and returns one group object
|
||||
* also checks all missing CONFIG vars from Basic class
|
||||
*
|
||||
* PUBLIC VARIABLES
|
||||
*
|
||||
* PRIVATE VARIABLES
|
||||
*
|
||||
* PUBLIC METHODS
|
||||
*
|
||||
* PRIVATE METHODS
|
||||
*
|
||||
* HISTORY:
|
||||
*
|
||||
*********************************************************************/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Admin;
|
||||
|
||||
class Backend
|
||||
{
|
||||
// page name
|
||||
/** @var array<mixed> */
|
||||
public $menu = [];
|
||||
/** @var int|string */
|
||||
public $menu_show_flag = 0; // top menu flag (mostly string)
|
||||
// action ids
|
||||
/** @var array<string> */
|
||||
public $action_list = [
|
||||
'action', 'action_id', 'action_sub_id', 'action_yes', 'action_flag',
|
||||
'action_menu', 'action_value', 'action_error', 'action_loaded'
|
||||
];
|
||||
/** @var string */
|
||||
public $action;
|
||||
/** @var string|int */
|
||||
public $action_id;
|
||||
/** @var string|int */
|
||||
public $action_sub_id;
|
||||
/** @var string|int|bool */
|
||||
public $action_yes;
|
||||
/** @var string */
|
||||
public $action_flag;
|
||||
/** @var string */
|
||||
public $action_menu;
|
||||
/** @var string */
|
||||
public $action_loaded;
|
||||
/** @var string */
|
||||
public $action_value;
|
||||
/** @var string */
|
||||
public $action_error;
|
||||
// ACL array variable if we want to set acl data from outisde
|
||||
/** @var array<mixed> */
|
||||
public $acl = [];
|
||||
/** @var int */
|
||||
public $default_acl;
|
||||
// queue key
|
||||
/** @var string */
|
||||
public $queue_key;
|
||||
// the current active edit access id
|
||||
/** @var int */
|
||||
public $edit_access_id;
|
||||
/** @var string */
|
||||
public $page_name;
|
||||
// error/warning/info messages
|
||||
/** @var array<mixed> */
|
||||
public $messages = [];
|
||||
/** @var bool */
|
||||
public $error = false;
|
||||
/** @var bool */
|
||||
public $warning = false;
|
||||
/** @var bool */
|
||||
public $info = false;
|
||||
// internal lang & encoding vars
|
||||
/** @var string */
|
||||
public $lang_dir = '';
|
||||
/** @var string */
|
||||
public $lang;
|
||||
/** @var string */
|
||||
public $lang_short;
|
||||
/** @var string */
|
||||
public $domain;
|
||||
/** @var string */
|
||||
public $encoding;
|
||||
/** @var \CoreLibs\Debug\Logging logger */
|
||||
public $log;
|
||||
/** @var \CoreLibs\DB\IO database */
|
||||
public $db;
|
||||
/** @var \CoreLibs\Language\L10n language */
|
||||
public $l;
|
||||
/** @var \CoreLibs\Create\Session session class */
|
||||
public $session;
|
||||
// smarty publics [end processing in smarty class]
|
||||
/** @var array<mixed> */
|
||||
public $DATA;
|
||||
/** @var array<mixed> */
|
||||
public $HEADER;
|
||||
/** @var array<mixed> */
|
||||
public $DEBUG_DATA;
|
||||
/** @var array<mixed> */
|
||||
public $CONTENT_DATA;
|
||||
|
||||
// CONSTRUCTOR / DECONSTRUCTOR |====================================>
|
||||
/**
|
||||
* main class constructor
|
||||
*
|
||||
* @param \CoreLibs\DB\IO $db Database connection class
|
||||
* @param \CoreLibs\Debug\Logging $log Logging class
|
||||
* @param \CoreLibs\Create\Session $session Session interface class
|
||||
* @param \CoreLibs\Language\L10n $l10n l10n language class
|
||||
* @param array<string,string> $locale locale data read from setLocale
|
||||
*/
|
||||
public function __construct(
|
||||
\CoreLibs\DB\IO $db,
|
||||
\CoreLibs\Debug\Logging $log,
|
||||
\CoreLibs\Create\Session $session,
|
||||
\CoreLibs\Language\L10n $l10n,
|
||||
array $locale
|
||||
) {
|
||||
// attach db class
|
||||
$this->db = $db;
|
||||
// set to log not per class
|
||||
$log->setLogPer('class', false);
|
||||
// attach logger
|
||||
$this->log = $log;
|
||||
// attach session class
|
||||
$this->session = $session;
|
||||
// get the language sub class & init it
|
||||
$this->l = $l10n;
|
||||
// parse and read, legacy stuff
|
||||
$this->encoding = $locale['encoding'];
|
||||
$this->lang = $locale['lang'];
|
||||
// get first part from lang
|
||||
$this->lang_short = explode('_', $locale['lang'])[0];
|
||||
$this->domain = $this->l->getDomain();
|
||||
$this->lang_dir = $this->l->getBaseLocalePath();
|
||||
|
||||
// set the page name
|
||||
$this->page_name = \CoreLibs\Get\System::getPageName();
|
||||
|
||||
// set the action ids
|
||||
foreach ($this->action_list as $_action) {
|
||||
$this->$_action = $_POST[$_action] ?? '';
|
||||
}
|
||||
|
||||
$this->default_acl = DEFAULT_ACL_LEVEL;
|
||||
|
||||
// queue key
|
||||
if (preg_match("/^(add|save|delete|remove|move|up|down|push_live)$/", $this->action)) {
|
||||
$this->queue_key = \CoreLibs\Create\RandomKey::randomKeyGen(3);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* class deconstructor
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
// NO OP
|
||||
}
|
||||
|
||||
// PUBLIC METHODS |=================================================>
|
||||
|
||||
/**
|
||||
* set internal ACL from login ACL
|
||||
*
|
||||
* @param array<mixed> $acl login acl array
|
||||
*/
|
||||
public function setACL(array $acl): void
|
||||
{
|
||||
$this->acl = $acl;
|
||||
}
|
||||
|
||||
/**
|
||||
* writes all action vars plus other info into edit_log table
|
||||
*
|
||||
* @param string $event any kind of event description,
|
||||
* @param string|array<mixed> $data any kind of data related to that event
|
||||
* @param string $write_type write type can bei STRING or BINARY
|
||||
* @return void
|
||||
*/
|
||||
public function adbEditLog(
|
||||
string $event = '',
|
||||
$data = '',
|
||||
string $write_type = 'STRING'
|
||||
): void {
|
||||
$data_binary = '';
|
||||
if ($write_type == 'BINARY') {
|
||||
$data_binary = $this->db->dbEscapeBytea((string)bzcompress(serialize($data)));
|
||||
$data = 'see bzip compressed data_binary field';
|
||||
}
|
||||
if ($write_type == 'STRING') {
|
||||
$data_binary = '';
|
||||
$data = $this->db->dbEscapeString(serialize($data));
|
||||
}
|
||||
|
||||
// check schema
|
||||
$SCHEMA = 'public';
|
||||
/** @phpstan-ignore-next-line */
|
||||
if (defined('LOGIN_DB_SCHEMA') && !empty(LOGIN_DB_SCHEMA)) {
|
||||
$SCHEMA = LOGIN_DB_SCHEMA;
|
||||
} elseif ($this->db->dbGetSchema()) {
|
||||
$SCHEMA = $this->db->dbGetSchema();
|
||||
} elseif (defined('PUBLIC_SCHEMA')) {
|
||||
$SCHEMA = PUBLIC_SCHEMA;
|
||||
}
|
||||
/** @phpstan-ignore-next-line for whatever reason $SCHEMA is seen as possible array */
|
||||
$q = "INSERT INTO " . $SCHEMA . ".edit_log "
|
||||
. "(euid, event_date, event, data, data_binary, page, "
|
||||
. "ip, user_agent, referer, script_name, query_string, server_name, http_host, "
|
||||
. "http_accept, http_accept_charset, http_accept_encoding, session_id, "
|
||||
. "action, action_id, action_yes, action_flag, action_menu, action_loaded, action_value, action_error) "
|
||||
. "VALUES "
|
||||
. "(" . $this->db->dbEscapeString(isset($_SESSION['EUID']) && is_numeric($_SESSION['EUID']) ?
|
||||
$_SESSION['EUID'] :
|
||||
'NULL')
|
||||
. ", "
|
||||
. "NOW(), "
|
||||
. "'" . $this->db->dbEscapeString((string)$event) . "', '" . $data . "', "
|
||||
. "'" . $data_binary . "', '" . $this->db->dbEscapeString((string)$this->page_name) . "', "
|
||||
. "'" . @$_SERVER["REMOTE_ADDR"] . "', "
|
||||
. "'" . $this->db->dbEscapeString(@$_SERVER['HTTP_USER_AGENT']) . "', "
|
||||
. "'" . $this->db->dbEscapeString($_SERVER['HTTP_REFERER'] ?? '') . "', "
|
||||
. "'" . $this->db->dbEscapeString($_SERVER['SCRIPT_FILENAME'] ?? '') . "', "
|
||||
. "'" . $this->db->dbEscapeString($_SERVER['QUERY_STRING'] ?? '') . "', "
|
||||
. "'" . $this->db->dbEscapeString($_SERVER['SERVER_NAME'] ?? '') . "', "
|
||||
. "'" . $this->db->dbEscapeString($_SERVER['HTTP_HOST'] ?? '') . "', "
|
||||
. "'" . $this->db->dbEscapeString($_SERVER['HTTP_ACCEPT'] ?? '') . "', "
|
||||
. "'" . $this->db->dbEscapeString($_SERVER['HTTP_ACCEPT_CHARSET'] ?? '') . "', "
|
||||
. "'" . $this->db->dbEscapeString($_SERVER['HTTP_ACCEPT_ENCODING'] ?? '') . "', "
|
||||
. ($this->session->getSessionId() === false ?
|
||||
"NULL" :
|
||||
"'" . $this->session->getSessionId() . "'")
|
||||
. ", "
|
||||
. "'" . $this->db->dbEscapeString($this->action) . "', "
|
||||
. "'" . $this->db->dbEscapeString($this->action_id) . "', "
|
||||
. "'" . $this->db->dbEscapeString($this->action_yes) . "', "
|
||||
. "'" . $this->db->dbEscapeString($this->action_flag) . "', "
|
||||
. "'" . $this->db->dbEscapeString($this->action_menu) . "', "
|
||||
. "'" . $this->db->dbEscapeString($this->action_loaded) . "', "
|
||||
. "'" . $this->db->dbEscapeString($this->action_value) . "', "
|
||||
. "'" . $this->db->dbEscapeString($this->action_error) . "')";
|
||||
$this->db->dbExec($q, 'NULL');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the menu show flag
|
||||
*
|
||||
* @param string|int $menu_show_flag
|
||||
* @return string|int
|
||||
*/
|
||||
public function adbSetMenuShowFlag($menu_show_flag)
|
||||
{
|
||||
// must be string or int
|
||||
$this->menu_show_flag = $menu_show_flag;
|
||||
return $this->menu_show_flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the menu show flag
|
||||
*
|
||||
* @return string|int
|
||||
*/
|
||||
public function adbGetMenuShowFlag()
|
||||
{
|
||||
return $this->menu_show_flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* menu creater (from login menu session pages)
|
||||
*
|
||||
* @param int $flag visible flag trigger
|
||||
* @return array<mixed> menu array for output on page (smarty)
|
||||
*/
|
||||
public function adbTopMenu(int $flag = 0): array
|
||||
{
|
||||
if ($this->menu_show_flag) {
|
||||
$flag = $this->menu_show_flag;
|
||||
}
|
||||
|
||||
// get the session pages array
|
||||
$PAGES = $_SESSION['PAGES'] ?? null;
|
||||
if (!isset($PAGES) || !is_array($PAGES)) {
|
||||
$PAGES = [];
|
||||
}
|
||||
$pages = [];
|
||||
foreach ($PAGES as $PAGE_DATA) {
|
||||
$pages[] = $PAGE_DATA;
|
||||
}
|
||||
// $this->debug('pages', $this->print_ar($pages));
|
||||
// if flag is 0, then we show all, else, we show only the matching flagges array points
|
||||
// array is already sorted after correct order
|
||||
reset($pages);
|
||||
foreach ($pages as $data) {
|
||||
// for ($i = 0, $iMax = count($pages); $i < $iMax; $i ++) {
|
||||
$show = 0;
|
||||
// is it visible in the menu & is it online
|
||||
if ($data['menu'] && $data['online']) {
|
||||
// check if it falls into our flag if we have a flag
|
||||
if ($flag) {
|
||||
foreach ($data['visible'] as $name => $key) {
|
||||
if ($key == $flag) {
|
||||
$show = 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// if no flag given, show all menu points
|
||||
$show = 1;
|
||||
}
|
||||
|
||||
if ($show) {
|
||||
// if it is popup, write popup arrayound
|
||||
if (isset($data['popup']) && $data['popup']) {
|
||||
$type = 'popup';
|
||||
} else {
|
||||
$type = 'normal';
|
||||
$data['popup'] = 0;
|
||||
}
|
||||
$query_string = '';
|
||||
|
||||
if (
|
||||
isset($data['query']) &&
|
||||
is_array($data['query']) &&
|
||||
count($data['query'])
|
||||
) {
|
||||
// for ($j = 0, $jMax = count($pages[$i]['query']); $j < $jMax; $j ++) {
|
||||
foreach ($data['query'] as $j => $query) {
|
||||
if (
|
||||
!empty($query['name']) &&
|
||||
!empty($query['value'])
|
||||
) {
|
||||
if (strlen($query_string)) {
|
||||
$query_string .= '&';
|
||||
}
|
||||
$query_string .= $query['name'] . '=';
|
||||
if (
|
||||
isset($query['dynamic']) &&
|
||||
$query['dynamic']
|
||||
) {
|
||||
if (isset($_GET[$query['value']])) {
|
||||
$query_string .= urlencode($_GET[$query['value']]);
|
||||
} elseif (isset($_POST[$query['value']])) {
|
||||
$query_string .= urlencode($_POST[$query['value']]);
|
||||
}
|
||||
} else {
|
||||
$query_string .= urlencode($query['value']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$url = '';
|
||||
if (isset($data['hostname']) && $data['hostname']) {
|
||||
$url .= $data['hostname'];
|
||||
}
|
||||
$url .= $data['filename'] ?? '';
|
||||
if (strlen($query_string)) {
|
||||
$url .= '?' . $query_string;
|
||||
}
|
||||
$name = $data['page_name'] ?? '';
|
||||
// if page name matchs -> set selected flag
|
||||
$selected = 0;
|
||||
if (
|
||||
isset($data['filename']) &&
|
||||
\CoreLibs\Get\System::getPageName() == $data['filename'] &&
|
||||
(!isset($data['hostname']) || (
|
||||
isset($data['hostname']) &&
|
||||
(!$data['hostname'] || strstr($data['hostname'], CONTENT_PATH) !== false)
|
||||
))
|
||||
) {
|
||||
$selected = 1;
|
||||
$this->page_name = $name;
|
||||
}
|
||||
// last check, is this menu point okay to show
|
||||
$enabled = 0;
|
||||
if (
|
||||
isset($data['filename']) &&
|
||||
$this->adbShowMenuPoint($data['filename'])
|
||||
) {
|
||||
$enabled = 1;
|
||||
}
|
||||
// write in to view menu array
|
||||
array_push($this->menu, [
|
||||
'name' => $this->l->__($name),
|
||||
'url' => $url,
|
||||
'selected' => $selected,
|
||||
'enabled' => $enabled,
|
||||
'popup' => $type == 'popup' ? 1 : 0,
|
||||
'type' => $type
|
||||
]);
|
||||
} // show page
|
||||
} // online and in menu
|
||||
} // for each page
|
||||
return $this->menu;
|
||||
}
|
||||
|
||||
/**
|
||||
* ONLY USED IN adbTopMenu
|
||||
* checks if this filename is in the current situation (user id, etc) available
|
||||
*
|
||||
* @param string|null $filename filename
|
||||
* @return bool true for visible/accessable menu point, false for not
|
||||
*/
|
||||
private function adbShowMenuPoint(?string $filename): bool
|
||||
{
|
||||
$enabled = false;
|
||||
if ($filename === null) {
|
||||
return $enabled;
|
||||
}
|
||||
/** @phan-suppress-next-line PhanNoopSwitchCases */
|
||||
switch ($filename) {
|
||||
default:
|
||||
$enabled = true;
|
||||
break;
|
||||
};
|
||||
return $enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* creates out of a normal db_return array an assoc array
|
||||
*
|
||||
* @param array<mixed> $db_array input array
|
||||
* @param string|int|bool $key key
|
||||
* @param string|int|bool $value value
|
||||
* @return array<mixed> associative array
|
||||
* @deprecated \CoreLibs\Combined\ArrayHandler::genAssocArray()
|
||||
*/
|
||||
public function adbAssocArray(array $db_array, $key, $value): array
|
||||
{
|
||||
trigger_error(
|
||||
'Method ' . __METHOD__ . ' is deprecated: \CoreLibs\Combined\ArrayHandler::genAssocArray',
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
return \CoreLibs\Combined\ArrayHandler::genAssocArray($db_array, $key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* converts bytes into formated string with KB, MB, etc
|
||||
*
|
||||
* @param string|int|float $number string or int or number
|
||||
* @return string formatted string
|
||||
* @deprecated \CoreLibs\Convert\Byte::humanReadableByteFormat()
|
||||
*/
|
||||
public function adbByteStringFormat($number): string
|
||||
{
|
||||
trigger_error(
|
||||
'Method ' . __METHOD__ . ' is deprecated: \CoreLibs\Convert\Byte::humanReadableByteFormat()',
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
return \CoreLibs\Convert\Byte::humanReadableByteFormat($number);
|
||||
}
|
||||
|
||||
/**
|
||||
* converts picture to a thumbnail with max x and max y size
|
||||
*
|
||||
* @param string $pic source image file with or without path
|
||||
* @param int $size_x maximum size width
|
||||
* @param int $size_y maximum size height
|
||||
* @param string $dummy empty, or file_type to show an icon
|
||||
* instead of nothing if file is not found
|
||||
* @param string $path if source start is not ROOT path
|
||||
* if empty ROOT is choosen
|
||||
* @return string|bool thumbnail name, or false for error
|
||||
* @deprecated \CoreLibs\Output\Image::createThumbnail()
|
||||
*/
|
||||
public function adbCreateThumbnail(
|
||||
string $pic,
|
||||
int $size_x,
|
||||
int $size_y,
|
||||
string $dummy = '',
|
||||
string $path = '',
|
||||
string $cache = ''
|
||||
) {
|
||||
trigger_error(
|
||||
'Method ' . __METHOD__ . ' is deprecated: \CoreLibs\Output\Image::createThumbnail()',
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
return \CoreLibs\Output\Image::createThumbnail($pic, $size_x, $size_y, $dummy, $path, $cache);
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper function to fill up the mssages array
|
||||
*
|
||||
* @param string $level info/warning/error
|
||||
* @param string $msg string, can be printf formated
|
||||
* @param array<mixed> $vars optional data for a possible printf formated msg
|
||||
* @return void has no return
|
||||
*/
|
||||
public function adbMsg(string $level, string $msg, array $vars = []): void
|
||||
{
|
||||
if (!preg_match("/^info|warning|error$/", $level)) {
|
||||
$level = "info";
|
||||
}
|
||||
$this->messages[] = [
|
||||
'msg' => vsprintf($this->l->__($msg), $vars),
|
||||
'class' => $level
|
||||
];
|
||||
switch ($level) {
|
||||
case 'info':
|
||||
$this->info = true;
|
||||
break;
|
||||
case 'warning':
|
||||
$this->warning = true;
|
||||
break;
|
||||
case 'error':
|
||||
$this->error = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* writes live queue
|
||||
*
|
||||
* @param string $queue_key string to identfy the queue
|
||||
* @param string $type [description]
|
||||
* @param string $target [description]
|
||||
* @param string $data [description]
|
||||
* @param string $key_name [description]
|
||||
* @param string $key_value [description]
|
||||
* @param ?string $associate [description]
|
||||
* @param ?string $file [description]
|
||||
* @return void has no return
|
||||
*/
|
||||
public function adbLiveQueue(
|
||||
string $queue_key,
|
||||
string $type,
|
||||
string $target,
|
||||
string $data,
|
||||
string $key_name,
|
||||
string $key_value,
|
||||
string $associate = null,
|
||||
string $file = null
|
||||
): void {
|
||||
/** @phpstan-ignore-next-line */
|
||||
if (defined('GLOBAL_DB_SCHEMA') && !empty(GLOBAL_DB_SCHEMA)) {
|
||||
$SCHEMA = GLOBAL_DB_SCHEMA;
|
||||
} elseif ($this->db->dbGetSchema()) {
|
||||
$SCHEMA = $this->db->dbGetSchema();
|
||||
} elseif (defined('PUBLIC_SCHEMA')) {
|
||||
$SCHEMA = PUBLIC_SCHEMA;
|
||||
} else {
|
||||
$SCHEMA = 'public';
|
||||
}
|
||||
$q = "INSERT INTO " . $SCHEMA . ".live_queue ("
|
||||
. "queue_key, key_value, key_name, type, target, data, group_key, action, associate, file"
|
||||
. ") VALUES ("
|
||||
. "'" . $this->db->dbEscapeString($queue_key) . "', '" . $this->db->dbEscapeString($key_value) . "', "
|
||||
. "'" . $this->db->dbEscapeString($key_name) . "', '" . $this->db->dbEscapeString($type) . "', "
|
||||
. "'" . $this->db->dbEscapeString($target) . "', '" . $this->db->dbEscapeString($data) . "', "
|
||||
. "'" . $this->queue_key . "', '" . $this->action . "', "
|
||||
. "'" . $this->db->dbEscapeString((string)$associate) . "', "
|
||||
. "'" . $this->db->dbEscapeString((string)$file) . "')";
|
||||
$this->db->dbExec($q);
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic class holds exact the same, except the Year/Month/Day/etc strings
|
||||
* are translated in this call
|
||||
*
|
||||
* @param int $year year YYYY
|
||||
* @param int $month month m
|
||||
* @param int $day day d
|
||||
* @param int $hour hour H
|
||||
* @param int $min min i
|
||||
* @param string $suffix additional info printed after the date time
|
||||
* variable in the drop down
|
||||
* also used for ID in the on change JS call
|
||||
* @param int $min_steps default is 1 (minute), can set to anything,
|
||||
* is used as sum up from 0
|
||||
* @param bool $name_pos_back default false, if set to true,
|
||||
* the name will be printend
|
||||
* after the drop down and not before the drop down
|
||||
* @return string HTML formated strings for drop down lists
|
||||
* of date and time
|
||||
*/
|
||||
public function adbPrintDateTime(
|
||||
$year,
|
||||
$month,
|
||||
$day,
|
||||
$hour,
|
||||
$min,
|
||||
string $suffix = '',
|
||||
int $min_steps = 1,
|
||||
bool $name_pos_back = false
|
||||
) {
|
||||
// get the build layout
|
||||
$html_time = \CoreLibs\Output\Form\Elements::printDateTime(
|
||||
$year,
|
||||
$month,
|
||||
$day,
|
||||
$hour,
|
||||
$min,
|
||||
$suffix,
|
||||
$min_steps,
|
||||
$name_pos_back
|
||||
);
|
||||
// translate the strings inside
|
||||
foreach (['Year ', 'Month ', 'Day ', 'Hour ', 'Minute '] as $_time) {
|
||||
$html_time = str_replace($_time, $this->l->__(str_replace(' ', '', $_time)) . ' ', $html_time);
|
||||
}
|
||||
// replace week days in short
|
||||
foreach (['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] as $_date) {
|
||||
$html_time = str_replace('(' . $_date . ')', '(' . $this->l->__($_date) . ')', $html_time);
|
||||
}
|
||||
// return the datetime select string with strings translated
|
||||
return $html_time;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
588
4dev/deprecated/CoreLibs/Admin/EditBase.php
Normal file
588
4dev/deprecated/CoreLibs/Admin/EditBase.php
Normal file
@@ -0,0 +1,588 @@
|
||||
<?php
|
||||
|
||||
/*********************************************************************
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2023/1/6
|
||||
* DESCRIPTION:
|
||||
* Original created: 2003/06/10
|
||||
* This is the edit_base.php data as is moved into a class so we can
|
||||
* more easy update this and also move to a different AJAX style more
|
||||
* easy
|
||||
*********************************************************************/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Admin;
|
||||
|
||||
use Exception;
|
||||
use SmartyException;
|
||||
|
||||
class EditBase
|
||||
{
|
||||
/** @var array<mixed> */
|
||||
private $HEADER = [];
|
||||
/** @var array<mixed> */
|
||||
private $DATA = [];
|
||||
/** @var array<mixed> */
|
||||
private $DEBUG_DATA = [];
|
||||
|
||||
/** @var string the template name */
|
||||
private $EDIT_TEMPLATE = '';
|
||||
|
||||
/** @var \CoreLibs\Template\SmartyExtend smarty system */
|
||||
private $smarty;
|
||||
/** @var \CoreLibs\Output\Form\Generate form generate system */
|
||||
private $form;
|
||||
/** @var \CoreLibs\Debug\Logging */
|
||||
public $log;
|
||||
|
||||
/**
|
||||
* construct form generator
|
||||
*
|
||||
* @param array<mixed> $db_config db config array, mandatory
|
||||
* @param \CoreLibs\Debug\Logging $log Logging class, null auto set
|
||||
* @param \CoreLibs\Language\L10n $l10n l10n language class, null auto set
|
||||
* @param array<string,string> $locale locale array from ::setLocale,
|
||||
* null auto set
|
||||
*/
|
||||
public function __construct(
|
||||
array $db_config,
|
||||
\CoreLibs\Debug\Logging $log,
|
||||
\CoreLibs\Language\L10n $l10n,
|
||||
array $locale
|
||||
) {
|
||||
$this->log = $log;
|
||||
// smarty template engine (extended Translation version)
|
||||
$this->smarty = new \CoreLibs\Template\SmartyExtend($l10n, $locale);
|
||||
// turn off set log per class
|
||||
$log->setLogPer('class', false);
|
||||
|
||||
// create form class
|
||||
$this->form = new \CoreLibs\Output\Form\Generate(
|
||||
$db_config,
|
||||
$log,
|
||||
$l10n,
|
||||
$locale
|
||||
);
|
||||
if ($this->form->mobile_phone) {
|
||||
echo "I am sorry, but this page cannot be viewed by a mobile phone";
|
||||
exit;
|
||||
}
|
||||
// $this->form->log->debug('POST', $this->form->log->prAr($_POST));
|
||||
}
|
||||
|
||||
/**
|
||||
* edit order page
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function editOrderPage(): void
|
||||
{
|
||||
// get is for "table_name" and "where" only
|
||||
$table_name = $_GET['table_name'] ?? $_POST['table_name'] ?? '';
|
||||
// not in use
|
||||
// $where_string = $_GET['where'] ?? $_POST['where'] ?? '';
|
||||
// order name is _always_ order_number for the edit interface
|
||||
|
||||
// follwing arrays do exist here:
|
||||
// $position ... has the positions of the [0..max], cause in a <select>
|
||||
// I can't put an number into the array field, in this array,
|
||||
// there are the POSITION stored,
|
||||
// that should CHANGE there order (up/down)
|
||||
// $row_data_id ... has ALL ids from the sorting part
|
||||
// $row_data_order ... has ALL order positions from the soirting part
|
||||
$position = $_POST['position'] ?? [];
|
||||
$row_data_id = $_POST['row_data_id'] ?? [];
|
||||
$original_id = $row_data_id;
|
||||
$row_data_order = $_POST['row_data_order'] ?? [];
|
||||
// direction
|
||||
$up = $_POST['up'] ?? '';
|
||||
$down = $_POST['down'] ?? '';
|
||||
if (count($position)) {
|
||||
// FIRST u have to put right sort, then read again ...
|
||||
// hast to be >0 or the first one is selected and then there is no move
|
||||
if (!empty($up) && isset($position[0]) && $position[0] > 0) {
|
||||
for ($i = 0; $i < count($position); $i++) {
|
||||
// change position order
|
||||
// this gets temp, id before that, gets actual (moves one "down")
|
||||
// this gets the old before (moves one "up")
|
||||
// is done for every element in row
|
||||
// echo "A: ".$row_data_id[$position[$i]]
|
||||
// ." (".$row_data_order[$position[$i]].") -- ".$row_data_id[$position[$i]-1]
|
||||
// ." (".$row_data_order[$position[$i]-1].")<br>";
|
||||
$temp_id = $row_data_id[$position[$i]] ?? null;
|
||||
$row_data_id[$position[$i]] = $row_data_id[(int)$position[$i] - 1] ?? null;
|
||||
$row_data_id[(int)$position[$i] - 1] = $temp_id;
|
||||
// echo "A: ".$row_data_id[$position[$i]]
|
||||
// ." (".$row_data_order[$position[$i]].") -- "
|
||||
// .$row_data_id[$position[$i]-1]." (".$row_data_order[$position[$i]-1].")<br>";
|
||||
} // for
|
||||
} // if up
|
||||
|
||||
// the last position id from position array is not to be the count - 1 of
|
||||
// row_data_id array, or it is the last element
|
||||
if (!empty($down) && ($position[count($position) - 1] != (count($row_data_id) - 1))) {
|
||||
for ($i = count($position) - 1; $i >= 0; $i--) {
|
||||
// same as up, just up in other way, starts from bottom (last element) and moves "up"
|
||||
// element before actuel gets temp, this element, becomes element after this,
|
||||
// element after this, gets this
|
||||
$temp_id = $row_data_id[(int)$position[$i] + 1] ?? null;
|
||||
$row_data_id[(int)$position[$i] + 1] = $row_data_id[$position[$i]] ?? null;
|
||||
$row_data_id[$position[$i]] = $temp_id;
|
||||
} // for
|
||||
} // if down
|
||||
|
||||
// write data ... (which has to be abstrackt ...)
|
||||
if (
|
||||
(!empty($up) && $position[0] > 0) ||
|
||||
(!empty($down) && ($position[count($position) - 1] != (count($row_data_id) - 1)))
|
||||
) {
|
||||
for ($i = 0; $i < count($row_data_id); $i++) {
|
||||
if (isset($row_data_order[$i]) && isset($row_data_id[$i])) {
|
||||
$q = "UPDATE " . $table_name
|
||||
. " SET order_number = " . $row_data_order[$i]
|
||||
. " WHERE " . $table_name . "_id = " . $row_data_id[$i];
|
||||
$q = $this->form->dbExec($q);
|
||||
}
|
||||
} // for all article ids ...
|
||||
} // if write
|
||||
} // if there is something to move
|
||||
|
||||
// get ...
|
||||
$q = "SELECT " . $table_name . "_id, name, order_number FROM " . $table_name . " ";
|
||||
// /* if (!empty($where_string)) {
|
||||
// $q .= "WHERE $where_string ";
|
||||
// } */
|
||||
$q .= "ORDER BY order_number";
|
||||
|
||||
// init arrays
|
||||
$row_data = [];
|
||||
$options_id = [];
|
||||
$options_name = [];
|
||||
$options_selected = [];
|
||||
// DB read data for menu
|
||||
while (is_array($res = $this->form->dbReturn($q))) {
|
||||
$row_data[] = [
|
||||
"id" => $res[$table_name . "_id"],
|
||||
"name" => $res["name"],
|
||||
"order" => $res["order_number"]
|
||||
];
|
||||
} // while read data ...
|
||||
|
||||
// html title
|
||||
$this->HEADER['HTML_TITLE'] = $this->form->l->__('Edit Order');
|
||||
|
||||
$messages = [];
|
||||
$error = $_POST['error'] ?? 0;
|
||||
// error msg
|
||||
if (!empty($error)) {
|
||||
$msg = $_POST['msg'] ?? [];
|
||||
if (!is_array($msg)) {
|
||||
$msg = [];
|
||||
}
|
||||
$messages[] = [
|
||||
'msg' => $msg,
|
||||
'class' => 'error',
|
||||
'width' => '100%'
|
||||
];
|
||||
}
|
||||
$this->DATA['form_error_msg'] = $messages;
|
||||
|
||||
// all the row data
|
||||
for ($i = 0; $i < count($row_data); $i++) {
|
||||
$options_id[] = $i;
|
||||
$options_name[] = $row_data[$i]['name'];
|
||||
// list of points to order
|
||||
for ($j = 0; $j < count($position); $j++) {
|
||||
// if matches, put into select array
|
||||
if (
|
||||
isset($original_id[$position[$j]]) && isset($row_data[$i]['id']) &&
|
||||
$original_id[$position[$j]] == $row_data[$i]['id']
|
||||
) {
|
||||
$options_selected[] = $i;
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->DATA['options_id'] = $options_id;
|
||||
$this->DATA['options_name'] = $options_name;
|
||||
$this->DATA['options_selected'] = $options_selected;
|
||||
|
||||
// hidden list for the data (id, order number)
|
||||
$row_data_id = [];
|
||||
$row_data_order = [];
|
||||
for ($i = 0; $i < count($row_data); $i++) {
|
||||
$row_data_id[] = $row_data[$i]['id'];
|
||||
$row_data_order[] = $row_data[$i]['order'];
|
||||
}
|
||||
$this->DATA['row_data_id'] = $row_data_id;
|
||||
$this->DATA['row_data_order'] = $row_data_order;
|
||||
|
||||
// hidden names for the table & where string
|
||||
$this->DATA['table_name'] = $table_name;
|
||||
$this->DATA['where_string'] = '';
|
||||
// $this->DATA['where_string'] = $where_string ?? '';
|
||||
|
||||
$this->EDIT_TEMPLATE = 'edit_order.tpl';
|
||||
}
|
||||
|
||||
/**
|
||||
* all edit pages
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function editPageFlow(): void
|
||||
{
|
||||
// set table width
|
||||
$table_width = '100%';
|
||||
// load call only if id is set
|
||||
if (!empty($_POST[$this->form->archive_pk_name])) {
|
||||
$this->form->formProcedureLoad($_POST[$this->form->archive_pk_name]);
|
||||
}
|
||||
$this->form->formProcedureNew();
|
||||
$this->form->formProcedureSave();
|
||||
$this->form->formProcedureDelete();
|
||||
// delete call only if those two are set
|
||||
// and we are not in new/save/master delete
|
||||
if (
|
||||
!$this->form->new &&
|
||||
!$this->form->save &&
|
||||
!$this->form->delete &&
|
||||
!empty($_POST['element_list']) &&
|
||||
!empty($_POST['remove_name'])
|
||||
) {
|
||||
$this->form->formProcedureDeleteFromElementList(
|
||||
$_POST['element_list'],
|
||||
$_POST['remove_name']
|
||||
);
|
||||
// run a load post element delete to not end up with empty page
|
||||
$this->form->formLoadTableArray($_POST[$this->form->archive_pk_name]);
|
||||
$this->form->yes = 1;
|
||||
}
|
||||
|
||||
$this->DATA['table_width'] = $table_width;
|
||||
|
||||
$messages = [];
|
||||
// write out error / status messages
|
||||
$messages[] = $this->form->formPrintMsg();
|
||||
$this->DATA['form_error_msg'] = $messages;
|
||||
|
||||
// MENU START
|
||||
// request some session vars
|
||||
if (empty($_SESSION['HEADER_COLOR'])) {
|
||||
$this->DATA['HEADER_COLOR'] = '#E0E2FF';
|
||||
} else {
|
||||
$this->DATA['HEADER_COLOR'] = $_SESSION['HEADER_COLOR'];
|
||||
}
|
||||
$this->DATA['USER_NAME'] = $_SESSION['USER_NAME'];
|
||||
$this->DATA['EUID'] = $_SESSION['EUID'];
|
||||
$this->DATA['GROUP_NAME'] = $_SESSION['GROUP_NAME'];
|
||||
$this->DATA['GROUP_LEVEL'] = $_SESSION['GROUP_ACL_LEVEL'];
|
||||
$PAGES = $_SESSION['PAGES'];
|
||||
|
||||
//$this->form->log->debug('menu', $this->form->log->prAr($PAGES));
|
||||
|
||||
// build nav from $PAGES ...
|
||||
if (!isset($PAGES) || !is_array($PAGES)) {
|
||||
$PAGES = [];
|
||||
}
|
||||
$menuarray = [];
|
||||
foreach ($PAGES as $PAGE_CUID => $PAGE_DATA) {
|
||||
if ($PAGE_DATA['menu'] && $PAGE_DATA['online']) {
|
||||
$menuarray[] = $PAGE_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
// split point for nav points
|
||||
$COUNT_NAV_POINTS = count($menuarray);
|
||||
$SPLIT_FACTOR = 3;
|
||||
$START_SPLIT_COUNT = 3;
|
||||
// WTF ?? I dunno what I am doing here ...
|
||||
for ($i = 9; $i < $COUNT_NAV_POINTS; $i += $START_SPLIT_COUNT) {
|
||||
if ($COUNT_NAV_POINTS > $i) {
|
||||
$SPLIT_FACTOR += 1;
|
||||
}
|
||||
}
|
||||
|
||||
$position = 0;
|
||||
$menu_data = [];
|
||||
// for ($i = 1; $i <= count($menuarray); $i ++) {
|
||||
foreach ($menuarray as $i => $menu_element) {
|
||||
// do that for new array
|
||||
$j = $i + 1;
|
||||
$menu_data[$i]['pagename'] = htmlentities($menu_element['page_name']);
|
||||
$menu_data[$i]['filename'] =
|
||||
// prefix folder or host name
|
||||
(isset($menu_element['hostname']) && $menu_element['hostname'] ?
|
||||
$menu_element['hostname'] :
|
||||
''
|
||||
)
|
||||
// filename
|
||||
. ($menu_element['filename'] ?? '')
|
||||
// query string
|
||||
. (isset($menu_element['query_string']) && $menu_element['query_string'] ?
|
||||
$menu_element['query_string'] :
|
||||
''
|
||||
);
|
||||
if ($j == 1 || !($i % $SPLIT_FACTOR)) {
|
||||
$menu_data[$i]['splitfactor_in'] = 1;
|
||||
} else {
|
||||
$menu_data[$i]['splitfactor_in'] = 0;
|
||||
}
|
||||
// on matching, we also need to check if we are in the same folder
|
||||
if (
|
||||
isset($menu_element['filename']) &&
|
||||
$menu_element['filename'] == \CoreLibs\Get\System::getPageName() &&
|
||||
(!isset($menu_element['hostname']) || (
|
||||
isset($menu_element['hostname']) &&
|
||||
(!$menu_element['hostname'] || strstr($menu_element['hostname'], CONTENT_PATH) !== false)
|
||||
))
|
||||
) {
|
||||
$position = $i;
|
||||
$menu_data[$i]['position'] = 1;
|
||||
$menu_data[$i]['popup'] = 0;
|
||||
} else {
|
||||
// add query stuff
|
||||
// HAS TO DONE LATER ... set urlencode, etc ...
|
||||
// check if popup needed
|
||||
if (isset($menu_element['popup']) && $menu_element['popup'] == 1) {
|
||||
$menu_data[$i]['popup'] = 1;
|
||||
$menu_data[$i]['rand'] = uniqid((string)rand());
|
||||
$menu_data[$i]['width'] = $menu_element['popup_x'];
|
||||
$menu_data[$i]['height'] = $menu_element['popup_y'];
|
||||
} else {
|
||||
$menu_data[$i]['popup'] = 0;
|
||||
}
|
||||
$menu_data[$i]['position'] = 0;
|
||||
} // highlight or not
|
||||
if (!($j % $SPLIT_FACTOR) || (($j + 1) > count($menuarray))) {
|
||||
$menu_data[$i]['splitfactor_out'] = 1;
|
||||
} else {
|
||||
$menu_data[$i]['splitfactor_out'] = 0;
|
||||
}
|
||||
} // for
|
||||
// $this->form->log->debug('MENU ARRAY', $this->form->log->prAr($menu_data));
|
||||
$this->DATA['menu_data'] = $menu_data;
|
||||
$this->DATA['page_name'] = $menuarray[$position]['page_name'] ?? '-Undefined [' . $position . '] -';
|
||||
$L_TITLE = $this->DATA['page_name'];
|
||||
// html title
|
||||
$this->HEADER['HTML_TITLE'] = $this->form->l->__($L_TITLE);
|
||||
// END MENU
|
||||
// LOAD AND NEW
|
||||
$this->DATA['load'] = $this->form->formCreateLoad();
|
||||
$this->DATA['new'] = $this->form->formCreateNew();
|
||||
// SHOW DATA PART
|
||||
if ($this->form->yes) {
|
||||
$this->DATA['form_yes'] = $this->form->yes;
|
||||
$this->DATA['form_my_page_name'] = $this->form->my_page_name;
|
||||
$this->DATA['filename_exist'] = 0;
|
||||
$this->DATA['drop_down_input'] = 0;
|
||||
$elements = [];
|
||||
// depending on the "getPageName()" I show different stuff
|
||||
switch ($this->form->my_page_name) {
|
||||
case 'edit_users':
|
||||
$elements[] = $this->form->formCreateElement('login_error_count');
|
||||
$elements[] = $this->form->formCreateElement('login_error_date_last');
|
||||
$elements[] = $this->form->formCreateElement('login_error_date_first');
|
||||
$elements[] = $this->form->formCreateElement('enabled');
|
||||
$elements[] = $this->form->formCreateElement('deleted');
|
||||
$elements[] = $this->form->formCreateElement('protected');
|
||||
$elements[] = $this->form->formCreateElement('username');
|
||||
$elements[] = $this->form->formCreateElement('password');
|
||||
$elements[] = $this->form->formCreateElement('password_change_interval');
|
||||
$elements[] = $this->form->formCreateElement('login_user_id');
|
||||
$elements[] = $this->form->formCreateElement('login_user_id_set_date');
|
||||
$elements[] = $this->form->formCreateElement('login_user_id_last_revalidate');
|
||||
$elements[] = $this->form->formCreateElement('login_user_id_locked');
|
||||
$elements[] = $this->form->formCreateElement('login_user_id_revalidate_after');
|
||||
$elements[] = $this->form->formCreateElement('login_user_id_valid_from');
|
||||
$elements[] = $this->form->formCreateElement('login_user_id_valid_until');
|
||||
$elements[] = $this->form->formCreateElement('email');
|
||||
$elements[] = $this->form->formCreateElement('last_name');
|
||||
$elements[] = $this->form->formCreateElement('first_name');
|
||||
$elements[] = $this->form->formCreateElement('edit_group_id');
|
||||
$elements[] = $this->form->formCreateElement('edit_access_right_id');
|
||||
$elements[] = $this->form->formCreateElement('strict');
|
||||
$elements[] = $this->form->formCreateElement('locked');
|
||||
$elements[] = $this->form->formCreateElement('lock_until');
|
||||
$elements[] = $this->form->formCreateElement('lock_after');
|
||||
$elements[] = $this->form->formCreateElement('admin');
|
||||
$elements[] = $this->form->formCreateElement('debug');
|
||||
$elements[] = $this->form->formCreateElement('db_debug');
|
||||
$elements[] = $this->form->formCreateElement('edit_language_id');
|
||||
$elements[] = $this->form->formCreateElement('edit_scheme_id');
|
||||
$elements[] = $this->form->formCreateElementListTable('edit_access_user');
|
||||
$elements[] = $this->form->formCreateElement('additional_acl');
|
||||
break;
|
||||
case 'edit_schemes':
|
||||
$elements[] = $this->form->formCreateElement('enabled');
|
||||
$elements[] = $this->form->formCreateElement('name');
|
||||
$elements[] = $this->form->formCreateElement('header_color');
|
||||
$elements[] = $this->form->formCreateElement('template');
|
||||
break;
|
||||
case 'edit_pages':
|
||||
if (!isset($this->form->table_array['edit_page_id']['value'])) {
|
||||
$q = "DELETE FROM temp_files";
|
||||
$this->form->dbExec($q);
|
||||
// gets all files in the current dir and dirs given ending with .php
|
||||
$folders = ['../admin/', '../frontend/'];
|
||||
$files = ['*.php'];
|
||||
$search_glob = [];
|
||||
foreach ($folders as $folder) {
|
||||
// make sure this folder actually exists
|
||||
if (is_dir(ROOT . $folder)) {
|
||||
foreach ($files as $file) {
|
||||
$search_glob[] = $folder . $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
$crap = exec('ls ' . join(' ', $search_glob), $output, $status);
|
||||
// now get all that are NOT in de DB
|
||||
$q = "INSERT INTO temp_files (folder, filename) VALUES ";
|
||||
$t_q = '';
|
||||
foreach ($output as $output_file) {
|
||||
// split the ouput into folder and file
|
||||
$pathinfo = pathinfo($output_file);
|
||||
if (!empty($pathinfo['dirname'])) {
|
||||
$pathinfo['dirname'] .= DIRECTORY_SEPARATOR;
|
||||
} else {
|
||||
$pathinfo['dirname'] = '';
|
||||
}
|
||||
if ($t_q) {
|
||||
$t_q .= ', ';
|
||||
}
|
||||
$t_q .= "('" . $this->form->dbEscapeString($pathinfo['dirname']) . "', '"
|
||||
. $this->form->dbEscapeString($pathinfo['basename']) . "')";
|
||||
}
|
||||
$this->form->dbExec($q . $t_q, 'NULL');
|
||||
$elements[] = $this->form->formCreateElement('filename');
|
||||
} else {
|
||||
// show file menu
|
||||
// just show name of file ...
|
||||
$this->DATA['filename_exist'] = 1;
|
||||
$this->DATA['filename'] = $this->form->table_array['filename']['value'];
|
||||
} // File Name View IF
|
||||
$elements[] = $this->form->formCreateElement('hostname');
|
||||
$elements[] = $this->form->formCreateElement('name');
|
||||
// $elements[] = $this->form->formCreateElement('tag');
|
||||
// $elements[] = $this->form->formCreateElement('min_acl');
|
||||
$elements[] = $this->form->formCreateElement('order_number');
|
||||
$elements[] = $this->form->formCreateElement('online');
|
||||
$elements[] = $this->form->formCreateElement('menu');
|
||||
$elements[] = $this->form->formCreateElementListTable('edit_query_string');
|
||||
$elements[] = $this->form->formCreateElement('content_alias_edit_page_id');
|
||||
$elements[] = $this->form->formCreateElementListTable('edit_page_content');
|
||||
$elements[] = $this->form->formCreateElement('popup');
|
||||
$elements[] = $this->form->formCreateElement('popup_x');
|
||||
$elements[] = $this->form->formCreateElement('popup_y');
|
||||
$elements[] = $this->form->formCreateElementReferenceTable('edit_visible_group');
|
||||
$elements[] = $this->form->formCreateElementReferenceTable('edit_menu_group');
|
||||
break;
|
||||
case 'edit_languages':
|
||||
$elements[] = $this->form->formCreateElement('enabled');
|
||||
$elements[] = $this->form->formCreateElement('short_name');
|
||||
$elements[] = $this->form->formCreateElement('long_name');
|
||||
$elements[] = $this->form->formCreateElement('iso_name');
|
||||
break;
|
||||
case 'edit_groups':
|
||||
$elements[] = $this->form->formCreateElement('enabled');
|
||||
$elements[] = $this->form->formCreateElement('name');
|
||||
$elements[] = $this->form->formCreateElement('edit_access_right_id');
|
||||
$elements[] = $this->form->formCreateElement('edit_scheme_id');
|
||||
$elements[] = $this->form->formCreateElementListTable('edit_page_access');
|
||||
$elements[] = $this->form->formCreateElement('additional_acl');
|
||||
break;
|
||||
case 'edit_visible_group':
|
||||
$elements[] = $this->form->formCreateElement('name');
|
||||
$elements[] = $this->form->formCreateElement('flag');
|
||||
break;
|
||||
case 'edit_menu_group':
|
||||
$elements[] = $this->form->formCreateElement('name');
|
||||
$elements[] = $this->form->formCreateElement('flag');
|
||||
$elements[] = $this->form->formCreateElement('order_number');
|
||||
break;
|
||||
case 'edit_access':
|
||||
$elements[] = $this->form->formCreateElement('name');
|
||||
$elements[] = $this->form->formCreateElement('enabled');
|
||||
$elements[] = $this->form->formCreateElement('protected');
|
||||
$elements[] = $this->form->formCreateElement('color');
|
||||
$elements[] = $this->form->formCreateElement('description');
|
||||
// add name/value list here
|
||||
$elements[] = $this->form->formCreateElementListTable('edit_access_data');
|
||||
$elements[] = $this->form->formCreateElement('additional_acl');
|
||||
break;
|
||||
default:
|
||||
print '[No valid page definition given]';
|
||||
break;
|
||||
}
|
||||
// $this->form->log->debug('edit', "Elements: <pre>".$this->form->log->prAr($elements));
|
||||
$this->DATA['elements'] = $elements;
|
||||
$this->DATA['hidden'] = $this->form->formCreateHiddenFields();
|
||||
$this->DATA['save_delete'] = $this->form->formCreateSaveDelete();
|
||||
} else {
|
||||
$this->DATA['form_yes'] = 0;
|
||||
}
|
||||
$this->EDIT_TEMPLATE = 'edit_body.tpl';
|
||||
}
|
||||
|
||||
/**
|
||||
* main method that either calls edit order page method or general page
|
||||
* builds the smarty content and runs smarty display output
|
||||
*
|
||||
* @return void
|
||||
* @throws Exception
|
||||
* @throws SmartyException
|
||||
*/
|
||||
public function editBaseRun()
|
||||
{
|
||||
// set the template dir
|
||||
// WARNING: this has a special check for the mailing tool layout (old layout)
|
||||
if (defined('LAYOUT')) {
|
||||
$this->smarty->setTemplateDir(BASE . INCLUDES . TEMPLATES . CONTENT_PATH);
|
||||
$this->DATA['css'] = LAYOUT . CSS;
|
||||
$this->DATA['js'] = LAYOUT . JS;
|
||||
} else {
|
||||
$this->smarty->setTemplateDir(TEMPLATES);
|
||||
$this->DATA['css'] = CSS;
|
||||
$this->DATA['js'] = JS;
|
||||
}
|
||||
$ADMIN_STYLESHEET = 'edit.css';
|
||||
// define all needed smarty stuff for the general HTML/page building
|
||||
$this->HEADER['CSS'] = CSS;
|
||||
$this->HEADER['DEFAULT_ENCODING'] = DEFAULT_ENCODING;
|
||||
/** @phpstan-ignore-next-line because ADMIN_STYLESHEET can be null */
|
||||
$this->HEADER['STYLESHEET'] = $ADMIN_STYLESHEET ?? ADMIN_STYLESHEET;
|
||||
|
||||
// main run
|
||||
if ($this->form->my_page_name == 'edit_order') {
|
||||
$this->editOrderPage();
|
||||
} else {
|
||||
$this->editPageFlow();
|
||||
}
|
||||
|
||||
// debug data, if DEBUG flag is on, this data is print out
|
||||
// $this->DEBUG_DATA['DEBUG'] = $DEBUG_TMPL ?? '';
|
||||
$this->DEBUG_DATA['DEBUG'] = '';
|
||||
|
||||
// create main data array
|
||||
$CONTENT_DATA = array_merge($this->HEADER, $this->DATA, $this->DEBUG_DATA);
|
||||
// data is 1:1 mapping (all vars, values, etc)
|
||||
foreach ($CONTENT_DATA as $key => $value) {
|
||||
$this->smarty->assign($key, $value);
|
||||
}
|
||||
if (is_dir(BASE . TEMPLATES_C)) {
|
||||
$this->smarty->setCompileDir(BASE . TEMPLATES_C);
|
||||
}
|
||||
if (is_dir(BASE . CACHE)) {
|
||||
$this->smarty->setCacheDir(BASE . CACHE);
|
||||
}
|
||||
$this->smarty->display(
|
||||
$this->EDIT_TEMPLATE,
|
||||
'editAdmin_' . $this->smarty->lang,
|
||||
'editAdmin_' . $this->smarty->lang
|
||||
);
|
||||
|
||||
$this->form->log->debug('DEBUGEND', '==================================== [Form END]');
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
1549
4dev/deprecated/CoreLibs/Basic.php
Normal file
1549
4dev/deprecated/CoreLibs/Basic.php
Normal file
File diff suppressed because it is too large
Load Diff
187
4dev/deprecated/CoreLibs/Check/Colors.php
Normal file
187
4dev/deprecated/CoreLibs/Check/Colors.php
Normal file
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* valid checks for css/html based colors
|
||||
* # hex
|
||||
* # hex + alpha
|
||||
* rgb
|
||||
* rgba
|
||||
* hsl
|
||||
* hsla
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Check;
|
||||
|
||||
use Exception;
|
||||
|
||||
class Colors
|
||||
{
|
||||
/** @var int 1 for HEX rgb */
|
||||
public const HEX_RGB = 1;
|
||||
/** @var int 2 for HEX rgb with alpha */
|
||||
public const HEX_RGBA = 2;
|
||||
/** @var int 4 for rgb() */
|
||||
public const RGB = 4;
|
||||
/** @var int 8 for rgba() */
|
||||
public const RGBA = 8;
|
||||
/** @var int 16 for hsl() */
|
||||
public const HSL = 16;
|
||||
/** @var int 32 for hsla() */
|
||||
public const HSLA = 32;
|
||||
/** @var int 63 for all bits set (sum of above) */
|
||||
public const ALL = 63;
|
||||
|
||||
/**
|
||||
* check rgb/hsl content values in detail
|
||||
* will abort and return false on first error found
|
||||
*
|
||||
* @param string $color html/css tring to check
|
||||
* @param int|false $rgb_flag flag to check for rgb
|
||||
* @param int|false $hsl_flag flag to check for hsl type
|
||||
* @return bool True if no error, False if error
|
||||
*/
|
||||
private static function rgbHslContentCheck(string $color, $rgb_flag, $hsl_flag): bool
|
||||
{
|
||||
// extract string between () and split into elements
|
||||
preg_match("/\((.*)\)/", $color, $matches);
|
||||
if (
|
||||
!is_array($color_list = preg_split("/,\s*/", $matches[1] ?? ''))
|
||||
) {
|
||||
throw new \Exception("Could not extract color list from rgg/hsl", 3);
|
||||
}
|
||||
// based on rgb/hsl settings check that entries are valid
|
||||
// rgb: either 0-255 OR 0-100%
|
||||
// hsl: first: 0-360
|
||||
foreach ($color_list as $pos => $color_check) {
|
||||
if (empty($color_check)) {
|
||||
return false;
|
||||
}
|
||||
$percent_check = false;
|
||||
if (strrpos($color_check, '%', -1) !== false) {
|
||||
$percent_check = true;
|
||||
$color_check = str_replace('%', '', $color_check);
|
||||
}
|
||||
// first three normal percent or valid number
|
||||
if ($rgb_flag !== false) {
|
||||
if ($percent_check === true) {
|
||||
// for ALL pos
|
||||
if ($color_check < 0 || $color_check > 100) {
|
||||
return false;
|
||||
}
|
||||
} elseif (
|
||||
$pos < 3 &&
|
||||
($color_check < 0 || $color_check > 255)
|
||||
) {
|
||||
return false;
|
||||
} elseif (
|
||||
// RGBA set pos 3 if not percent
|
||||
$pos == 3 &&
|
||||
($color_check < 0 || $color_check > 1)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
} elseif ($hsl_flag !== false) {
|
||||
// pos 0: 0-360
|
||||
// pos 1,2: %
|
||||
// pos 3: % or 0-1 (float)
|
||||
if (
|
||||
$pos == 0 &&
|
||||
($color_check < 0 || $color_check > 360)
|
||||
) {
|
||||
return false;
|
||||
} elseif (
|
||||
// if pos 1/2 are not percent
|
||||
($pos == 1 || $pos == 2) &&
|
||||
($percent_check != true ||
|
||||
($color_check < 0 || $color_check > 100))
|
||||
) {
|
||||
return false;
|
||||
} elseif (
|
||||
// 3 is either percent or 0~1
|
||||
$pos == 3 &&
|
||||
(
|
||||
($percent_check == false &&
|
||||
($color_check < 0 || $color_check > 1)) ||
|
||||
($percent_check === true &&
|
||||
($color_check < 0 || $color_check > 100))
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if html/css color string is valid
|
||||
* @param string $color A color string of any format
|
||||
* @param int $flags defaults to ALL, else use | to combined from
|
||||
* HEX_RGB, HEX_RGBA, RGB, RGBA, HSL, HSLA
|
||||
* @return bool True if valid, False if not
|
||||
* @throws Exception 1: no valid flag set
|
||||
*/
|
||||
public static function validateColor(string $color, int $flags = self::ALL): bool
|
||||
{
|
||||
// blocks for each check
|
||||
$regex_blocks = [];
|
||||
// set what to check
|
||||
if ($flags & self::HEX_RGB) {
|
||||
$regex_blocks[] = '#[\dA-Fa-f]{6}';
|
||||
}
|
||||
if ($flags & self::HEX_RGBA) {
|
||||
$regex_blocks[] = '#[\dA-Fa-f]{8}';
|
||||
}
|
||||
if ($flags & self::RGB) {
|
||||
$regex_blocks[] = 'rgb\(\d{1,3}%?,\s*\d{1,3}%?,\s*\d{1,3}%?\)';
|
||||
}
|
||||
if ($flags & self::RGBA) {
|
||||
$regex_blocks[] = 'rgba\(\d{1,3}%?,\s*\d{1,3}%?,\s*\d{1,3}%?(,\s*(0\.\d{1,2}|1(\.0)?|\d{1,3}%))?\)';
|
||||
}
|
||||
if ($flags & self::HSL) {
|
||||
$regex_blocks[] = 'hsl\(\d{1,3},\s*\d{1,3}(\.\d{1})?%,\s*\d{1,3}(\.\d{1})?%\)';
|
||||
}
|
||||
if ($flags & self::HSLA) {
|
||||
$regex_blocks[] = 'hsla\(\d{1,3},\s*\d{1,3}(\.\d{1})?%,\s*\d{1,3}'
|
||||
. '(\.\d{1})?%(,\s*(0\.\d{1,2}|1(\.0)?|\d{1,3}%))?\)';
|
||||
}
|
||||
// wrong flag set
|
||||
if ($flags > self::ALL) {
|
||||
throw new \Exception("Invalid flags parameter: $flags", 1);
|
||||
}
|
||||
if (!count($regex_blocks)) {
|
||||
throw new \Exception("No regex blocks set: $flags", 2);
|
||||
}
|
||||
|
||||
// build regex
|
||||
$regex = '^('
|
||||
. join('|', $regex_blocks)
|
||||
// close regex
|
||||
. ')$';
|
||||
// print "C: $color, F: $flags, R: $regex\n";
|
||||
|
||||
if (preg_match("/$regex/", $color)) {
|
||||
// if valid regex, we now need to check if the content is actually valid
|
||||
// only for rgb/hsl type
|
||||
/** @var int|false */
|
||||
$rgb_flag = strpos($color, 'rgb');
|
||||
/** @var int|false */
|
||||
$hsl_flag = strpos($color, 'hsl');
|
||||
// if both not match, return true
|
||||
if (
|
||||
$rgb_flag === false &&
|
||||
$hsl_flag === false
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
// run detaul rgb/hsl content check
|
||||
return self::rgbHslContentCheck($color, $rgb_flag, $hsl_flag);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
@@ -8,7 +8,7 @@ class Email
|
||||
{
|
||||
// this is for error check parts in where the email regex failed
|
||||
/** @var array<int,string> */
|
||||
private static array $email_regex_check = [
|
||||
private static $email_regex_check = [
|
||||
0 => "^[A-Za-z0-9!#$%&'*+\-\/=?^_`{|}~][A-Za-z0-9!#$%:\(\)&'*+\-\/=?^_`{|}~\.]{0,63}@"
|
||||
. "[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]{1,})*\.([a-zA-Z]{2,}){1}$", // MASTER
|
||||
1 => "@(.*)@(.*)", // double @
|
||||
@@ -21,7 +21,7 @@ class Email
|
||||
];
|
||||
// for above position, description string below
|
||||
/** @var array<int,string> */
|
||||
private static array $email_regex_check_message = [
|
||||
private static $email_regex_check_message = [
|
||||
0 => 'Invalid email address',
|
||||
1 => 'Double @ mark in email address',
|
||||
2 => 'Invalid email part before @ sign',
|
||||
@@ -33,7 +33,7 @@ class Email
|
||||
];
|
||||
// the array with the mobile types that are valid
|
||||
/** @var array<string,string> */
|
||||
private static array $mobile_email_type = [
|
||||
private static $mobile_email_type = [
|
||||
'.*@docomo\.ne\.jp$' => 'keitai_docomo',
|
||||
// correct are a[2-4], b2, c[1-9], e[2-9], h[2-4], t[1-9]
|
||||
'.*@([a-z0-9]{2}\.)?ezweb\.ne\.jp$' => 'keitai_kddi_ezweb',
|
||||
@@ -72,7 +72,7 @@ class Email
|
||||
];
|
||||
// short list for mobile email types
|
||||
/** @var array<string,string> */
|
||||
private static array $mobile_email_type_short = [
|
||||
private static $mobile_email_type_short = [
|
||||
'keitai_docomo' => 'docomo',
|
||||
'keitai_kddi_ezweb' => 'kddi',
|
||||
'keitai_kddi' => 'kddi',
|
||||
@@ -169,10 +169,10 @@ class Email
|
||||
* @param string $email email string
|
||||
* @param bool $short default false, if true,
|
||||
* returns only short type (pc instead of pc_html)
|
||||
* @return string|false email type, eg "pc", "docomo", etc,
|
||||
* @return string|bool email type, eg "pc", "docomo", etc,
|
||||
* false for invalid short type
|
||||
*/
|
||||
public static function getEmailType(string $email, bool $short = false): string|false
|
||||
public static function getEmailType(string $email, bool $short = false)
|
||||
{
|
||||
// trip if there is no email address
|
||||
if (!$email) {
|
||||
@@ -200,9 +200,9 @@ class Email
|
||||
* gets the short email type from a long email type
|
||||
*
|
||||
* @param string $email_type email string
|
||||
* @return string|false short string or false for invalid
|
||||
* @return string|bool short string or false for invalid
|
||||
*/
|
||||
public static function getShortEmailType(string $email_type): string|false
|
||||
public static function getShortEmailType(string $email_type)
|
||||
{
|
||||
// check if the short email type exists
|
||||
if (isset(self::$mobile_email_type_short[$email_type])) {
|
||||
117
4dev/deprecated/CoreLibs/Check/Encoding.php
Normal file
117
4dev/deprecated/CoreLibs/Check/Encoding.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* check if string is valid in target encoding
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Check;
|
||||
|
||||
class Encoding
|
||||
{
|
||||
/** @var int<min, -1>|int<1, max>|string */
|
||||
private static $mb_error_char = '';
|
||||
|
||||
/**
|
||||
* set error char
|
||||
*
|
||||
* @param string|int|null $string The character to use to represent
|
||||
* error chars
|
||||
* "long" for long, "none" for none
|
||||
* or a valid code point in int
|
||||
* like 0x2234 (8756, ∴)
|
||||
* default character is ? (63)
|
||||
* if null is set then "none"
|
||||
* @return void
|
||||
*/
|
||||
public static function setErrorChar($string): void
|
||||
{
|
||||
if (empty($string)) {
|
||||
$string = 'none';
|
||||
}
|
||||
// if not special string or char but code point
|
||||
if (in_array($string, ['none', 'long', 'entity'])) {
|
||||
self::$mb_error_char = $string;
|
||||
} else {
|
||||
// always convert to char for internal use
|
||||
self::$mb_error_char = \IntlChar::chr($string);
|
||||
// if string convert to code point
|
||||
if (is_string($string)) {
|
||||
$string = \IntlChar::ord($string);
|
||||
}
|
||||
}
|
||||
mb_substitute_character($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the current set error character
|
||||
*
|
||||
* @param bool $return_substitute_func if set to true return the set
|
||||
* character from the php function
|
||||
* directly
|
||||
* @return string|int Set error character
|
||||
*/
|
||||
public static function getErrorChar(bool $return_substitute_func = false)
|
||||
{
|
||||
// return mb_substitute_character();
|
||||
if ($return_substitute_func === true) {
|
||||
return mb_substitute_character();
|
||||
} else {
|
||||
return self::$mb_error_char;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* test if a string can be safely convert between encodings.
|
||||
* mostly utf8 to shift jis
|
||||
* the default compare has a possibility of failure, especially with windows
|
||||
* it is recommended to the following in the script which uses this method:
|
||||
* mb_substitute_character(0x2234);
|
||||
* $class->mb_error_char = '∴';
|
||||
* if check to Shift JIS
|
||||
* if check to ISO-2022-JP
|
||||
* if check to ISO-2022-JP-MS
|
||||
* set three dots (∴) as wrong character for correct convert error detect
|
||||
* (this char is used, because it is one of the least used ones)
|
||||
*
|
||||
* @param string $string string to test
|
||||
* @param string $from_encoding encoding of string to test
|
||||
* @param string $to_encoding target encoding
|
||||
* @return bool|array<string> false if no error or
|
||||
* array with failed characters
|
||||
*/
|
||||
public static function checkConvertEncoding(
|
||||
string $string,
|
||||
string $from_encoding,
|
||||
string $to_encoding
|
||||
) {
|
||||
// convert to target encoding and convert back
|
||||
$temp = mb_convert_encoding($string, $to_encoding, $from_encoding);
|
||||
$compare = mb_convert_encoding($temp, $from_encoding, $to_encoding);
|
||||
// if string does not match anymore we have a convert problem
|
||||
if ($string != $compare) {
|
||||
$failed = [];
|
||||
// go through each character and find the ones that do not match
|
||||
for ($i = 0, $iMax = mb_strlen($string, $from_encoding); $i < $iMax; $i++) {
|
||||
$char = mb_substr($string, $i, 1, $from_encoding);
|
||||
$r_char = mb_substr($compare, $i, 1, $from_encoding);
|
||||
// the ord 194 is a hack to fix the IE7/IE8
|
||||
// bug with line break and illegal character
|
||||
if (
|
||||
(($char != $r_char && (!self::$mb_error_char ||
|
||||
in_array(self::$mb_error_char, ['none', 'long', 'entity']))) ||
|
||||
($char != $r_char && $r_char == self::$mb_error_char && self::$mb_error_char)) &&
|
||||
ord($char) != 194
|
||||
) {
|
||||
$failed[] = $char;
|
||||
}
|
||||
}
|
||||
return $failed;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
56
4dev/deprecated/CoreLibs/Check/File.php
Normal file
56
4dev/deprecated/CoreLibs/Check/File.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* various file/file name functions
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Check;
|
||||
|
||||
class File
|
||||
{
|
||||
/**
|
||||
* quick return the extension of the given file name
|
||||
*
|
||||
* @param string $filename file name
|
||||
* @return string extension of the file name
|
||||
*/
|
||||
public static function getFilenameEnding(string $filename): string
|
||||
{
|
||||
$page_temp = pathinfo($filename);
|
||||
return $page_temp['extension'] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* get lines in a file
|
||||
*
|
||||
* @param string $file file for line count read
|
||||
* @return int number of lines or -1 for non readable file
|
||||
*/
|
||||
public static function getLinesFromFile(string $file): int
|
||||
{
|
||||
if (
|
||||
is_file($file) &&
|
||||
file_exists($file) &&
|
||||
is_readable($file)
|
||||
) {
|
||||
$f = fopen($file, 'rb');
|
||||
if (!is_resource($f)) {
|
||||
return 0;
|
||||
}
|
||||
$lines = 0;
|
||||
while (!feof($f)) {
|
||||
$lines += substr_count(fread($f, 8192) ?: '', "\n");
|
||||
}
|
||||
fclose($f);
|
||||
} else {
|
||||
// if file does not exist or is not readable, return -1
|
||||
$lines = -1;
|
||||
}
|
||||
// return lines in file
|
||||
return $lines;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
41
4dev/deprecated/CoreLibs/Check/Jason.php
Normal file
41
4dev/deprecated/CoreLibs/Check/Jason.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DEPRECATED: Use correct Convert\Json:: instead
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Check;
|
||||
|
||||
use CoreLibs\Convert\Json;
|
||||
|
||||
class Jason
|
||||
{
|
||||
/**
|
||||
* @param string|null $json a json string, or null data
|
||||
* @param bool $override if set to true, then on json error
|
||||
* set original value as array
|
||||
* @return array<mixed> returns an array from the json values
|
||||
* @deprecated Use Json::jsonConvertToArray()
|
||||
*/
|
||||
public static function jsonConvertToArray(?string $json, bool $override = false): array
|
||||
{
|
||||
return Json::jsonConvertToArray($json, $override);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool|boolean $return_string [default=false] if set to true
|
||||
* it will return the message string and not
|
||||
* the error number
|
||||
* @return int|string Either error number (0 for no error)
|
||||
* or error string ('' for no error)
|
||||
* @deprecated Use Json::jsonGetLastError()
|
||||
*/
|
||||
public static function jsonGetLastError(bool $return_string = false)
|
||||
{
|
||||
return Json::jsonGetLastError($return_string);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
59
4dev/deprecated/CoreLibs/Check/Password.php
Normal file
59
4dev/deprecated/CoreLibs/Check/Password.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* core password set, check and rehash check wrapper functions
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Check;
|
||||
|
||||
class Password
|
||||
{
|
||||
/**
|
||||
* creates the password hash
|
||||
*
|
||||
* @param string $password password
|
||||
* @return string hashed password
|
||||
*/
|
||||
public static function passwordSet(string $password): string
|
||||
{
|
||||
// always use the PHP default for the password
|
||||
// password options ca be set in the password init,
|
||||
// but should be kept as default
|
||||
return password_hash($password, PASSWORD_DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if the entered password matches the hash
|
||||
*
|
||||
* @param string $password password
|
||||
* @param string $hash password hash
|
||||
* @return bool true or false
|
||||
*/
|
||||
public static function passwordVerify(string $password, string $hash): bool
|
||||
{
|
||||
if (password_verify($password, $hash)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if the password needs to be rehashed
|
||||
*
|
||||
* @param string $hash password hash
|
||||
* @return bool true or false
|
||||
*/
|
||||
public static function passwordRehashCheck(string $hash): bool
|
||||
{
|
||||
if (password_needs_rehash($hash, PASSWORD_DEFAULT)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
412
4dev/deprecated/CoreLibs/Combined/ArrayHandler.php
Normal file
412
4dev/deprecated/CoreLibs/Combined/ArrayHandler.php
Normal file
@@ -0,0 +1,412 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* array search and transform functions
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Combined;
|
||||
|
||||
class ArrayHandler
|
||||
{
|
||||
/**
|
||||
* searches key = value in an array / array
|
||||
* only returns the first one found
|
||||
*
|
||||
* @param string|int $needle needle (search for)
|
||||
* @param array<mixed> $haystack haystack (search in)
|
||||
* @param string|null $key_search_for the key to look out for, default empty
|
||||
* @return array<mixed> array with the elements where
|
||||
* the needle can be found in the
|
||||
* haystack array
|
||||
*/
|
||||
public static function arraySearchRecursive($needle, array $haystack, ?string $key_search_for = null): array
|
||||
{
|
||||
$path = [];
|
||||
if (!is_array($haystack)) {
|
||||
$haystack = [];
|
||||
}
|
||||
if (
|
||||
$key_search_for != null &&
|
||||
array_key_exists($key_search_for, $haystack) &&
|
||||
$needle === $haystack[$key_search_for]
|
||||
) {
|
||||
$path[] = $key_search_for;
|
||||
} else {
|
||||
foreach ($haystack as $key => $val) {
|
||||
if (
|
||||
is_scalar($val) &&
|
||||
$val === $needle &&
|
||||
empty($key_search_for)
|
||||
) {
|
||||
$path[] = $key;
|
||||
break;
|
||||
} elseif (
|
||||
is_scalar($val) &&
|
||||
!empty($key_search_for) &&
|
||||
$key === $key_search_for &&
|
||||
$val === $needle
|
||||
) {
|
||||
$path[] = $key;
|
||||
break;
|
||||
} elseif (
|
||||
is_array($val) &&
|
||||
$path = self::arraySearchRecursive(
|
||||
$needle,
|
||||
(array)$val,
|
||||
// to avoid PhanTypeMismatchArgumentNullable
|
||||
($key_search_for === null ? $key_search_for : (string)$key_search_for)
|
||||
)
|
||||
) {
|
||||
array_unshift($path, $key);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* recursive array search function, which returns all found not only the first one
|
||||
*
|
||||
* @param string|int $needle needle (search for)
|
||||
* @param array<mixed> $haystack haystack (search in)
|
||||
* @param string|int $key_search_for the key to look for in
|
||||
* @param bool $old [true], if set to false will
|
||||
* return new flat layout
|
||||
* @param array<mixed>|null $path recursive call for previous path
|
||||
* @return array<mixed>|null all array elements paths where
|
||||
* the element was found
|
||||
*/
|
||||
public static function arraySearchRecursiveAll(
|
||||
$needle,
|
||||
array $haystack,
|
||||
$key_search_for,
|
||||
bool $old = true,
|
||||
?array $path = null
|
||||
): ?array {
|
||||
// init if not set on null
|
||||
if ($path === null) {
|
||||
$path = [
|
||||
'level' => 0,
|
||||
'work' => []
|
||||
];
|
||||
} else {
|
||||
// init sub sets if not set
|
||||
if (!isset($path['level'])) {
|
||||
$path['level'] = 0;
|
||||
}
|
||||
if (!isset($path['work'])) {
|
||||
$path['work'] = [];
|
||||
}
|
||||
}
|
||||
// should not be needed because it would trigger a php mehtod error
|
||||
if (!is_array($haystack)) {
|
||||
$haystack = [];
|
||||
}
|
||||
|
||||
// go through the array,
|
||||
foreach ($haystack as $_key => $_value) {
|
||||
if (
|
||||
is_scalar($_value) &&
|
||||
$_value === $needle &&
|
||||
empty($key_search_for)
|
||||
) {
|
||||
// only value matches
|
||||
$path['work'][$path['level'] ?? 0] = $_key;
|
||||
$path['found'][] = $path['work'];
|
||||
} elseif (
|
||||
is_scalar($_value) &&
|
||||
!empty($key_search_for) &&
|
||||
$_key === $key_search_for &&
|
||||
$_value === $needle
|
||||
) {
|
||||
// key and value matches
|
||||
$path['work'][$path['level'] ?? 0] = $_key;
|
||||
$path['found'][] = $path['work'];
|
||||
} elseif (is_array($_value)) {
|
||||
// add position to working
|
||||
$path['work'][$path['level'] ?? 0] = $_key;
|
||||
// we will up a level
|
||||
$path['level'] += 1;
|
||||
// call recursive
|
||||
$path = self::arraySearchRecursiveAll($needle, $_value, $key_search_for, $old, $path);
|
||||
}
|
||||
}
|
||||
// be 100% sure the array elements are set
|
||||
$path['level'] = $path['level'] ?? 0;
|
||||
$path['work'] = $path['work'] ?? [];
|
||||
// cut all that is >= level
|
||||
array_splice($path['work'], $path['level']);
|
||||
// step back a level
|
||||
$path['level'] -= 1;
|
||||
if ($old === false && $path['level'] == -1) {
|
||||
return $path['found'] ?? [];
|
||||
} else {
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* array search simple. looks for key, value combination, if found, returns true
|
||||
* on default does not strict check, so string '4' will match int 4 and vica versa
|
||||
*
|
||||
* @param array<mixed> $array search in as array
|
||||
* @param string|int $key key (key to search in)
|
||||
* @param string|int $value value (what to find)
|
||||
* @param bool $strict [false], if set to true, will strict check key/value
|
||||
* @return bool true on found, false on not found
|
||||
*/
|
||||
public static function arraySearchSimple(array $array, $key, $value, bool $strict = false): bool
|
||||
{
|
||||
if (!is_array($array)) {
|
||||
$array = [];
|
||||
}
|
||||
foreach ($array as $_key => $_value) {
|
||||
// if value is an array, we search
|
||||
if (is_array($_value)) {
|
||||
// call recursive, and return result if it is true, else continue
|
||||
if (($result = self::arraySearchSimple($_value, $key, $value, $strict)) !== false) {
|
||||
return $result;
|
||||
}
|
||||
} elseif ($strict === false && $_key == $key && $_value == $value) {
|
||||
return true;
|
||||
} elseif ($strict === true && $_key === $key && $_value === $value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// no true returned, not found
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* correctly recursive merges as an array as array_merge_recursive
|
||||
* just glues things together
|
||||
* array first array to merge
|
||||
* array second array to merge
|
||||
* ... etc
|
||||
* bool key flag: true: handle keys as string or int
|
||||
* default false: all keys are string
|
||||
*
|
||||
* @return array<mixed>|bool merged array
|
||||
*/
|
||||
public static function arrayMergeRecursive()
|
||||
{
|
||||
// croak on not enough arguemnts (we need at least two)
|
||||
if (func_num_args() < 2) {
|
||||
trigger_error(__FUNCTION__ . ' needs two or more array arguments', E_USER_WARNING);
|
||||
return false;
|
||||
}
|
||||
// default key is not string
|
||||
$key_is_string = false;
|
||||
$arrays = func_get_args();
|
||||
// if last is not array, then assume it is trigger for key is always string
|
||||
if (!is_array(end($arrays))) {
|
||||
if (array_pop($arrays)) {
|
||||
$key_is_string = true;
|
||||
}
|
||||
}
|
||||
// check that arrays count is at least two, else we don't have enough to do anything
|
||||
if (count($arrays) < 2) {
|
||||
trigger_error(__FUNCTION__ . ' needs two or more array arguments', E_USER_WARNING);
|
||||
return false;
|
||||
}
|
||||
$merged = [];
|
||||
while ($arrays) {
|
||||
$array = array_shift($arrays);
|
||||
if (!is_array($array)) {
|
||||
trigger_error(__FUNCTION__ . ' encountered a non array argument', E_USER_WARNING);
|
||||
return false;
|
||||
}
|
||||
if (!$array) {
|
||||
continue;
|
||||
}
|
||||
foreach ($array as $key => $value) {
|
||||
// if string or if key is assumed to be string do key match
|
||||
// else add new entry
|
||||
if (is_string($key) || $key_is_string === false) {
|
||||
if (is_array($value) && array_key_exists($key, $merged) && is_array($merged[$key])) {
|
||||
// $merged[$key] = call_user_func(__METHOD__, $merged[$key], $value, $key_is_string);
|
||||
$merged[$key] = self::arrayMergeRecursive($merged[$key], $value, $key_is_string);
|
||||
} else {
|
||||
$merged[$key] = $value;
|
||||
}
|
||||
} else {
|
||||
$merged[] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $merged;
|
||||
}
|
||||
|
||||
/**
|
||||
* correct array_diff that does an actualy difference between two arrays.
|
||||
* array_diff only checks elements from A that are not in B, but not the
|
||||
* other way around.
|
||||
* Note that like array_diff this only checks first level values not keys
|
||||
*
|
||||
* @param array<mixed> $a array to compare a
|
||||
* @param array<mixed> $b array to compare b
|
||||
* @return array<mixed> array with missing elements from a & b
|
||||
*/
|
||||
public static function arrayDiff(array $a, array $b): array
|
||||
{
|
||||
$intersect = array_intersect($a, $b);
|
||||
return array_merge(array_diff($a, $intersect), array_diff($b, $intersect));
|
||||
}
|
||||
|
||||
/**
|
||||
* search for the needle array elements in haystack and
|
||||
* return the ones found as an array,
|
||||
* is there nothing found, it returns FALSE (boolean)
|
||||
*
|
||||
* @param array<mixed> $needle elements to search for
|
||||
* @param array<mixed> $haystack array where the $needle elements should
|
||||
* be searched int
|
||||
* @return array<mixed>|bool either the found elements or
|
||||
* false for nothing found or error
|
||||
*/
|
||||
public static function inArrayAny(array $needle, array $haystack)
|
||||
{
|
||||
$found = [];
|
||||
foreach ($needle as $element) {
|
||||
if (in_array($element, $haystack)) {
|
||||
$found[] = $element;
|
||||
}
|
||||
}
|
||||
if (count($found) == 0) {
|
||||
return false;
|
||||
} else {
|
||||
return $found;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* creates out of a normal db_return array an assoc array
|
||||
*
|
||||
* @param array<mixed> $db_array return array from the database
|
||||
* @param string|int|bool $key key set, false for not set
|
||||
* @param string|int|bool $value value set, false for not set
|
||||
* @param bool $set_only flag to return all (default), or set only
|
||||
* @return array<mixed> associative array
|
||||
*/
|
||||
public static function genAssocArray(array $db_array, $key, $value, bool $set_only = false): array
|
||||
{
|
||||
$ret_array = [];
|
||||
// do this to only run count once
|
||||
for ($i = 0, $iMax = count($db_array); $i < $iMax; $i++) {
|
||||
// if no key then we make an order reference
|
||||
if (
|
||||
$key !== false &&
|
||||
$value !== false &&
|
||||
(($set_only && !empty($db_array[$i][$value])) ||
|
||||
(!$set_only && isset($db_array[$i][$value]))) &&
|
||||
!empty($db_array[$i][$key])
|
||||
) {
|
||||
$ret_array[$db_array[$i][$key]] = $db_array[$i][$value];
|
||||
} elseif (
|
||||
$key === false && $value !== false &&
|
||||
isset($db_array[$i][$value])
|
||||
) {
|
||||
$ret_array[] = $db_array[$i][$value];
|
||||
} elseif (
|
||||
$key !== false && $value === false &&
|
||||
!empty($db_array[$i][$key])
|
||||
) {
|
||||
$ret_array[$db_array[$i][$key]] = $i;
|
||||
}
|
||||
}
|
||||
return $ret_array;
|
||||
}
|
||||
|
||||
/**
|
||||
* converts multi dimensional array to a flat array
|
||||
* does NOT preserve keys
|
||||
*
|
||||
* @param array<mixed> $array multi dimensionial array
|
||||
* @return array<mixed> flattened array
|
||||
*/
|
||||
public static function flattenArray(array $array): array
|
||||
{
|
||||
$return = [];
|
||||
array_walk_recursive(
|
||||
$array,
|
||||
function ($value) use (&$return) {
|
||||
$return[] = $value;
|
||||
}
|
||||
);
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* will loop through an array recursivly and write the array keys back
|
||||
*
|
||||
* @param array<mixed> $array multidemnsional array to flatten
|
||||
* @param array<mixed> $return recoursive pass on array of keys
|
||||
* @return array<mixed> flattened keys array
|
||||
*/
|
||||
public static function flattenArrayKey(array $array, array $return = []): array
|
||||
{
|
||||
foreach ($array as $key => $sub) {
|
||||
$return[] = $key;
|
||||
if (is_array($sub) && count($sub) > 0) {
|
||||
$return = self::flattenArrayKey($sub, $return);
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* as above will flatten an array, but in this case only the outmost
|
||||
* leave nodes, all other keyswill be skipped
|
||||
*
|
||||
* @param array<mixed> $array multidemnsional array to flatten
|
||||
* @return array<mixed> flattened keys array
|
||||
*/
|
||||
public static function flattenArrayKeyLeavesOnly(array $array): array
|
||||
{
|
||||
$return = [];
|
||||
array_walk_recursive(
|
||||
$array,
|
||||
function ($value, $key) use (&$return) {
|
||||
$return[] = $key;
|
||||
}
|
||||
);
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* searches for key -> value in an array tree and writes the value one level up
|
||||
* this will remove this leaf will all other values
|
||||
*
|
||||
* @param array<mixed> $array nested array
|
||||
* @param string|int $search key to find that has no sub leaf
|
||||
* and will be pushed up
|
||||
* @return array<mixed> modified, flattened array
|
||||
*/
|
||||
public static function arrayFlatForKey(array $array, $search): array
|
||||
{
|
||||
if (!is_array($array)) {
|
||||
$array = [];
|
||||
}
|
||||
foreach ($array as $key => $value) {
|
||||
// if it is not an array do just nothing
|
||||
if (!is_array($value)) {
|
||||
continue;
|
||||
}
|
||||
// probe it has search key
|
||||
if (isset($value[$search])) {
|
||||
// set as current
|
||||
$array[$key] = $value[$search];
|
||||
} else {
|
||||
// call up next node down
|
||||
// $array[$key] = call_user_func(__METHOD__, $value, $search);
|
||||
$array[$key] = self::arrayFlatForKey($value, $search);
|
||||
}
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
452
4dev/deprecated/CoreLibs/Combined/DateTime.php
Normal file
452
4dev/deprecated/CoreLibs/Combined/DateTime.php
Normal file
@@ -0,0 +1,452 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* date convert and check functions
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Combined;
|
||||
|
||||
use Exception;
|
||||
|
||||
class DateTime
|
||||
{
|
||||
/** @var array<int,string> */
|
||||
public const DAY_SHORT = [
|
||||
1 => 'Mon',
|
||||
2 => 'Tue',
|
||||
3 => 'Wed',
|
||||
4 => 'Thu',
|
||||
5 => 'Fri',
|
||||
6 => 'Sat',
|
||||
7 => 'Sun',
|
||||
];
|
||||
/** @var array<int,string> */
|
||||
public const DAY_LONG = [
|
||||
1 => 'Monday',
|
||||
2 => 'Tuesday',
|
||||
3 => 'Wednesday',
|
||||
4 => 'Thursday',
|
||||
5 => 'Friday',
|
||||
6 => 'Saturday',
|
||||
7 => 'Sunday',
|
||||
];
|
||||
/** @var array<int,string> */
|
||||
public const MONTH_LONG = [
|
||||
1 => 'January',
|
||||
2 => 'February',
|
||||
3 => 'March',
|
||||
4 => 'April',
|
||||
5 => 'May',
|
||||
6 => 'June',
|
||||
7 => 'July',
|
||||
8 => 'August',
|
||||
9 => 'September',
|
||||
10 => 'October',
|
||||
11 => 'November',
|
||||
12 => 'December',
|
||||
];
|
||||
/** @var array<int,string> */
|
||||
public const MONTH_SHORT = [
|
||||
1 => 'Jan',
|
||||
2 => 'Feb',
|
||||
3 => 'Mar',
|
||||
4 => 'Apr',
|
||||
5 => 'May',
|
||||
6 => 'Jun',
|
||||
7 => 'Jul',
|
||||
8 => 'Aug',
|
||||
9 => 'Sep',
|
||||
10 => 'Oct',
|
||||
11 => 'Nov',
|
||||
12 => 'Dec',
|
||||
];
|
||||
|
||||
/**
|
||||
* a simple wrapper for the date format
|
||||
* if an invalid timestamp is give zero timestamp unix time is used
|
||||
*
|
||||
* @param int|float $timestamp unix timestamp
|
||||
* @param bool $show_micro show the micro time (default false)
|
||||
* @param bool $micro_as_float Add the micro time with . instead
|
||||
* of ms (default false)
|
||||
* @return string formated date+time in Y-M-D h:m:s ms
|
||||
*/
|
||||
public static function dateStringFormat(
|
||||
$timestamp,
|
||||
bool $show_micro = false,
|
||||
bool $micro_as_float = false
|
||||
): string {
|
||||
// split up the timestamp, assume . in timestamp
|
||||
// array pad $ms if no microtime
|
||||
list ($timestamp, $ms) = array_pad(explode('.', (string)round($timestamp, 4)), 2, null);
|
||||
$string = date("Y-m-d H:i:s", (int)$timestamp);
|
||||
if ($show_micro && $ms) {
|
||||
if ($micro_as_float == false) {
|
||||
$string .= ' ' . $ms . 'ms';
|
||||
} else {
|
||||
$string .= '.' . $ms;
|
||||
}
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* formats a timestamp into interval, not into a date
|
||||
*
|
||||
* @param string|int|float $timestamp interval in seconds and optional
|
||||
* float micro seconds
|
||||
* @param bool $show_micro show micro seconds, default true
|
||||
* @return string interval formatted string or string as is
|
||||
*/
|
||||
public static function timeStringFormat($timestamp, bool $show_micro = true): string
|
||||
{
|
||||
// check if the timestamp has any h/m/s/ms inside, if yes skip
|
||||
if (!preg_match("/(h|m|s|ms)/", (string)$timestamp)) {
|
||||
list($timestamp, $ms) = array_pad(explode('.', (string)round((float)$timestamp, 4)), 2, null);
|
||||
// if negative remember
|
||||
$negative = false;
|
||||
if ((int)$timestamp < 0) {
|
||||
$negative = true;
|
||||
}
|
||||
$timestamp = abs((float)$timestamp);
|
||||
$timegroups = [86400, 3600, 60, 1];
|
||||
$labels = ['d', 'h', 'm', 's'];
|
||||
$time_string = '';
|
||||
// if timestamp is zero, return zero string
|
||||
if ($timestamp == 0) {
|
||||
$time_string = '0s';
|
||||
} else {
|
||||
for ($i = 0, $iMax = count($timegroups); $i < $iMax; $i++) {
|
||||
$output = floor((float)$timestamp / $timegroups[$i]);
|
||||
$timestamp = (float)$timestamp % $timegroups[$i];
|
||||
// output has days|hours|min|sec
|
||||
if ($output || $time_string) {
|
||||
$time_string .= $output . $labels[$i] . (($i + 1) != count($timegroups) ? ' ' : '');
|
||||
}
|
||||
}
|
||||
}
|
||||
// only add ms if we have an ms value
|
||||
if ($ms !== null) {
|
||||
// if we have ms and it has leading zeros, remove them, but only if it is nut just 0
|
||||
$ms = preg_replace("/^0+(\d+)$/", '${1}', $ms);
|
||||
if (!is_string($ms) || empty($ms)) {
|
||||
$ms = '0';
|
||||
}
|
||||
// add ms if there
|
||||
if ($show_micro) {
|
||||
$time_string .= ' ' . $ms . 'ms';
|
||||
} elseif (!$time_string) {
|
||||
$time_string .= $ms . 'ms';
|
||||
}
|
||||
}
|
||||
if ($negative) {
|
||||
$time_string = '-' . $time_string;
|
||||
}
|
||||
} else {
|
||||
$time_string = $timestamp;
|
||||
}
|
||||
return (string)$time_string;
|
||||
}
|
||||
|
||||
/**
|
||||
* does a reverse of the timeStringFormat and converts the string from
|
||||
* xd xh xm xs xms to a timestamp.microtime format
|
||||
*
|
||||
* @param string|int|float $timestring formatted interval
|
||||
* @return string|int|float converted float interval, or string as is
|
||||
*/
|
||||
public static function stringToTime($timestring)
|
||||
{
|
||||
$timestamp = 0;
|
||||
if (preg_match("/(d|h|m|s|ms)/", (string)$timestring)) {
|
||||
$timestring = (string)$timestring;
|
||||
// pos for preg match read + multiply factor
|
||||
$timegroups = [2 => 86400, 4 => 3600, 6 => 60, 8 => 1];
|
||||
$matches = [];
|
||||
// if start with -, strip and set negative
|
||||
$negative = false;
|
||||
if (preg_match("/^-/", $timestring)) {
|
||||
$negative = true;
|
||||
$timestring = substr($timestring, 1);
|
||||
}
|
||||
// preg match: 0: full string
|
||||
// 2, 4, 6, 8 are the to need values
|
||||
preg_match("/^((\d+)d ?)?((\d+)h ?)?((\d+)m ?)?((\d+)s ?)?((\d+)ms)?$/", $timestring, $matches);
|
||||
// multiply the returned matches and sum them up. the last one (ms) is added with .
|
||||
foreach ($timegroups as $i => $time_multiply) {
|
||||
if (isset($matches[$i]) && is_numeric($matches[$i])) {
|
||||
$timestamp += (float)$matches[$i] * $time_multiply;
|
||||
}
|
||||
}
|
||||
if (isset($matches[10]) && is_numeric($matches[10])) {
|
||||
$timestamp .= '.' . $matches[10];
|
||||
}
|
||||
if ($negative) {
|
||||
// cast to flaot so we can do a negative multiplication
|
||||
$timestamp = (float)$timestamp * -1;
|
||||
}
|
||||
return $timestamp;
|
||||
} else {
|
||||
return $timestring;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns long or short day of week name based on ISO day of week number
|
||||
* 1: Monday
|
||||
* ...
|
||||
* 7: Sunday
|
||||
*
|
||||
* @param int $isodow 1: Monday, 7: Sunday
|
||||
* @param bool $long Default false 'Mon', if true 'Monday'
|
||||
* @return string Day of week string either short 'Mon' or long 'Monday'
|
||||
*/
|
||||
public static function setWeekdayNameFromIsoDow(int $isodow, bool $long = false): string
|
||||
{
|
||||
// if not valid, set to invalid
|
||||
if ($isodow < 1 || $isodow > 7) {
|
||||
return $long ? 'Invalid' : 'Inv';
|
||||
}
|
||||
return date($long ? 'l' : 'D', strtotime("Sunday +{$isodow} days") ?: null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the day of week Name from date
|
||||
*
|
||||
* @param string $date Any valid date
|
||||
* @param bool $long Default false 'Mon', if true 'Monday'
|
||||
* @return string Day of week string either short 'Mon' or long 'Monday'
|
||||
*/
|
||||
public static function setWeekdayNameFromDate(string $date, bool $long = false): string
|
||||
{
|
||||
if (!self::checkDate($date)) {
|
||||
return $long ? 'Invalid' : 'Inv';
|
||||
}
|
||||
return date($long ? 'l' : 'D', strtotime($date) ?: null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the day of week Name from date
|
||||
*
|
||||
* @param string $date Any valid date
|
||||
* @return int ISO Weekday number 1: Monday, 7: Sunday, -1 for invalid date
|
||||
*/
|
||||
public static function setWeekdayNumberFromDate(string $date): int
|
||||
{
|
||||
if (!self::checkDate($date)) {
|
||||
return -1;
|
||||
}
|
||||
return (int)date('N', strtotime($date) ?: null);
|
||||
}
|
||||
|
||||
/**
|
||||
* splits & checks date, wrap around for check_date function
|
||||
*
|
||||
* @param string $date a date string in the format YYYY-MM-DD
|
||||
* @return bool true if valid date, false if date not valid
|
||||
*/
|
||||
public static function checkDate($date): bool
|
||||
{
|
||||
if (!$date) {
|
||||
return false;
|
||||
}
|
||||
list ($year, $month, $day) = array_pad(
|
||||
preg_split("/[\/-]/", $date) ?: [],
|
||||
3,
|
||||
null
|
||||
);
|
||||
if (!$year || !$month || !$day) {
|
||||
return false;
|
||||
}
|
||||
if (!checkdate((int)$month, (int)$day, (int)$year)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* splits & checks date, wrap around for check_date function
|
||||
*
|
||||
* @param string $datetime date (YYYY-MM-DD) + time (HH:MM:SS), SS can be dropped
|
||||
* @return bool true if valid date, false if date not valid
|
||||
*/
|
||||
public static function checkDateTime(string $datetime): bool
|
||||
{
|
||||
if (!$datetime) {
|
||||
return false;
|
||||
}
|
||||
// catch last overflow if sec has - in front
|
||||
list ($year, $month, $day, $hour, $min, $sec, $sec_overflow) = array_pad(
|
||||
preg_split("/[\/\- :]/", $datetime) ?: [],
|
||||
7,
|
||||
null
|
||||
);
|
||||
if (!$year || !$month || !$day) {
|
||||
return false;
|
||||
}
|
||||
if (!checkdate((int)$month, (int)$day, (int)$year)) {
|
||||
return false;
|
||||
}
|
||||
if (!is_numeric($hour) || !is_numeric($min)) {
|
||||
return false;
|
||||
}
|
||||
if (!empty($sec) && !is_numeric($sec)) {
|
||||
return false;
|
||||
}
|
||||
if (!empty($sec) && ($sec < 0 || $sec > 60)) {
|
||||
return false;
|
||||
};
|
||||
// in case we have - for seconds
|
||||
if (!empty($sec_overflow)) {
|
||||
return false;
|
||||
}
|
||||
if (
|
||||
($hour < 0 || $hour > 24) ||
|
||||
($min < 0 || $min > 60)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* compares two dates, tries to convert them via strtotime to timestamps
|
||||
* returns int/bool in:
|
||||
* -1 if the first date is smaller the last
|
||||
* 0 if both are equal
|
||||
* 1 if the first date is bigger than the last
|
||||
* false if date validation/conversion failed
|
||||
*
|
||||
* @param string $start_date start date string in YYYY-MM-DD
|
||||
* @param string $end_date end date string in YYYY-MM-DD
|
||||
* @return int|bool false on error
|
||||
* or int -1 (s<e)/0 (s=e)/1 (s>e) as difference
|
||||
*/
|
||||
public static function compareDate($start_date, $end_date)
|
||||
{
|
||||
// pre check for empty or wrong
|
||||
if ($start_date == '--' || $end_date == '--' || !$start_date || !$end_date) {
|
||||
return false;
|
||||
}
|
||||
// if invalid, quit
|
||||
if (($start_timestamp = strtotime($start_date)) === false) {
|
||||
return false;
|
||||
}
|
||||
if (($end_timestamp = strtotime($end_date)) === false) {
|
||||
return false;
|
||||
}
|
||||
// convert anything to Y-m-d and then to timestamp
|
||||
// this is to remove any time parts
|
||||
$start_timestamp = strtotime(date('Y-m-d', $start_timestamp));
|
||||
$end_timestamp = strtotime(date('Y-m-d', $end_timestamp));
|
||||
// compare, or end with false
|
||||
if ($start_timestamp < $end_timestamp) {
|
||||
return -1;
|
||||
} elseif ($start_timestamp == $end_timestamp) {
|
||||
return 0;
|
||||
} elseif ($start_timestamp > $end_timestamp) {
|
||||
return 1;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* compares the two dates + times. if seconds missing in one set,
|
||||
* adds :00, converts date + times via strtotime to timestamps
|
||||
* returns int/bool in:
|
||||
* -1 if the first date is smaller the last
|
||||
* 0 if both are equal
|
||||
* 1 if the first date is bigger than the last
|
||||
* false if date/times validation/conversion failed
|
||||
*
|
||||
* @param string $start_datetime start date/time in YYYY-MM-DD HH:mm:ss
|
||||
* @param string $end_datetime end date/time in YYYY-MM-DD HH:mm:ss
|
||||
* @return int|bool false for error
|
||||
* or -1 (s<e)/0 (s=e)/1 (s>e) as difference
|
||||
*/
|
||||
public static function compareDateTime($start_datetime, $end_datetime)
|
||||
{
|
||||
// pre check for empty or wrong
|
||||
if ($start_datetime == '--' || $end_datetime == '--' || !$start_datetime || !$end_datetime) {
|
||||
return false;
|
||||
}
|
||||
// quit if invalid timestamp
|
||||
if (($start_timestamp = strtotime($start_datetime)) === false) {
|
||||
return false;
|
||||
}
|
||||
if (($end_timestamp = strtotime($end_datetime)) === false) {
|
||||
return false;
|
||||
}
|
||||
// compare, or return false
|
||||
if ($start_timestamp < $end_timestamp) {
|
||||
return -1;
|
||||
} elseif ($start_timestamp == $end_timestamp) {
|
||||
return 0;
|
||||
} elseif ($start_timestamp > $end_timestamp) {
|
||||
return 1;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* calculates the days between two dates
|
||||
* return: overall days, week days, weekend days as array 0...2 or named
|
||||
* as overall, weekday and weekend
|
||||
*
|
||||
* @param string $start_date valid start date (y/m/d)
|
||||
* @param string $end_date valid end date (y/m/d)
|
||||
* @param bool $return_named return array type, false (default), true for named
|
||||
* @return array<mixed> 0/overall, 1/weekday, 2/weekend
|
||||
*/
|
||||
public static function calcDaysInterval($start_date, $end_date, bool $return_named = false): array
|
||||
{
|
||||
// pos 0 all, pos 1 weekday, pos 2 weekend
|
||||
$days = [];
|
||||
// if anything invalid, return 0,0,0
|
||||
try {
|
||||
$start = new \DateTime($start_date);
|
||||
$end = new \DateTime($end_date);
|
||||
} catch (Exception $e) {
|
||||
if ($return_named === true) {
|
||||
return [
|
||||
'overall' => 0,
|
||||
'weekday' => 0,
|
||||
'weekend' => 0,
|
||||
];
|
||||
} else {
|
||||
return [0, 0, 0];
|
||||
}
|
||||
}
|
||||
// so we include the last day too, we need to add +1 second in the time
|
||||
$end->setTime(0, 0, 1);
|
||||
// if end date before start date, only this will be filled
|
||||
$days[0] = $end->diff($start)->days;
|
||||
$days[1] = 0;
|
||||
$days[2] = 0;
|
||||
// get period for weekends/weekdays
|
||||
$period = new \DatePeriod($start, new \DateInterval('P1D'), $end);
|
||||
foreach ($period as $dt) {
|
||||
$curr = $dt->format('D');
|
||||
if ($curr == 'Sat' || $curr == 'Sun') {
|
||||
$days[2] ++;
|
||||
} else {
|
||||
$days[1] ++;
|
||||
}
|
||||
}
|
||||
if ($return_named === true) {
|
||||
return [
|
||||
'overall' => $days[0],
|
||||
'weekday' => $days[1],
|
||||
'weekend' => $days[2],
|
||||
];
|
||||
} else {
|
||||
return $days;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
167
4dev/deprecated/CoreLibs/Convert/Byte.php
Normal file
167
4dev/deprecated/CoreLibs/Convert/Byte.php
Normal file
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* byte conversion from and to human readable
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert;
|
||||
|
||||
class Byte
|
||||
{
|
||||
// define byteFormat
|
||||
public const BYTE_FORMAT_NOSPACE = 1;
|
||||
public const BYTE_FORMAT_ADJUST = 2;
|
||||
public const BYTE_FORMAT_SI = 4;
|
||||
|
||||
/**
|
||||
* This function replaces the old byteStringFormat
|
||||
*
|
||||
* Converts any number string to human readable byte format
|
||||
* Maxium is Exobytes and above that the Exobytes suffix is used for all
|
||||
* If more are needed only the correct short name for the suffix has to be
|
||||
* added to the labels array
|
||||
* On no number string it returns string as is
|
||||
* Source Idea: SOURCE: https://programming.guide/worlds-most-copied-so-snippet.html
|
||||
*
|
||||
* The class itself hast the following defined
|
||||
* BYTE_FORMAT_NOSPACE [1] turn off spaces between number and suffix
|
||||
* BYTE_FORMAT_ADJUST [2] use sprintf to always print two decimals
|
||||
* BYTE_FORMAT_SI [4] use si standard 1000 instead of bytes 1024
|
||||
* To use the constant from outside use class::CONSTANT
|
||||
*
|
||||
* @param string|int|float $bytes bytes as string int or pure int
|
||||
* @param int $flags bitwise flag with use space turned on
|
||||
* BYTE_FORMAT_NOSPACE: no space between number and suffix
|
||||
* BYTE_FORMAT_ADJUST: sprintf adjusted two 2 decimals
|
||||
* BYTE_FORMAT_SI: use 1000 instead of 1024
|
||||
* @return string converted byte number (float) with suffix
|
||||
* @throws \Exception 1: no valid flag set
|
||||
*/
|
||||
public static function humanReadableByteFormat($bytes, int $flags = 0): string
|
||||
{
|
||||
// if not numeric, return as is
|
||||
if (is_numeric($bytes)) {
|
||||
// flags bit wise check
|
||||
// remove space between number and suffix
|
||||
if ($flags & self::BYTE_FORMAT_NOSPACE) {
|
||||
$space = false;
|
||||
} else {
|
||||
$space = true;
|
||||
}
|
||||
// use sprintf instead of round
|
||||
if ($flags & self::BYTE_FORMAT_ADJUST) {
|
||||
$adjust = true;
|
||||
} else {
|
||||
$adjust = false;
|
||||
}
|
||||
// use SI 1000 mod and not 1024 mod
|
||||
if ($flags & self::BYTE_FORMAT_SI) {
|
||||
$si = true;
|
||||
} else {
|
||||
$si = false;
|
||||
}
|
||||
if ($flags > 7) {
|
||||
throw new \Exception("Invalid flags parameter: $flags", 1);
|
||||
}
|
||||
|
||||
// si or normal
|
||||
$unit = $si ? 1000 : 1024;
|
||||
// always positive
|
||||
$abs_bytes = $bytes == PHP_INT_MIN ? PHP_INT_MAX : abs((float)$bytes);
|
||||
// smaller than unit is always B
|
||||
if ($abs_bytes < $unit) {
|
||||
return $bytes . 'B';
|
||||
}
|
||||
// labels in order of size [Y, Z]
|
||||
$labels = ['', 'K', 'M', 'G', 'T', 'P', 'E'];
|
||||
// exp position calculation
|
||||
$exp = floor(log($abs_bytes, $unit));
|
||||
// avoid printing out anything larger than max labels
|
||||
if ($exp >= count($labels)) {
|
||||
$exp = count($labels) - 1;
|
||||
}
|
||||
// deviation calculation
|
||||
$dev = pow($unit, $exp) * ($unit - 0.05);
|
||||
// shift the exp +1 for on the border units
|
||||
if (
|
||||
$exp < 6 &&
|
||||
$abs_bytes > ($dev - (((int)$dev & 0xfff) == 0xd00 ? 52 : 0))
|
||||
) {
|
||||
$exp++;
|
||||
}
|
||||
// label name, including leading space if flagged
|
||||
$pre = ($space ? ' ' : '') . ($labels[$exp] ?? '>E') . ($si ? 'i' : '') . 'B';
|
||||
$bytes_calc = $abs_bytes / pow($unit, $exp);
|
||||
// if original is negative, reverse
|
||||
if ($bytes < 0) {
|
||||
$bytes_calc *= -1;
|
||||
}
|
||||
if ($adjust) {
|
||||
return sprintf("%.2f%s", $bytes_calc, $pre);
|
||||
} else {
|
||||
return round($bytes_calc, 2) . $pre;
|
||||
}
|
||||
} else {
|
||||
// if anything other return as string
|
||||
return (string)$bytes;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* calculates the bytes based on a string with nnG, nnGB, nnM, etc
|
||||
* NOTE: large exabyte numbers will overflow
|
||||
* flag allowed:
|
||||
* BYTE_FORMAT_SI [4] use si standard 1000 instead of bytes 1024
|
||||
*
|
||||
* @param string|int|float $number any string or number to convert
|
||||
* @param int $flags bitwise flag with use space turned on
|
||||
* BYTE_FORMAT_SI: use 1000 instead of 1024
|
||||
* @return string|int|float converted value or original value
|
||||
* @throws \Exception 1: no valid flag set
|
||||
*/
|
||||
public static function stringByteFormat($number, int $flags = 0)
|
||||
{
|
||||
// use SI 1000 mod and not 1024 mod
|
||||
if ($flags & self::BYTE_FORMAT_SI) {
|
||||
$si = true;
|
||||
} else {
|
||||
$si = false;
|
||||
}
|
||||
if ($flags != 0 && $flags != 4) {
|
||||
throw new \Exception("Invalid flags parameter: $flags", 1);
|
||||
}
|
||||
// matches in regex
|
||||
$matches = [];
|
||||
// all valid units
|
||||
$valid_units_ = 'bkmgtpezy';
|
||||
// detects up to exo bytes
|
||||
preg_match(
|
||||
"/(-)?([\d.,]*)\s?(eib|pib|tib|gib|mib|kib|eb|pb|tb|gb|mb|kb|e|p|t|g|m|k|b)$/i",
|
||||
strtolower((string)$number),
|
||||
$matches
|
||||
);
|
||||
if (isset($matches[2]) && isset($matches[3])) {
|
||||
// remove all non valid characters from the number
|
||||
$number = preg_replace('/[^0-9\.]/', '', $matches[2]);
|
||||
// final clean up and convert to float
|
||||
$number = (float)trim((string)$number);
|
||||
// convert any mb/gb/etc to single m/b
|
||||
$unit = preg_replace('/[^bkmgtpezy]/i', '', $matches[3]);
|
||||
if ($unit) {
|
||||
$number = $number * pow($si ? 1000 : 1024, stripos($valid_units_, $unit[0]) ?: 0);
|
||||
}
|
||||
// convert to INT to avoid +E output
|
||||
$number = (int)round($number);
|
||||
// if negative input, keep nnegative
|
||||
if (!empty($matches[1])) {
|
||||
$number *= -1;
|
||||
}
|
||||
}
|
||||
// if not matching return as is
|
||||
return $number;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
344
4dev/deprecated/CoreLibs/Convert/Colors.php
Normal file
344
4dev/deprecated/CoreLibs/Convert/Colors.php
Normal file
@@ -0,0 +1,344 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Convert color spaces
|
||||
* rgb to hex
|
||||
* hex to rgb
|
||||
* rgb to hsb
|
||||
* hsb to rgb
|
||||
* rgb to hsl
|
||||
* hsl to rgb
|
||||
*/
|
||||
|
||||
// TODO: use oklab as base for converting colors
|
||||
// https://bottosson.github.io/posts/oklab/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert;
|
||||
|
||||
class Colors
|
||||
{
|
||||
/**
|
||||
* converts the rgb values from int data to the valid rgb html hex string
|
||||
* optional can turn of leading #
|
||||
* if one value is invalid, will return false
|
||||
*
|
||||
* @param int $red red 0-255
|
||||
* @param int $green green 0-255
|
||||
* @param int $blue blue 0-255
|
||||
* @param bool $hex_prefix default true, prefix with "#"
|
||||
* @return string|bool rgb in hex values with leading # if set,
|
||||
* false for invalid color
|
||||
*/
|
||||
public static function rgb2hex(int $red, int $green, int $blue, bool $hex_prefix = true)
|
||||
{
|
||||
$hex_color = '';
|
||||
if ($hex_prefix === true) {
|
||||
$hex_color = '#';
|
||||
}
|
||||
foreach (['red', 'green', 'blue'] as $color) {
|
||||
// if not valid, abort
|
||||
if ($$color < 0 || $$color > 255) {
|
||||
return false;
|
||||
}
|
||||
// pad left with 0
|
||||
$hex_color .= str_pad(dechex($$color), 2, '0', STR_PAD_LEFT);
|
||||
}
|
||||
return $hex_color;
|
||||
}
|
||||
|
||||
/**
|
||||
* converts a hex RGB color to the int numbers
|
||||
*
|
||||
* @param string $hexStr RGB hexstring
|
||||
* @param bool $return_as_string flag to return as string
|
||||
* @param string $seperator string seperator: default: ","
|
||||
* @return string|array<string,float|int>|bool false on error or array with RGB
|
||||
* or a string with the seperator
|
||||
*/
|
||||
public static function hex2rgb(
|
||||
string $hexStr,
|
||||
bool $return_as_string = false,
|
||||
string $seperator = ','
|
||||
) {
|
||||
$hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string
|
||||
if (!is_string($hexStr)) {
|
||||
return false;
|
||||
}
|
||||
$rgbArray = [];
|
||||
if (strlen($hexStr) == 6) {
|
||||
// If a proper hex code, convert using bitwise operation.
|
||||
// No overhead... faster
|
||||
$colorVal = hexdec($hexStr);
|
||||
$rgbArray['r'] = 0xFF & ($colorVal >> 0x10);
|
||||
$rgbArray['g'] = 0xFF & ($colorVal >> 0x8);
|
||||
$rgbArray['b'] = 0xFF & $colorVal;
|
||||
} elseif (strlen($hexStr) == 3) {
|
||||
// If shorthand notation, need some string manipulations
|
||||
$rgbArray['r'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
|
||||
$rgbArray['g'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
|
||||
$rgbArray['b'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
|
||||
} else {
|
||||
// Invalid hex color code
|
||||
return false;
|
||||
}
|
||||
// returns the rgb string or the associative array
|
||||
return $return_as_string ? implode($seperator, $rgbArray) : $rgbArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* rgb2hsb does not clean convert back to rgb in a round trip
|
||||
* converts RGB to HSB/V values
|
||||
* returns:
|
||||
* array with hue (0-360), sat (0-100%), brightness/value (0-100%)
|
||||
*
|
||||
* @param int $red red 0-255
|
||||
* @param int $green green 0-255
|
||||
* @param int $blue blue 0-255
|
||||
* @return array<int|float>|bool Hue, Sat, Brightness/Value
|
||||
* false for input value error
|
||||
*/
|
||||
public static function rgb2hsb(int $red, int $green, int $blue)
|
||||
{
|
||||
// check that rgb is from 0 to 255
|
||||
foreach (['red', 'green', 'blue'] as $c) {
|
||||
if ($$c < 0 || $$c > 255) {
|
||||
return false;
|
||||
}
|
||||
$$c = $$c / 255;
|
||||
}
|
||||
|
||||
$MAX = max($red, $green, $blue);
|
||||
$MIN = min($red, $green, $blue);
|
||||
$HUE = 0;
|
||||
|
||||
if ($MAX == $MIN) {
|
||||
return [0, 0, round($MAX * 100)];
|
||||
}
|
||||
if ($red == $MAX) {
|
||||
$HUE = ($green - $blue) / ($MAX - $MIN);
|
||||
} elseif ($green == $MAX) {
|
||||
$HUE = 2 + (($blue - $red) / ($MAX - $MIN));
|
||||
} elseif ($blue == $MAX) {
|
||||
$HUE = 4 + (($red - $green) / ($MAX - $MIN));
|
||||
}
|
||||
$HUE *= 60;
|
||||
if ($HUE < 0) {
|
||||
$HUE += 360;
|
||||
}
|
||||
|
||||
return [
|
||||
(int)round($HUE),
|
||||
(int)round((($MAX - $MIN) / $MAX) * 100),
|
||||
(int)round($MAX * 100)
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* hsb2rgb does not clean convert back to hsb in a round trip
|
||||
* converts HSB/V to RGB values RGB is full INT
|
||||
* if HSB/V value is invalid, sets this value to 0
|
||||
*
|
||||
* @param float $H hue 0-360 (int)
|
||||
* @param float $S saturation 0-100 (int)
|
||||
* @param float $V brightness/value 0-100 (int)
|
||||
* @return array<int>|bool 0 red/1 green/2 blue array as 0-255
|
||||
* false for input value error
|
||||
*/
|
||||
public static function hsb2rgb(float $H, float $S, float $V)
|
||||
{
|
||||
// check that H is 0 to 359, 360 = 0
|
||||
// and S and V are 0 to 1
|
||||
if ($H == 360) {
|
||||
$H = 0;
|
||||
}
|
||||
if ($H < 0 || $H > 359) {
|
||||
return false;
|
||||
}
|
||||
if ($S < 0 || $S > 100) {
|
||||
return false;
|
||||
}
|
||||
if ($V < 0 || $V > 100) {
|
||||
return false;
|
||||
}
|
||||
// convert to internal 0-1 format
|
||||
$S /= 100;
|
||||
$V /= 100;
|
||||
|
||||
if ($S == 0) {
|
||||
$V = (int)round($V * 255);
|
||||
return [$V, $V, $V];
|
||||
}
|
||||
|
||||
$Hi = floor($H / 60);
|
||||
$f = ($H / 60) - $Hi;
|
||||
$p = $V * (1 - $S);
|
||||
$q = $V * (1 - ($S * $f));
|
||||
$t = $V * (1 - ($S * (1 - $f)));
|
||||
|
||||
switch ($Hi) {
|
||||
case 0:
|
||||
$red = $V;
|
||||
$green = $t;
|
||||
$blue = $p;
|
||||
break;
|
||||
case 1:
|
||||
$red = $q;
|
||||
$green = $V;
|
||||
$blue = $p;
|
||||
break;
|
||||
case 2:
|
||||
$red = $p;
|
||||
$green = $V;
|
||||
$blue = $t;
|
||||
break;
|
||||
case 3:
|
||||
$red = $p;
|
||||
$green = $q;
|
||||
$blue = $V;
|
||||
break;
|
||||
case 4:
|
||||
$red = $t;
|
||||
$green = $p;
|
||||
$blue = $V;
|
||||
break;
|
||||
case 5:
|
||||
$red = $V;
|
||||
$green = $p;
|
||||
$blue = $q;
|
||||
break;
|
||||
default:
|
||||
$red = 0;
|
||||
$green = 0;
|
||||
$blue = 0;
|
||||
}
|
||||
|
||||
return [
|
||||
(int)round($red * 255),
|
||||
(int)round($green * 255),
|
||||
(int)round($blue * 255)
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* converts a RGB (0-255) to HSL
|
||||
* return:
|
||||
* array with hue (0-360), saturation (0-100%) and luminance (0-100%)
|
||||
*
|
||||
* @param int $red red 0-255
|
||||
* @param int $green green 0-255
|
||||
* @param int $blue blue 0-255
|
||||
* @return array<float>|bool hue/sat/luminance
|
||||
* false for input value error
|
||||
*/
|
||||
public static function rgb2hsl(int $red, int $green, int $blue)
|
||||
{
|
||||
// check that rgb is from 0 to 255
|
||||
foreach (['red', 'green', 'blue'] as $c) {
|
||||
if ($$c < 0 || $$c > 255) {
|
||||
return false;
|
||||
}
|
||||
$$c = $$c / 255;
|
||||
}
|
||||
|
||||
$min = min($red, $green, $blue);
|
||||
$max = max($red, $green, $blue);
|
||||
$chroma = $max - $min;
|
||||
$sat = 0;
|
||||
$hue = 0;
|
||||
// luminance
|
||||
$lum = ($max + $min) / 2;
|
||||
|
||||
// achromatic
|
||||
if ($chroma == 0) {
|
||||
// H, S, L
|
||||
return [0.0, 0.0, round($lum * 100, 1)];
|
||||
} else {
|
||||
$sat = $chroma / (1 - abs(2 * $lum - 1));
|
||||
if ($max == $red) {
|
||||
$hue = fmod((($green - $blue) / $chroma), 6);
|
||||
if ($hue < 0) {
|
||||
$hue = (6 - fmod(abs($hue), 6));
|
||||
}
|
||||
} elseif ($max == $green) {
|
||||
$hue = ($blue - $red) / $chroma + 2;
|
||||
} elseif ($max == $blue) {
|
||||
$hue = ($red - $green) / $chroma + 4;
|
||||
}
|
||||
$hue = $hue * 60;
|
||||
// $sat = 1 - abs(2 * $lum - 1);
|
||||
return [
|
||||
round($hue, 1),
|
||||
round($sat * 100, 1),
|
||||
round($lum * 100, 1)
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* converts an HSL to RGB
|
||||
* if HSL value is invalid, set this value to 0
|
||||
*
|
||||
* @param float $hue hue: 0-360 (degrees)
|
||||
* @param float $sat saturation: 0-100
|
||||
* @param float $lum luminance: 0-100
|
||||
* @return array<int,float|int>|bool red/blue/green 0-255 each
|
||||
*/
|
||||
public static function hsl2rgb(float $hue, float $sat, float $lum)
|
||||
{
|
||||
if (!is_numeric($hue)) {
|
||||
return false;
|
||||
}
|
||||
if ($hue == 360) {
|
||||
$hue = 0;
|
||||
}
|
||||
if ($hue < 0 || $hue > 359) {
|
||||
return false;
|
||||
}
|
||||
if ($sat < 0 || $sat > 100) {
|
||||
return false;
|
||||
}
|
||||
if ($lum < 0 || $lum > 100) {
|
||||
return false;
|
||||
}
|
||||
// calc to internal convert value for hue
|
||||
$hue = (1 / 360) * $hue;
|
||||
// convert to internal 0-1 format
|
||||
$sat /= 100;
|
||||
$lum /= 100;
|
||||
// if saturation is 0
|
||||
if ($sat == 0) {
|
||||
$lum = (int)round($lum * 255);
|
||||
return [$lum, $lum, $lum];
|
||||
} else {
|
||||
$m2 = $lum < 0.5 ? $lum * ($sat + 1) : ($lum + $sat) - ($lum * $sat);
|
||||
$m1 = $lum * 2 - $m2;
|
||||
$hueue = function ($base) use ($m1, $m2) {
|
||||
// base = hue, hue > 360 (1) - 360 (1), else < 0 + 360 (1)
|
||||
$base = $base < 0 ? $base + 1 : ($base > 1 ? $base - 1 : $base);
|
||||
// 6: 60, 2: 180, 3: 240
|
||||
// 2/3 = 240
|
||||
// 1/3 = 120 (all from 360)
|
||||
if ($base * 6 < 1) {
|
||||
return $m1 + ($m2 - $m1) * $base * 6;
|
||||
}
|
||||
if ($base * 2 < 1) {
|
||||
return $m2;
|
||||
}
|
||||
if ($base * 3 < 2) {
|
||||
return $m1 + ($m2 - $m1) * ((2 / 3) - $base) * 6;
|
||||
}
|
||||
return $m1;
|
||||
};
|
||||
|
||||
return [
|
||||
(int)round(255 * $hueue($hue + (1 / 3))),
|
||||
(int)round(255 * $hueue($hue)),
|
||||
(int)round(255 * $hueue($hue - (1 / 3)))
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
256
4dev/deprecated/CoreLibs/Convert/Extends/VarSetTypeMain.php
Normal file
256
4dev/deprecated/CoreLibs/Convert/Extends/VarSetTypeMain.php
Normal file
@@ -0,0 +1,256 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Run is_<type> checks and return default value if not this type
|
||||
* This will return default null on invalid entries
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert\Extends;
|
||||
|
||||
class VarSetTypeMain
|
||||
{
|
||||
/**
|
||||
* If input variable is string then returns it, else returns default set
|
||||
* if not null is true, then null as return is allowed, else return is
|
||||
* converted to string
|
||||
*
|
||||
* @param mixed $val Input variable
|
||||
* @param string|null $default Default value
|
||||
* @param bool $to_null Convert to null (default no)
|
||||
* @return string|null Input var or default value
|
||||
*/
|
||||
protected static function setStrMain(
|
||||
mixed $val,
|
||||
?string $default = null,
|
||||
bool $to_null = false
|
||||
): ?string {
|
||||
if (is_string($val)) {
|
||||
return $val;
|
||||
}
|
||||
if ($to_null === false) {
|
||||
return (string)$default;
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Will convert input data to string if possible.
|
||||
* Runs for string/int/float/bool/null
|
||||
* Will skip array/object/resource/callable/etc and use default for that
|
||||
*
|
||||
* @param mixed $val Input variable
|
||||
* @param string|null $default Default value
|
||||
* @param bool $to_null Convert to null (default no)
|
||||
* @return string|null Converted input data to string/null
|
||||
*/
|
||||
protected static function makeStrMain(
|
||||
mixed $val,
|
||||
string $default = null,
|
||||
bool $to_null = false
|
||||
): ?string {
|
||||
// int/float/string/bool/null, everything else is ignored
|
||||
// no: array/object/resource/callable
|
||||
if (
|
||||
is_int($val) ||
|
||||
is_float($val) ||
|
||||
is_string($val) ||
|
||||
is_bool($val) ||
|
||||
is_null($val)
|
||||
) {
|
||||
return (string)$val;
|
||||
}
|
||||
if ($to_null === false) {
|
||||
return (string)$default;
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* If input variable is int, return it, else return default value. If to_null
|
||||
* is true then null as return is allowed, else only int is returned
|
||||
*
|
||||
* @param mixed $val Input variable
|
||||
* @param int|null $default Default value
|
||||
* @param bool $to_null Convert to null (default no)
|
||||
* @return int|null Input var or default value
|
||||
*/
|
||||
protected static function setIntMain(
|
||||
mixed $val,
|
||||
?int $default = null,
|
||||
bool $to_null = false
|
||||
): ?int {
|
||||
if (is_int($val)) {
|
||||
return $val;
|
||||
}
|
||||
if ($to_null === false) {
|
||||
return (int)$default;
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert input to int via filter_var. If not convertable return default value.
|
||||
* If to_null is set to true null return is allowed
|
||||
* NOTE: this is only a drastic fallback and not recommned for special use.
|
||||
* It will try to check via filter_var if we can get an int value and then use
|
||||
* intval to convert it.
|
||||
* Reason is that filter_var will convert eg 1.5 to 15 instead 1
|
||||
* One is very wrong, the other is at least better, but not perfect
|
||||
*
|
||||
* @param mixed $val Input variable
|
||||
* @param int|null $default Default value
|
||||
* @param bool $to_null Convert to null (default no)
|
||||
* @return int|null Converted input data to int/null
|
||||
*/
|
||||
protected static function makeIntMain(
|
||||
mixed $val,
|
||||
int $default = null,
|
||||
bool $to_null = false
|
||||
): ?int {
|
||||
// if we can filter it to a valid int, we can convert it
|
||||
// we so avoid object, array, etc
|
||||
if (
|
||||
filter_var(
|
||||
$val,
|
||||
FILTER_SANITIZE_NUMBER_INT
|
||||
) !== false
|
||||
) {
|
||||
return intval($val);
|
||||
}
|
||||
if ($to_null === false) {
|
||||
return (int)$default;
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* If input is float return it, else set to default value. If to_null is set
|
||||
* to true, allow null return
|
||||
*
|
||||
* @param mixed $val Input variable
|
||||
* @param float|null $default Default value
|
||||
* @param bool $to_null Convert to null (default no)
|
||||
* @return float|null Input var or default value
|
||||
*/
|
||||
protected static function setFloatMain(
|
||||
mixed $val,
|
||||
?float $default = null,
|
||||
bool $to_null = false
|
||||
): ?float {
|
||||
if (is_float($val)) {
|
||||
return $val;
|
||||
}
|
||||
if ($to_null === false) {
|
||||
return (float)$default;
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert intput var to float via filter_var. If failed to so return default.
|
||||
* If to_null is set to true allow null return
|
||||
*
|
||||
* @param mixed $val Input variable
|
||||
* @param float|null $default Default value
|
||||
* @param bool $to_null Convert to null (default no)
|
||||
* @return float|null Converted intput data to float/null
|
||||
*/
|
||||
protected static function makeFloatMain(
|
||||
mixed $val,
|
||||
float $default = null,
|
||||
bool $to_null = false
|
||||
): ?float {
|
||||
if (
|
||||
(
|
||||
$val = filter_var(
|
||||
$val,
|
||||
FILTER_SANITIZE_NUMBER_FLOAT,
|
||||
FILTER_FLAG_ALLOW_FRACTION
|
||||
)
|
||||
) !== false
|
||||
) {
|
||||
return (float)$val;
|
||||
}
|
||||
if ($to_null === false) {
|
||||
return (float)$default;
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* If input var is array return it, else return default value. If to_null is
|
||||
* set to true, allow null return
|
||||
*
|
||||
* @param mixed $val Input variable
|
||||
* @param array<mixed>|null $default Default value
|
||||
* @param bool $to_null Convert to null (default no)
|
||||
* @return array<mixed>|null Input var or default value
|
||||
*/
|
||||
protected static function setArrayMain(
|
||||
mixed $val,
|
||||
?array $default = null,
|
||||
bool $to_null = false
|
||||
): ?array {
|
||||
if (is_array($val)) {
|
||||
return $val;
|
||||
}
|
||||
if ($to_null === false) {
|
||||
return (array)$default;
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* If input var is bool return it, else return default value. If to_null is
|
||||
* set to true will allow null return.
|
||||
*
|
||||
* @param mixed $val Input variable
|
||||
* @param bool|null $default Default value
|
||||
* @param bool $to_null Convert to null (default no)
|
||||
* @return bool|null Input var or default value
|
||||
*/
|
||||
protected static function setBoolMain(
|
||||
mixed $val,
|
||||
?bool $default = null,
|
||||
bool $to_null = false
|
||||
): ?bool {
|
||||
if (is_bool($val)) {
|
||||
return $val;
|
||||
}
|
||||
if ($to_null === false) {
|
||||
return (bool)$default;
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert anything to bool. If it is a string it will try to use the filter_var
|
||||
* to convert know true/false strings.
|
||||
* Else it uses (bool) to convert the rest
|
||||
* If null is allowed, will return null
|
||||
*
|
||||
* @param mixed $val Input variable
|
||||
* @param bool $default Default value if to_null if false
|
||||
* @param bool $to_null Convert to null (default no)
|
||||
* @return bool|null Converted input data to bool/ null
|
||||
*/
|
||||
protected static function makeBoolMain(
|
||||
mixed $val,
|
||||
bool $default = false,
|
||||
bool $to_null = false
|
||||
): ?bool {
|
||||
$boolvar = is_string($val) ?
|
||||
filter_var(
|
||||
$val,
|
||||
FILTER_VALIDATE_BOOLEAN,
|
||||
FILTER_NULL_ON_FAILURE
|
||||
) :
|
||||
(bool)$val;
|
||||
return $boolvar === null && !$to_null ? $default : $boolvar;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
70
4dev/deprecated/CoreLibs/Convert/Html.php
Normal file
70
4dev/deprecated/CoreLibs/Convert/Html.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* html convert functions
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert;
|
||||
|
||||
class Html
|
||||
{
|
||||
public const SELECTED = 0;
|
||||
public const CHECKED = 1;
|
||||
|
||||
/**
|
||||
* full wrapper for html entities
|
||||
*
|
||||
* @param mixed $string string to html encode
|
||||
* @return mixed if string, encoded, else as is (eg null)
|
||||
*/
|
||||
public static function htmlent($string)
|
||||
{
|
||||
if (is_string($string)) {
|
||||
return htmlentities($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);
|
||||
} else {
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* strips out all line breaks or replaced with given string
|
||||
* @param string $string string
|
||||
* @param string $replace replace character, default ' '
|
||||
* @return string cleaned string without any line breaks
|
||||
*/
|
||||
public static function removeLB(string $string, string $replace = ' '): string
|
||||
{
|
||||
return str_replace(["\n\r", "\r", "\n"], $replace, $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* returns 'checked' or 'selected' if okay
|
||||
* $needle is a var, $haystack an array or a string
|
||||
* **** THE RETURN: VALUE WILL CHANGE TO A DEFAULT NULL IF NOT FOUND ****
|
||||
*
|
||||
* @param array<mixed>|string $haystack (search in) haystack can be
|
||||
* an array or a string
|
||||
* @param string $needle needle (search for)
|
||||
* @param int $type type: 0: returns selected, 1,
|
||||
* returns checked
|
||||
* @return ?string returns checked or selected,
|
||||
* else returns null
|
||||
*/
|
||||
public static function checked($haystack, string $needle, int $type = 0): ?string
|
||||
{
|
||||
if (is_array($haystack)) {
|
||||
if (in_array($needle, $haystack)) {
|
||||
return $type ? 'checked' : 'selected';
|
||||
}
|
||||
} else {
|
||||
if ($haystack == $needle) {
|
||||
return $type ? 'checked' : 'selected';
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
97
4dev/deprecated/CoreLibs/Convert/Json.php
Normal file
97
4dev/deprecated/CoreLibs/Convert/Json.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Converts a json string to array and stores error for later checking
|
||||
* can also return empty array on demand
|
||||
* and self set json as is on error as array
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert;
|
||||
|
||||
class Json
|
||||
{
|
||||
/** @var int */
|
||||
private static $json_last_error;
|
||||
|
||||
/**
|
||||
* converts a json string to an array
|
||||
* or inits an empty array on null string
|
||||
* or failed convert to array
|
||||
* In ANY case it will ALWAYS return array.
|
||||
* Does not throw errors
|
||||
*
|
||||
* @param string|null $json a json string, or null data
|
||||
* @param bool $override if set to true, then on json error
|
||||
* set original value as array
|
||||
* @return array<mixed> returns an array from the json values
|
||||
*/
|
||||
public static function jsonConvertToArray(?string $json, bool $override = false): array
|
||||
{
|
||||
if ($json !== null) {
|
||||
$_json = json_decode($json, true);
|
||||
if (self::$json_last_error = json_last_error()) {
|
||||
if ($override == true) {
|
||||
// init return as array with original as element
|
||||
$json = [$json];
|
||||
} else {
|
||||
$json = [];
|
||||
}
|
||||
} else {
|
||||
$json = $_json;
|
||||
}
|
||||
} else {
|
||||
$json = [];
|
||||
}
|
||||
// be sure that we return an array
|
||||
return (array)$json;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns human readable string for json errors thrown in jsonConvertToArray
|
||||
*
|
||||
* @param bool|boolean $return_string [default=false] if set to true
|
||||
* it will return the message string and not
|
||||
* the error number
|
||||
* @return int|string Either error number (0 for no error)
|
||||
* or error string ('' for no error)
|
||||
*/
|
||||
public static function jsonGetLastError(bool $return_string = false)
|
||||
{
|
||||
$json_error_string = '';
|
||||
// valid errors as of php 8.0
|
||||
switch (self::$json_last_error) {
|
||||
case JSON_ERROR_NONE:
|
||||
$json_error_string = '';
|
||||
break;
|
||||
case JSON_ERROR_DEPTH:
|
||||
$json_error_string = 'Maximum stack depth exceeded';
|
||||
break;
|
||||
case JSON_ERROR_STATE_MISMATCH:
|
||||
$json_error_string = 'Underflow or the modes mismatch';
|
||||
break;
|
||||
case JSON_ERROR_CTRL_CHAR:
|
||||
$json_error_string = 'Unexpected control character found';
|
||||
break;
|
||||
case JSON_ERROR_SYNTAX:
|
||||
$json_error_string = 'Syntax error, malformed JSON';
|
||||
break;
|
||||
case JSON_ERROR_UTF8:
|
||||
$json_error_string = 'Malformed UTF-8 characters, possibly incorrectly encoded';
|
||||
break;
|
||||
case JSON_ERROR_INVALID_PROPERTY_NAME:
|
||||
$json_error_string = 'A key starting with \u0000 character was in the string';
|
||||
break;
|
||||
case JSON_ERROR_UTF16:
|
||||
$json_error_string = 'Single unpaired UTF-16 surrogate in unicode escape';
|
||||
break;
|
||||
default:
|
||||
$json_error_string = 'Unknown error';
|
||||
break;
|
||||
}
|
||||
return $return_string === true ? $json_error_string : self::$json_last_error;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
61
4dev/deprecated/CoreLibs/Convert/Math.php
Normal file
61
4dev/deprecated/CoreLibs/Convert/Math.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* various math related function wrappers
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert;
|
||||
|
||||
class Math
|
||||
{
|
||||
/**
|
||||
* some float numbers will be rounded up even if they have no decimal entries
|
||||
* this function fixes this by pre-rounding before calling ceil
|
||||
*
|
||||
* @param float $number number to round
|
||||
* @param int|integer $precision intermediat round up decimals (default 10)
|
||||
* @return float correct ceil number
|
||||
*/
|
||||
public static function fceil(float $number, int $precision = 10): float
|
||||
{
|
||||
return ceil(round($number, $precision));
|
||||
}
|
||||
|
||||
/**
|
||||
* round inside an a number, not the decimal part only
|
||||
* eg 48767 with -2 -> 48700
|
||||
*
|
||||
* @param float $number number to round
|
||||
* @param int $precision negative number for position in number (default -2)
|
||||
* @return float rounded number
|
||||
*/
|
||||
public static function floorp(float $number, int $precision = -2): float
|
||||
{
|
||||
// if precision is requal or larger than the number length,
|
||||
// set precision to length -1
|
||||
if (abs($precision) >= strlen((string)$number)) {
|
||||
$precision = (strlen((string)$number) - 1) * -1;
|
||||
}
|
||||
$mult = pow(10, $precision); // Can be cached in lookup table
|
||||
return floor($number * $mult) / $mult;
|
||||
}
|
||||
|
||||
/**
|
||||
* inits input to 0, if value is not numeric
|
||||
*
|
||||
* @param string|int|float $number string or number to check
|
||||
* @return float if not number, then returns 0, else original input
|
||||
*/
|
||||
public static function initNumeric($number): float
|
||||
{
|
||||
if (!is_numeric($number)) {
|
||||
return 0;
|
||||
} else {
|
||||
return (float)$number;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
85
4dev/deprecated/CoreLibs/Convert/MimeAppName.php
Normal file
85
4dev/deprecated/CoreLibs/Convert/MimeAppName.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Translates a mime id string into the actual application or file name
|
||||
* for example 'text/plain' will output 'Text file'
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert;
|
||||
|
||||
class MimeAppName
|
||||
{
|
||||
/** @var array<string,string> */
|
||||
private static $mime_apps = [];
|
||||
|
||||
/**
|
||||
* constructor: init mime list
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
self::$mime_apps = [
|
||||
// zip
|
||||
'application/zip' => 'Zip File',
|
||||
// Powerpoint
|
||||
'application/vnd.ms-powerpoint' => 'Microsoft Powerpoint',
|
||||
'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'Microsoft Powerpoint',
|
||||
// PDF
|
||||
'pplication/pdf' => 'PDF',
|
||||
// JPEG
|
||||
'image/jpeg' => 'JPEG',
|
||||
// PNG
|
||||
'image/png' => 'PNG',
|
||||
// Indesign
|
||||
'application/x-indesign' => 'Adobe InDesign',
|
||||
// Photoshop
|
||||
'image/vnd.adobe.photoshop' => 'Adobe Photoshop',
|
||||
'application/photoshop' => 'Adobe Photoshop',
|
||||
// Illustrator
|
||||
'application/illustrator' => 'Adobe Illustrator',
|
||||
// Word
|
||||
'application/vnd.ms-word' => 'Microsoft Word',
|
||||
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'Microsoft Word',
|
||||
// Excel
|
||||
'application/vnd.ms-excel' => 'Microsoft Excel',
|
||||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'Microsoft Excel',
|
||||
// plain text
|
||||
'text/plain' => 'Text file',
|
||||
// html
|
||||
'text/html' => 'HTML',
|
||||
// mp4 (max 45MB each)
|
||||
'video/mpeg' => 'MPEG Video'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets or updates a mime type
|
||||
*
|
||||
* @param string $mime MIME Name, no validiation
|
||||
* @param string $app Applicaiton name
|
||||
* @return void
|
||||
*/
|
||||
public static function mimeSetAppName(string $mime, string $app): void
|
||||
{
|
||||
// if empty, don't set
|
||||
if (empty($mime) || empty($app)) {
|
||||
return;
|
||||
}
|
||||
self::$mime_apps[$mime] = $app;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the application name from mime type
|
||||
* if not set returns "Other file"
|
||||
*
|
||||
* @param string $mime MIME Name
|
||||
* @return string Application name matching
|
||||
*/
|
||||
public static function mimeGetAppName(string $mime): string
|
||||
{
|
||||
return self::$mime_apps[$mime] ?? 'Other file';
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
71
4dev/deprecated/CoreLibs/Convert/MimeEncode.php
Normal file
71
4dev/deprecated/CoreLibs/Convert/MimeEncode.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* alternate for header mime encode to void problems with long strings and
|
||||
* spaces/strange encoding problems.
|
||||
* Orignal issues during PHP5/7
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert;
|
||||
|
||||
class MimeEncode
|
||||
{
|
||||
/**
|
||||
* wrapper function for mb mime convert
|
||||
* for correct conversion with long strings
|
||||
*
|
||||
* @param string $string string to encode
|
||||
* @param string $encoding target encoding
|
||||
* @param string $line_break default line break is \r\n
|
||||
* @return string encoded string
|
||||
*/
|
||||
public static function __mbMimeEncode(
|
||||
string $string,
|
||||
string $encoding,
|
||||
string $line_break = "\r\n"
|
||||
): string {
|
||||
$current_internal_encoding = mb_internal_encoding();
|
||||
// set internal encoding, so the mimeheader encode works correctly
|
||||
mb_internal_encoding($encoding);
|
||||
// if a subject, make a work around for the broken mb_mimencode
|
||||
$pos = 0;
|
||||
// after 36 single bytes characters,
|
||||
// if then comes MB, it is broken
|
||||
// has to 2 x 36 < 74 so the mb_encode_mimeheader
|
||||
// 74 hardcoded split does not get triggered
|
||||
$split = 36;
|
||||
$_string = '';
|
||||
while ($pos < mb_strlen($string, $encoding)) {
|
||||
$output = mb_strimwidth($string, $pos, $split, "", $encoding);
|
||||
$pos += mb_strlen($output, $encoding);
|
||||
// if the strinlen is 0 here, get out of the loop
|
||||
if (!mb_strlen($output, $encoding)) {
|
||||
$pos += mb_strlen($string, $encoding);
|
||||
}
|
||||
$_string_encoded = mb_encode_mimeheader($output, $encoding);
|
||||
// only make linebreaks if we have mime encoded code inside
|
||||
// the space only belongs in the second line
|
||||
if ($_string && preg_match("/^=\?/", $_string_encoded)) {
|
||||
$_string .= $line_break . " ";
|
||||
} elseif (
|
||||
// hack for plain text with space at the end
|
||||
mb_strlen($output, $encoding) == $split &&
|
||||
mb_substr($output, -1, 1, $encoding) == " "
|
||||
) {
|
||||
// if output ends with space, add one more
|
||||
$_string_encoded .= " ";
|
||||
}
|
||||
$_string .= $_string_encoded;
|
||||
}
|
||||
// strip out any spaces BEFORE a line break
|
||||
$string = str_replace(" " . $line_break, $line_break, $_string);
|
||||
// before we end, reset internal encoding
|
||||
mb_internal_encoding($current_internal_encoding);
|
||||
// return mime encoded string
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
@@ -118,34 +118,6 @@ class Strings
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip any duplicated slahes from a path
|
||||
* eg: //foo///bar/foo.inc -> /foo/bar/foo.inc
|
||||
*
|
||||
* @param string $path Path to strip slashes from
|
||||
* @return string Clean path, on error returns original path
|
||||
*/
|
||||
public static function stripMultiplePathSlashes(string $path): string
|
||||
{
|
||||
return preg_replace(
|
||||
'#/+#',
|
||||
'/',
|
||||
$path
|
||||
) ?? $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove UTF8 BOM Byte string from line
|
||||
* Note: this is often found in CSV files exported from Excel at the first row, first element
|
||||
*
|
||||
* @param string $text
|
||||
* @return string
|
||||
*/
|
||||
public static function stripUTF8BomBytes(string $text): string
|
||||
{
|
||||
return trim($text, pack('H*', 'EFBBBF'));
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
136
4dev/deprecated/CoreLibs/Convert/VarSetType.php
Normal file
136
4dev/deprecated/CoreLibs/Convert/VarSetType.php
Normal file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Run is_<type> checks and return default value if not this type
|
||||
* This will return a default value as always what is expected and never null
|
||||
* Use this for santize output from multi return functions where we know what
|
||||
* will come back
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert;
|
||||
|
||||
use CoreLibs\Convert\Extends\VarSetTypeMain;
|
||||
|
||||
class VarSetType extends Extends\VarSetTypeMain
|
||||
{
|
||||
/**
|
||||
* Check is input is string, if not return default string.
|
||||
* Will always return string
|
||||
*
|
||||
* @param mixed $val Input value
|
||||
* @param string $default Default override value
|
||||
* @return string Input value or default as string
|
||||
*/
|
||||
public static function setStr(mixed $val, string $default = ''): string
|
||||
{
|
||||
return (string)VarSetTypeMain::setStrMain($val, $default, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert input to string if possible.
|
||||
* Will only work on string/int/float/bool/null types
|
||||
* Will always return string
|
||||
*
|
||||
* @param mixed $val Input value
|
||||
* @param string $default Default override value
|
||||
* @return string Input value as string or default as string
|
||||
*/
|
||||
public static function makeStr(mixed $val, string $default = ''): string
|
||||
{
|
||||
return (string)VarSetTypeMain::makeStrMain($val, $default, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if input is int, if not return default int value 0.
|
||||
* Will always return int.
|
||||
*
|
||||
* @param mixed $val Input value
|
||||
* @param int $default Default override value
|
||||
* @return int Input value or default as int
|
||||
*/
|
||||
public static function setInt(mixed $val, int $default = 0): int
|
||||
{
|
||||
return (int)VarSetTypeMain::setIntMain($val, $default, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert intput to int if possible, if not return default value 0.
|
||||
* Will always return int.
|
||||
*
|
||||
* @param mixed $val Input value
|
||||
* @param int $default Default override value
|
||||
* @return int Input value as int or default as int
|
||||
*/
|
||||
public static function makeInt(mixed $val, int $default = 0): int
|
||||
{
|
||||
return (int)VarSetTypeMain::makeIntMain($val, $default, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if input is float, if not return default value value 0.0.
|
||||
* Will always return float
|
||||
*
|
||||
* @param mixed $val Input value
|
||||
* @param float $default Default override value
|
||||
* @return float Input value or default as float
|
||||
*/
|
||||
public static function setFloat(mixed $val, float $default = 0.0): float
|
||||
{
|
||||
return (float)VarSetTypeMain::setFloatMain($val, $default, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert input to float, if not possible return default value 0.0.
|
||||
* Will always return float
|
||||
*
|
||||
* @param mixed $val Input value
|
||||
* @param float $default Default override value
|
||||
* @return float Input value as float or default as float
|
||||
*/
|
||||
public static function makeFloat(mixed $val, float $default = 0.0): float
|
||||
{
|
||||
return (float)VarSetTypeMain::makeFloatMain($val, $default, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if input is array, if not return default empty array.
|
||||
* Will always return array.
|
||||
*
|
||||
* @param mixed $val Input value
|
||||
* @param array<mixed> $default Default override value
|
||||
* @return array<mixed> Input value or default as array
|
||||
*/
|
||||
public static function setArray(mixed $val, array $default = []): array
|
||||
{
|
||||
return (array)VarSetTypeMain::setArrayMain($val, $default, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if input is bool, if not will return default value false.
|
||||
* Will aways return bool.
|
||||
*
|
||||
* @param mixed $val Input value
|
||||
* @param bool $default Default override value
|
||||
* @return bool Input value or default as bool
|
||||
*/
|
||||
public static function setBool(mixed $val, bool $default = false): bool
|
||||
{
|
||||
return (bool)VarSetTypeMain::setBoolMain($val, $default, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert anything to bool
|
||||
*
|
||||
* @param mixed $val Input value
|
||||
* @param bool $default Default override value
|
||||
* @return bool Input value as bool or default as bool
|
||||
*/
|
||||
public static function makeBool(mixed $val, bool $default = false): bool
|
||||
{
|
||||
return (bool)VarSetTypeMain::makeBoolMain($val, $default, false);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
130
4dev/deprecated/CoreLibs/Convert/VarSetTypeNull.php
Normal file
130
4dev/deprecated/CoreLibs/Convert/VarSetTypeNull.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Run is_<type> checks and return default value if not this type
|
||||
* This will return default null on invalid entries
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert;
|
||||
|
||||
use CoreLibs\Convert\Extends\VarSetTypeMain;
|
||||
|
||||
class VarSetTypeNull extends Extends\VarSetTypeMain
|
||||
{
|
||||
/**
|
||||
* Check is input is string, if not return default string.
|
||||
* Will return null if no string as default.
|
||||
*
|
||||
* @param mixed $val Input value
|
||||
* @param string|null $default Default override value
|
||||
* @return string|null Input value or default as string/null
|
||||
*/
|
||||
public static function setStr(mixed $val, ?string $default = null): ?string
|
||||
{
|
||||
return VarSetTypeMain::setStrMain($val, $default, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert input to string if possible.
|
||||
* Will only work on string/int/float/bool/null types.
|
||||
* Will return null if convert failed as default.
|
||||
*
|
||||
* @param mixed $val Input value
|
||||
* @param string|null $default Default override value
|
||||
* @return string|null Input value as string or default as string/null
|
||||
*/
|
||||
public static function makeStr(mixed $val, string $default = null): ?string
|
||||
{
|
||||
return VarSetTypeMain::makeStrMain($val, $default, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if input is int, if not return default value null.
|
||||
*
|
||||
* @param mixed $val Input value
|
||||
* @param int|null $default Default override value
|
||||
* @return int|null Input value or default as int/null
|
||||
*/
|
||||
public static function setInt(mixed $val, ?int $default = null): ?int
|
||||
{
|
||||
return VarSetTypeMain::setIntMain($val, $default, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert intput to int if possible, if not return default value value null.
|
||||
*
|
||||
* @param mixed $val Input value $val
|
||||
* @param int|null $default Default override value
|
||||
* @return int|null Input value as int or default as int/null
|
||||
*/
|
||||
public static function makeInt(mixed $val, int $default = null): ?int
|
||||
{
|
||||
return VarSetTypeMain::makeIntMain($val, $default, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if input is float, if not return default value value null.
|
||||
*
|
||||
* @param mixed $val Input value $val
|
||||
* @param float|null $default Default override value
|
||||
* @return float|null Input value or default as float/null
|
||||
*/
|
||||
public static function setFloat(mixed $val, ?float $default = null): ?float
|
||||
{
|
||||
return VarSetTypeMain::setFloatMain($val, $default, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert input to float, if not possible return default value null.
|
||||
*
|
||||
* @param mixed $val Input value $val
|
||||
* @param float|null $default Default override value
|
||||
* @return float|null Input value as float or default as float/null
|
||||
*/
|
||||
public static function makeFloat(mixed $val, float $default = null): ?float
|
||||
{
|
||||
return VarSetTypeMain::makeFloatMain($val, $default, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if input is array, if not return default value null.
|
||||
*
|
||||
* @param mixed $val Input value $val
|
||||
* @param array<mixed>|null $default Default override value
|
||||
* @return array<mixed>|null Input value or default as array/null
|
||||
*/
|
||||
public static function setArray(mixed $val, ?array $default = null): ?array
|
||||
{
|
||||
return VarSetTypeMain::setArrayMain($val, $default, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if input is bool, if not will return default value null.
|
||||
*
|
||||
* @param mixed $val Input value $val
|
||||
* @param bool|null $default Default override value
|
||||
* @return bool|null Input value or default as bool/null
|
||||
*/
|
||||
public static function setBool(mixed $val, ?bool $default = null): ?bool
|
||||
{
|
||||
return VarSetTypeMain::setBoolMain($val, $default, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert anything to bool
|
||||
*
|
||||
* @param mixed $val Input value $val
|
||||
* @return bool|null Input value as bool or default as bool/null
|
||||
*/
|
||||
public static function makeBool(mixed $val): ?bool
|
||||
{
|
||||
// note that the default value here is irrelevant, we return null
|
||||
// on unsetable string var
|
||||
return VarSetTypeMain::makeBoolMain($val, false, true);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
@@ -14,7 +14,7 @@ namespace CoreLibs\Create;
|
||||
class Email
|
||||
{
|
||||
/** @var array<string> allowed list for encodings that can do KV folding */
|
||||
private static array $encoding_kv_allowed = [
|
||||
private static $encoding_kv_allowed = [
|
||||
'UTF-8',
|
||||
'EUC-JP',
|
||||
'SJIS',
|
||||
@@ -25,7 +25,7 @@ class Email
|
||||
'JIS-ms',
|
||||
];
|
||||
/** @var string normaly this does not need to be changed */
|
||||
private static string $mb_convert_kana_mode = 'KV';
|
||||
private static $mb_convert_kana_mode = 'KV';
|
||||
|
||||
/**
|
||||
* create mime encoded email part for to/from emails.
|
||||
@@ -137,7 +137,7 @@ class Email
|
||||
* @param bool $kv_folding If set to true and a valid encoding,
|
||||
* do KV folding
|
||||
* @param bool $test test flag, default off
|
||||
* @param \CoreLibs\Logging\Logging|null $log Logging class,
|
||||
* @param \CoreLibs\Debug\Logging|null $log Logging class,
|
||||
* only used if test flag is true
|
||||
* @return int 2 test only, no sent
|
||||
* 1 for ok,
|
||||
@@ -156,7 +156,7 @@ class Email
|
||||
string $encoding = 'UTF-8',
|
||||
bool $kv_folding = false,
|
||||
bool $test = false,
|
||||
?\CoreLibs\Logging\Logging $log = null
|
||||
?\CoreLibs\Debug\Logging $log = null
|
||||
): int {
|
||||
/** @var array<string> */
|
||||
$to_emails = [];
|
||||
@@ -259,11 +259,11 @@ class Email
|
||||
$mail_delivery_status = 2;
|
||||
}
|
||||
// log if an log instance exists
|
||||
if ($log instanceof \CoreLibs\Logging\Logging) {
|
||||
if ($log instanceof \CoreLibs\Debug\Logging) {
|
||||
// build debug strings: convert to UTF-8 if not utf-8
|
||||
$log->debug('SEND EMAIL', 'HEADERS: ' . \CoreLibs\Debug\Support::prAr($headers) . ', '
|
||||
$log->debug('SEND EMAIL', 'HEADERS: ' . $log->prAr($headers) . ', '
|
||||
. 'ENCODING: ' . $encoding . ', '
|
||||
. 'KV FOLDING: ' . \CoreLibs\Debug\Support::prBl($kv_folding) . ', '
|
||||
. 'KV FOLDING: ' . $log->prBl($kv_folding) . ', '
|
||||
. 'TO: ' . $to_email . ', '
|
||||
. 'SUBJECT: ' . $out_subject . ', '
|
||||
. 'BODY: ' . ($encoding == 'UTF-8' ?
|
||||
122
4dev/deprecated/CoreLibs/Create/Hash.php
Normal file
122
4dev/deprecated/CoreLibs/Create/Hash.php
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* hash wrapper functions for old problem fixes
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Create;
|
||||
|
||||
class Hash
|
||||
{
|
||||
public const STANDARD_HASH_LONG = 'ripemd160';
|
||||
public const STANDARD_HASH_SHORT = 'adler32';
|
||||
|
||||
/**
|
||||
* checks php version and if >=5.2.7 it will flip the string
|
||||
* can return empty string if none of string sets work
|
||||
* hash returns false
|
||||
* preg_replace fails for older php version
|
||||
* Use __hash with crc32b or hash('crc32b', ...) for correct output
|
||||
*
|
||||
* @param string $string string to crc
|
||||
* @return string crc32b hash (old type)
|
||||
*/
|
||||
public static function __crc32b(string $string): string
|
||||
{
|
||||
// do normal hash crc32b
|
||||
$string = hash('crc32b', $string);
|
||||
// if bigger than 5.2.7, we need to "unfix" the fix
|
||||
if (\CoreLibs\Check\PhpVersion::checkPHPVersion('5.2.7')) {
|
||||
// flip it back to old (two char groups)
|
||||
$string = preg_replace("/^([a-z0-9]{2})([a-z0-9]{2})([a-z0-9]{2})([a-z0-9]{2})$/", "$4$3$2$1", $string);
|
||||
}
|
||||
if (!is_string($string)) {
|
||||
$string = '';
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* replacement for __crc32b call
|
||||
*
|
||||
* @param string $string string to hash
|
||||
* @param bool $use_sha use sha instead of crc32b (default false)
|
||||
* @return string hash of the string
|
||||
*/
|
||||
public static function __sha1Short(string $string, bool $use_sha = false): string
|
||||
{
|
||||
if ($use_sha) {
|
||||
// return only the first 9 characters
|
||||
return substr(hash('sha1', $string), 0, 9);
|
||||
} else {
|
||||
return self::__crc32b($string);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* replacemend for __crc32b call (alternate)
|
||||
* defaults to adler 32
|
||||
* allowed crc32b, adler32, fnv132, fnv1a32, joaat
|
||||
* all that create 8 char long hashes
|
||||
*
|
||||
* @param string $string string to hash
|
||||
* @param string $hash_type hash type (default adler32)
|
||||
* @return string hash of the string
|
||||
*/
|
||||
public static function __hash(
|
||||
string $string,
|
||||
string $hash_type = self::STANDARD_HASH_SHORT
|
||||
): string {
|
||||
if (
|
||||
!in_array(
|
||||
$hash_type,
|
||||
['crc32b', 'adler32', 'fnv132', 'fnv1a32', 'joaat']
|
||||
)
|
||||
) {
|
||||
$hash_type = 'adler32';
|
||||
}
|
||||
return hash($hash_type, $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper function for standard long hashd
|
||||
*
|
||||
* @param string $string String to be hashed
|
||||
* @return string Hashed string
|
||||
*/
|
||||
public static function __hashLong(string $string): string
|
||||
{
|
||||
return hash(self::STANDARD_HASH_LONG, $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* create a unique id with the standard hash type defined in __hash
|
||||
*
|
||||
* @return string Unique ID with fixed length of 8 characters
|
||||
* @deprecated Use \CoreLibs\Create\Uids::uniqIdShort() instead
|
||||
*/
|
||||
public static function __uniqId(): string
|
||||
{
|
||||
trigger_error('Method ' . __METHOD__ . ' is deprecated, '
|
||||
. '\CoreLibs\Create\Uids::uniqIdShort() class', E_USER_DEPRECATED);
|
||||
return \CoreLibs\Create\Uids::uniqIdShort();
|
||||
}
|
||||
|
||||
/**
|
||||
* create a unique id with the standard long hash type
|
||||
* defined in __hashLong
|
||||
*
|
||||
* @return string Unique ID with length of current default long hash
|
||||
* @deprecated Use \CoreLibs\Create\Uids::uniqIdLong() instead
|
||||
*/
|
||||
public static function __uniqIdLong(): string
|
||||
{
|
||||
trigger_error('Method ' . __METHOD__ . ' is deprecated, '
|
||||
. '\CoreLibs\Create\Uids::uniqIdLong() class', E_USER_DEPRECATED);
|
||||
return \CoreLibs\Create\Uids::uniqIdLong();
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
121
4dev/deprecated/CoreLibs/Create/RandomKey.php
Normal file
121
4dev/deprecated/CoreLibs/Create/RandomKey.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* random key functions
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Create;
|
||||
|
||||
class RandomKey
|
||||
{
|
||||
// key generation
|
||||
/** @var string */
|
||||
private static $key_range = '';
|
||||
/** @var int */
|
||||
private static $one_key_length;
|
||||
/** @var int */
|
||||
private static $key_length = 4; // default key length
|
||||
/** @var int */
|
||||
private static $max_key_length = 256; // max allowed length
|
||||
|
||||
/**
|
||||
* if launched as class, init random key data first
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->initRandomKeyData();
|
||||
}
|
||||
/**
|
||||
* sets the random key range with the default values
|
||||
*
|
||||
* @return void has no return
|
||||
*/
|
||||
private static function initRandomKeyData()
|
||||
{
|
||||
// random key generation base string
|
||||
self::$key_range = join('', array_merge(
|
||||
range('A', 'Z'),
|
||||
range('a', 'z'),
|
||||
range('0', '9')
|
||||
));
|
||||
self::$one_key_length = strlen(self::$key_range);
|
||||
}
|
||||
|
||||
/**
|
||||
* validates they key length for random string generation
|
||||
*
|
||||
* @param int $key_length key length
|
||||
* @return bool true for valid, false for invalid length
|
||||
*/
|
||||
private static function validateRandomKeyLenght(int $key_length): bool
|
||||
{
|
||||
if (
|
||||
is_numeric($key_length) &&
|
||||
$key_length > 0 &&
|
||||
$key_length <= self::$max_key_length
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the key length and checks that they key given is valid
|
||||
* if failed it will not change the default key length and return false
|
||||
*
|
||||
* @param int $key_length key length
|
||||
* @return bool true/false for set status
|
||||
*/
|
||||
public static function setRandomKeyLength(int $key_length): bool
|
||||
{
|
||||
// only if valid int key with valid length
|
||||
if (self::validateRandomKeyLenght($key_length) === true) {
|
||||
self::$key_length = $key_length;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get the current set random key length
|
||||
*
|
||||
* @return int Current set key length
|
||||
*/
|
||||
public static function getRandomKeyLength(): int
|
||||
{
|
||||
return self::$key_length;
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a random key based on the key_range with key_length
|
||||
* if override key length is set, it will check on valid key and use this
|
||||
* this will not set the class key length variable
|
||||
*
|
||||
* @param int $key_length key length override, -1 for use default
|
||||
* @return string random key
|
||||
*/
|
||||
public static function randomKeyGen(int $key_length = -1): string
|
||||
{
|
||||
// init random key strings if not set
|
||||
if (!is_numeric(self::$one_key_length)) {
|
||||
self::initRandomKeyData();
|
||||
}
|
||||
$use_key_length = 0;
|
||||
// only if valid int key with valid length
|
||||
if (self::validateRandomKeyLenght($key_length) === true) {
|
||||
$use_key_length = $key_length;
|
||||
} else {
|
||||
$use_key_length = self::$key_length;
|
||||
}
|
||||
// create random string
|
||||
$random_string = '';
|
||||
for ($i = 1; $i <= $use_key_length; $i++) {
|
||||
$random_string .= self::$key_range[random_int(0, self::$one_key_length - 1)];
|
||||
}
|
||||
return $random_string;
|
||||
}
|
||||
}
|
||||
371
4dev/deprecated/CoreLibs/Create/Session.php
Normal file
371
4dev/deprecated/CoreLibs/Create/Session.php
Normal file
@@ -0,0 +1,371 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* DESCRIPTION:
|
||||
* start a php sesseion
|
||||
* name can be given via startSession parameter
|
||||
* if not set tries to read $SET_SESSION_NAME from global
|
||||
* else will use default set in php.ini
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Create;
|
||||
|
||||
class Session
|
||||
{
|
||||
/** @var string list for errors */
|
||||
private $session_intern_error_str = '';
|
||||
|
||||
/**
|
||||
* Start session
|
||||
* startSession should be called for complete check
|
||||
* If this is called without any name set before the php.ini name is
|
||||
* used.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function startSessionCall(): void
|
||||
{
|
||||
session_start();
|
||||
}
|
||||
|
||||
/**
|
||||
* init a session, if array is empty or array does not have session_name set
|
||||
* then no auto init is run
|
||||
*
|
||||
* @param string $session_name if set and not empty, will start session
|
||||
*/
|
||||
public function __construct(string $session_name = '')
|
||||
{
|
||||
if (!empty($session_name)) {
|
||||
$this->startSession($session_name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* check if we are in CLI, we set this, so we can mock this
|
||||
* Not this is just a wrapper for the static System::checkCLI call
|
||||
*
|
||||
* @return bool True if we are in a CLI enviroment, or false for everything else
|
||||
*/
|
||||
public function checkCliStatus(): bool
|
||||
{
|
||||
return \CoreLibs\Get\System::checkCLI();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set session name call. If not valid session name, will return false
|
||||
*
|
||||
* @param string $session_name A valid string for session name
|
||||
* @return bool True if session name is valid,
|
||||
* False if not
|
||||
*/
|
||||
public function setSessionName(string $session_name): bool
|
||||
{
|
||||
if (!$this->checkValidSessionName($session_name)) {
|
||||
return false;
|
||||
}
|
||||
session_name($session_name);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return set error string, empty if none set
|
||||
* Error strings are only set in the startSession method
|
||||
*
|
||||
* @return string Last error string
|
||||
*/
|
||||
public function getErrorStr(): string
|
||||
{
|
||||
return $this->session_intern_error_str;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if session name is valid
|
||||
*
|
||||
* As from PHP 8.1/8.0/7.4 error
|
||||
* INVALID CHARS: =,; \t\r\n\013\014
|
||||
* NOTE: using . will fail even thought valid
|
||||
* we allow only alphanumeric with - (dash) and 1 to 128 characters
|
||||
*
|
||||
* @param string $session_name any string, not null
|
||||
* @return bool True for valid, False for invalid
|
||||
*/
|
||||
public static function checkValidSessionName(string $session_name): bool
|
||||
{
|
||||
// check
|
||||
if (
|
||||
// must only have those
|
||||
!preg_match('/^[-a-zA-Z0-9]{1,128}$/', $session_name) ||
|
||||
// cannot be only numbers
|
||||
preg_match('/^[0-9]+$/', $session_name)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* start session with given session name if set
|
||||
* aborts on command line or if sessions are not enabled
|
||||
* also aborts if session cannot be started
|
||||
* On sucess returns the session id
|
||||
*
|
||||
* @param string|null $session_name
|
||||
* @return string|bool
|
||||
*/
|
||||
public function startSession(?string $session_name = null)
|
||||
{
|
||||
// we can't start sessions on command line
|
||||
if ($this->checkCliStatus()) {
|
||||
$this->session_intern_error_str = '[SESSION] No sessions in php cli';
|
||||
return false;
|
||||
}
|
||||
// if session are OFF
|
||||
if ($this->getSessionStatus() === PHP_SESSION_DISABLED) {
|
||||
$this->session_intern_error_str = '[SESSION] Sessions are disabled';
|
||||
return false;
|
||||
}
|
||||
// session_status
|
||||
// initial the session if there is no session running already
|
||||
if (!$this->checkActiveSession()) {
|
||||
// if session name is emtpy, check if there is a global set
|
||||
// this is a deprecated fallback
|
||||
$session_name = $session_name ?? $GLOBALS['SET_SESSION_NAME'] ?? '';
|
||||
// DEPRECTED: constant SET_SESSION_NAME is no longer used
|
||||
// if set, set special session name
|
||||
if (!empty($session_name)) {
|
||||
// invalid session name, abort
|
||||
if (!$this->checkValidSessionName($session_name)) {
|
||||
$this->session_intern_error_str = '[SESSION] Invalid session name: ' . $session_name;
|
||||
return false;
|
||||
}
|
||||
$this->setSessionName($session_name);
|
||||
}
|
||||
// start session
|
||||
$this->startSessionCall();
|
||||
}
|
||||
// if we still have no active session
|
||||
if (!$this->checkActiveSession()) {
|
||||
$this->session_intern_error_str = '[SESSION] Failed to activate session';
|
||||
return false;
|
||||
}
|
||||
if (false === ($session_id = $this->getSessionId())) {
|
||||
$this->session_intern_error_str = '[SESSION] getSessionId did not return a session id';
|
||||
}
|
||||
return $session_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* get current set session id or false if none started
|
||||
*
|
||||
* @return string|bool
|
||||
*/
|
||||
public function getSessionId()
|
||||
{
|
||||
return session_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* get set session name or false if none started
|
||||
*
|
||||
* @return string|bool
|
||||
*/
|
||||
public function getSessionName()
|
||||
{
|
||||
return session_name();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if there is an active session.
|
||||
* Does not check if we can have a session
|
||||
*
|
||||
* @return bool True if there is an active session, else false
|
||||
*/
|
||||
public function checkActiveSession(): bool
|
||||
{
|
||||
if ($this->getSessionStatus() === PHP_SESSION_ACTIVE) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* unlock the session file, so concurrent AJAX requests can be done
|
||||
* NOTE: after this has been called, no changes in _SESSION will be stored
|
||||
* NOTE: a new session with a different name can be started after this one is called
|
||||
* if problem, run ob_flush() and flush() too
|
||||
*
|
||||
* @return bool True und sucess, false on failure
|
||||
*/
|
||||
public function writeClose(): bool
|
||||
{
|
||||
return session_write_close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Proper destroy a session
|
||||
* - unset the _SESSION array
|
||||
* - unset cookie if cookie on and we have not strict mode
|
||||
* - destroy session
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function sessionDestroy(): bool
|
||||
{
|
||||
$_SESSION = [];
|
||||
if (
|
||||
ini_get('session.use_cookies') &&
|
||||
!ini_get('session.use_strict_mode')
|
||||
) {
|
||||
$session_name = $this->getSessionName();
|
||||
if ($session_name === false) {
|
||||
$session_name = '';
|
||||
}
|
||||
$params = session_get_cookie_params();
|
||||
setcookie(
|
||||
(string)$session_name,
|
||||
'',
|
||||
time() - 42000,
|
||||
$params['path'],
|
||||
$params['domain'],
|
||||
$params['secure'],
|
||||
$params['httponly']
|
||||
);
|
||||
}
|
||||
return session_destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* get session status
|
||||
* PHP_SESSION_DISABLED if sessions are disabled.
|
||||
* PHP_SESSION_NONE if sessions are enabled, but none exists.
|
||||
* PHP_SESSION_ACTIVE if sessions are enabled, and one exists.
|
||||
*
|
||||
* https://www.php.net/manual/en/function.session-status.php
|
||||
*
|
||||
* @return int See possible return int values above
|
||||
*/
|
||||
public function getSessionStatus(): int
|
||||
{
|
||||
return session_status();
|
||||
}
|
||||
|
||||
// _SESSION set/unset methods
|
||||
|
||||
/**
|
||||
* unset all _SESSION entries
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function unsetAllS(): void
|
||||
{
|
||||
foreach (array_keys($_SESSION ?? []) as $name) {
|
||||
unset($_SESSION[$name]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* set _SESSION entry 'name' with any value
|
||||
*
|
||||
* @param string|int $name array name in _SESSION
|
||||
* @param mixed $value value to set (can be anything)
|
||||
* @return void
|
||||
*/
|
||||
public function setS($name, $value): void
|
||||
{
|
||||
$_SESSION[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* get _SESSION 'name' entry or empty string if not set
|
||||
*
|
||||
* @param string|int $name value key to get from _SESSION
|
||||
* @return mixed value stored in _SESSION
|
||||
*/
|
||||
public function getS($name)
|
||||
{
|
||||
return $_SESSION[$name] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a name is set in the _SESSION array
|
||||
*
|
||||
* @param string|int $name Name to check for
|
||||
* @return bool True for set, False fornot set
|
||||
*/
|
||||
public function issetS($name): bool
|
||||
{
|
||||
return isset($_SESSION[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* unset one _SESSION entry 'name' if exists
|
||||
*
|
||||
* @param string|int $name _SESSION key name to remove
|
||||
* @return void
|
||||
*/
|
||||
public function unsetS($name): void
|
||||
{
|
||||
if (isset($_SESSION[$name])) {
|
||||
unset($_SESSION[$name]);
|
||||
}
|
||||
}
|
||||
|
||||
// set/get below
|
||||
// ->var = value;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string|int $name
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function __set($name, $value): void
|
||||
{
|
||||
$_SESSION[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string|int $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
if (isset($_SESSION[$name])) {
|
||||
return $_SESSION[$name];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string|int $name
|
||||
* @return bool
|
||||
*/
|
||||
public function __isset($name): bool
|
||||
{
|
||||
return isset($_SESSION[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string|int $name
|
||||
* @return void
|
||||
*/
|
||||
public function __unset($name): void
|
||||
{
|
||||
if (isset($_SESSION[$name])) {
|
||||
unset($_SESSION[$name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
108
4dev/deprecated/CoreLibs/Create/Uids.php
Normal file
108
4dev/deprecated/CoreLibs/Create/Uids.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Create;
|
||||
|
||||
class Uids
|
||||
{
|
||||
// what to use as a default hash if non ise set and no DEFAULT_HASH is defined
|
||||
public const DEFAULT_HASH = 'sha256';
|
||||
public const STANDARD_HASH_LONG = 'ripemd160';
|
||||
public const STANDARD_HASH_SHORT = 'adler32';
|
||||
|
||||
/**
|
||||
* creates psuedo random uuid v4
|
||||
* Code take from class here:
|
||||
* https://www.php.net/manual/en/function.uniqid.php#94959
|
||||
*
|
||||
* @return string pseudo random uuid v4
|
||||
*/
|
||||
public static function uuidv4(): string
|
||||
{
|
||||
return sprintf(
|
||||
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
||||
// 32 bits for "time_low"
|
||||
mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0xffff),
|
||||
// 16 bits for "time_mid"
|
||||
mt_rand(0, 0xffff),
|
||||
// 16 bits for "time_hi_and_version",
|
||||
// four most significant bits holds version number 4
|
||||
mt_rand(0, 0x0fff) | 0x4000,
|
||||
// 16 bits, 8 bits for "clk_seq_hi_res",
|
||||
// 8 bits for "clk_seq_low",
|
||||
// two most significant bits holds zero and one for variant DCE1.1
|
||||
mt_rand(0, 0x3fff) | 0x8000,
|
||||
// 48 bits for "node"
|
||||
mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0xffff)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: make this a proper uniq ID creation
|
||||
* add uuidv4 subcall to the uuid function too
|
||||
* creates a uniq id
|
||||
*
|
||||
* @param string $type uniq id type, currently md5 or sha256 allowed
|
||||
* if not set will use DEFAULT_HASH if set
|
||||
* @return string uniq id
|
||||
*/
|
||||
public static function uniqId(string $type = ''): string
|
||||
{
|
||||
$uniq_id = '';
|
||||
switch ($type) {
|
||||
case 'md5':
|
||||
$uniq_id = md5(uniqid((string)rand(), true));
|
||||
break;
|
||||
case self::DEFAULT_HASH:
|
||||
$uniq_id = hash(self::DEFAULT_HASH, uniqid((string)rand(), true));
|
||||
break;
|
||||
case self::STANDARD_HASH_LONG:
|
||||
$uniq_id = hash(self::STANDARD_HASH_LONG, uniqid((string)rand(), true));
|
||||
break;
|
||||
case self::STANDARD_HASH_SHORT:
|
||||
$uniq_id = hash(self::STANDARD_HASH_SHORT, uniqid((string)rand(), true));
|
||||
break;
|
||||
default:
|
||||
// if not empty, check if in valid list
|
||||
if (
|
||||
!empty($type) &&
|
||||
in_array($type, hash_algos())
|
||||
) {
|
||||
$hash = $type;
|
||||
} else {
|
||||
// fallback to default hash type if none set or invalid
|
||||
$hash = self::DEFAULT_HASH;
|
||||
}
|
||||
$uniq_id = hash($hash, uniqid((string)rand(), true));
|
||||
break;
|
||||
}
|
||||
return $uniq_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* create a unique id with the standard hash type defined in __hash
|
||||
*
|
||||
* @return string Unique ID with fixed length of 8 characters
|
||||
*/
|
||||
public static function uniqIdShort(): string
|
||||
{
|
||||
return self::uniqId(self::STANDARD_HASH_SHORT);
|
||||
}
|
||||
|
||||
/**
|
||||
* create a unique id with the standard long hash type
|
||||
* defined in __hashLong
|
||||
*
|
||||
* @return string Unique ID with length of current default long hash
|
||||
*/
|
||||
public static function uniqIdLong(): string
|
||||
{
|
||||
return self::uniqId(self::STANDARD_HASH_LONG);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
655
4dev/deprecated/CoreLibs/DB/Extended/ArrayIO.php
Normal file
655
4dev/deprecated/CoreLibs/DB/Extended/ArrayIO.php
Normal file
@@ -0,0 +1,655 @@
|
||||
<?php
|
||||
|
||||
/*********************************************************************
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2002/12/17
|
||||
* VERSION: 1.0.0
|
||||
* RELEASED LICENSE: GNU GPL 3
|
||||
* SHORT DESC :RIPTION:
|
||||
* DB Array IO Class:
|
||||
* writes, reads or deletes a complete array (one data set) in/out a
|
||||
* table from the connected DB.
|
||||
* you don't have to write any SQL queries, worry over update/insert
|
||||
*
|
||||
* HISTORY:
|
||||
* 2019/9/11 (cs) error string 21->1021, 22->1022 for not overlapping with IO
|
||||
* 2005/07/07 (cs) updated array class for postgres: set 0 & NULL if int field given, insert uses () values () syntax
|
||||
* 2005/03/31 (cs) fixed the class call with all debug vars
|
||||
* 2003-03-10: error_ids where still wrong chagned 11->21 and 12->22
|
||||
* 2003-02-26: db_array_io is no longer single class but extens db_io,
|
||||
* as it needs it anyway
|
||||
* moved the class info vars into class_info array into
|
||||
* the constructor, removed info function
|
||||
* 2003-02-24: in db_delete moved query build to top, or pk_name/value
|
||||
* will be reset before delete is done
|
||||
* 2002-12-20: just added info() method
|
||||
* 2002-12-17: splitted the class from other file (with main db wrapper)
|
||||
*********************************************************************/
|
||||
|
||||
// picture upload should be taken out from here and out in media_class
|
||||
// as it actually has nothing to do with this one here ? (or at least
|
||||
// put into separete function in this class)
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\DB\Extended;
|
||||
|
||||
// subclass for one array handling
|
||||
class ArrayIO extends \CoreLibs\DB\IO
|
||||
{
|
||||
// main calss variables
|
||||
/** @var array<mixed> */
|
||||
public $table_array; // the array from the table to work on
|
||||
/** @var string */
|
||||
public $table_name; // the table_name
|
||||
/** @var string */
|
||||
public $pk_name = ''; // the primary key from this table
|
||||
/** @var int|string|null */
|
||||
public $pk_id; // the PK id
|
||||
// security values
|
||||
/** @var int base acl for current page */
|
||||
private $base_acl_level = 0;
|
||||
|
||||
/**
|
||||
* constructor for the array io class, set the
|
||||
* primary key name automatically (from array)
|
||||
*
|
||||
* @param array<mixed> $db_config db connection config
|
||||
* @param array<mixed> $table_array table array config
|
||||
* @param string $table_name table name string
|
||||
* @param \CoreLibs\Debug\Logging|null $log Logging class, default set if not set
|
||||
* @param int $base_acl_level Set base acl level, if needed
|
||||
* @param int $acl_admin Flag if this is an admin ACL access level
|
||||
*/
|
||||
public function __construct(
|
||||
array $db_config,
|
||||
array $table_array,
|
||||
string $table_name,
|
||||
\CoreLibs\Debug\Logging $log = null,
|
||||
int $base_acl_level = 0,
|
||||
int $acl_admin = 0
|
||||
) {
|
||||
// instance db_io class
|
||||
parent::__construct($db_config, $log ?? new \CoreLibs\Debug\Logging());
|
||||
// more error vars for this class
|
||||
$this->error_string['1999'] = 'No table array or table name set';
|
||||
$this->error_string['1021'] = 'No Primary Key given';
|
||||
$this->error_string['1022'] = 'Could not run Array Query';
|
||||
|
||||
$this->table_array = $table_array;
|
||||
$this->table_name = $table_name;
|
||||
|
||||
// error abort if no table array or no table name
|
||||
if (empty($table_array) || empty($table_name)) {
|
||||
$this->__dbError(1999, false, 'MAJOR ERROR: Core settings missing');
|
||||
}
|
||||
|
||||
// set primary key for given table_array
|
||||
foreach ($this->table_array as $key => $value) {
|
||||
if (!empty($value['pk'])) {
|
||||
$this->pk_name = $key;
|
||||
}
|
||||
}
|
||||
$this->dbArrayIOSetAcl($base_acl_level, $acl_admin);
|
||||
}
|
||||
|
||||
/**
|
||||
* class deconstructor
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
parent::__destruct();
|
||||
}
|
||||
|
||||
/**
|
||||
* set the base acl level and admin acl flag
|
||||
* This is needed for table array ACL checks
|
||||
* if not set I assume 0 (non write/non read/non admin)
|
||||
*
|
||||
* @param int $base_acl_level ACL Level from 0 to 100, -1 is not allowed
|
||||
* Will sett 0 if invalid
|
||||
* @param int $acl_admin 0 for non admin, 1 for admin (base acl is 100)
|
||||
* @return void
|
||||
*/
|
||||
public function dbArrayIOSetAcl(int $base_acl_level, int $acl_admin): void
|
||||
{
|
||||
// default not allowed, must be 0 at least
|
||||
if ($base_acl_level < 0) {
|
||||
$base_acl_level = 0;
|
||||
}
|
||||
// only 0 or 1 allowed
|
||||
if (!in_array($acl_admin, [0, 1])) {
|
||||
$acl_admin = 0;
|
||||
}
|
||||
// if the user is admin flagged, auto set to 100, if not already set to 100
|
||||
if ($acl_admin == 1) {
|
||||
$base_acl_level = 100;
|
||||
}
|
||||
$this->base_acl_level = $base_acl_level;
|
||||
}
|
||||
|
||||
/**
|
||||
* changes all previously alterd HTML code into visible one,
|
||||
* works for <b>,<i>, and <a> (thought <a> can be / or should
|
||||
* be handled with the magic links functions
|
||||
* used with the read function
|
||||
*
|
||||
* @param string $text any html encoded string
|
||||
* @return string decoded html string
|
||||
*/
|
||||
public function convertData($text): string
|
||||
{
|
||||
$text = str_replace('<b>', '<b>', $text);
|
||||
$text = str_replace('</b>', '</b>', $text);
|
||||
$text = str_replace('<i>', '<i>', $text);
|
||||
$text = str_replace('</i>', '</i>', $text);
|
||||
// my need a change
|
||||
$text = str_replace('<a href="', '<a target="_blank" href="', $text);
|
||||
$text = str_replace('">', '">', $text);
|
||||
$text = str_replace('</a>', '</a>', $text);
|
||||
return $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* changeds all HTML entities into non HTML ones
|
||||
*
|
||||
* @param string $text encoded html string
|
||||
* @return string decoded html string
|
||||
*/
|
||||
public function convertEntities($text): string
|
||||
{
|
||||
$text = str_replace('<', '<', $text);
|
||||
$text = str_replace('>', '>', $text);
|
||||
$text = str_replace('&', '&', $text);
|
||||
$text = str_replace('"', '"', $text);
|
||||
$text = str_replace(''', "'", $text);
|
||||
return $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* dumps the current data
|
||||
*
|
||||
* @param bool $write write to error message, default false
|
||||
* @return string the array data as html string entry
|
||||
*/
|
||||
public function dbDumpArray($write = false): string
|
||||
{
|
||||
reset($this->table_array);
|
||||
$string = '';
|
||||
foreach ($this->table_array as $column => $data_array) {
|
||||
$string .= '<b>' . $column . '</b> -> ' . $data_array['value'] . '<br>';
|
||||
}
|
||||
// add output to internal error_msg
|
||||
if ($write === true) {
|
||||
$this->__dbDebug('dbArray', $string);
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if pk is set and if not, set from pk_id and
|
||||
* if this also not set return 0
|
||||
*
|
||||
* @return bool true if pk value is set, else false
|
||||
*/
|
||||
public function dbCheckPkSet()
|
||||
{
|
||||
// if pk_id is set, overrule ...
|
||||
if ($this->pk_id) {
|
||||
$this->table_array[$this->pk_name]['value'] = $this->pk_id;
|
||||
}
|
||||
// if not set ... produce error
|
||||
if (!$this->table_array[$this->pk_name]['value']) {
|
||||
// if no PK found, error ...
|
||||
$this->__dbError(1021);
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* resets the whole array values
|
||||
* @param boolean $reset_pk true if we want to reset the pk too
|
||||
* @return void has no return
|
||||
*/
|
||||
public function dbResetArray($reset_pk = false): void
|
||||
{
|
||||
reset($this->table_array);
|
||||
foreach (array_keys($this->table_array) as $column) {
|
||||
if (!$this->table_array[$column]['pk']) {
|
||||
unset($this->table_array[$column]['value']);
|
||||
} elseif ($reset_pk) {
|
||||
unset($this->table_array[$column]['value']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes one dataset
|
||||
*
|
||||
* @param array<mixed> $table_array optional override for table array set
|
||||
* set this as new table array too
|
||||
* @param boolean $acl_limit [false], if set to true, well do ACL limit check
|
||||
* @return array<mixed> returns the table array that was deleted
|
||||
*/
|
||||
public function dbDelete($table_array = [], $acl_limit = false)
|
||||
{
|
||||
// is array and has values, override set and set new
|
||||
if (is_array($table_array) && count($table_array)) {
|
||||
$this->table_array = $table_array;
|
||||
}
|
||||
if (!$this->dbCheckPkSet()) {
|
||||
return $this->table_array;
|
||||
}
|
||||
if ($acl_limit === true && $this->base_acl_level < 100) {
|
||||
$this->log->debug('DB DELETE ERROR', 'ACL Limit on, Delete, '
|
||||
. 'but base ACL level of 100 not met: ' . $this->base_acl_level);
|
||||
return $this->table_array;
|
||||
}
|
||||
// delete query
|
||||
$q = 'DELETE FROM ' . $this->table_name . ' WHERE ';
|
||||
$q .= $this->pk_name . ' = ' . $this->table_array[$this->pk_name]['value'] . ' ';
|
||||
// delete files and build FK query
|
||||
reset($this->table_array);
|
||||
$q_where = '';
|
||||
foreach (array_keys($this->table_array) as $column) {
|
||||
// suchen nach bildern und lschen ...
|
||||
if (
|
||||
!empty($this->table_array[$column]['file']) &&
|
||||
file_exists($this->table_array[$column]['url'] . $this->table_array[$column]['value'])
|
||||
) {
|
||||
if (file_exists($this->table_array[$column]['path'] . $this->table_array[$column]['value'])) {
|
||||
unlink($this->table_array[$column]['path'] . $this->table_array[$column]['value']);
|
||||
}
|
||||
$file_name = str_replace('_tn', '', $this->table_array[$column]['value']);
|
||||
if (file_exists($this->table_array[$column]['path'] . $file_name)) {
|
||||
unlink($this->table_array[$column]['path'] . $file_name);
|
||||
}
|
||||
}
|
||||
// if we have a foreign key
|
||||
if (!empty($this->table_array[$column]['fk'])) {
|
||||
// create FK constraint checks
|
||||
if ($q_where) {
|
||||
$q_where .= ' AND ';
|
||||
}
|
||||
$q_where .= $column . ' = ' . $this->table_array[$column]['value'];
|
||||
}
|
||||
// allgemeines zurcksetzen des arrays
|
||||
unset($this->table_array[$column]['value']);
|
||||
}
|
||||
|
||||
// attach fk row if there ...
|
||||
if ($q_where) {
|
||||
$q .= ' AND ' . $q_where;
|
||||
}
|
||||
// if 0, error
|
||||
$this->pk_id = null;
|
||||
if (!$this->dbExec($q)) {
|
||||
$this->__dbError(1022);
|
||||
}
|
||||
return $this->table_array;
|
||||
}
|
||||
|
||||
/**
|
||||
* reads one row into the array
|
||||
*
|
||||
* @param boolean $edit on true convert data, else as is
|
||||
* @param array<mixed> $table_array optional table array, overwrites
|
||||
* internal set array
|
||||
* @return array<mixed> set table array with values
|
||||
*/
|
||||
public function dbRead($edit = false, $table_array = [])
|
||||
{
|
||||
// if array give, overrules internal array
|
||||
if (is_array($table_array) && count($table_array)) {
|
||||
$this->table_array = $table_array;
|
||||
}
|
||||
if (!$this->dbCheckPkSet()) {
|
||||
return $this->table_array;
|
||||
}
|
||||
reset($this->table_array);
|
||||
$q_select = '';
|
||||
$q_where = '';
|
||||
// create select part & addition FK part
|
||||
foreach ($this->table_array as $column => $data_array) {
|
||||
if ($q_select) {
|
||||
$q_select .= ', ';
|
||||
}
|
||||
if (
|
||||
!empty($data_array['type']) && $data_array['type'] == 'datetime' &&
|
||||
!empty($data_array['sql_read'])
|
||||
) {
|
||||
// convert tom different timestamp type
|
||||
$q_select .= "TO_CHAR($column, '" . $data_array['sql_read'] . "') AS $column";
|
||||
} else {
|
||||
$q_select .= $column;
|
||||
}
|
||||
|
||||
// check FK ...
|
||||
if (
|
||||
isset($this->table_array[$column]['fk']) &&
|
||||
isset($this->table_array[$column]['value'])
|
||||
) {
|
||||
if (!empty($q_where)) {
|
||||
$q_where .= ' AND ';
|
||||
}
|
||||
$q_where .= $column .= ' = ' . $this->table_array[$column]['value'];
|
||||
}
|
||||
}
|
||||
|
||||
$q = 'SELECT ';
|
||||
$q .= $q_select;
|
||||
$q .= ' FROM ' . $this->table_name . ' WHERE ';
|
||||
$q .= $this->pk_name . ' = ' . $this->table_array[$this->pk_name]['value'] . ' ';
|
||||
if ($q_where) {
|
||||
$q .= ' AND ' . $q_where;
|
||||
}
|
||||
|
||||
// if query was executed okay, else set error
|
||||
if ($this->dbExec($q)) {
|
||||
if (is_array($res = $this->dbFetchArray())) {
|
||||
reset($this->table_array);
|
||||
foreach ($this->table_array as $column => $data_array) {
|
||||
// wenn "edit" dann gib daten wie in DB zurück, ansonten aufbereiten fr ausgabe
|
||||
// ?? sollte das nicht drauen ??? man weis ja net was da drin steht --> is noch zu berlegen
|
||||
// $this->log->debug('DB READ', 'EDIT: $edit | Spalte: $column | type: '
|
||||
// .$this->table_array[$column]['type'].' | Res: '.$res[$column]);
|
||||
if ($edit) {
|
||||
$this->table_array[$column]['value'] = $res[$column];
|
||||
// if password, also write to hidden
|
||||
if (
|
||||
isset($this->table_array[$column]['type']) &&
|
||||
$this->table_array[$column]['type'] == 'password'
|
||||
) {
|
||||
$this->table_array[$column]['HIDDEN_value'] = $res[$column];
|
||||
}
|
||||
} else {
|
||||
$this->table_array[$column]['value'] = $this->convertData(nl2br($res[$column]));
|
||||
// had to put out the htmlentities from the line above as it breaks japanese characters
|
||||
}
|
||||
}
|
||||
}
|
||||
// possible dbFetchArray errors ...
|
||||
$this->pk_id = $this->table_array[$this->pk_name]['value'];
|
||||
} else {
|
||||
$this->__dbError(1022);
|
||||
}
|
||||
return $this->table_array;
|
||||
}
|
||||
|
||||
/**
|
||||
* writes one set into DB or updates one set (if PK exists)
|
||||
*
|
||||
* @param boolean $addslashes old convert entities and set set escape
|
||||
* @param array<mixed> $table_array optional table array, overwrites internal one
|
||||
* @param boolean $acl_limit [false], if set to true, well do ACL limit check
|
||||
* @return array<mixed> table array or null
|
||||
*/
|
||||
public function dbWrite(
|
||||
bool $addslashes = false,
|
||||
array $table_array = [],
|
||||
bool $acl_limit = false
|
||||
): array {
|
||||
if (is_array($table_array) && count($table_array)) {
|
||||
$this->table_array = $table_array;
|
||||
}
|
||||
// PK ID check
|
||||
// if ($this->pk_id && !$this->table_array[$this->pk_name]["value"]) {
|
||||
// $this->table_array[$this->pk_name]["value"]=$this->pk_id;
|
||||
// }
|
||||
// checken ob PKs gesetzt, wenn alle -> update, wenn keiner -> insert, wenn ein paar -> ERROR!
|
||||
if (!$this->table_array[$this->pk_name]['value']) {
|
||||
$insert = 1;
|
||||
} else {
|
||||
$insert = 0;
|
||||
}
|
||||
// early abort for new write with not enough ACL level
|
||||
if ($insert && $acl_limit === true && $this->base_acl_level < 100) {
|
||||
$this->log->debug('DB WRITE ERROR', 'ACL Limit on, Insert, '
|
||||
. 'but base ACL level of 100 not met: ' . $this->base_acl_level);
|
||||
return $this->table_array;
|
||||
}
|
||||
|
||||
reset($this->table_array);
|
||||
$q_data = '';
|
||||
$q_vars = '';
|
||||
$q_where = '';
|
||||
foreach ($this->table_array as $column => $data_array) {
|
||||
/********************************* START FILE *************************************/
|
||||
// file upload
|
||||
if (isset($this->table_array[$column]['file'])) {
|
||||
// falls was im tmp drinnen, sprich ein upload, datei kopieren, Dateinamen in db schreiben
|
||||
// falls datei schon am server (physischer pfad), dann einfach url in db schreiben (update)
|
||||
// falls in 'delete' 'ja' dann loeschen (und gibts eh nur beim update)
|
||||
if ($this->table_array[$column]['delete']) {
|
||||
unset($this->table_array[$column]['delete']);
|
||||
if (file_exists($this->table_array[$column]['path'] . $this->table_array[$column]['value'])) {
|
||||
unlink($this->table_array[$column]['path'] . $this->table_array[$column]['value']);
|
||||
}
|
||||
$file_name = str_replace('_tn', '', $this->table_array[$column]['value']);
|
||||
if (file_exists($this->table_array[$column]['path'] . $file_name)) {
|
||||
unlink($this->table_array[$column]['path'] . $file_name);
|
||||
}
|
||||
$this->table_array[$column]['value'] = '';
|
||||
} else {
|
||||
if ($this->table_array[$column]['tmp'] != 'none' && $this->table_array[$column]['tmp']) {
|
||||
// mozilla, patch
|
||||
$fn_name = explode('/', $this->table_array[$column]['dn']);
|
||||
$this->table_array[$column]['dn'] = $fn_name[count($fn_name) - 1];
|
||||
$filename_parts = explode('.', $this->table_array[$column]['dn']);
|
||||
$ext = end($filename_parts);
|
||||
array_splice($filename_parts, -1, 1);
|
||||
$name = str_replace(' ', '_', implode('.', $filename_parts));
|
||||
$file_name = $name . '.' . $ext;
|
||||
//echo 'Dn: $file_name';
|
||||
copy($this->table_array[$column]['tmp'], $this->table_array[$column]['path'] . $file_name);
|
||||
// automatisch thumbnail generieren, geht nur mit convert (ImageMagic!!!), aber nur bei bild ..
|
||||
if (in_array(strtolower($ext), ['jpeg', 'jpg', 'gif', 'png'])) {
|
||||
$file_name_tn = $name . '_tn.' . $ext;
|
||||
$input = $this->table_array[$column]['path'] . $file_name;
|
||||
$output = $this->table_array[$column]['path'] . $file_name_tn;
|
||||
$com = 'convert -geometry 115 ' . $input . ' ' . $output;
|
||||
exec($com);
|
||||
$this->table_array[$column]['value'] = $file_name_tn;
|
||||
} else {
|
||||
$this->table_array[$column]['value'] = $file_name;
|
||||
}
|
||||
} elseif (file_exists($this->table_array[$column]['path'] . $this->table_array[$column]['value'])) {
|
||||
// mach gar nix, wenn bild schon da ???
|
||||
}
|
||||
} // delete or upload
|
||||
} // file IF
|
||||
/********************************* END FILE **************************************/
|
||||
|
||||
// do not write 'pk' (primary key) or 'view' values
|
||||
// also do not write UPDATE for elements that are
|
||||
// acl flagged, not if we have an ACL limiter, don't insert
|
||||
// $this->log->debug('DB WRITE', 'C: ' . $column . ', '
|
||||
// . 'ACL Level ' . $this->log->prBl($acl_limit) . ', '
|
||||
// . 'TA ACL: ' . ($this->table_array[$column]['min_edit_acl'] ?? 100) . ', '
|
||||
// . 'Base ACL: ' . $this->base_acl_level);
|
||||
if (
|
||||
!isset($this->table_array[$column]['pk']) &&
|
||||
isset($this->table_array[$column]['type']) &&
|
||||
$this->table_array[$column]['type'] != 'view' &&
|
||||
strlen($column) > 0 &&
|
||||
// no acl limiter
|
||||
($acl_limit === false ||
|
||||
(
|
||||
// acl limit is true, min edit must be at larger than set
|
||||
$acl_limit === true &&
|
||||
$this->base_acl_level >=
|
||||
($this->table_array[$column]['min_edit_acl'] ?? 100)
|
||||
))
|
||||
) {
|
||||
// for password use hidden value if main is not set
|
||||
if (
|
||||
isset($this->table_array[$column]['type']) &&
|
||||
$this->table_array[$column]['type'] == 'password' &&
|
||||
empty($this->table_array[$column]['value'])
|
||||
) {
|
||||
$this->table_array[$column]['value'] = $this->table_array[$column]['HIDDEN_value'];
|
||||
}
|
||||
if (!$insert) {
|
||||
if (strlen($q_data)) {
|
||||
$q_data .= ', ';
|
||||
}
|
||||
$q_data .= $column . ' = ';
|
||||
} else {
|
||||
// this is insert
|
||||
if (strlen($q_data)) {
|
||||
$q_data .= ', ';
|
||||
}
|
||||
if (strlen($q_vars)) {
|
||||
$q_vars .= ', ';
|
||||
}
|
||||
$q_vars .= $column;
|
||||
}
|
||||
// integer is different
|
||||
if (isset($this->table_array[$column]['int']) || isset($this->table_array[$column]['int_null'])) {
|
||||
$this->log->debug('WRITE CHECK', '[' . $column . '][' . $this->table_array[$column]['value'] . ']'
|
||||
. '[' . $this->table_array[$column]['type'] . '] '
|
||||
. 'VALUE SET: ' . (string)isset($this->table_array[$column]['value'])
|
||||
. ' | INT NULL: ' . (string)isset($this->table_array[$column]['int_null']));
|
||||
if (
|
||||
isset($this->table_array[$column]['value']) &&
|
||||
!$this->table_array[$column]['value'] &&
|
||||
isset($this->table_array[$column]['int_null'])
|
||||
) {
|
||||
$_value = 'NULL';
|
||||
} elseif (
|
||||
!isset($this->table_array[$column]['value']) ||
|
||||
(isset($this->table_array[$column]['value']) && !$this->table_array[$column]['value'])
|
||||
) {
|
||||
$_value = 0;
|
||||
} else {
|
||||
$_value = $this->table_array[$column]['value'];
|
||||
}
|
||||
$q_data .= $_value;
|
||||
} elseif (isset($this->table_array[$column]['bool'])) {
|
||||
// boolean storeage (reverse check on ifset)
|
||||
$q_data .= "'" . $this->dbBoolean($this->table_array[$column]['value'], true) . "'";
|
||||
} elseif (
|
||||
isset($this->table_array[$column]['interval']) ||
|
||||
isset($this->table_array[$column]['date']) ||
|
||||
isset($this->table_array[$column]['datetime']) ||
|
||||
isset($this->table_array[$column]['emptynull'])
|
||||
) {
|
||||
// for interval we check if no value, then we set null
|
||||
if (
|
||||
!isset($this->table_array[$column]['value']) ||
|
||||
(isset($this->table_array[$column]['value']) && !$this->table_array[$column]['value'])
|
||||
) {
|
||||
$_value = 'NULL';
|
||||
} elseif (isset($this->table_array[$column]['value'])) {
|
||||
$_value = $this->dbEscapeLiteral($this->table_array[$column]['value']);
|
||||
} else {
|
||||
// fallback
|
||||
$_value = 'NULL';
|
||||
}
|
||||
$q_data .= $_value;
|
||||
} else {
|
||||
// if the error check is json, we set field to null if NOT set
|
||||
// else normal string write
|
||||
if (
|
||||
isset($this->table_array[$column]['error_check']) &&
|
||||
$this->table_array[$column]['error_check'] == 'json' &&
|
||||
(
|
||||
!isset($this->table_array[$column]['value']) ||
|
||||
(isset($this->table_array[$column]['value']) &&
|
||||
!$this->table_array[$column]['value'])
|
||||
)
|
||||
) {
|
||||
$q_data .= 'NULL';
|
||||
} else {
|
||||
// normal string
|
||||
$q_data .= "'";
|
||||
// if add slashes do convert & add slashes else write AS is
|
||||
if ($addslashes) {
|
||||
$q_data .= $this->dbEscapeString(
|
||||
$this->convertEntities($this->table_array[$column]['value'])
|
||||
);
|
||||
} else {
|
||||
$q_data .= $this->dbEscapeString($this->table_array[$column]['value']);
|
||||
}
|
||||
$q_data .= "'";
|
||||
}
|
||||
}
|
||||
}
|
||||
} // while ...
|
||||
|
||||
if (empty($q_data)) {
|
||||
$this->log->debug('DB WRITE ERROR', 'No data to write, possible through ACL');
|
||||
return $this->table_array;
|
||||
}
|
||||
|
||||
// NOW get PK, and FK settings (FK only for update query)
|
||||
// get it at the end, cause now we can be more sure of no double IDs, etc
|
||||
reset($this->table_array);
|
||||
// create select part & addition FK part
|
||||
foreach ($this->table_array as $column => $data_array) {
|
||||
// check FK ...
|
||||
if (
|
||||
isset($this->table_array[$column]['fk']) &&
|
||||
isset($this->table_array[$column]['value'])
|
||||
) {
|
||||
if (!empty($q_where)) {
|
||||
$q_where .= ' AND ';
|
||||
}
|
||||
$q_where .= $column .= ' = ' . $this->table_array[$column]['value'];
|
||||
}
|
||||
}
|
||||
|
||||
// if no PK set, then get max ID from DB
|
||||
if (!$this->table_array[$this->pk_name]['value']) {
|
||||
// max id, falls INSERT
|
||||
$q = 'SELECT MAX(' . $this->pk_name . ') + 1 AS pk_id FROM ' . $this->table_name;
|
||||
if (is_array($res = $this->dbReturnRow($q))) {
|
||||
$pk_id = $res['pk_id'];
|
||||
} else {
|
||||
$pk_id = 1;
|
||||
}
|
||||
$this->table_array[$this->pk_name]['value'] = $pk_id;
|
||||
}
|
||||
|
||||
if (!$insert) {
|
||||
$q = 'UPDATE ' . $this->table_name . ' SET ';
|
||||
$q .= $q_data;
|
||||
$q .= ' WHERE ';
|
||||
$q .= $this->pk_name . ' = ' . $this->table_array[$this->pk_name]['value'] . ' ';
|
||||
if (!empty($q_where)) {
|
||||
$q .= ' AND ' . $q_where;
|
||||
}
|
||||
// set pk_id ... if it has changed or so
|
||||
$this->pk_id = $this->table_array[$this->pk_name]['value'];
|
||||
} else {
|
||||
$q = 'INSERT INTO ' . $this->table_name . ' ';
|
||||
$q .= '(' . $q_vars . ') ';
|
||||
$q .= 'VALUES (' . $q_data . ')';
|
||||
// write primary key too
|
||||
// if ($q_data)
|
||||
// $q .= ", ";
|
||||
// $q .= $this->pk_name." = ".$this->table_array[$this->pk_name]['value']." ";
|
||||
// $this->pk_id = $this->table_array[$this->pk_name]['value'];
|
||||
}
|
||||
// return success or not
|
||||
if (!$this->dbExec($q)) {
|
||||
$this->__dbError(1022);
|
||||
}
|
||||
// set primary key
|
||||
if ($insert) {
|
||||
$insert_id = $this->dbGetInsertPK();
|
||||
if (is_array($insert_id)) {
|
||||
$insert_id = 0;
|
||||
}
|
||||
$this->table_array[$this->pk_name]['value'] = $insert_id;
|
||||
$this->pk_id = $insert_id;
|
||||
}
|
||||
// return the table if needed
|
||||
return $this->table_array;
|
||||
}
|
||||
// end of class
|
||||
}
|
||||
|
||||
// __END__
|
||||
3342
4dev/deprecated/CoreLibs/DB/IO.php
Normal file
3342
4dev/deprecated/CoreLibs/DB/IO.php
Normal file
File diff suppressed because it is too large
Load Diff
883
4dev/deprecated/CoreLibs/DB/SQL/PgSQL.php
Normal file
883
4dev/deprecated/CoreLibs/DB/SQL/PgSQL.php
Normal file
@@ -0,0 +1,883 @@
|
||||
<?php
|
||||
|
||||
/*********************************************************************
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2003/04/09
|
||||
* SHORT DESCRIPTION:
|
||||
* 2018/3/23, the whole class system is transformed to namespaces
|
||||
* also all internal class calls are converted to camel case
|
||||
*
|
||||
* pgsql wrapper calls
|
||||
*
|
||||
* HISTORY:
|
||||
* 2008/04/16 (cs) wrapper for pg escape string
|
||||
* 2007/01/11 (cs) add prepare/execute for postgres
|
||||
* 2006/09/12 (cs) in case db_query retuns false, save the query and
|
||||
* run the query through the send/get procedure to get
|
||||
* correct error data from the db
|
||||
* 2006/06/26 (cs) added port for db connection
|
||||
* 2006/04/03 (cs) added meta data for table
|
||||
* 2005/07/25 (cs) removed the plural s remove, not needed and not 100% working
|
||||
* 2005/07/07 (cs) the default it is table_name _ id
|
||||
* 2005/01/19 (cs) changed the pgsql connect, so it dies if it can't connect to the DB
|
||||
* 2004/09/30 (cs) layout cleanup
|
||||
*
|
||||
*
|
||||
* collection of PostgreSQL wrappers
|
||||
*
|
||||
* pg_prepare
|
||||
* pg_execute
|
||||
* pg_num_rows
|
||||
* pg_num_fields
|
||||
* pg_field_name
|
||||
* pg_affected_rows (*)
|
||||
* pg_fetch_array
|
||||
* pg_query
|
||||
* pg_send_query
|
||||
* pg_get_result
|
||||
* pg_connection_busy
|
||||
* pg_close
|
||||
* pg_connect (*)
|
||||
* pg_meta_data
|
||||
* pg_escape_string
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\DB\SQL;
|
||||
|
||||
// below no ignore is needed if we want to use PgSql interface checks with PHP 8.0
|
||||
// as main system. Currently all @var sets are written as object
|
||||
/** @#phan-file-suppress PhanUndeclaredTypeProperty,PhanUndeclaredTypeParameter,PhanUndeclaredTypeReturnType */
|
||||
|
||||
class PgSQL implements \CoreLibs\DB\SQL\SqlInterface\SqlFunctions
|
||||
{
|
||||
/** @var string */
|
||||
private $last_error_query;
|
||||
// NOTE for PHP 8.1 this is no longer a resource
|
||||
/** @var object|resource|bool */ // replace object with PgSql\Connection
|
||||
private $dbh;
|
||||
|
||||
/**
|
||||
* queries last error query and returns true or false if error was set
|
||||
*
|
||||
* @return bool true/false if last error is set
|
||||
*/
|
||||
public function __dbLastErrorQuery(): bool
|
||||
{
|
||||
if ($this->last_error_query) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_query, catches error and stores it in class var
|
||||
*
|
||||
* @param string $query Query string
|
||||
* @return object|resource|bool query result (PgSql\Result)
|
||||
*/
|
||||
public function __dbQuery(string $query)
|
||||
{
|
||||
$this->last_error_query = '';
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return false;
|
||||
}
|
||||
// read out the query status and save the query if needed
|
||||
$result = pg_query($this->dbh, $query);
|
||||
if ($result === false) {
|
||||
$this->last_error_query = $query;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Proposed
|
||||
* wrapperf or pg_query_params for queries in the style of
|
||||
* SELECT foo FROM bar WHERE foobar = $1
|
||||
*
|
||||
* @param string $query Query string with placeholders $1, ..
|
||||
* @param array<mixed> $params Matching parameters for each placerhold
|
||||
* @return object|resource|bool Query result (PgSql\Result)
|
||||
*/
|
||||
public function __dbQueryParams(string $query, array $params)
|
||||
{
|
||||
$this->last_error_query = '';
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return false;
|
||||
}
|
||||
// parse query and get all $n entries
|
||||
// TODO count of $n must match params
|
||||
// read out the query status and save the query if needed
|
||||
$result = pg_query_params($this->dbh, $query, $params);
|
||||
if ($result === false) {
|
||||
$this->last_error_query = $query;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* sends an async query to the server
|
||||
*
|
||||
* @param string $query query string
|
||||
* @return bool true/false if query was sent successful
|
||||
*/
|
||||
public function __dbSendQuery(string $query): bool
|
||||
{
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return false;
|
||||
}
|
||||
$result = pg_send_query($this->dbh, $query);
|
||||
return $result ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_get_result
|
||||
*
|
||||
* @return object|resource|bool resource handler or false for error (PgSql\Result)
|
||||
*/
|
||||
public function __dbGetResult()
|
||||
{
|
||||
$this->last_error_query = '';
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return false;
|
||||
}
|
||||
$result = pg_get_result($this->dbh);
|
||||
if ($result === false) {
|
||||
return false;
|
||||
}
|
||||
if ($error = pg_result_error($result)) {
|
||||
$this->last_error_query = $error;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_close
|
||||
*
|
||||
* @return void has no return
|
||||
*/
|
||||
public function __dbClose(): void
|
||||
{
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return;
|
||||
}
|
||||
if (pg_connection_status($this->dbh) === PGSQL_CONNECTION_OK) {
|
||||
// in 8.1 this throws an error, and we don't need that anyway
|
||||
// pg_close($this->dbh);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_prepare
|
||||
*
|
||||
* @param string $name statement name
|
||||
* @param string $query query string
|
||||
* @return object|resource|bool prepare statement handler or
|
||||
* false for error (PgSql\Result)
|
||||
*/
|
||||
public function __dbPrepare(string $name, string $query)
|
||||
{
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return false;
|
||||
}
|
||||
$result = pg_prepare($this->dbh, $name, $query);
|
||||
if (!$result) {
|
||||
$this->last_error_query = $query;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_execute for running a prepared statement
|
||||
*
|
||||
* @param string $name statement name
|
||||
* @param array<mixed> $data data array
|
||||
* @return object|resource|bool returns status or false for error (PgSql\Result)
|
||||
*/
|
||||
public function __dbExecute(string $name, array $data)
|
||||
{
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return false;
|
||||
}
|
||||
$result = pg_execute($this->dbh, $name, $data);
|
||||
if (!$result) {
|
||||
$this->last_error_query = $name;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_num_rows
|
||||
*
|
||||
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
||||
* @return int number of rows, -1 on error
|
||||
*/
|
||||
public function __dbNumRows($cursor): int
|
||||
{
|
||||
if ($cursor === false || is_bool($cursor)) {
|
||||
return -1;
|
||||
}
|
||||
return pg_num_rows($cursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_num_fields
|
||||
*
|
||||
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
||||
* @return int number for fields in result, -1 on error
|
||||
*/
|
||||
public function __dbNumFields($cursor): int
|
||||
{
|
||||
if ($cursor === false || is_bool($cursor)) {
|
||||
return -1;
|
||||
}
|
||||
return pg_num_fields($cursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_field_name
|
||||
*
|
||||
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
||||
* @param int $i field position
|
||||
* @return string|bool name or false on error
|
||||
*/
|
||||
public function __dbFieldName($cursor, int $i)
|
||||
{
|
||||
if ($cursor === false || is_bool($cursor)) {
|
||||
return false;
|
||||
}
|
||||
return pg_field_name($cursor, $i);
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_fetch_array
|
||||
* if through/true false, use __dbResultType(true)
|
||||
*
|
||||
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
||||
* @param int $result_type result type as int number
|
||||
* @return array<mixed>|bool array result data or false on end/error
|
||||
*/
|
||||
public function __dbFetchArray($cursor, int $result_type = PGSQL_BOTH)
|
||||
{
|
||||
if ($cursor === false || is_bool($cursor)) {
|
||||
return false;
|
||||
}
|
||||
// result type is passed on as is [should be checked]
|
||||
return pg_fetch_array($cursor, null, $result_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* simple match up between assoc true/false
|
||||
*
|
||||
* @param bool $assoc_type true (default) for PGSQL_ASSOC, false for PGSQL_BOTH
|
||||
* @return int valid result type for fetch array
|
||||
*/
|
||||
public function __dbResultType(bool $assoc_type = true): int
|
||||
{
|
||||
if ($assoc_type == true) {
|
||||
return PGSQL_ASSOC;
|
||||
}
|
||||
// fallback to default
|
||||
return PGSQL_BOTH;
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_fetch_all
|
||||
*
|
||||
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
||||
* @return array<mixed>|bool data array or false for end/error
|
||||
*/
|
||||
public function __dbFetchAll($cursor)
|
||||
{
|
||||
if ($cursor === false || is_bool($cursor)) {
|
||||
return false;
|
||||
}
|
||||
return pg_fetch_all($cursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_affected_rows
|
||||
*
|
||||
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
|
||||
* @return int affected rows, 0 for none, -1 for error
|
||||
*/
|
||||
public function __dbAffectedRows($cursor): int
|
||||
{
|
||||
if ($cursor === false || is_bool($cursor)) {
|
||||
return -1;
|
||||
}
|
||||
return pg_affected_rows($cursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* reads the last inserted primary key for the query
|
||||
* if there is no pk_name tries to auto built it from the table name
|
||||
* this only works if db schema is after no plural names
|
||||
* and pk name is table name + _id
|
||||
*
|
||||
* detects schema prefix in table name
|
||||
* @param string $query query string
|
||||
* @param string|null $pk_name primary key name, if '' then auto detect
|
||||
* @return string|int|false primary key value
|
||||
*/
|
||||
public function __dbInsertId(string $query, ?string $pk_name)
|
||||
{
|
||||
// only if an insert has been done
|
||||
if (preg_match("/^insert /i", $query)) {
|
||||
$schema = '';
|
||||
// get table name from insert
|
||||
$array = explode(' ', $query);
|
||||
$_table = $array[2];
|
||||
// if there is a dot inside, we need to split
|
||||
if (strstr($_table, '.')) {
|
||||
list($schema, $table) = explode('.', $_table);
|
||||
} else {
|
||||
$table = $_table;
|
||||
}
|
||||
// no PK name given at all
|
||||
if (empty($pk_name)) {
|
||||
// if name is plurar, make it singular
|
||||
// if (preg_match("/.*s$/i", $table))
|
||||
// $table = substr($table, 0, -1);
|
||||
// set pk_name to "id"
|
||||
$pk_name = $table . "_id";
|
||||
}
|
||||
$seq = ($schema ? $schema . '.' : '') . $table . "_" . $pk_name . "_seq";
|
||||
$q = "SELECT CURRVAL('$seq') AS insert_id";
|
||||
// I have to do manually or I overwrite the original insert internal vars ...
|
||||
if ($q = $this->__dbQuery($q)) {
|
||||
if (is_array($res = $this->__dbFetchArray($q))) {
|
||||
list($id) = $res;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$id = [-1, $q];
|
||||
}
|
||||
return $id;
|
||||
} else {
|
||||
//if not insert, return false
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* queries database for the primary key name to this table in the selected schema
|
||||
*
|
||||
* @param string $table table name
|
||||
* @param string $schema optional schema name, '' for default
|
||||
* @return string|bool primary key name or false if not found
|
||||
*/
|
||||
public function __dbPrimaryKey(string $table, string $schema = '')
|
||||
{
|
||||
if ($table) {
|
||||
// check if schema set is different from schema given,
|
||||
// only needed if schema is not empty
|
||||
$table_prefix = '';
|
||||
if ($schema) {
|
||||
$search_path = $this->__dbGetSchema();
|
||||
if ($search_path != $schema) {
|
||||
$table_prefix = $schema . '.';
|
||||
}
|
||||
}
|
||||
// read from table the PK name
|
||||
// faster primary key get
|
||||
$q = "SELECT pg_attribute.attname AS column_name, "
|
||||
. "format_type(pg_attribute.atttypid, pg_attribute.atttypmod) AS type "
|
||||
. "FROM pg_index, pg_class, pg_attribute ";
|
||||
if ($schema) {
|
||||
$q .= ", pg_namespace ";
|
||||
}
|
||||
$q .= "WHERE "
|
||||
// regclass translates the OID to the name
|
||||
. "pg_class.oid = '" . $table_prefix . $table . "'::regclass AND "
|
||||
. "indrelid = pg_class.oid AND ";
|
||||
if ($schema) {
|
||||
$q .= "nspname = '" . $schema . "' AND "
|
||||
. "pg_class.relnamespace = pg_namespace.oid AND ";
|
||||
}
|
||||
$q .= "pg_attribute.attrelid = pg_class.oid AND "
|
||||
. "pg_attribute.attnum = any(pg_index.indkey) "
|
||||
. "AND indisprimary";
|
||||
$cursor = $this->__dbQuery($q);
|
||||
if ($cursor !== false) {
|
||||
$__db_fetch_array = $this->__dbFetchArray($cursor);
|
||||
if (!is_array($__db_fetch_array)) {
|
||||
return false;
|
||||
}
|
||||
return $__db_fetch_array['column_name'] ?? false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_connect, writes out failure to screen if error occurs (hidden var)
|
||||
*
|
||||
* @param string $db_host host name
|
||||
* @param string $db_user user name
|
||||
* @param string $db_pass password
|
||||
* @param string $db_name databse name
|
||||
* @param integer $db_port port (int, 5432 is default)
|
||||
* @param string $db_ssl SSL (allow is default)
|
||||
* @return object|resource|bool db handler PgSql\Connection or false on error
|
||||
*/
|
||||
public function __dbConnect(
|
||||
string $db_host,
|
||||
string $db_user,
|
||||
string $db_pass,
|
||||
string $db_name,
|
||||
int $db_port,
|
||||
string $db_ssl = 'allow'
|
||||
) {
|
||||
if (empty($db_name)) {
|
||||
return false;
|
||||
}
|
||||
// if there is no host, leave it empty, this will try default unix path
|
||||
// same for port (defaults to 5432 if not set)
|
||||
// must set is db name
|
||||
// if no user name, db name is used
|
||||
$connection_string = [];
|
||||
if (!empty($db_host)) {
|
||||
$connection_string[] = 'host=' . $db_host;
|
||||
}
|
||||
if (!empty($db_port)) {
|
||||
$connection_string[] = 'port=' . $db_port;
|
||||
}
|
||||
if (!empty($db_user)) {
|
||||
$connection_string[] = 'user=' . $db_user;
|
||||
}
|
||||
if (!empty($db_pass)) {
|
||||
$connection_string[] = 'password=' . $db_pass;
|
||||
}
|
||||
// we must have at least db name set
|
||||
$connection_string[] = 'dbname=' . $db_name;
|
||||
if (!empty($db_ssl)) {
|
||||
$connection_string[] = 'sslmode=' . $db_ssl;
|
||||
}
|
||||
// connect
|
||||
$this->dbh = pg_connect(join(' ', $connection_string));
|
||||
return $this->dbh;
|
||||
}
|
||||
|
||||
/**
|
||||
* reads the last error for this cursor and returns
|
||||
* html formatted string with error name
|
||||
*
|
||||
* @param bool|object|resource $cursor cursor PgSql\Result (former resource)
|
||||
* or null
|
||||
* @return string error string
|
||||
*/
|
||||
public function __dbPrintError($cursor = false): string
|
||||
{
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return '';
|
||||
}
|
||||
// run the query again for the error result here
|
||||
if (($cursor === false || is_bool($cursor)) && $this->last_error_query) {
|
||||
pg_send_query($this->dbh, $this->last_error_query);
|
||||
$this->last_error_query = '';
|
||||
$cursor = pg_get_result($this->dbh);
|
||||
}
|
||||
if ($cursor && !is_bool($cursor) && $error_str = pg_result_error($cursor)) {
|
||||
return '-PostgreSQL-Error- '
|
||||
. $error_str;
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_meta_data
|
||||
*
|
||||
* @param string $table table name
|
||||
* @param bool $extended show extended info (default true)
|
||||
* @return array<mixed>|bool array data for the table info or false on error
|
||||
*/
|
||||
public function __dbMetaData(string $table, $extended = true)
|
||||
{
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return false;
|
||||
}
|
||||
// needs to prefixed with @ or it throws a warning on not existing table
|
||||
return @pg_meta_data($this->dbh, $table, $extended);
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_escape_string
|
||||
*
|
||||
* @param string|int|float|bool $string any string/int/float/bool
|
||||
* @return string excaped string
|
||||
*/
|
||||
public function __dbEscapeString($string): string
|
||||
{
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return '';
|
||||
}
|
||||
return pg_escape_string($this->dbh, (string)$string);
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_escape_literal
|
||||
* difference to escape string is that this one adds quotes ('') around
|
||||
* the string too
|
||||
*
|
||||
* @param string|int|float|bool $string any string/int/float/bool
|
||||
* @return string excaped string including quites
|
||||
*/
|
||||
public function __dbEscapeLiteral($string): string
|
||||
{
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return (string)'';
|
||||
}
|
||||
// for phpstan, thinks this is string|false?
|
||||
return (string)pg_escape_literal($this->dbh, (string)$string);
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_escape_identifier
|
||||
* Only used for table names, column names
|
||||
*
|
||||
* @param string $string any string
|
||||
* @return string escaped string
|
||||
*/
|
||||
public function __dbEscapeIdentifier(string $string): string
|
||||
{
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return '';
|
||||
}
|
||||
// for phpstan, thinks this is string|false?
|
||||
return (string)pg_escape_identifier($this->dbh, (string)$string);
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_escape_byte
|
||||
*
|
||||
* @param string $data data stream
|
||||
* @return string escaped bytea string
|
||||
*/
|
||||
public function __dbEscapeBytea(string $data): string
|
||||
{
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return '';
|
||||
}
|
||||
return pg_escape_bytea($this->dbh, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* unescape bytea data from postgesql
|
||||
*
|
||||
* @param string $bytea Bytea data stream
|
||||
* @return string Unescaped bytea data
|
||||
*/
|
||||
public function __dbUnescapeBytea(string $bytea): string
|
||||
{
|
||||
return pg_unescape_bytea($bytea);
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_connection_busy
|
||||
*
|
||||
* @return bool True if connection is busy, False if not or no db connection at all
|
||||
*/
|
||||
public function __dbConnectionBusy(): bool
|
||||
{
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return false;
|
||||
}
|
||||
return pg_connection_busy($this->dbh);
|
||||
}
|
||||
|
||||
/**
|
||||
* Experimental wrapper with scoket timetout
|
||||
*
|
||||
* @param integer $timeout_seconds Wait how many seconds on timeout
|
||||
* @return boolean True if connection is busy, or false on
|
||||
* not busy or no db connection at all
|
||||
*/
|
||||
public function __dbConnectionBusySocketWait(int $timeout_seconds = 3): bool
|
||||
{
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return false;
|
||||
}
|
||||
$busy = pg_connection_busy($this->dbh);
|
||||
/** @var array<resource>|null */
|
||||
$socket = [pg_socket($this->dbh)];
|
||||
$null = null;
|
||||
while ($busy) {
|
||||
// Will wait on that socket until that happens or the timeout is reached
|
||||
stream_select($socket, $null, $null, $timeout_seconds);
|
||||
$busy = pg_connection_busy($this->dbh);
|
||||
}
|
||||
return $busy;
|
||||
}
|
||||
|
||||
/**
|
||||
* extended wrapper for pg_version
|
||||
* can return any setting in this array block
|
||||
* If no connection, return empty string,
|
||||
* if not in array return empty string
|
||||
* On default 'version' will be stripped of any space attached info
|
||||
* eg 13.5 (other info) will return only 13.5
|
||||
*
|
||||
* @param string $parameter Parameter string to extract from array
|
||||
* @param boolean $strip If parameter is server strip out on default
|
||||
* Set to false to get original string AS is
|
||||
* @return string The parameter value
|
||||
*/
|
||||
public function __dbVersionInfo(string $parameter, bool $strip = true): string
|
||||
{
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return '';
|
||||
}
|
||||
// extract element
|
||||
$return_string = (string)(pg_version($this->dbh)[$parameter] ?? '');
|
||||
// for version, strip if requested
|
||||
if (
|
||||
in_array($parameter, ['server']) &&
|
||||
$strip === true
|
||||
) {
|
||||
$return_string = explode(' ', $return_string, 2)[0] ?? '';
|
||||
}
|
||||
return $return_string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all parameters that are possible from the db_version
|
||||
*
|
||||
* @return array<mixed> Parameter key names from pg_version
|
||||
*/
|
||||
public function __dbVersionInfoParameterList(): array
|
||||
{
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return [];
|
||||
}
|
||||
return array_keys(pg_version($this->dbh));
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for pg_version
|
||||
* Note: this only returns server version
|
||||
* not connection version OR client version
|
||||
*
|
||||
* @return string version string
|
||||
*/
|
||||
public function __dbVersion(): string
|
||||
{
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return '';
|
||||
}
|
||||
// array has client, protocol, server, we just return server stripped
|
||||
return $this->__dbVersionInfo('server', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a numeric version eg 90506 or 130006, etc
|
||||
* Note that this calls a show command on the server
|
||||
* Note:
|
||||
* Old version is 9.5.6 where 9.5 is the major version
|
||||
* Newer Postgresql (10 on) have only one major version so eg 13.5
|
||||
* is returned as 130005
|
||||
*
|
||||
* @return integer Server version
|
||||
*/
|
||||
public function __dbVersionNumeric(): int
|
||||
{
|
||||
return (int)$this->__dbShow('server_version_num');
|
||||
}
|
||||
|
||||
/**
|
||||
* NOTE: it is recommended to convert arrays to json and parse the json in
|
||||
* PHP itself, array_to_json(array)
|
||||
* This is a fallback for old PostgreSQL versions
|
||||
* postgresql array to php array
|
||||
* https://stackoverflow.com/a/27964420
|
||||
*
|
||||
* @param string $array_text Array text from PostgreSQL
|
||||
* @param int $start Start string position
|
||||
* @param int|null $end End string position from recursive call
|
||||
* @return array<mixed>|null PHP type array
|
||||
*/
|
||||
public function __dbArrayParse(
|
||||
string $array_text,
|
||||
int $start = 0,
|
||||
?int &$end = null
|
||||
): ?array {
|
||||
if (empty($array_text) || $array_text[0] != '{') {
|
||||
return null;
|
||||
}
|
||||
$return = [];
|
||||
$string = false;
|
||||
$quote = '';
|
||||
$len = strlen($array_text);
|
||||
$v = '';
|
||||
// start from offset
|
||||
for ($array_pos = $start + 1; $array_pos < $len; $array_pos += 1) {
|
||||
$ch = $array_text[$array_pos];
|
||||
// check wher ein the string are we
|
||||
// end, one down
|
||||
if (!$string && $ch == '}') {
|
||||
if ($v !== '' || !empty($return)) {
|
||||
$return[] = $v;
|
||||
}
|
||||
$end = $array_pos;
|
||||
break;
|
||||
// open new array, jump recusrive up
|
||||
} elseif (!$string && $ch == '{') {
|
||||
// full string + poff set and end
|
||||
$v = $this->__dbArrayParse($array_text, $array_pos, $array_pos);
|
||||
// next array element
|
||||
} elseif (!$string && $ch == ',') {
|
||||
$return[] = $v;
|
||||
$v = '';
|
||||
// flag that this is a string
|
||||
} elseif (!$string && ($ch == '"' || $ch == "'")) {
|
||||
$string = true;
|
||||
$quote = $ch;
|
||||
// quoted string
|
||||
} elseif ($string && $ch == $quote && $array_text[$array_pos - 1] == "\\") {
|
||||
$v = substr($v, 0, -1) . $ch;
|
||||
} elseif ($string && $ch == $quote && $array_text[$array_pos - 1] != "\\") {
|
||||
$string = false;
|
||||
} else {
|
||||
// build string
|
||||
$v .= $ch;
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns any server setting
|
||||
* if no connection or empty parameter or other error returns false
|
||||
* else returns a string
|
||||
*
|
||||
* @param string $parameter Parameter to query
|
||||
* @return string|bool Settings value as string
|
||||
*/
|
||||
public function __dbParameter(string $parameter)
|
||||
{
|
||||
if ($this->dbh === false || is_bool($this->dbh)) {
|
||||
return false;
|
||||
}
|
||||
if (empty($parameter)) {
|
||||
return false;
|
||||
}
|
||||
return pg_parameter_status($this->dbh, $parameter);
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for any SHOW data blocks
|
||||
* eg search_path or client_encoding
|
||||
*
|
||||
* @param string $show_string Part to show, if invalid will return empty string
|
||||
* @return string Found part as is
|
||||
*/
|
||||
public function __dbShow(string $show_string): string
|
||||
{
|
||||
// get search path
|
||||
$cursor = $this->__dbQuery("SHOW " . $this->__dbEscapeIdentifier($show_string));
|
||||
// abort on failure and return empty
|
||||
if ($cursor === false) {
|
||||
return '';
|
||||
}
|
||||
// get result
|
||||
$db_schema = $this->__dbFetchArray($cursor, PGSQL_ASSOC);
|
||||
return $db_schema[$show_string] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a new database schema/search_path
|
||||
* Checks if schema exits and if not aborts with error code 2
|
||||
*
|
||||
* @param string $db_schema Schema to set
|
||||
* @return int Returns 0 if no error
|
||||
* 1 for check query failed
|
||||
* 2 for schema not found in database
|
||||
*/
|
||||
public function __dbSetSchema(string $db_schema): int
|
||||
{
|
||||
// check if schema actually exists
|
||||
$query = "SELECT EXISTS("
|
||||
. "SELECT 1 FROM information_schema.schemata "
|
||||
. "WHERE schema_name = " . $this->__dbEscapeLiteral($db_schema)
|
||||
. ")";
|
||||
$cursor = $this->__dbQuery($query);
|
||||
// abort if execution fails
|
||||
if ($cursor === false) {
|
||||
return 1;
|
||||
}
|
||||
// check if schema does not exists
|
||||
$row = $this->__dbFetchArray($cursor, PGSQL_ASSOC);
|
||||
if (empty($row['exists']) || $row['exists'] == 'f') {
|
||||
return 2;
|
||||
}
|
||||
$query = "SET search_path TO " . $this->__dbEscapeLiteral($db_schema);
|
||||
$this->__dbQuery($query);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns current set schema/search_path
|
||||
*
|
||||
* @return string Search Path as currently set in DB
|
||||
*/
|
||||
public function __dbGetSchema(): string
|
||||
{
|
||||
return $this->__dbShow('search_path');
|
||||
}
|
||||
|
||||
/**
|
||||
* set the client encoding
|
||||
* Returns 0 on set ok, or 3 if the client encoding could not be set
|
||||
*
|
||||
* @param string $db_encoding
|
||||
* @return int Returns 0 for no error
|
||||
* [not used] 1 for check query failed
|
||||
* [not used] 2 for invalid client encoding
|
||||
* 3 client encoding could not be set
|
||||
*/
|
||||
public function __dbSetEncoding(string $db_encoding): int
|
||||
{
|
||||
// check if ecnoding is valid first
|
||||
// does not take into account aliases
|
||||
// TODO lookup with alisaes so eg ShiftJIS does not get a false
|
||||
// $query = "SELECT EXISTS("
|
||||
// . "SELECT pg_catalog.pg_encoding_to_char(conforencoding) "
|
||||
// . "FROM pg_catalog.pg_conversion "
|
||||
// . "WHERE pg_catalog.pg_encoding_to_char(conforencoding) = "
|
||||
// . $this->dbEscapeLiteral($db_encoding)
|
||||
// . ")";
|
||||
// $cursor = $this->__dbQuery($query);
|
||||
// if ($cursor === false) {
|
||||
// return 1;
|
||||
// }
|
||||
// $row = $this->__dbFetchArray($cursor, PGSQL_ASSOC);
|
||||
// if ($row['exists'] == 'f') {
|
||||
// return 2;
|
||||
// }
|
||||
$query = "SET client_encoding TO " . $this->__dbEscapeLiteral($db_encoding);
|
||||
if ($this->__dbQuery($query) === false) {
|
||||
return 3;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns current set client encoding
|
||||
* @return string Client encoding string, empty if not set
|
||||
*/
|
||||
public function __dbGetEncoding(): string
|
||||
{
|
||||
return $this->__dbShow('client_encoding');
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
305
4dev/deprecated/CoreLibs/DB/SQL/SqlInterface/SqlFunctions.php
Normal file
305
4dev/deprecated/CoreLibs/DB/SQL/SqlInterface/SqlFunctions.php
Normal file
@@ -0,0 +1,305 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Intrface for all SQL\* functions
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\DB\SQL\SqlInterface;
|
||||
|
||||
interface SqlFunctions
|
||||
{
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function __dbLastErrorQuery(): bool;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $query
|
||||
* @return object|resource|bool
|
||||
*/
|
||||
public function __dbQuery(string $query);
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $query
|
||||
* @param array<mixed> $params
|
||||
* @return object|resource|bool
|
||||
*/
|
||||
public function __dbQueryParams(string $query, array $params);
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $query
|
||||
* @return boolean
|
||||
*/
|
||||
public function __dbSendQuery(string $query): bool;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return object|resource|bool
|
||||
*/
|
||||
public function __dbGetResult();
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __dbClose(): void;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $query
|
||||
* @return object|resource|bool
|
||||
*/
|
||||
public function __dbPrepare(string $name, string $query);
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $name
|
||||
* @param array<mixed> $data
|
||||
* @return object|resource|bool
|
||||
*/
|
||||
public function __dbExecute(string $name, array $data);
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param object|resource|bool $cursor
|
||||
* @return integer
|
||||
*/
|
||||
public function __dbNumRows($cursor): int;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param object|resource|bool $cursor
|
||||
* @return integer
|
||||
*/
|
||||
public function __dbNumFields($cursor): int;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param object|resource|bool $cursor
|
||||
* @param int $i
|
||||
* @return string|bool
|
||||
*/
|
||||
public function __dbFieldName($cursor, int $i);
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param object|resource|bool $cursor
|
||||
* @param int $result_type
|
||||
* @return array<mixed>|bool
|
||||
*/
|
||||
public function __dbFetchArray($cursor, int $result_type = PGSQL_BOTH);
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param boolean $assoc_type
|
||||
* @return integer
|
||||
*/
|
||||
public function __dbResultType(bool $assoc_type = true): int;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param object|resource|bool $cursor
|
||||
* @return array<mixed>|bool
|
||||
*/
|
||||
public function __dbFetchAll($cursor);
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param object|resource|bool $cursor
|
||||
* @return integer
|
||||
*/
|
||||
public function __dbAffectedRows($cursor): int;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $query
|
||||
* @param string|null $pk_name
|
||||
* @return string|integer|false
|
||||
*/
|
||||
public function __dbInsertId(string $query, ?string $pk_name);
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $table
|
||||
* @param string $schema
|
||||
* @return string|bool
|
||||
*/
|
||||
public function __dbPrimaryKey(string $table, string $schema = '');
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $db_host
|
||||
* @param string $db_user
|
||||
* @param string $db_pass
|
||||
* @param string $db_name
|
||||
* @param integer $db_port
|
||||
* @param string $db_ssl
|
||||
* @return object|resource|bool
|
||||
*/
|
||||
public function __dbConnect(
|
||||
string $db_host,
|
||||
string $db_user,
|
||||
string $db_pass,
|
||||
string $db_name,
|
||||
int $db_port,
|
||||
string $db_ssl = 'allow'
|
||||
);
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param object|resource|bool $cursor
|
||||
* @return string
|
||||
*/
|
||||
public function __dbPrintError($cursor = false): string;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $table
|
||||
* @param boolean $extended
|
||||
* @return array<mixed>|bool
|
||||
*/
|
||||
public function __dbMetaData(string $table, $extended = true);
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string|int|float|bool $string
|
||||
* @return string
|
||||
*/
|
||||
public function __dbEscapeString($string): string;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string|int|float|bool $string
|
||||
* @return string
|
||||
*/
|
||||
public function __dbEscapeLiteral($string): string;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
public function __dbEscapeIdentifier(string $string): string;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $data
|
||||
* @return string
|
||||
*/
|
||||
public function __dbEscapeBytea(string $data): string;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $bytea
|
||||
* @return string
|
||||
*/
|
||||
public function __dbUnescapeBytea(string $bytea): string;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function __dbConnectionBusy(): bool;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param integer $timeout_seconds
|
||||
* @return boolean
|
||||
*/
|
||||
public function __dbConnectionBusySocketWait(int $timeout_seconds = 3): bool;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __dbVersion(): string;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $array_text
|
||||
* @param integer $start
|
||||
* @param integer|null $end
|
||||
* @return array<mixed>|null
|
||||
*/
|
||||
public function __dbArrayParse(
|
||||
string $array_text,
|
||||
int $start = 0,
|
||||
?int &$end = null
|
||||
): ?array;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $show_string
|
||||
* @return string
|
||||
*/
|
||||
public function __dbShow(string $show_string): string;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $db_schema
|
||||
* @return integer
|
||||
*/
|
||||
public function __dbSetSchema(string $db_schema): int;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __dbGetSchema(): string;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $db_encoding
|
||||
* @return integer
|
||||
*/
|
||||
public function __dbSetEncoding(string $db_encoding): int;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __dbGetEncoding(): string;
|
||||
}
|
||||
|
||||
// __END__
|
||||
99
4dev/deprecated/CoreLibs/Debug/FileWriter.php
Normal file
99
4dev/deprecated/CoreLibs/Debug/FileWriter.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* direct write to log file
|
||||
* must have BASE folder and LOG foder defined
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Debug;
|
||||
|
||||
class FileWriter
|
||||
{
|
||||
/** @var string */
|
||||
private static $debug_filename = 'debug_file.log'; // where to write output
|
||||
/** @var string */
|
||||
private static $debug_folder;
|
||||
|
||||
/**
|
||||
* Set a debug log folder, if not set BASE+LOG folders are set
|
||||
* if they are defined
|
||||
* This folder name must exist and must be writeable
|
||||
*
|
||||
* @param string $folder Folder name to where the log file will be written
|
||||
* @return boolean True for valid folder name, False for invalid
|
||||
*/
|
||||
public static function fsetFolder(string $folder): bool
|
||||
{
|
||||
if (!preg_match("/^[\w\-\/]+/", $folder)) {
|
||||
return false;
|
||||
}
|
||||
if (!is_writeable($folder)) {
|
||||
return false;
|
||||
}
|
||||
// if last is not / then add
|
||||
if (substr($folder, -1, 1) != DIRECTORY_SEPARATOR) {
|
||||
$folder .= DIRECTORY_SEPARATOR;
|
||||
}
|
||||
self::$debug_folder = $folder;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* set new debug file name
|
||||
* Must be alphanumeric and underscore only.
|
||||
* Must end with .log
|
||||
*
|
||||
* @param string $filename File name to set
|
||||
* @return bool True for valid file name, False if invalid
|
||||
*/
|
||||
public static function fsetFilename(string $filename): bool
|
||||
{
|
||||
// valid file. must be only ascii & _, must end with .log
|
||||
if (!preg_match("/^[\w\-]+\.log$/", $filename)) {
|
||||
return false;
|
||||
}
|
||||
self::$debug_filename = $filename;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* writes a string to a file immediatly, for fast debug output
|
||||
*
|
||||
* @param string $string string to write to the file
|
||||
* @param boolean $enter default true, if set adds a linebreak \n at the end
|
||||
* @return bool True for log written, false for not wirrten
|
||||
*/
|
||||
public static function fdebug(string $string, bool $enter = true): bool
|
||||
{
|
||||
if (empty(self::$debug_filename)) {
|
||||
return false;
|
||||
}
|
||||
// if empty try to set base log folder
|
||||
if (
|
||||
empty(self::$debug_folder) &&
|
||||
defined('BASE') && defined('LOG')
|
||||
) {
|
||||
self::$debug_folder = BASE . LOG;
|
||||
}
|
||||
if (!is_writeable(self::$debug_folder)) {
|
||||
return false;
|
||||
}
|
||||
$filename = self::$debug_folder . self::$debug_filename;
|
||||
$fh = fopen($filename, 'a');
|
||||
if ($fh === false) {
|
||||
return false;
|
||||
}
|
||||
if ($enter === true) {
|
||||
$string .= "\n";
|
||||
}
|
||||
$string = "[" . \CoreLibs\Debug\Support::printTime() . "] "
|
||||
. "[" . \CoreLibs\Get\System::getPageName(2) . "] - " . $string;
|
||||
fwrite($fh, $string);
|
||||
fclose($fh);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
881
4dev/deprecated/CoreLibs/Debug/Logging.php
Normal file
881
4dev/deprecated/CoreLibs/Debug/Logging.php
Normal file
@@ -0,0 +1,881 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Debug support functions
|
||||
*
|
||||
* These are if there is any debug to print out at all at the end
|
||||
* debug_output_all - general yes no
|
||||
* It's recommended to use the method "debug_for" to turn on of the array vars
|
||||
* debug_output - turn on for one level (Array)
|
||||
* debug_output_not - turn off for one level (array)
|
||||
*
|
||||
* Print out the debug at thend of the html
|
||||
* echo_output_all
|
||||
* echo_output
|
||||
* echo_output_not
|
||||
*
|
||||
* Write debug to file
|
||||
* print_output_all
|
||||
* print_output
|
||||
* print_output_not
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Debug;
|
||||
|
||||
use CoreLibs\Debug\Support;
|
||||
use CoreLibs\Create\Uids;
|
||||
use CoreLibs\Get\System;
|
||||
use CoreLibs\Convert\Html;
|
||||
|
||||
class Logging
|
||||
{
|
||||
// options
|
||||
/** @var array<mixed> */
|
||||
private $options = [];
|
||||
// page and host name
|
||||
/** @var string */
|
||||
private $page_name;
|
||||
/** @var string */
|
||||
private $host_name;
|
||||
/** @var int */
|
||||
private $host_port;
|
||||
// internal error reporting vars
|
||||
/** @var array<mixed> */
|
||||
private $error_msg = []; // the "connection" to the outside errors
|
||||
// debug output prefix
|
||||
/** @var string */
|
||||
private $error_msg_prefix = ''; // prefix to the error string (the class name)
|
||||
// debug flags
|
||||
/** @var array<mixed> */
|
||||
private $debug_output = []; // if this is true, show debug on desconstructor
|
||||
/** @var array<mixed> */
|
||||
private $debug_output_not = [];
|
||||
/** @var bool */
|
||||
private $debug_output_all = false;
|
||||
/** @var array<mixed> */
|
||||
private $echo_output = []; // errors: echo out, default is 1
|
||||
/** @var array<mixed> */
|
||||
private $echo_output_not = [];
|
||||
/** @var bool */
|
||||
private $echo_output_all = false;
|
||||
/** @var array<mixed> */
|
||||
private $print_output = []; // errors: print to file, default is 0
|
||||
/** @var array<mixed> */
|
||||
private $print_output_not = [];
|
||||
/** @var bool */
|
||||
private $print_output_all = false;
|
||||
// debug flags/settings
|
||||
/** @var string */
|
||||
private $running_uid = ''; // unique ID set on class init and used in logging as prefix
|
||||
// log file name
|
||||
/** @var string */
|
||||
private $log_folder = '';
|
||||
/** @var string */
|
||||
private $log_file_name_ext = 'log'; // use this for date rotate
|
||||
/** @var string */
|
||||
private $log_file_name = '';
|
||||
/** @var int */
|
||||
private $log_max_filesize = 0; // set in kilobytes
|
||||
/** @var string */
|
||||
private $log_print_file = 'error_msg##LOGID####LEVEL####CLASS####PAGENAME####DATE##';
|
||||
/** @var string */
|
||||
private $log_file_unique_id; // a unique ID set only once for call derived from this class
|
||||
/** @var string */
|
||||
private $log_file_date = ''; // Y-m-d file in file name
|
||||
/** @var bool */
|
||||
private $log_print_file_date = true; // if set add Y-m-d and do automatic daily rotation
|
||||
/** @var string */
|
||||
private $log_file_id = ''; // a alphanumeric name that has to be set as global definition
|
||||
/** @var bool */
|
||||
private $log_per_level = false; // set, it will split per level (first parameter in debug call)
|
||||
/** @var bool */
|
||||
private $log_per_class = false; // set, will split log per class
|
||||
/** @var bool */
|
||||
private $log_per_page = false; // set, will split log per called file
|
||||
/** @var bool */
|
||||
private $log_per_run = false; // create a new log file per run (time stamp + unique ID)
|
||||
// script running time
|
||||
/** @var float */
|
||||
private $script_starttime;
|
||||
|
||||
/**
|
||||
* Init logger
|
||||
*
|
||||
* global vars that can be used
|
||||
* - BASE
|
||||
* - LOG
|
||||
* - LOG_FILE_ID
|
||||
* options array layout
|
||||
* - log_folder:
|
||||
* - print_file_date:
|
||||
* - file_id:
|
||||
* - unique_id:
|
||||
* - log_per_level:
|
||||
* - log_per_class:
|
||||
* - log_per_page:
|
||||
* - log_per_run:
|
||||
* - debug_all:
|
||||
* - echo_all:
|
||||
* - print_all:
|
||||
* - debug (array):
|
||||
* - echo (array):
|
||||
* - print (array):
|
||||
* - debug_not (array):
|
||||
* - echo_not (array):
|
||||
* - print_not (array):
|
||||
*
|
||||
* @param array<mixed> $options Array with settings options
|
||||
*/
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
// copy the options over
|
||||
$this->options = $options;
|
||||
// set log folder from options
|
||||
$this->log_folder = $this->options['log_folder'] ?? '';
|
||||
// legacy flow, check must set constants
|
||||
if (empty($this->log_folder) && defined('BASE') && defined('LOG')) {
|
||||
// make sure this is writeable, else skip
|
||||
$this->log_folder = BASE . LOG;
|
||||
}
|
||||
// fallback + notice
|
||||
if (empty($this->log_folder)) {
|
||||
/* trigger_error(
|
||||
'options or constant not set or folder not writable. fallback to: ' . getcwd(),
|
||||
E_USER_NOTICE
|
||||
); */
|
||||
$this->log_folder = getcwd() . DIRECTORY_SEPARATOR;
|
||||
}
|
||||
// if folder is not writeable, abort
|
||||
if (!is_writeable($this->log_folder)) {
|
||||
trigger_error(
|
||||
'Folder: ' . $this->log_folder . ' is not writeable for logging',
|
||||
E_USER_ERROR
|
||||
);
|
||||
}
|
||||
// check if log_folder has a trailing /
|
||||
if (substr($this->log_folder, -1, 1) != DIRECTORY_SEPARATOR) {
|
||||
$this->log_folder .= DIRECTORY_SEPARATOR;
|
||||
}
|
||||
// running time start for script
|
||||
$this->script_starttime = microtime(true);
|
||||
// set per run UID for logging
|
||||
$this->running_uid = Uids::uniqIdShort();
|
||||
// set the page name
|
||||
$this->page_name = System::getPageName();
|
||||
// set host name
|
||||
list($this->host_name , $this->host_port) = System::getHostName();
|
||||
// add port to host name if not port 80
|
||||
if ($this->host_port != 80) {
|
||||
$this->host_name .= ':' . $this->host_port;
|
||||
}
|
||||
|
||||
// can be overridden with basicSetLogFileId later
|
||||
if (!empty($this->options['file_id'])) {
|
||||
$this->setLogId($this->options['file_id']);
|
||||
} elseif (!empty($GLOBALS['LOG_FILE_ID'])) {
|
||||
// legacy flow, should be removed and only set via options
|
||||
$this->setLogId($GLOBALS['LOG_FILE_ID']);
|
||||
// TODO trigger deprecation error
|
||||
// trigger_error(
|
||||
// 'Debug\Logging: Do not use globals LOG_FILE_ID to set log id for Logging',
|
||||
// E_USER_DEPRECATED
|
||||
// );
|
||||
} elseif (defined('LOG_FILE_ID')) {
|
||||
// legacy flow, should be removed and only set via options
|
||||
$this->setLogId(LOG_FILE_ID);
|
||||
// trigger deprecation error
|
||||
// trigger_error(
|
||||
// 'Debug\Logging: Do not use constant LOG_FILE_ID to set log id for Logging',
|
||||
// E_USER_DEPRECATED
|
||||
// );
|
||||
}
|
||||
|
||||
// init the log levels
|
||||
$this->initLogLevels();
|
||||
}
|
||||
|
||||
// *** PRIVATE ***
|
||||
|
||||
/**
|
||||
* init the basic log levels based on global set variables
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function initLogLevels(): void
|
||||
{
|
||||
// if given via parameters, only for all
|
||||
// globals overrule given settings, for one (array), eg $ECHO['db'] = 1;
|
||||
foreach (['debug', 'echo', 'print'] as $type) {
|
||||
// include or exclude (off) from output
|
||||
foreach (['on', 'off'] as $flag) {
|
||||
$in_type = $type;
|
||||
if ($flag == 'off') {
|
||||
$in_type .= '_not';
|
||||
}
|
||||
$up_type = strtoupper($in_type);
|
||||
if (
|
||||
isset($this->options[$in_type]) &&
|
||||
is_array($this->options[$in_type])
|
||||
) {
|
||||
$this->setLogLevel($type, $flag, $this->options[$in_type]);
|
||||
} elseif (
|
||||
isset($GLOBALS[$up_type]) &&
|
||||
is_array($GLOBALS[$up_type])
|
||||
) {
|
||||
// TODO trigger deprecation error
|
||||
$this->setLogLevel($type, $flag, $GLOBALS[$up_type]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO remove all $GLOBALS call and only use options
|
||||
// all overrule
|
||||
$this->setLogLevelAll(
|
||||
'debug',
|
||||
$this->options['debug_all'] ??
|
||||
// for user login, should be handled outside like globals
|
||||
$_SESSION['DEBUG_ALL'] ??
|
||||
$GLOBALS['DEBUG_ALL'] ??
|
||||
false
|
||||
);
|
||||
$this->setLogLevelAll(
|
||||
'print',
|
||||
$this->options['print_all'] ??
|
||||
// for user login, should be handled outside like globals
|
||||
$_SESSION['DEBUG_ALL'] ??
|
||||
$GLOBALS['PRINT_ALL'] ??
|
||||
false
|
||||
);
|
||||
$this->setLogLevelAll(
|
||||
'echo',
|
||||
$this->options['echo_all'] ??
|
||||
$GLOBALS['ECHO_ALL'] ??
|
||||
false
|
||||
);
|
||||
|
||||
// GLOBAL rules for log writing
|
||||
// add file date is default on
|
||||
$this->setGetLogPrintFileDate(
|
||||
$this->options['print_file_date'] ??
|
||||
$GLOBALS['LOG_PRINT_FILE_DATE'] ??
|
||||
true
|
||||
);
|
||||
// all other logging file name flags are off
|
||||
$this->setLogPer(
|
||||
'level',
|
||||
$this->options['per_level'] ??
|
||||
$GLOBALS['LOG_PER_LEVEL'] ??
|
||||
false
|
||||
);
|
||||
$this->setLogPer(
|
||||
'class',
|
||||
$this->options['per_class'] ??
|
||||
$GLOBALS['LOG_PER_CLASS'] ??
|
||||
false
|
||||
);
|
||||
$this->setLogPer(
|
||||
'page',
|
||||
$this->options['per_page'] ??
|
||||
$GLOBALS['LOG_PER_PAGE'] ??
|
||||
false
|
||||
);
|
||||
$this->setLogPer(
|
||||
'run',
|
||||
$this->options['per_run'] ??
|
||||
$GLOBALS['LOG_PER_RUN'] ??
|
||||
false
|
||||
);
|
||||
// set log per date
|
||||
if ($this->setGetLogPrintFileDate()) {
|
||||
$this->log_file_date = date('Y-m-d');
|
||||
}
|
||||
// set per run ID
|
||||
if ($this->log_per_run) {
|
||||
$this->setLogUniqueId();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if we have a need to work on certain debug output
|
||||
* Needs debug/echo/print ad target for which of the debug flag groups we check
|
||||
* also needs level string to check in the per level output flag check.
|
||||
* In case we have invalid target it will return false
|
||||
*
|
||||
* @param string $target target group to check debug/echo/print
|
||||
* @param string $level level to check in detailed level flag
|
||||
* @return bool true on access allowed or false on no access
|
||||
*/
|
||||
private function doDebugTrigger(string $target, string $level): bool
|
||||
{
|
||||
$access = false;
|
||||
// check if we do debug, echo or print
|
||||
if (
|
||||
(
|
||||
$this->getLogLevel($target, 'on', $level) ||
|
||||
$this->getLogLevelAll($target)
|
||||
) &&
|
||||
!$this->getLogLevel($target, 'off', $level)
|
||||
) {
|
||||
$access = true;
|
||||
}
|
||||
return $access;
|
||||
}
|
||||
|
||||
/**
|
||||
* writes error msg data to file for current level
|
||||
*
|
||||
* @param string $level the level to write
|
||||
* @param string $error_string error string to write
|
||||
* @return bool True if message written, FAlse if not
|
||||
*/
|
||||
private function writeErrorMsg(string $level, string $error_string): bool
|
||||
{
|
||||
// only write if write is requested
|
||||
if (
|
||||
!($this->doDebugTrigger('debug', $level) &&
|
||||
$this->doDebugTrigger('print', $level))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// init base file path
|
||||
$fn = $this->log_folder . $this->log_print_file . '.' . $this->log_file_name_ext;
|
||||
// log ID prefix settings, if not valid, replace with empty
|
||||
if (!empty($this->log_file_id)) {
|
||||
$rpl_string = '_' . $this->log_file_id;
|
||||
} else {
|
||||
$rpl_string = '';
|
||||
}
|
||||
$fn = str_replace('##LOGID##', $rpl_string, $fn); // log id (like a log file prefix)
|
||||
|
||||
if ($this->log_per_run) {
|
||||
$rpl_string = '_' . $this->log_file_unique_id; // add 8 char unique string
|
||||
} elseif ($this->setGetLogPrintFileDate()) {
|
||||
$rpl_string = '_' . $this->log_file_date; // add date to file
|
||||
} else {
|
||||
$rpl_string = '';
|
||||
}
|
||||
$fn = str_replace('##DATE##', $rpl_string, $fn); // create output filename
|
||||
|
||||
// write per level
|
||||
$rpl_string = !$this->log_per_level ? '' :
|
||||
// normalize level, replace all non alphanumeric characters with -
|
||||
'_' . (
|
||||
// if return is only - then set error string
|
||||
preg_match(
|
||||
"/^-+$/",
|
||||
$level_string = preg_replace("/[^A-Za-z0-9-_]/", '-', $level) ?? ''
|
||||
) ?
|
||||
'INVALID-LEVEL-STRING' :
|
||||
$level_string
|
||||
);
|
||||
$fn = str_replace('##LEVEL##', $rpl_string, $fn); // create output filename
|
||||
// set per class, but don't use get_class as we will only get self
|
||||
$rpl_string = !$this->log_per_class ? '' : '_'
|
||||
// set sub class settings
|
||||
. str_replace('\\', '-', Support::getCallerClass());
|
||||
$fn = str_replace('##CLASS##', $rpl_string, $fn); // create output filename
|
||||
|
||||
// if request to write to one file
|
||||
$rpl_string = !$this->log_per_page ?
|
||||
'' :
|
||||
'_' . System::getPageName(System::NO_EXTENSION);
|
||||
$fn = str_replace('##PAGENAME##', $rpl_string, $fn); // create output filename
|
||||
|
||||
// write to file
|
||||
// first check if max file size is is set and file is bigger
|
||||
if (
|
||||
$this->log_max_filesize > 0 &&
|
||||
((filesize($fn) / 1024) > $this->log_max_filesize)
|
||||
) {
|
||||
// for easy purpose, rename file only to attach timestamp, nur sequence numbering
|
||||
rename($fn, $fn . '.' . date("YmdHis"));
|
||||
}
|
||||
$this->log_file_name = $fn;
|
||||
$fp = fopen($this->log_file_name, 'a');
|
||||
if ($fp !== false) {
|
||||
fwrite($fp, $error_string);
|
||||
fclose($fp);
|
||||
return true;
|
||||
} else {
|
||||
echo "<!-- could not open file: " . $this->log_file_name . " //-->";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// *** PUBLIC ***
|
||||
|
||||
/**
|
||||
* Temporary method to read all class variables for testing purpose
|
||||
*
|
||||
* @param string $name what variable to return
|
||||
* @return mixed can be anything, bool, string, int, array
|
||||
*/
|
||||
public function getSetting(string $name) //:mixed DOES not work with PHP 7.4
|
||||
{
|
||||
// for debug purpose only
|
||||
return $this->{$name};
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the internal log file prefix id
|
||||
* string must be a alphanumeric string
|
||||
* if non valid string is given it returns the previous set one only
|
||||
*
|
||||
* @param string $string log file id string value
|
||||
* @return string returns the set log file id string
|
||||
* @deprecated Use $log->setLogId()
|
||||
*/
|
||||
public function basicSetLogId(string $string): string
|
||||
{
|
||||
return $this->setLogId($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the internal log file prefix id
|
||||
* string must be a alphanumeric string
|
||||
* if non valid string is given it returns the previous set one only
|
||||
*
|
||||
* @param string $string log file id string value
|
||||
* @return string returns the set log file id string
|
||||
*/
|
||||
public function setLogId(string $string): string
|
||||
{
|
||||
if (preg_match("/^[\w\-]+$/", $string)) {
|
||||
$this->log_file_id = $string;
|
||||
}
|
||||
return $this->log_file_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* return current set log file id
|
||||
* @return string
|
||||
*/
|
||||
public function getLogId(): string
|
||||
{
|
||||
return $this->log_file_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* old name for setLogLevel
|
||||
*
|
||||
* @param string $type debug, echo, print
|
||||
* @param string $flag on/off
|
||||
* array $array of levels to turn on/off debug
|
||||
* @return bool Return false if type or flag is invalid
|
||||
* @deprecated Use setLogLevel
|
||||
*/
|
||||
public function debugFor(string $type, string $flag): bool
|
||||
{
|
||||
/** @phan-suppress-next-line PhanTypeMismatchArgumentReal, PhanParamTooFew @phpstan-ignore-next-line */
|
||||
return $this->setLogLevel(...[func_get_args()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* set log level settings for All types
|
||||
* if invalid type, skip
|
||||
*
|
||||
* @param string $type Type to get: debug, echo, print
|
||||
* @param bool $set True or False
|
||||
* @return bool Return false if type invalid
|
||||
*/
|
||||
public function setLogLevelAll(string $type, bool $set): bool
|
||||
{
|
||||
// skip set if not valid
|
||||
if (!in_array($type, ['debug', 'echo', 'print'])) {
|
||||
return false;
|
||||
}
|
||||
$this->{$type . '_output_all'} = $set;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the current log level setting for All level blocks
|
||||
*
|
||||
* @param string $type Type to get: debug, echo, print
|
||||
* @return bool False on failure, or the boolean flag from the all var
|
||||
*/
|
||||
public function getLogLevelAll(string $type): bool
|
||||
{
|
||||
// type check for debug/echo/print
|
||||
if (!in_array($type, ['debug', 'echo', 'print'])) {
|
||||
return false;
|
||||
}
|
||||
return $this->{$type . '_output_all'};
|
||||
}
|
||||
|
||||
/**
|
||||
* passes list of level names, to turn on debug
|
||||
* eg $foo->debugFor('print', 'on', ['LOG', 'DEBUG', 'INFO']);
|
||||
*
|
||||
* @param string $type debug, echo, print
|
||||
* @param string $flag on/off
|
||||
* @param array<mixed> $debug_on Array of levels to turn on/off debug
|
||||
* To turn off a level set 'Level' => false,
|
||||
* If not set, switches to on
|
||||
* @return bool Return false if type or flag invalid
|
||||
* also false if debug array is empty
|
||||
*/
|
||||
public function setLogLevel(string $type, string $flag, array $debug_on): bool
|
||||
{
|
||||
// abort if not valid type
|
||||
if (!in_array($type, ['debug', 'echo', 'print'])) {
|
||||
return false;
|
||||
}
|
||||
// invalid flag type
|
||||
if (!in_array($flag, ['on', 'off'])) {
|
||||
return false;
|
||||
}
|
||||
if (count($debug_on) >= 1) {
|
||||
foreach ($debug_on as $level => $set) {
|
||||
$switch = $type . '_output' . ($flag == 'off' ? '_not' : '');
|
||||
if (!is_bool($set)) {
|
||||
$level = $set;
|
||||
$set = true;
|
||||
}
|
||||
$this->{$switch}[$level] = $set;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* return the log level for the array type normal and not (disable)
|
||||
*
|
||||
* @param string $type debug, echo, print
|
||||
* @param string $flag on/off
|
||||
* @param string|null $level if not null then check if this array entry is set
|
||||
* else return false
|
||||
* @return bool|array<mixed> if $level is null, return array, else boolean true/false
|
||||
*/
|
||||
public function getLogLevel(string $type, string $flag, ?string $level = null)
|
||||
{
|
||||
// abort if not valid type
|
||||
if (!in_array($type, ['debug', 'echo', 'print'])) {
|
||||
return false;
|
||||
}
|
||||
// invalid flag type
|
||||
if (!in_array($flag, ['on', 'off'])) {
|
||||
return false;
|
||||
}
|
||||
$switch = $type . '_output' . ($flag == 'off' ? '_not' : '');
|
||||
// log level direct check must be not null or not empty string
|
||||
if (!empty($level)) {
|
||||
return $this->{$switch}[$level] ?? false;
|
||||
}
|
||||
// array
|
||||
return $this->{$switch};
|
||||
}
|
||||
|
||||
/**
|
||||
* set flags for per log level type
|
||||
* - level: set per sub group level
|
||||
* - class: split by class
|
||||
* - page: split per page called
|
||||
* - run: for each run
|
||||
*
|
||||
* @param string $type Type to get: level, class, page, run
|
||||
* @param bool $set True or False
|
||||
* @return bool Return false if type invalid
|
||||
*/
|
||||
public function setLogPer(string $type, bool $set): bool
|
||||
{
|
||||
if (!in_array($type, ['level', 'class', 'page', 'run'])) {
|
||||
return false;
|
||||
}
|
||||
$this->{'log_per_' . $type} = $set;
|
||||
// if per run set unique id
|
||||
if ($type == 'run' && $set == true) {
|
||||
$this->setLogUniqueId();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* return current set log per flag in bool
|
||||
*
|
||||
* @param string $type Type to get: level, class, page, run
|
||||
* @return bool True of false for turned on or off
|
||||
*/
|
||||
public function getLogPer(string $type): bool
|
||||
{
|
||||
if (!in_array($type, ['level', 'class', 'page', 'run'])) {
|
||||
return false;
|
||||
}
|
||||
return $this->{'log_per_' . $type};
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a unique id based on current date (y/m/d, h:i:s) and a unique id (8 chars)
|
||||
* if override is set to true it will be newly set, else if already set nothing changes
|
||||
*
|
||||
* @param bool $override True to force new set
|
||||
* @return void
|
||||
*/
|
||||
public function setLogUniqueId(bool $override = false): void
|
||||
{
|
||||
if (!$this->log_file_unique_id || $override == true) {
|
||||
$this->log_file_unique_id =
|
||||
date('Y-m-d_His') . '_U_'
|
||||
. substr(hash('sha1', uniqid((string)mt_rand(), true)), 0, 8);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return current set log file unique id,
|
||||
* empty string for not set
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLogUniqueId(): string
|
||||
{
|
||||
return $this->log_file_unique_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set or get the log file date extension flag
|
||||
* if null or empty parameter gets current flag
|
||||
*
|
||||
* @param boolean|null $set Set the date suffix for log files
|
||||
* If set to null return current set
|
||||
* @return boolean Current set flag
|
||||
*/
|
||||
public function setGetLogPrintFileDate(?bool $set = null): bool
|
||||
{
|
||||
if ($set !== null) {
|
||||
$this->log_print_file_date = $set;
|
||||
}
|
||||
return $this->log_print_file_date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return current set log file name
|
||||
*
|
||||
* @return string Filename set set after the last time debug was called
|
||||
*/
|
||||
public function getLogFileName(): string
|
||||
{
|
||||
return $this->log_file_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* A replacement for the \CoreLibs\Debug\Support::printAr
|
||||
* But this does not wrap it in <pre></pre>
|
||||
* It uses some special code sets so we can convert that to pre flags
|
||||
* for echo output {##HTMLPRE##} ... {##/HTMLPRE##}
|
||||
* Do not use this without using it in a string in debug function
|
||||
*
|
||||
* @param array<mixed> $a Array to format
|
||||
* @return string print_r formated
|
||||
*/
|
||||
public function prAr(array $a): string
|
||||
{
|
||||
return '##HTMLPRE##' . print_r($a, true) . '##/HTMLPRE##';
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert bool value to string value
|
||||
*
|
||||
* @param bool $bool Bool value to be transformed
|
||||
* @param string $true Override default string 'true'
|
||||
* @param string $false Override default string 'false'
|
||||
* @return string $true or $false string for true/false bool
|
||||
*/
|
||||
public function prBl(
|
||||
bool $bool,
|
||||
string $true = 'true',
|
||||
string $false = 'false'
|
||||
): string {
|
||||
return $bool ? $true : $false;
|
||||
}
|
||||
|
||||
/**
|
||||
* write debug data to error_msg array
|
||||
*
|
||||
* @param string $level id for error message, groups messages together
|
||||
* @param string $string the actual error message
|
||||
* @param bool $strip default on false, if set to true,
|
||||
* all html tags will be stripped and <br> changed to \n
|
||||
* this is only used for debug output
|
||||
* @param string $prefix Attach some block before $string.
|
||||
* Will not be stripped even
|
||||
* when strip is true
|
||||
* if strip is false, recommended to add that to $string
|
||||
* @return bool True if logged, false if not logged
|
||||
*/
|
||||
public function debug(
|
||||
string $level,
|
||||
string $string,
|
||||
bool $strip = false,
|
||||
string $prefix = ''
|
||||
): bool {
|
||||
$status = false;
|
||||
// must be debug on and either echo or print on
|
||||
if (
|
||||
!$this->doDebugTrigger('debug', $level) ||
|
||||
(
|
||||
// if debug is on, either print or echo must be set to on
|
||||
!$this->doDebugTrigger('print', $level) &&
|
||||
!$this->doDebugTrigger('echo', $level)
|
||||
)
|
||||
) {
|
||||
return $status;
|
||||
}
|
||||
// get the last class entry and wrie that
|
||||
$class = Support::getCallerClass();
|
||||
// get timestamp
|
||||
$timestamp = Support::printTime();
|
||||
// same string put for print (no html data inside)
|
||||
// write to file if set
|
||||
$status = $this->writeErrorMsg(
|
||||
$level,
|
||||
'[' . $timestamp . '] '
|
||||
. '[' . $this->host_name . '] '
|
||||
. '[' . System::getPageName(System::FULL_PATH) . '] '
|
||||
. '[' . $this->running_uid . '] '
|
||||
. '{' . $class . '} '
|
||||
. '<' . $level . '> - '
|
||||
// strip the htmlpre special tags if exist
|
||||
. str_replace(
|
||||
['##HTMLPRE##', '##/HTMLPRE##'],
|
||||
'',
|
||||
// if stripping all html, etc is requested, only for write error msg
|
||||
($strip ?
|
||||
// find any <br> and replace them with \n
|
||||
// strip rest of html elements (base only)
|
||||
preg_replace(
|
||||
"/(<\/?)(\w+)([^>]*>)/",
|
||||
'',
|
||||
str_replace('<br>', "\n", $prefix . $string)
|
||||
) :
|
||||
$prefix . $string
|
||||
) ?: ''
|
||||
)
|
||||
. "\n"
|
||||
);
|
||||
// write to error level msg array if there is an echo request
|
||||
if ($this->doDebugTrigger('echo', $level)) {
|
||||
// init if not set
|
||||
if (!isset($this->error_msg[$level])) {
|
||||
$this->error_msg[$level] = [];
|
||||
}
|
||||
// HTML string
|
||||
$this->error_msg[$level][] = '<div>'
|
||||
. '[<span style="font-weight: bold; color: #5e8600;">' . $timestamp . '</span>] '
|
||||
. '[<span style="font-weight: bold; color: #c56c00;">' . $level . '</span>] '
|
||||
. '[<span style="color: #b000ab;">' . $this->host_name . '</span>] '
|
||||
. '[<span style="color: #08b369;">' . $this->page_name . '</span>] '
|
||||
. '[<span style="color: #0062A2;">' . $this->running_uid . '</span>] '
|
||||
. '{<span style="font-style: italic; color: #928100;">' . $class . '</span>} - '
|
||||
// as is prefix, allow HTML
|
||||
. $prefix
|
||||
// we replace special HTMLPRE with <pre> entries
|
||||
. str_replace(
|
||||
['##HTMLPRE##', '##/HTMLPRE##'],
|
||||
['<pre>', '</pre>'],
|
||||
Html::htmlent($string)
|
||||
)
|
||||
. "</div><!--#BR#-->";
|
||||
$status = true;
|
||||
}
|
||||
return $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* for ECHO ON only
|
||||
* returns error data as string so it can be echoed out
|
||||
*
|
||||
* @param string $header_prefix prefix string for header
|
||||
* @return string error msg for all levels
|
||||
*/
|
||||
public function printErrorMsg(string $header_prefix = ''): string
|
||||
{
|
||||
$string_output = '';
|
||||
// if not debug && echo on, do not return anything
|
||||
if (
|
||||
!$this->getLogLevelAll('debug') ||
|
||||
!$this->getLogLevelAll('echo')
|
||||
) {
|
||||
return $string_output;
|
||||
}
|
||||
if ($this->error_msg_prefix) {
|
||||
$header_prefix = $this->error_msg_prefix;
|
||||
}
|
||||
$script_end = microtime(true) - $this->script_starttime;
|
||||
foreach ($this->error_msg as $level => $temp_debug_output) {
|
||||
if ($this->doDebugTrigger('debug', $level)) {
|
||||
if ($this->doDebugTrigger('echo', $level)) {
|
||||
$string_output .= '<div style="font-size: 12px;">'
|
||||
. '[<span style="font-style: italic; color: #c56c00;">' . $level . '</span>] '
|
||||
. ($header_prefix ? "<b>**** " . Html::htmlent($header_prefix) . " ****</br>\n" : '')
|
||||
. '</div>'
|
||||
. join('', $temp_debug_output);
|
||||
} // echo it out
|
||||
} // do printout
|
||||
} // for each level
|
||||
// create the output wrapper around
|
||||
// so we have a nice formated output per class
|
||||
if ($string_output) {
|
||||
$string_prefix = '<div style="text-align: left; padding: 5px; font-size: 10px; '
|
||||
. 'font-family: sans-serif; border-top: 1px solid black; '
|
||||
. 'border-bottom: 1px solid black; margin: 10px 0 10px 0; '
|
||||
. 'background-color: white; color: black;">'
|
||||
. '<div style="font-size: 12px;">{<span style="font-style: italic; color: #928100;">'
|
||||
. Support::getCallerClass() . '</span>}</div>';
|
||||
$string_output = $string_prefix . $string_output
|
||||
. '<div><span style="font-style: italic; color: #108db3;">Script Run Time:</span> '
|
||||
. $script_end . '</div>'
|
||||
. '</div>';
|
||||
}
|
||||
// }
|
||||
return $string_output;
|
||||
}
|
||||
|
||||
/**
|
||||
* for ECHO ON only
|
||||
* unsests the error message array
|
||||
* can be used if writing is primary to file
|
||||
* if no level given resets all
|
||||
*
|
||||
* @param string $level optional level
|
||||
* @return void has no return
|
||||
*/
|
||||
public function resetErrorMsg(string $level = ''): void
|
||||
{
|
||||
if (!$level) {
|
||||
$this->error_msg = [];
|
||||
} elseif (isset($this->error_msg[$level])) {
|
||||
unset($this->error_msg[$level]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* for ECHO ON only
|
||||
* Get current error message array
|
||||
*
|
||||
* @return array<mixed> error messages collected
|
||||
*/
|
||||
public function getErrorMsg(): array
|
||||
{
|
||||
return $this->error_msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* for ECHO ON only
|
||||
* merges the given error array with the one from this class
|
||||
* only merges visible ones
|
||||
*
|
||||
* @param array<mixed> $error_msg error array
|
||||
* @return void has no return
|
||||
*/
|
||||
public function mergeErrors(array $error_msg = []): void
|
||||
{
|
||||
array_push($this->error_msg, ...$error_msg);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
129
4dev/deprecated/CoreLibs/Debug/MemoryUsage.php
Normal file
129
4dev/deprecated/CoreLibs/Debug/MemoryUsage.php
Normal file
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* dump memory usage
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Debug;
|
||||
|
||||
use CoreLibs\Convert\Byte;
|
||||
|
||||
class MemoryUsage
|
||||
{
|
||||
/** @var int */
|
||||
private static $start_memory = 0;
|
||||
/** @var int */
|
||||
private static $set_memory = 0;
|
||||
/** @var int */
|
||||
private static $previous_memory = 0;
|
||||
/** @var bool */
|
||||
private static $debug_memory = false;
|
||||
|
||||
/**
|
||||
* set memory flag, or return set memory flag
|
||||
*
|
||||
* @param bool|null $set_debug
|
||||
* @return bool
|
||||
*/
|
||||
public static function debugMemoryFlag(?bool $set_debug = null): bool
|
||||
{
|
||||
if ($set_debug === null) {
|
||||
return self::$debug_memory;
|
||||
}
|
||||
self::$debug_memory = $set_debug;
|
||||
return self::$debug_memory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset all memory variables to 0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function resetMemory(): void
|
||||
{
|
||||
self::$start_memory = 0;
|
||||
self::$set_memory = 0;
|
||||
self::$previous_memory = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the start memory velue, or reset to a new start value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function setStartMemory(): void
|
||||
{
|
||||
self::$start_memory = memory_get_usage();
|
||||
}
|
||||
|
||||
/**
|
||||
* set the and independent memory set for a sub tracking outside main
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function setMemory(): void
|
||||
{
|
||||
self::$set_memory = memory_get_usage();
|
||||
}
|
||||
|
||||
/**
|
||||
* calculate and set memory usage values
|
||||
* this will return an array with all the data that can be used in
|
||||
* printMemoryUsage for human readable output
|
||||
*
|
||||
* @param string $prefix A prefix tag
|
||||
* @return array<string,int|string> return array
|
||||
*/
|
||||
public static function memoryUsage(string $prefix): array
|
||||
{
|
||||
// skip if DEBUG is off
|
||||
if (self::$debug_memory === false) {
|
||||
return [];
|
||||
}
|
||||
if (empty(self::$start_memory)) {
|
||||
self::$start_memory = memory_get_usage();
|
||||
}
|
||||
$memory_usage = memory_get_usage();
|
||||
$data = [
|
||||
'prefix' => $prefix,
|
||||
'peak' => memory_get_peak_usage(),
|
||||
'usage' => $memory_usage,
|
||||
'start' => $memory_usage - self::$start_memory,
|
||||
'last' => $memory_usage - self::$previous_memory,
|
||||
'set' => $memory_usage - self::$set_memory
|
||||
];
|
||||
self::$previous_memory = $memory_usage;
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a human readable output from the memoryUsage function
|
||||
* can be used for logging purpose
|
||||
*
|
||||
* @param array<string,int|string> $data Data array from memoryUsage
|
||||
* @param bool $raw Flag to shaw unconverted memory numbers
|
||||
* @return string Return debug string with memory usage
|
||||
*/
|
||||
public static function printMemoryUsage(array $data, bool $raw = false): string
|
||||
{
|
||||
return
|
||||
'[' . $data['prefix'] . '] Peak/Curr/Change: '
|
||||
. Byte::humanReadableByteFormat($data['peak'])
|
||||
. '/'
|
||||
. Byte::humanReadableByteFormat($data['usage'])
|
||||
// . ($raw === true ? ' [' . $data['usage'] . ']' : '')
|
||||
. '/Since Start: '
|
||||
. Byte::humanReadableByteFormat($data['start'])
|
||||
. ($raw === true ? ' [' . $data['start'] . ']' : '')
|
||||
. ' | Since Last: '
|
||||
. Byte::humanReadableByteFormat($data['last'])
|
||||
. ($raw === true ? ' [' . $data['last'] . ']' : '')
|
||||
. ' | Since Set: '
|
||||
. Byte::humanReadableByteFormat($data['set'])
|
||||
. ($raw === true ? ' [' . $data['set'] . ']' : '');
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
173
4dev/deprecated/CoreLibs/Debug/RunningTime.php
Normal file
173
4dev/deprecated/CoreLibs/Debug/RunningTime.php
Normal file
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* various running time checkers
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Debug;
|
||||
|
||||
class RunningTime
|
||||
{
|
||||
// hr
|
||||
/** @var float */
|
||||
private static $hr_start_time;
|
||||
/** @var float */
|
||||
private static $hr_end_time;
|
||||
/** @var float */
|
||||
private static $hr_last_time;
|
||||
// normal
|
||||
/** @var float */
|
||||
private static $start_time;
|
||||
/** @var float */
|
||||
private static $end_time;
|
||||
/** @var string */
|
||||
private static $running_time_string;
|
||||
|
||||
/**
|
||||
* sub calculation for running time based on out time.
|
||||
* If no running time set, return 0
|
||||
*
|
||||
* @param string $out_time
|
||||
* @return float
|
||||
*/
|
||||
private static function hrRunningTimeCalc(
|
||||
float $run_time,
|
||||
string $out_time = 'ms'
|
||||
): float {
|
||||
// init divisor, just in case
|
||||
$divisor = 1;
|
||||
// check through valid out time, if nothing matches default to ms
|
||||
switch ($out_time) {
|
||||
case 'n':
|
||||
case 'ns':
|
||||
$divisor = 1;
|
||||
break;
|
||||
case 'y':
|
||||
case 'ys':
|
||||
$divisor = 1000;
|
||||
break;
|
||||
case 'm':
|
||||
case 'ms':
|
||||
$divisor = 1000000;
|
||||
break;
|
||||
case 's':
|
||||
$divisor = 1000000000;
|
||||
break;
|
||||
// default is ms
|
||||
default:
|
||||
$divisor = 1000000;
|
||||
break;
|
||||
}
|
||||
// return the run time in converted format
|
||||
return $run_time /= $divisor;
|
||||
}
|
||||
|
||||
/**
|
||||
* for messure run time between two calls for this method
|
||||
* uses the hrtime() for running time
|
||||
* first call sets start time and returns 0,
|
||||
* every other call sets end time and returns the run time since start
|
||||
* the out_time parameter can be:
|
||||
* n/ns (nano), y/ys (micro), m/ms (milli), s (seconds)
|
||||
* default is milliseconds
|
||||
*
|
||||
* @param string $out_time set return time adjustment calculation
|
||||
* @return float running time without out_time suffix
|
||||
*/
|
||||
public static function hrRunningTime(string $out_time = 'ms'): float
|
||||
{
|
||||
// if start time not set, set start time
|
||||
if (!self::$hr_start_time) {
|
||||
self::$hr_start_time = hrtime(true);
|
||||
self::$hr_last_time = self::$hr_start_time;
|
||||
$run_time = 0;
|
||||
} else {
|
||||
self::$hr_end_time = hrtime(true);
|
||||
$run_time = self::$hr_end_time - self::$hr_last_time;
|
||||
self::$hr_last_time = self::$hr_end_time;
|
||||
}
|
||||
return self::hrRunningTimeCalc($run_time, $out_time);
|
||||
}
|
||||
|
||||
/**
|
||||
* print overall end time , can only be called after hrRunningtime
|
||||
* see $out_time parameter description in hrRunningtime.
|
||||
* Does not record a new timestamp, only prints different between start and
|
||||
* last recoreded timestamp
|
||||
*
|
||||
* @param string $out_time set return time adjustment calculation
|
||||
* @return float overall running time without out_time suffix
|
||||
*/
|
||||
public static function hrRunningTimeFromStart(string $out_time = 'ms'): float
|
||||
{
|
||||
if (!self::$hr_start_time) {
|
||||
return (float)0;
|
||||
}
|
||||
$time = self::hrRunningTimeCalc(
|
||||
self::$hr_end_time - self::$hr_start_time,
|
||||
$out_time
|
||||
);
|
||||
return $time;
|
||||
}
|
||||
|
||||
/**
|
||||
* reset hr running time internal variables (start, end, last)
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function hrRunningTimeReset(): void
|
||||
{
|
||||
self::$hr_start_time = 0;
|
||||
self::$hr_end_time = 0;
|
||||
self::$hr_last_time = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* prints start or end time in text format. On first call sets start time
|
||||
* on second call it sends the end time and then also prints the running time
|
||||
* Sets the internal runningtime_string variable with Start/End/Run time string
|
||||
* NOTE: for pure running time check it is recommended to use hrRunningTime method
|
||||
* @param bool $simple if true prints HTML strings, default text only
|
||||
* @return float running time as float number
|
||||
*/
|
||||
public static function runningTime(bool $simple = false): float
|
||||
{
|
||||
list($micro, $timestamp) = explode(' ', microtime());
|
||||
$running_time = 0;
|
||||
// set start & end time
|
||||
if (!self::$start_time) {
|
||||
// always reset running time string on first call
|
||||
self::$running_time_string = '';
|
||||
self::$start_time = ((float)$micro + (float)$timestamp);
|
||||
self::$running_time_string .= $simple ? 'Start: ' : "<b>Started at</b>: ";
|
||||
} else {
|
||||
self::$end_time = ((float)$micro + (float)$timestamp);
|
||||
self::$running_time_string .= $simple ? 'End: ' : "<b>Stopped at</b>: ";
|
||||
}
|
||||
self::$running_time_string .= date('Y-m-d H:i:s', (int)$timestamp);
|
||||
self::$running_time_string .= ' ' . $micro . ($simple ? ', ' : '<br>');
|
||||
// if both are set
|
||||
if (self::$start_time && self::$end_time) {
|
||||
$running_time = self::$end_time - self::$start_time;
|
||||
self::$running_time_string .= ($simple ? 'Run: ' : "<b>Script running time</b>: ") . $running_time . " s";
|
||||
// reset start & end time after run
|
||||
self::$start_time = 0;
|
||||
self::$end_time = 0;
|
||||
}
|
||||
return $running_time;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the runningTime string (for debug visual)
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function runningTimeString(): string
|
||||
{
|
||||
return self::$running_time_string;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
219
4dev/deprecated/CoreLibs/Debug/Support.php
Normal file
219
4dev/deprecated/CoreLibs/Debug/Support.php
Normal file
@@ -0,0 +1,219 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Debug support functions
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Debug;
|
||||
|
||||
use CoreLibs\Convert\Html;
|
||||
|
||||
class Support
|
||||
{
|
||||
/**
|
||||
* wrapper around microtime function to print out y-m-d h:i:s.ms
|
||||
*
|
||||
* @param int $set_microtime -1 to set micro time, 0 for none,
|
||||
* positive for rounding
|
||||
* @return string formated datetime string with microtime
|
||||
*/
|
||||
public static function printTime(int $set_microtime = -1): string
|
||||
{
|
||||
list($microtime, $timestamp) = explode(' ', microtime());
|
||||
$string = date("Y-m-d H:i:s", (int)$timestamp);
|
||||
// if microtime flag is -1 no round, if 0, no microtime, if >= 1, round that size
|
||||
if ($set_microtime == -1) {
|
||||
$string .= substr($microtime, 1);
|
||||
} elseif ($set_microtime >= 1) {
|
||||
// in round case we run this through number format to always get the same amount of digits
|
||||
$string .= substr(number_format(round((float)$microtime, $set_microtime), $set_microtime), 1);
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* prints a html formatted (pre) array
|
||||
*
|
||||
* @param array<mixed> $array any array
|
||||
* @param bool $no_html set to true to use ##HTMLPRE##
|
||||
* @return string formatted array for output with <pre> tag added
|
||||
*/
|
||||
public static function printAr(array $array, bool $no_html = false): string
|
||||
{
|
||||
if ($no_html === false) {
|
||||
return "<pre>" . print_r($array, true) . "</pre>";
|
||||
} else {
|
||||
return '##HTMLPRE##' . print_r($array, true) . '##/HTMLPRE##';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* alternate name for printAr function
|
||||
*
|
||||
* @param array<mixed> $array any array
|
||||
* @param bool $no_html set to true to use ##HTMLPRE##
|
||||
* @return string formatted array for output with <pre> tag added
|
||||
*/
|
||||
public static function printArray(array $array, bool $no_html = false): string
|
||||
{
|
||||
return self::printAr($array, $no_html);
|
||||
}
|
||||
|
||||
/**
|
||||
* convert bool value to string
|
||||
* if $name is set prefix with nae
|
||||
* default true: true, false: false
|
||||
*
|
||||
* @param bool $bool Variable to convert
|
||||
* @param string $name [default: ''] Prefix name
|
||||
* @param string $true [default: true] True string
|
||||
* @param string $false [default: false] False string
|
||||
* @return string String with converted bool text for debug
|
||||
*/
|
||||
public static function printBool(
|
||||
bool $bool,
|
||||
string $name = '',
|
||||
string $true = 'true',
|
||||
string $false = 'false'
|
||||
): string {
|
||||
$string = (!empty($name) ? '<b>' . $name . '</b>: ' : '')
|
||||
. ($bool ? $true : $false);
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* print out any data as string
|
||||
* will convert boolean to TRUE/FALSE
|
||||
* if object return get_class
|
||||
* for array use printAr function, can be controlled with no_html for
|
||||
* Debug\Logging compatible output
|
||||
*
|
||||
* @param mixed $mixed
|
||||
* @param bool $no_html set to true to use ##HTMLPRE##or html escape
|
||||
* @return string
|
||||
*/
|
||||
public static function printToString(mixed $mixed, bool $no_html = false): string
|
||||
{
|
||||
if (is_null($mixed)) {
|
||||
return (string)'NULL';
|
||||
} elseif (is_bool($mixed)) {
|
||||
return self::printBool($mixed, '', 'TRUE', 'FALSE');
|
||||
} elseif (is_resource($mixed)) {
|
||||
return (string)$mixed;
|
||||
} elseif (is_object($mixed)) {
|
||||
return get_class($mixed);
|
||||
} elseif (is_array($mixed)) {
|
||||
// use the pre one OR debug one
|
||||
return self::printAr($mixed, $no_html);
|
||||
} elseif (is_string($mixed)) {
|
||||
if ($no_html) {
|
||||
return Html::htmlent((string)$mixed);
|
||||
} else {
|
||||
return (string)$mixed;
|
||||
}
|
||||
} else {
|
||||
// should be int/float/string
|
||||
return (string)$mixed;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* if there is a need to find out which parent method called a child method,
|
||||
* eg for debugging, this function does this
|
||||
*
|
||||
* call this method in the child method and you get the parent function that called
|
||||
* @param int $level debug level, default 1
|
||||
* @return ?string null or the function that called the function
|
||||
* where this method is called
|
||||
*/
|
||||
public static function getCallerMethod(int $level = 1): ?string
|
||||
{
|
||||
$traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
// print \CoreLibs\Debug\Support::printAr($traces);
|
||||
// We should check from top down if unset?
|
||||
// sets the start point here, and in level two (the sub call) we find this
|
||||
if (isset($traces[$level])) {
|
||||
return $traces[$level]['function'];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array with all methods in the call stack in the order so that last
|
||||
* called is last in order
|
||||
* Will start with start_level to skip unwanted from stack
|
||||
* Defaults to skip level 0 wich is this methid
|
||||
*
|
||||
* @param integer $start_level From what level on, as defaul starts with 1
|
||||
* to exclude self
|
||||
* @return array<mixed> All method names in list where max is last called
|
||||
*/
|
||||
public static function getCallerMethodList(int $start_level = 1): array
|
||||
{
|
||||
$traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
$methods = [];
|
||||
foreach ($traces as $level => $data) {
|
||||
if ($level >= $start_level) {
|
||||
if (!empty($data['function'])) {
|
||||
array_unshift($methods, $data['function']);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $methods;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current class where this function is called
|
||||
* Is mostly used in debug log statements to get the class where the debug
|
||||
* was called
|
||||
* gets top level class
|
||||
* loops over the debug backtrace until if finds the first class (from the end)
|
||||
*
|
||||
* @return string Class name with namespace
|
||||
*/
|
||||
public static function getCallerClass(): string
|
||||
{
|
||||
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
// ?? [['class' => get_called_class()]];
|
||||
// TODO make sure that this doesn't loop forver
|
||||
$class = null;
|
||||
while ($class === null && count($backtrace) > 0) {
|
||||
// if current is
|
||||
// [function] => debug
|
||||
// [class] => CoreLibs\Debug\Logging
|
||||
// then return
|
||||
// (OUTSIDE) because it was not called from a class method
|
||||
// or return file name
|
||||
$get_class = array_pop($backtrace);
|
||||
$class = $get_class['class'] ?? null;
|
||||
}
|
||||
// on null or empty return empty string
|
||||
return empty($class) ? '' : $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* If a string is empty, sets '-' for return, or if given any other string
|
||||
*
|
||||
* @param string|null $string The string to check
|
||||
* @param string $replace [default '-'] What to replace the empty string with
|
||||
* @return string String itself or the replaced value
|
||||
*/
|
||||
public static function debugString(
|
||||
?string $string,
|
||||
string $replace = '-',
|
||||
bool $no_html = false
|
||||
): string {
|
||||
if (empty($string)) {
|
||||
$string = $replace;
|
||||
}
|
||||
if ($no_html) {
|
||||
return Html::htmlent($string);
|
||||
} else {
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
110
4dev/deprecated/CoreLibs/Get/DotEnv.php
Normal file
110
4dev/deprecated/CoreLibs/Get/DotEnv.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Get;
|
||||
|
||||
class DotEnv
|
||||
{
|
||||
/** @var string constant comment char, set to # */
|
||||
private const COMMENT_CHAR = '#';
|
||||
|
||||
/**
|
||||
* parses .env file
|
||||
*
|
||||
* Rules for .env file
|
||||
* variable is any alphanumeric string followed by = on the same line
|
||||
* content starts with the first non space part
|
||||
* strings can be contained in "
|
||||
* strings MUST be contained in " if they are multiline
|
||||
* if string starts with " it will match until another " is found
|
||||
* anything AFTER " is ignored
|
||||
* if there are two variables with the same name only the first is used
|
||||
* variables are case sensitive
|
||||
*
|
||||
* @param string $path Folder to file, default is __DIR__
|
||||
* @param string $env_file What file to load, default is .env
|
||||
* @return int -1 other error
|
||||
* 0 for success full load
|
||||
* 1 for file loadable, no data or data already loaded
|
||||
* 2 for file not readable or open failed
|
||||
* 3 for file not found
|
||||
*/
|
||||
public static function readEnvFile(
|
||||
string $path = __DIR__,
|
||||
string $env_file = '.env'
|
||||
): int {
|
||||
// default -1;
|
||||
$status = -1;
|
||||
$env_file_target = $path . DIRECTORY_SEPARATOR . $env_file;
|
||||
// this is not a file -> abort
|
||||
if (!is_file($env_file_target)) {
|
||||
$status = 3;
|
||||
return $status;
|
||||
}
|
||||
// cannot open file -> abort
|
||||
if (!is_readable($env_file_target)) {
|
||||
$status = 2;
|
||||
return $status;
|
||||
}
|
||||
// open file
|
||||
if (($fp = fopen($env_file_target, 'r')) === false) {
|
||||
$status = 2;
|
||||
return $status;
|
||||
}
|
||||
// set to readable but not yet any data loaded
|
||||
$status = 1;
|
||||
$block = false;
|
||||
$var = '';
|
||||
while ($line = fgets($fp)) {
|
||||
// main match for variable = value part
|
||||
if (preg_match("/^\s*([\w_.]+)\s*=\s*((\"?).*)/", $line, $matches)) {
|
||||
$var = $matches[1];
|
||||
$value = $matches[2];
|
||||
$quotes = $matches[3];
|
||||
// write only if env is not set yet, and write only the first time
|
||||
if (empty($_ENV[$var])) {
|
||||
if (!empty($quotes)) {
|
||||
// match greedy for first to last so we move any " if there are
|
||||
if (preg_match('/^"(.*[^\\\])"/U', $value, $matches)) {
|
||||
$value = $matches[1];
|
||||
} else {
|
||||
// this is a multi line
|
||||
$block = true;
|
||||
// first " in string remove
|
||||
// add removed new line back because this is a multi line
|
||||
$value = ltrim($value, '"') . PHP_EOL;
|
||||
}
|
||||
} else {
|
||||
// strip any quotes at end for unquoted single line
|
||||
// an right hand spaces are removed too
|
||||
$value = false !== ($pos = strpos($value, self::COMMENT_CHAR)) ?
|
||||
rtrim(substr($value, 0, $pos)) : $value;
|
||||
}
|
||||
// if block is set, we strip line of slashes
|
||||
$_ENV[$var] = $block === true ? stripslashes($value) : $value;
|
||||
// set successful load
|
||||
$status = 0;
|
||||
}
|
||||
} elseif ($block === true) {
|
||||
// read line until there is a unescaped "
|
||||
// this also strips everything after the last "
|
||||
if (preg_match("/(.*[^\\\])\"/", $line, $matches)) {
|
||||
$block = false;
|
||||
// strip ending " and EVERYTHING that follows after that
|
||||
$line = $matches[1];
|
||||
}
|
||||
// just be sure it is init before we fill
|
||||
if (!isset($_ENV[$var])) {
|
||||
$_ENV[$var] = '';
|
||||
}
|
||||
// strip line of slashes
|
||||
$_ENV[$var] .= stripslashes($line);
|
||||
}
|
||||
}
|
||||
fclose($fp);
|
||||
return $status;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
42
4dev/deprecated/CoreLibs/Get/ReadEnvFile.php
Normal file
42
4dev/deprecated/CoreLibs/Get/ReadEnvFile.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Get;
|
||||
|
||||
/**
|
||||
* @deprecated use \CoreLibs\Get\DotEnv instead
|
||||
*/
|
||||
class ReadEnvFile
|
||||
{
|
||||
/**
|
||||
* parses .env file
|
||||
*
|
||||
* Rules for .env file
|
||||
* variable is any alphanumeric string followed by = on the same line
|
||||
* content starts with the first non space part
|
||||
* strings can be contained in "
|
||||
* strings MUST be contained in " if they are multiline
|
||||
* if string starts with " it will match until another " is found
|
||||
* anything AFTER " is ignored
|
||||
* if there are two variables with the same name only the first is used
|
||||
* variables are case sensitive
|
||||
*
|
||||
* @param string $path Folder to file, default is __DIR__
|
||||
* @param string $env_file What file to load, default is .env
|
||||
* @return int -1 other error
|
||||
* 0 for success full load
|
||||
* 1 for file loadable, but no data inside
|
||||
* 2 for file not readable or open failed
|
||||
* 3 for file not found
|
||||
* @deprecated Use \CoreLibs\Get\DotEnv::readEnvFile() instead
|
||||
*/
|
||||
public static function readEnvFile(
|
||||
string $path = __DIR__,
|
||||
string $env_file = '.env'
|
||||
): int {
|
||||
return \CoreLibs\Get\DotEnv::readEnvFile($path, $env_file);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
120
4dev/deprecated/CoreLibs/Get/System.php
Normal file
120
4dev/deprecated/CoreLibs/Get/System.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* system related functions to get self name, host name, error strings
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Get;
|
||||
|
||||
class System
|
||||
{
|
||||
public const WITH_EXTENSION = 0;
|
||||
public const NO_EXTENSION = 1;
|
||||
public const FULL_PATH = 2;
|
||||
private const DEFAULT_PORT = '80';
|
||||
|
||||
/**
|
||||
* helper function for PHP file upload error messgaes to messge string
|
||||
*
|
||||
* @param int $error_code integer _FILE upload error code
|
||||
* @return string message string, translated
|
||||
*/
|
||||
public static function fileUploadErrorMessage(int $error_code): string
|
||||
{
|
||||
switch ($error_code) {
|
||||
case UPLOAD_ERR_INI_SIZE:
|
||||
$message = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
|
||||
break;
|
||||
case UPLOAD_ERR_FORM_SIZE:
|
||||
$message = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
|
||||
break;
|
||||
case UPLOAD_ERR_PARTIAL:
|
||||
$message = 'The uploaded file was only partially uploaded';
|
||||
break;
|
||||
case UPLOAD_ERR_NO_FILE:
|
||||
$message = 'No file was uploaded';
|
||||
break;
|
||||
case UPLOAD_ERR_NO_TMP_DIR:
|
||||
$message = 'Missing a temporary folder';
|
||||
break;
|
||||
case UPLOAD_ERR_CANT_WRITE:
|
||||
$message = 'Failed to write file to disk';
|
||||
break;
|
||||
case UPLOAD_ERR_EXTENSION:
|
||||
$message = 'File upload stopped by extension';
|
||||
break;
|
||||
default:
|
||||
$message = 'Unknown upload error';
|
||||
break;
|
||||
}
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the host name without the port as given by the SELF var
|
||||
*
|
||||
* @return array<mixed> host name/port name
|
||||
*/
|
||||
public static function getHostName(): array
|
||||
{
|
||||
$host = $_SERVER['HTTP_HOST'] ?? 'NOHOST:NOPORT';
|
||||
list($host_name, $port) = array_pad(explode(':', $host), 2, self::DEFAULT_PORT);
|
||||
return [$host_name, $port];
|
||||
}
|
||||
|
||||
/**
|
||||
* get the page name of the curronte page
|
||||
*
|
||||
* @param int $strip_ext WITH_EXTENSION: keep filename as is (default)
|
||||
* NO_EXTENSION: strip page file name extension
|
||||
* FULL_PATH: keep filename as is, but add dirname too
|
||||
* @return string filename
|
||||
*/
|
||||
public static function getPageName(int $strip_ext = self::WITH_EXTENSION): string
|
||||
{
|
||||
// get the file info
|
||||
$page_temp = pathinfo($_SERVER['PHP_SELF']);
|
||||
if ($strip_ext == self::NO_EXTENSION) {
|
||||
// no extension
|
||||
return $page_temp['filename'];
|
||||
} elseif ($strip_ext == self::FULL_PATH) {
|
||||
// full path
|
||||
return $_SERVER['PHP_SELF'];
|
||||
} else {
|
||||
// with extension
|
||||
return $page_temp['basename'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* similar to getPageName, but it retuns the raw array
|
||||
*
|
||||
* @return array<string> pathinfo array from PHP SELF
|
||||
*/
|
||||
public static function getPageNameArray(): array
|
||||
{
|
||||
return pathinfo($_SERVER['PHP_SELF']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the php sapi interface has cli inside
|
||||
*
|
||||
* @return bool True for CLI type PHP, else false
|
||||
*/
|
||||
public static function checkCLI(): bool
|
||||
{
|
||||
return substr(
|
||||
// if return is false, use empty string
|
||||
(($sapi_name = php_sapi_name()) === false ?
|
||||
'' :
|
||||
$sapi_name
|
||||
),
|
||||
0,
|
||||
3
|
||||
) === 'cli' ? true : false;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
58
4dev/deprecated/CoreLibs/Language/Core/CachedFileReader.php
Normal file
58
4dev/deprecated/CoreLibs/Language/Core/CachedFileReader.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
Copyright (c) 2003, 2005, 2006, 2009 Danilo Segan <danilo@kvota.net>.
|
||||
|
||||
This file is part of PHP-gettext.
|
||||
|
||||
PHP-gettext is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
PHP-gettext is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with PHP-gettext; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Language\Core;
|
||||
|
||||
// Preloads entire file in memory first, then creates a StringReader
|
||||
// over it (it assumes knowledge of StringReader internals)
|
||||
class CachedFileReader extends \CoreLibs\Language\Core\StringReader
|
||||
{
|
||||
/** @var int */
|
||||
public $error = 0;
|
||||
/** @var string */
|
||||
public $fd_str = '';
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $filename
|
||||
*/
|
||||
public function __construct(string $filename)
|
||||
{
|
||||
parent::__construct();
|
||||
if (file_exists($filename)) {
|
||||
$fd = fopen($filename, 'rb');
|
||||
if (!is_resource($fd)) {
|
||||
$this->error = 3; // Cannot read file, probably permissions
|
||||
} else {
|
||||
$this->fd_str = fread($fd, filesize($filename) ?: 0) ?: '';
|
||||
fclose($fd);
|
||||
}
|
||||
} else {
|
||||
$this->error = 2; // File doesn't exist
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
135
4dev/deprecated/CoreLibs/Language/Core/FileReader.php
Normal file
135
4dev/deprecated/CoreLibs/Language/Core/FileReader.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
Copyright (c) 2003, 2005, 2006, 2009 Danilo Segan <danilo@kvota.net>.
|
||||
|
||||
This file is part of PHP-gettext.
|
||||
|
||||
PHP-gettext is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
PHP-gettext is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with PHP-gettext; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Language\Core;
|
||||
|
||||
class FileReader
|
||||
{
|
||||
/** @var int */
|
||||
public $fr_pos;
|
||||
/** @var resource|bool */
|
||||
public $fr_fd;
|
||||
/** @var int */
|
||||
public $fr_length;
|
||||
/** @var int */
|
||||
public $error = 0;
|
||||
|
||||
/**
|
||||
* file read constructor
|
||||
*
|
||||
* @param string $filename file name to load
|
||||
*/
|
||||
public function __construct(string $filename)
|
||||
{
|
||||
if (file_exists($filename)) {
|
||||
$this->fr_length = filesize($filename) ?: 0;
|
||||
$this->fr_pos = 0;
|
||||
$this->fr_fd = fopen($filename, 'rb');
|
||||
if (!is_resource($this->fr_fd)) {
|
||||
$this->error = 3; // Cannot read file, probably permissions
|
||||
}
|
||||
} else {
|
||||
$this->error = 2; // File doesn't exist
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* read byte data length
|
||||
*
|
||||
* @param int $bytes how many bytes to read
|
||||
* @return string read data as string
|
||||
*/
|
||||
public function read(int $bytes): string
|
||||
{
|
||||
if (!$bytes || !is_resource($this->fr_fd)) {
|
||||
return '';
|
||||
}
|
||||
fseek($this->fr_fd, $this->fr_pos);
|
||||
|
||||
// PHP 5.1.1 does not read more than 8192 bytes in one fread()
|
||||
// the discussions at PHP Bugs suggest it's the intended behaviour
|
||||
$data = '';
|
||||
while ($bytes > 0) {
|
||||
$chunk = fread($this->fr_fd, $bytes);
|
||||
if ($chunk === false) {
|
||||
break;
|
||||
}
|
||||
$data .= $chunk;
|
||||
$bytes -= strlen($chunk);
|
||||
}
|
||||
$this->fr_pos = ftell($this->fr_fd) ?: 0;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* seek to a position in the file
|
||||
*
|
||||
* @param int $pos position where to go to
|
||||
* @return int file position after seek done
|
||||
*/
|
||||
public function seekto(int $pos): int
|
||||
{
|
||||
if (!is_resource($this->fr_fd)) {
|
||||
return 0;
|
||||
}
|
||||
fseek($this->fr_fd, $pos);
|
||||
$this->fr_pos = ftell($this->fr_fd) ?: 0;
|
||||
return $this->fr_pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* get current position in file
|
||||
*
|
||||
* @return int current position in bytes
|
||||
*/
|
||||
public function currentpos(): int
|
||||
{
|
||||
return $this->fr_pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* file length/size
|
||||
*
|
||||
* @return int file size in bytes
|
||||
*/
|
||||
public function length(): int
|
||||
{
|
||||
return $this->fr_length;
|
||||
}
|
||||
|
||||
/**
|
||||
* close open file handler
|
||||
*
|
||||
* @return void has no return
|
||||
*/
|
||||
public function close(): void
|
||||
{
|
||||
if (is_resource($this->fr_fd)) {
|
||||
fclose($this->fr_fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
546
4dev/deprecated/CoreLibs/Language/Core/GetTextReader.php
Normal file
546
4dev/deprecated/CoreLibs/Language/Core/GetTextReader.php
Normal file
@@ -0,0 +1,546 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
Copyright (c) 2003, 2009 Danilo Segan <danilo@kvota.net>.
|
||||
Copyright (c) 2005 Nico Kaiser <nico@siriux.net>
|
||||
|
||||
This file is part of PHP-gettext.
|
||||
|
||||
PHP-gettext is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
PHP-gettext is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with PHP-gettext; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Language\Core;
|
||||
|
||||
/**
|
||||
* Provides a simple gettext replacement that works independently from
|
||||
* the system's gettext abilities.
|
||||
* It can read MO files and use them for translating strings.
|
||||
* The files are passed to gettext_reader as a Stream (see streams.php)
|
||||
*
|
||||
* This version has the ability to cache all strings and translations to
|
||||
* speed up the string lookup.
|
||||
* While the cache is enabled by default, it can be switched off with the
|
||||
* second parameter in the constructor (e.g. whenusing very large MO files
|
||||
* that you don't want to keep in memory)
|
||||
*/
|
||||
class GetTextReader
|
||||
{
|
||||
// public:
|
||||
/** @var int */
|
||||
public $error = 0; // public variable that holds error code (0 if no error)
|
||||
|
||||
// private:
|
||||
/** @var int */
|
||||
private $BYTEORDER = 0; // 0: low endian, 1: big endian
|
||||
/** @var FileReader */
|
||||
private $STREAM;
|
||||
/** @var bool */
|
||||
private $short_circuit = false;
|
||||
/** @var bool */
|
||||
private $enable_cache = false;
|
||||
/** @var int */
|
||||
private $originals = 0; // offset of original table
|
||||
/** @var int */
|
||||
private $translations = 0; // offset of translation table
|
||||
/** @var string */
|
||||
private $pluralheader = ''; // cache header field for plural forms
|
||||
/** @var int */
|
||||
private $total = 0; // total string count
|
||||
/** @var array<mixed>|null */
|
||||
private $table_originals = null; // table for original strings (offsets)
|
||||
/** @var array<mixed>|null */
|
||||
private $table_translations = null; // table for translated strings (offsets)
|
||||
/** @var array<mixed> */
|
||||
private $cache_translations = []; // original -> translation mapping
|
||||
|
||||
/* Methods */
|
||||
|
||||
/**
|
||||
* Reads a 32bit Integer from the Stream
|
||||
*
|
||||
* @access private
|
||||
* @return int Integer from the Stream
|
||||
*/
|
||||
private function readint(): int
|
||||
{
|
||||
if ($this->BYTEORDER == 0) {
|
||||
// low endian
|
||||
$input = unpack('V', $this->STREAM->read(4)) ?: [];
|
||||
} else {
|
||||
// big endian
|
||||
$input = unpack('N', $this->STREAM->read(4)) ?: [];
|
||||
}
|
||||
return array_shift($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* read bytes
|
||||
*
|
||||
* @param int $bytes byte length to read
|
||||
* @return string return data, possible string
|
||||
*/
|
||||
public function read(int $bytes): string
|
||||
{
|
||||
return $this->STREAM->read($bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads an array of Integers from the Stream
|
||||
*
|
||||
* @param int $count How many elements should be read
|
||||
* @return array<mixed> Array of Integers
|
||||
*/
|
||||
public function readintarray(int $count): array
|
||||
{
|
||||
if ($this->BYTEORDER == 0) {
|
||||
// low endian
|
||||
return unpack('V' . $count, $this->STREAM->read(4 * $count)) ?: [];
|
||||
} else {
|
||||
// big endian
|
||||
return unpack('N' . $count, $this->STREAM->read(4 * $count)) ?: [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param FileReader|bool $Reader the StreamReader object
|
||||
* @param bool $enable_cache Enable or disable caching
|
||||
* of strings (default on)
|
||||
*/
|
||||
public function __construct($Reader, bool $enable_cache = true)
|
||||
{
|
||||
// If there isn't a StreamReader, turn on short circuit mode.
|
||||
if ((!is_object($Reader) && !$Reader) || (is_object($Reader) && $Reader->error)) {
|
||||
$this->short_circuit = true;
|
||||
return;
|
||||
}
|
||||
// bail out for sure if this is not an objet here
|
||||
if (!is_object($Reader)) {
|
||||
$this->short_circuit = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Caching can be turned off
|
||||
$this->enable_cache = $enable_cache;
|
||||
|
||||
$MAGIC1 = "\x95\x04\x12\xde";
|
||||
$MAGIC2 = "\xde\x12\x04\x95";
|
||||
|
||||
$this->STREAM = $Reader;
|
||||
$magic = $this->read(4);
|
||||
if ($magic == $MAGIC1) {
|
||||
$this->BYTEORDER = 1;
|
||||
} elseif ($magic == $MAGIC2) {
|
||||
$this->BYTEORDER = 0;
|
||||
} else {
|
||||
$this->error = 1; // not MO file
|
||||
}
|
||||
|
||||
// FIXME: Do we care about revision? We should.
|
||||
$revision = $this->readint();
|
||||
|
||||
$this->total = $this->readint();
|
||||
$this->originals = $this->readint();
|
||||
$this->translations = $this->readint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current short circuit, equals to no translator running
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getShortCircuit(): bool
|
||||
{
|
||||
return $this->short_circuit;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the current cache enabled status
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getEnableCache(): bool
|
||||
{
|
||||
return $this->enable_cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the translation tables from the MO file into the cache
|
||||
* If caching is enabled, also loads all strings into a cache
|
||||
* to speed up translation lookups
|
||||
*
|
||||
* @access private
|
||||
* @return void
|
||||
*/
|
||||
private function loadTables(): void
|
||||
{
|
||||
if (
|
||||
is_array($this->cache_translations) &&
|
||||
is_array($this->table_originals) &&
|
||||
is_array($this->table_translations)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* get original and translations tables */
|
||||
if (!is_array($this->table_originals)) {
|
||||
$this->STREAM->seekto($this->originals);
|
||||
$this->table_originals = $this->readintarray($this->total * 2);
|
||||
}
|
||||
if (!is_array($this->table_translations)) {
|
||||
$this->STREAM->seekto($this->translations);
|
||||
$this->table_translations = $this->readintarray($this->total * 2);
|
||||
}
|
||||
|
||||
if ($this->enable_cache) {
|
||||
$this->cache_translations = [];
|
||||
/* read all strings in the cache */
|
||||
for ($i = 0; $i < $this->total; $i++) {
|
||||
$this->STREAM->seekto($this->table_originals[$i * 2 + 2]);
|
||||
$original = $this->STREAM->read($this->table_originals[$i * 2 + 1]);
|
||||
$this->STREAM->seekto($this->table_translations[$i * 2 + 2]);
|
||||
$translation = $this->STREAM->read($this->table_translations[$i * 2 + 1]);
|
||||
$this->cache_translations[$original] = $translation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string from the "originals" table
|
||||
*
|
||||
* @access private
|
||||
* @param int $num Offset number of original string
|
||||
* @return string Requested string if found, otherwise ''
|
||||
*/
|
||||
private function getOriginalString(int $num): string
|
||||
{
|
||||
$length = $this->table_originals[$num * 2 + 1] ?? 0;
|
||||
$offset = $this->table_originals[$num * 2 + 2] ?? 0;
|
||||
if (!$length) {
|
||||
return '';
|
||||
}
|
||||
$this->STREAM->seekto($offset);
|
||||
$data = $this->STREAM->read($length);
|
||||
return (string)$data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string from the "translations" table
|
||||
*
|
||||
* @access private
|
||||
* @param int $num Offset number of original string
|
||||
* @return string Requested string if found, otherwise ''
|
||||
*/
|
||||
private function getTranslationString(int $num): string
|
||||
{
|
||||
$length = $this->table_translations[$num * 2 + 1] ?? 0;
|
||||
$offset = $this->table_translations[$num * 2 + 2] ?? 0;
|
||||
if (!$length) {
|
||||
return '';
|
||||
}
|
||||
$this->STREAM->seekto($offset);
|
||||
$data = $this->STREAM->read($length);
|
||||
return (string)$data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Binary search for string
|
||||
*
|
||||
* @access private
|
||||
* @param string $string string to find
|
||||
* @param int $start (internally used in recursive function)
|
||||
* @param int $end (internally used in recursive function)
|
||||
* @return int (offset in originals table)
|
||||
*/
|
||||
private function findString(string $string, int $start = -1, int $end = -1): int
|
||||
{
|
||||
if (($start == -1) or ($end == -1)) {
|
||||
// findString is called with only one parameter, set start end end
|
||||
$start = 0;
|
||||
$end = $this->total;
|
||||
}
|
||||
if (abs($start - $end) <= 1) {
|
||||
// We're done, now we either found the string, or it doesn't exist
|
||||
$txt = $this->getOriginalString($start);
|
||||
if ($string == $txt) {
|
||||
return $start;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
} elseif ($start > $end) {
|
||||
// start > end -> turn around and start over
|
||||
return $this->findString($string, $end, $start);
|
||||
} else {
|
||||
// Divide table in two parts
|
||||
$half = (int)(($start + $end) / 2);
|
||||
$cmp = strcmp($string, $this->getOriginalString($half));
|
||||
if ($cmp == 0) {
|
||||
// string is exactly in the middle => return it
|
||||
return $half;
|
||||
} elseif ($cmp < 0) {
|
||||
// The string is in the upper half
|
||||
return $this->findString($string, $start, $half);
|
||||
} else {
|
||||
// Translateshe string is in the lower half
|
||||
return $this->findString($string, $half, $end);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates a string
|
||||
*
|
||||
* @access public
|
||||
* @param string $string to be translated
|
||||
* @return string translated string (or original, if not found)
|
||||
*/
|
||||
public function translate(string $string): string
|
||||
{
|
||||
if ($this->short_circuit) {
|
||||
return $string;
|
||||
}
|
||||
$this->loadTables();
|
||||
|
||||
if ($this->enable_cache) {
|
||||
// Caching enabled, get translated string from cache
|
||||
if (
|
||||
is_array($this->cache_translations) &&
|
||||
array_key_exists($string, $this->cache_translations)
|
||||
) {
|
||||
return $this->cache_translations[$string];
|
||||
} else {
|
||||
return $string;
|
||||
}
|
||||
} else {
|
||||
// Caching not enabled, try to find string
|
||||
$num = $this->findString($string);
|
||||
if ($num == -1) {
|
||||
return $string;
|
||||
} else {
|
||||
return $this->getTranslationString($num);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize plural form expression for use in PHP eval call.
|
||||
*
|
||||
* @access private
|
||||
* @param string $expr an expression to match
|
||||
* @return string sanitized plural form expression
|
||||
*/
|
||||
private function sanitizePluralExpression(string $expr): string
|
||||
{
|
||||
// Get rid of disallowed characters.
|
||||
$expr = preg_replace('@[^a-zA-Z0-9_:;\(\)\?\|\&=!<>+*/\%-]@', '', $expr);
|
||||
|
||||
// Add parenthesis for tertiary '?' operator.
|
||||
$expr .= ';';
|
||||
$res = '';
|
||||
$p = 0;
|
||||
$expr_len = strlen($expr);
|
||||
for ($i = 0; $i < $expr_len; $i++) {
|
||||
$ch = $expr[$i];
|
||||
switch ($ch) {
|
||||
case '?':
|
||||
$res .= ' ? (';
|
||||
$p++;
|
||||
break;
|
||||
case ':':
|
||||
$res .= ') : (';
|
||||
break;
|
||||
case ';':
|
||||
$res .= str_repeat(')', $p) . ';';
|
||||
$p = 0;
|
||||
break;
|
||||
default:
|
||||
$res .= $ch;
|
||||
}
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse full PO header and extract only plural forms line.
|
||||
*
|
||||
* @access private
|
||||
* @param string $header header search in plurals
|
||||
* @return string verbatim plural form header field
|
||||
*/
|
||||
private function extractPluralFormsHeaderFromPoHeader(string $header): string
|
||||
{
|
||||
if (preg_match("/(^|\n)plural-forms: ([^\n]*)\n/i", $header, $regs)) {
|
||||
$expr = $regs[2];
|
||||
} else {
|
||||
$expr = "nplurals=2; plural=n == 1 ? 0 : 1;";
|
||||
}
|
||||
return $expr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get possible plural forms from MO header
|
||||
*
|
||||
* @access private
|
||||
* @return string plural form header
|
||||
*/
|
||||
private function getPluralForms(): string
|
||||
{
|
||||
// lets assume message number 0 is header
|
||||
// this is true, right?
|
||||
$this->loadTables();
|
||||
|
||||
// cache header field for plural forms
|
||||
if (empty($this->pluralheader) || !is_string($this->pluralheader)) {
|
||||
if ($this->enable_cache) {
|
||||
$header = $this->cache_translations[''];
|
||||
} else {
|
||||
$header = $this->getTranslationString(0);
|
||||
}
|
||||
$expr = $this->extractPluralFormsHeaderFromPoHeader($header);
|
||||
$this->pluralheader = $this->sanitizePluralExpression($expr);
|
||||
}
|
||||
return $this->pluralheader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detects which plural form to take
|
||||
*
|
||||
* @access private
|
||||
* @param int $n count
|
||||
* @return int array index of the right plural form
|
||||
*/
|
||||
private function selectString(int $n): int
|
||||
{
|
||||
$string = $this->getPluralForms();
|
||||
$string = str_replace('nplurals', "\$total", $string);
|
||||
$string = str_replace("n", (string)$n, $string);
|
||||
$string = str_replace('plural', "\$plural", $string);
|
||||
|
||||
$total = 0;
|
||||
$plural = 0;
|
||||
|
||||
// FIXME use Symfony\Component\ExpressionLanguage\ExpressionLanguage or similar
|
||||
eval("$string");
|
||||
/** @phpstan-ignore-next-line 0 >= 0 is always true*/
|
||||
if ($plural >= $total) {
|
||||
$plural = $total - 1;
|
||||
}
|
||||
return (int)$plural;
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper for translate() method
|
||||
*
|
||||
* @access public
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
public function gettext(string $string): string
|
||||
{
|
||||
return $this->translate($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Plural version of gettext
|
||||
*
|
||||
* @access public
|
||||
* @param string $single
|
||||
* @param string $plural
|
||||
* @param int $number
|
||||
* @return string plural form
|
||||
*/
|
||||
public function ngettext(string $single, string $plural, int $number): string
|
||||
{
|
||||
if ($this->short_circuit) {
|
||||
if ($number != 1) {
|
||||
return $plural;
|
||||
} else {
|
||||
return $single;
|
||||
}
|
||||
}
|
||||
|
||||
// find out the appropriate form
|
||||
$select = $this->selectString($number);
|
||||
|
||||
// this should contains all strings separated by NULLs
|
||||
$key = $single . chr(0) . $plural;
|
||||
|
||||
if ($this->enable_cache) {
|
||||
if (is_array($this->cache_translations) && !array_key_exists($key, $this->cache_translations)) {
|
||||
return ($number != 1) ? $plural : $single;
|
||||
} else {
|
||||
$result = $this->cache_translations[$key];
|
||||
$list = explode(chr(0), $result);
|
||||
return $list[$select];
|
||||
}
|
||||
} else {
|
||||
$num = $this->findString($key);
|
||||
if ($num == -1) {
|
||||
return ($number != 1) ? $plural : $single;
|
||||
} else {
|
||||
$result = $this->getTranslationString($num);
|
||||
$list = explode(chr(0), $result);
|
||||
return $list[$select];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* p get text
|
||||
*
|
||||
* @param string $context [description]
|
||||
* @param string $msgid [description]
|
||||
* @return string [description]
|
||||
*/
|
||||
public function pgettext(string $context, string $msgid): string
|
||||
{
|
||||
$key = $context . chr(4) . $msgid;
|
||||
$ret = $this->translate($key);
|
||||
if (strpos($ret, "\004") !== false) {
|
||||
return $msgid;
|
||||
} else {
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* np get text
|
||||
*
|
||||
* @param string $context [description]
|
||||
* @param string $singular [description]
|
||||
* @param string $plural [description]
|
||||
* @param int $number [description]
|
||||
* @return string [description]
|
||||
*/
|
||||
public function npgettext(
|
||||
string $context,
|
||||
string $singular,
|
||||
string $plural,
|
||||
int $number
|
||||
): string {
|
||||
$key = $context . chr(4) . $singular;
|
||||
$ret = $this->ngettext($key, $plural, $number);
|
||||
if (strpos($ret, "\004") !== false) {
|
||||
return $singular;
|
||||
} else {
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
98
4dev/deprecated/CoreLibs/Language/Core/StringReader.php
Normal file
98
4dev/deprecated/CoreLibs/Language/Core/StringReader.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
Copyright (c) 2003, 2005, 2006, 2009 Danilo Segan <danilo@kvota.net>.
|
||||
|
||||
This file is part of PHP-gettext.
|
||||
|
||||
PHP-gettext is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
PHP-gettext is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with PHP-gettext; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Language\Core;
|
||||
|
||||
class StringReader
|
||||
{
|
||||
/** @var int */
|
||||
public $sr_pos;
|
||||
/** @var string */
|
||||
public $sr_str;
|
||||
|
||||
/**
|
||||
* constructor for string reader
|
||||
*
|
||||
* @param string $str basic string
|
||||
*/
|
||||
public function __construct(string $str = '')
|
||||
{
|
||||
$this->sr_str = $str;
|
||||
$this->sr_pos = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* read bytes in string
|
||||
*
|
||||
* @param int $bytes bytes to read in string
|
||||
* @return string data read in length of bytes as string
|
||||
*/
|
||||
public function read(int $bytes): string
|
||||
{
|
||||
$data = substr($this->sr_str, $this->sr_pos, $bytes);
|
||||
$this->sr_pos += $bytes;
|
||||
if (strlen($this->sr_str) < $this->sr_pos) {
|
||||
$this->sr_pos = strlen($this->sr_str);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* go to position in string
|
||||
*
|
||||
* @param int $pos position in string
|
||||
* @return int new position in string after seek
|
||||
*/
|
||||
public function seekto(int $pos): int
|
||||
{
|
||||
$this->sr_pos = $pos;
|
||||
if (strlen($this->sr_str) < $this->sr_pos) {
|
||||
$this->sr_pos = strlen($this->sr_str);
|
||||
}
|
||||
return $this->sr_pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* get current position in string
|
||||
*
|
||||
* @return int position in string
|
||||
*/
|
||||
public function currentpos(): int
|
||||
{
|
||||
return $this->sr_pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* get length of string
|
||||
*
|
||||
* @return int return length of assigned string
|
||||
*/
|
||||
public function length(): int
|
||||
{
|
||||
return strlen($this->sr_str);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
127
4dev/deprecated/CoreLibs/Language/Encoding.php
Normal file
127
4dev/deprecated/CoreLibs/Language/Encoding.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* deprecated function calls
|
||||
* Language\Encoding::__mbMimeEncode -> Convert\MimeEncode::__mbMimeEncode
|
||||
* Langauge\Encoding::checkConvertEncoding -> Check\Encoding::checkConvertEncoding
|
||||
* Langauge\Encoding::setErrorChar -> Check\Encoding::setErrorChar
|
||||
* Langauge\Encoding::getErrorChar -> Check\Encoding::getErrorChar
|
||||
* Langauge\Encoding::convertEncoding -> Convert\Encoding::convertEncoding
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Language;
|
||||
|
||||
class Encoding
|
||||
{
|
||||
/**
|
||||
* wrapper function for mb mime convert
|
||||
* for correct conversion with long strings
|
||||
*
|
||||
* @param string $string string to encode
|
||||
* @param string $encoding target encoding
|
||||
* @param string $line_break default line break is \r\n
|
||||
* @return string encoded string
|
||||
* @deprecated Use \CoreLibs\Convert\MimeEncode::__mbMimeEncode();
|
||||
*/
|
||||
public static function __mbMimeEncode(
|
||||
string $string,
|
||||
string $encoding,
|
||||
string $line_break = "\r\n"
|
||||
): string {
|
||||
return \CoreLibs\Convert\MimeEncode::__mbMimeEncode($string, $encoding, $line_break);
|
||||
}
|
||||
|
||||
/**
|
||||
* set error char
|
||||
*
|
||||
* @param string|int|null $string The character to use to represent
|
||||
* error chars
|
||||
* "long" for long, "none" for none
|
||||
* or a valid code point in int
|
||||
* like 0x2234 (8756, ∴)
|
||||
* default character is ? (63)
|
||||
* if null is set then "none"
|
||||
* @return void
|
||||
* @deprecated Use \CoreLibs\Check\Encoding::setErrorChar();
|
||||
*/
|
||||
public static function setErrorChar($string): void
|
||||
{
|
||||
\CoreLibs\Check\Encoding::setErrorChar($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the current set error character
|
||||
*
|
||||
* @param bool $return_substitute_func if set to true return the set
|
||||
* character from the php function
|
||||
* directly
|
||||
* @return string|int Set error character
|
||||
* @deprecated Use \CoreLibs\Check\Encoding::getErrorChar();
|
||||
*/
|
||||
public static function getErrorChar(bool $return_substitute_func = false)
|
||||
{
|
||||
return \CoreLibs\Check\Encoding::getErrorChar($return_substitute_func);
|
||||
}
|
||||
|
||||
/**
|
||||
* test if a string can be safely convert between encodings.
|
||||
* mostly utf8 to shift jis
|
||||
* the default compare has a possibility of failure, especially with windows
|
||||
* it is recommended to the following in the script which uses this method:
|
||||
* mb_substitute_character(0x2234);
|
||||
* $class->mb_error_char = '∴';
|
||||
* if check to Shift JIS
|
||||
* if check to ISO-2022-JP
|
||||
* if check to ISO-2022-JP-MS
|
||||
* set three dots (∴) as wrong character for correct convert error detect
|
||||
* (this char is used, because it is one of the least used ones)
|
||||
*
|
||||
* @param string $string string to test
|
||||
* @param string $from_encoding encoding of string to test
|
||||
* @param string $to_encoding target encoding
|
||||
* @return bool|array<string> false if no error or
|
||||
* array with failed characters
|
||||
* @deprecated Use \CoreLibs\Check\Encoding::checkConvertEncoding();
|
||||
*/
|
||||
public static function checkConvertEncoding(
|
||||
string $string,
|
||||
string $from_encoding,
|
||||
string $to_encoding
|
||||
) {
|
||||
return \CoreLibs\Check\Encoding::checkConvertEncoding($string, $from_encoding, $to_encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* detects the source encoding of the string and if doesn't match
|
||||
* to the given target encoding it convert is
|
||||
* if source encoding is set and auto check is true (default) a second
|
||||
* check is done so that the source string encoding actually matches
|
||||
* will be skipped if source encoding detection is ascii
|
||||
*
|
||||
* @param string $string string to convert
|
||||
* @param string $to_encoding target encoding
|
||||
* @param string $source_encoding optional source encoding, will try to auto detect
|
||||
* @param bool $auto_check default true, if source encoding is set
|
||||
* check that the source is actually matching
|
||||
* to what we sav the source is
|
||||
* @return string encoding converted string
|
||||
* @deprecated Use \CoreLibs\Convert\Encoding::convertEncoding();
|
||||
*/
|
||||
public static function convertEncoding(
|
||||
string $string,
|
||||
string $to_encoding,
|
||||
string $source_encoding = '',
|
||||
bool $auto_check = true
|
||||
): string {
|
||||
return \CoreLibs\Convert\Encoding::convertEncoding(
|
||||
$string,
|
||||
$to_encoding,
|
||||
$source_encoding,
|
||||
$auto_check
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
117
4dev/deprecated/CoreLibs/Language/GetLocale.php
Normal file
117
4dev/deprecated/CoreLibs/Language/GetLocale.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Internal function for getting locale and encodig settings
|
||||
* used for new locale layout
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Language;
|
||||
|
||||
class GetLocale
|
||||
{
|
||||
/**
|
||||
* returns locale, lang, domain, encoding, path
|
||||
* from either parameter set or from sessions/config variables
|
||||
*
|
||||
* @param string|null $locale override auto detect
|
||||
* @param string|null $domain override domain
|
||||
* @param string|null $encoding override encoding
|
||||
* @param string|null $path override path
|
||||
* @return array<string,string> locale, domain, encoding, path
|
||||
*/
|
||||
public static function setLocale(
|
||||
?string $locale = null,
|
||||
?string $domain = null,
|
||||
?string $encoding = null,
|
||||
?string $path = null
|
||||
): array {
|
||||
// locale must match at least basic rules
|
||||
if (
|
||||
empty($locale) ||
|
||||
!preg_match("/^[-A-Za-z0-9_.@]+$/", $locale)
|
||||
) {
|
||||
if (!empty($_SESSION['DEFAULT_LOCALE'])) {
|
||||
// parse from session (logged in)
|
||||
$locale = $_SESSION['DEFAULT_LOCALE'];
|
||||
} else {
|
||||
// else parse from site locale
|
||||
$locale = defined('SITE_LOCALE') && !empty(SITE_LOCALE) ?
|
||||
SITE_LOCALE :
|
||||
// else parse from default, if not 'en'
|
||||
/** @phpstan-ignore-next-line DEFAULT_LOCALE could be empty */
|
||||
(defined('DEFAULT_LOCALE') && !empty(DEFAULT_LOCALE) ?
|
||||
DEFAULT_LOCALE : 'en');
|
||||
}
|
||||
}
|
||||
// if domain is set, must be alphanumeric, if not unset
|
||||
if (
|
||||
empty($domain) ||
|
||||
!preg_match("/^\w+$/", $domain)
|
||||
) {
|
||||
// if no domain is set, fall back to content path
|
||||
$domain = str_replace('/', '', CONTENT_PATH);
|
||||
}
|
||||
// check that override encoding matches locale encoding
|
||||
// if locale encoding is set
|
||||
preg_match('/(?:\\.(?P<charset>[-A-Za-z0-9_]+))/', $locale, $matches);
|
||||
$locale_encoding = $matches['charset'] ?? null;
|
||||
if (
|
||||
// empty encoding
|
||||
empty($encoding) ||
|
||||
// not valid encoding
|
||||
!preg_match("/^[-A-Za-z0-9_]+$/", $encoding) ||
|
||||
// locale encoding set and not matching to encoding
|
||||
(!empty($locale_encoding) && $encoding != $locale_encoding)
|
||||
) {
|
||||
if (!empty($locale_encoding)) {
|
||||
$encoding = strtoupper($locale_encoding);
|
||||
} elseif (!empty($_SESSION['DEFAULT_CHARSET'])) {
|
||||
// else set from session
|
||||
$encoding = $_SESSION['DEFAULT_CHARSET'];
|
||||
} else {
|
||||
// else set from site encoding
|
||||
$encoding = defined('SITE_ENCODING') && !empty(SITE_ENCODING) ?
|
||||
SITE_ENCODING :
|
||||
// or default encoding, if not 'UTF-8'
|
||||
/** @phpstan-ignore-next-line DEFAULT_LOCALE could be empty */
|
||||
(defined('DEFAULT_ENCODING') && !empty(DEFAULT_ENCODING) ?
|
||||
DEFAULT_ENCODING : 'UTF-8');
|
||||
}
|
||||
}
|
||||
// path checks if set, if not valid path unset to default BASE path
|
||||
if (
|
||||
empty($path) ||
|
||||
!is_dir($path)
|
||||
) {
|
||||
$path = BASE . INCLUDES . LOCALE;
|
||||
}
|
||||
// extract lang & country from locale string, else set to en
|
||||
if (
|
||||
preg_match(
|
||||
// lang
|
||||
'/^(?P<lang>[a-z]{2,3})'
|
||||
// country code
|
||||
. '(?:_(?P<country>[A-Z]{2}))?/',
|
||||
$locale,
|
||||
$matches
|
||||
)
|
||||
) {
|
||||
$lang = ($matches['lang'] ?? 'en')
|
||||
// add country only if set
|
||||
. (!empty($matches['country']) ? '_' . $matches['country'] : '');
|
||||
} else {
|
||||
$lang = 'en';
|
||||
}
|
||||
return [
|
||||
'locale' => $locale,
|
||||
'lang' => $lang,
|
||||
'domain' => $domain,
|
||||
'encoding' => $encoding,
|
||||
'path' => $path,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
663
4dev/deprecated/CoreLibs/Language/L10n.php
Normal file
663
4dev/deprecated/CoreLibs/Language/L10n.php
Normal file
@@ -0,0 +1,663 @@
|
||||
<?php
|
||||
|
||||
/*********************************************************************
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2004/11/18
|
||||
* VERSION: 3.0.0
|
||||
* RELEASED LICENSE: GNU GPL 3
|
||||
* SHORT DESCRIPTION:
|
||||
* init class for gettext. Original was just a function &
|
||||
* var setting include for wordpress.
|
||||
* I changed that to a class to be more portable with my style of coding
|
||||
* VERSION 3.0 (2022/4) removes all old folder layout and uses standard gettext
|
||||
* PUBLIC METHODS
|
||||
* __ : returns string (translated or original if not found)
|
||||
* __n : plural string
|
||||
* __p : string with context
|
||||
* __np: string with context and plural
|
||||
*
|
||||
* HISTORY:
|
||||
* 2022/4/15 (cs) drop all old folder layout support, new folder base
|
||||
* in locale with standard gettext layout of
|
||||
* locale/LC_MESSAGES/domain.mo
|
||||
* 2005/10/17 (cs) made an on the fly switch method (reload of lang)
|
||||
*********************************************************************/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Language;
|
||||
|
||||
use CoreLibs\Language\Core\FileReader;
|
||||
use CoreLibs\Language\Core\GetTextReader;
|
||||
|
||||
class L10n
|
||||
{
|
||||
/** @var string the current locale */
|
||||
private $locale = '';
|
||||
/** @var string the SET locale as WHERE the domain file is */
|
||||
private $locale_set = '';
|
||||
/** @var string the default selected/active domain */
|
||||
private $domain = '';
|
||||
/** @var array<string,array<string,GetTextReader>> locale > domain = translator */
|
||||
private $domains = [];
|
||||
/** @var array<string,string> bound paths for domains */
|
||||
private $paths = ['' => './'];
|
||||
|
||||
// files
|
||||
/** @var string the full path to the mo file to loaded */
|
||||
private $mofile = '';
|
||||
/** @var string base path to search level */
|
||||
private $base_locale_path = '';
|
||||
/** @var string dynamic set path to where the mo file is actually */
|
||||
private $base_content_path = '';
|
||||
|
||||
// errors
|
||||
/** @var bool if load of mo file was unsuccessful */
|
||||
private $load_failure = false;
|
||||
|
||||
// object holders
|
||||
/** @var FileReader|bool reader class for file reading, false for short circuit */
|
||||
private $input = false;
|
||||
/** @var GetTextReader reader class for MO data */
|
||||
private $l10n;
|
||||
/**
|
||||
* @static
|
||||
* @var L10n self class
|
||||
*/
|
||||
private static $instance;
|
||||
|
||||
/**
|
||||
* class constructor call for language getstring
|
||||
* if locale is not empty will load translation
|
||||
* else getTranslator needs to be called
|
||||
*
|
||||
* @param string $locale language name, default empty string
|
||||
* will return self instance
|
||||
* @param string $domain override CONTENT_PATH . $encoding name for mo file
|
||||
* @param string $path path, if empty fallback on default internal path
|
||||
*/
|
||||
public function __construct(
|
||||
string $locale = '',
|
||||
string $domain = '',
|
||||
string $path = ''
|
||||
) {
|
||||
// auto load language only if at least locale and domain is set
|
||||
if (!empty($locale) && !empty($domain)) {
|
||||
// check hack if domain and path is switched
|
||||
// Note this can be removed in future versions
|
||||
if (strstr($domain, DIRECTORY_SEPARATOR) !== false) {
|
||||
$_domain = $path;
|
||||
$path = $domain;
|
||||
$domain = $_domain;
|
||||
}
|
||||
$this->getTranslator($locale, $domain, $path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the singleton L10n object.
|
||||
* For function wrapper use
|
||||
*
|
||||
* @return L10n object
|
||||
*/
|
||||
public static function getInstance(): L10n
|
||||
{
|
||||
/** @phpstan-ignore-next-line */
|
||||
if (empty(self::$instance)) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads global localization functions.
|
||||
* prefixed with double underscore
|
||||
* eg: gettext -> __gettext
|
||||
*/
|
||||
public static function loadFunctions(): void
|
||||
{
|
||||
require_once __DIR__ . '/l10n_functions.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* loads the mo file base on path, locale and domain set
|
||||
*
|
||||
* @param string $locale language name (optional), fallback is en
|
||||
* @param string $domain override CONTENT_PATH . $encoding name for mo file
|
||||
* @param string $path path, if empty fallback on default internal path
|
||||
* @return GetTextReader the main gettext reader object
|
||||
*/
|
||||
public function getTranslator(
|
||||
string $locale = '',
|
||||
string $domain = '',
|
||||
string $path = ''
|
||||
): GetTextReader {
|
||||
// set local if not from parameter
|
||||
if (empty($locale)) {
|
||||
$locale = $this->locale;
|
||||
}
|
||||
// set domain if not given
|
||||
if (empty($domain)) {
|
||||
$domain = $this->domain;
|
||||
}
|
||||
// store old settings
|
||||
$old_mofile = $this->mofile;
|
||||
$old_lang = $this->locale;
|
||||
$old_lang_set = $this->locale_set;
|
||||
$old_domain = $this->domain;
|
||||
$old_base_locale_path = $this->base_locale_path;
|
||||
$old_base_content_path = $this->base_content_path;
|
||||
|
||||
// if path is a dir
|
||||
// 1) from a previous set domain
|
||||
// 2) from method option as is
|
||||
// 3) fallback if BASE/INCLUDES/LOCALE set
|
||||
// 4) current dir
|
||||
if (!empty($this->paths[$domain]) && is_dir($this->paths[$domain])) {
|
||||
$this->base_locale_path = $this->paths[$domain];
|
||||
} elseif (is_dir($path)) {
|
||||
$this->base_locale_path = $path;
|
||||
} elseif (
|
||||
defined('BASE') && defined('INCLUDES') && defined('LOCALE')
|
||||
) {
|
||||
// set fallback base path if constant set
|
||||
$this->base_locale_path = BASE . INCLUDES . LOCALE;
|
||||
} else {
|
||||
$this->base_locale_path = './';
|
||||
}
|
||||
// now we loop over lang compositions to get the base path
|
||||
// then we check
|
||||
$locales = $this->listLocales($locale);
|
||||
foreach ($locales as $_locale) {
|
||||
$this->base_content_path = $_locale . DIRECTORY_SEPARATOR
|
||||
. 'LC_MESSAGES' . DIRECTORY_SEPARATOR;
|
||||
$this->mofile = $this->base_locale_path
|
||||
. $this->base_content_path
|
||||
. $domain . '.mo';
|
||||
if (file_exists($this->mofile)) {
|
||||
$this->locale_set = $_locale;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// check if get a readable mofile
|
||||
if (is_readable($this->mofile)) {
|
||||
// locale and domain current wanted
|
||||
$this->locale = $locale;
|
||||
$this->domain = $domain;
|
||||
// set empty domains path with current locale
|
||||
if (empty($this->domains[$locale])) {
|
||||
$this->domains[$locale] = [];
|
||||
}
|
||||
// store current base path (without locale, etc)
|
||||
if (empty($this->paths[$domain])) {
|
||||
$this->paths[$domain] = $this->base_locale_path;
|
||||
}
|
||||
// file reader and mo reader
|
||||
$this->input = new FileReader($this->mofile);
|
||||
$this->l10n = new GetTextReader($this->input);
|
||||
// if short circuit is true, we failed to have a translator loaded
|
||||
$this->load_failure = $this->l10n->getShortCircuit();
|
||||
// below is not used at the moment, but can be to avoid reloading
|
||||
$this->domains[$this->locale][$domain] = $this->l10n;
|
||||
} elseif (!empty($old_mofile)) {
|
||||
// mo file not readable
|
||||
$this->load_failure = true;
|
||||
// else fall back to the old ones
|
||||
$this->mofile = $old_mofile;
|
||||
$this->locale = $old_lang;
|
||||
$this->locale_set = $old_lang_set;
|
||||
$this->domain = $old_domain;
|
||||
$this->base_locale_path = $old_base_locale_path;
|
||||
$this->base_content_path = $old_base_content_path;
|
||||
} else {
|
||||
// mo file not readable, no previous mo file set, set short circuit
|
||||
$this->load_failure = true;
|
||||
// dummy
|
||||
$this->l10n = new GetTextReader($this->input);
|
||||
}
|
||||
return $this->l10n;
|
||||
}
|
||||
|
||||
/**
|
||||
* return current set GetTextReader or return the one for given
|
||||
* domain name if set
|
||||
* This can be used to access all the public methods from the
|
||||
* GetTextReader
|
||||
*
|
||||
* @param string $domain optional domain name
|
||||
* @return GetTextReader
|
||||
*/
|
||||
public function getTranslatorClass(string $domain = ''): GetTextReader
|
||||
{
|
||||
if (!empty($domain) && !empty($this->domains[$this->locale][$domain])) {
|
||||
return $this->domains[$this->locale][$domain];
|
||||
}
|
||||
// if null return short circuit version
|
||||
if ($this->l10n === null) {
|
||||
return new GetTextReader($this->input);
|
||||
}
|
||||
return $this->l10n;
|
||||
}
|
||||
|
||||
/**
|
||||
* parse the locale string for further processing
|
||||
*
|
||||
* @param string $locale Locale to parse
|
||||
* @return array<string,string|null> array with lang, country, charset, modifier
|
||||
*/
|
||||
public static function parseLocale(string $locale = ''): array
|
||||
{
|
||||
preg_match(
|
||||
// language code
|
||||
'/^(?P<lang>[a-z]{2,3})'
|
||||
// country code
|
||||
. '(?:_(?P<country>[A-Z]{2}))?'
|
||||
// charset
|
||||
. '(?:\\.(?P<charset>[-A-Za-z0-9_]+))?'
|
||||
// @ modifier
|
||||
. '(?:@(?P<modifier>[-A-Za-z0-9_]+))?$/',
|
||||
$locale,
|
||||
$matches
|
||||
);
|
||||
return [
|
||||
'lang' => $matches['lang'] ?? null,
|
||||
'country' => $matches['country'] ?? null,
|
||||
'charset' => $matches['charset'] ?? null,
|
||||
'modifier' => $matches['modifier'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* original:
|
||||
* vendor/phpmyadmin/motranslator/src/Loader.php
|
||||
*
|
||||
* Returns array with all possible locale combinations based on the
|
||||
* given locale name
|
||||
*
|
||||
* I.e. for sr_CS.UTF-8@latin, look through all of
|
||||
* sr_CS.UTF-8@latin, sr_CS@latin, sr@latin, sr_CS.UTF-8, sr_CS, sr.
|
||||
*
|
||||
* @param string $locale Locale string
|
||||
* @return array<string> List of locale path parts that can be possible
|
||||
*/
|
||||
public static function listLocales(string $locale): array
|
||||
{
|
||||
$locale_list = [];
|
||||
|
||||
if (empty($locale)) {
|
||||
return $locale_list;
|
||||
}
|
||||
// is matching regex
|
||||
$locale_detail = L10n::parseLocale($locale);
|
||||
// all null = nothing mached, return locale as is
|
||||
if ($locale_detail === array_filter($locale_detail, 'is_null')) {
|
||||
return [$locale];
|
||||
}
|
||||
// write to innteral vars
|
||||
$lang = $locale_detail['lang'];
|
||||
$country = $locale_detail['country'];
|
||||
$charset = $locale_detail['charset'];
|
||||
$modifier = $locale_detail['modifier'];
|
||||
// we need to add all possible cominations from not null set
|
||||
// entries to the list, from longest to shortest
|
||||
// %s_%s.%s@%s (lang _ country . encoding @ suffix)
|
||||
// %s_%s@%s (lang _ country @ suffix)
|
||||
// %s@%s (lang @ suffix)
|
||||
// %s_%s.%s (lang _ country . encoding)
|
||||
// %s_%s (lang _ country)
|
||||
// %s (lang)
|
||||
|
||||
// if lang is set
|
||||
if ($lang) {
|
||||
// modifier group
|
||||
if ($modifier) {
|
||||
if ($country) {
|
||||
if ($charset) {
|
||||
array_push(
|
||||
$locale_list,
|
||||
sprintf('%s_%s.%s@%s', $lang, $country, $charset, $modifier)
|
||||
);
|
||||
}
|
||||
|
||||
array_push(
|
||||
$locale_list,
|
||||
sprintf('%s_%s@%s', $lang, $country, $modifier)
|
||||
);
|
||||
} elseif ($charset) {
|
||||
array_push(
|
||||
$locale_list,
|
||||
sprintf('%s.%s@%s', $lang, $charset, $modifier)
|
||||
);
|
||||
}
|
||||
|
||||
array_push(
|
||||
$locale_list,
|
||||
sprintf('%s@%s', $lang, $modifier)
|
||||
);
|
||||
}
|
||||
// country group
|
||||
if ($country) {
|
||||
if ($charset) {
|
||||
array_push(
|
||||
$locale_list,
|
||||
sprintf('%s_%s.%s', $lang, $country, $charset)
|
||||
);
|
||||
}
|
||||
|
||||
array_push(
|
||||
$locale_list,
|
||||
sprintf('%s_%s', $lang, $country)
|
||||
);
|
||||
} elseif ($charset) {
|
||||
array_push(
|
||||
$locale_list,
|
||||
sprintf('%s.%s', $lang, $charset)
|
||||
);
|
||||
}
|
||||
// lang only
|
||||
array_push($locale_list, $lang);
|
||||
}
|
||||
|
||||
// If the locale name doesn't match POSIX style, just include it as-is.
|
||||
if (!in_array($locale, $locale_list)) {
|
||||
array_push($locale_list, $locale);
|
||||
}
|
||||
|
||||
return $locale_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* tries to detect the locale set in the following order:
|
||||
* - globals: LOCALE
|
||||
* - globals: LANG
|
||||
* - env: LC_ALL
|
||||
* - env: LC_MESSAGES
|
||||
* - env: LANG
|
||||
* if nothing set, returns 'en' as default
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function detectLocale(): string
|
||||
{
|
||||
// globals
|
||||
foreach (['LOCALE', 'LANG'] as $global) {
|
||||
if (!empty($GLOBALS[$global])) {
|
||||
return $GLOBALS[$global];
|
||||
}
|
||||
}
|
||||
// enviroment
|
||||
foreach (['LC_ALL', 'LC_MESSAGES', 'LANG'] as $env) {
|
||||
$locale = getenv($env);
|
||||
if ($locale !== false && !empty($locale)) {
|
||||
return $locale;
|
||||
}
|
||||
}
|
||||
return 'en';
|
||||
}
|
||||
|
||||
/************
|
||||
* INTERNAL VAR SET/GET
|
||||
*/
|
||||
|
||||
/**
|
||||
* Sets the path for a domain.
|
||||
* must be set before running getTranslator (former l10nReloadMOfile)
|
||||
*
|
||||
* @param string $domain Domain name
|
||||
* @param string $path Path where to find locales
|
||||
*/
|
||||
public function setTextDomain(string $domain, string $path): void
|
||||
{
|
||||
$this->paths[$domain] = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* return set path for given domain
|
||||
* if not found return false
|
||||
*
|
||||
* @param string $domain
|
||||
* @return string|bool
|
||||
*/
|
||||
public function getTextDomain(string $domain)
|
||||
{
|
||||
return $this->paths[$domain] ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the default domain.
|
||||
*
|
||||
* @param string $domain Domain name
|
||||
*/
|
||||
public function setDomain(string $domain): void
|
||||
{
|
||||
$this->domain = $domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* return current set domain name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDomain(): string
|
||||
{
|
||||
return $this->domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets a requested locale.
|
||||
*
|
||||
* @param string $locale Locale name
|
||||
* @return string Set or current locale
|
||||
*/
|
||||
public function setLocale(string $locale): string
|
||||
{
|
||||
if (!empty($locale)) {
|
||||
$this->locale = $locale;
|
||||
}
|
||||
return $this->locale;
|
||||
}
|
||||
|
||||
/**
|
||||
* get current set locale (want locale)
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLocale(): string
|
||||
{
|
||||
return $this->locale;
|
||||
}
|
||||
|
||||
/**
|
||||
* current set locale where mo file is located
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLocaleSet(): string
|
||||
{
|
||||
return $this->locale_set;
|
||||
}
|
||||
|
||||
/**
|
||||
* get current set language
|
||||
*
|
||||
* @return string current set language string
|
||||
* @deprecated Use getLocale()
|
||||
*/
|
||||
public function __getLang(): string
|
||||
{
|
||||
return $this->getLocale();
|
||||
}
|
||||
|
||||
/**
|
||||
* get current set mo file
|
||||
*
|
||||
* @return string current set mo language file
|
||||
*/
|
||||
public function getMoFile(): string
|
||||
{
|
||||
return $this->mofile;
|
||||
}
|
||||
|
||||
/**
|
||||
* get current set mo file
|
||||
*
|
||||
* @return string current set mo language file
|
||||
* @deprecated Use getMoFile()
|
||||
*/
|
||||
public function __getMoFile(): string
|
||||
{
|
||||
return $this->getMoFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* get the current base path in which we search
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getBaseLocalePath(): string
|
||||
{
|
||||
return $this->base_locale_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* the path below the base path to where the mo file is located
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getBaseContentPath(): string
|
||||
{
|
||||
return $this->base_content_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the current load error status
|
||||
* if true then the mo file failed to load
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getLoadError(): bool
|
||||
{
|
||||
return $this->load_failure;
|
||||
}
|
||||
|
||||
/************
|
||||
* TRANSLATION METHODS
|
||||
*/
|
||||
|
||||
/**
|
||||
* translates a string and returns translated text
|
||||
*
|
||||
* @param string $text text to translate
|
||||
* @return string translated text
|
||||
*/
|
||||
public function __(string $text): string
|
||||
{
|
||||
// fallback passthrough
|
||||
if ($this->l10n === null) {
|
||||
return $text;
|
||||
}
|
||||
return $this->l10n->translate($text);
|
||||
}
|
||||
|
||||
/**
|
||||
* prints translated string out to the screen
|
||||
* @param string $text text to translate
|
||||
* @return void has no return
|
||||
* @deprecated use echo __() instead
|
||||
*/
|
||||
public function __e(string $text): void
|
||||
{
|
||||
// fallback passthrough
|
||||
if ($this->l10n === null) {
|
||||
echo $text;
|
||||
}
|
||||
echo $this->l10n->translate($text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the plural form.
|
||||
*
|
||||
* @param string $single string for single word
|
||||
* @param string $plural string for plural word
|
||||
* @param int $number number value
|
||||
* @return string translated plural string
|
||||
*/
|
||||
public function __n(string $single, string $plural, int $number): string
|
||||
{
|
||||
// in case nothing got set yet, this is fallback
|
||||
if ($this->l10n === null) {
|
||||
return $number > 1 ? $plural : $single;
|
||||
}
|
||||
return $this->l10n->ngettext($single, $plural, $number);
|
||||
}
|
||||
|
||||
/**
|
||||
* context translation via msgctxt
|
||||
*
|
||||
* @param string $context context string
|
||||
* @param string $text text to translate
|
||||
* @return string
|
||||
*/
|
||||
public function __p(string $context, string $text): string
|
||||
{
|
||||
if ($this->l10n === null) {
|
||||
return $text;
|
||||
}
|
||||
return $this->l10n->pgettext($context, $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* context translation via msgctxt
|
||||
*
|
||||
* @param string $context context string
|
||||
* @param string $single string for single word
|
||||
* @param string $plural string for plural word
|
||||
* @param int $number number value
|
||||
* @return string
|
||||
*/
|
||||
public function __np(string $context, string $single, string $plural, int $number): string
|
||||
{
|
||||
if ($this->l10n === null) {
|
||||
return $number > 1 ? $plural : $single;
|
||||
}
|
||||
return $this->l10n->npgettext($context, $single, $plural, $number);
|
||||
}
|
||||
|
||||
// alias functions to mimic gettext calls
|
||||
|
||||
/**
|
||||
* alias for gettext,
|
||||
* calls __
|
||||
*
|
||||
* @param string $text
|
||||
* @return string
|
||||
* @deprecated Use __()
|
||||
*/
|
||||
public function gettext(string $text): string
|
||||
{
|
||||
return $this->__($text);
|
||||
}
|
||||
|
||||
/**
|
||||
* alias for ngettext
|
||||
* calls __n
|
||||
*
|
||||
* @param string $single
|
||||
* @param string $plural
|
||||
* @param int $number
|
||||
* @return string
|
||||
* @deprecated Use __n()
|
||||
*/
|
||||
public function ngettext(string $single, string $plural, int $number): string
|
||||
{
|
||||
return $this->__n($single, $plural, $number);
|
||||
}
|
||||
|
||||
// TODO: dgettext(string $domain, string $message): string
|
||||
// TODO: dngettext(string $domain, string $singular, string $plural, int $count): string
|
||||
// TODO: dpgettext(string $domain, string $message, int $category): string
|
||||
// TODO: dpngettext(string $domain, string $singular, string $plural, int $count, int $category): string
|
||||
}
|
||||
|
||||
// __END__
|
||||
290
4dev/deprecated/CoreLibs/Output/Form/Elements.php
Normal file
290
4dev/deprecated/CoreLibs/Output/Form/Elements.php
Normal file
@@ -0,0 +1,290 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* elements for html output direct
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Output\Form;
|
||||
|
||||
class Elements
|
||||
{
|
||||
/**
|
||||
* print the date/time drop downs, used in any queue/send/insert at date/time place
|
||||
*
|
||||
* @param int $year year YYYY
|
||||
* @param int $month month m
|
||||
* @param int $day day d
|
||||
* @param int $hour hour H
|
||||
* @param int $min min i
|
||||
* @param string $suffix additional info printed after the date time
|
||||
* variable in the drop down
|
||||
* also used for ID in the on change JS call
|
||||
* @param int $min_steps default is 1 (minute), can set to anything,
|
||||
* is used as sum up from 0
|
||||
* @param bool $name_pos_back default false, if set to true,
|
||||
* the name will be printend after the drop down
|
||||
* and not before the drop down
|
||||
* @return string HTML formated strings for drop down lists of date and time
|
||||
*/
|
||||
public static function printDateTime(
|
||||
$year,
|
||||
$month,
|
||||
$day,
|
||||
$hour,
|
||||
$min,
|
||||
string $suffix = '',
|
||||
int $min_steps = 1,
|
||||
bool $name_pos_back = false
|
||||
) {
|
||||
// if suffix given, add _ before
|
||||
if ($suffix) {
|
||||
$suffix = '_' . $suffix;
|
||||
}
|
||||
if ($min_steps < 1 || $min_steps > 59) {
|
||||
$min_steps = 1;
|
||||
}
|
||||
|
||||
$on_change_call = 'dt_list(\'' . $suffix . '\');';
|
||||
|
||||
// always be 1h ahead (for safety)
|
||||
$timestamp = time() + 3600; // in seconds
|
||||
|
||||
// the max year is this year + 1;
|
||||
$max_year = (int)date("Y", $timestamp) + 1;
|
||||
|
||||
// preset year, month, ...
|
||||
$year = !$year ? date('Y', $timestamp) : $year;
|
||||
$month = !$month ? date('m', $timestamp) : $month;
|
||||
$day = !$day ? date('d', $timestamp) : $day;
|
||||
$hour = !$hour ? date('H', $timestamp) : $hour;
|
||||
$min = !$min ? date('i', $timestamp) : $min; // add to five min?
|
||||
// max days in selected month
|
||||
$days_in_month = date(
|
||||
't',
|
||||
strtotime($year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $min . ':0') ?: null
|
||||
);
|
||||
$string = '';
|
||||
// from now to ?
|
||||
if ($name_pos_back === false) {
|
||||
$string = 'Year ';
|
||||
}
|
||||
$string .= '<select id="year' . $suffix . '" name="year' . $suffix . '" onChange="' . $on_change_call . '">';
|
||||
for ($i = date("Y"); $i <= $max_year; $i++) {
|
||||
$string .= '<option value="' . $i . '" ' . ($year == $i ? 'selected' : '') . '>' . $i . '</option>';
|
||||
}
|
||||
$string .= '</select> ';
|
||||
if ($name_pos_back === true) {
|
||||
$string .= 'Year ';
|
||||
}
|
||||
if ($name_pos_back === false) {
|
||||
$string .= 'Month ';
|
||||
}
|
||||
$string .= '<select id="month' . $suffix . '" name="month' . $suffix . '" onChange="' . $on_change_call . '">';
|
||||
for ($i = 1; $i <= 12; $i++) {
|
||||
$string .= '<option value="' . ($i < 10 ? '0' . $i : $i) . '" '
|
||||
. ($month == $i ? 'selected' : '') . '>' . $i . '</option>';
|
||||
}
|
||||
$string .= '</select> ';
|
||||
if ($name_pos_back === true) {
|
||||
$string .= 'Month ';
|
||||
}
|
||||
if ($name_pos_back === false) {
|
||||
$string .= 'Day ';
|
||||
}
|
||||
$string .= '<select id="day' . $suffix . '" name="day' . $suffix . '" onChange="' . $on_change_call . '">';
|
||||
for ($i = 1; $i <= $days_in_month; $i++) {
|
||||
// set weekday text based on current month ($month) and year ($year)
|
||||
$string .= '<option value="' . ($i < 10 ? '0' . $i : $i) . '" '
|
||||
. ($day == $i ? 'selected' : '') . '>' . $i
|
||||
. ' (' . date('D', mktime(0, 0, 0, (int)$month, $i, (int)$year) ?: null) . ')</option>';
|
||||
}
|
||||
$string .= '</select> ';
|
||||
if ($name_pos_back === true) {
|
||||
$string .= 'Day ';
|
||||
}
|
||||
if ($name_pos_back === false) {
|
||||
$string .= 'Hour ';
|
||||
}
|
||||
$string .= '<select id="hour' . $suffix . '" name="hour' . $suffix . '" onChange="' . $on_change_call . '">';
|
||||
for ($i = 0; $i <= 23; $i += $min_steps) {
|
||||
$string .= '<option value="' . ($i < 10 ? '0' . $i : $i)
|
||||
. '" ' . ($hour == $i ? 'selected' : '') . '>' . $i . '</option>';
|
||||
}
|
||||
$string .= '</select> ';
|
||||
if ($name_pos_back === true) {
|
||||
$string .= 'Hour ';
|
||||
}
|
||||
if ($name_pos_back === false) {
|
||||
$string .= 'Minute ';
|
||||
}
|
||||
$string .= '<select id="min' . $suffix . '" name="min'
|
||||
. $suffix . '" onChange="' . $on_change_call . '">';
|
||||
for ($i = 0; $i <= 59; $i++) {
|
||||
$string .= '<option value="' . ($i < 10 ? '0' . $i : $i)
|
||||
. '" ' . ($min == $i ? 'selected' : '') . '>' . $i . '</option>';
|
||||
}
|
||||
$string .= '</select>';
|
||||
if ($name_pos_back === true) {
|
||||
$string .= ' Minute ';
|
||||
}
|
||||
// return the datetime select string
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* tries to find mailto:user@bubu.at and changes it into ->
|
||||
* <a href="mailto:user@bubu.at">E-Mail senden</a>
|
||||
* or tries to take any url (http, ftp, etc) and transform it into a valid URL
|
||||
* the string is in the format: some url|name#css|, same for email
|
||||
*
|
||||
* @param string $string data to transform to a valid HTML url
|
||||
* @param string $target target string, default _blank
|
||||
* @return string correctly formed html url link
|
||||
*/
|
||||
public static function magicLinks(string $string, string $target = "_blank"): string
|
||||
{
|
||||
$output = $string;
|
||||
$protList = ["http", "https", "ftp", "news", "nntp"];
|
||||
|
||||
// find urls w/o protocol
|
||||
$output = preg_replace("/([^\/])www\.([\w\.-]+)\.([a-zA-Z]{2,4})/", "\\1http://www.\\2.\\3", $output) ?: '';
|
||||
$output = preg_replace("/([^\/])ftp\.([\w\.-]+)\.([a-zA-Z]{2,4})/", "\\1ftp://ftp.\\2.\\3", $output) ?: '';
|
||||
|
||||
// remove doubles, generate protocol-regex
|
||||
// DIRTY HACK
|
||||
$protRegex = "";
|
||||
foreach ($protList as $protocol) {
|
||||
if ($protRegex) {
|
||||
$protRegex .= "|";
|
||||
}
|
||||
$protRegex .= "$protocol:\/\/";
|
||||
}
|
||||
|
||||
// find urls w/ protocol
|
||||
// cs: escaped -, added / for http urls
|
||||
// added | |, this time mandatory, todo: if no | |use \\1\\2
|
||||
// backslash at the end of a url also allowed now
|
||||
// do not touch <.*=".."> things!
|
||||
// _1: URL or email
|
||||
// _2: atag (>)
|
||||
// _3: (_1) part of url or email [main url or email pre @ part]
|
||||
// _4: (_2) parameters of url or email post @ part
|
||||
// _5: (_3) parameters of url or tld part of email
|
||||
// _7: link name/email link name
|
||||
// _9: style sheet class
|
||||
$output = preg_replace_callback(
|
||||
"/(href=\")?(\>)?\b($protRegex)([\w\.\-?&=+%#~,;\/]+)\b([\.\-?&=+%#~,;\/]*)(\|([^\||^#]+)(#([^\|]+))?\|)?/",
|
||||
function ($matches) {
|
||||
return self::createUrl(
|
||||
$matches[1] ?? '',
|
||||
$matches[2] ?? '',
|
||||
$matches[3] ?? '',
|
||||
$matches[4] ?? '',
|
||||
$matches[5] ?? '',
|
||||
$matches[7] ?? '',
|
||||
$matches[9] ?? ''
|
||||
);
|
||||
},
|
||||
$output
|
||||
) ?: '';
|
||||
// find email-addresses, but not mailto prefix ones
|
||||
$output = preg_replace_callback(
|
||||
"/(mailto:)?(\>)?\b([\w\.-]+)@([\w\.\-]+)\.([a-zA-Z]{2,4})\b(\|([^\||^#]+)(#([^\|]+))?\|)?/",
|
||||
function ($matches) {
|
||||
return self::createEmail(
|
||||
$matches[1] ?? '',
|
||||
$matches[2] ?? '',
|
||||
$matches[3] ?? '',
|
||||
$matches[4] ?? '',
|
||||
$matches[5] ?? '',
|
||||
$matches[7] ?? '',
|
||||
$matches[9] ?? ''
|
||||
);
|
||||
},
|
||||
$output
|
||||
) ?: '';
|
||||
|
||||
// we have one slashes after the Protocol ->
|
||||
// internal link no domain, strip out the proto
|
||||
// $output = preg_replace("/($protRegex)\/(.*)/e", "\\2", $ouput);
|
||||
|
||||
// post processing
|
||||
$output = str_replace("{TARGET}", $target, $output);
|
||||
$output = str_replace("##LT##", "<", $output);
|
||||
$output = str_replace("##GT##", ">", $output);
|
||||
$output = str_replace("##QUOT##", "\"", $output);
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* internal function, called by the magic url create functions.
|
||||
* checks if title $_4 exists, if not, set url as title
|
||||
*
|
||||
* @param string $href url link
|
||||
* @param string $atag anchor tag (define both type or url)
|
||||
* @param string $_1 part of the URL, if atag is set, _1 is not used
|
||||
* @param string $_2 part of the URL
|
||||
* @param string $_3 part of the URL
|
||||
* @param string $name name for the url, if not given _2 + _3 is used
|
||||
* @param string $class style sheet
|
||||
* @return string correct string for url href process
|
||||
*/
|
||||
private static function createUrl($href, $atag, $_1, $_2, $_3, $name, $class): string
|
||||
{
|
||||
// $this->debug('URL', "1: $_1 - 2: $_2 - $_3 - atag: $atag - name: $name - class: $class");
|
||||
// if $_1 ends with //, then we strip $_1 complete & target is also blanked (its an internal link)
|
||||
if (preg_match("/\/\/$/", $_1) && preg_match("/^\//", $_2)) {
|
||||
$_1 = '';
|
||||
$target = '';
|
||||
} else {
|
||||
$target = '{TARGET}';
|
||||
}
|
||||
// if it is a link already just return the original link do not touch anything
|
||||
if (!$href && !$atag) {
|
||||
return "##LT##a href=##QUOT##" . $_1 . $_2 . $_3 . "##QUOT##"
|
||||
. ($class ? ' class=##QUOT##' . $class . '##QUOT##' : '')
|
||||
. ($target ? " target=##QUOT##" . $target . "##QUOT##" : '')
|
||||
. "##GT##" . ($name ? $name : $_2 . $_3) . "##LT##/a##GT##";
|
||||
} elseif ($href && !$atag) {
|
||||
return "href=##QUOT##$_1$_2$_3##QUOT##";
|
||||
} elseif ($atag) {
|
||||
return $atag . $_2 . $_3;
|
||||
} else {
|
||||
return $href;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* internal function for createing email, returns data to magic_url method
|
||||
*
|
||||
* @param string $mailto email address
|
||||
* @param string $atag atag (define type of url)
|
||||
* @param string $_1 parts of the email _1 before @, 3_ tld
|
||||
* @param string $_2 _2 domain part after @
|
||||
* @param string $_3 _3 tld
|
||||
* @param string $title name for the link, if not given use email
|
||||
* @param string $class style sheet
|
||||
* @return string created html email a href string
|
||||
*/
|
||||
private static function createEmail($mailto, $atag, $_1, $_2, $_3, $title, $class)
|
||||
{
|
||||
$email = $_1 . "@" . $_2 . "." . $_3;
|
||||
if (!$mailto && !$atag) {
|
||||
return "##LT##a href=##QUOT##mailto:" . $email . "##QUOT##"
|
||||
. ($class ? ' class=##QUOT##' . $class . '##QUOT##' : '')
|
||||
. "##GT##" . ($title ? $title : $email) . "##LT##/a##GT##";
|
||||
} elseif ($mailto && !$atag) {
|
||||
return "mailto:" . $email;
|
||||
} elseif ($atag) {
|
||||
return $atag . $email;
|
||||
} else {
|
||||
// else just return email as is
|
||||
return $email;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
2748
4dev/deprecated/CoreLibs/Output/Form/Generate.php
Normal file
2748
4dev/deprecated/CoreLibs/Output/Form/Generate.php
Normal file
File diff suppressed because it is too large
Load Diff
140
4dev/deprecated/CoreLibs/Output/Form/TableArrays/EditAccess.php
Normal file
140
4dev/deprecated/CoreLibs/Output/Form/TableArrays/EditAccess.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Output\Form\TableArrays;
|
||||
|
||||
class EditAccess implements \CoreLibs\Output\Form\TableArraysInterface
|
||||
{
|
||||
/** @var \CoreLibs\Output\Form\Generate */
|
||||
private $form;
|
||||
|
||||
/**
|
||||
* constructor
|
||||
* @param \CoreLibs\Output\Form\Generate $form base form class
|
||||
*/
|
||||
public function __construct(\CoreLibs\Output\Form\Generate $form)
|
||||
{
|
||||
$this->form = $form;
|
||||
$this->form->log->debug('CLASS LOAD', __NAMESPACE__ . __CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
* return the table array
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function setTableArray(): array
|
||||
{
|
||||
return [
|
||||
'table_array' => [
|
||||
'edit_access_id' => [
|
||||
'value' => $_POST['edit_access_id'] ?? '',
|
||||
'type' => 'hidden',
|
||||
'pk' => 1
|
||||
],
|
||||
'name' => [
|
||||
'value' => $_POST['name'] ?? '',
|
||||
'output_name' => 'Access Group Name',
|
||||
'mandatory' => 1,
|
||||
'type' => 'text',
|
||||
'error_check' => 'alphanumericspace|unique'
|
||||
],
|
||||
'description' => [
|
||||
'value' => $_POST['description'] ?? '',
|
||||
'output_name' => 'Description',
|
||||
'type' => 'textarea'
|
||||
],
|
||||
'color' => [
|
||||
'value' => $_POST['color'] ?? '',
|
||||
'output_name' => 'Color',
|
||||
'mandatory' => 0,
|
||||
'type' => 'text',
|
||||
'size' => 10,
|
||||
'length' => 9,
|
||||
'error_check' => 'custom',
|
||||
// FIXME: update regex check for hex/rgb/hsl with color check class
|
||||
'error_regex' => '/^#([\dA-Fa-f]{6}|[\dA-Fa-f]{8})$/',
|
||||
'error_example' => '#F6A544'
|
||||
],
|
||||
'enabled' => [
|
||||
'value' => $_POST['enabled'] ?? 0,
|
||||
'output_name' => 'Enabled',
|
||||
'type' => 'binary',
|
||||
'int' => 1, // OR 'bool' => 1
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
],
|
||||
'protected' => [
|
||||
'value' => $_POST['protected'] ?? 0,
|
||||
'output_name' => 'Protected',
|
||||
'type' => 'binary',
|
||||
'int' => 1,
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
],
|
||||
'additional_acl' => [
|
||||
'value' => $_POST['additional_acl'] ?? '',
|
||||
'output_name' => 'Additional ACL (as JSON)',
|
||||
'type' => 'textarea',
|
||||
'error_check' => 'json',
|
||||
'rows' => 10,
|
||||
'cols' => 60
|
||||
],
|
||||
],
|
||||
'table_name' => 'edit_access',
|
||||
"load_query" => "SELECT edit_access_id, name FROM edit_access ORDER BY name",
|
||||
'show_fields' => [
|
||||
[
|
||||
'name' => 'name'
|
||||
],
|
||||
],
|
||||
'element_list' => [
|
||||
'edit_access_data' => [
|
||||
'output_name' => 'Edit Access Data',
|
||||
'delete_name' => 'remove_edit_access_data',
|
||||
// is not a sub table read and connect, but only a sub table with data
|
||||
// 'type' => 'reference_data',
|
||||
// maxium visible if no data is set, if filled add this number to visible
|
||||
'max_empty' => 5,
|
||||
'prefix' => 'ead',
|
||||
'elements' => [
|
||||
'name' => [
|
||||
'type' => 'text',
|
||||
'error_check' => 'alphanumeric|unique',
|
||||
'output_name' => 'Name',
|
||||
'mandatory' => 1
|
||||
],
|
||||
'value' => [
|
||||
'type' => 'text',
|
||||
'output_name' => 'Value'
|
||||
],
|
||||
'enabled' => [
|
||||
'type' => 'checkbox',
|
||||
'output_name' => 'Activate',
|
||||
'int' => 1,
|
||||
'element_list' => [1]
|
||||
],
|
||||
/*'edit_access_id' => [
|
||||
'int' => 1,
|
||||
'type' => 'hidden',
|
||||
// reference main key from master table above
|
||||
'fk_id' => 1
|
||||
],*/
|
||||
'edit_access_data_id' => [
|
||||
'type' => 'hidden',
|
||||
'int' => 1,
|
||||
'pk_id' => 1
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
137
4dev/deprecated/CoreLibs/Output/Form/TableArrays/EditGroups.php
Normal file
137
4dev/deprecated/CoreLibs/Output/Form/TableArrays/EditGroups.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Output\Form\TableArrays;
|
||||
|
||||
class EditGroups implements \CoreLibs\Output\Form\TableArraysInterface
|
||||
{
|
||||
/** @var \CoreLibs\Output\Form\Generate */
|
||||
private $form;
|
||||
|
||||
/**
|
||||
* constructor
|
||||
* @param \CoreLibs\Output\Form\Generate $form base form class
|
||||
*/
|
||||
public function __construct(\CoreLibs\Output\Form\Generate $form)
|
||||
{
|
||||
$this->form = $form;
|
||||
$this->form->log->debug('CLASS LOAD', __NAMESPACE__ . __CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
* return the table array
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function setTableArray(): array
|
||||
{
|
||||
return [
|
||||
'table_array' => [
|
||||
'edit_group_id' => [
|
||||
'value' => $_POST['edit_group_id'] ?? '',
|
||||
'pk' => 1,
|
||||
'type' => 'hidden'
|
||||
],
|
||||
'enabled' => [
|
||||
'value' => $_POST['enabled'] ?? '',
|
||||
'output_name' => 'Enabled',
|
||||
'int' => 1,
|
||||
'type' => 'binary',
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
],
|
||||
'name' => [
|
||||
'value' => $_POST['name'] ?? '',
|
||||
'output_name' => 'Group Name',
|
||||
'type' => 'text',
|
||||
'mandatory' => 1
|
||||
],
|
||||
'edit_access_right_id' => [
|
||||
'value' => $_POST['edit_access_right_id'] ?? '',
|
||||
'output_name' => 'Group Level',
|
||||
'mandatory' => 1,
|
||||
'int' => 1,
|
||||
'type' => 'drop_down_db',
|
||||
'query' => "SELECT edit_access_right_id, name FROM edit_access_right ORDER BY level"
|
||||
],
|
||||
'edit_scheme_id' => [
|
||||
'value' => $_POST['edit_scheme_id'] ?? '',
|
||||
'output_name' => 'Group Scheme',
|
||||
'int_null' => 1,
|
||||
'type' => 'drop_down_db',
|
||||
'query' => "SELECT edit_scheme_id, name FROM edit_scheme WHERE enabled = 1 ORDER BY name"
|
||||
],
|
||||
'additional_acl' => [
|
||||
'value' => $_POST['additional_acl'] ?? '',
|
||||
'output_name' => 'Additional ACL (as JSON)',
|
||||
'type' => 'textarea',
|
||||
'error_check' => 'json',
|
||||
'rows' => 10,
|
||||
'cols' => 60
|
||||
],
|
||||
],
|
||||
'load_query' => "SELECT edit_group_id, name, enabled FROM edit_group ORDER BY name",
|
||||
'table_name' => 'edit_group',
|
||||
'show_fields' => [
|
||||
[
|
||||
'name' => 'name'
|
||||
],
|
||||
[
|
||||
'name' => 'enabled',
|
||||
'binary' => ['Yes', 'No'],
|
||||
'before_value' => 'Enabled: '
|
||||
],
|
||||
],
|
||||
'element_list' => [
|
||||
'edit_page_access' => [
|
||||
'output_name' => 'Pages',
|
||||
'mandatory' => 1,
|
||||
'delete' => 0, // set then reference entries are deleted, else the 'enable' flag is only set
|
||||
'enable_name' => 'enable_page_access',
|
||||
'prefix' => 'epa',
|
||||
'read_data' => [
|
||||
'table_name' => 'edit_page',
|
||||
'pk_id' => 'edit_page_id',
|
||||
'name' => 'name',
|
||||
'order' => 'order_number'
|
||||
],
|
||||
'elements' => [
|
||||
'edit_page_access_id' => [
|
||||
'type' => 'hidden',
|
||||
'int' => 1,
|
||||
'pk_id' => 1
|
||||
],
|
||||
'enabled' => [
|
||||
'type' => 'checkbox',
|
||||
'output_name' => 'Activate',
|
||||
'int' => 1,
|
||||
'element_list' => [1],
|
||||
],
|
||||
'edit_access_right_id' => [
|
||||
'type' => 'drop_down_db',
|
||||
'output_name' => 'Access Level',
|
||||
'int' => 1,
|
||||
'preset' => 1, // first of the select
|
||||
'query' => "SELECT edit_access_right_id, name FROM edit_access_right ORDER BY level"
|
||||
],
|
||||
'edit_page_id' => [
|
||||
'int' => 1,
|
||||
'type' => 'hidden'
|
||||
],
|
||||
/*,
|
||||
'edit_default' => [
|
||||
'output_name' => 'Default',
|
||||
'type' => 'radio',
|
||||
'mandatory' => 1
|
||||
],*/
|
||||
],
|
||||
], // edit pages ggroup
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Output\Form\TableArrays;
|
||||
|
||||
class EditLanguages implements \CoreLibs\Output\Form\TableArraysInterface
|
||||
{
|
||||
/** @var \CoreLibs\Output\Form\Generate */
|
||||
private $form;
|
||||
|
||||
/**
|
||||
* constructor
|
||||
* @param \CoreLibs\Output\Form\Generate $form base form class
|
||||
*/
|
||||
public function __construct(\CoreLibs\Output\Form\Generate $form)
|
||||
{
|
||||
$this->form = $form;
|
||||
$this->form->log->debug('CLASS LOAD', __NAMESPACE__ . __CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
* return the table array
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function setTableArray(): array
|
||||
{
|
||||
return [
|
||||
'table_array' => [
|
||||
'edit_language_id' => [
|
||||
'value' => $_POST['edit_language_id'] ?? '',
|
||||
'type' => 'hidden',
|
||||
'pk' => 1
|
||||
],
|
||||
'short_name' => [
|
||||
'value' => $_POST['short_name'] ?? '',
|
||||
'output_name' => 'Language (short)',
|
||||
'mandatory' => 1,
|
||||
'type' => 'text',
|
||||
'size' => 2,
|
||||
'length' => 2
|
||||
],
|
||||
'long_name' => [
|
||||
'value' => $_POST['long_name'] ?? '',
|
||||
'output_name' => 'Language (long)',
|
||||
'mandatory' => 1,
|
||||
'type' => 'text',
|
||||
'size' => 40
|
||||
],
|
||||
'iso_name' => [
|
||||
'value' => $_POST['iso_name'] ?? '',
|
||||
'output_name' => 'ISO Code',
|
||||
'mandatory' => 1,
|
||||
'type' => 'text'
|
||||
],
|
||||
'order_number' => [
|
||||
'value' => $_POST['order_number'] ?? '',
|
||||
'int' => 1,
|
||||
'order' => 1
|
||||
],
|
||||
'enabled' => [
|
||||
'value' => $_POST['enabled'] ?? '',
|
||||
'output_name' => 'Enabled',
|
||||
'int' => 1,
|
||||
'type' => 'binary',
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
],
|
||||
'lang_default' => [
|
||||
'value' => $_POST['lang_default'] ?? '',
|
||||
'output_name' => 'Default Language',
|
||||
'int' => 1,
|
||||
'type' => 'binary',
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
],
|
||||
],
|
||||
'load_query' => "SELECT edit_language_id, long_name, iso_name, enabled "
|
||||
. "FROM edit_language "
|
||||
. "ORDER BY long_name",
|
||||
'show_fields' => [
|
||||
[
|
||||
'name' => 'long_name'
|
||||
],
|
||||
[
|
||||
'name' => 'iso_name',
|
||||
'before_value' => 'ISO: '
|
||||
],
|
||||
[
|
||||
'name' => 'enabled',
|
||||
'before_value' => 'Enabled: ',
|
||||
'binary' => ['Yes','No'],
|
||||
],
|
||||
],
|
||||
'table_name' => 'edit_language'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Output\Form\TableArrays;
|
||||
|
||||
class EditMenuGroup implements \CoreLibs\Output\Form\TableArraysInterface
|
||||
{
|
||||
/** @var \CoreLibs\Output\Form\Generate */
|
||||
private $form;
|
||||
|
||||
/**
|
||||
* constructor
|
||||
* @param \CoreLibs\Output\Form\Generate $form base form class
|
||||
*/
|
||||
public function __construct(\CoreLibs\Output\Form\Generate $form)
|
||||
{
|
||||
$this->form = $form;
|
||||
$this->form->log->debug('CLASS LOAD', __NAMESPACE__ . __CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
* return the table array
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function setTableArray(): array
|
||||
{
|
||||
return [
|
||||
'table_array' => [
|
||||
'edit_menu_group_id' => [
|
||||
'value' => $_POST['edit_menu_group_id'] ?? '',
|
||||
'type' => 'hidden',
|
||||
'pk' => 1
|
||||
],
|
||||
'name' => [
|
||||
'value' => $_POST['name'] ?? '',
|
||||
'output_name' => 'Group name',
|
||||
'mandatory' => 1,
|
||||
'type' => 'text'
|
||||
],
|
||||
'flag' => [
|
||||
'value' => $_POST['flag'] ?? '',
|
||||
'output_name' => 'Flag',
|
||||
'mandatory' => 1,
|
||||
'type' => 'text',
|
||||
'error_check' => 'alphanumeric|unique'
|
||||
],
|
||||
'order_number' => [
|
||||
'value' => $_POST['order_number'] ?? '',
|
||||
'output_name' => 'Group order',
|
||||
'type' => 'order',
|
||||
'int' => 1,
|
||||
'order' => 1
|
||||
],
|
||||
],
|
||||
'table_name' => 'edit_menu_group',
|
||||
'load_query' => "SELECT edit_menu_group_id, name FROM edit_menu_group ORDER BY name",
|
||||
'show_fields' => [
|
||||
[
|
||||
'name' => 'name'
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
275
4dev/deprecated/CoreLibs/Output/Form/TableArrays/EditPages.php
Normal file
275
4dev/deprecated/CoreLibs/Output/Form/TableArrays/EditPages.php
Normal file
@@ -0,0 +1,275 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Output\Form\TableArrays;
|
||||
|
||||
class EditPages implements \CoreLibs\Output\Form\TableArraysInterface
|
||||
{
|
||||
/** @var \CoreLibs\Output\Form\Generate */
|
||||
private $form;
|
||||
|
||||
/**
|
||||
* constructor
|
||||
* @param \CoreLibs\Output\Form\Generate $form base form class
|
||||
*/
|
||||
public function __construct(\CoreLibs\Output\Form\Generate $form)
|
||||
{
|
||||
$this->form = $form;
|
||||
$this->form->log->debug('CLASS LOAD', __NAMESPACE__ . __CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
* return the table array
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function setTableArray(): array
|
||||
{
|
||||
return [
|
||||
'table_array' => [
|
||||
'edit_page_id' => [
|
||||
'value' => $_POST['edit_page_id'] ?? '',
|
||||
'type' => 'hidden',
|
||||
'pk' => 1
|
||||
],
|
||||
'filename' => [
|
||||
'value' => $_POST['filename'] ?? '',
|
||||
'output_name' => 'Add File ...',
|
||||
'mandatory' => 1,
|
||||
'type' => 'drop_down_db',
|
||||
'query' => "SELECT DISTINCT temp_files.filename AS id, "
|
||||
. "temp_files.folder || temp_files.filename AS name "
|
||||
. "FROM temp_files "
|
||||
. "LEFT JOIN edit_page ep ON temp_files.filename = ep.filename "
|
||||
. "WHERE ep.filename IS NULL"
|
||||
],
|
||||
'hostname' => [
|
||||
'value' => $_POST['hostname'] ?? '',
|
||||
'output_name' => 'Hostname or folder',
|
||||
'type' => 'text'
|
||||
],
|
||||
'name' => [
|
||||
'value' => $_POST['name'] ?? '',
|
||||
'output_name' => 'Page name',
|
||||
'mandatory' => 1,
|
||||
'type' => 'text'
|
||||
],
|
||||
'order_number' => [
|
||||
'value' => $_POST['order_number'] ?? '',
|
||||
'output_name' => 'Page order',
|
||||
'type' => 'order',
|
||||
'int' => 1,
|
||||
'order' => 1
|
||||
],
|
||||
/* 'flag' => [
|
||||
'value' => $_POST['flag']) ?? '',
|
||||
'output_name' => 'Page Flag',
|
||||
'type' => 'drop_down_array',
|
||||
'query' => [
|
||||
'0' => '0',
|
||||
'1' => '1',
|
||||
'2' => '2',
|
||||
'3' => '3',
|
||||
'4' => '4',
|
||||
'5' => '5'
|
||||
],
|
||||
],*/
|
||||
'online' => [
|
||||
'value' => $_POST['online'] ?? '',
|
||||
'output_name' => 'Online',
|
||||
'int' => 1,
|
||||
'type' => 'binary',
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
],
|
||||
'menu' => [
|
||||
'value' => $_POST['menu'] ?? '',
|
||||
'output_name' => 'Menu',
|
||||
'int' => 1,
|
||||
'type' => 'binary',
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
],
|
||||
'popup' => [
|
||||
'value' => $_POST['popup'] ?? '',
|
||||
'output_name' => 'Popup',
|
||||
'int' => 1,
|
||||
'type' => 'binary',
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
],
|
||||
'popup_x' => [
|
||||
'value' => $_POST['popup_x'] ?? '',
|
||||
'output_name' => 'Popup Width',
|
||||
'int_null' => 1,
|
||||
'type' => 'text',
|
||||
'size' => 4,
|
||||
'length' => 4
|
||||
],
|
||||
'popup_y' => [
|
||||
'value' => $_POST['popup_y'] ?? '',
|
||||
'output_name' => 'Popup Height',
|
||||
'int_null' => 1,
|
||||
'type' => 'text',
|
||||
'size' => 4,
|
||||
'length' => 4
|
||||
],
|
||||
'content_alias_edit_page_id' => [
|
||||
'value' => $_POST['content_alias_edit_page_id'] ?? '',
|
||||
'output_name' => 'Content Alias Source',
|
||||
'int_null' => 1,
|
||||
'type' => 'drop_down_db',
|
||||
// query creation
|
||||
'select_distinct' => 0,
|
||||
'pk_name' => 'edit_page_id AS content_alias_edit_page_id',
|
||||
'input_name' => 'name',
|
||||
'table_name' => 'edit_page',
|
||||
'where_not_self' => 1,
|
||||
'order_by' => 'order_number'
|
||||
// 'query' => "SELECT edit_page_id AS content_alias_edit_page_id, name ".
|
||||
// "FROM edit_page ".
|
||||
// (!empty($_POST['edit_page_id']) ? " WHERE edit_page_id <> ".$_POST['edit_page_id'] : "")." ".
|
||||
// "ORDER BY order_number"
|
||||
],
|
||||
],
|
||||
'load_query' => "SELECT edit_page_id, "
|
||||
. "CASE WHEN hostname IS NOT NULL THEN hostname ELSE ''::VARCHAR END || filename AS filename, "
|
||||
. "name, online, menu, popup "
|
||||
. "FROM edit_page "
|
||||
. "ORDER BY order_number",
|
||||
'table_name' => 'edit_page',
|
||||
'show_fields' => [
|
||||
[
|
||||
'name' => 'name'
|
||||
],
|
||||
[
|
||||
'name' => 'filename',
|
||||
'before_value' => 'Filename: '
|
||||
],
|
||||
[
|
||||
'name' => 'online',
|
||||
'binary' => ['Yes', 'No'],
|
||||
'before_value' => 'Online: '
|
||||
],
|
||||
[
|
||||
'name' => 'menu',
|
||||
'binary' => ['Yes', 'No'],
|
||||
'before_value' => 'Menu: '
|
||||
],
|
||||
[
|
||||
'name' => 'popup',
|
||||
'binary' => ['Yes', 'No'],
|
||||
'before_value' => 'Popup: '
|
||||
],
|
||||
],
|
||||
'reference_arrays' => [
|
||||
'edit_visible_group' => [
|
||||
'table_name' => 'edit_page_visible_group',
|
||||
'other_table_pk' => 'edit_visible_group_id',
|
||||
'output_name' => 'Visible Groups (access)',
|
||||
'mandatory' => 1,
|
||||
'select_size' => 10,
|
||||
'selected' => $_POST['edit_visible_group_id'] ?? '',
|
||||
'query' => "SELECT edit_visible_group_id, 'Name: ' || name || ', ' || 'Flag: ' || flag "
|
||||
. "FROM edit_visible_group ORDER BY name"
|
||||
],
|
||||
'edit_menu_group' => [
|
||||
'table_name' => 'edit_page_menu_group',
|
||||
'other_table_pk' => 'edit_menu_group_id',
|
||||
'output_name' => 'Menu Groups (grouping)',
|
||||
'mandatory' => 1,
|
||||
'select_size' => 10,
|
||||
'selected' => $_POST['edit_menu_group_id'] ?? '',
|
||||
'query' => "SELECT edit_menu_group_id, 'Name: ' || name || ', ' || 'Flag: ' || flag "
|
||||
. "FROM edit_menu_group ORDER BY order_number"
|
||||
],
|
||||
],
|
||||
'element_list' => [
|
||||
'edit_query_string' => [
|
||||
'output_name' => 'Query Strings',
|
||||
'delete_name' => 'remove_query_string',
|
||||
'prefix' => 'eqs',
|
||||
'elements' => [
|
||||
'name' => [
|
||||
'output_name' => 'Name',
|
||||
'type' => 'text',
|
||||
'error_check' => 'unique|alphanumeric',
|
||||
'mandatory' => 1
|
||||
],
|
||||
'value' => [
|
||||
'output_name' => 'Value',
|
||||
'type' => 'text'
|
||||
],
|
||||
'enabled' => [
|
||||
'output_name' => 'Enabled',
|
||||
'int' => 1,
|
||||
'type' => 'checkbox',
|
||||
'element_list' => [1],
|
||||
],
|
||||
'dynamic' => [
|
||||
'output_name' => 'Dynamic',
|
||||
'int' => 1,
|
||||
'type' => 'checkbox',
|
||||
'element_list' => [1],
|
||||
],
|
||||
'edit_query_string_id' => [
|
||||
'type' => 'hidden',
|
||||
'pk_id' => 1
|
||||
],
|
||||
], // elements
|
||||
], // query_string element list
|
||||
'edit_page_content' => [
|
||||
'output_name' => 'Page Content',
|
||||
'delete_name' => 'remove_page_content',
|
||||
'prefix' => 'epc',
|
||||
'elements' => [
|
||||
'name' => [
|
||||
'output_name' => 'Content',
|
||||
'type' => 'text',
|
||||
'error_check' => 'alphanumeric',
|
||||
'mandatory' => 1
|
||||
],
|
||||
'uid' => [
|
||||
'output_name' => 'UID',
|
||||
'type' => 'text',
|
||||
'error_check' => 'unique|alphanumeric',
|
||||
'mandatory' => 1
|
||||
],
|
||||
'order_number' => [
|
||||
'output_name' => 'Order',
|
||||
'type' => 'text',
|
||||
'error_check' => 'int',
|
||||
'mandatory' => 1
|
||||
],
|
||||
'online' => [
|
||||
'output_name' => 'Online',
|
||||
'int' => 1,
|
||||
'type' => 'checkbox',
|
||||
'element_list' => [1],
|
||||
],
|
||||
'edit_access_right_id' => [
|
||||
'type' => 'drop_down_db',
|
||||
'output_name' => 'Access Level',
|
||||
'int' => 1,
|
||||
'preset' => 1, // first of the select
|
||||
'query' => "SELECT edit_access_right_id, name FROM edit_access_right ORDER BY level"
|
||||
],
|
||||
'edit_page_content_id' => [
|
||||
'type' => 'hidden',
|
||||
'pk_id' => 1
|
||||
],
|
||||
],
|
||||
],
|
||||
], // element list
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Output\Form\TableArrays;
|
||||
|
||||
class EditSchemas implements \CoreLibs\Output\Form\TableArraysInterface
|
||||
{
|
||||
/** @var \CoreLibs\Output\Form\Generate */
|
||||
private $form;
|
||||
|
||||
/**
|
||||
* constructor
|
||||
* @param \CoreLibs\Output\Form\Generate $form base form class
|
||||
*/
|
||||
public function __construct(\CoreLibs\Output\Form\Generate $form)
|
||||
{
|
||||
$this->form = $form;
|
||||
$this->form->log->debug('CLASS LOAD', __NAMESPACE__ . __CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
* return the table array
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function setTableArray(): array
|
||||
{
|
||||
return [
|
||||
'table_array' => [
|
||||
'edit_scheme_id' => [
|
||||
'value' => $_POST['edit_scheme_id'] ?? '',
|
||||
'type' => 'hidden',
|
||||
'pk' => 1
|
||||
],
|
||||
'name' => [
|
||||
'value' => $_POST['name'] ?? '',
|
||||
'output_name' => 'Scheme Name',
|
||||
'mandatory' => 1,
|
||||
'type' => 'text'
|
||||
],
|
||||
'header_color' => [
|
||||
'value' => $_POST['header_color'] ?? '',
|
||||
'output_name' => 'Header Color',
|
||||
'mandatory' => 1,
|
||||
'type' => 'text',
|
||||
'size' => 10,
|
||||
'length' => 9,
|
||||
'error_check' => 'custom',
|
||||
// FIXME: update regex check for hex/rgb/hsl with color check class
|
||||
'error_regex' => '/^#([\dA-Fa-f]{6}|[\dA-Fa-f]{8})$/',
|
||||
'error_example' => '#F6A544'
|
||||
],
|
||||
'enabled' => [
|
||||
'value' => $_POST['enabled'] ?? '',
|
||||
'output_name' => 'Enabled',
|
||||
'int' => 1,
|
||||
'type' => 'binary',
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
],
|
||||
'template' => [
|
||||
'value' => $_POST['template'] ?? '',
|
||||
'output_name' => 'Template',
|
||||
'type' => 'text'
|
||||
],
|
||||
],
|
||||
'table_name' => 'edit_scheme',
|
||||
'load_query' => "SELECT edit_scheme_id, name, enabled FROM edit_scheme ORDER BY name",
|
||||
'show_fields' => [
|
||||
[
|
||||
'name' => 'name'
|
||||
],
|
||||
[
|
||||
'name' => 'enabled',
|
||||
'binary' => ['Yes', 'No'],
|
||||
'before_value' => 'Enabled: '
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
456
4dev/deprecated/CoreLibs/Output/Form/TableArrays/EditUsers.php
Normal file
456
4dev/deprecated/CoreLibs/Output/Form/TableArrays/EditUsers.php
Normal file
@@ -0,0 +1,456 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Output\Form\TableArrays;
|
||||
|
||||
class EditUsers implements \CoreLibs\Output\Form\TableArraysInterface
|
||||
{
|
||||
/** @var \CoreLibs\Output\Form\Generate */
|
||||
private $form;
|
||||
|
||||
/**
|
||||
* constructor
|
||||
* @param \CoreLibs\Output\Form\Generate $form base form class
|
||||
*/
|
||||
public function __construct(\CoreLibs\Output\Form\Generate $form)
|
||||
{
|
||||
$this->form = $form;
|
||||
$this->form->log->debug('CLASS LOAD', __NAMESPACE__ . __CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
* return the table array
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function setTableArray(): array
|
||||
{
|
||||
return [
|
||||
'table_array' => [
|
||||
'edit_user_id' => [
|
||||
'value' => $_POST['edit_user_id'] ?? '',
|
||||
'type' => 'hidden',
|
||||
'pk' => 1,
|
||||
'int' => 1
|
||||
],
|
||||
'username' => [
|
||||
'value' => $_POST['username'] ?? '',
|
||||
'output_name' => 'Username',
|
||||
'mandatory' => 1,
|
||||
'error_check' => 'unique|alphanumericextended',
|
||||
'type' => 'text',
|
||||
// if not min_edit_acl only read
|
||||
// if not min_show_acl not visible
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '-1',
|
||||
],
|
||||
'password' => [
|
||||
'value' => $_POST['password'] ?? '',
|
||||
'HIDDEN_value' => $_POST['HIDDEN_password'] ?? '',
|
||||
'CONFIRM_value' => $_POST['CONFIRM_password'] ?? '',
|
||||
'output_name' => 'Password',
|
||||
'mandatory' => 1,
|
||||
'type' => 'password', // later has to be password for encryption in database
|
||||
'update' => [ // connected field updates, and update data
|
||||
'password_change_date' => [ // db row to update
|
||||
'type' => 'date', // type of field (int/text/date/etc)
|
||||
'value' => 'NOW()' // value [todo: complex reference
|
||||
],
|
||||
],
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
// password date when first insert and password is set, needs special field with connection to password
|
||||
// password reset force interval, if set, user needs to reset password after X time period
|
||||
'password_change_interval' => [
|
||||
'value' => $_POST['password_change_interval'] ?? '',
|
||||
'output_name' => 'Password change interval',
|
||||
// can be any date length format. n Y/M/D [not H/M/S], only one set, no combination
|
||||
'error_check' => 'intervalshort',
|
||||
'type' => 'text',
|
||||
'interval' => 1, // interval needs NULL write for empty
|
||||
'size' => 5, // make it 5 chars long
|
||||
'length' => 5,
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'enabled' => [
|
||||
'value' => $_POST['enabled'] ?? '',
|
||||
'output_name' => 'Enabled',
|
||||
'type' => 'binary',
|
||||
'int' => 1,
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '-1',
|
||||
],
|
||||
'deleted' => [
|
||||
'value' => $_POST['deleted'] ?? '',
|
||||
'output_name' => 'Deleted',
|
||||
'type' => 'binary',
|
||||
'int' => 1,
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'strict' => [
|
||||
'value' => $_POST['strict'] ?? '',
|
||||
'output_name' => 'Strict (Lock after errors)',
|
||||
'type' => 'binary',
|
||||
'int' => 1,
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'locked' => [
|
||||
'value' => $_POST['locked'] ?? '',
|
||||
'output_name' => 'Locked (auto set if strict with errors)',
|
||||
'type' => 'binary',
|
||||
'int' => 1,
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'admin' => [
|
||||
'value' => $_POST['admin'] ?? '',
|
||||
'output_name' => 'Admin',
|
||||
'type' => 'binary',
|
||||
'int' => 1,
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'debug' => [
|
||||
'value' => $_POST['debug'] ?? '',
|
||||
'output_name' => 'Debug',
|
||||
'type' => 'binary',
|
||||
'int' => 1,
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'db_debug' => [
|
||||
'value' => $_POST['db_debug'] ?? '',
|
||||
'output_name' => 'DB Debug',
|
||||
'type' => 'binary',
|
||||
'int' => 1,
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'email' => [
|
||||
'value' => $_POST['email'] ?? '',
|
||||
'output_name' => 'E-Mail',
|
||||
'type' => 'text',
|
||||
'error_check' => 'email',
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'last_name' => [
|
||||
'value' => $_POST['last_name'] ?? '',
|
||||
'output_name' => 'Last Name',
|
||||
'type' => 'text',
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'first_name' => [
|
||||
'value' => $_POST['first_name'] ?? '',
|
||||
'output_name' => 'First Name',
|
||||
'type' => 'text',
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'lock_until' => [
|
||||
'value' => $_POST['lock_until'] ?? '',
|
||||
'output_name' => 'Lock account until',
|
||||
'type' => 'datetime',
|
||||
'error_check' => 'datetime',
|
||||
'sql_read' => 'YYYY-MM-DD HH24:MI',
|
||||
'datetime' => 1,
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'lock_after' => [
|
||||
'value' => $_POST['lock_after'] ?? '',
|
||||
'output_name' => 'Lock account after',
|
||||
'type' => 'datetime',
|
||||
'error_check' => 'datetime',
|
||||
'sql_read' => 'YYYY-MM-DD HH24:MI',
|
||||
'datetime' => 1,'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'login_user_id' => [
|
||||
'value' => $_POST['login_user_id'] ?? '',
|
||||
'output_name' => '_GET/_POST loginUserId direct login ID',
|
||||
'type' => 'text',
|
||||
'error_check' => 'unique|custom',
|
||||
'error_regex' => "/^[A-Za-z0-9]+$/",
|
||||
'emptynull' => 1,'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'login_user_id_set_date' => [
|
||||
'output_name' => 'loginUserId set date',
|
||||
'value' => $_POST['login_user_id_set_date'] ?? '',
|
||||
'type' => 'view',
|
||||
'empty' => '-',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'login_user_id_last_revalidate' => [
|
||||
'output_name' => 'loginUserId last revalidate date',
|
||||
'value' => $_POST['login_user_id_last_revalidate'] ?? '',
|
||||
'type' => 'view',
|
||||
'empty' => '-',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'login_user_id_locked' => [
|
||||
'value' => $_POST['login_user_id_locked'] ?? '',
|
||||
'output_name' => 'loginUserId usage locked',
|
||||
'type' => 'binary',
|
||||
'int' => 1,
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'login_user_id_revalidate_after' => [
|
||||
'value' => $_POST['login_user_id_revalidate_after'] ?? '',
|
||||
'output_name' => 'loginUserId, User must login after n days',
|
||||
'type' => 'text',
|
||||
'error_check' => 'intervalshort',
|
||||
'interval' => 1, // interval needs NULL write for empty
|
||||
'size' => 5, // make it 5 chars long
|
||||
'length' => 5,
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'login_user_id_valid_from' => [
|
||||
'value' => $_POST['login_user_id_valid_from'] ?? '',
|
||||
'output_name' => 'loginUserId valid from',
|
||||
'type' => 'datetime',
|
||||
'error_check' => 'datetime',
|
||||
'sql_read' => 'YYYY-MM-DD HH24:MI',
|
||||
'datetime' => 1,
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'login_user_id_valid_until' => [
|
||||
'value' => $_POST['login_user_id_valid_until'] ?? '',
|
||||
'output_name' => 'loginUserId valid until',
|
||||
'type' => 'datetime',
|
||||
'error_check' => 'datetime',
|
||||
'sql_read' => 'YYYY-MM-DD HH24:MI',
|
||||
'datetime' => 1,
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'edit_language_id' => [
|
||||
'value' => $_POST['edit_language_id'] ?? '',
|
||||
'output_name' => 'Language',
|
||||
'mandatory' => 1,
|
||||
'int' => 1,
|
||||
'type' => 'drop_down_db',
|
||||
'query' => "SELECT edit_language_id, long_name "
|
||||
. "FROM edit_language "
|
||||
. "WHERE enabled = 1"
|
||||
. "ORDER BY order_number",
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'edit_scheme_id' => [
|
||||
'value' => $_POST['edit_scheme_id'] ?? '',
|
||||
'output_name' => 'Scheme',
|
||||
'int_null' => 1,
|
||||
'type' => 'drop_down_db',
|
||||
'query' => "SELECT edit_scheme_id, name FROM edit_scheme WHERE enabled = 1 ORDER BY name",
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'edit_group_id' => [
|
||||
'value' => $_POST['edit_group_id'] ?? '',
|
||||
'output_name' => 'Group',
|
||||
'int' => 1,
|
||||
'type' => 'drop_down_db',
|
||||
'query' => "SELECT edit_group_id, name FROM edit_group WHERE enabled = 1 ORDER BY name",
|
||||
'mandatory' => 1,
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'edit_access_right_id' => [
|
||||
'value' => $_POST['edit_access_right_id'] ?? '',
|
||||
'output_name' => 'User Level',
|
||||
'mandatory' => 1,
|
||||
'int' => 1,
|
||||
'type' => 'drop_down_db',
|
||||
'query' => "SELECT edit_access_right_id, name FROM edit_access_right ORDER BY level",
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'login_error_count' => [
|
||||
'output_name' => 'Login error count',
|
||||
'value' => $_POST['login_error_count'] ?? '',
|
||||
'type' => 'view',
|
||||
'empty' => '0',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'login_error_date_last' => [
|
||||
'output_name' => 'Last login error',
|
||||
'value' => $_POST['login_error_date_liast'] ?? '',
|
||||
'type' => 'view',
|
||||
'empty' => '-',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'login_error_date_first' => [
|
||||
'output_name' => 'First login error',
|
||||
'value' => $_POST['login_error_date_first'] ?? '',
|
||||
'type' => 'view',
|
||||
'empty' => '-',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'protected' => [
|
||||
'value' => $_POST['protected'] ?? '',
|
||||
'output_name' => 'Protected',
|
||||
'type' => 'binary',
|
||||
'int' => 1,
|
||||
'element_list' => [
|
||||
'1' => 'Yes',
|
||||
'0' => 'No'
|
||||
],
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
'additional_acl' => [
|
||||
'value' => $_POST['additional_acl'] ?? '',
|
||||
'output_name' => 'Additional ACL (as JSON)',
|
||||
'type' => 'textarea',
|
||||
'error_check' => 'json',
|
||||
'rows' => 10,
|
||||
'cols' => 60,
|
||||
'min_edit_acl' => '100',
|
||||
'min_show_acl' => '100',
|
||||
],
|
||||
],
|
||||
'load_query' => "SELECT edit_user_id, username, enabled, deleted, "
|
||||
. "strict, locked, login_error_count "
|
||||
. "FROM edit_user "
|
||||
// if base acl is not 90 only list enabled
|
||||
// if not admin flag, do not list admin flagged
|
||||
. (
|
||||
!$this->form->getAclAdmin() ?
|
||||
"WHERE admin = 0 "
|
||||
. (
|
||||
!$this->form->checkBaseACL(90) ?
|
||||
// $_POST['base_acl_level'] < 90 ?
|
||||
"AND enabled = 1 " :
|
||||
""
|
||||
)
|
||||
: ''
|
||||
)
|
||||
. "ORDER BY username",
|
||||
'table_name' => 'edit_user',
|
||||
'show_fields' => [
|
||||
[
|
||||
'name' => 'username'
|
||||
],
|
||||
[
|
||||
'name' => 'enabled',
|
||||
'binary' => ['Yes', 'No'],
|
||||
'before_value' => 'ENBL: '
|
||||
],
|
||||
[
|
||||
'name' => 'deleted',
|
||||
'binary' => ['Yes', 'No'],
|
||||
'before_value' => 'DEL: '
|
||||
],
|
||||
[
|
||||
'name' => 'strict',
|
||||
'binary' => ['Yes', 'No'],
|
||||
'before_value' => 'STRC: '
|
||||
],
|
||||
[
|
||||
'name' => 'locked',
|
||||
'binary' => ['Yes', 'No'],
|
||||
'before_value' => 'LCK: '
|
||||
],
|
||||
[
|
||||
'name' => 'login_error_count',
|
||||
'before_value' => 'ERR: '
|
||||
],
|
||||
],
|
||||
'element_list' => [
|
||||
'edit_access_user' => [
|
||||
'output_name' => 'Accounts',
|
||||
'mandatory' => 1,
|
||||
// set then reference entries are deleted, else the 'enable' flag is only set
|
||||
'delete' => 0,
|
||||
// acl
|
||||
'min_edit_acl' => '40',
|
||||
'min_show_acl' => '20',
|
||||
// table read prefix
|
||||
'prefix' => 'ecu',
|
||||
'read_data' => [
|
||||
'table_name' => 'edit_access',
|
||||
'pk_id' => 'edit_access_id',
|
||||
'name' => 'name',
|
||||
'order' => 'name'
|
||||
],
|
||||
'elements' => [
|
||||
'edit_access_user_id' => [
|
||||
'output_name' => 'Activate',
|
||||
'type' => 'hidden',
|
||||
'int' => 1,
|
||||
'pk_id' => 1
|
||||
],
|
||||
'enabled' => [
|
||||
'type' => 'checkbox',
|
||||
'output_name' => 'Activate',
|
||||
'int' => 1,
|
||||
'element_list' => [1],
|
||||
],
|
||||
'edit_access_right_id' => [
|
||||
'type' => 'drop_down_db',
|
||||
'output_name' => 'Access Level',
|
||||
'preset' => 1, // first of the select
|
||||
'int' => 1,
|
||||
'query' => "SELECT edit_access_right_id, name FROM edit_access_right ORDER BY level"
|
||||
],
|
||||
'edit_default' => [
|
||||
'type' => 'radio_group',
|
||||
'output_name' => 'Default',
|
||||
'int' => 1,
|
||||
'element_list' => 'radio_group'
|
||||
],
|
||||
'edit_access_id' => [
|
||||
'type' => 'hidden',
|
||||
'int' => 1
|
||||
],
|
||||
],
|
||||
], // edit pages ggroup
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Output\Form\TableArrays;
|
||||
|
||||
class EditVisibleGroup implements \CoreLibs\Output\Form\TableArraysInterface
|
||||
{
|
||||
/** @var \CoreLibs\Output\Form\Generate */
|
||||
private $form;
|
||||
|
||||
/**
|
||||
* constructor
|
||||
* @param \CoreLibs\Output\Form\Generate $form base form class
|
||||
*/
|
||||
public function __construct(\CoreLibs\Output\Form\Generate $form)
|
||||
{
|
||||
$this->form = $form;
|
||||
$this->form->log->debug('CLASS LOAD', __NAMESPACE__ . __CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
* return the table array
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function setTableArray(): array
|
||||
{
|
||||
return [
|
||||
'table_array' => [
|
||||
'edit_visible_group_id' => [
|
||||
'value' => $_POST['edit_visible_group_id'] ?? '',
|
||||
'type' => 'hidden',
|
||||
'pk' => 1
|
||||
],
|
||||
'name' => [
|
||||
'value' => $_POST['name'] ?? '',
|
||||
'output_name' => 'Group name',
|
||||
'mandatory' => 1,
|
||||
'type' => 'text'
|
||||
],
|
||||
'flag' => [
|
||||
'value' => $_POST['flag'] ?? '',
|
||||
'output_name' => 'Flag',
|
||||
'mandatory' => 1,
|
||||
'type' => 'text',
|
||||
'error_check' => 'alphanumeric|unique'
|
||||
],
|
||||
],
|
||||
'table_name' => 'edit_visible_group',
|
||||
'load_query' => "SELECT edit_visible_group_id, name FROM edit_visible_group ORDER BY name",
|
||||
'show_fields' => [
|
||||
[
|
||||
'name' => 'name'
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
45
4dev/deprecated/CoreLibs/Output/Form/Token.php
Normal file
45
4dev/deprecated/CoreLibs/Output/Form/Token.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* sets a form token in the _SESSION variable
|
||||
* session must be started for this to work
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Output\Form;
|
||||
|
||||
class Token
|
||||
{
|
||||
/**
|
||||
* sets a form token in a session and returns form token
|
||||
*
|
||||
* @param string $name optional form name, default form_token
|
||||
* @return string token name for given form id string
|
||||
*/
|
||||
public static function setFormToken(string $name = 'form_token'): string
|
||||
{
|
||||
// current hard set to sha256
|
||||
$token = uniqid(hash('sha256', (string)rand()));
|
||||
$_SESSION[$name] = $token;
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if the form token matches the session set form token
|
||||
*
|
||||
* @param string $token token string to check
|
||||
* @param string $name optional form name to check to, default form_token
|
||||
* @return bool false if not set, or true/false if matching or not mtaching
|
||||
*/
|
||||
public static function validateFormToken(string $token, string $name = 'form_token'): bool
|
||||
{
|
||||
if (isset($_SESSION[$name])) {
|
||||
return $_SESSION[$name] === $token;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// __END_
|
||||
493
4dev/deprecated/CoreLibs/Output/Image.php
Normal file
493
4dev/deprecated/CoreLibs/Output/Image.php
Normal file
@@ -0,0 +1,493 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* image thumbnail, rotate, etc
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Output;
|
||||
|
||||
class Image
|
||||
{
|
||||
/**
|
||||
* converts picture to a thumbnail with max x and max y size
|
||||
*
|
||||
* @param string $pic source image file with or without path
|
||||
* @param int $size_x maximum size width
|
||||
* @param int $size_y maximum size height
|
||||
* @param string $dummy empty, or file_type to show an icon
|
||||
* instead of nothing if file is not found
|
||||
* @param string $path if source start is not ROOT path,
|
||||
* if empty ROOT is choosen
|
||||
* @param string $cache_source cache path, if not given TMP is used
|
||||
* @param bool $clear_cache if set to true, will create thumb all the tame
|
||||
* @return string|bool thumbnail name, or false for error
|
||||
*/
|
||||
public static function createThumbnail(
|
||||
string $pic,
|
||||
int $size_x,
|
||||
int $size_y,
|
||||
string $dummy = '',
|
||||
string $path = '',
|
||||
string $cache_source = '',
|
||||
bool $clear_cache = false
|
||||
) {
|
||||
// get image type flags
|
||||
$image_types = [
|
||||
1 => 'gif',
|
||||
2 => 'jpg',
|
||||
3 => 'png'
|
||||
];
|
||||
$return_data = false;
|
||||
$CONVERT = '';
|
||||
// if CONVERT is not defined, abort
|
||||
/** @phan-suppress-next-line PhanUndeclaredConstant */
|
||||
if (defined('CONVERT') && is_executable(CONVERT)) {
|
||||
/** @phan-suppress-next-line PhanUndeclaredConstant */
|
||||
$CONVERT = CONVERT;
|
||||
} else {
|
||||
return $return_data;
|
||||
}
|
||||
if (!empty($cache_source)) {
|
||||
$tmp_src = $cache_source;
|
||||
} else {
|
||||
$tmp_src = BASE . TMP;
|
||||
}
|
||||
// check if pic has a path, and override next sets
|
||||
if (strstr($pic, '/') === false) {
|
||||
if (empty($path)) {
|
||||
$path = BASE;
|
||||
}
|
||||
$filename = $path . MEDIA . PICTURES . $pic;
|
||||
} else {
|
||||
$filename = $pic;
|
||||
// and get the last part for pic (the filename)
|
||||
$tmp = explode('/', $pic);
|
||||
$pic = $tmp[(count($tmp) - 1)];
|
||||
}
|
||||
// does this picture exist and is it a picture
|
||||
if (file_exists($filename) && is_file($filename)) {
|
||||
[$width, $height, $type] = getimagesize($filename) ?: [];
|
||||
$convert_prefix = '';
|
||||
$create_file = false;
|
||||
$delete_filename = '';
|
||||
// check if we can skip the PDF creation: if we have size, if do not have type, we assume type png
|
||||
if (!$type && is_numeric($size_x) && is_numeric($size_y)) {
|
||||
$check_thumb = $tmp_src . 'thumb_' . $pic . '_' . $size_x . 'x' . $size_y . '.' . $image_types[3];
|
||||
if (!is_file($check_thumb)) {
|
||||
$create_file = true;
|
||||
} else {
|
||||
$type = 3;
|
||||
}
|
||||
}
|
||||
// if type is not in the list, but returns as PDF, we need to convert to JPEG before
|
||||
if (!$type) {
|
||||
$output = [];
|
||||
$return = null;
|
||||
// is this a PDF, if no, return from here with nothing
|
||||
$convert_prefix = 'png:';
|
||||
# TEMP convert to PNG, we then override the file name
|
||||
$convert_string = $CONVERT . ' ' . $filename . ' ' . $convert_prefix . $filename . '_TEMP';
|
||||
$status = exec($convert_string, $output, $return);
|
||||
$filename .= '_TEMP';
|
||||
// for delete, in case we need to glob
|
||||
$delete_filename = $filename;
|
||||
// find file, if we can't find base name, use -0 as the first one (ignore other pages in multiple ones)
|
||||
if (!is_file($filename)) {
|
||||
$filename .= '-0';
|
||||
}
|
||||
[$width, $height, $type] = getimagesize($filename) ?: [];
|
||||
}
|
||||
// if no size given, set size to original
|
||||
if (!$size_x || $size_x < 1 || !is_numeric($size_x)) {
|
||||
$size_x = $width;
|
||||
}
|
||||
if (!$size_y || $size_y < 1 || !is_numeric($size_y)) {
|
||||
$size_y = $height;
|
||||
}
|
||||
$thumb = 'thumb_' . $pic . '_' . $size_x . 'x' . $size_y . '.' . $image_types[$type];
|
||||
$thumbnail = $tmp_src . $thumb;
|
||||
// check if we already have this picture converted
|
||||
if (!is_file($thumbnail) || $clear_cache == true) {
|
||||
// convert the picture
|
||||
if ($width > $size_x) {
|
||||
$convert_string = $CONVERT . ' -geometry ' . $size_x . 'x ' . $filename . ' ' . $thumbnail;
|
||||
$status = exec($convert_string, $output, $return);
|
||||
// get the size of the converted data, if converted
|
||||
if (is_file($thumbnail)) {
|
||||
[$width, $height, $type] = getimagesize($thumbnail) ?: [];
|
||||
}
|
||||
}
|
||||
if ($height > $size_y) {
|
||||
$convert_string = $CONVERT . ' -geometry x' . $size_y . ' ' . $filename . ' ' . $thumbnail;
|
||||
$status = exec($convert_string, $output, $return);
|
||||
}
|
||||
}
|
||||
if (!is_file($thumbnail)) {
|
||||
copy($filename, $thumbnail);
|
||||
}
|
||||
$return_data = $thumb;
|
||||
// if we have a delete filename, delete here with glob
|
||||
if ($delete_filename) {
|
||||
array_map('unlink', glob($delete_filename . '*') ?: []);
|
||||
}
|
||||
} else {
|
||||
if (!empty($dummy) && strstr($dummy, '/') === false) {
|
||||
// check if we have the "dummy" image flag set
|
||||
$filename = PICTURES . ICONS . strtoupper($dummy) . ".png";
|
||||
/** @phpstan-ignore-next-line */
|
||||
if (!empty($dummy) && file_exists($filename) && is_file($filename)) {
|
||||
$return_data = $filename;
|
||||
} else {
|
||||
$return_data = false;
|
||||
}
|
||||
} else {
|
||||
$filename = $dummy;
|
||||
}
|
||||
}
|
||||
return $return_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* simple thumbnail creation for jpeg, png only
|
||||
* TODO: add other types like gif, etc
|
||||
* - bails with false on failed create
|
||||
* - if either size_x or size_y are empty (0)
|
||||
* the resize is to max of one size
|
||||
* if both are set, those are the max sizes (aspect ration is always ekpt)
|
||||
* - if path is not given will cache folder for current path set
|
||||
*
|
||||
* @param string $filename source file name with full path
|
||||
* @param int $thumb_width thumbnail width
|
||||
* @param int $thumb_height thumbnail height
|
||||
* @param string|null $thumbnail_path altnerative path for thumbnails
|
||||
* @param bool $create_dummy if we encounter an invalid file
|
||||
* create a dummy image file and return it
|
||||
* @param bool $use_cache default to true, set to false to skip
|
||||
* creating new image if exists
|
||||
* @param bool $high_quality default to true, uses sample version,
|
||||
* set to false to not use (default true)
|
||||
* to use quick but less nice version
|
||||
* @param int $jpeg_quality default 80, set image quality for jpeg only
|
||||
* @return string|bool thumbnail with path
|
||||
*/
|
||||
public static function createThumbnailSimple(
|
||||
string $filename,
|
||||
int $thumb_width = 0,
|
||||
int $thumb_height = 0,
|
||||
?string $thumbnail_path = null,
|
||||
bool $create_dummy = true,
|
||||
bool $use_cache = true,
|
||||
bool $high_quality = true,
|
||||
int $jpeg_quality = 80
|
||||
) {
|
||||
$thumbnail = false;
|
||||
// $this->debug('IMAGE PREPARE', "FILE: $filename (exists "
|
||||
// .(string)file_exists($filename)."), WIDTH: $thumb_width, HEIGHT: $thumb_height");
|
||||
// check that input image exists and is either jpeg or png
|
||||
// also fail if the basic CACHE folder does not exist at all
|
||||
if (
|
||||
file_exists($filename) &&
|
||||
is_dir(BASE . LAYOUT . CONTENT_PATH . CACHE) &&
|
||||
is_writable(BASE . LAYOUT . CONTENT_PATH . CACHE)
|
||||
) {
|
||||
// $this->debug('IMAGE PREPARE', "FILENAME OK, THUMB WIDTH/HEIGHT OK");
|
||||
[$inc_width, $inc_height, $img_type] = getimagesize($filename) ?: [];
|
||||
$thumbnail_write_path = null;
|
||||
$thumbnail_web_path = null;
|
||||
// path set first
|
||||
if (
|
||||
$img_type == IMAGETYPE_JPEG ||
|
||||
$img_type == IMAGETYPE_PNG ||
|
||||
$create_dummy === true
|
||||
) {
|
||||
// $this->debug('IMAGE PREPARE', "IMAGE TYPE OK: ".$inc_width.'x'.$inc_height);
|
||||
// set thumbnail paths
|
||||
$thumbnail_write_path = BASE . LAYOUT . CONTENT_PATH . CACHE . IMAGES;
|
||||
$thumbnail_web_path = LAYOUT . CACHE . IMAGES;
|
||||
// if images folder in cache does not exist create it, if failed, fall back to base cache folder
|
||||
if (!is_dir($thumbnail_write_path)) {
|
||||
if (false === mkdir($thumbnail_write_path)) {
|
||||
$thumbnail_write_path = BASE . LAYOUT . CONTENT_PATH . CACHE;
|
||||
$thumbnail_web_path = LAYOUT . CACHE;
|
||||
}
|
||||
}
|
||||
}
|
||||
// do resize or fall back on dummy run
|
||||
if (
|
||||
$img_type == IMAGETYPE_JPEG ||
|
||||
$img_type == IMAGETYPE_PNG
|
||||
) {
|
||||
// if missing width or height in thumb, use the set one
|
||||
if ($thumb_width == 0) {
|
||||
$thumb_width = $inc_width;
|
||||
}
|
||||
if ($thumb_height == 0) {
|
||||
$thumb_height = $inc_height;
|
||||
}
|
||||
// check resize parameters
|
||||
if ($inc_width > $thumb_width || $inc_height > $thumb_height) {
|
||||
$thumb_width_r = 0;
|
||||
$thumb_height_r = 0;
|
||||
// we need to keep the aspect ration on longest side
|
||||
if (
|
||||
($inc_height > $inc_width &&
|
||||
// and the height is bigger than thumb set
|
||||
$inc_height > $thumb_height) ||
|
||||
// or the height is smaller or equal width
|
||||
// but the width for the thumb is equal to the image height
|
||||
($inc_height <= $inc_width &&
|
||||
$inc_width == $thumb_width
|
||||
)
|
||||
) {
|
||||
// $this->debug('IMAGE PREPARE', 'HEIGHT > WIDTH');
|
||||
$ratio = $inc_height / $thumb_height;
|
||||
$thumb_width_r = (int)ceil($inc_width / $ratio);
|
||||
$thumb_height_r = $thumb_height;
|
||||
} else {
|
||||
// $this->debug('IMAGE PREPARE', 'WIDTH > HEIGHT');
|
||||
$ratio = $inc_width / $thumb_width;
|
||||
$thumb_width_r = $thumb_width;
|
||||
$thumb_height_r = (int)ceil($inc_height / $ratio);
|
||||
}
|
||||
// $this->debug('IMAGE PREPARE', "Ratio: $ratio, Target size $thumb_width_r x $thumb_height_r");
|
||||
// set output thumbnail name
|
||||
$thumbnail = 'thumb-' . pathinfo($filename)['filename'] . '-'
|
||||
. $thumb_width_r . 'x' . $thumb_height_r;
|
||||
if (
|
||||
$use_cache === false ||
|
||||
!file_exists($thumbnail_write_path . $thumbnail)
|
||||
) {
|
||||
// image, copy source image, offset in image, source x/y, new size, source image size
|
||||
$thumb = imagecreatetruecolor($thumb_width_r, $thumb_height_r);
|
||||
if ($thumb === false) {
|
||||
return false;
|
||||
}
|
||||
if ($img_type == IMAGETYPE_PNG) {
|
||||
$imagecolorallocatealpha = imagecolorallocatealpha($thumb, 0, 0, 0, 127);
|
||||
if ($imagecolorallocatealpha === false) {
|
||||
return false;
|
||||
}
|
||||
// preservere transaprency
|
||||
imagecolortransparent(
|
||||
$thumb,
|
||||
$imagecolorallocatealpha
|
||||
);
|
||||
imagealphablending($thumb, false);
|
||||
imagesavealpha($thumb, true);
|
||||
}
|
||||
$source = null;
|
||||
switch ($img_type) {
|
||||
case IMAGETYPE_JPEG:
|
||||
$source = imagecreatefromjpeg($filename);
|
||||
break;
|
||||
case IMAGETYPE_PNG:
|
||||
$source = imagecreatefrompng($filename);
|
||||
break;
|
||||
}
|
||||
// check that we have a source image resource
|
||||
if ($source !== null && $source !== false) {
|
||||
// resize no shift
|
||||
if ($high_quality === true) {
|
||||
imagecopyresized(
|
||||
$thumb,
|
||||
$source,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
$thumb_width_r,
|
||||
$thumb_height_r,
|
||||
$inc_width,
|
||||
$inc_height
|
||||
);
|
||||
} else {
|
||||
imagecopyresampled(
|
||||
$thumb,
|
||||
$source,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
$thumb_width_r,
|
||||
$thumb_height_r,
|
||||
$inc_width,
|
||||
$inc_height
|
||||
);
|
||||
}
|
||||
// write file
|
||||
switch ($img_type) {
|
||||
case IMAGETYPE_JPEG:
|
||||
imagejpeg($thumb, $thumbnail_write_path . $thumbnail, $jpeg_quality);
|
||||
break;
|
||||
case IMAGETYPE_PNG:
|
||||
imagepng($thumb, $thumbnail_write_path . $thumbnail);
|
||||
break;
|
||||
}
|
||||
// free up resources (in case we are called in a loop)
|
||||
imagedestroy($source);
|
||||
imagedestroy($thumb);
|
||||
} else {
|
||||
$thumbnail = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// we just copy over the image as is, we never upscale
|
||||
$thumbnail = 'thumb-' . pathinfo($filename)['filename'] . '-' . $inc_width . 'x' . $inc_height;
|
||||
if (
|
||||
$use_cache === false ||
|
||||
!file_exists($thumbnail_write_path . $thumbnail)
|
||||
) {
|
||||
copy($filename, $thumbnail_write_path . $thumbnail);
|
||||
}
|
||||
}
|
||||
// add output path
|
||||
if ($thumbnail !== false) {
|
||||
$thumbnail = $thumbnail_web_path . $thumbnail;
|
||||
}
|
||||
} elseif ($create_dummy === true) {
|
||||
// create dummy image in the thumbnail size
|
||||
// if one side is missing, use the other side to create a square
|
||||
if (!$thumb_width) {
|
||||
$thumb_width = $thumb_height;
|
||||
}
|
||||
if (!$thumb_height) {
|
||||
$thumb_height = $thumb_width;
|
||||
}
|
||||
// do we have an image already?
|
||||
$thumbnail = 'thumb-' . pathinfo($filename)['filename'] . '-' . $thumb_width . 'x' . $thumb_height;
|
||||
if (
|
||||
$use_cache === false ||
|
||||
!file_exists($thumbnail_write_path . $thumbnail)
|
||||
) {
|
||||
// if both are unset, set to 250
|
||||
if ($thumb_height == 0) {
|
||||
$thumb_height = 250;
|
||||
}
|
||||
if ($thumb_width == 0) {
|
||||
$thumb_width = 250;
|
||||
}
|
||||
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
|
||||
if ($thumb === false) {
|
||||
return false;
|
||||
}
|
||||
// add outside border px = 5% (rounded up)
|
||||
// eg 50px -> 2.5px
|
||||
$gray = imagecolorallocate($thumb, 200, 200, 200);
|
||||
$white = imagecolorallocate($thumb, 255, 255, 255);
|
||||
if ($gray === false || $white === false) {
|
||||
return false;
|
||||
}
|
||||
// fill gray background
|
||||
imagefill($thumb, 0, 0, $gray);
|
||||
// now create rectangle
|
||||
if (imagesx($thumb) < imagesy($thumb)) {
|
||||
$width = (int)round(imagesx($thumb) / 100 * 5);
|
||||
} else {
|
||||
$width = (int)round(imagesy($thumb) / 100 * 5);
|
||||
}
|
||||
imagefilledrectangle(
|
||||
$thumb,
|
||||
0 + $width,
|
||||
0 + $width,
|
||||
imagesx($thumb) - $width,
|
||||
imagesy($thumb) - $width,
|
||||
$white
|
||||
);
|
||||
// add "No valid images source"
|
||||
// OR add circle
|
||||
// * find center
|
||||
// * width/height is 75% of size - border
|
||||
// smaller size is taken
|
||||
$base_width = imagesx($thumb) > imagesy($thumb) ? imagesy($thumb) : imagesx($thumb);
|
||||
// get 75% width
|
||||
$cross_width = (int)round((($base_width - ($width * 2)) / 100 * 75) / 2);
|
||||
$center_x = (int)round(imagesx($thumb) / 2);
|
||||
$center_y = (int)round(imagesy($thumb) / 2);
|
||||
imagefilledellipse($thumb, $center_x, $center_y, $cross_width, $cross_width, $gray);
|
||||
// find top left and bottom left for first line
|
||||
imagepng($thumb, $thumbnail_write_path . $thumbnail);
|
||||
}
|
||||
// add web path
|
||||
$thumbnail = $thumbnail_web_path . $thumbnail;
|
||||
}
|
||||
}
|
||||
// either return false or the thumbnail name + output path web
|
||||
return $thumbnail;
|
||||
}
|
||||
|
||||
/**
|
||||
* reads the rotation info of an file and rotates it to be correctly upright
|
||||
* this is done because not all software honers the exit Orientation flag
|
||||
* only works with jpg or png
|
||||
*
|
||||
* @param string $filename path + filename to rotate. This file must be writeable
|
||||
* @return void
|
||||
*/
|
||||
public static function correctImageOrientation($filename): void
|
||||
{
|
||||
// function exists & file is writeable, else do nothing
|
||||
if (!function_exists('exif_read_data') || !is_writeable($filename)) {
|
||||
return;
|
||||
}
|
||||
[$inc_width, $inc_height, $img_type] = getimagesize($filename) ?: [];
|
||||
// add @ to avoid "file not supported error"
|
||||
$exif = @exif_read_data($filename);
|
||||
$orientation = null;
|
||||
$img = null;
|
||||
if ($exif && isset($exif['Orientation'])) {
|
||||
$orientation = $exif['Orientation'];
|
||||
}
|
||||
// only if we need to rotate, if 1 it is already upright
|
||||
if ($orientation === null || $orientation == 1) {
|
||||
return;
|
||||
}
|
||||
switch ($img_type) {
|
||||
case IMAGETYPE_JPEG:
|
||||
$img = imagecreatefromjpeg($filename);
|
||||
break;
|
||||
case IMAGETYPE_PNG:
|
||||
$img = imagecreatefrompng($filename);
|
||||
break;
|
||||
}
|
||||
// no image loaded (wrong type)
|
||||
if ($img === null || $img === false) {
|
||||
return;
|
||||
}
|
||||
$deg = 0;
|
||||
// 1 top, 6: left, 8: right, 3: bottom
|
||||
switch ($orientation) {
|
||||
case 3:
|
||||
$deg = 180;
|
||||
break;
|
||||
case 6:
|
||||
$deg = -90;
|
||||
break;
|
||||
case 8:
|
||||
$deg = 90;
|
||||
break;
|
||||
}
|
||||
// rotate if needed
|
||||
if ($deg) {
|
||||
$img = imagerotate($img, $deg, 0);
|
||||
}
|
||||
// rotate failed
|
||||
if ($img === false) {
|
||||
return;
|
||||
}
|
||||
// then rewrite the rotated image back to the disk as $filename
|
||||
switch ($img_type) {
|
||||
case IMAGETYPE_JPEG:
|
||||
imagejpeg($img, $filename);
|
||||
break;
|
||||
case IMAGETYPE_PNG:
|
||||
imagepng($img, $filename);
|
||||
break;
|
||||
}
|
||||
// clean up image if we have an image
|
||||
imagedestroy($img);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
932
4dev/deprecated/CoreLibs/Output/ProgressBar.php
Normal file
932
4dev/deprecated/CoreLibs/Output/ProgressBar.php
Normal file
@@ -0,0 +1,932 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Class ProgressBar
|
||||
*
|
||||
* Author: Gerd Weitenberg (hahnebuechen@web.de)
|
||||
* Date: 2005.03.09
|
||||
*
|
||||
* Update: Clemens Schwaighofer
|
||||
* Date: 2012.9.5 [stacked output]
|
||||
* Date: 2013.2.21 [proper class formatting]
|
||||
* Date: 2017.4.13 [no output fix with cache overload]
|
||||
* Date: 2018.3.28 [PHPCS + namespace]
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Output;
|
||||
|
||||
class ProgressBar
|
||||
{
|
||||
// private vars
|
||||
|
||||
/** @var string */
|
||||
public $code; // unique code
|
||||
/** @var string */
|
||||
public $status = 'new'; // current status (new,show,hide)
|
||||
/** @var float|int */
|
||||
public $step = 0; // current step
|
||||
/** @var array<string,null|int|float> */
|
||||
public $position = [ // current bar position
|
||||
'left' => null,
|
||||
'top' => null,
|
||||
'width' => null,
|
||||
'height' => null,
|
||||
];
|
||||
|
||||
/** @var int */
|
||||
public $clear_buffer_size = 1; // we need to send this before the lfush to get browser output
|
||||
/** @var int */
|
||||
public $clear_buffer_size_init = 1024 * 1024; // if I don't send that junk, it won't send anything
|
||||
|
||||
// public vars
|
||||
|
||||
/** @var int */
|
||||
public $min = 0; // minimal steps
|
||||
/** @var int */
|
||||
public $max = 100; // maximal steps
|
||||
|
||||
/** @var int */
|
||||
public $left = 5; // bar position from left
|
||||
/** @var int */
|
||||
public $top = 5; // bar position from top
|
||||
/** @var int */
|
||||
public $width = 300; // bar width
|
||||
/** @var int */
|
||||
public $height = 25; // bar height
|
||||
/** @var int */
|
||||
public $pedding = 0; // bar pedding
|
||||
/** @var string */
|
||||
public $color = '#0033ff'; // bar color
|
||||
/** @var string */
|
||||
public $bgr_color = '#c0c0c0'; // bar background color
|
||||
/** @var string */
|
||||
public $bgr_color_master = '#ffffff'; // master div background color
|
||||
/** @var int */
|
||||
public $border = 1; // bar border width
|
||||
/** @var string */
|
||||
public $brd_color = '#000000'; // bar border color
|
||||
|
||||
/** @var string */
|
||||
public $direction = 'right'; // direction of motion (right,left,up,down)
|
||||
|
||||
/** @var array<string,string|bool|int> */
|
||||
public $frame = ['show' => false]; // ProgressBar Frame
|
||||
/* 'show' => false, # frame show (true/false)
|
||||
'left' => 200, # frame position from left
|
||||
'top' => 100, # frame position from top
|
||||
'width' => 300, # frame width
|
||||
'height' => 75, # frame height
|
||||
'color' => '#c0c0c0', # frame color
|
||||
'border' => 2, # frame border
|
||||
'brd_color' => '#dfdfdf #404040 #404040 #dfdfdf' # frame border color
|
||||
*/
|
||||
|
||||
/** @#var array{string}{string: string|int} */
|
||||
/** @var mixed[][] */
|
||||
public $label = []; // ProgressBar Labels
|
||||
/* 'name' => [ # label name
|
||||
'type' => 'text', # label type (text,button,step,percent,crossbar)
|
||||
'value' => 'Please wait ...', # label value
|
||||
'left' => 10, # label position from left
|
||||
'top' => 20, # label position from top
|
||||
'width' => 0, # label width
|
||||
'height' => 0, # label height
|
||||
'align' => 'left', # label align
|
||||
'font-size' => 11, # label font size
|
||||
'font-family' => 'Verdana, Tahoma, Arial', # label font family
|
||||
'font-weight' => '', # label font weight
|
||||
'color' => '#000000', # label font color
|
||||
'bgr_color' => '' # label background color
|
||||
]
|
||||
*/
|
||||
|
||||
/** @var string */
|
||||
// output strings
|
||||
public $prefix_message = '';
|
||||
|
||||
/**
|
||||
* progress bar constructor
|
||||
*
|
||||
* @param integer $width progress bar width, default 0
|
||||
* @param integer $height progress bar height, default 0
|
||||
*/
|
||||
public function __construct(int $width = 0, int $height = 0)
|
||||
{
|
||||
$this->code = substr(md5(microtime()), 0, 6);
|
||||
if ($width > 0) {
|
||||
$this->width = $width;
|
||||
}
|
||||
if ($height > 0) {
|
||||
$this->height = $height;
|
||||
}
|
||||
// needs to be called twice or I do not get any output
|
||||
$this->__flushCache($this->clear_buffer_size_init);
|
||||
$this->__flushCache($this->clear_buffer_size_init);
|
||||
}
|
||||
|
||||
// private functions
|
||||
|
||||
/**
|
||||
* flush cache hack for IE and others
|
||||
*
|
||||
* @param integer $clear_buffer_size buffer size override
|
||||
* @return void has not return
|
||||
*/
|
||||
private function __flushCache(int $clear_buffer_size = 0): void
|
||||
{
|
||||
if (!$clear_buffer_size) {
|
||||
$clear_buffer_size = $this->clear_buffer_size;
|
||||
}
|
||||
echo str_repeat(' ', $clear_buffer_size);
|
||||
// a small hack to avoid warnings about no buffer to flush
|
||||
@ob_flush();
|
||||
flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* [__calculatePercent description]
|
||||
*
|
||||
* @param float $step percent step to do
|
||||
* @return float percent step done
|
||||
*/
|
||||
private function __calculatePercent(float $step): float
|
||||
{
|
||||
// avoid divison through 0
|
||||
if ($this->max - $this->min == 0) {
|
||||
$this->max ++;
|
||||
}
|
||||
$percent = round(($step - $this->min) / ($this->max - $this->min) * 100);
|
||||
if ($percent > 100) {
|
||||
$percent = 100;
|
||||
}
|
||||
return $percent;
|
||||
}
|
||||
|
||||
/**
|
||||
* calculate position in bar step
|
||||
*
|
||||
* @param float $step percent step to do
|
||||
* @return array<string,int|float> bar position as array
|
||||
*/
|
||||
private function __calculatePosition(float $step): array
|
||||
{
|
||||
$bar = 0;
|
||||
switch ($this->direction) {
|
||||
case 'right':
|
||||
case 'left':
|
||||
$bar = $this->width;
|
||||
break;
|
||||
case 'down':
|
||||
case 'up':
|
||||
$bar = $this->height;
|
||||
break;
|
||||
}
|
||||
// avoid divison through 0
|
||||
if ($this->max - $this->min == 0) {
|
||||
$this->max ++;
|
||||
}
|
||||
$pixel = round(($step - $this->min) * ($bar - ($this->pedding * 2)) / ($this->max - $this->min));
|
||||
if ($step <= $this->min) {
|
||||
$pixel = 0;
|
||||
}
|
||||
if ($step >= $this->max) {
|
||||
$pixel = $bar - ($this->pedding * 2);
|
||||
}
|
||||
|
||||
$position = [];
|
||||
switch ($this->direction) {
|
||||
case 'right':
|
||||
$position['left'] = $this->pedding;
|
||||
$position['top'] = $this->pedding;
|
||||
$position['width'] = $pixel;
|
||||
$position['height'] = $this->height - ($this->pedding * 2);
|
||||
break;
|
||||
case 'left':
|
||||
$position['left'] = $this->width - $this->pedding - $pixel;
|
||||
$position['top'] = $this->pedding;
|
||||
$position['width'] = $pixel;
|
||||
$position['height'] = $this->height - ($this->pedding * 2);
|
||||
break;
|
||||
case 'down':
|
||||
$position['left'] = $this->pedding;
|
||||
$position['top'] = $this->pedding;
|
||||
$position['width'] = $this->width - ($this->pedding * 2);
|
||||
$position['height'] = $pixel;
|
||||
break;
|
||||
case 'up':
|
||||
$position['left'] = $this->pedding;
|
||||
$position['top'] = $this->height - $this->pedding - $pixel;
|
||||
$position['width'] = $this->width - ($this->pedding * 2);
|
||||
$position['height'] = $pixel;
|
||||
break;
|
||||
}
|
||||
return $position;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the step
|
||||
*
|
||||
* @param float $step percent step to do
|
||||
* @return void
|
||||
*/
|
||||
private function __setStep(float $step): void
|
||||
{
|
||||
if ($step > $this->max) {
|
||||
$step = $this->max;
|
||||
}
|
||||
if ($step < $this->min) {
|
||||
$step = $this->min;
|
||||
}
|
||||
$this->step = $step;
|
||||
}
|
||||
|
||||
// public functions
|
||||
/**
|
||||
* set frame layout
|
||||
*
|
||||
* @param integer $width bar width
|
||||
* @param integer $height bar height
|
||||
* @return void
|
||||
*/
|
||||
public function setFrame(int $width = 0, int $height = 0): void
|
||||
{
|
||||
$this->frame = [
|
||||
'show' => true,
|
||||
'left' => 20,
|
||||
'top' => 35,
|
||||
'width' => $this->width + 6,
|
||||
'height' => 'auto',
|
||||
'color' => '#c0c0c0',
|
||||
'border' => 2,
|
||||
'brd_color' => '#dfdfdf #404040 #404040 #dfdfdf'
|
||||
];
|
||||
|
||||
if ($width > 0) {
|
||||
$this->frame['width'] = $width;
|
||||
}
|
||||
if ($height > 0) {
|
||||
$this->frame['height'] = $height;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* set bar label text
|
||||
* allowed types are: text, button, step, percent, percentlbl, crossbar
|
||||
*
|
||||
* @param string $type label type
|
||||
* @param string $name label name (internal)
|
||||
* @param string $value label output name (optional)
|
||||
* @return void
|
||||
*/
|
||||
public function addLabel(string $type, string $name, string $value = ' '): void
|
||||
{
|
||||
switch ($type) {
|
||||
case 'text':
|
||||
$this->label[$name] = [
|
||||
'type' => 'text',
|
||||
'value' => $value,
|
||||
'left' => 0, // keep all to the left in box
|
||||
'top' => 2, // default top is 2px
|
||||
'width' => $this->width,
|
||||
'height' => 0,
|
||||
'align' => 'left',
|
||||
'font-size' => 11,
|
||||
'font-family' => 'Verdana, Tahoma, Arial',
|
||||
'font-weight' => 'normal',
|
||||
'color' => '#000000',
|
||||
'bgr_color' => ''
|
||||
];
|
||||
break;
|
||||
case 'button':
|
||||
$this->label[$name] = [
|
||||
'type' => 'button',
|
||||
'value' => $value,
|
||||
'action' => '',
|
||||
'target' => 'self',
|
||||
'left' => 5,
|
||||
'top' => 5,
|
||||
'width' => 0,
|
||||
'height' => 0,
|
||||
'align' => 'center',
|
||||
'font-size' => 11,
|
||||
'font-family' => 'Verdana, Tahoma, Arial',
|
||||
'font-weight' => 'normal',
|
||||
'color' => '#000000',
|
||||
'bgr_color' => ''
|
||||
];
|
||||
break;
|
||||
case 'step':
|
||||
$this->label[$name] = [
|
||||
'type' => 'step',
|
||||
'value' => $value,
|
||||
'left' => $this->left + 5,
|
||||
'top' => $this->top + 5,
|
||||
'width' => 10,
|
||||
'height' => 0,
|
||||
'align' => 'right',
|
||||
'font-size' => 11,
|
||||
'font-family' => 'Verdana, Tahoma, Arial',
|
||||
'font-weight' => 'normal',
|
||||
'color' => '#000000',
|
||||
'bgr_color' => ''
|
||||
];
|
||||
break;
|
||||
case 'percentlbl':
|
||||
case 'percent':
|
||||
// check font size
|
||||
if ($this->height <= 11) {
|
||||
$font_size = $this->height - 1;
|
||||
} else {
|
||||
$font_size = 11;
|
||||
}
|
||||
$this->label[$name] = [
|
||||
'type' => $type, // either percent or percentlbl
|
||||
'value' => $value,
|
||||
'left' => false,
|
||||
'top' => round(
|
||||
($this->height - $font_size) / log($this->height - $font_size, 7),
|
||||
0
|
||||
) - $this->pedding,
|
||||
'width' => $this->width,
|
||||
'height' => 0,
|
||||
'align' => 'center',
|
||||
'font-size' => $font_size,
|
||||
'font-family' => 'sans-serif',
|
||||
'font-weight' => 'normal',
|
||||
'color' => '#000000',
|
||||
'bgr_color' => ''
|
||||
];
|
||||
break;
|
||||
case 'crossbar':
|
||||
$this->label[$name] = [
|
||||
'type' => 'crossbar',
|
||||
'value' => $value,
|
||||
'left' => $this->left + ($this->width / 2),
|
||||
'top' => $this->top - 16,
|
||||
'width' => 10,
|
||||
'height' => 0,
|
||||
'align' => 'center',
|
||||
'font-size' => 11,
|
||||
'font-family' => 'Verdana, Tahoma, Arial',
|
||||
'font-weight' => 'normal',
|
||||
'color' => '#000000',
|
||||
'bgr_color' => ''
|
||||
];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* add a button to the progress bar
|
||||
*
|
||||
* @param string $name button name (internal)
|
||||
* @param string $value button text (output)
|
||||
* @param string $action button action (link)
|
||||
* @param string $target button action target (default self)
|
||||
* @return void
|
||||
*/
|
||||
public function addButton(string $name, string $value, string $action, string $target = 'self'): void
|
||||
{
|
||||
$this->addLabel('button', $name, $value);
|
||||
$this->label[$name]['action'] = $action;
|
||||
$this->label[$name]['target'] = $target;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the label position
|
||||
*
|
||||
* @param string $name label name to set
|
||||
* @param int $left left px
|
||||
* @param int $top top px
|
||||
* @param int $width width px
|
||||
* @param int $height height px
|
||||
* @param string $align alignment (left/right/etc), default empty
|
||||
* @return void
|
||||
*/
|
||||
public function setLabelPosition(
|
||||
string $name,
|
||||
int $left,
|
||||
int $top,
|
||||
int $width,
|
||||
int $height,
|
||||
string $align = ''
|
||||
): void {
|
||||
// print "SET POSITION[$name]: $left<br>";
|
||||
// if this is percent, we ignore anything, it is auto positioned
|
||||
if ($this->label[$name]['type'] != 'percent') {
|
||||
foreach (['top', 'left', 'width', 'height'] as $pos_name) {
|
||||
if ($$pos_name !== false) {
|
||||
$this->label[$name][$pos_name] = intval($$pos_name);
|
||||
}
|
||||
}
|
||||
|
||||
if ($align != '') {
|
||||
$this->label[$name]['align'] = $align;
|
||||
}
|
||||
}
|
||||
// init
|
||||
if ($this->status != 'new') {
|
||||
$output = '<script type="text/JavaScript">';
|
||||
$output .= 'document.getElementById("plbl' . $name
|
||||
. $this->code . '").style.top="' . $this->label[$name]['top'] . 'px";';
|
||||
$output .= 'document.getElementById("plbl' . $name
|
||||
. $this->code . '").style.left="' . $this->label[$name]['left'] . 'px";';
|
||||
$output .= 'document.getElementById("plbl' . $name
|
||||
. $this->code . '").style.width="' . $this->label[$name]['width'] . 'px";';
|
||||
$output .= 'document.getElementById("plbl' . $name
|
||||
. $this->code . '").style.height="' . $this->label[$name]['height'] . 'px";';
|
||||
$output .= 'document.getElementById("plbl' . $name
|
||||
. $this->code . '").style.align="' . $this->label[$name]['align'] . '";';
|
||||
$output .= '</script>' . "\n";
|
||||
echo $output;
|
||||
$this->__flushCache();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* set label color
|
||||
*
|
||||
* @param string $name label name to set
|
||||
* @param string $color color value in rgb html hex
|
||||
* @return void
|
||||
*/
|
||||
public function setLabelColor(string $name, string $color): void
|
||||
{
|
||||
$this->label[$name]['color'] = $color;
|
||||
if ($this->status != 'new') {
|
||||
echo '<script type="text/JavaScript">document.getElementById("plbl' . $name
|
||||
. $this->code . '").style.color="' . $color . '";</script>' . "\n";
|
||||
$this->__flushCache();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* set the label background color
|
||||
*
|
||||
* @param string $name label name to set
|
||||
* @param string $color background color to set in rgb html hex
|
||||
* @return void
|
||||
*/
|
||||
public function setLabelBackground(string $name, string $color): void
|
||||
{
|
||||
$this->label[$name]['bgr_color'] = $color;
|
||||
if ($this->status != 'new') {
|
||||
echo '<script type="text/JavaScript">document.getElementById("plbl' . $name
|
||||
. $this->code . '").style.background="' . $color . '";</script>' . "\n";
|
||||
$this->__flushCache();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* [setLabelFont description]
|
||||
*
|
||||
* @param string $name label name to set
|
||||
* @param int $size font size in px
|
||||
* @param string $family font family (default empty)
|
||||
* @param string $weight font weight (default empty)
|
||||
* @return void
|
||||
*/
|
||||
public function setLabelFont(string $name, int $size, string $family = '', string $weight = ''): void
|
||||
{
|
||||
// just in case if it is too small
|
||||
if (intval($size) < 0) {
|
||||
$size = 11;
|
||||
}
|
||||
// if this is percent, the size is not allowed to be bigger than the bar size - 5px
|
||||
if ($this->label[$name]['type'] == 'percent' && intval($size) >= $this->height) {
|
||||
$size = $this->height - 1;
|
||||
}
|
||||
// position the label new if this is percent
|
||||
if ($this->label[$name]['type'] == 'percent') {
|
||||
$this->label[$name]['top'] = round(
|
||||
($this->height - intval($size)) / log($this->height - intval($size), 7),
|
||||
0
|
||||
) - $this->pedding;
|
||||
}
|
||||
// print "HEIGHT: ".$this->height.", Size: ".intval($size)
|
||||
// .", Pedding: ".$this->pedding.", Calc: ".round($this->height - intval($size))
|
||||
// .", Log: ".log($this->height - intval($size), 7)."<br>";
|
||||
// then set like usual
|
||||
$this->label[$name]['font-size'] = intval($size);
|
||||
if ($family != '') {
|
||||
$this->label[$name]['font-family'] = $family;
|
||||
}
|
||||
if ($weight != '') {
|
||||
$this->label[$name]['font-weight'] = $weight;
|
||||
}
|
||||
|
||||
if ($this->status != 'new') {
|
||||
$output = '<script type="text/JavaScript">';
|
||||
$output .= 'document.getElementById("plbl' . $name
|
||||
. $this->code . '").style.font-size="' . $this->label[$name]['font-size'] . 'px";';
|
||||
$output .= 'document.getElementById("plbl' . $name
|
||||
. $this->code . '").style.font-family="' . $this->label[$name]['font-family'] . '";';
|
||||
$output .= 'document.getElementById("plbl' . $name
|
||||
. $this->code . '").style.font-weight="' . $this->label[$name]['font-weight'] . '";';
|
||||
$output .= '</script>' . "\n";
|
||||
echo $output;
|
||||
$this->__flushCache();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* set the label value
|
||||
*
|
||||
* @param string $name label name to set
|
||||
* @param string $value label value (output)
|
||||
* @return void
|
||||
*/
|
||||
public function setLabelValue(string $name, string $value): void
|
||||
{
|
||||
$this->label[$name]['value'] = $value;
|
||||
// print "NAME[$name], Status: ".$this->status.": ".$value."<Br>";
|
||||
if ($this->status != 'new') {
|
||||
echo '<script type="text/JavaScript">PBlabelText' . $this->code
|
||||
. '("' . $name . '","' . $this->label[$name]['value'] . '");</script>' . "\n";
|
||||
$this->__flushCache();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* set the bar color
|
||||
*
|
||||
* @param string $color color for the progress bar in rgb html hex
|
||||
* @return void
|
||||
*/
|
||||
public function setBarColor(string $color): void
|
||||
{
|
||||
$this->color = $color;
|
||||
if ($this->status != 'new') {
|
||||
echo '<script type="text/JavaScript">document.getElementById("pbar' . $this->code
|
||||
. '").style.background="' . $color . '";</script>' . "\n";
|
||||
$this->__flushCache();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* set the progress bar background color
|
||||
*
|
||||
* @param string $color background color in rgb html hex
|
||||
* @return void
|
||||
*/
|
||||
public function setBarBackground(string $color): void
|
||||
{
|
||||
$this->bgr_color = $color;
|
||||
if ($this->status != 'new') {
|
||||
echo '<script type="text/JavaScript">document.getElementById("pbrd' . $this->code
|
||||
. '").style.background="' . $color . '";</script>' . "\n";
|
||||
$this->__flushCache();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* progress bar direct (left/right)
|
||||
*
|
||||
* @param string $direction set direction as left/right
|
||||
* @return void
|
||||
*/
|
||||
public function setBarDirection(string $direction): void
|
||||
{
|
||||
$this->direction = $direction;
|
||||
|
||||
if ($this->status != 'new') {
|
||||
$this->position = $this->__calculatePosition($this->step);
|
||||
|
||||
echo '<script type="text/JavaScript">';
|
||||
echo 'PBposition' . $this->code . '("left",' . $this->position['left'] . ');';
|
||||
echo 'PBposition' . $this->code . '("top",' . $this->position['top'] . ');';
|
||||
echo 'PBposition' . $this->code . '("width",' . $this->position['width'] . ');';
|
||||
echo 'PBposition' . $this->code . '("height",' . $this->position['height'] . ');';
|
||||
echo '</script>' . "\n";
|
||||
$this->__flushCache();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get the progress bar base HTML
|
||||
*
|
||||
* @return string progress bar HTML code
|
||||
*/
|
||||
public function getHtml(): string
|
||||
{
|
||||
$html = '';
|
||||
$js = '';
|
||||
$html_button = '';
|
||||
$html_percent = '';
|
||||
|
||||
$this->__setStep($this->step);
|
||||
$this->position = $this->__calculatePosition($this->step);
|
||||
|
||||
$style_master = '';
|
||||
if ($this->top || $this->left) {
|
||||
$style_master = 'position:relative;top:' . $this->top
|
||||
. 'px;left:' . $this->left . 'px;width:' . ($this->width + 10) . 'px;';
|
||||
}
|
||||
$html = '<div id="pbm' . $this->code . '" style="' . $style_master
|
||||
. 'background:' . $this->bgr_color_master . ';">';
|
||||
$style_brd = 'width:' . $this->width . 'px;height:' . $this->height
|
||||
. 'px;background:' . $this->bgr_color . ';';
|
||||
if ($this->border > 0) {
|
||||
$style_brd .= 'border:'
|
||||
. $this->border . 'px solid; border-color:'
|
||||
. $this->brd_color . '; -webkit-border-radius: 5px 5px 5px 5px; '
|
||||
. 'border-radius: 5px 5px 5px 5px; -webkit-shadow: 2px 2px 10px rgba(0, 0, 0, 0.25) inset; '
|
||||
. 'box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.25) inset;';
|
||||
}
|
||||
|
||||
$style_bar = 'position:relative;width:' . $this->position['width']
|
||||
. 'px;height:' . $this->position['height'] . 'px;background:' . $this->color . ';';
|
||||
if ($this->position['top'] !== false) {
|
||||
$style_bar .= 'top:' . $this->position['top'] . 'px;';
|
||||
}
|
||||
if ($this->position['left'] !== false) {
|
||||
$style_bar .= 'left:' . $this->position['left'] . 'px;';
|
||||
}
|
||||
if ($this->border > 0) {
|
||||
$style_bar .= '-webkit-border-radius: 5px 5px 5px 5px; '
|
||||
. 'border-radius: 5px 5px 5px 5px; -webkit-shadow: 2px 2px 10px rgba(0, 0, 0, 0.25) inset; '
|
||||
. 'box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.25) inset;';
|
||||
}
|
||||
|
||||
if ($this->frame['show'] == true) {
|
||||
$border = '';
|
||||
if ($this->frame['border'] > 0) {
|
||||
$border = 'border:' . $this->frame['border']
|
||||
. 'px solid;border-color:' . $this->frame['brd_color'] . ';margin-top:2px; '
|
||||
. '-webkit-border-radius: 5px 5px 5px 5px; border-radius: 5px 5px 5px 5px;';
|
||||
}
|
||||
$html .= '<div id="pfrm' . $this->code . '" style="width:'
|
||||
. $this->frame['width'] . 'px;height:' . $this->frame['height'] . 'px;'
|
||||
. $border . 'background:' . $this->frame['color'] . ';">' . "\n";
|
||||
}
|
||||
|
||||
// temp write the bar here, we add that later, below all the html + progress %
|
||||
$html_bar_top = '<div id="pbrd' . $this->code . '" style="' . $style_brd
|
||||
. ($this->frame['show'] == true ? 'margin-left: 2px;margin-bottom:2px;' : '') . '">' . "\n";
|
||||
$html_bar_top .= '<div id="pbar' . $this->code . '" style="' . $style_bar . '">';
|
||||
// insert single percent there
|
||||
$html_bar_bottom = '</div></div>' . "\n";
|
||||
|
||||
$js .= 'function PBposition' . $this->code . '(item,pixel) {' . "\n";
|
||||
$js .= ' pixel = parseInt(pixel);' . "\n";
|
||||
$js .= ' switch(item) {' . "\n";
|
||||
$js .= ' case "left": document.getElementById("pbar' . $this->code
|
||||
. '").style.left=(pixel) + \'px\'; break;' . "\n";
|
||||
$js .= ' case "top": document.getElementById("pbar' . $this->code
|
||||
. '").style.top=(pixel) + \'px\'; break;' . "\n";
|
||||
$js .= ' case "width": document.getElementById("pbar' . $this->code
|
||||
. '").style.width=(pixel) + \'px\'; break;' . "\n";
|
||||
$js .= ' case "height": document.getElementById("pbar' . $this->code
|
||||
. '").style.height=(pixel) + \'px\'; break;' . "\n";
|
||||
$js .= ' }' . "\n";
|
||||
$js .= '}' . "\n";
|
||||
|
||||
// print "DUMP LABEL: <br><pre>".print_r($this->label, true)."</pre><br>";
|
||||
foreach ($this->label as $name => $data) {
|
||||
// set what type of move we do
|
||||
$move_prefix = $data['type'] == 'button' ? 'margin' : 'padding';
|
||||
$style_lbl = 'position:relative;';
|
||||
if ($data['top'] !== false) {
|
||||
$style_lbl .= $move_prefix . '-top:' . $data['top'] . 'px;';
|
||||
}
|
||||
if ($data['left'] !== false) {
|
||||
$style_lbl .= $move_prefix . '-left:' . $data['left'] . 'px;';
|
||||
}
|
||||
$style_lbl .= 'text-align:' . $data['align'] . ';';
|
||||
if ($data['width'] > 0) {
|
||||
$style_lbl .= 'width:' . $data['width'] . 'px;';
|
||||
}
|
||||
if ($data['height'] > 0) {
|
||||
$style_lbl .= 'height:' . $data['height'] . 'px;';
|
||||
}
|
||||
|
||||
if (array_key_exists('font-size', $data)) {
|
||||
$style_lbl .= 'font-size:' . $data['font-size'] . 'px;';
|
||||
}
|
||||
if (array_key_exists('font-family', $data)) {
|
||||
$style_lbl .= 'font-family:' . $data['font-family'] . ';';
|
||||
}
|
||||
if (array_key_exists('font-weight', $data)) {
|
||||
$style_lbl .= 'font-weight:' . $data['font-weight'] . ';';
|
||||
}
|
||||
if (array_key_exists('bgr_color', $data) && ($data['bgr_color'] != '')) {
|
||||
$style_lbl .= 'background:' . $data['bgr_color'] . ';';
|
||||
}
|
||||
|
||||
if (array_key_exists('type', $data)) {
|
||||
switch ($data['type']) {
|
||||
case 'text':
|
||||
$html .= '<div id="plbl' . $name . $this->code . '" style="'
|
||||
. $style_lbl . 'margin-bottom:2px;">' . $data['value'] . '</div>' . "\n";
|
||||
break;
|
||||
case 'button':
|
||||
$html_button .= '<div><input id="plbl' . $name
|
||||
. $this->code . '" type="button" value="' . $data['value'] . '" style="'
|
||||
. $style_lbl . 'margin-bottom:5px;" onclick="' . $data['target'] . '.location.href=\''
|
||||
. $data['action'] . '\'" /></div>' . "\n";
|
||||
break;
|
||||
case 'step':
|
||||
$html .= '<div id="plbl' . $name . $this->code . '" style="'
|
||||
. $style_lbl . '">' . $this->step . '</div>' . "\n";
|
||||
break;
|
||||
case 'percent':
|
||||
// only one inner percent
|
||||
// print "STYLE[$name]: ".$style_lbl."<br>";
|
||||
if (empty($html_percent)) {
|
||||
$html_percent = '<div id="plbl' . $name . $this->code
|
||||
. '" style="' . $style_lbl . 'width:' . $data['width']
|
||||
. 'px;line-height:1;text-shadow: 0 0 .2em white, 0 0 .5em white;">'
|
||||
. $this->__calculatePercent($this->step) . '%</div>' . "\n";
|
||||
}
|
||||
break;
|
||||
case 'percentlbl':
|
||||
$html .= '<div id="plbl' . $name . $this->code . '" style="'
|
||||
. $style_lbl . 'width:' . $data['width'] . 'px;">'
|
||||
. $this->__calculatePercent($this->step) . '%</div>' . "\n";
|
||||
break;
|
||||
case 'crossbar':
|
||||
$html .= '<div id="plbl' . $name . $this->code . '" style="'
|
||||
. $style_lbl . '">' . $data['value'] . '</div>' . "\n";
|
||||
|
||||
$js .= 'function PBrotaryCross' . $name . $this->code . '() {'
|
||||
. "\n"
|
||||
. ' cross = document.getElementById("plbl' . $name
|
||||
. $this->code . '").firstChild.nodeValue;' . "\n"
|
||||
. ' switch(cross) {' . "\n"
|
||||
. ' case "--": cross = "\\\\"; break;' . "\n"
|
||||
. ' case "\\\\": cross = "|"; break;' . "\n"
|
||||
. ' case "|": cross = "/"; break;' . "\n"
|
||||
. ' default: cross = "--"; break;' . "\n"
|
||||
. ' }' . "\n"
|
||||
. ' document.getElementById("plbl' . $name
|
||||
. $this->code . '").firstChild.nodeValue = cross;' . "\n"
|
||||
. '}' . "\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// write the progress bar + inner percent inside
|
||||
$html .= $html_bar_top;
|
||||
$html .= $html_percent;
|
||||
$html .= $html_bar_bottom;
|
||||
$html .= $html_button; // any buttons on bottom
|
||||
|
||||
if (count($this->label) > 0) {
|
||||
$js .= 'function PBlabelText' . $this->code . '(name,text) {' . "\n";
|
||||
$js .= ' name = "plbl" + name + "' . $this->code . '";' . "\n";
|
||||
$js .= ' document.getElementById(name).innerHTML=text;' . "\n";
|
||||
$js .= '}' . "\n";
|
||||
}
|
||||
|
||||
if ($this->frame['show'] == true) {
|
||||
$html .= '</div>' . "\n";
|
||||
}
|
||||
|
||||
$html .= '<script type="text/JavaScript">' . "\n";
|
||||
$html .= $js;
|
||||
$html .= '</script>' . "\n";
|
||||
|
||||
$html .= '</div>';
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* show the progress bar after initialize
|
||||
*
|
||||
* @return void has no return
|
||||
*/
|
||||
public function show(): void
|
||||
{
|
||||
$this->status = 'show';
|
||||
echo $this->getHtml();
|
||||
$this->__flushCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* move the progress bar by one step
|
||||
* prints out javascript to move progress bar
|
||||
*
|
||||
* @param float $step percent step
|
||||
* @return void has no return
|
||||
*/
|
||||
public function moveStep(float $step): void
|
||||
{
|
||||
$last_step = $this->step;
|
||||
$this->__setStep($step);
|
||||
|
||||
$js = '';
|
||||
$new_position = $this->__calculatePosition($this->step);
|
||||
if (
|
||||
$new_position['width'] != $this->position['width'] &&
|
||||
($this->direction == 'right' || $this->direction == 'left')
|
||||
) {
|
||||
if ($this->direction == 'left') {
|
||||
$js .= 'PBposition' . $this->code . '("left",' . $new_position['left'] . ');';
|
||||
}
|
||||
$js .= 'PBposition' . $this->code . '("width",' . $new_position['width'] . ');';
|
||||
}
|
||||
if (
|
||||
$new_position['height'] != $this->position['height'] &&
|
||||
($this->direction == 'up' || $this->direction == 'down')
|
||||
) {
|
||||
if ($this->direction == 'up') {
|
||||
$js .= 'PBposition' . $this->code . '("top",' . $new_position['top'] . ');';
|
||||
}
|
||||
$js .= 'PBposition' . $this->code . '("height",' . $new_position['height'] . ');';
|
||||
}
|
||||
$this->position = $new_position;
|
||||
foreach ($this->label as $name => $data) {
|
||||
if (array_key_exists('type', $data)) {
|
||||
switch ($data['type']) {
|
||||
case 'step':
|
||||
if ($this->step != $last_step) {
|
||||
$js .= 'PBlabelText' . $this->code . '("'
|
||||
. $name . '","' . $this->step . '/' . $this->max . '");';
|
||||
}
|
||||
break;
|
||||
case 'percentlbl':
|
||||
case 'percent':
|
||||
$percent = $this->__calculatePercent($this->step);
|
||||
if ($percent != $this->__calculatePercent($last_step)) {
|
||||
$js .= 'PBlabelText' . $this->code . '("' . $name . '","' . $percent . '%");';
|
||||
}
|
||||
break;
|
||||
case 'crossbar':
|
||||
$js .= 'PBrotaryCross' . $name . $this->code . '();';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($js != '') {
|
||||
echo '<script type="text/JavaScript">' . $js . '</script>' . "\n";
|
||||
$this->__flushCache();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* moves progress bar by one step (1)
|
||||
*
|
||||
* @return void has no return
|
||||
*/
|
||||
public function moveNext(): void
|
||||
{
|
||||
$this->moveStep($this->step + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* moves the progress bar back to the beginning
|
||||
*
|
||||
* @return void has no return
|
||||
*/
|
||||
public function moveMin(): void
|
||||
{
|
||||
$this->moveStep($this->min);
|
||||
}
|
||||
|
||||
/**
|
||||
* hide the progress bar if it is visible
|
||||
*
|
||||
* @return void has no return
|
||||
*/
|
||||
public function hide(): void
|
||||
{
|
||||
if ($this->status == 'show') {
|
||||
$this->status = 'hide';
|
||||
|
||||
$output = '<script type="text/JavaScript">'
|
||||
. 'document.getElementById("pbm' . $this->code
|
||||
. '").style.visibility="hidden";document.getElementById("pbm'
|
||||
. $this->code . '").style.display="none";'
|
||||
. '</script>' . "\n";
|
||||
echo $output;
|
||||
$this->__flushCache();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* show progress bar again after it was hidden with hide()
|
||||
*
|
||||
* @return void has no return
|
||||
*/
|
||||
public function unhide(): void
|
||||
{
|
||||
if ($this->status == 'hide') {
|
||||
$this->status = 'show';
|
||||
|
||||
$output = '<script type="text/JavaScript">'
|
||||
. 'document.getElementById("pbm' . $this->code
|
||||
. '").style.visibility="visible";document.getElementById("pbm'
|
||||
. $this->code . '").style.visibility="block";'
|
||||
. '</script>' . "\n";
|
||||
echo $output;
|
||||
$this->__flushCache();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
16
4dev/deprecated/CoreLibs/Output/TableArraysInterface.php
Normal file
16
4dev/deprecated/CoreLibs/Output/TableArraysInterface.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Output\Form;
|
||||
|
||||
interface TableArraysInterface
|
||||
{
|
||||
/**
|
||||
* setTableArray interface, set the table array
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function setTableArray(): array;
|
||||
}
|
||||
|
||||
// __END__
|
||||
551
4dev/deprecated/CoreLibs/Template/SmartyExtend.php
Normal file
551
4dev/deprecated/CoreLibs/Template/SmartyExtend.php
Normal file
@@ -0,0 +1,551 @@
|
||||
<?php // phpcs:ignore PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* @phan-file-suppress PhanRedefinedExtendedClass
|
||||
*/
|
||||
|
||||
// because smarty is symlinked folder
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/********************************************************************
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2004/12/21
|
||||
* SHORT DESCRIPTION:
|
||||
* extends smarty with the l10n class so I can use __(, etc calls
|
||||
* HISTORY:
|
||||
* 2005/06/22 (cs) include smarty class here, so there is no need to include it in the main file
|
||||
*********************************************************************/
|
||||
|
||||
namespace CoreLibs\Template;
|
||||
|
||||
// leading slash if this is in lib\Smarty
|
||||
class SmartyExtend extends \Smarty
|
||||
{
|
||||
// internal translation engine
|
||||
/** @var \CoreLibs\Language\L10n */
|
||||
public $l10n;
|
||||
|
||||
// lang & encoding
|
||||
/** @var string */
|
||||
public $lang_dir = '';
|
||||
/** @var string */
|
||||
public $lang;
|
||||
/** @var string */
|
||||
public $locale_set;
|
||||
/** @var string */
|
||||
public $lang_short;
|
||||
/** @var string */
|
||||
public $domain;
|
||||
/** @var string */
|
||||
public $encoding;
|
||||
// page name
|
||||
/** @var string */
|
||||
public $page_name;
|
||||
|
||||
// array for data parsing
|
||||
/** @var array<mixed> */
|
||||
public $HEADER = [];
|
||||
/** @var array<mixed> */
|
||||
public $DATA = [];
|
||||
/** @var array<mixed> */
|
||||
public $DEBUG_DATA = [];
|
||||
/** @var array<mixed> */
|
||||
private $CONTENT_DATA = [];
|
||||
// control vars
|
||||
/** @var bool */
|
||||
public $USE_PROTOTYPE = USE_PROTOTYPE;
|
||||
/** @var bool */
|
||||
public $USE_JQUERY = USE_JQUERY;
|
||||
/** @var bool */
|
||||
public $USE_SCRIPTACULOUS = USE_SCRIPTACULOUS;
|
||||
// sub content input vars
|
||||
/** @var bool */
|
||||
public $USE_TINY_MCE = false;
|
||||
/** @var bool */
|
||||
public $JS_DATEPICKR = false;
|
||||
/** @var bool */
|
||||
public $JS_FLATPICKR = false;
|
||||
/** @var bool */
|
||||
public $JS_FILE_UPLOADER = false;
|
||||
/** @var bool */
|
||||
public $DEBUG_TMPL = false;
|
||||
/** @var bool */
|
||||
public $USE_INCLUDE_TEMPLATE = false;
|
||||
// cache & compile
|
||||
/** @var string */
|
||||
public $CACHE_ID = '';
|
||||
/** @var string */
|
||||
public $COMPILE_ID = '';
|
||||
// template vars
|
||||
/** @var string */
|
||||
public $MASTER_TEMPLATE_NAME;
|
||||
/** @var string */
|
||||
public $PAGE_FILE_NAME;
|
||||
/** @var string */
|
||||
public $CONTENT_INCLUDE;
|
||||
/** @var string */
|
||||
public $FORM_NAME;
|
||||
/** @var string */
|
||||
public $FORM_ACTION;
|
||||
/** @var string */
|
||||
public $L_TITLE;
|
||||
/** @var string|int */
|
||||
public $PAGE_WIDTH;
|
||||
// smarty include/set var
|
||||
/** @var string */
|
||||
public $TEMPLATE_PATH;
|
||||
/** @var string */
|
||||
public $TEMPLATE_NAME;
|
||||
/** @var string */
|
||||
public $INC_TEMPLATE_NAME;
|
||||
/** @var string */
|
||||
public $JS_TEMPLATE_NAME;
|
||||
/** @var string */
|
||||
public $CSS_TEMPLATE_NAME;
|
||||
/** @var string|null */
|
||||
public $TEMPLATE_TRANSLATE;
|
||||
/** @var string|null */
|
||||
public $JS_TRANSLATE;
|
||||
// core group
|
||||
/** @var string */
|
||||
public $JS_CORE_TEMPLATE_NAME;
|
||||
/** @var string */
|
||||
public $CSS_CORE_TEMPLATE_NAME;
|
||||
/** @var string */
|
||||
public $JS_CORE_INCLUDE;
|
||||
/** @var string */
|
||||
public $CSS_CORE_INCLUDE;
|
||||
// local names
|
||||
/** @var string */
|
||||
public $JS_SPECIAL_TEMPLATE_NAME = '';
|
||||
/** @var string */
|
||||
public $CSS_SPECIAL_TEMPLATE_NAME = '';
|
||||
/** @var string */
|
||||
public $JS_INCLUDE;
|
||||
/** @var string */
|
||||
public $CSS_INCLUDE;
|
||||
/** @var string */
|
||||
public $JS_SPECIAL_INCLUDE;
|
||||
/** @var string */
|
||||
public $CSS_SPECIAL_INCLUDE;
|
||||
/** @var string */
|
||||
public $ADMIN_JAVASCRIPT;
|
||||
/** @var string */
|
||||
public $ADMIN_STYLESHEET;
|
||||
/** @var string */
|
||||
public $FRONTEND_JAVASCRIPT;
|
||||
/** @var string */
|
||||
public $FRONTEND_STYLESHEET;
|
||||
// other smarty folder vars
|
||||
/** @var string */
|
||||
public $INCLUDES;
|
||||
/** @var string */
|
||||
public $JAVASCRIPT;
|
||||
/** @var string */
|
||||
public $CSS;
|
||||
/** @var string */
|
||||
public $FONT;
|
||||
/** @var string */
|
||||
public $PICTURES;
|
||||
/** @var string */
|
||||
public $CACHE_PICTURES;
|
||||
/** @var string */
|
||||
public $CACHE_PICTURES_ROOT;
|
||||
|
||||
// constructor class, just sets the language stuff
|
||||
/**
|
||||
* constructor class, just sets the language stuff
|
||||
* calls L10 for pass on internaly in smarty
|
||||
* also registers the getvar caller plugin
|
||||
*
|
||||
* @param \CoreLibs\Language\L10n $l10n l10n language class
|
||||
* @param array<string,string> $locale locale data read from setLocale
|
||||
*/
|
||||
public function __construct(\CoreLibs\Language\L10n $l10n, array $locale)
|
||||
{
|
||||
// call basic smarty
|
||||
// or Smarty::__construct();
|
||||
parent::__construct();
|
||||
// iinit lang
|
||||
$this->l10n = $l10n;
|
||||
// parse and read, legacy stuff
|
||||
$this->encoding = $locale['encoding'];
|
||||
$this->lang = $locale['lang'];
|
||||
// get first part from lang
|
||||
$this->lang_short = explode('_', $locale['lang'])[0];
|
||||
$this->domain = $this->l10n->getDomain();
|
||||
$this->locale_set = $this->l10n->getLocaleSet();
|
||||
$this->lang_dir = $this->l10n->getBaseLocalePath();
|
||||
|
||||
// opt load functions so we can use legacy init for smarty run perhaps
|
||||
\CoreLibs\Language\L10n::loadFunctions();
|
||||
_setlocale(LC_MESSAGES, $locale['locale']);
|
||||
_textdomain($this->domain);
|
||||
_bindtextdomain($this->domain, $this->lang_dir);
|
||||
_bind_textdomain_codeset($this->domain, $this->encoding);
|
||||
|
||||
// register smarty variable
|
||||
$this->registerPlugin('modifier', 'getvar', [&$this, 'getTemplateVars']);
|
||||
|
||||
$this->page_name = \CoreLibs\Get\System::getPageName();
|
||||
|
||||
// set internal settings
|
||||
$this->CACHE_ID = defined('CACHE_ID') ? CACHE_ID : '';
|
||||
$this->COMPILE_ID = defined('COMPILE_ID') ? COMPILE_ID : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function setSmartCoreIncludeCssJs(): void
|
||||
{
|
||||
// core CS
|
||||
$this->CSS_CORE_INCLUDE = '';
|
||||
if (
|
||||
file_exists($this->CSS . $this->CSS_CORE_TEMPLATE_NAME) &&
|
||||
is_file($this->CSS . $this->CSS_CORE_TEMPLATE_NAME)
|
||||
) {
|
||||
$this->CSS_CORE_INCLUDE = $this->CSS . $this->CSS_CORE_TEMPLATE_NAME;
|
||||
}
|
||||
// core JS
|
||||
$this->JS_CORE_INCLUDE = '';
|
||||
if (
|
||||
file_exists($this->JAVASCRIPT . $this->JS_CORE_TEMPLATE_NAME) &&
|
||||
is_file($this->JAVASCRIPT . $this->JS_CORE_TEMPLATE_NAME)
|
||||
) {
|
||||
$this->JS_CORE_INCLUDE = $this->JAVASCRIPT . $this->JS_CORE_TEMPLATE_NAME;
|
||||
}
|
||||
// additional per page Javascript include
|
||||
$this->JS_INCLUDE = '';
|
||||
if (
|
||||
file_exists($this->JAVASCRIPT . $this->JS_TEMPLATE_NAME) &&
|
||||
is_file($this->JAVASCRIPT . $this->JS_TEMPLATE_NAME)
|
||||
) {
|
||||
$this->JS_INCLUDE = $this->JAVASCRIPT . $this->JS_TEMPLATE_NAME;
|
||||
}
|
||||
// per page css file
|
||||
$this->CSS_INCLUDE = '';
|
||||
if (
|
||||
file_exists($this->CSS . $this->CSS_TEMPLATE_NAME) &&
|
||||
is_file($this->CSS . $this->CSS_TEMPLATE_NAME)
|
||||
) {
|
||||
$this->CSS_INCLUDE = $this->CSS . $this->CSS_TEMPLATE_NAME;
|
||||
}
|
||||
// optional CSS file
|
||||
$this->CSS_SPECIAL_INCLUDE = '';
|
||||
if (
|
||||
file_exists($this->CSS . $this->CSS_SPECIAL_TEMPLATE_NAME) &&
|
||||
is_file($this->CSS . $this->CSS_SPECIAL_TEMPLATE_NAME)
|
||||
) {
|
||||
$this->CSS_SPECIAL_INCLUDE = $this->CSS . $this->CSS_SPECIAL_TEMPLATE_NAME;
|
||||
}
|
||||
// optional JS file
|
||||
$this->JS_SPECIAL_INCLUDE = '';
|
||||
if (
|
||||
file_exists($this->JAVASCRIPT . $this->JS_SPECIAL_TEMPLATE_NAME) &&
|
||||
is_file($this->JAVASCRIPT . $this->JS_SPECIAL_TEMPLATE_NAME)
|
||||
) {
|
||||
$this->JS_SPECIAL_INCLUDE = $this->JAVASCRIPT . $this->JS_SPECIAL_TEMPLATE_NAME;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* sets all internal paths and names that need to be passed on to the smarty template
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setSmartyPaths(): void
|
||||
{
|
||||
// master template
|
||||
if (empty($this->MASTER_TEMPLATE_NAME)) {
|
||||
$this->MASTER_TEMPLATE_NAME = MASTER_TEMPLATE_NAME;
|
||||
}
|
||||
|
||||
// set include & template names
|
||||
if (empty($this->CONTENT_INCLUDE)) {
|
||||
$this->CONTENT_INCLUDE = str_replace('.php', '', $this->page_name) . '.tpl';
|
||||
}
|
||||
// strip tpl and replace it with php
|
||||
// php include file per page
|
||||
$this->INC_TEMPLATE_NAME = str_replace('.tpl', '.php', $this->CONTENT_INCLUDE);
|
||||
// javascript include per page
|
||||
$this->JS_TEMPLATE_NAME = str_replace('.tpl', '.js', $this->CONTENT_INCLUDE);
|
||||
// css per page
|
||||
$this->CSS_TEMPLATE_NAME = str_replace('.tpl', '.css', $this->CONTENT_INCLUDE);
|
||||
|
||||
// set basic template path (tmp)
|
||||
$this->INCLUDES = BASE . INCLUDES; // no longer in templates, only global
|
||||
$this->TEMPLATE_PATH = BASE . INCLUDES . TEMPLATES . CONTENT_PATH;
|
||||
$this->setTemplateDir($this->TEMPLATE_PATH);
|
||||
$this->JAVASCRIPT = LAYOUT . JS;
|
||||
$this->CSS = LAYOUT . CSS;
|
||||
$this->FONT = LAYOUT . FONT;
|
||||
$this->PICTURES = LAYOUT . IMAGES;
|
||||
$this->CACHE_PICTURES = LAYOUT . CACHE;
|
||||
$this->CACHE_PICTURES_ROOT = ROOT . $this->CACHE_PICTURES;
|
||||
// check if we have an external file with the template name
|
||||
if (
|
||||
file_exists($this->INCLUDES . $this->INC_TEMPLATE_NAME) &&
|
||||
is_file($this->INCLUDES . $this->INC_TEMPLATE_NAME)
|
||||
) {
|
||||
include($this->INCLUDES . $this->INC_TEMPLATE_NAME);
|
||||
}
|
||||
// check for template include
|
||||
if (
|
||||
$this->USE_INCLUDE_TEMPLATE === true &&
|
||||
!$this->TEMPLATE_NAME
|
||||
) {
|
||||
$this->TEMPLATE_NAME = $this->CONTENT_INCLUDE;
|
||||
// add to cache & compile id
|
||||
$this->COMPILE_ID .= '_' . $this->TEMPLATE_NAME;
|
||||
$this->CACHE_ID .= '_' . $this->TEMPLATE_NAME;
|
||||
}
|
||||
// set all the additional CSS/JS parths
|
||||
$this->setSmartCoreIncludeCssJs();
|
||||
// check if template names exist
|
||||
if (!$this->MASTER_TEMPLATE_NAME) {
|
||||
exit('MASTER TEMPLATE is not set');
|
||||
} elseif (!file_exists($this->getTemplateDir()[0] . DIRECTORY_SEPARATOR . $this->MASTER_TEMPLATE_NAME)) {
|
||||
// abort if master template could not be found
|
||||
exit('MASTER TEMPLATE: ' . $this->MASTER_TEMPLATE_NAME . ' could not be found');
|
||||
}
|
||||
if (
|
||||
$this->TEMPLATE_NAME &&
|
||||
!file_exists($this->getTemplateDir()[0] . DIRECTORY_SEPARATOR . $this->TEMPLATE_NAME)
|
||||
) {
|
||||
exit('INCLUDE TEMPLATE: ' . $this->TEMPLATE_NAME . ' could not be found');
|
||||
}
|
||||
// javascript translate data as template for auto translate
|
||||
if (empty($this->TEMPLATE_TRANSLATE)) {
|
||||
$this->TEMPLATE_TRANSLATE = 'jsTranslate-'
|
||||
. $this->locale_set . '.' . $this->encoding
|
||||
. '.tpl';
|
||||
} else {
|
||||
// we assume we have some fixed set
|
||||
// we must add _<locale>.<encoding>
|
||||
// if .tpl, put before .tpl
|
||||
// if not .tpl, add _<locale>.<encoding>.tpl
|
||||
if (strpos($this->TEMPLATE_TRANSLATE, '.tpl')) {
|
||||
$this->TEMPLATE_TRANSLATE = str_replace(
|
||||
'.tpl',
|
||||
'-' . $this->locale_set . '.' . $this->encoding . '.tpl',
|
||||
$this->TEMPLATE_TRANSLATE
|
||||
);
|
||||
} else {
|
||||
$this->TEMPLATE_TRANSLATE .= '-'
|
||||
. $this->locale_set . '.' . $this->encoding
|
||||
. '.tpl';
|
||||
}
|
||||
}
|
||||
// if we can't find it, dump it
|
||||
if (!file_exists($this->getTemplateDir()[0] . DIRECTORY_SEPARATOR . $this->TEMPLATE_TRANSLATE)) {
|
||||
$this->TEMPLATE_TRANSLATE = null;
|
||||
}
|
||||
if (empty($this->JS_TRANSLATE)) {
|
||||
$this->JS_TRANSLATE = 'translate-'
|
||||
. $this->locale_set . '.' . $this->encoding . '.js';
|
||||
} else {
|
||||
// we assume we have some fixed set
|
||||
// we must add _<locale>.<encoding>
|
||||
// if .js, put before .js
|
||||
// if not .js, add _<locale>.<encoding>.js
|
||||
if (strpos($this->JS_TRANSLATE, '.js')) {
|
||||
$this->JS_TRANSLATE = str_replace(
|
||||
'.js',
|
||||
'-' . $this->locale_set . '.' . $this->encoding . '.js',
|
||||
$this->JS_TRANSLATE
|
||||
);
|
||||
} else {
|
||||
$this->JS_TRANSLATE .= '-'
|
||||
. $this->locale_set . '.' . $this->encoding
|
||||
. '.js';
|
||||
}
|
||||
}
|
||||
if (!file_exists($this->JAVASCRIPT . $this->JS_TRANSLATE)) {
|
||||
$this->JS_TRANSLATE = null;
|
||||
} else {
|
||||
$this->JS_TRANSLATE = $this->JAVASCRIPT . $this->JS_TRANSLATE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper call for setSmartyVars
|
||||
* this is for frontend type and will not set any only admin needed variables
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setSmartyVarsFrontend(): void
|
||||
{
|
||||
$this->setSmartyVars();
|
||||
}
|
||||
|
||||
/**
|
||||
* wrapper call for setSmartyVars
|
||||
* this is only for admin interface and will set additional variables
|
||||
*/
|
||||
public function setSmartyVarsAdmin(): void
|
||||
{
|
||||
$this->setSmartyVars(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* set smarty pass on variables, sub template names and finally calls the smarty parser
|
||||
*
|
||||
* @param boolean $admin_call default false, will set admin only variables
|
||||
* @return void
|
||||
*/
|
||||
private function setSmartyVars($admin_call = false): void
|
||||
{
|
||||
/** @var \CoreLibs\Admin\Backend This is an assumption */
|
||||
global $cms;
|
||||
if (is_object($cms)) {
|
||||
$this->mergeCmsSmartyVars($cms);
|
||||
}
|
||||
|
||||
// trigger flags
|
||||
$this->HEADER['USE_PROTOTYPE'] = $this->USE_PROTOTYPE;
|
||||
// scriptacolous, can only be used with prototype
|
||||
if ($this->HEADER['USE_PROTOTYPE']) {
|
||||
$this->HEADER['USE_SCRIPTACULOUS'] = $this->USE_SCRIPTACULOUS;
|
||||
}
|
||||
// jquery and prototype should not be used together
|
||||
$this->HEADER['USE_JQUERY'] = $this->USE_JQUERY;
|
||||
|
||||
// set all the additional CSS/JS parths
|
||||
$this->setSmartCoreIncludeCssJs();
|
||||
|
||||
// the actual include files for javascript (per page)
|
||||
$this->HEADER['JS_CORE_INCLUDE'] = $this->JS_CORE_INCLUDE;
|
||||
$this->HEADER['CSS_CORE_INCLUDE'] = $this->CSS_CORE_INCLUDE;
|
||||
$this->HEADER['JS_INCLUDE'] = $this->JS_INCLUDE;
|
||||
$this->HEADER['CSS_INCLUDE'] = $this->CSS_INCLUDE;
|
||||
$this->HEADER['CSS_SPECIAL_INCLUDE'] = $this->CSS_SPECIAL_INCLUDE;
|
||||
$this->HEADER['JS_SPECIAL_INCLUDE'] = $this->JS_SPECIAL_INCLUDE;
|
||||
// paths to the files
|
||||
$this->DATA['includes'] = $this->INCLUDES;
|
||||
$this->DATA['js'] = $this->JAVASCRIPT;
|
||||
$this->DATA['css'] = $this->CSS;
|
||||
$this->DATA['pictures'] = $this->PICTURES;
|
||||
|
||||
// default CMS settings
|
||||
// define all needed smarty stuff for the general HTML/page building
|
||||
$this->HEADER['CSS'] = CSS;
|
||||
$this->HEADER['FONT'] = FONT;
|
||||
$this->HEADER['JS'] = JS;
|
||||
$this->HEADER['ENCODING'] = $this->encoding;
|
||||
$this->HEADER['DEFAULT_ENCODING'] = DEFAULT_ENCODING;
|
||||
|
||||
// form name
|
||||
$this->DATA['FORM_NAME'] = !$this->FORM_NAME ?
|
||||
str_replace('.php', '', $this->page_name) :
|
||||
$this->FORM_NAME;
|
||||
$this->DATA['FORM_ACTION'] = $this->FORM_ACTION;
|
||||
// special for admin
|
||||
if ($admin_call === true) {
|
||||
// set ACL extra show
|
||||
if (is_object($cms)) {
|
||||
$this->DATA['show_ea_extra'] = $cms->acl['show_ea_extra'] ?? false;
|
||||
$this->DATA['ADMIN'] = $cms->acl['admin'] ?? 0;
|
||||
// top menu
|
||||
$this->DATA['nav_menu'] = $cms->adbTopMenu();
|
||||
$this->DATA['nav_menu_count'] = count($this->DATA['nav_menu']);
|
||||
// messages = ['msg' =>, 'class' => 'error/warning/...']
|
||||
$this->DATA['messages'] = $cms->messages;
|
||||
} else { /** @phpstan-ignore-line Because I assume object for phpstan */
|
||||
$this->DATA['show_ea_extra'] = false;
|
||||
$this->DATA['ADMIN'] = 0;
|
||||
$this->DATA['nav_menu'] = [];
|
||||
$this->DATA['nav_menu_count'] = 0;
|
||||
$this->DATA['messages'] = [];
|
||||
}
|
||||
// set style sheets
|
||||
$this->HEADER['STYLESHEET'] = $this->ADMIN_STYLESHEET ? $this->ADMIN_STYLESHEET : ADMIN_STYLESHEET;
|
||||
$this->HEADER['JAVASCRIPT'] = $this->ADMIN_JAVASCRIPT ? $this->ADMIN_JAVASCRIPT : ADMIN_JAVASCRIPT;
|
||||
// the page name
|
||||
$this->DATA['page_name'] = $this->page_name;
|
||||
$this->DATA['table_width'] = !empty($this->PAGE_WIDTH) ?: PAGE_WIDTH;
|
||||
$this->DATA['form_name'] = $this->DATA['FORM_NAME'];
|
||||
// for tinymce special
|
||||
$this->DATA['TINYMCE_LANG'] = $this->lang_short;
|
||||
// include flags
|
||||
$this->DATA['USE_TINY_MCE'] = $this->USE_TINY_MCE;
|
||||
// debug data, if DEBUG flag is on, this data is print out
|
||||
$this->DEBUG_DATA['DEBUG'] = $this->DEBUG_TMPL;
|
||||
} else {
|
||||
$this->HEADER['STYLESHEET'] = $this->FRONTEND_STYLESHEET ? $this->FRONTEND_STYLESHEET : STYLESHEET;
|
||||
$this->HEADER['JAVASCRIPT'] = $this->FRONTEND_JAVASCRIPT ? $this->FRONTEND_JAVASCRIPT : JAVASCRIPT;
|
||||
}
|
||||
// html title
|
||||
// set local page title
|
||||
$this->HEADER['HTML_TITLE'] = !$this->L_TITLE ?
|
||||
ucfirst(str_replace('_', ' ', \CoreLibs\Get\System::getPageName(1)))
|
||||
. (defined('G_TITLE') ? ' - ' . $this->l10n->__(G_TITLE) : '') :
|
||||
$this->l10n->__($this->L_TITLE);
|
||||
|
||||
// LANG
|
||||
$this->DATA['LANG'] = $this->lang;
|
||||
// include flags
|
||||
$this->DATA['JS_DATEPICKR'] = $this->JS_DATEPICKR;
|
||||
$this->DATA['JS_FLATPICKR'] = $this->JS_FLATPICKR;
|
||||
$this->DATA['JS_FILE_UPLOADER'] = $this->JS_FILE_UPLOADER;
|
||||
// user name
|
||||
$this->DATA['USER_NAME'] = !empty($_SESSION['USER_NAME']) ? $_SESSION['USER_NAME'] : '';
|
||||
// the template part to include into the body
|
||||
$this->DATA['TEMPLATE_NAME'] = $this->TEMPLATE_NAME;
|
||||
$this->DATA['CONTENT_INCLUDE'] = $this->CONTENT_INCLUDE;
|
||||
$this->DATA['TEMPLATE_TRANSLATE'] = $this->TEMPLATE_TRANSLATE ?? null;
|
||||
$this->DATA['JS_TRANSLATE'] = $this->JS_TRANSLATE ?? null;
|
||||
$this->DATA['PAGE_FILE_NAME'] = str_replace('.php', '', $this->page_name) . '.tpl';
|
||||
// render page
|
||||
$this->renderSmarty();
|
||||
}
|
||||
|
||||
/**
|
||||
* merge outside object HEADER/DATA/DEBUG_DATA vars into the smarty class
|
||||
*
|
||||
* @param object $cms object that has header/data/debug_data
|
||||
* @return void
|
||||
*/
|
||||
public function mergeCmsSmartyVars(object $cms): void
|
||||
{
|
||||
// array merge HEADER, DATA, DEBUG DATA
|
||||
foreach (['HEADER', 'DATA', 'DEBUG_DATA'] as $ext_smarty) {
|
||||
if (
|
||||
isset($cms->{$ext_smarty}) &&
|
||||
is_array($cms->{$ext_smarty})
|
||||
) {
|
||||
$this->{$ext_smarty} = array_merge($this->{$ext_smarty}, $cms->{$ext_smarty});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* render smarty data (can be called sepparate)
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function renderSmarty(): void
|
||||
{
|
||||
// create main data array
|
||||
$this->CONTENT_DATA = array_merge($this->HEADER, $this->DATA, $this->DEBUG_DATA);
|
||||
// data is 1:1 mapping (all vars, values, etc)
|
||||
foreach ($this->CONTENT_DATA as $key => $value) {
|
||||
$this->assign($key, $value);
|
||||
}
|
||||
if (is_dir(BASE . TEMPLATES_C)) {
|
||||
$this->setCompileDir(BASE . TEMPLATES_C);
|
||||
}
|
||||
if (is_dir(BASE . CACHE)) {
|
||||
$this->setCacheDir(BASE . CACHE);
|
||||
}
|
||||
$this->display(
|
||||
$this->MASTER_TEMPLATE_NAME,
|
||||
$this->CACHE_ID . ($this->CACHE_ID ? '_' : '') . $this->lang,
|
||||
$this->COMPILE_ID . ($this->COMPILE_ID ? '_' : '') . $this->lang
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
1
4dev/deprecated/Smarty
Symbolic link
1
4dev/deprecated/Smarty
Symbolic link
@@ -0,0 +1 @@
|
||||
smarty-4.3.0/libs/
|
||||
111
4dev/deprecated/smarty-4.3.0/libs/Autoloader.php
Normal file
111
4dev/deprecated/smarty-4.3.0/libs/Autoloader.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
/**
|
||||
* Smarty Autoloader
|
||||
*
|
||||
* @package Smarty
|
||||
*/
|
||||
|
||||
|
||||
if (!defined('SMARTY_HELPER_FUNCTIONS_LOADED')) {
|
||||
include __DIR__ . '/functions.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Smarty Autoloader
|
||||
*
|
||||
* @package Smarty
|
||||
* @author Uwe Tews
|
||||
* Usage:
|
||||
* require_once '...path/Autoloader.php';
|
||||
* Smarty_Autoloader::register();
|
||||
* or
|
||||
* include '...path/bootstrap.php';
|
||||
*
|
||||
* $smarty = new Smarty();
|
||||
*/
|
||||
class Smarty_Autoloader
|
||||
{
|
||||
/**
|
||||
* Filepath to Smarty root
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $SMARTY_DIR = null;
|
||||
|
||||
/**
|
||||
* Filepath to Smarty internal plugins
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $SMARTY_SYSPLUGINS_DIR = null;
|
||||
|
||||
/**
|
||||
* Array with Smarty core classes and their filename
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $rootClasses = array('smarty' => 'Smarty.class.php');
|
||||
|
||||
/**
|
||||
* Registers Smarty_Autoloader backward compatible to older installations.
|
||||
*
|
||||
* @param bool $prepend Whether to prepend the autoloader or not.
|
||||
*/
|
||||
public static function registerBC($prepend = false)
|
||||
{
|
||||
/**
|
||||
* register the class autoloader
|
||||
*/
|
||||
if (!defined('SMARTY_SPL_AUTOLOAD')) {
|
||||
define('SMARTY_SPL_AUTOLOAD', 0);
|
||||
}
|
||||
if (SMARTY_SPL_AUTOLOAD
|
||||
&& set_include_path(get_include_path() . PATH_SEPARATOR . SMARTY_SYSPLUGINS_DIR) !== false
|
||||
) {
|
||||
$registeredAutoLoadFunctions = spl_autoload_functions();
|
||||
if (!isset($registeredAutoLoadFunctions[ 'spl_autoload' ])) {
|
||||
spl_autoload_register();
|
||||
}
|
||||
} else {
|
||||
self::register($prepend);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers Smarty_Autoloader as an SPL autoloader.
|
||||
*
|
||||
* @param bool $prepend Whether to prepend the autoloader or not.
|
||||
*/
|
||||
public static function register($prepend = false)
|
||||
{
|
||||
self::$SMARTY_DIR = defined('SMARTY_DIR') ? SMARTY_DIR : __DIR__ . DIRECTORY_SEPARATOR;
|
||||
self::$SMARTY_SYSPLUGINS_DIR = defined('SMARTY_SYSPLUGINS_DIR') ? SMARTY_SYSPLUGINS_DIR :
|
||||
self::$SMARTY_DIR . 'sysplugins' . DIRECTORY_SEPARATOR;
|
||||
spl_autoload_register(array(__CLASS__, 'autoload'), true, $prepend);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles auto loading of classes.
|
||||
*
|
||||
* @param string $class A class name.
|
||||
*/
|
||||
public static function autoload($class)
|
||||
{
|
||||
if ($class[ 0 ] !== 'S' || strpos($class, 'Smarty') !== 0) {
|
||||
return;
|
||||
}
|
||||
$_class = smarty_strtolower_ascii($class);
|
||||
if (isset(self::$rootClasses[ $_class ])) {
|
||||
$file = self::$SMARTY_DIR . self::$rootClasses[ $_class ];
|
||||
if (is_file($file)) {
|
||||
include $file;
|
||||
}
|
||||
} else {
|
||||
$file = self::$SMARTY_SYSPLUGINS_DIR . $_class . '.php';
|
||||
if (is_file($file)) {
|
||||
include $file;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
1405
4dev/deprecated/smarty-4.3.0/libs/Smarty.class.php
Normal file
1405
4dev/deprecated/smarty-4.3.0/libs/Smarty.class.php
Normal file
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user