FROM php:5.6-apache

# Backup and clean source.list file
RUN cp /etc/apt/sources.list /etc/apt/sources.list.old && \
    cat /dev/null > /etc/apt/sources.list

# Fix the source.list for stretch
RUN printf "deb http://archive.debian.org/debian/ stretch main\n" > /etc/apt/sources.list && \
    printf "deb-src http://archive.debian.org/debian/ stretch main\n" >> /etc/apt/sources.list && \
    printf "deb http://archive.debian.org/debian-security stretch/updates main\n" >> /etc/apt/sources.list && \
    printf "deb-src http://archive.debian.org/debian-security stretch/updates main" >> /etc/apt/sources.list

RUN apt-get -y update && apt-get upgrade -y

# Install tools && libraries
RUN apt-get -y install --fix-missing nano wget dialog \
    build-essential git curl libcurl3 libcurl3-dev zip \
    libmcrypt-dev libsqlite3-dev libsqlite3-0 mysql-client \
    zlib1g-dev libicu-dev libfreetype6-dev libjpeg62-turbo-dev libpng-dev \
    unixodbc unixodbc-dev libodbc1 odbcinst odbcinst1debian2 \
    freetds-bin freetds-dev tdsodbc \
    && rm -rf /var/lib/apt/lists/*

# Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Configure FreeTDS
RUN echo "[mssql]\n\
host = your_sql_server_host\n\
port = 1433\n\
tds version = 7.4\n" >> /etc/freetds/freetds.conf

# Register FreeTDS driver with ODBC
RUN echo "[FreeTDS]\n\
Description = FreeTDS Driver for MSSQL\n\
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so\n\
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so\n\
UsageCount = 1\n" >> /etc/odbcinst.ini

# Download PHP 5.6 source, patch ODBC extension, and build it with proper linking
RUN apt-get update && apt-get install -y wget && \
    mkdir -p /usr/src/php && \
    cd /usr/src/php && \
    wget -q https://www.php.net/distributions/php-5.6.40.tar.gz && \
    tar -xzf php-5.6.40.tar.gz && \
    rm php-5.6.40.tar.gz && \
    mkdir -p /usr/local/incl && \
    ln -s /usr/include/sql.h /usr/local/incl/sql.h && \
    ln -s /usr/include/sqlext.h /usr/local/incl/sqlext.h && \
    ln -s /usr/include/sqltypes.h /usr/local/incl/sqltypes.h && \
    ln -s /usr/include/odbcinst.h /usr/local/incl/odbcinst.h && \
    cd /usr/src/php/php-5.6.40/ext/odbc && \
    sed -i 's/#include <WINDOWS.H>/\/* #include <WINDOWS.H> *\//' php_odbc_includes.h && \
    phpize && \
    ./configure --with-unixODBC=/usr --with-odbcver=0x0350 LDFLAGS="-L/usr/lib/x86_64-linux-gnu -lodbc" && \
    make && \
    make install && \
    echo "extension=odbc.so" > /usr/local/etc/php/conf.d/odbc.ini && \
    rm -rf /usr/src/php/php-5.6.40

# PHP5 Extensions (without ODBC, since it's handled above)
RUN docker-php-ext-install curl \
    && docker-php-ext-install tokenizer \
    && docker-php-ext-install json \
    && docker-php-ext-install bcmath \
    && docker-php-ext-install mcrypt \
    && docker-php-ext-install pdo_mysql \
    && docker-php-ext-install pdo_sqlite \
    && docker-php-ext-install mysqli \
    && docker-php-ext-install zip \
    && docker-php-ext-install -j$(nproc) intl \
    && docker-php-ext-install mbstring \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \
    && pecl install xdebug-2.5.5 && docker-php-ext-enable xdebug \
    && echo "xdebug.remote_enable=1" >> /usr/local/etc/php/php.ini

# Ensure an SSL directory exists
RUN mkdir -p /etc/apache2/ssl

# Enable SSL support and Apache modules
RUN a2enmod ssl && a2enmod rewrite && a2enmod headers

# Set ServerName to suppress Apache FQDN warning
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf

# Add a default index.php to test the server
RUN echo "<?php phpinfo();" > /var/www/html/index.php

EXPOSE 80
EXPOSE 443

ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]