Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
316 changes: 316 additions & 0 deletions .ddev/.typo3-setup/scripts/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,316 @@
#!/bin/bash

#ddev-generated
# If you want to take over this file and customize it, remove the line above
# and ddev will respect it and won't overwrite the file.

# Function to get the lowest supported TYPO3 version from the environment variable TYPO3_VERSIONS
# It reads the TYPO3_VERSIONS environment variable, splits it into an array, and sorts the versions.
# If no versions are found, it prints an error message and exits with status 1.
# Otherwise, it prints the lowest version.
function get_lowest_supported_typo3_versions() {
local TYPO3_VERSIONS_ARRAY=()
IFS=' ' read -r -a TYPO3_VERSIONS_ARRAY <<< "$TYPO3_VERSIONS"
if [ ${#TYPO3_VERSIONS_ARRAY[@]} -eq 0 ]; then
message red "Error! No supported TYPO3 versions found in environment variables."
exit 1
fi
printf "%s\n" "${TYPO3_VERSIONS_ARRAY[@]}" | sort -V | head -n 1
}

# Function to get the supported TYPO3 versions from the environment variable TYPO3_VERSIONS.
# It checks if the TYPO3_VERSIONS environment variable is set and not empty.
# If the variable is unset or empty, it prints an error message and returns 1.
# Otherwise, it splits the TYPO3_VERSIONS variable into an array and prints the supported versions.
function get_supported_typo3_versions() {
if [ -z "${TYPO3_VERSIONS+x}" ]; then
message red "TYPO3_VERSIONS is unset. Please set it before running this function."
return 1
else
local TYPO3_VERSIONS_ARRAY=()
IFS=' ' read -r -a TYPO3_VERSIONS_ARRAY <<< "$TYPO3_VERSIONS"
if [ ${#TYPO3_VERSIONS_ARRAY[@]} -eq 0 ]; then
message red "Error! No supported TYPO3 versions found in environment variables."
return 1
fi
printf "%s\n" "${TYPO3_VERSIONS_ARRAY[@]}"
fi
}

# Function to check if a given TYPO3 version is supported.
# It takes one argument, the TYPO3 version to check.
# The function reads the supported TYPO3 versions from the environment variable TYPO3_VERSIONS.
# If the provided version is not in the list of supported versions, it prints an error message and exits with status 1.
# If the provided version is supported, it returns 0.
#
# Arguments:
# $1 - The TYPO3 version to check.
#
# Returns:
# 0 if the provided TYPO3 version is supported.
# 1 if the provided TYPO3 version is not supported or if no version is provided.
function check_typo3_version() {
local TYPO3=$1
local SUPPORTED_TYPO3_VERSIONS=()
local found=0

if [ -z "$TYPO3" ]; then
message red "No TYPO3 version provided. Please set one of the supported TYPO3 versions as argument: $(get_supported_typo3_versions_comma_separated)"
exit 1
fi

while IFS= read -r line; do
SUPPORTED_TYPO3_VERSIONS+=("$line")
done < <(get_supported_typo3_versions)

for version in "${SUPPORTED_TYPO3_VERSIONS[@]}"; do
if [[ "$version" == "$TYPO3" ]]; then
found=1
break
fi
done

if [[ $found -eq 0 ]]; then
message red "TYPO3 version '$TYPO3' is not supported."
exit 1
fi

return 0
}

# Function to perform pre-setup tasks for TYPO3 installation.
# It exports the provided TYPO3 version to the VERSION environment variable,
# displays an introductory message for the TYPO3 version, and starts the installation process.
#
# Arguments:
# $1 - The TYPO3 version to set up.
#
# Usage:
# pre_setup <TYPO3_VERSION>
function pre_setup() {
export VERSION=$1
export BASE_PATH="/var/www/html/.Build/$VERSION"
intro_typo3
message blue "Pre Setup for TYPO3 $VERSION"
install_start
message blue "Install Composer Packages"
}

# Function to perform post-setup tasks for TYPO3 installation.
# It changes the current directory to the base path, sets the TYPO3 installation database name,
# and calls the appropriate post-setup function based on the TYPO3 version.
# After that, it imports data and updates TYPO3.
function post_setup() {
message blue "Post Setup for TYPO3 $VERSION"
cd $BASE_PATH

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add error handling for directory changes.

Static analysis correctly identifies that cd commands should include error handling to prevent silent failures that could lead to operations in wrong directories.

Apply this diff to add proper error handling:

-  cd $BASE_PATH
+  cd "$BASE_PATH" || { message red "Failed to change to directory $BASE_PATH"; exit 1; }
-    cd $BASE_PATH
+    cd "$BASE_PATH" || { message red "Failed to change to directory $BASE_PATH"; exit 1; }

Also applies to: 202-202

🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 105-105: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

(SC2164)

🤖 Prompt for AI Agents
In .ddev/.typo3-setup/scripts/utils.sh at line 105, the cd command lacks error
handling which can cause silent failures if the directory change fails. Modify
the cd command to include error checking by appending a conditional that exits
the script or handles the error if the cd fails. Also apply the same fix to
lines 202-202 where a similar cd command is used without error handling.

TYPO3_INSTALL_DB_DBNAME=$DATABASE

if [ "$VERSION" == "11" ]; then
post_setup_11
elif [ "$VERSION" == "12" ]; then
post_setup_12
elif [ "$VERSION" == "13" ]; then
post_setup_13
fi

import_data
update_typo3
}

# Function to display an introductory message for the TYPO3 version.
# It prints a formatted message with the TYPO3 version in magenta color.
function intro_typo3() {
message magenta "-------------------------------------------------"
message magenta "|\t\t\t\t\t\t|"
message magenta "| \t\t TYPO3 $VERSION \t\t|"
message magenta "|\t\t\t\t\t\t|"
message magenta "-------------------------------------------------"
}

# Function to start the installation process for TYPO3.
# It removes any existing files in the test directory for the specified version,
# sets up the environment, creates symlinks for the main and additional extensions,
# and sets up Composer for the TYPO3 installation.
function install_start() {
rm -rf /var/www/html/.Build/$VERSION/*
setup_environment
create_symlinks_main_extension
create_symlinks_additional_extensions
setup_composer
}

# Function to set up the environment for TYPO3 installation.
# It sets the base path for the TYPO3 installation, removes any existing files in the base path,
# creates necessary directories, sets permissions, and exports environment variables.
# Additionally, it drops the existing database for the TYPO3 version.
function setup_environment() {
rm -rf "$BASE_PATH"
mkdir -p "$BASE_PATH/packages/$EXTENSION_KEY"
chmod 775 -R $BASE_PATH
export DATABASE="database_$VERSION"
if [ "$VERSION" == "11" ]; then
export TYPO3_BIN="$BASE_PATH/vendor/bin/typo3cms"
else
export TYPO3_BIN="$BASE_PATH/vendor/bin/typo3"
fi
mysql -uroot -proot -e "DROP DATABASE IF EXISTS $DATABASE"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Security concern: Hardcoded database credentials.

The script uses hardcoded database credentials (-uroot -proot) which poses security risks. Consider using environment variables or configuration files.

Apply this diff to use environment variables:

-    mysql -uroot -proot -e "DROP DATABASE IF EXISTS $DATABASE"
+    mysql -u"${DB_USER:-root}" -p"${DB_PASSWORD:-root}" -e "DROP DATABASE IF EXISTS $DATABASE"
-  mysql -h db -u root -p"root" -e "CREATE DATABASE $DATABASE;"
+  mysql -h db -u "${DB_USER:-root}" -p"${DB_PASSWORD:-root}" -e "CREATE DATABASE $DATABASE;"

Also applies to: 271-271

🤖 Prompt for AI Agents
In .ddev/.typo3-setup/scripts/utils.sh at lines 156 and 271, the script uses
hardcoded MySQL credentials (-uroot -proot), which is insecure. Replace these
hardcoded credentials with environment variables by referencing variables like
$DB_USER and $DB_PASSWORD instead. Ensure these environment variables are set
securely outside the script to avoid exposing sensitive information.

}

# Function to create symlinks for the main extension.
# It iterates over the items in the current directory, excluding certain directories and files,
# and creates symbolic links for the remaining items in the specified base path.
function create_symlinks_main_extension() {
local EXCLUSIONS_ARRAY=()
IFS=' ' read -r -a EXCLUSIONS_ARRAY <<< "$EXCLUSIONS"
for item in ./*; do
local base_name=$(basename "$item")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Fix variable assignment to avoid masking return values.

Static analysis correctly identifies that declaring and assigning in the same line can mask command failures.

Apply this diff to separate declaration and assignment:

-        local base_name=$(basename "$item")
+        local base_name
+        base_name=$(basename "$item")
🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 166-166: Declare and assign separately to avoid masking return values.

(SC2155)

🤖 Prompt for AI Agents
In .ddev/.typo3-setup/scripts/utils.sh at line 166, the variable assignment
combines declaration and assignment, which can mask command failures. To fix
this, declare the variable first without assignment, then assign the output of
the basename command on the next line. This separation ensures any errors from
the command are not masked by the assignment.

for exclusion in "${EXCLUSIONS_ARRAY[@]}"; do
if [[ $base_name == "$exclusion" ]]; then
continue 2
fi
done
ln -sr "$item" "$BASE_PATH/packages/$EXTENSION_KEY/$base_name"
done
}

# Function to create symlinks for additional extensions.
# It iterates over the directories in the specified path and creates symbolic links
# for each directory in the base path.
function create_symlinks_additional_extensions() {
for dir in Tests/.typo3-setup/packages/*/; do
ln -sr "$dir" "$BASE_PATH/packages/$(basename "$dir")"
done
}

# Function to set up Composer for TYPO3 installation.
# It initializes a new Composer project in the specified base path,
# configures the TYPO3 web directory, sets up the repository for packages,
# and allows necessary Composer plugins.
function setup_composer() {
composer init --name="xima/typo3-$VERSION" --description="TYPO3 $VERSION" --no-interaction --working-dir "$BASE_PATH"
composer config extra.typo3/cms.web-dir public --working-dir "$BASE_PATH"
composer config repositories.packages path 'packages/*' --working-dir "$BASE_PATH"
composer config --no-interaction allow-plugins.typo3/cms-composer-installers true --working-dir "$BASE_PATH"
composer config --no-interaction allow-plugins.typo3/class-alias-loader true --working-dir "$BASE_PATH"
}

# Function to set up TYPO3 configuration.
# It changes the current directory to the base path, sets the TYPO3 installation database name,
# and configures various TYPO3 settings such as debug mode, error display, trusted hosts pattern,
# mail transport, and graphics processor.
function setup_typo3() {
cd $BASE_PATH
export TYPO3_INSTALL_DB_DBNAME=$DATABASE
$TYPO3_BIN configuration:set 'BE/debug' 1
$TYPO3_BIN configuration:set 'FE/debug' 1
$TYPO3_BIN configuration:set 'SYS/devIPmask' '*'
$TYPO3_BIN configuration:set 'SYS/displayErrors' 1
$TYPO3_BIN configuration:set 'SYS/trustedHostsPattern' '.*.*'
$TYPO3_BIN configuration:set 'MAIL/transport' 'smtp'
$TYPO3_BIN configuration:set 'MAIL/transport_smtp_server' 'localhost:1025'
$TYPO3_BIN configuration:set 'GFX/processor' 'ImageMagick'
$TYPO3_BIN configuration:set 'GFX/processor_path' '/usr/bin/'
}

# Function to update TYPO3.
# It updates the TYPO3 database schema and flushes the cache.
function update_typo3() {
$TYPO3_BIN database:updateschema
$TYPO3_BIN cache:flush
}

# Function to import data into TYPO3.
# It sets the public directory and export directory paths, checks if the data file exists,
# and if it does, it copies the data file to the export directory and imports the data using TYPO3's import/export tool.
function import_data() {
PUBLIC_DIR="/var/www/html/.Build/${VERSION}/public"
EXPORT_DIR="${PUBLIC_DIR}/fileadmin/user_upload/_temp_/importexport"
DATA_FILE="/var/www/html/Tests/.typo3-setup/data/data.xml"

if [ ! -f "$DATA_FILE" ]; then
message yellow "Data file $DATA_FILE not found. Skipping import."
return
fi

mkdir -p $EXPORT_DIR
cp /$DATA_FILE $EXPORT_DIR
$TYPO3_BIN impexp:import -vvv --force-uid "$EXPORT_DIR/data.xml"
}

# Function to perform post-setup tasks for TYPO3 version 11.
# It sets up TYPO3 by running the installation setup, configuring TYPO3 settings,
# and modifying configuration files to enable deprecations and adjust base paths.
function post_setup_11 {
$TYPO3_BIN install:setup -n --database-name $DATABASE
setup_typo3
$TYPO3_BIN configuration:set 'GFX/processor_path_lzw' '/usr/bin/'

sed -i "/'deprecations'/,/^[[:space:]]*'disabled' => true,/s/'disabled' => true,/'disabled' => false,/" /var/www/html/.Build/$VERSION/public/typo3conf/LocalConfiguration.php

sed -i -e "s/base: ht\//base: \//g" /var/www/html/.Build/$VERSION/config/sites/main/config.yaml
sed -i -e 's/base: \/en\//base: \//g' /var/www/html/.Build/$VERSION/config/sites/main/config.yaml
Comment on lines +248 to +251

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve robustness of sed commands with better error handling.

The sed commands for configuration file modifications should include validation that the target files exist and the operations succeed.

Add file existence checks and error handling:

+  if [[ -f "/var/www/html/.Build/$VERSION/public/typo3conf/LocalConfiguration.php" ]]; then
     sed -i "/'deprecations'/,/^[[:space:]]*'disabled' => true,/s/'disabled' => true,/'disabled' => false,/" /var/www/html/.Build/$VERSION/public/typo3conf/LocalConfiguration.php
+  fi

+  if [[ -f "/var/www/html/.Build/$VERSION/config/sites/main/config.yaml" ]]; then
     sed -i -e "s/base: ht\//base: \//g" /var/www/html/.Build/$VERSION/config/sites/main/config.yaml
     sed -i -e 's/base: \/en\//base: \//g' /var/www/html/.Build/$VERSION/config/sites/main/config.yaml
+  fi

Also applies to: 261-264, 275-275

🤖 Prompt for AI Agents
In .ddev/.typo3-setup/scripts/utils.sh around lines 248 to 251, the sed commands
modify configuration files without checking if the target files exist or if the
commands succeed. To fix this, add checks before each sed command to verify the
file exists, and after running sed, check the command's exit status. If the file
does not exist or the sed command fails, output an error message and handle the
failure appropriately. Apply the same pattern of file existence checks and error
handling to the sed commands in lines 261-264 and 275.

}

# Function to perform post-setup tasks for TYPO3 version 12.
# It sets up TYPO3 by running the installation setup, configuring TYPO3 settings,
# and modifying configuration files to enable deprecations and adjust base paths.
function post_setup_12 {
$TYPO3_BIN install:setup -n --database-name $DATABASE
setup_typo3

sed -i "/'deprecations'/,/^[[:space:]]*'disabled' => true,/s/'disabled' => true,/'disabled' => false,/" /var/www/html/.Build/$VERSION/config/system/settings.php

sed -i -e "s/base: ht\//base: \//g" /var/www/html/.Build/$VERSION/config/sites/main/config.yaml
sed -i -e 's/base: \/en\//base: \//g' /var/www/html/.Build/$VERSION/config/sites/main/config.yaml
}

# Function to perform post-setup tasks for TYPO3 version 13.
# It creates the TYPO3 database, sets up TYPO3 by running the installation setup,
# configures TYPO3 settings, and modifies configuration files to enable deprecations.
function post_setup_13 {
mysql -h db -u root -p"root" -e "CREATE DATABASE $DATABASE;"
$TYPO3_BIN setup -n --dbname=$DATABASE --password=$TYPO3_DB_PASSWORD --create-site="https://${VERSION}.${EXTENSION_NAME}.ddev.site" --admin-user-password=$TYPO3_SETUP_ADMIN_PASSWORD
setup_typo3

sed -i "/'deprecations'/,/^[[:space:]]*'disabled' => true,/s/'disabled' => true,/'disabled' => false,/" /var/www/html/.Build/$VERSION/config/system/settings.php
}

# Function to display a colored message.
# It takes two arguments: the color and the message to display.
# The function supports the following colors: red, green, yellow, blue, magenta, cyan.
# If an unsupported color is provided, the message is displayed without color.
#
# Usage:
# message <color> <message>
#
# Arguments:
# color - The color to use for the message (red, green, yellow, blue, magenta, cyan).
# message - The message to display.
message() {
local color=$1
local message=$2

case $color in
red)
echo -e "\033[31m$message\033[0m"
;;
green)
echo -e "\033[32m$message\033[0m"
;;
yellow)
echo -e "\033[33m$message\033[0m"
;;
blue)
echo -e "\033[34m$message\033[0m"
;;
magenta)
echo -e "\033[35m$message\033[0m"
;;
cyan)
echo -e "\033[36m$message\033[0m"
;;
*)
echo -e "$message"
;;
esac
}
Loading
Loading