# Drop (s42transfer) – production image: Apache + PHP 8.4, nothing else.
#
#   docker compose up -d          build it yourself
#   docker pull registry.gitlab.com/jensche/s42transfer:latest
#
# The code is part of the image; configuration and data live on volumes, so
# updating means pulling a new image and nothing else.
FROM php:8.4-apache

RUN a2enmod rewrite headers

# Everything Drop needs is in a standard build – except that sodium is not
# compiled into every base image. Install it only if it is missing, then
# refuse to build an image that would run without what the code requires.
RUN set -eux; \
    if ! php -m | grep -qi '^sodium$'; then \
      apt-get update; \
      apt-get install -y --no-install-recommends libsodium-dev; \
      docker-php-ext-install -j"$(nproc)" sodium; \
      rm -rf /var/lib/apt/lists/*; \
    fi; \
    php -r 'foreach (["curl", "fileinfo", "mbstring", "openssl", "session", "sodium"] as $e) { \
        if (!extension_loaded($e)) { fwrite(STDERR, "missing PHP extension: $e\n"); exit(1); } }'

# OPcache: without it every request compiles lib/ again – 2.6 ms instead of
# 0.4 ms for a page, and for every part of an upload. The base image ships it
# but does not switch it on (PHP 8.5 always has it). The check at the end
# fails the build rather than let an image out without it.
RUN set -eux; \
    if ! php -m | grep -qi '^zend opcache$'; then \
      docker-php-ext-enable opcache || docker-php-ext-install -j"$(nproc)" opcache; \
    fi; \
    php -m | grep -qi '^zend opcache$'

# Uploads: the chunk size adapts to these limits automatically, so they are
# the size of one part, never the size of a file.
RUN { \
      echo 'upload_max_filesize = 64M'; \
      echo 'post_max_size = 66M'; \
      echo 'max_execution_time = 300'; \
      echo 'memory_limit = 256M'; \
      echo 'session.gc_maxlifetime = 2592000'; \
      echo 'expose_php = Off'; \
    } > /usr/local/etc/php/conf.d/drop.ini

# Configuration and data live on volumes; the code is replaced on every image update.
ENV S42_CONFIG=/var/www/data/config.local.php
# The code is part of the image: update by pulling/building a new one, not from the admin.
ENV S42_NO_UPDATE=1
# How often the housekeeping runs inside the container, in seconds (0 = never,
# for when an external scheduler does it).
ENV S42_CRON_SECONDS=900

# The code belongs to root: whatever the web server may write, it may not
# turn into code. Only the data and the uploaded images are its own.
COPY . /var/www/html/
RUN rm -rf /var/www/html/docs /var/www/html/tests /var/www/html/e2e /var/www/html/.claude /var/www/html/tools/sbom.py \
           /var/www/html/Dockerfile /var/www/html/docker-compose.yml \
           /var/www/html/docker-entrypoint.sh /var/www/html/.dockerignore \
           /var/www/html/.gitlab-ci.yml /var/www/html/.gitignore \
 && mkdir -p /var/www/data /var/www/html/uploads \
 && chown -R www-data:www-data /var/www/data /var/www/html/uploads

# There is no cron in a container, and without it nothing expires, no reminder
# goes out and the trash never empties. So the entrypoint runs the same two
# commands the cron lines run, in the background, and then hands over to Apache.
COPY docker-entrypoint.sh /usr/local/bin/drop-entrypoint
RUN chmod +x /usr/local/bin/drop-entrypoint

HEALTHCHECK --interval=30s --timeout=5s --start-period=20s \
  CMD php -r '$f = @fsockopen("127.0.0.1", 80, $e, $s, 3); exit($f ? 0 : 1);'

VOLUME ["/var/www/data", "/var/www/html/uploads"]
EXPOSE 80
ENTRYPOINT ["/usr/local/bin/drop-entrypoint"]
CMD ["apache2-foreground"]
