v-change-sys-api 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/bin/bash
  2. # info: Enable / Disable API access
  3. # options: STATUS
  4. # labels: hestia
  5. #
  6. # example: v-change-sys-api enable
  7. # # Enable API
  8. #
  9. # example: v-change-sys-api disable
  10. # # Disable API
  11. #
  12. # Enabled / Disable API
  13. status=$1
  14. # Includes
  15. # shellcheck source=/usr/local/hestia/func/main.sh
  16. source $HESTIA/func/main.sh
  17. # shellcheck source=/usr/local/hestia/conf/hestia.conf
  18. source $HESTIA/conf/hestia.conf
  19. #----------------------------------------------------------#
  20. # Variable&Function #
  21. #----------------------------------------------------------#
  22. check_args '1' "$#" "STATUS"
  23. is_type_valid "enable,disable,remove" "$status"
  24. # Perform verification if read-only mode is enabled
  25. check_hestia_demo_mode
  26. #----------------------------------------------------------#
  27. # Action #
  28. #----------------------------------------------------------#
  29. if [ "$status" = "enable" ]; then
  30. if [ ! -f "$HESTIA/web/api/index.php" ]; then
  31. wget -q https://raw.githubusercontent.com/hestiacp/hestiacp/$RELEASE_BRANCH/web/api/index.php -O $HESTIA/web/api/index.php
  32. if [ ! -s $HESTIA/web/api/index.php ]; then
  33. wget -q https://raw.githubusercontent.com/hestiacp/hestiacp/release/web/api/index.php -O $HESTIA/web/api/index.php
  34. if [ ! -s $HESTIA/web/api/index.php ]; then
  35. # Throw error message to user
  36. echo "ERROR: API installation failed."
  37. # Remove empty file created by wget output
  38. rm -f "$HESTIA/web/api/index.php"
  39. exit 1
  40. fi
  41. fi
  42. else
  43. sed -i 's|die("Error: Disabled");|//die("Error: Disabled");|g' $HESTIA/web/api/index.php
  44. sed -i 's|////|//|g' $HESTIA/web/api/index.php
  45. fi
  46. $HESTIA/bin/v-change-sys-config-value "API" "yes"
  47. else
  48. $HESTIA/bin/v-change-sys-config-value "API" "no"
  49. $HESTIA/bin/v-change-sys-config-value "API_ALLOWED_IP" ""
  50. if [ "$status" != "remove" ]; then
  51. sed -i 's|//die("Error: Disabled");|die("Error: Disabled");|g' $HESTIA/web/api/index.php
  52. fi
  53. fi
  54. if [ "$status" = "remove" ]; then
  55. if [ ! -f "$HESTIA/web/api/index.php" ]; then
  56. echo "ERROR: API is not installed."
  57. exit 1
  58. else
  59. rm -f "$HESTIA/web/api/index.php"
  60. fi
  61. fi
  62. #----------------------------------------------------------#
  63. # Logging #
  64. #----------------------------------------------------------#
  65. if [ "$status" = "enable" ]; then
  66. $BIN/v-log-action "system" "Warning" "System" "System API access enabled."
  67. else
  68. $BIN/v-log-action "system" "Info" "System" "System API access disabled."
  69. fi
  70. log_event "$OK" "$ARGUMENTS"