FROM php:8.5.4-fpm-alpine AS builder

ARG http_proxy

ENV http_proxy=${http_proxy} \
    https_proxy=${http_proxy} \
    HTTP_PROXY=${http_proxy} \
    HTTPS_PROXY=${http_proxy}

# Installation des packages nécessaires à la compilation des extensions php.
RUN apk update && apk upgrade --no-cache && apk add --no-cache \
    freetype-dev jpeg-dev libpng-dev libwebp-dev libxpm-dev \
    zlib-dev icu-dev libxslt-dev libzip-dev \
    imagemagick-dev \
    libmemcached-dev cyrus-sasl-dev \
    oniguruma-dev gettext-dev gmp-dev \
    linux-headers \
    autoconf libtool pkgconfig alpine-sdk

# Installation des extensions php natives.
RUN docker-php-ext-configure gd --enable-gd --with-webp --with-jpeg --with-xpm --with-freetype \
  && docker-php-ext-configure zip --with-zip \
  && docker-php-ext-install -j"$(nproc)" \
    exif \
    gd \
    gettext \
    intl \
    mysqli \
    pdo_mysql \
    xsl \
    zip \
    bcmath \
    gmp

# PECL maintenant déprécié, on installe directement les extensions souhaitées depuis github.
RUN \
    curl -fsSL https://github.com/Imagick/imagick/archive/refs/tags/3.8.1.tar.gz -o /tmp/imagick.tar.gz \
    && tar -xzf /tmp/imagick.tar.gz -C /tmp \
    && cd /tmp/imagick-3.8.1 \
    && phpize && ./configure && make -j"$(nproc)" && make install \
    && rm -rf /tmp/imagick* \
    \
    && curl -fsSL https://github.com/php-memcached-dev/php-memcached/archive/refs/tags/v3.4.0.tar.gz -o /tmp/memcached.tar.gz \
    && tar -xzf /tmp/memcached.tar.gz -C /tmp \
    && cd /tmp/php-memcached-3.4.0 \
    && phpize && ./configure && make -j"$(nproc)" && make install \
    && rm -rf /tmp/memcached* \
    \
    && curl -fsSL https://github.com/phpredis/phpredis/archive/refs/tags/6.3.0.tar.gz -o /tmp/redis.tar.gz \
    && tar -xzf /tmp/redis.tar.gz -C /tmp \
    && cd /tmp/phpredis-6.3.0 \
    && phpize && ./configure && make -j"$(nproc)" && make install \
    && rm -rf /tmp/redis*

FROM php:8.5.4-fpm-alpine AS base

ARG http_proxy

ENV http_proxy=${http_proxy} \
    https_proxy=${http_proxy} \
    HTTP_PROXY=${http_proxy} \
    HTTPS_PROXY=${http_proxy} \
    LC_ALL=fr_FR.UTF-8 \
    LANG=fr_FR.UTF-8 \
    LANGUAGE=fr_FR.UTF-8:fr

# Installation des packages runtime
RUN apk update && apk upgrade --no-cache && apk add --no-cache \
    bash \
    ca-certificates curl \
    unzip zip vim \
    apache2 apache2-proxy apache2-brotli \
    freetype libjpeg libpng libwebp libxpm \
    icu-libs libxslt libzip \
    imagemagick \
    libcurl libmemcached \
    musl-locales musl-locales-lang \
    oniguruma \
    gmp gettext-libs \
    libcap

# Copie des extensions compilées depuis le stage builder.
COPY --from=builder /usr/local/lib/php/extensions/ /usr/local/lib/php/extensions/
COPY --from=builder /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/

# Active les extensions PECL copiées.
RUN docker-php-ext-enable imagick memcached redis

# Copie des fichiers de configuration.
COPY conf/php-fpm.conf         /usr/local/etc/php-fpm.d/www.conf
COPY conf/php.ini              /usr/local/etc/php/conf.d/app.ini
COPY conf/vhost.conf           /etc/apache2/conf.d/vhost.conf
COPY conf/common-apache.conf   /etc/apache2/conf.d/z-app.conf
COPY conf/common-security.conf /etc/apache2/conf.d/security.conf
COPY --chmod=755 conf/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh

# Configuration des modules apache sans duplication. (par défaut dans alpine c'est commenté, on décommente juste ce dont on a besoin)
RUN sed -i \
    -e 's/#LoadModule proxy_module/LoadModule proxy_module/' \
    -e 's/#LoadModule proxy_fcgi_module/LoadModule proxy_fcgi_module/' \
    -e 's/#LoadModule rewrite_module/LoadModule rewrite_module/' \
    -e 's/#LoadModule remoteip_module/LoadModule remoteip_module/' \
    -e 's/#LoadModule headers_module/LoadModule headers_module/' \
    -e 's/#LoadModule expires_module/LoadModule expires_module/' \
    -e 's/#LoadModule deflate_module/LoadModule deflate_module/' \
    -e 's/#LoadModule mime_module/LoadModule mime_module/' \
    -e 's/#LoadModule mpm_event_module/LoadModule mpm_event_module/' \
    -e 's/LoadModule mpm_prefork_module/#LoadModule mpm_prefork_module/' \
    -e 's/^[[:space:]]*#*[[:space:]]*LoadModule[[:space:]]\+ssl_module/#LoadModule ssl_module/' \
    -e 's/^[[:space:]]*Listen[[:space:]]\+443/#Listen 443/' \
    /etc/apache2/httpd.conf \
    && printf '\nPidFile /tmp/httpd.pid\n' >> /etc/apache2/httpd.conf \
    && if [ -f /etc/apache2/conf.d/ssl.conf ]; then \
         mv /etc/apache2/conf.d/ssl.conf \
            /etc/apache2/conf.d/ssl.conf.disabled; \
       fi \
    && find /etc/apache2 -type f \( -name '*.conf' -o -name 'httpd.conf' \) \
        -exec sed -i \
            -e 's!^[[:space:]]*CustomLog[[:space:]].*!CustomLog /proc/self/fd/1 combined!' \
            -e 's!^[[:space:]]*TransferLog[[:space:]].*!CustomLog /proc/self/fd/1 combined!' \
            -e 's!^[[:space:]]*ErrorLog[[:space:]].*!ErrorLog /proc/self/fd/2!' \
            -e 's/^[[:space:]]*#*[[:space:]]*LoadModule[[:space:]]\+lbmethod_heartbeat_module/#LoadModule lbmethod_heartbeat_module/' \
            -e 's/^[[:space:]]*#*[[:space:]]*LoadModule[[:space:]]\+lbmethod_/#&/' \
            -e 's/^[[:space:]]*#*[[:space:]]*LoadModule[[:space:]]\+proxy_balancer_module/#&/' \
            {} \; \
    && httpd -t

# Création des répertoires avec les bons droits.
RUN mkdir -p /app/tmp /app/tmp/sessions /app/logs /app/srcs \
    && chown -R www-data:www-data /app \
    && chmod 770 /app/tmp /app/logs \
    && setcap 'cap_net_bind_service=+ep' /usr/sbin/httpd

USER www-data

# Définit le répertoire de travail par défaut dans le conteneur.
WORKDIR /app/srcs

# Entrypoint custom pour démarrer php-fpm et apache (en executable seule).
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]