ARG PHP_VERSION
FROM ${PHP_VERSION}

ARG TZ
ARG PHP_EXTENSIONS
ARG CONTAINER_PACKAGE_URL

RUN echo "${TZ}"


# 设置时区
ENV TZ=${TZ}
ENV DEBIAN_FRONTEND=noninteractive

# 替换源列表，更新软件包索引
RUN echo "deb http://archive.debian.org/debian/ stretch main contrib non-free" > /etc/apt/sources.list && \
    echo "deb-src http://archive.debian.org/debian/ stretch main contrib non-free" >> /etc/apt/sources.list && \
    echo "deb http://archive.debian.org/debian-security/ stretch/updates main contrib non-free" >> /etc/apt/sources.list && \
    echo "deb-src http://archive.debian.org/debian-security/ stretch/updates main contrib non-free" >> /etc/apt/sources.list && \
    echo "deb http://archive.debian.org/debian/ stretch-backports main contrib non-free" >> /etc/apt/sources.list && \
    apt-get update

RUN apt-get install -y openssl --allow-unauthenticated --no-install-recommends libyaml-dev

# 安装 SSL 库（多个版本确保兼容）
RUN apt-get install -y \
    openssl \
    libssl1.0-dev \
    libssl1.0.0 \
    --allow-unauthenticated \
    --no-install-recommends
    
# 创建 SSL 库的符号链接
RUN ln -sf /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib/libssl.so && \
    ln -sf /usr/lib/x86_64-linux-gnu/libcrypto.so /usr/lib/libcrypto.so

# 安装必要的软件包并更新 CA 证书
RUN apt-get install -y \
    openssl \
    ca-certificates \
    curl \
    wget \
    gnupg \
    --allow-unauthenticated \
    --no-install-recommends \
    libyaml-dev && \
    update-ca-certificates --fresh && \
    rm -rf /var/lib/apt/lists/*

# 设置时区
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
    echo $TZ > /etc/timezone

COPY ./docker-php-ext-enable /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-php-ext-enable

COPY ./extensions /tmp/extensions
WORKDIR /tmp/extensions
RUN chmod +x install.sh \
    && sh install.sh \
    && rm -fr /tmp/extensions

# 配置 Composer 使用系统 CA 证书
RUN mkdir -p /var/www/.config/composer && \
    mkdir -p /var/www/.cache/composer && \
    chmod -R 777 /var/www/.config /var/www/.cache

# 设置 Composer 环境变量
ENV COMPOSER_ALLOW_SUPERUSER=1
ENV COMPOSER_HOME=/var/www/.config/composer
ENV COMPOSER_CACHE_DIR=/var/www/.cache/composer

# php image's www-data user uid & gid are 82, change them to 1000 (primary user)
RUN usermod -u 1000 www-data && groupmod -g 1000 www-data
# Use www-data to run the first process
USER "www-data"

WORKDIR /www
