# remirepo spec file for php-ioncube-loader
#
# Copyright (c) 2012-2022 Remi Collet
# License: CC-BY-SA
# http://creativecommons.org/licenses/by-sa/4.0/
#
# Please, preserve the changelog entries
#
%{?scl:          %scl_package             php-ioncube-loader}
%global extname       ioncube_loader
%global debug_package %{nil}
%global __debug_install_post /bin/true
%if 0%{?fedora} >= 27 || 0%{?rhel} >= 8
%global _build_id_links none
%endif
%ifarch armv7hl aarch64 armv7hl
%global with_zts      0
%else
%global with_zts      0%{!?_without_zts:%{?__ztsphp:1}}
%endif
%if "%{php_version}" < "5.6"
%global ininame       %{extname}.ini
%else
# [ionCube Loader] The Loader must appear as the first entry in the php.ini
%global ininame       01-%{extname}.ini
%endif

# Open issues
# http://forum.ioncube.com/viewtopic.php?t=4244 - No version in Reflection

Name:          %{?scl_prefix}php-ioncube-loader
Summary:       Loader for ionCube Encoded Files with ionCube 24 support
%global upstream_version  13.3.1
#global upstream_prever   beta2
Version:       %{upstream_version}%{?upstream_prever:~%{upstream_prever}}
Release:       1
License:       Distribuable

URL:           http://www.ioncube.com
%if 0%{?upstream_prever:1}
Source1:       https://www.ioncube.com/php-7.4.0-beta-loaders/ioncube_loaders_lin_x86-64_7.4_BETA2.tar.gz
%else
Source1:       https://downloads.ioncube.com/loader_downloads/%{extname}s_lin_x86-64_%{upstream_version}.tar.gz
Source2:       https://downloads.ioncube.com/loader_downloads/%{extname}s_lin_aarch64_%{upstream_version}.tar.gz
%endif
Source90:      LICENSE.txt
Source91:      USER-GUIDE.txt

ExclusiveArch: x86_64 aarch64
BuildRequires: %{?scl_prefix}php-devel < 8.0

# ABI check
Requires:      %{?scl_prefix}php(zend-abi) = %{php_zend_api}
Requires:      %{?scl_prefix}php(api) = %{php_core_api}
%{?_sclreq:Requires: %{?scl_prefix}runtime%{?_sclreq}%{?_isa}}

%if "%{?packager}" == "Remi Collet" && 0%{!?scl:1} && 0%{?rhel}
# Other third party repo stuff
Obsoletes:      php53-ioncube-loader <= %{version}
Obsoletes:     php53u-ioncube-loader <= %{version}
Obsoletes:      php54-ioncube-loader <= %{version}
%if "%{php_version}" > "5.5"
Obsoletes:     php55u-ioncube-loader <= %{version}
%endif
%if "%{php_version}" > "5.6"
Obsoletes:     php56u-ioncube-loader <= %{version}
%endif
%if "%{php_version}" > "7.0"
Obsoletes:     php70u-ioncube-loader <= %{version}
%endif
%if "%{php_version}" > "7.1"
Obsoletes:     php71u-ioncube-loader <= %{version}
%endif
%if "%{php_version}" > "7.2"
Obsoletes:     php72u-ioncube-loader <= %{version}
%endif
%if "%{php_version}" > "7.3"
Obsoletes:      php73-ioncube-loader <= %{version}
%endif
%if "%{php_version}" > "7.4"
Obsoletes:      php74-ioncube-loader <= %{version}
%endif
%endif


%description
Loader for ionCube Encoded Files with ionCube 24 support.

Package built for PHP %(%{__php} -n -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')%{?scl: as Software Collection (%{scl} by %{?scl_vendor}%{!?scl_vendor:rh})}.


%prep
%setup -q -T -c

%ifarch aarch64
tar xvf %{SOURCE2}
%else
tar xvf %{SOURCE1}
%endif

%if 0%{?upstream_prever:1}
mkdir ioncube
mv ioncube_loader_lin_7.4_%{upstream_version}_%{upstream_prever}.so    ioncube/ioncube_loader_lin_7.4.so
mv ioncube_loader_lin_7.4_%{upstream_version}_%{upstream_prever}_ts.so ioncube/ioncube_loader_lin_7.4_ts.so
%endif

[ -d linux_x86_64 ] && ln -s linux_x86_64 ioncube

# Drop in the bit of configuration
# Sometime file is missing
# http://forum.ioncube.com/viewtopic.php?t=4245
[ -f ioncube/LICENSE.txt ]    || cp %{SOURCE90} ioncube/
[ -f ioncube/USER-GUIDE.txt ] || cp %{SOURCE91} ioncube/
sed -e 's/\r//' -i ioncube/*.txt

cat << 'EOF' | tee %{extname}.nts
; Enable %{extname} extension module
zend_extension = %{extname}.so

; ionCube PHP Loader + Intrusion Protection from ioncube24.com configuration
;ic24.enable = 0
;ic24.sec.stop_on_error = 1
;ic24.sec.trusted_include_paths = ''
;ic24.sec.block_uploaded_files = 1
;ic24.api_access_key = ''
;ic24.api_check_ip = 1
;ic24.slt = '7'
;ic24.sec.enable = 'auto'
;ic24.sec.initial_state = 1
;ic24.sec.initial_action = 'block'
;ic24.sec.initial_notify = 'always'
;ic24.sec.initial_cache_all = 1
;ic24.sec.exclusion_key = ''
;ic24.home_dir = ''
;ic24.sec.block_stdin = '1'
;ic24.update_domains_retry_interval = '30'
;ic24.phperr.enable = 'auto'
;ic24.phperr.ignore = 0
;ic24.api.log_msg_errors = 0
;ic24.api.max_timeout = 7
;ioncube.loader.encoded_paths = ''
EOF

cp %{extname}.nts %{extname}.zts

%if "%{php_version}" < "5.5"
sed -e 's:%{extname}.so:%{php_ztsextdir}/%{extname}.so:' \
    -i %{extname}.zts
sed -e 's:%{extname}.so:%{php_extdir}/%{extname}.so:' \
    -i %{extname}.nts
diff %{extname}.nts %{extname}.zts || : ok
%endif


%build
# tarball provides binaries


%install
ver=$(%{__php} -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')

if [ -f ioncube/%{extname}_lin_${ver}.so ]; then

  install -D -pm 755 ioncube/%{extname}_lin_${ver}.so    %{buildroot}%{php_extdir}/%{extname}.so
  %if %{with_zts}
  install -D -pm 755 ioncube/%{extname}_lin_${ver}_ts.so %{buildroot}%{php_ztsextdir}/%{extname}.so
  %endif

elif [ -f linux_x86_64/${ver}.0/ioncube_loader_%{version}.so ]; then

  install -D -pm 755 linux_x86_64/${ver}.0/ioncube_loader_%{version}.so       %{buildroot}%{php_extdir}/%{extname}.so
  %if %{with_zts}
  install -D -pm 755 linux_x86_64/${ver}.0-ts/ioncube_loader_%{version}_ts.so %{buildroot}%{php_ztsextdir}/%{extname}.so
  %endif

else
  : Module for PHP $ver not provided
  exit 1
fi

install -D -m 644  %{extname}.nts %{buildroot}%{php_inidir}/%{ininame}
%if %{with_zts}
install -D -m 644  %{extname}.zts %{buildroot}%{php_ztsinidir}/%{ininame}
%endif


%check
# simple module load test
%{__php} --no-php-ini \
    --define zend_extension=%{buildroot}%{php_extdir}/%{extname}.so \
    --version | grep 'ionCube.*%{upstream_version}'

%if %{with_zts}
%{__ztsphp} --no-php-ini \
    --define zend_extension=%{buildroot}%{php_ztsextdir}/%{extname}.so \
    --version | grep 'ionCube.*%{upstream_version}'
%endif


%files
%{!?_licensedir:%global license %%doc}
%license ioncube/LICENSE.txt
%doc ioncube/USER-GUIDE.txt

%config(noreplace) %{php_inidir}/%{ininame}
%{php_extdir}/%{extname}.so

%if %{with_zts}
%config(noreplace) %{php_ztsinidir}/%{ininame}
%{php_ztsextdir}/%{extname}.so
%endif