#!/bin/bash
PHPINI="/etc/suphp-php.ini"
VHOSTROOT="/var/www/vhosts"
#if the NEW_DOMAIN_NAME (folder) in the vhost root has a conf sub directory then
# its a NEW_DOMAIN_NAME name, and not folders like chroot, default, etc.
MYSQL_PW=`cat /etc/psa/.psa.shadow`
MYSQL_RUN="mysql -N -B -uadmin -p$MYSQL_PW psa"
#Build MySQL Query for getting php and php safe mode statuses
MYSQL_QUERY="select php, php_safe_mode from hosting where dom_id=(select id from domains where name='$NEW_DOMAIN_NAME');"
#Get the query results, and format
QUERY_RESULTS=`echo "$MYSQL_QUERY"|$MYSQL_RUN | awk '{print $1,$2}'`
#Get individual results
PHP_ENABLED=`echo $QUERY_RESULTS | awk '{print $1}'`
SAFE_MODE=`echo $QUERY_RESULTS | awk '{print $2}'`
#if PHP is enabled, then we will tell vhost.conf to include the suphp.conf file - otherwise we dont care
if [ "$PHP_ENABLED" == "true" ]; then
# if vhost.conf exists
if [ -f $VHOSTROOT/$NEW_DOMAIN_NAME/conf/vhost.conf ]; then
# if suphp.conf exists then they had php
if ! grep -q suphp.conf /var/www/vhosts/$NEW_DOMAIN_NAME/conf/vhost.conf ; then
echo "Include $VHOSTROOT/$NEW_DOMAIN_NAME/conf/suphp.conf" >> $VHOSTROOT/$NEW_DOMAIN_NAME/conf/vhost.conf
fi
# if vhost.conf doesnt exist
else
echo "Include $VHOSTROOT/$NEW_DOMAIN_NAME/conf/suphp.conf" > $VHOSTROOT/$NEW_DOMAIN_NAME/conf/vhost.conf
fi
fi
#Make the directories for the client
mkdir -p $VHOSTROOT/$NEW_DOMAIN_NAME/tmp
mkdir -p $VHOSTROOT/$NEW_DOMAIN_NAME/etc
#copy over a php.ini file if they dont already have one
if [ ! -f $VHOSTROOT/$NEW_DOMAIN_NAME/etc/php.ini ]; then
#They dont have one, lets give them one
cp -f --reply=yes $PHPINI $VHOSTROOT/$NEW_DOMAIN_NAME/etc/php.ini >/dev/null 2>&1
fi
# Replace NEW_DOMAIN_NAME_NAME in the php file
# This should be preconfigured so that you just have to replace
# the text above with the new NEW_DOMAIN_NAME names
perl -pi -e "s/DOMAIN_NAME/$NEW_DOMAIN_NAME/gi" $VHOSTROOT/$NEW_DOMAIN_NAME/etc/php.ini
#If there is an Old domain name, make sure its nto the same, and has a value other wise it will
#replace all characters in the php file, and that is no good.
if [ "$OLD_DOMAIN_NAME" != "" ]; then
if [ "$OLD_DOMAIN_NAME" != "$NEW_DOMAIN_NAME" ]; then
perl -pi -e "s/$OLD_DOMAIN_NAME/$NEW_DOMAIN_NAME/gi" $VHOSTROOT/$NEW_DOMAIN_NAME/etc/php.ini
fi
fi
#in case vhost root is not default or is changed with out updating php.ini
if [ "/var/www/vhosts" != "$VHOSTROOT" ]; then
perl -pi -e "s@/var/www/vhosts@$VHOSTROOT@gi" $VHOSTROOT/$NEW_DOMAIN_NAME/etc/php.ini
fi
#Turn OFF allow_url_fopen
perl -pi -e "s/allow_url_fopen = On/allow_url_fopen = Off/gi" $VHOSTROOT/$NEW_DOMAIN_NAME/etc/php.ini
if [ "$SAFE_MODE" == "false" ]; then
#Turn OFF safe mode
perl -pi -e "s/safe_mode = on/safe_mode = off/gi" $VHOSTROOT/$NEW_DOMAIN_NAME/etc/php.ini
fi
if [ "$SAFE_MODE" == "true" ]; then
#Turn ON safe mode
perl -pi -e "s/safe_mode = off/safe_mode = on/gi" $VHOSTROOT/$NEW_DOMAIN_NAME/etc/php.ini
fi
#Get the user and group for suphp file
SUPHP_USER=`ls -la $VHOSTROOT/$NEW_DOMAIN_NAME/ | grep httpdocs | awk '{print $3}'`
SUPHP_GROUP=`ls -la $VHOSTROOT/$NEW_DOMAIN_NAME/ | grep httpdocs | awk '{print $4}'`
FILE_GROUP="psacln"
#Set permissions on them all too
chown -R $SUPHP_USER:$FILE_GROUP $VHOSTROOT/$NEW_DOMAIN_NAME/tmp
chown -R root:root $VHOSTROOT/$NEW_DOMAIN_NAME/etc
chmod -R 770 $VHOSTROOT/$NEW_DOMAIN_NAME/tmp
chmod -R 755 $VHOSTROOT/$NEW_DOMAIN_NAME/etc
for FOLDER in `ls $VHOSTROOT/$NEW_DOMAIN_NAME/httpdocs | grep -v plesk-stat`;
do chown -R $SUPHP_USER:$FILE_GROUP $VHOSTROOT/$NEW_DOMAIN_NAME/httpdocs/$FOLDER;
done
#remove any pre-existing suphp.conf
rm -Rf $VHOSTROOT/$NEW_DOMAIN_NAME/conf/suphp.conf
echo "
<Directory $VHOSTROOT/$NEW_DOMAIN_NAME/httpdocs>
php_admin_flag engine Off
suPHP_Engine on
suPHP_UserGroup $SUPHP_USER $FILE_GROUP
#vhost php.ini
suPHP_ConfigPath \"$VHOSTROOT/$NEW_DOMAIN_NAME/etc\"
AddHandler x-httpd-php .php
suPHP_AddHandler x-httpd-php
</Directory>" > $VHOSTROOT/$NEW_DOMAIN_NAME/conf/suphp.conf
/usr/local/psa/admin/bin/websrvmng -u --vhost-name=$NEW_DOMAIN_NAME