FROM php:5.6-apache

# Set noninteractive installation
ENV DEBIAN_FRONTEND=noninteractive

# Configure Debian Jessie repositories properly
RUN echo "deb http://archive.debian.org/debian/ jessie main contrib non-free" > /etc/apt/sources.list \
    && echo "deb http://archive.debian.org/debian-security/ jessie/updates main contrib non-free" >> /etc/apt/sources.list \
    && echo 'Acquire::Check-Valid-Until "false";' > /etc/apt/apt.conf.d/10no-check-valid-until \
    && echo 'APT::Get::AllowUnauthenticated "true";' >> /etc/apt/apt.conf.d/10no-check-valid-until \
    && echo 'Acquire::AllowInsecureRepositories "true";' >> /etc/apt/apt.conf.d/10no-check-valid-until \
    && echo 'APT::Get::Allow-Downgrades "true";' >> /etc/apt/apt.conf.d/10no-check-valid-until

# Install base dependencies
RUN apt-get clean \
    && apt-get update \
    && apt-get install -y --no-install-recommends --allow-unauthenticated \
        apt-transport-https \
        ca-certificates \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Install zlib and other dependencies
RUN apt-get update \
    && apt-get install -y --no-install-recommends --allow-unauthenticated \
        zlib1g=1:1.2.8.dfsg-2+deb8u1 \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get update \
    && apt-get install -y --no-install-recommends --allow-unauthenticated \
        zlib1g-dev=1:1.2.8.dfsg-2+deb8u1 \
        libxml2=2.9.1+dfsg1-5+deb8u8 \
        libxml2-dev=2.9.1+dfsg1-5+deb8u8 \
        libpng12-0=1.2.50-2+deb8u3 \
        libpng12-dev=1.2.50-2+deb8u3 \
        libjpeg62-turbo=1:1.3.1-12 \
        libjpeg62-turbo-dev=1:1.3.1-12 \
        libmcrypt4=2.5.8-3.3 \
        libmcrypt-dev=2.5.8-3.3 \
        git \
        curl \
        zip \
        unzip \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Configure and install PHP extensions
RUN docker-php-ext-configure gd --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) \
        pdo_mysql \
        mysql \
        mysqli \
        mbstring \
        mcrypt \
        xml \
        gd \
        zip \
        bcmath

# Enable Apache modules
RUN a2enmod rewrite

# Set working directory
WORKDIR /var/www/html

# Configure Apache DocumentRoot
RUN sed -i 's!/var/www/html!/var/www/html/public!g' /etc/apache2/sites-available/000-default.conf

# Set recommended PHP.ini settings
RUN { \
        echo 'memory_limit = 256M'; \
        echo 'max_execution_time = 300'; \
        echo 'upload_max_filesize = 64M'; \
        echo 'post_max_size = 64M'; \
        echo 'max_input_vars = 3000'; \
    } > /usr/local/etc/php/conf.d/custom.ini

# Expose port 80
EXPOSE 80