Skip to content

Commit

Permalink
Rewriting values in PHP to avoid escaping hell
Browse files Browse the repository at this point in the history
  • Loading branch information
rgaudin committed Jan 8, 2024
1 parent da1f65b commit a4a4227
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
9 changes: 5 additions & 4 deletions file-manager/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ if (is_readable(\$config_file)) {\n\
}\n" > index.php \
&& tail -n +$lnum tinyfilemanager.php >> index.php

COPY config.php $WEBDIR/
COPY listing_auth.php $WEBDIR/
COPY manager_auth.php $WEBDIR/
COPY conf-rewrite.php $WEBDIR/
COPY lighttpd.conf /etc/lighttpd/lighttpd.conf
COPY config.php /var/www/html
COPY listing_auth.php /var/www/html
COPY manager_auth.php /var/www/html
COPY entrypoint /usr/local/bin/

# there are three modes:
Expand All @@ -71,7 +72,7 @@ ENV ADMIN_PASSWORD "admin@123"

# timezone to use for date/time (assuming host has date)
# /!\ must be escaped! (slash)
ENV UI_TIMEZONE "Etc\/UTC"
ENV UI_TIMEZONE "Etc/UTC"

ENV UI_LANG "en"

Expand Down
21 changes: 21 additions & 0 deletions file-manager/conf-rewrite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
Rewrite config.php and manager_auth.php to replace expected patterns based on
envrion values.
*/

// APP_URL must not be set in mixed mode as there are two distinct URLs
if (getenv("ACCESS_MODE") == "mixed"){
$_SERVER['APP_URL'] = '';
}

foreach (array('config.php', 'manager_auth.php') as $fname) {
$config_text = file_get_contents($fname);
foreach(array("ADMIN_USERNAME", "ADMIN_PASSWORD", "UI_TIMEZONE", "UI_LANG", "APP_URL") as $pattern) {
$config_text = str_replace($pattern, getenv($pattern), $config_text);
}
file_put_contents($fname, $config_text);
}

?>
46 changes: 16 additions & 30 deletions file-manager/entrypoint
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
#!/bin/sh
set -e

echo "set credentials"
sed -i "s/ADMIN_USERNAME/${ADMIN_USERNAME}/g" manager_auth.php
sed -i "s/ADMIN_PASSWORD/${ADMIN_PASSWORD}/g" manager_auth.php

# mixed mode cannot set APP_URL as there are two apps
if [[ "$ACCESS_MODE" = "mixed" ]]
then
unset APP_URL
mkdir -p admin
cd admin
cp ../index.php index.php
cp ../config.php config.php
cp ../manager_auth.php manager_auth.php
ln -s manager_auth.php auth.php
# rewrite conf in /admin/ faking
if [[ "$APP_URL" != "" ]]
then
ADMIN_APP_URL="${APP_URL}/admin"
fi
APP_URL="$ADMIN_APP_URL" ACCESS_MODE=manager php83 ../conf-rewrite.php
cd ..
ln -s listing_auth.php auth.php
fi

# static replacement for envs (so we can keep clear_env=yes)
echo "set statics"
sed -i "s/UI_TIMEZONE/${UI_TIMEZONE}/g" config.php
sed -i "s/UI_LANG/${UI_LANG}/g" config.php
sed -i "s/APP_URL/${APP_URL}/g" config.php
# rewrite config for single-app mode
php83 ./conf-rewrite.php

echo "configuring for ${ACCESS_MODE}"
if [[ "$ACCESS_MODE" = "listing" ]]
Expand All @@ -24,28 +29,9 @@ then

elif [[ "$ACCESS_MODE" = "manager" ]]
then

ln -s manager_auth.php auth.php

elif [[ "$ACCESS_MODE" = "mixed" ]]
then
mkdir -p admin
cd admin
cp ../index.php index.php
cp ../config.php config.php
cp ../manager_auth.php auth.php
cd ..
ln -s listing_auth.php auth.php
else
echo "Unsupported mode: ${ACCESS_MODE}"
exit 1
fi

ls -l
cat config.php
cat auth.php
cat manager_auth.php

php-fpm83 -D

exec "$@"

0 comments on commit a4a4227

Please sign in to comment.