#!/bin/bash

#this script is to find references to bad php.ini files and .htaccess files in order to quickly and efficiently install iopn and fix addhandler problems with .htaccess files.


#first, lets identify the .htaccess files and replace any php5 php4 and php4s handlers.

echo "Do you need to fix the .htaccess files and change them to PHP5 Single php.ini mode? "
echo "if yes, press 'y' to continue, otherwise press enter to skip the .htaccess fix"
read YES
if [ "$YES" = "y" ]; then
	echo "starting the .htaccess fix"
	htaccesspath=`find ~/public_html/ -maxdepth 10 -name .htaccess`;
	blankphp='application\/x\-httpd\-php '
	blankphp4='application\/x\-httpd\-php4 '
	blankphp4s='application\/x\-httpd\-php4s '
	blankphp5='application\/x\-httpd\-php5 '
	blankphp5s='application\/x\-httpd\-php5s '

	for loop1 in "$htaccesspath"; do
		for loop2 in "$blankphp" "$blankphp4" "$blankphp4s" "$blankphp5"; do
                	 sed -i.bak.`date +%Y.%m.%d.%H:%M` "s/$loop2/$blankphp5s/g" $loop1;
	         done
	done

	echo "all .htaccess files are now php5 single php.ini"
	echo 'if you get an "Operation not permitted" error, go to that file and rename it to .htaccess.old.bak or just get rid of the .htaccess file'
	sleep 1
else
	echo "skipping .htaccess fix"
	sleep 1
fi

#Now, lets get the home path nd username so we can use it in the sed statements:
homefix=""
homefix=`echo $HOME | sed 's/\//\\\\\//g'`
CheckHomeFix(){
        if [ -z  "$homefix" ]; then
                echo "The \$homefix variable is blank, please enter the path to 
the users account in the format of: /home/username and press enter"
                echo "accourding to pwd, the path your currently in is:"
                echo `pwd`;
                echo "please enter the home path to the account:"
                read fixhome
                homefix=`echo $fixhome | sed 's/\//\\\\\//g'`
                CheckHomeFix
        else
                echo "The \$homefix variable is set,"
                echo "homefix =  $homefix"
                echo 'This line above should appears as:'
                echo "\/home?\/username"
                echo "if it does not, please contact Matt Montgomery to fix it"
                sleep 3
        fi
}

CheckHomeFix

CheckCorrectExtension(){
        phpextendir=`grep 'no-debug-zts' public_html/php.ini | awk -F / '{print $7}'| awk -F \" '{print $1}'`
        echo "this is the current php extension_dir $phpextendir"
        extendir=`find /usr/lib/php/extensions/ -name no-debug-zts* -type d | awk -F / '{print $6}'`
        echo "this is the current extension_dir of the server: $extendir"
        if [ "$phpextendir" = "$extendir" ]; then
                echo "the PHP extension Directory and the Server match. SkippingExtension directory correction."
                sleep 2
        else
                echo "The Extension directory for the php.ini file does Not match the servers extension directory," 
echo " Do you want to replace it with the correct folder? y or n?"
                read FIXEXTDIR
                if [ "$FIXEXTDIR" = "y" ]; then
                        echo "Fixing..."
                        sed -i "s/\/usr\/local\/lib\/php\/extensions\/no\-debug\-zts\-20060613/\/usr\/local\/lib\/php\/extensions\/$extendir/g" ~/public_html/php.ini
                        echo "fixed"
                else
                        echo "I will leave that for you to update manually."
                fi
        fi
}


#the account should now have the php.ini file checked for the proper php5 version and extension directory.  If it is incorrect,  it will replace the file with a new php.ini file.
#check that the php.ini exists, if it does go to the extension check, then ioncube check, then pdo check
CheckPhpIniExists() {
	if [ -f ~/public_html/php.ini ]; then
		echo "php.ini exists"
		CheckExtensionDir
		sleep 3
		IoncubeExist
		sleep 3
		PdoExist
	else
		echo "php.ini does not exist"
		CheckPhpDefaultExists
	fi
}

#If the php.ini does not exist check if a php.ini.default exists.
CheckPhpDefaultExists() {
	if [ -f ~/public_html/php.ini.default ]; then
		echo "php.ini.default exists"
		RenamePhpDefault
	else
		echo "php.ini.default does not exist"
		PhpNotExist
	fi
}

#Check the php.ini file to check the Extension directory is correct.
CheckExtensionDir() {
	extendir2=`find /usr/lib/php/extensions/ -name no-debug-non-zts* -type d | awk -F / '{print $6}'`
	echo "the Current extension directory for the server is: $extendir2"
	sleep 2
	grep "/usr/local/lib/php/extensions/$extendir2" ~/public_html/php.ini
        if [ "$?" -eq "1" ]; then
		echo "extension directory does not exist, moving the php.ini to php.ini.`date +%Y.%m.%d`"
		sleep 2
                mv ~/public_html/php.ini ~/public_html/php.ini.`date +%Y.%m.%d`
		CheckPhpIniExists
	else
		echo "extension directory does exist"
		echo "your php.ini should work."
		sleep 2
	fi
}

#If the php.ini does not exist, present options to get it
PhpNotExist() {
	echo "how do you want to get the php.ini installed?"
	echo "press 1 to download a php.ini file from matt2themax.com website"
	echo "or press 2 to have the script wait while you install a php.ini.default file from the PHP CONFIG in the cpanel"
	read CONT
	echo "you pressed: $CONT"
	if [ $CONT = "1" ]; then
		echo "getting php.ini file from matt2themax.com..."
		`wget -O ~/public_html/php.ini.default matt2themax.com/php.ini.default`
		echo "download complete"
		RenamePhpDefault
		CheckCorrectExtension
		CheckPhpIniExists
	elif [ "$CONT" = "2" ]; then
		echo "Please log into the Cpanel for this customer and go to the PHP Config section."
		echo "Install a php.ini.default file then press enter to continue"
		read Blah
		RenamePhpDefault
		CheckCorrectExtension
		CheckPhpIniExists
	else
		echo "You didn't press a key i recognized.  This script will now exit"
		exit 1
	fi	
}
#simply changes the php.ini.default to php.ini
RenamePhpDefault() {
	mv ~/public_html/php.ini.default ~/public_html/php.ini
	echo "php.ini.default has been renamed to php.ini"
}

#function to check the Ioncube installation
IoncubeExist() {
	grep "zend_extension_ts=$HOME/ioncube/ioncube_loader_lin_ts.so" ~/public_html/php.ini
        if [ "$?" -eq "1" ]; then
		echo "IonCube is not installed,"
		IoncubeInstall
	else
		echo "Ioncube is installed, it should work"
		sleep 2
	fi
}


#install the ioncube
IoncubeInstall() {
	echo "Do you want to install Ioncube?"
	echo "y or n"
	read YEP
	if [ "$YEP" = "y" ]; then
		echo "installing..."
		sed -i "s/\[Zend\]/zend\_extension\=$homefix\/ioncube\/ioncube\_loader\_lin\.so\nzend\_extension\_ts\=$homefix\/ioncube\/ioncube\_loader\_lin\_ts\.so\n\[Zend\]/g" ~/public_html/php.ini
		`wget -O ~/public_html/iontest.php matt2themax.com/iontest.php`
		`mkdir ~/ioncube`
		`ln -s /opt/ioncube/ioncube_loader_lin_5.so ~/ioncube/ioncube_loader_lin.so`
		`ln -s /opt/ioncube/ioncube_loader_lin_5_ts.so ~/ioncube/ioncube_loader_lin_ts.so` 
		echo "an iontest.php has been uploaded into the public_html folder.  you can now check the domain.com/iontest.php"
		echo "if the iontest still does not work after running this script, contact your local level 2."
		sleep 2
		IoncubeExist
	else
		echo "skipping the Ioncube installation"
	fi
}

#check if the PDO extensions exist
PdoExist() {
	echo "do you want to install the pdo extensions?"
	echo "y or n"
	read PDO
	if [ "$PDO" = "y" ]; then
		grep "extension=pdo_mysql.so" ~/public_html/php.ini
		if [ "$?" -eq "1" ]; then
			echo "pdo_mysql.so does not exist... installing..."
			sleep 1
			InstallPdoMysqlSo
		else
			echo "pdo_mysql exists, skipping..."
			sleep 1
		fi
		grep "extension=pdo_sqlite.so" ~/public_html/php.ini
		if [ "$?" -eq "1" ]; then
			echo "pdo_sqlite.so does not exist... installing..."
			sleep 1
			InstallPdoSqliteSo
		else
			echo "pdo_sqlite.so exists... skipping.."
			sleep 1
		fi
		grep "extension=sqlite.so" ~/public_html/php.ini
		if [ "$?" -eq "1" ]; then
			echo "sqlite.so does not exist... installing..."
			sleep 1
			InstallSqliteSo
		else
			echo "Sqlite.so exists... skipping..."
			sleep 1
		fi
		grep "extension=pdo.so" ~/public_html/php.ini
		if [ "$?" = "1" ]; then
			echo "pdo.so does not exist... installing..."
			sleep 1
			InstallPdoSo
		else
			echo "Pdo.so exists... skipping..."
			sleep 1
		fi
	else
		echo "skipping PDO installation."
	fi
}

#installs the pdo extension
InstallPdoSo() {
	sed -i 's/\; End\:/\; End\:\nextension\=pdo\.so/g' ~/public_html/php.ini
	echo "pdo.so is now installed."
}


InstallSqliteSo() {
        sed -i 's/\; End\:/\; End\:\nextension\=sqlite\.so/g' ~/public_html/php.ini
        echo "sqlite.so is now installed."
}

InstallPdoSqliteSo() {
        sed -i 's/\; End\:/\; End\:\nextension\=pdo\_sqlite\.so/g' ~/public_html/php.ini
        echo "pdo_sqlite.so is now installed."
}

InstallPdoMysqlSo() {
        sed -i 's/\; End\:/\; End\:\nextension\=pdo\_mysql\.so/g' ~/public_html/php.ini
        echo "pdo_mysql.so is now installed."
}

#finally, actually execute all those functions.
CheckPhpIniExists

CheckMemoryLimit(){
        echo "your current Memory limit is:"
        memlim=` grep "memory_limit" ~/public_html/php.ini | awk '{print $3}'`
        echo "$memlim";
        echo 'do you want to raise the memory limit? press "y" to change the limit or any other key to continue'
        read MOVEON
        if [ "$MOVEON" = "y" ]; then
                ChangeMemoryLimit
        else
                echo "Your Memory Limit of $memlim will not be changed.  continuing";
        fi
}

CheckExecutionTime(){
echo "your current Max Execution Time limit is:"
        Etime=` grep "max_execution_time" ~/public_html/php.ini | awk '{print $3}'`
        echo "$Etime";
        echo 'do you want to raise the Max Execution Time Limit? press "y" to change the limit or any other key to continue'
        read MOVEON2
        if [ "$MOVEON2" = "y" ]; then
                ChangeExecutionTime
        else
                echo "Your Max Execution time of $Etime seconds will not be changed.  continuing";
        fi
}

CheckInputTime(){
echo "your current Max Input Time limit is:"
        Itime=` grep "max_input_time" ~/public_html/php.ini | awk '{print $3}'`
        echo "$Itime";
        echo 'do you want to raise the Max Input Time Limit? press "y" to change the limit or any other key to continue'
        read MOVEON3
        if [ "$MOVEON3" = "y" ]; then
                ChangeInputTime
        else
                echo "Your Max Input time of $Itime seconds will not be changed.  continuing";
        fi
}

ChangeMemoryLimit(){
        echo "What memory Limit do you want to set?  Please enter a number only"
        read NEWMEM
        if [ -z "$NEWMEM" ]; then
                echo "you cannot enter nothing as the memory limit, please do it again."
                ChangeMemoryLimit
        elif [ "$NEWMEM" -lt "32" ]; then
                echo "You cannot set the memory limit lower than 32 MB.  please try again"
                ChangeMemoryLimit
        else

                echo "your new memory limit will be $NEWMEM MB, is this correct?  y or n"
                read YCHECK
                if [ "$YCHECK" = "y" ]; then
                        NEWMEM=`echo "$NEWMEM?M"| sed "s/\?//g"`
                        echo "your new memory limit is $NEWMEM"
                        sed -i "s/memory\_limit\ \=\ $memlim/memory\_limit\ \=\ $NEWMEM/g" ~/public_html/php.ini
                else
                        echo "Memory limit was not correct"
                        ChangeMemoryLimit
                fi
        fi
}

ChangeExecutionTime(){
        echo "What Execution Time Limit do you want to set? Enter the number of seconds,  Please enter a number only"
        read NEWEXC
        if [ -z "$NEWEXC" ]; then
                echo "you cannot enter nothing as the Execution limit, please do it again."
                ChangeExecutionTime
        elif [ "$NEWEXC" -lt "30" ]; then
                echo "You cannot set the Execution limit lower than 30 seconds.  please try again"
                ChangeExecutionTime
        else
                echo "your new Execution time will be $NEWEXC seconds, is this correct?  y or n"
                read YCHECK2
                if [ "$YCHECK2" = "y" ]; then
                        echo "your new Max Execution Time is $NEWEXC seconds"
                        sed -i "s/max\_execution\_time\ \=\ $Etime/max\_execution\_time\ \=\ $NEWEXC/g" ~/public_html/php.ini
                else
                        echo "Max Execution Time was not correct"
                        ChangeExecutionTime
                fi
        fi
}

ChangeInputTime(){
        echo "What Input Time Limit do you want to set? Enter the number of seconds,  Please enter a number only"
        read NEWINP
        if [ -z "$NEWINP" ]; then
                echo "you cannot enter nothing as the Input limit, please do it again."
                ChangeInputTime
        elif [ "$NEWINP" -lt "60" ]; then
                echo "You cannot set the Input limit lower than 60 seconds.  please try again"
                ChangeInputTime
        else
                echo "your new Input Time  will be $NEWINP Seconds, is this correct?  y or n"
                read YCHECK3
                if [ "$YCHECK3" = "y" ]; then
                        echo "your new Input time limit is $NEWINP seconds"
                        sleep 1
                        sed -i "s/max\_input\_time\ \=\ $Itime/max\_input\_time\ \=\ $NEWINP/g" ~/public_html/php.ini
                else
                        echo "Max Input Time was not correct"
                        ChangeInputTime
                fi
        fi
}

CheckMemoryLimit
CheckExecutionTime
CheckInputTime

CheckUploadMaxSize(){
        echo "your current Maximum Upload Size is:"
        umaxsize=` grep "upload_max_filesize" ~/public_html/php.ini | awk '{print $3}'`
        echo "$umaxsize";
        echo 'do you want to raise the Upload limit? press "y" to change the limit or any other key to continue'
        read MOVEON7
        if [ "$MOVEON7" = "y" ]; then
                ChangeUploadMaxSize
        else
                echo "Your Upload Max Filesize of $umaxsize will not be changed.  continuing";
        fi
}

CheckPostMaxSize(){
        echo "your current Mamimum Posing limit is:"
        pmaxsize=` grep "post_max_size" ~/public_html/php.ini | awk '{print $3}'`
        echo "$pmaxsize";
        echo 'do you want to raise the Posting limit? press "y" to change the limit or any other key to continue'
        read MOVEON8
        if [ "$MOVEON8" = "y" ]; then
                ChangePostMaxSize
        else
                echo "Your Max Post Size of $umaxsize will not be changed.  continuing";
        fi
}

ChangeUploadMaxSize(){
        echo "What File Size Upload limit do you want to set?  Please enter a number only"
        read NEWUP
        if [ -z "$NEWUP" ]; then
                echo "you cannot enter nothing as the File Upload limit, please do it again."
                ChangeUploadMaxSize
        elif [ "$NEWUP" -lt "2" ]; then
                echo "You cannot set the File Upload limit lower than 2 MB.  please try again"
                ChangeUploadMaxSize
        else

                echo "your new Upload limit will be $NEWUP MB, is this correct?  y or n"
                read YCHECK5
                if [ "$YCHECK5" = "y" ]; then
                        NEWUP=`echo "$NEWUP?M"| sed "s/\?//g"`
                        echo "your new Upload limit is $NEWUP"
                        sed -i "s/upload\_max\_filesize\ \=\ $umaxsize/upload\_max\_filesize\ \=\ $NEWUP/g" ~/public_html/php.ini
                else
                        echo "The Upload limit was not correct"
                        ChangeUploadMaxSize
                fi
        fi
}


ChangePostMaxSize(){
        echo "What Max Post Size limit do you want to set?  Please enter a number only"
        read NEWPOST
        if [ -z "$NEWPOST" ]; then
                echo "you cannot enter nothing as the Max Post limit, please do it again."
                ChangePostMaxSize
        elif [ "$NEWPOST" -lt "8" ]; then
                echo "You cannot set the Max Post Limit lower than 8 MB.  please try again"
                ChangePostMaxSize
        else

                echo "your new Post limit will be $NEWPOST MB, is this correct?  y or n"
                read YCHECK6
                if [ "$YCHECK6" = "y" ]; then
                        NEWPOST=`echo "$NEWPOST?M"| sed "s/\?//g"`
                        echo "your new Post limit is $NEWPOST"
                        sleep 1
                        sed -i "s/post\_max\_size\ \=\ $pmaxsize/post\_max\_size\ \=\ $NEWPOST/g" ~/public_html/php.ini
                else
                        echo "The Max Post limit was not correct"
                        ChangePostMaxSize
                fi
        fi
}

CheckFileUploadStatus(){
        FUP=`grep "file_uploads =" ~/public_html/php.ini | awk '{print $3}'`
        echo "your Current file_upload is set to $FUP"
        if [ "$FUP" = "Off" ]; then
                echo "your file_upload is set to Off, Do you want to Turn it on? y or n?"
                read MOVEON9
                if [ "$MOVEON9" = "y" ]; then
                        echo "okay, switching File_upload to On"
                        sed -i 's/file\_uploads\ \=\ Off/file\_uploads\ \=\ On/g' ~/public_html/php.ini
                        echo "File_uploads is now enabled."
                else
                        echo "I will leave the file_upload Variable as it is: $FUP"
                fi
        else
                echo "Your File_Upload Variable is turned On, continuing.."
        fi
}
CheckFileUploadStatus
CheckUploadMaxSize
CheckPostMaxSize

CheckRegGlobalStatus(){
        RGL=`grep "register_globals =" ~/public_html/php.ini | awk '{print $3}'`
        echo "Your Register_Globals status is set to $RGL"
        if [ "$RGL" = "Off" ]; then
                echo "your Register_Globals is set to Off, This is generally safest.  Turning it on can cause hacking if your code is not protected."
                echo "Do you want to turn Register_globals on? y or n?"
                read MOVE10
                if [ "$MOVE10" = "y" ]; then
                        echo "okay, switching Register_globals to On"
                        sed -i 's/register\_globals\ \=\ Off/register\_globals\ \=\ On/g' ~/public_html/php.ini
                        echo "Register_globals is now enabled."
                else
                        echo "I will leave Register_globals turned: $RGL"
                fi
        elif [ "$RGL" = "On" ]; then
                echo "your Register_globals is set to On, This is not safe.  Turning it off could protect you from  hacking if your code is not protected."
                echo "Do you want to turn Register_globals off? y or n?"
                read MOVE11
                if [ "$MOVE11" = "y" ]; then
                        echo "okay, switching Register_globals to Off"
                        sed -i 's/register\_globals\ \=\ On/register\_globals\ \=\ Off/g' ~/public_html/php.ini
                        echo "Register_globals is now Disabled."
                else
                        echo "I will leave Register_globals turned: $RGL"
                fi
        else
                echo "Something is wrong with the Register_Globals variable, please Edit the php.ini file"
		exit 1
        fi
}
CheckRegGlobalStatus



echo "This is the End of the script.  Please report any problems to Matt Montgomery, Thanks"
