Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ###
- ### MAKE BACK-UPS
- ### I AM NOT RESPONSIBLE
- ### MAKE BACK-UPS
- ### DON'T USE REGEXES
- ### MAKE BACK-UPS
- ### D O N O T U S E R E G E X E S
- ### MAKE BACK-UPS
- ###
- ###
- ### oh, and make back-ups
- # Where the php.ini file is stored
- inifile="/etc/php/php.ini"
- # Get extension
- ext="$1"
- RED=$(echo -e '\e[91m')
- GREEN=$(echo -e '\e[92m')
- BOLD=$(echo -e '\e[1m')
- NONE=$(echo -e '\e[0m')
- # Check if extension specified
- if [ "x$ext" = "x" ]; then
- echo $RED"No extension specified"$NONE >&2
- exit 1
- fi
- # Check if ini file exists
- if ! [ -f "$inifile" ]; then
- echo $RED"INI file not found (check script)"$NONE >&2
- exit 1
- fi
- # Check if we have the right permissions
- if ! [ -r "$inifile" -a -w "$inifile" ]; then
- echo $RED"Not enough permissions for INI file (try sudo?)"$NONE >&2
- exit 1
- fi
- # Check if user read the warnings
- if [ "$ext" = ".*" ]; then
- lines=$(cat "$inifile" | wc -l)
- echo $RED"Replaced $lines lines with $ext because you didn't read the warnings"$NONE >&2
- echo "(I hope you have back-ups)" >&2
- exit 1
- fi
- # Create back-up just in case user is stupid
- cp "$inifile" "/tmp/.php-toggle-ext-php.ini.bak" >/dev/null
- # Regexes
- REX_ALL="^;?extension"
- REX_ENA="^extension"
- REX_DIS="^;extension"
- # Find the line(s) matching this extension
- matches=$(grep -E "$REX_ALL=$ext\$" "$inifile")
- # Check if any matches are found
- if [ $? -ne 0 ]; then
- echo $RED"Failed to find extension in INI file"$NONE >&2
- echo
- # Show extensions that contain given value
- echo "Maybe you want one of these?"
- # enabled => green ; disabled => red ; tab ; end color
- grep -E "$REX_ALL=.*$ext" "$inifile" | sed -E "s/$REX_ENA=/$GREEN/g;s/$REX_DIS=/$RED/g;s/^/\t/g;s/\$/$NONE/g"
- exit 1
- fi
- # Count amount of matches
- amount=$(echo "$matches" | wc -l)
- # Warn for multiple lines (this shouldn't happen)
- if [ $amount -ge 2 ]; then
- echo $RED"Too many matches ($amount) found, only changing the first line!!"$NONE >&2
- fi
- # Check state of extension
- grep -E "$REX_ENA=$ext\$" "$inifile" >/dev/null
- state=$?
- # Toggle state
- if [ $state -eq 0 ]; then
- echo -n "[ ] Disabling extension $BOLD$ext$NONE"
- sed -Ei "s/$REX_ENA=$ext\$/;extension=$ext/" "$inifile"
- echo -e "\r[$GREEN DONE $NONE]"
- else
- echo -n "[ ] Enabling extension $BOLD$ext$NONE"
- sed -Ei "s/$REX_DIS=$ext\$/extension=$ext/" "$inifile"
- echo -e "\r[$GREEN DONE $NONE]"
- fi
Add Comment
Please, Sign In to add comment