#!/bin/bash

#if [[ `cat /etc/fstab | grep -E "tmp.*noexec"` != "" ]]; then mount -o remount,exec /tmp >/dev/null 2>&1 ; fi

arch=$(uname -m)
pubip=`curl -s http://centos-webpanel.com/webpanel/main.php?app=showip`
# CONFIGURE MIRRORS HERE
version=2.4.62
apachesource="https://archive.apache.org/dist/httpd/httpd-$version.tar.gz"

# Dependency installer
yum -y install openssl-devel apr-util-devel apr-devel apr apr-util
if [ -f /usr/local/apache/conf/httpd.conf ]; then cp /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf-cwpsave ; fi
test -h /usr/local/src/apache-build || rm -rf /usr/local/src/apache-build
mkdir -p /usr/local/src/apache-build

cd /usr/local/src/apache-build
wget -q $apachesource
tar -xf httpd-$version.tar.gz
chown -R root:root httpd-$version/
cd httpd-$version/

# Install Patch - this are patches for version 2.4.26+
wget -q "http://dl1.centos-webpanel.com/files/apache/patch/suexec.patch"
/usr/bin/patch -p1 < suexec.patch

#rm -f /usr/local/apache/conf/httpd.conf
./configure --enable-so --prefix=/usr/local/apache --enable-unique-id --enable-ssl --enable-rewrite --enable-deflate --enable-suexec --with-suexec-docroot=/home --with-suexec-caller=nobody --with-suexec-logfile=/usr/local/apache/logs/suexec_log --enable-asis --enable-filter --with-pcre --with-apr=/usr/bin/apr-1-config  --with-apr-util=/usr/bin/apu-1-config  --enable-headers --enable-expires --enable-proxy --enable-rewrite --enable-userdir --enable-http2 

configurecheck=$?
if [ $configurecheck -eq 0 ];then
        rm -f /usr/local/apache/conf/httpd.conf
fi

make clean

if [ -e "/usr/bin/nproc" ];then
	make -j `/usr/bin/nproc`
else
	make
fi
make install

mkdir /usr/local/apache/conf.d
touch /usr/local/apache/conf.d/empty.conf
# Check if lines below already added and add those if wasn't added before.
if [[ "`grep ExtendedStatus /usr/local/apache/conf/httpd.conf 2>/dev/null`" == "" ]]; then
echo "ExtendedStatus On" >> /usr/local/apache/conf/httpd.conf
echo "Include /usr/local/apache/conf/sharedip.conf" >> /usr/local/apache/conf/httpd.conf
echo "Include /usr/local/apache/conf.d/*.conf" >> /usr/local/apache/conf/httpd.conf
fi
# Check if installed nginx/varnish.
if [[ "`grep ':8181' /usr/local/apache/conf.d/vhosts.conf 2> /dev/null`" != "" ]]; then
        sed -i "s/^.*Listen 80$/Listen 8181/" /usr/local/apache/conf/httpd.conf
        sed -i "s/:80/:8181/" /usr/local/apache/conf/sharedip.conf
fi
sed -i "s|DirectoryIndex index.html|DirectoryIndex index.php index.html index.htm|g" /usr/local/apache/conf/httpd.conf
sed -i "0,/^\([[:blank:]]*\)DirectoryIndex.*$/ s//\1 DirectoryIndex index.html.var index.htm index.html index.shtml index.xhtml index.wml index.perl index.pl index.plx index.ppl index.cgi index.jsp index.js index.jp index.php4 index.php3 index.php index.phtml default.htm default.html home.htm index.php5 Default.html Default.htm home.html/" /usr/local/apache/conf/httpd.conf


cat > /usr/local/apache/conf/sharedip.conf <<EOF
<VirtualHost $pubip:80>
    ServerName $pubip
    DocumentRoot /usr/local/apache/htdocs
    ServerAdmin info@centos-webpanel.com
    <IfModule mod_suphp.c>
        suPHP_UserGroup nobody nobody
    </IfModule>
    <Proxy "*">
        <IfModule mod_security2.c>
            SecRuleEngine Off
        </IfModule>
    </Proxy>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^$pubip\$
    RewriteCond %{HTTP_HOST} ^webmail.
    RewriteRule ^/(.*) http://%{HTTP_HOST}:2095/\$1 [P]
</VirtualHost>
<Directory "/">
        AllowOverride All
        Require all granted
</Directory>
EOF

cat > /usr/local/apache/conf.d/http2.conf << EOF
LoadModule http2_module modules/mod_http2.so
LogLevel http2:info
Protocols h2 h2c http/1.1
EOF

sed -i "s|User daemon|User nobody|g" /usr/local/apache/conf/httpd.conf
sed -i "s|Group daemon|Group nobody|g" /usr/local/apache/conf/httpd.conf
sed -i "s|.*modules/libphp5.so.*||g" /usr/local/apache/conf/httpd.conf
sed -i "s|.*httpd-userdir.conf.*|Include conf/extra/httpd-userdir.conf|" /usr/local/apache/conf/httpd.conf
sed -i "s|#LoadModule userdir_module modules.*$|LoadModule userdir_module modules/mod_userdir.so|" /usr/local/apache/conf/httpd.conf
sed -i "s|#LoadModule unique_id_module modules.*$|LoadModule unique_id_module modules/mod_unique_id.so|" /usr/local/apache/conf/httpd.conf
sed -i "s|#LoadModule rewrite_module modules.*$|LoadModule rewrite_module modules/mod_rewrite.so|" /usr/local/apache/conf/httpd.conf
sed -i "s|#LoadModule proxy_module modules.*$|LoadModule proxy_module modules/mod_proxy.so|" /usr/local/apache/conf/httpd.conf
sed -i "s|#LoadModule proxy_connect_module modules.*$|LoadModule proxy_connect_module modules/mod_proxy_connect.so|" /usr/local/apache/conf/httpd.conf
sed -i "s|#LoadModule proxy_http_module modules.*$|LoadModule proxy_http_module modules/mod_proxy_http.so|" /usr/local/apache/conf/httpd.conf

mkdir -p /usr/local/apache/conf.d

cd /usr/local/apache/bin/
ldconfig

#ADDITIONAL_CONFIGURATION
if [ ! -e "/usr/local/apache/conf.d/domain-redirects.conf" ];then
cat > /usr/local/apache/conf.d/domain-redirects.conf <<EOF
RewriteEngine on
Alias /myadmin /usr/local/apache/htdocs/phpMyAdmin
Alias /MyAdmin /usr/local/apache/htdocs/phpMyAdmin
Alias /phpmyadmin /usr/local/apache/htdocs/phpMyAdmin
Alias /phpMyAdmin /usr/local/apache/htdocs/phpMyAdmin
Alias /webmail /usr/local/apache/htdocs/roundcube
Alias /WebMail /usr/local/apache/htdocs/roundcube
Alias /roundcube /usr/local/apache/htdocs/roundcube
Alias /webftp /usr/local/apache/htdocs/webftp_simple
Alias /WebFTP /usr/local/apache/htdocs/webftp_simple
Alias /webftp_simple /usr/local/apache/htdocs/webftp_simple
EOF
fi

cat > /usr/local/apache/conf.d/system-redirects.conf <<EOF
Redirect permanent /myadmin http://$pubip:2030/pma
Redirect permanent /MyAdmin http://$pubip:2030/pma
Redirect permanent /phpmyadmin http://$pubip:2030/pma
Redirect permanent /phpMyAdmin http://$pubip:2030/pma
Redirect permanent /kpanel http://$pubip:2030/
Redirect permanent /cwp http://$pubip:2082/
Redirect permanent /isp http://$pubip:2030/
Redirect permanent /cpanel http://$pubip:2082/
Redirect permanent /scwp https://$pubip:2083/
Redirect permanent /controlpanel http://$pubip:2082/
Redirect permanent /securecontrolpanel https://$pubip:2083/
Redirect permanent /securewhm https://$pubip:2031/
Redirect permanent /whm http://$pubip:2030/
EOF

if [[ ! -z "`grep ':8181' /usr/local/apache/conf.d/vhosts/*.conf 2> /dev/null`" ]]; then
        sed -i "s/^.*Listen 80$/Listen 8181/" /usr/local/apache/conf/httpd.conf
        sed -i "s/:80/:8181/" /usr/local/apache/conf/sharedip.conf
fi

if [[ ! -e "/usr/local/apache/conf.d/vhosts/" ]]; then
        mkdir /usr/local/apache/conf.d/vhosts/
fi

if [[ ! -e "/usr/local/apache/domlogs/" ]]; then
        mkdir /usr/local/apache/domlogs/
fi

if [ ! -e "/usr/local/apache/conf.d/vhosts.conf" ];then
       cat > /usr/local/apache/conf.d/vhosts.conf <<EOF
IncludeOptional /usr/local/apache/conf.d/vhosts/*.conf
EOF
fi

if [[ -z `grep "LogFormat.*\"%b\".*bytes" /usr/local/apache/conf/httpd.conf` ]];then
        sed -i '0,/    LogFormat/s//    LogFormat \"%b\" bytes\n&/' /usr/local/apache/conf/httpd.conf
        sed -i "s/LogFormat \"%h/LogFormat \"%a/g" /usr/local/apache/conf/httpd.conf
fi

if [[ ! -z `grep "you@example.com" /usr/local/apache/conf/httpd.conf` ]];then
        sed -i 's/you@example.com/root@localhost/g' /usr/local/apache/conf/httpd.conf
fi

#cat > /usr/local/apache/conf.d/ssl.conf <<EOF
#<IfModule !ssl_module>
#LoadModule ssl_module modules/mod_ssl.so
#</IfModule>
#Listen 443
#SSLProtocol -All +TLSv1.2 +TLSv1.3 
#SSLHonorCipherOrder on
#SSLCipherSuite EECDH+ECDSA+AESGCM:EECDH+aRSA+AESGCM:EECDH+ECDSA+SHA384:EECDH+ECDSA+SHA256:EECDH+aRSA+SHA384:EECDH+aRSA+SHA256:EECDH+aRSA!RC4:EECDH:!RC4:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS
#EOF
sed -i 's/All -SSLv2 -SSLv3/-All +TLSv1.2 +TLSv1.3 /g' /usr/local/apache/conf.d/ssl.conf 

#touch /etc/ld.so.conf.d/opensslso.conf
#grep "/usr/local/opensslso/lib" /etc/ld.so.conf || echo "/usr/local/opensslso/lib" >> /etc/ld.so.conf.d/opensslso.conf
#ldconfig

sed -i "s|#LoadModule userdir_module modules.*$|LoadModule userdir_module modules/mod_userdir.so|" /usr/local/apache/conf/httpd.conf
sed -i "s|#LoadModule unique_id_module modules.*$|LoadModule unique_id_module modules/mod_unique_id.so|" /usr/local/apache/conf/httpd.conf
sed -i "s|#LoadModule rewrite_module modules.*$|LoadModule rewrite_module modules/mod_rewrite.so|" /usr/local/apache/conf/httpd.conf
sed -i "s|#LoadModule proxy_module modules.*$|LoadModule proxy_module modules/mod_proxy.so|" /usr/local/apache/conf/httpd.conf
sed -i "s|#LoadModule proxy_connect_module modules.*$|LoadModule proxy_connect_module modules/mod_proxy_connect.so|" /usr/local/apache/conf/httpd.conf
sed -i "s|#LoadModule proxy_http_module modules.*$|LoadModule proxy_http_module modules/mod_proxy_http.so|" /usr/local/apache/conf/httpd.conf
sed -i "s|#LoadModule deflate_module modules.*$|LoadModule deflate_module modules/mod_deflate.so|" /usr/local/apache/conf/httpd.conf
sed -i "s|#LoadModule expires_module modules.*$|LoadModule expires_module modules/mod_expires.so|" /usr/local/apache/conf/httpd.conf

service httpd restart
echo "Apache Rebuild Completed"
echo
echo
#if [[ `cat /etc/fstab | grep -E "tmp.*noexec"` != "" ]]; then mount -o remount /tmp >/dev/null 2>&1 ; fi

# Add alert info into cwp
#sh /scripts/add_alert alert-info "Apache Re-Build task completed, please check the log for more details." /var/log/apache-rebuild.log
/usr/local/cwp/php71/bin/php /usr/local/cwpsrv/htdocs/resources/admin/include/libs/notifications/cli.php --level="info" --subject="Apache Re-Build INFO" --message="Apache Re-Build task completed, please check the log for more details. Click <a title='Apache Re-Build task LOG' href='index.php?module=file_editor&file=/var/log/apache-rebuild.log'>here</a> to check it."

test -h /usr/local/src/apache-build || rm -Rf /usr/local/src/apache-build
rm -rf /usr/local/src/apache-rebuild.sh
