# Thinkera Academy — Moodle LMS image.
# There is no official production Moodle image (moodlehq/moodle-docker is for developers, and the free
# Bitnami image is unmaintained), so we build our own: Moodle source on php-fpm, nginx sits in front.
# Moodle 5.2 requires PHP 8.3+ (8.4 supported) and PostgreSQL 16+; this stack runs PostgreSQL 17.
FROM php:8.3-fpm-bookworm

ARG MOODLE_BRANCH=502
ARG MOODLE_TGZ=https://download.moodle.org/download.php/direct/stable502/moodle-latest-502.tgz

RUN apt-get update && apt-get install -y --no-install-recommends \
      libpng-dev libjpeg62-turbo-dev libfreetype6-dev libwebp-dev libzip-dev libicu-dev libxml2-dev \
      libxslt1-dev libsodium-dev libcurl4-openssl-dev libpq-dev ghostscript unzip cron curl ca-certificates \
      graphviz aspell rsync \
 && docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp \
 && docker-php-ext-install -j"$(nproc)" gd intl zip soap xsl exif opcache pgsql pdo_pgsql sodium \
 && pecl install redis apcu \
 && docker-php-ext-enable redis apcu \
 && rm -rf /var/lib/apt/lists/* /tmp/pear

# Moodle's own requirements: max_input_vars is the one people always miss
RUN { \
      echo "memory_limit=512M"; \
      echo "upload_max_filesize=512M"; \
      echo "post_max_size=512M"; \
      echo "max_execution_time=300"; \
      echo "max_input_vars=5000"; \
      echo "opcache.enable=1"; \
      echo "opcache.memory_consumption=256"; \
      echo "opcache.max_accelerated_files=20000"; \
      echo "opcache.revalidate_freq=60"; \
      echo "opcache.save_comments=1"; \
      echo "expose_php=Off"; \
      echo "session.cookie_httponly=On"; \
      echo "session.cookie_samesite=Lax"; \
    } > /usr/local/etc/php/conf.d/zz-moodle.ini

WORKDIR /var/www/html
RUN curl -fsSL "$MOODLE_TGZ" -o /tmp/moodle.tgz \
 && tar -xzf /tmp/moodle.tgz -C /var/www --strip-components=1 --one-top-level=html \
 && rm /tmp/moodle.tgz \
 && mkdir -p /var/www/moodledata \
 && chown -R www-data:www-data /var/www/html /var/www/moodledata

# config.php reads the environment, so the same image runs locally and on a server
COPY php/config.php /var/www/html/config.php
COPY php/entrypoint.sh /usr/local/bin/entrypoint.sh
COPY php/sync-plugins.sh /usr/local/bin/sync-plugins
RUN chmod +x /usr/local/bin/entrypoint.sh /usr/local/bin/sync-plugins

ENTRYPOINT ["entrypoint.sh"]
CMD ["php-fpm"]
