#!/bin/bash
clear
echo "#################################################################"
echo "#             CentOS Web Panel (CWP)   UPDATER                  #"
echo "#################################################################"
echo ""
echo "visit www.centos-webpanel.com"
echo "For Support and informations visit our website."
echo 
echo "Please wait this can take few minutes..."


if [ ! -e "/usr/local/cwpsrv/bin" ]
then
echo "CentOS Web Panel is not installed!!"
echo "Get CWP installer on: www.centos-webpanel.com"
exit 0
fi

# Upgrade to php 7.2
PHPMAJORVER=`/usr/local/cwp/php71/bin/php -i 2> /dev/null|grep '^PHP Version' |head -n1|awk {'print $4'}|cut -c 1,3`

if [ "$PHPMAJORVER" -eq "70" ];then
	PHPIONCUBEPHPINI=`grep ioncube_loader_lin_7.0.so /usr/local/cwp/php71/php.ini`
	if [ ! -z "$PHPIONCUBEPHPINI" ];then

		if [ ! -e "/usr/local/ioncube/ioncube_loader_lin_7.2.so" ];then
			sh /scripts/update_ioncube
		fi

		if [ -e "/usr/local/ioncube/ioncube_loader_lin_7.2.so" ];then
			yum -y update cwpphp --enablerepo=cwp
			#rpm -Uvh https://repo.centos-webpanel.com/repo/7/beta/cwpphp-7.2.30-1.x86_64.rpm

			PHPIONCUBEPHPINI2=`grep ioncube_loader_lin_7.2.so /usr/local/cwp/php71/php.ini`

			if [ ! -z "$PHPIONCUBEPHPINI2" ];then
				UPDATECWPPHP72="1"
			else
				# Send error to admin panel
				/usr/local/cwp/php71/bin/php /usr/local/cwpsrv/htdocs/resources/admin/include/libs/notifications/cli.php --level="danger" --subject="CWP Update FAILED" --message="Please urgently run update of cwpphp rpm: yum update cwpphp --enablerepo=cwp"
				exit 1
			fi
		else
			# Send error to admin panel ioncube update failed
			/usr/local/cwp/php71/bin/php /usr/local/cwpsrv/htdocs/resources/admin/include/libs/notifications/cli.php --level="danger" --subject="CWP IonCube Update Failed" --message="Please urgently check your IonCube update by running shell command: sh /scripts/update_ioncube"
			exit 1
		fi
	else
		# Send error on admin panel
		/usr/local/cwp/php71/bin/php /usr/local/cwpsrv/htdocs/resources/admin/include/libs/notifications/cli.php --level="danger" --subject="CWP IonCube ERROR" --message="Please urgently check your IonCube configuration in file: /usr/local/cwp/php71/php.ini"
		exit 1
	fi
fi

# --- PHP 8.3 safety guard (added 2026-11-10, #967195 / #534445) ------------------------------------------------------
# The panel code (CWP 1.25+) is ionCube-encoded for PHP 8.3. If the working panel is overwritten with 8.3-encoded code
# while the panel PHP (php71) is still older than 8.3, every panel request and cron.php itself fatals
# ("...encoded ... for PHP 8.3 and cannot be decoded by this ionCube Loader") -> admin panel HTTP 500, and since
# cron.php is then unrunnable the server can no longer self-heal; only a manual "sh /scripts/update_cwpphp83" from the
# console recovers it. So make sure php71 is >= 8.3 BEFORE touching any code. If it is older, run the 8.3 migration;
# if that still fails (e.g. dead OS repositories), abort the update WITHOUT changing any code so the server keeps its
# current, working panel, and alert the admin.
php71_mm() { /usr/local/cwp/php71/bin/php -r 'echo PHP_MAJOR_VERSION*100+PHP_MINOR_VERSION;' 2>/dev/null; }
PHP71MM=`php71_mm`; case "$PHP71MM" in ''|*[!0-9]*) PHP71MM=0 ;; esac
if [ "$PHP71MM" -lt 803 ]; then
	echo "panel PHP (php71) reports $PHP71MM (<803); running the 8.3 migration before applying 8.3-encoded code"
	[ -x /scripts/update_cwpphp83 ] && sh /scripts/update_cwpphp83
	PHP71MM=`php71_mm`; case "$PHP71MM" in ''|*[!0-9]*) PHP71MM=0 ;; esac
fi
if [ "$PHP71MM" -lt 803 ]; then
	/usr/local/cwp/php71/bin/php /usr/local/cwpsrv/htdocs/resources/admin/include/libs/notifications/cli.php --level="danger" --subject="CWP update postponed (panel PHP too old)" --message="This CWP update was postponed: the new panel code needs PHP 8.3 but the panel PHP (php71) is still older. Your current panel keeps working. Please run on the server:  sh /scripts/update_cwpphp83  -- if it reports an RPM or dependency error, the operating-system repositories need fixing first (an EOL CentOS 8 needs the vault repositories)." 2>/dev/null
	echo "panel PHP is still <8.3 after the migration attempt; update aborted and the current panel code kept."
	echo "Fix the OS repositories if needed, then run: sh /scripts/update_cwpphp83"
	exit 1
fi
# ---------------------------------------------------------------------------------------------------------------------

# Remove old configs
rm -f /usr/local/cwpsrv/htdocs/resources/conf/monit.d/*

# WebPanel Install
echo
echo "-- CWP Systems --"
echo "Installing services"
if [ ! -e "/usr/local/cwpsrv/var/services/" ];then
mkdir -p /usr/local/cwpsrv/var/services/
fi

cd /usr/local/cwpsrv/var/services/
rm -f cwp-services.zip

wget -q https://static.cdn-cwp.com/files/cwp/el7/cwp-services.zip
if [ ! -f "cwp-services.zip" ]; then
	wget -q https://dl1.centos-webpanel.com/files/cwp/el7/cwp-services.zip
fi
unzip -o cwp-services.zip
rm -f cwp-services.zip*

echo "Installing admin files"

cd /usr/local/cwpsrv/htdocs
chattr -R -i /usr/local/cwpsrv/htdocs 2> /dev/null

# NEW MD5
rm -f cwp-el7-1.30.32.zip*
wget -q https://static.cdn-cwp.com/files/cwp/el7/cwp-el7-1.30.32.zip
wget -q https://static.cdn-cwp.com/files/cwp/el7/cwp-el7-1.30.32.zip.md5

if [ ! -f "cwp-el7-1.30.32.zip" ]; then
	wget  https://dl1.centos-webpanel.com/files/cwp/el7/cwp-el7-1.30.32.zip
fi

if [ ! -f "cwp-el7-1.30.32.zip.md5" ]; then
	wget  https://dl1.centos-webpanel.com/files/cwp/el7/cwp-el7-1.30.32.zip.md5
fi

FILECHECK1=`md5sum cwp-el7-1.30.32.zip|awk {'print $1'}`
FILECHECK2=`cat cwp-el7-1.30.32.zip.md5`

if [ -e "/usr/bin/md5sum" ];then
	if [ "$FILECHECK1" = "$FILECHECK2" ];then
		unzip -o cwp-el7-1.30.32.zip

		# Double extract for 7.2 only
		if [ "$UPDATECWPPHP72" = "1" ];then
			unzip -o cwp-el7-1.30.32.zip
			echo "CWPPHP 7.2 Upgrade: Starting CWP restart"
			sh /scripts/restart_cwpsrv
			exit 0
		fi
	else
		# need to add here admin panel notification about failed upgrade
		echo ""
	fi
else
	unzip -o cwp-el7-1.30.32.zip

	# Double extract for 7.2 only
	if [ "$UPDATECWPPHP72" = "1" ];then
		unzip -o cwp-el7-1.30.32.zip
		echo "CWPPHP 7.2 Upgrade: Starting CWP restart"
		sh /scripts/restart_cwpsrv
		exit 0
	fi
fi

rm -f cwp-el7-1.30.32.zip*
# END NEW MD5

# Update CWP rpms
#yum -y update cwpphp cwpsrv --enablerepo=cwp # disabled for php7.2 update
service cwpsrv restart
service cwp-phpfpm restart
sh /scripts/cwpsrv_rebuild_user_conf

# Fix default template for apache htdocs, also known as It works
CHECKCWPTPL=`grep "It works" /usr/local/apache/htdocs/index.html`

if [ ! -z "$CHECKCWPTPL" ];then
        rm -f /usr/local/apache/htdocs/index.html
	rsync -a /usr/local/cwpsrv/htdocs/resources/admin/tpl/new_account_tpl/ /usr/local/apache/htdocs/
fi

# Cron Chmod
chmod +x /etc/cron.daily/cwp

#Letsencrypt cron check
ROOTCRONFILE="/var/spool/cron/root"
if [ -e "$ROOTCRONFILE" ];then
        CHKACME=`grep "/root/.acme.sh" $ROOTCRONFILE`
       	CHKACMECWP=`grep "/root/.acme.sh/cwp_certs" $ROOTCRONFILE`

        if [ ! -z "$CHKACME" ];then
               	if [ -z "$CHKACMECWP" ];then
                        sed -i "s@--home \"/root/.acme.sh\"@--home \"/root/.acme.sh/cwp_certs\"@g" $ROOTCRONFILE
                fi
        fi
fi

echo "#################"
echo "CWP UPDATER END"
echo "#################"
echo "Check that you CWP Works."
echo "if you need help visit our website: www.centos-webpanel.com"
