Guest User

Untitled

a guest
Jan 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3.  
  4. ###
  5. ### MAKE BACK-UPS
  6. ### I AM NOT RESPONSIBLE
  7. ### MAKE BACK-UPS
  8. ### DON'T USE REGEXES
  9. ### MAKE BACK-UPS
  10. ### D O N O T U S E R E G E X E S
  11. ### MAKE BACK-UPS
  12.  
  13. ###
  14. ###
  15. ### oh, and make back-ups
  16.  
  17.  
  18.  
  19. # Where the php.ini file is stored
  20. inifile="/etc/php/php.ini"
  21.  
  22. # Get extension
  23. ext="$1"
  24.  
  25.  
  26. RED=$(echo -e '\e[91m')
  27. GREEN=$(echo -e '\e[92m')
  28. BOLD=$(echo -e '\e[1m')
  29. NONE=$(echo -e '\e[0m')
  30.  
  31.  
  32.  
  33. # Check if extension specified
  34. if [ "x$ext" = "x" ]; then
  35. echo $RED"No extension specified"$NONE >&2
  36. exit 1
  37. fi
  38.  
  39.  
  40. # Check if ini file exists
  41. if ! [ -f "$inifile" ]; then
  42. echo $RED"INI file not found (check script)"$NONE >&2
  43. exit 1
  44. fi
  45.  
  46. # Check if we have the right permissions
  47. if ! [ -r "$inifile" -a -w "$inifile" ]; then
  48. echo $RED"Not enough permissions for INI file (try sudo?)"$NONE >&2
  49. exit 1
  50. fi
  51.  
  52. # Check if user read the warnings
  53. if [ "$ext" = ".*" ]; then
  54. lines=$(cat "$inifile" | wc -l)
  55. echo $RED"Replaced $lines lines with $ext because you didn't read the warnings"$NONE >&2
  56. echo "(I hope you have back-ups)" >&2
  57. exit 1
  58. fi
  59.  
  60. # Create back-up just in case user is stupid
  61. cp "$inifile" "/tmp/.php-toggle-ext-php.ini.bak" >/dev/null
  62.  
  63.  
  64. # Regexes
  65. REX_ALL="^;?extension"
  66. REX_ENA="^extension"
  67. REX_DIS="^;extension"
  68.  
  69.  
  70. # Find the line(s) matching this extension
  71. matches=$(grep -E "$REX_ALL=$ext\$" "$inifile")
  72.  
  73.  
  74. # Check if any matches are found
  75. if [ $? -ne 0 ]; then
  76. echo $RED"Failed to find extension in INI file"$NONE >&2
  77. echo
  78.  
  79. # Show extensions that contain given value
  80. echo "Maybe you want one of these?"
  81.  
  82. # enabled => green ; disabled => red ; tab ; end color
  83. grep -E "$REX_ALL=.*$ext" "$inifile" | sed -E "s/$REX_ENA=/$GREEN/g;s/$REX_DIS=/$RED/g;s/^/\t/g;s/\$/$NONE/g"
  84.  
  85. exit 1
  86. fi
  87.  
  88.  
  89. # Count amount of matches
  90. amount=$(echo "$matches" | wc -l)
  91.  
  92. # Warn for multiple lines (this shouldn't happen)
  93. if [ $amount -ge 2 ]; then
  94. echo $RED"Too many matches ($amount) found, only changing the first line!!"$NONE >&2
  95. fi
  96.  
  97.  
  98. # Check state of extension
  99. grep -E "$REX_ENA=$ext\$" "$inifile" >/dev/null
  100. state=$?
  101.  
  102. # Toggle state
  103. if [ $state -eq 0 ]; then
  104. echo -n "[ ] Disabling extension $BOLD$ext$NONE"
  105. sed -Ei "s/$REX_ENA=$ext\$/;extension=$ext/" "$inifile"
  106. echo -e "\r[$GREEN DONE $NONE]"
  107. else
  108. echo -n "[ ] Enabling extension $BOLD$ext$NONE"
  109. sed -Ei "s/$REX_DIS=$ext\$/extension=$ext/" "$inifile"
  110. echo -e "\r[$GREEN DONE $NONE]"
  111. fi
Add Comment
Please, Sign In to add comment