#********************************************************************
#* WP-HTACCESS script Ben Makkink 13 december 2024                  *
#* Wordpress maakt gebruik van een .htaccess bestand                *
#* Als we geen .htaccess willen gebruiken moet de code overgebracht *
#* worden naar http configuratie in ../conf-available/wordpress.conf*
#* Als echter een update plaatsvind in .htaccess zonder dat er een  *
#* wordpress update plaatsvond kan dit wijzen op een hack en moet   *
#* deze code NIET naar wordpress.conf overgezet worden!             *
#********************************************************************
#!/bin/bash

wp_version1="/var/www/html/blog/wp-includes/version.php"
wp_version2="/var/www/html/blog/wp-includes/version_org"

htaccess1="/var/www/html/blog/.htaccess"
htaccess2="/var/www/html/blog/.htaccess_org"
hta_fout="/var/www/html/blog/hta_fout"
hta_ok="/var/www/html/blog/hta_ok"
hta_terug="/var/www/html/blog/hta_terug"

# Bij eerste gebruik van dit script bestaan de *_org bestanden
# nog niet en worden aangemaakt (voor checken updates/wijzigingen).
#-------------------------------------------------------------------
if ! [ -f "$wp_version2" ]; then
    cp -a $wp_version1 $wp_version2;
fi

if ! [ -f "$htaccess2" ]; then
    cp -a $htaccess1 $htaccess2;
fi

# Check of er een wijziging is in ../blog/.htaccess
#-------------------------------------------------------------------
  if cmp -s "$htaccess1" "$htaccess2"; then
      # Er is geen wijziging in de .htaccess code
    if [ $wp_version1 -nt $wp_version2 ]; then
      # Er is een WP update geweest
      # Zet nwe version_org gelijk aan version.php
      cp -a $wp_version1 $wp_version2;
      # Stuur mail met WP update melding
      cat /usr/local/bin/wp-updated-mail.txt | mail -s\
                   "Wordpress update is uitgevoerd" ben@makkink.eu
    fi
    # Er is geen wijziging in .htaccess, stop script.
    exit;
  fi

# Er is een wijziging in .htaccess, maar als er geen WP upgrade was
# dan is er mogelijk een hack! Maak in dat geval een kopie 'hta_fout'
# Als bij een volgende executie van het script het bestand hta_fout
# bestaat wordt het script beeindigd (voorkom herhaling foutmelding).
#--------------------------------------------------------------------
  if [ -f "$hta_fout" ]; then
    # echo "Bestand 'hta_fout' gevonden, exit script"
    exit;
  fi

# Er is een wijziging in .htaccess die na manuele evaluatie niet
# goedgekeurd is, dwz 'hta_fout' is gewijzigd naar 'hta_terug'
# De oorspronkelijke .htaccess wordt teruggezet
#--------------------------------------------------------------------
  if [ -f "$hta_terug" ]; then
    # echo "Bestand 'hta_terug' gevonden, rollback en exit script"
    cp -a $htaccess2 $htaccess1;

    # Opschonen
    if [ -f "$hta_fout" ]; then
      rm $hta_fout;
    fi

    if [ -f "$hta_terug" ]; then
      rm $hta_terug;
    fi

    if [ -f "$hta_ok" ]; then
      rm $hta_ok;
    fi

    exit;
  fi

# Er is een .htaccess wijziging zonder een WP upgrade. Hack?
#-------------------------------------------------------------------
if [ ! $wp_version1 -nt $wp_version2 ] && [ ! -f "$hta_ok" ]; then
  # Stuur een mail met instructie handmatige interventie
  cat /usr/local/bin/wp-htaccess-hack.txt | mail -s\
                   "Wordpress .htaccess gehackt?" ben@makkink.eu

  # Sla gewijzigde/gehachte .htaccess op als 'hta_fout'
  cp -a $htaccess1 $hta_fout;
  # Exit script.
  exit;
  fi

# Er is een wijziging in .htaccess als onderdeel van een WPupdate,
# of de nieuwe .htaccess is manueel geevalueerd en OK bevonden
# dwz. 'hta_fout' is gewijzigd naar 'hta_ok'
#
# Schrijf de nwe inhoud van .htaccess naar wordpress.conf
#--------------------------------------------------------------------
# Print inhoud huidige wordpress.conf tot 'BEGIN' naar wordpress.tmp
sed -n '/BEGIN/q;p' /etc/apache2/conf-available/wordpress.conf >> /etc/apache2/conf-available/wordpress.tmp;
# Concateneer inhoud van nwe .htaccess vanaf 'BEGIN' tot eind bestand
sed -n '/BEGIN/,$p' $htaccess1 >>/etc/apache2/conf-available/wordpress.tmp;
# Concateneer </Directory> om directive af te sluiten
printf "</Directory> \n" >>/etc/apache2/conf-available/wordpress.tmp;
# Hernoem wordpress.tpm naar wordpress.conf en herstart httpd
mv /etc/apache2/conf-available/wordpress.tmp /etc/apache2/conf-available/wordpress.conf;
systemctl restart apache2;

# Stuur een mail over WP Update met .htaccess wijziging
#------------------------------------------------------------------
  cat /usr/local/bin/wp-htaccessupdated-mail.txt | mail -s\
    "Wordpress .htaccess integratie is uitgevoerd." ben@makkink.eu

# Opschonen en reset '*_org' bestanden
#------------------------------------------------------------------
if [ -f "$hta_fout" ]; then
rm $hta_fout;
fi

if [ -f "$hta_terug" ]; then
rm $hta_terug;
fi

if [ -f "$hta_ok" ]; then
rm $hta_ok;
fi

# Reset '*_org' bestanden
#------------------------------------------------------------------
cp -a $wp_version1 $wp_version2
cp -a $htaccess1 $htaccess2 










