<commit_before>#!/bin/sh

# PHP-FPM can listen on a UNIX socket or a TCP port. Use 'tcp' if your web
# server will load balance to a pool of PHP-FPM servers. Else use sockets
# and avoid the TCP overhead.
PHP_LISTEN_MODE=${PHP_LISTEN_MODE:="socket"}

install_php()
{
	_version="$1"; if [ -z "$_version" ]; then _version="56"; fi

	tell_status "installing PHP $_version"

	_ports="php$_version"
	_modules="$2"

	if [ "$TOASTER_MYSQL" = "1" ]; then
		tell_status "including php mysqli & PDO_mysql modules"
		_modules="$_modules pdo_mysql mysqli"
	fi

	for m in $_modules
	do
		_ports="$_ports php$_version-$m"
	done

	stage_pkg_install $_ports || exit
	install_php_newsyslog
}

install_php_newsyslog() {
	tell_status "enabling PHP-FPM log file rotation"
	tee "$STAGE_MNT/etc/newsyslog.conf.d/php-fpm" <<EO_FPM_NSL
# rotate the file after it reaches 1M
/var/log/php-fpm.log 600 7	1024	*	BCX	/var/run/php-fpm.pid 30
EO_FPM_NSL
}

configure_php_ini()
{
	local _php_ini="$STAGE_MNT/usr/local/etc/php.ini"

	if [ -n "$1" ]; then
		if [ -f "$ZFS_JAIL_MNT/$1/usr/local/etc/php.ini" ]; then
			tell_status "preserving php.ini"
			cp "$ZFS_JAIL_MNT/$1/usr/local/etc/php.ini" "$_php_ini"
			return
		fi
	fi

	tell_status "getting the timezone"
	TZ=$(md5 -q /etc/localtime)
	TIMEZONE=$(find /usr/share/zoneinfo -type f -print0 | xargs -0 md5 -r | grep "$TZ" | awk '{print $2}' |cut -c21-)

	if [ -z "$TIMEZONE" ]; then
		TIMEZONE="America\/Los_Angeles"
	fi

	tell_status "Setting TIMEZONE to :  $TIMEZONE "

	cp "$STAGE_MNT/usr/local/etc/php.ini-production" "$_php_ini" || exit
	sed -i.bak \
		-e "s|^;date.timezone =|date.timezone = $TIMEZONE|" \
		-e '/^post_max_size/ s/8M/25M/' \
		-e '/^upload_max_filesize/ s/2M/25M/' \
		"$_php_ini"

}

configure_php_fpm() {

	tell_status "enable syslog for PHP-FPM"
	sed -i.bak \
		-e '/^;error_log/ s/^;//' \
		-e '/^error_log/ s/= .*/= syslog/' \
		"$STAGE_MNT/usr/local/etc/php-fpm.conf"

	if [ "$PHP_LISTEN_MODE" = "tcp" ]; then
		return
	fi

	tell_status "switch PHP-FPM from TCP to unix socket"
	local _fpmconf="$STAGE_MNT/usr/local/etc/php-fpm.conf"<commit_msg>Fix timezone detection for php.ini (#516)<commit_after>#!/bin/sh

# PHP-FPM can listen on a UNIX socket or a TCP port. Use 'tcp' if your web
# server will load balance to a pool of PHP-FPM servers. Else use sockets
# and avoid the TCP overhead.
PHP_LISTEN_MODE=${PHP_LISTEN_MODE:="socket"}

install_php()
{
	_version="$1"; if [ -z "$_version" ]; then _version="56"; fi

	tell_status "installing PHP $_version"

	_ports="php$_version"
	_modules="$2"

	if [ "$TOASTER_MYSQL" = "1" ]; then
		tell_status "including php mysqli & PDO_mysql modules"
		_modules="$_modules pdo_mysql mysqli"
	fi

	for m in $_modules
	do
		_ports="$_ports php$_version-$m"
	done

	stage_pkg_install $_ports || exit
	install_php_newsyslog
}

install_php_newsyslog() {
	tell_status "enabling PHP-FPM log file rotation"
	tee "$STAGE_MNT/etc/newsyslog.conf.d/php-fpm" <<EO_FPM_NSL
# rotate the file after it reaches 1M
/var/log/php-fpm.log 600 7	1024	*	BCX	/var/run/php-fpm.pid 30
EO_FPM_NSL
}

configure_php_ini()
{
	local _php_ini="$STAGE_MNT/usr/local/etc/php.ini"

	if [ -n "$1" ]; then
		if [ -f "$ZFS_JAIL_MNT/$1/usr/local/etc/php.ini" ]; then
			tell_status "preserving php.ini"
			cp "$ZFS_JAIL_MNT/$1/usr/local/etc/php.ini" "$_php_ini"
			return
		fi
	fi

	tell_status "getting the timezone"
	TZ=$(md5 -q /etc/localtime)
	TIMEZONE=$(find /usr/share/zoneinfo -type f -print0 | xargs -0 md5 -r | grep "$TZ" | awk '{print $2; exit}' |cut -c21-)

	if [ -z "$TIMEZONE" ]; then
		TIMEZONE="America\/Los_Angeles"
	fi

	tell_status "Setting TIMEZONE to :  $TIMEZONE "

	cp "$STAGE_MNT/usr/local/etc/php.ini-production" "$_php_ini" || exit
	sed -i.bak \
		-e "s|^;date.timezone =|date.timezone = $TIMEZONE|" \
		-e '/^post_max_size/ s/8M/25M/' \
		-e '/^upload_max_filesize/ s/2M/25M/' \
		"$_php_ini"

}

configure_php_fpm() {

	tell_status "enable syslog for PHP-FPM"
	sed -i.bak \
		-e '/^;error_log/ s/^;//' \
		-e '/^error_log/ s/= .*/= syslog/' \
		"$STAGE_MNT/usr/local/etc/php-fpm.conf"

	if [ "$PHP_LISTEN_MODE" = "tcp" ]; then
		return
	fi

	tell_status "switch PHP-FPM from TCP to unix socket"
	local _fpmconf="$STAGE_MNT/usr/local/etc/php-fpm.conf"
{'fraction_non_alphanumeric': 0.15917691579943236, 'fraction_numerical': 0.015846736045411543, 'mean_word_length': 3.6269146608315097, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3571405', 'n_tokens_mistral': 1927, 'n_tokens_neox': 1785, 'n_words': 413}