# Use an official PHP runtime as a parent image with Apache
FROM php:8.3.36-apache

# Install system dependencies and PHP extensions

RUN apt-get update && apt-get install -y \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
    libonig-dev \
    libzip-dev \
    zip \
    unzip \
    git \
    curl \
    vim \
    wget \
    && docker-php-ext-install pdo mbstring zip exif pcntl gd bcmath


## Enable mod_rewrite for Apache
RUN a2enmod rewrite

# Hide Apache server version information
RUN echo "ServerTokens Prod" >> /etc/apache2/conf-available/security.conf \
    && echo "ServerSignature Off" >> /etc/apache2/conf-available/security.conf \
    && a2enconf security

# Disable PHP version disclosure
RUN echo "expose_php = Off" > /usr/local/etc/php/conf.d/security.ini

# Secure PHP session cookies
RUN echo "session.cookie_httponly = 1" >> /usr/local/etc/php/conf.d/security.ini \
    && echo "session.cookie_secure = 1" >> /usr/local/etc/php/conf.d/security.ini \
    && echo "session.cookie_samesite = Lax" >> /usr/local/etc/php/conf.d/security.ini

# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Set the working directory
WORKDIR /var/www/html

# Copy only composer.json to install dependencies
COPY composer.json ./

# Install PHP dependencies using Composer
RUN composer install --no-dev --optimize-autoloader --no-interaction --prefer-dist

## RUN composer require ip2location/ip2location-php

RUN wget -O /var/www/html/ipdata.zip https://storage.googleapis.com/case-study-jagran-new-media/ipdata.zip

RUN unzip /var/www/html/ipdata.zip -d /var/www/html/

# Copy the application code
COPY . .

# Set correct permissions for web files
RUN chown -R www-data:www-data /var/www/html
#RUN chmod -R 644 /var/www/html/vendor/ip2location/ip2location-php/src/

# Expose port 80
EXPOSE 80

# Start Apache
CMD ["apache2-foreground"]
