# Phorge, built from the official PHP image.
#
# Based on the Dockerfile of the former ghcr.io/datenknoten/phorge image
# (github.com/phorge-docker/phorge, no longer available).
#
# To upgrade, set PHORGE_REF and ARCANIST_REF to the current heads of the
# stable branches:
#
#   git ls-remote https://we.phorge.it/source/phorge.git refs/heads/stable
#   git ls-remote https://we.phorge.it/source/arcanist.git refs/heads/stable

FROM php:8.4-apache-trixie

# Phorge 2026.27 (stable)
ARG PHORGE_REF=7d2dd8899c3b31bfd7a61d8e0de94b9b9a675615
ARG ARCANIST_REF=34b8b54e0a01a2622acea78b0b9ca82b45141f98

RUN apt-get update \
  && apt-get install -y --no-install-recommends \
    git \
    mercurial \
    subversion \
    ca-certificates \
    python3-pygments \
    imagemagick \
    openssh-client \
    openssh-server \
    procps \
    sudo \
  && rm -rf /var/lib/apt/lists/*

RUN set -ex; \
    a2enmod rewrite; \
    savedAptMark="$(apt-mark showmanual)"; \
    apt-get update; \
    apt-get install -y --no-install-recommends \
      libonig-dev \
      libcurl4-openssl-dev \
      libjpeg62-turbo-dev \
      libpng-dev \
      libfreetype-dev \
      libzip-dev \
    ; \
    docker-php-ext-configure gd --with-jpeg --with-freetype; \
    docker-php-ext-install -j "$(nproc)" \
      gd \
      opcache \
      mbstring \
      iconv \
      mysqli \
      curl \
      pcntl \
      zip \
    ; \
    pecl install apcu; \
    docker-php-ext-enable apcu; \
    apt-mark auto '.*' > /dev/null; \
    apt-mark manual $savedAptMark; \
    ldd "$(php -r 'echo ini_get("extension_dir");')"/*.so \
      | awk '/=>/ { print $3 }' \
      | sort -u \
      | xargs -r realpath \
      | xargs -r dpkg-query -S \
      | cut -d: -f1 \
      | sort -u \
      | xargs -rt apt-mark manual; \
    apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
    rm -rf /var/lib/apt/lists/* /tmp/pear

RUN { \
      echo 'opcache.memory_consumption=128'; \
      echo 'opcache.interned_strings_buffer=8'; \
      echo 'opcache.max_accelerated_files=4000'; \
      echo 'opcache.revalidate_freq=60'; \
      echo 'opcache.validate_timestamps=0'; \
    } > /usr/local/etc/php/conf.d/opcache-recommended.ini \
  && echo 'date.timezone="UTC"' > /usr/local/etc/php/conf.d/timezone.ini \
  && { \
      echo 'post_max_size=32M'; \
      echo 'upload_max_filesize=32M'; \
    } > /usr/local/etc/php/conf.d/uploads.ini

RUN mkdir /var/repo && chown www-data:www-data /var/repo

RUN git clone https://we.phorge.it/source/arcanist.git /opt/arcanist \
  && git -C /opt/arcanist checkout --detach "$ARCANIST_REF" \
  && git clone https://we.phorge.it/source/phorge.git /opt/phorge \
  && git -C /opt/phorge checkout --detach "$PHORGE_REF"

WORKDIR /opt/phorge

RUN rmdir /var/www/html \
  && ln -sf /opt/phorge/webroot /var/www/html \
  && { \
      echo '<VirtualHost *:80>'; \
      echo '  RewriteEngine on'; \
      echo '  RewriteRule ^(.*)$ /index.php?__path__=$1 [B,L,QSA]'; \
      echo '</VirtualHost>'; \
    } > /etc/apache2/sites-available/000-default.conf

COPY sudoers /etc/sudoers
ENV PATH="$PATH:/opt/phorge/bin:/var/www/phorge/bin"

RUN ln -s phorge /var/lib/phabricator

RUN ln -s /opt/phorge /var/www/phorge
RUN ln -s phorge /var/www/phabricator
