Advertisement
marioq

my_get_error_page.sh

May 12th, 2018
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.75 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. ######### for test
  4. # SGERROR_FILE="/home/mario.q/myScripts/sgerror.php"
  5. # MY_GET_ERROR_PAGE="/home/mario.q/myScripts/personalSgerror.php"
  6. #########
  7. ######### real file on pfsense
  8. SGERROR_FILE="/usr/local/www/sgerror.php"
  9. MY_GET_ERROR_PAGE="/home/mario.q/myScripts/my_get_error_page.php"
  10. #########
  11.  
  12. ########################################
  13. ########################################
  14. #
  15. # da qui in poi non dovresti modificare niente
  16. # from here on you should not change anything
  17. #
  18. ########################################
  19. ########################################
  20.  
  21. PFS="`cat /etc/platform`"
  22. if [ "$PFS" != "pfSense" ]; then
  23.     echo "This script work on pfSense"
  24.     exit 101
  25. fi
  26.  
  27. if [ ! -w "$SGERROR_FILE" -o ! -r "$MY_GET_ERROR_PAGE" ]; then
  28.     echo "####"
  29.  
  30.     if [ ! -w "$SGERROR_FILE" ]; then
  31.         # CK_SGERROR_FILE="`ls -lh "$SGERROR_FILE" 2>&1`"
  32.         CK_SGERROR_FILE="`stat -x "$SGERROR_FILE" 2>&1 |\
  33.                         sed 's/:[[:space:]]\{1,100\}/:/g ;\
  34.                             /Device/,/Change/d ;\
  35.                             /Size/d ;\
  36.                             s/[[:space:]]\{2,100\}/ /g'`"
  37.  
  38.         echo -e "You cannot edit the file in var SGERROR_FILE=\"$SGERROR_FILE\"\n$CK_SGERROR_FILE"
  39.     else
  40.         echo -e " SGERROR_FILE=\"$SGERROR_FILE\"\n    OK"
  41.     fi
  42.     echo " ####"
  43.     if [ ! -r "$MY_GET_ERROR_PAGE" ]; then
  44.         CK_MY_GET_ERROR_PAGE="`ls -lh "$MY_GET_ERROR_PAGE" 2>&1`"
  45.         echo -e " MY_GET_ERROR_PAGE=\"$MY_GET_ERROR_PAGE\"\n $CK_MY_GET_ERROR_PAGE\n    The file exist ?? you can read it ??"
  46.     else
  47.         echo -e " MY_GET_ERROR_PAGE=\"$MY_GET_ERROR_PAGE\"\n    OK"
  48.     fi
  49.     echo "####"
  50.     exit 1
  51. fi
  52.  
  53. FUNCTION_ORIGINAL="function get_error_page("
  54. FUNCTION_RENAMED="function NO_get_error_page_NO("
  55. BLOCK_START="start - this include replace the original function ---------"
  56. BLOCK_END="end   - this include replace the original function ---------"
  57. SGERROR_TMP="`cat $SGERROR_FILE`"
  58. ERROR=0
  59.  
  60. case $1 in
  61. install|i)
  62.     if [ "`echo "$SGERROR_TMP" | grep "$FUNCTION_ORIGINAL"`" ]; then
  63.         echo "$SGERROR_TMP" |\
  64.         sed "s/`echo "$FUNCTION_ORIGINAL"`/`echo "$FUNCTION_RENAMED"`/" |\
  65.         sed "/`echo "$FUNCTION_RENAMED"`/ i \\
  66. `echo "// $BLOCK_START"`\\
  67. `echo "// modified by \\"$(readlink -f $0)\\""`\\
  68. `echo "include \\"$MY_GET_ERROR_PAGE\\";"`\\
  69. `echo "// $BLOCK_END"`\\
  70. " >$SGERROR_FILE
  71.         echo "#### OK"
  72.     else
  73.         echo -e "####\n \"$FUNCTION_ORIGINAL...\"\n  Function to replace not found"
  74.         ERROR=15
  75.         if [ "`echo "$SGERROR_TMP" | grep "include \\"$MY_GET_ERROR_PAGE\\";"`" ]; then
  76.             echo -e " Your personal function is active.\n  include \"$MY_GET_ERROR_PAGE\";"
  77.         ERROR=0
  78.         fi
  79.         echo "####"
  80.     fi
  81. ;;
  82. uninstall|u)
  83.     if [ "`echo "$SGERROR_TMP" | grep "$BLOCK_START"`" -a \
  84.     "`echo "$SGERROR_TMP" | grep "include \\"$MY_GET_ERROR_PAGE\\";"`" -a \
  85.     "`echo "$SGERROR_TMP" | grep "$BLOCK_END"`" ]; then
  86.         echo "$SGERROR_TMP" |\
  87.         sed "/`echo "$BLOCK_START"`/,/`echo "$BLOCK_END"`/d" |\
  88.         sed "s/`echo "$FUNCTION_RENAMED"`/`echo "$FUNCTION_ORIGINAL"`/" >$SGERROR_FILE
  89.         echo "#### OK"
  90.     else
  91.         echo -e "####\nnot found\n // $BLOCK_START\n include \"$MY_GET_ERROR_PAGE\";\n // $BLOCK_END"
  92.         ERROR=20
  93.  
  94.         if [ "`echo "$SGERROR_TMP" | grep "$FUNCTION_ORIGINAL"`" ]; then
  95.             echo -e " ####\n the function for the error page is \"$FUNCTION_ORIGINAL...\""
  96.         else
  97.             echo -e " Not found \"$FUNCTION_ORIGINAL...\"\n not found include \"$MY_GET_ERROR_PAGE\";"
  98.             echo " ? ?? ???? ?????? ????????"
  99.             ERROR=100
  100.         fi
  101.         echo "####"
  102.     fi
  103. ;;
  104. *)
  105.     cat <<USE_H
  106. ####
  107.   $0 [i|install] [u|uninstall]
  108. ####
  109.   `basename $0` i|install
  110.     Edit the file "$SGERROR_FILE"
  111.     Rename the function "$FUNCTION_ORIGINAL..."
  112.     and replaces it with the file "$MY_GET_ERROR_PAGE"
  113.  
  114.  ####
  115.   `basename $0` u|uninstall
  116.     Removes the changes to the file "$SGERROR_FILE"
  117.     and restores the original function "$FUNCTION_ORIGINAL..."
  118. ####
  119. USE_H
  120.     ERROR=1
  121. ;;
  122. esac
  123. exit $ERROR
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement