_common.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/bin/bash
  2. #=================================================
  3. # COMMON VARIABLES
  4. #=================================================
  5. YNH_PHP_VERSION="7.3"
  6. pkg_dependencies="php${YNH_PHP_VERSION}-mbstring php${YNH_PHP_VERSION}-fpm php${YNH_PHP_VERSION}-common php${YNH_PHP_VERSION}-curl php${YNH_PHP_VERSION}-intl php${YNH_PHP_VERSION}-xmlrpc php${YNH_PHP_VERSION}-mysql php${YNH_PHP_VERSION}-gd php${YNH_PHP_VERSION}-xml php${YNH_PHP_VERSION}-cli php${YNH_PHP_VERSION}-zip"
  7. #=================================================
  8. # PERSONAL HELPERS
  9. #=================================================
  10. #=================================================
  11. # EXPERIMENTAL HELPERS
  12. #=================================================
  13. #=================================================
  14. # FUTURE OFFICIAL HELPERS
  15. #=================================================
  16. # Send an email to inform the administrator
  17. #
  18. # usage: ynh_send_readme_to_admin --app_message=app_message [--recipients=recipients] [--type=type]
  19. # | arg: -m --app_message= - The file with the content to send to the administrator.
  20. # | arg: -r, --recipients= - The recipients of this email. Use spaces to separate multiples recipients. - default: root
  21. # example: "root admin@domain"
  22. # If you give the name of a YunoHost user, ynh_send_readme_to_admin will find its email adress for you
  23. # example: "root admin@domain user1 user2"
  24. # | arg: -t, --type= - Type of mail, could be 'backup', 'change_url', 'install', 'remove', 'restore', 'upgrade'
  25. #
  26. # Requires YunoHost version 4.1.0 or higher.
  27. ynh_send_readme_to_admin() {
  28. # Declare an array to define the options of this helper.
  29. declare -Ar args_array=( [m]=app_message= [r]=recipients= [t]=type= )
  30. local app_message
  31. local recipients
  32. local type
  33. # Manage arguments with getopts
  34. ynh_handle_getopts_args "$@"
  35. app_message="${app_message:-}"
  36. recipients="${recipients:-root}"
  37. type="${type:-install}"
  38. # Get the value of admin_mail_html
  39. admin_mail_html=$(ynh_app_setting_get $app admin_mail_html)
  40. admin_mail_html="${admin_mail_html:-0}"
  41. # Retrieve the email of users
  42. find_mails () {
  43. local list_mails="$1"
  44. local mail
  45. local recipients=" "
  46. # Read each mail in argument
  47. for mail in $list_mails
  48. do
  49. # Keep root or a real email address as it is
  50. if [ "$mail" = "root" ] || echo "$mail" | grep --quiet "@"
  51. then
  52. recipients="$recipients $mail"
  53. else
  54. # But replace an user name without a domain after by its email
  55. if mail=$(ynh_user_get_info "$mail" "mail" 2> /dev/null)
  56. then
  57. recipients="$recipients $mail"
  58. fi
  59. fi
  60. done
  61. echo "$recipients"
  62. }
  63. recipients=$(find_mails "$recipients")
  64. # Subject base
  65. local mail_subject="☁️🆈🅽🅷☁️: \`$app\`"
  66. # Adapt the subject according to the type of mail required.
  67. if [ "$type" = "backup" ]; then
  68. mail_subject="$mail_subject has just been backup."
  69. elif [ "$type" = "change_url" ]; then
  70. mail_subject="$mail_subject has just been moved to a new URL!"
  71. elif [ "$type" = "remove" ]; then
  72. mail_subject="$mail_subject has just been removed!"
  73. elif [ "$type" = "restore" ]; then
  74. mail_subject="$mail_subject has just been restored!"
  75. elif [ "$type" = "upgrade" ]; then
  76. mail_subject="$mail_subject has just been upgraded!"
  77. else # install
  78. mail_subject="$mail_subject has just been installed!"
  79. fi
  80. ynh_add_config --template="$app_message" --destination="../conf/msg_to_send"
  81. ynh_delete_file_checksum --file="../conf/msg_to_send"
  82. local mail_message="This is an automated message from your beloved YunoHost server.
  83. Specific information for the application $app.
  84. $(cat "../conf/msg_to_send")"
  85. # Store the message into a file for further modifications.
  86. echo "$mail_message" > mail_to_send
  87. # If a html email is required. Apply html tags to the message.
  88. if [ "$admin_mail_html" -eq 1 ]
  89. then
  90. # Insert 'br' tags at each ending of lines.
  91. ynh_replace_string "$" "<br>" mail_to_send
  92. # Insert starting HTML tags
  93. sed --in-place '1s@^@<!DOCTYPE html>\n<html>\n<head></head>\n<body>\n@' mail_to_send
  94. # Keep tabulations
  95. ynh_replace_string " " "\&#160;\&#160;" mail_to_send
  96. ynh_replace_string "\t" "\&#160;\&#160;" mail_to_send
  97. # Insert url links tags
  98. ynh_replace_string "__URL_TAG1__\(.*\)__URL_TAG2__\(.*\)__URL_TAG3__" "<a href=\"\2\">\1</a>" mail_to_send
  99. # Insert finishing HTML tags
  100. echo -e "\n</body>\n</html>" >> mail_to_send
  101. # Otherwise, remove tags to keep a plain text.
  102. else
  103. # Remove URL tags
  104. ynh_replace_string "__URL_TAG[1,3]__" "" mail_to_send
  105. ynh_replace_string "__URL_TAG2__" ": " mail_to_send
  106. fi
  107. # Define binary to use for mail command
  108. if [ -e /usr/bin/bsd-mailx ]
  109. then
  110. local mail_bin=/usr/bin/bsd-mailx
  111. else
  112. local mail_bin=/usr/bin/mail.mailutils
  113. fi
  114. if [ "$admin_mail_html" -eq 1 ]
  115. then
  116. content_type="text/html"
  117. else
  118. content_type="text/plain"
  119. fi
  120. # Send the email to the recipients
  121. cat mail_to_send | $mail_bin -a "Content-Type: $content_type; charset=UTF-8" -s "$mail_subject" "$recipients"
  122. }