v-change-sys-api 3.1 KB

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