#!/bin/bash

if [ "$1" = "-T" ]; then
	echo -n "(c) ISPsystem.com"
	exit 0
fi

. /usr/local/mgr5/lib/pkgsh/ispmgr_pkg_funcs.sh

LOCKFILE=${MGRDIR}/var/phpsess_clean.lock
exec 200>${LOCKFILE}
flock -n 200
if [ $? -ne 0 ]; then
	exit 0
fi


dir_clean() {
	# $1 - session path
	# $2 - proc names
	local sdir pnames pid
	sdir=${1}
	pnames="${2}"
	if [ -d "${sdir}" ]; then
		for pid in $(pidof $pnames); do
			find "/proc/${pid}/fd" -ignore_readdir_race  -lname "${sdir}/sess_*" -exec touch -c {} \;
		done
		find -O3 "${sdir}" -ignore_readdir_race -depth -mindepth 1 -name 'sess_*' -type f -cmin "+$gc_maxlifetime_mins" -delete
	fi

}


case ${OSTYPE} in
	DEBIAN)
		apache_proc_names="apache2"
		aphpconf=$(${ISPMGRCTL} pathlist elid=php_apache_ini | awk -F= '{print $2}')
		if [ -z "${aphpconf}" ] || [ ! -f "${aphpconf}" ]; then
			if [ -f /etc/php5/apache2/php.ini ]; then
				aphpconf=/etc/php5/apache2/php.ini
			elif [ -f /etc/php/7.0/apache2/php.ini ]; then
				aphpconf=/etc/php/7.0/apache2/php.ini
			elif [ -f /etc/php/7.2/apache2/php.ini ]; then
				aphpconf=/etc/php/7.2/apache2/php.ini
			elif [ -f /etc/php/7.3/apache2/php.ini ]; then
				aphpconf=/etc/php/7.3/apache2/php.ini
			else
				exit 1
			fi
		fi
		fpm_native_dir=$(${ISPMGRCTL} pathlist elid=fpm-pool.d | awk -F= '{print $2}')
		if [ -z "${fpm_native_dir}" ]; then
			if [ -f /etc/php5/fpm/pool.d ]; then
				fpm_native_dir=/etc/php5/fpm/pool.d
			elif [ -f /etc/php/7.0/fpm/pool.d ]; then
                fpm_native_dir=/etc/php/7.0/fpm/pool.d
			elif [ -f /etc/php/7.2/fpm/pool.d ]; then
                fpm_native_dir=/etc/php/7.2/fpm/pool.d
			elif [ -f /etc/php/7.3/fpm/pool.d ]; then
                fpm_native_dir=/etc/php/7.3/fpm/pool.d
			fi
		fi
		test -f ${aphpconf} || exit 1
		gc_maxlifetime=$(php -c ${aphpconf} -d "error_reporting='~E_ALL'" -r 'echo ini_get("session.gc_maxlifetime");')
	;;
	REDHAT)
		apache_proc_names="httpd httpd.itk"
		aphpconf=/etc/php.ini
		fpm_native_dir=$(${ISPMGRCTL} pathlist elid=fpm-pool.d | awk -F= '{print $2}')
		if [ -z "${fpm_native_dir}" ]; then
			fpm_native_dir=/etc/php-fpm.d
		fi
		test -f ${aphpconf} || exit 1
		gc_maxlifetime=$(php -c ${aphpconf} -d "error_reporting='~E_ALL'" -r 'echo ini_get("session.gc_maxlifetime");')
	;;
esac

gc_maxlifetime_mins=$(($gc_maxlifetime / 60))

# apache
vhost_dir=$(${ISPMGRCTL} pathlist elid=apache-vhosts | awk -F= '{print $2}')
test -n "${vhost_dir}" || exit 0

for user_vhost in ${vhost_dir}/* ; do
	if [ ! -d "${user_vhost}" ]; then
		continue
	fi
	save_path_dirs=$(grep -sRh 'php_admin_value session.save_path' "${user_vhost}"/*  | awk '{print $3}' | sed 's/"//g; s/\r//g' | sort | uniq)
	for save_path in ${save_path_dirs} ; do
		dir_clean "${save_path}" "${apache_proc_names}"
	done
done

# fpm
fpm_dirs=$(find /opt -maxdepth 1 -type d -name 'php*')
for php_dir in ${fpm_dirs} ; do
	if [ ! -d ${php_dir}/etc/php-fpm.d ]; then
		continue
	fi
	find "${php_dir}/etc/php-fpm.d" -name '*.conf' |
		while read -r userconf ; do
		save_path=$(awk -F= '$1 ~ /php_admin_value\[session.save_path\]/ {gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2}' "${userconf}")
		if [ -n "${save_path}" ] && [ -d "${save_path}" ]; then
			dir_clean "${save_path}" "php-fpm php5-fpm"
		fi
	done
done
if [ -d "${fpm_native_dir}" ]; then
	find "${fpm_native_dir}" -name '*.conf' |
		while read -r userconf ; do
		save_path=$(awk -F= '$1 ~ /php_admin_value\[session.save_path\]/ {gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2}' "${userconf}")
		if [ -n "${save_path}" ] && [ -d "${save_path}" ]; then
			dir_clean "${save_path}" "php-fpm php5-fpm"
		fi
	done
fi


# cgi/fcgid
for user in $(${ISPMGRCTL} user | awk '{for(f=1;f<$NF;f++){if($f~/^name=/){sub("name=", "", $f); print $f; break}}}'); do
	if ! id ${user} >/dev/null 2>&1 ; then
		continue
	fi
	userdir=$(getent passwd "${user}" | awk -F: '{print $6}')
	if [ -n "${userdir}" ] && [ -d "${userdir}" ]; then
		save_path="${userdir}/bin-tmp"
		if [ -d "${save_path}" ]; then
			dir_clean "${save_path}" "php-cgi php5-cgi"
		fi
	fi
done
