v-change-sys-api 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. check_api_download=$(cat $HESTIA/web/api/index.php)
  33. if [ -z "$HESTIA/web/api/index.php" ]; then
  34. # Throw error message to user
  35. echo "ERROR: API installation failed."
  36. # Remove empty file created by wget output
  37. rm -f "$HESTIA/web/api/index.php"
  38. exit 1
  39. fi
  40. else
  41. sed -i 's|die("Error: Disabled");|//die("Error: Disabled");|g' $HESTIA/web/api/index.php
  42. sed -i 's|////|//|g' $HESTIA/web/api/index.php
  43. fi
  44. $HESTIA/bin/v-change-sys-config-value "API" "yes"
  45. else
  46. $HESTIA/bin/v-change-sys-config-value "API" "no"
  47. $HESTIA/bin/v-change-sys-config-value "API_ALLOWED_IP" ""
  48. if [ "$status" != "remove" ]; then
  49. sed -i 's|//die("Error: Disabled");|die("Error: Disabled");|g' $HESTIA/web/api/index.php
  50. fi
  51. fi
  52. if [ "$status" = "remove" ]; then
  53. if [ ! -f "$HESTIA/web/api/index.php" ]; then
  54. echo "ERROR: API is not installed."
  55. exit 1
  56. else
  57. rm -f "$HESTIA/web/api/index.php"
  58. fi
  59. fi
  60. #----------------------------------------------------------#
  61. # Logging #
  62. #----------------------------------------------------------#
  63. if [ "$status" = "enable" ] || [ "$status" = "disable" ]; then
  64. log_history "API status has been changed to $status" '' 'admin'
  65. else
  66. log_history "API has been disabled and removed" '' 'admin'
  67. fi
  68. log_event "$OK" "$ARGUMENTS"