freedombone-app-akaunting 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Personal or small business accounts
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. VARIANTS='full full-vim media'
  29. IN_DEFAULT_INSTALL=0
  30. SHOW_ON_ABOUT=1
  31. AKAUNTING_DOMAIN_NAME=
  32. AKAUNTING_CODE=
  33. AKAUNTING_ONION_PORT=8341
  34. AKAUNTING_REPO="https://github.com/akaunting/akaunting"
  35. AKAUNTING_COMMIT='b68c840cded60d92b86e98fcbc26ce338cf93fef'
  36. AKAUNTING_ADMIN_PASSWORD=
  37. AKAUNTING_BACKGROUND_IMAGE_URL=
  38. akaunting_variables=(ONION_ONLY
  39. AKAUNTING_DOMAIN_NAME
  40. AKAUNTING_CODE
  41. DDNS_PROVIDER
  42. MY_USERNAME)
  43. function akaunting_remove_bad_links {
  44. cd "/var/www/$AKAUNTING_DOMAIN_NAME/htdocs" || exit 365873658
  45. # copy jquery locally
  46. jquery_version='1.12.4'
  47. if [ ! -f jquery-${jquery_version}.js ]; then
  48. cd "/var/www/$AKAUNTING_DOMAIN_NAME/htdocs" || exit 3276324
  49. wget https://code.jquery.com/jquery-${jquery_version}.js
  50. jquery_hash=$(sha256sum jquery-${jquery_version}.js | awk -F ' ' '{print $1}')
  51. if [[ "$jquery_hash" != '430f36f9b5f21aae8cc9dca6a81c4d3d84da5175eaedcf2fdc2c226302cb3575' ]]; then
  52. echo $'Unexpected jquery hash value'
  53. exit 78363527
  54. fi
  55. fi
  56. sed -i '/googleapi/d' resources/assets/sass/app.scss
  57. sed -i "s|ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js|$AKAUNTING_DOMAIN_NAME/jquery-${jquery_version}.js|g" vendor/league/flysystem/docs/_layouts/default.html
  58. sed -i '/googleapi/d' vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/views/503.blade.php
  59. sed -i '/googleapi/d' vendor/almasaeed2010/adminlte/build/less/AdminLTE-without-plugins.less
  60. sed -i '/googleapi/d' vendor/almasaeed2010/adminlte/build/less/AdminLTE.less
  61. sed -i '/googleapi/d' vendor/almasaeed2010/adminlte/dist/css/AdminLTE.css
  62. sed -i '/googleapi/d' vendor/almasaeed2010/adminlte/dist/css/AdminLTE.min.css
  63. sed -i '/googleapi/d' vendor/almasaeed2010/adminlte/dist/css/alt/AdminLTE-without-plugins.css
  64. sed -i '/googleapi/d' vendor/almasaeed2010/adminlte/dist/css/alt/AdminLTE-without-plugins.min.css
  65. sed -i "s|ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js|$AKAUNTING_DOMAIN_NAME/jquery-${jquery_version}.js|g" vendor/almasaeed2010/adminlte/plugins/ckeditor/samples/old/jquery.html
  66. chown -R www-data:www-data "/var/www/$AKAUNTING_DOMAIN_NAME/htdocs"
  67. }
  68. function logging_on_akaunting {
  69. echo -n ''
  70. }
  71. function logging_off_akaunting {
  72. echo -n ''
  73. }
  74. function remove_user_akaunting {
  75. remove_username="$1"
  76. "${PROJECT_NAME}-pass" -u "$remove_username" --rmapp akaunting
  77. }
  78. function add_user_akaunting {
  79. new_username="$1"
  80. new_user_password="$2"
  81. "${PROJECT_NAME}-pass" -u "$new_username" -a akaunting -p "$new_user_password"
  82. echo '0'
  83. }
  84. function install_interactive_akaunting {
  85. if [ ! "$ONION_ONLY" ]; then
  86. ONION_ONLY='no'
  87. fi
  88. if [[ "$ONION_ONLY" != "no" ]]; then
  89. AKAUNTING_DOMAIN_NAME='akaunting.local'
  90. else
  91. AKAUNTING_DETAILS_COMPLETE=
  92. while [ ! $AKAUNTING_DETAILS_COMPLETE ]
  93. do
  94. data=$(mktemp 2>/dev/null)
  95. if [[ $DDNS_PROVIDER == *"freedns"* ]]; then
  96. dialog --backtitle $"Freedombone Configuration" \
  97. --title $"Akaunting Configuration" \
  98. --form $"\\nPlease enter your Akaunting details.\\n\\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 14 65 2 \
  99. $"Domain:" 1 1 "$(grep 'AKAUNTING_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
  100. $"Code:" 2 1 "$(grep 'AKAUNTING_CODE' temp.cfg | awk -F '=' '{print $2}')" 2 25 33 255 \
  101. 2> "$data"
  102. else
  103. dialog --backtitle $"Freedombone Configuration" \
  104. --title $"Akaunting Configuration" \
  105. --form $"\\nPlease enter your Akaunting details.\\n\\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 14 65 2 \
  106. $"Domain:" 1 1 "$(grep 'AKAUNTING_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
  107. 2> "$data"
  108. fi
  109. sel=$?
  110. case $sel in
  111. 1) rm -f "$data"
  112. exit 1;;
  113. 255) rm -f "$data"
  114. exit 1;;
  115. esac
  116. AKAUNTING_DOMAIN_NAME=$(sed -n 1p < "$data")
  117. if [ "$AKAUNTING_DOMAIN_NAME" ]; then
  118. if [[ $AKAUNTING_DOMAIN_NAME == "$HUBZILLA_DOMAIN_NAME" ]]; then
  119. AKAUNTING_DOMAIN_NAME=""
  120. fi
  121. TEST_DOMAIN_NAME=$AKAUNTING_DOMAIN_NAME
  122. validate_domain_name
  123. if [[ "$TEST_DOMAIN_NAME" != "$AKAUNTING_DOMAIN_NAME" ]]; then
  124. AKAUNTING_DOMAIN_NAME=
  125. dialog --title $"Domain name validation" --msgbox "$TEST_DOMAIN_NAME" 15 50
  126. else
  127. if [[ $DDNS_PROVIDER == *"freedns"* ]]; then
  128. AKAUNTING_CODE=$(sed -n 2p < "$data")
  129. validate_freedns_code "$AKAUNTING_CODE"
  130. if [ ! "$VALID_CODE" ]; then
  131. AKAUNTING_DOMAIN_NAME=
  132. fi
  133. fi
  134. fi
  135. fi
  136. if [ $AKAUNTING_DOMAIN_NAME ]; then
  137. AKAUNTING_DETAILS_COMPLETE="yes"
  138. fi
  139. rm -f "$data"
  140. done
  141. write_config_param "AKAUNTING_CODE" "$AKAUNTING_CODE"
  142. fi
  143. write_config_param "AKAUNTING_DOMAIN_NAME" "$AKAUNTING_DOMAIN_NAME"
  144. APP_INSTALLED=1
  145. }
  146. function change_password_akaunting {
  147. curr_username="$1"
  148. new_user_password="$2"
  149. read_config_param 'AKAUNTING_DOMAIN_NAME'
  150. "${PROJECT_NAME}-pass" -u "$curr_username" -a akaunting -p "$new_user_password"
  151. }
  152. function akaunting_create_database {
  153. if [ -f "$IMAGE_PASSWORD_FILE" ]; then
  154. AKAUNTING_ADMIN_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
  155. else
  156. if [ ! "$AKAUNTING_ADMIN_PASSWORD" ]; then
  157. AKAUNTING_ADMIN_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  158. fi
  159. fi
  160. if [ ! "$AKAUNTING_ADMIN_PASSWORD" ]; then
  161. return
  162. fi
  163. function_check create_database
  164. create_database akaunting "$AKAUNTING_ADMIN_PASSWORD" "$MY_USERNAME"
  165. }
  166. function reconfigure_akaunting {
  167. echo -n ''
  168. }
  169. function upgrade_akaunting {
  170. CURR_AKAUNTING_COMMIT=$(get_completion_param "akaunting commit")
  171. if [[ "$CURR_AKAUNTING_COMMIT" == "$AKAUNTING_COMMIT" ]]; then
  172. return
  173. fi
  174. if grep -q "akaunting domain" "$COMPLETION_FILE"; then
  175. AKAUNTING_DOMAIN_NAME=$(get_completion_param "akaunting domain")
  176. fi
  177. # update to the next commit
  178. function_check set_repo_commit
  179. set_repo_commit "/var/www/$AKAUNTING_DOMAIN_NAME/htdocs" "akaunting commit" "$AKAUNTING_COMMIT" "$AKAUNTING_REPO"
  180. cd "/var/www/$AKAUNTING_DOMAIN_NAME/htdocs" || exit 367835
  181. install_composer
  182. akaunting_remove_bad_links
  183. chown -R www-data:www-data "/var/www/${AKAUNTING_DOMAIN_NAME}/htdocs"
  184. }
  185. function backup_local_akaunting {
  186. akaunting_path="/var/www/${AKAUNTING_DOMAIN_NAME}/htdocs"
  187. if [ -d "$akaunting_path" ]; then
  188. suspend_site "${AKAUNTING_DOMAIN_NAME}"
  189. function_check backup_database_to_usb
  190. backup_database_to_usb akaunting
  191. backup_directory_to_usb "$akaunting_path" akaunting
  192. restart_site
  193. fi
  194. }
  195. function restore_local_akaunting {
  196. temp_restore_dir=/root/tempakaunting
  197. akaunting_dir="/var/www/${AKAUNTING_DOMAIN_NAME}/htdocs"
  198. suspend_site "${AKAUNTING_DOMAIN_NAME}"
  199. function_check akaunting_create_database
  200. akaunting_create_database
  201. restore_database akaunting "${AKAUNTING_DOMAIN_NAME}"
  202. chown www-data:www-data "$akaunting_dir"
  203. restart_site
  204. }
  205. function backup_remote_akaunting {
  206. akaunting_path="/var/www/${AKAUNTING_DOMAIN_NAME}/htdocs"
  207. if [ -d "$akaunting_path" ]; then
  208. suspend_site "${AKAUNTING_DOMAIN_NAME}"
  209. function_check backup_database_to_friend
  210. backup_database_to_friend akaunting
  211. backup_directory_to_friend "$akaunting_path" akaunting
  212. restart_site
  213. fi
  214. }
  215. function restore_remote_akaunting {
  216. akaunting_dir="/var/www/${AKAUNTING_DOMAIN_NAME}/htdocs"
  217. suspend_site "${AKAUNTING_DOMAIN_NAME}"
  218. function_check akaunting_create_database
  219. akaunting_create_database
  220. restore_database_from_friend akaunting "${AKAUNTING_DOMAIN_NAME}"
  221. chown www-data:www-data "$akaunting_dir"
  222. restart_site
  223. }
  224. function remove_akaunting {
  225. if [ ${#AKAUNTING_DOMAIN_NAME} -eq 0 ]; then
  226. return
  227. fi
  228. read_config_param "AKAUNTING_DOMAIN_NAME"
  229. read_config_param "MY_USERNAME"
  230. echo "Removing $AKAUNTING_DOMAIN_NAME"
  231. nginx_dissite "$AKAUNTING_DOMAIN_NAME"
  232. remove_certs "$AKAUNTING_DOMAIN_NAME"
  233. if [ -d "/var/www/$AKAUNTING_DOMAIN_NAME" ]; then
  234. rm -rf "/var/www/$AKAUNTING_DOMAIN_NAME"
  235. fi
  236. if [ -f "/etc/nginx/sites-available/$AKAUNTING_DOMAIN_NAME" ]; then
  237. rm "/etc/nginx/sites-available/$AKAUNTING_DOMAIN_NAME"
  238. fi
  239. function_check drop_database
  240. drop_database akaunting
  241. function_check remove_onion_service
  242. remove_onion_service akaunting "${AKAUNTING_ONION_PORT}"
  243. if grep -q "akaunting" /etc/crontab; then
  244. sed -i "/akaunting/d" /etc/crontab
  245. fi
  246. remove_app akaunting
  247. remove_completion_param install_akaunting
  248. sed -i '/akaunting/d' "$COMPLETION_FILE"
  249. function_check remove_ddns_domain
  250. remove_ddns_domain "$AKAUNTING_DOMAIN_NAME"
  251. }
  252. function install_akaunting {
  253. if [ ! $ONION_ONLY ]; then
  254. ONION_ONLY='no'
  255. fi
  256. if [ ! "$AKAUNTING_DOMAIN_NAME" ]; then
  257. echo $'No domain name was given for akaunting'
  258. exit 89353
  259. fi
  260. function_check install_mariadb
  261. install_mariadb
  262. function_check get_mariadb_password
  263. get_mariadb_password
  264. apt-get -yq install php-gettext php-curl php-gd php-mysql git curl
  265. apt-get -yq install memcached php-memcached php-intl exiftool libfcgi0ldbl
  266. if [ ! -d "/var/www/$AKAUNTING_DOMAIN_NAME" ]; then
  267. mkdir "/var/www/$AKAUNTING_DOMAIN_NAME"
  268. fi
  269. if [ ! -d "/var/www/$AKAUNTING_DOMAIN_NAME/htdocs" ]; then
  270. if [ -d /repos/akaunting ]; then
  271. mkdir "/var/www/$AKAUNTING_DOMAIN_NAME/htdocs"
  272. cp -r -p /repos/akaunting/. "/var/www/$AKAUNTING_DOMAIN_NAME/htdocs"
  273. cd "/var/www/$AKAUNTING_DOMAIN_NAME/htdocs" || exit 23458735
  274. git pull
  275. else
  276. function_check git_clone
  277. git_clone "$AKAUNTING_REPO" "/var/www/$AKAUNTING_DOMAIN_NAME/htdocs"
  278. fi
  279. if [ ! -d "/var/www/$AKAUNTING_DOMAIN_NAME/htdocs" ]; then
  280. echo $'Unable to clone akaunting repo'
  281. exit 2589389
  282. fi
  283. fi
  284. cd "/var/www/$AKAUNTING_DOMAIN_NAME/htdocs" || exit 23468724527
  285. git checkout "$AKAUNTING_COMMIT" -b "$AKAUNTING_COMMIT"
  286. set_completion_param "akaunting commit" "$AKAUNTING_COMMIT"
  287. chmod g+w "/var/www/$AKAUNTING_DOMAIN_NAME/htdocs"
  288. chown -R www-data:www-data "/var/www/$AKAUNTING_DOMAIN_NAME/htdocs"
  289. cd "/var/www/$AKAUNTING_DOMAIN_NAME/htdocs" || exit 34783524
  290. install_composer
  291. function_check akaunting_create_database
  292. akaunting_create_database
  293. function_check add_ddns_domain
  294. add_ddns_domain "$AKAUNTING_DOMAIN_NAME"
  295. AKAUNTING_ONION_HOSTNAME=$(add_onion_service akaunting 80 ${AKAUNTING_ONION_PORT})
  296. akaunting_nginx_site=/etc/nginx/sites-available/$AKAUNTING_DOMAIN_NAME
  297. if [[ $ONION_ONLY == "no" ]]; then
  298. function_check nginx_http_redirect
  299. nginx_http_redirect "$AKAUNTING_DOMAIN_NAME" "index index.php"
  300. { echo 'server {';
  301. echo ' listen 443 ssl;';
  302. echo ' #listen [::]:443 ssl;';
  303. echo " server_name $AKAUNTING_DOMAIN_NAME;";
  304. echo ''; } >> "$akaunting_nginx_site"
  305. function_check nginx_compress
  306. nginx_compress "$AKAUNTING_DOMAIN_NAME"
  307. echo '' >> "$akaunting_nginx_site"
  308. echo ' # Security' >> "$akaunting_nginx_site"
  309. function_check nginx_ssl
  310. nginx_ssl "$AKAUNTING_DOMAIN_NAME"
  311. function_check nginx_security_options
  312. nginx_security_options "$AKAUNTING_DOMAIN_NAME"
  313. { echo ' add_header Strict-Transport-Security max-age=15768000;';
  314. echo '';
  315. echo ' access_log /dev/null;';
  316. echo ' error_log /dev/null;';
  317. echo '';
  318. echo " root /var/www/$AKAUNTING_DOMAIN_NAME/htdocs;";
  319. echo '';
  320. echo ' index index.php;';
  321. echo '';
  322. echo ' location ~ \.php {';
  323. echo ' include snippets/fastcgi-php.conf;';
  324. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  325. echo ' fastcgi_read_timeout 30;';
  326. echo ' }';
  327. echo '';
  328. echo ' location / {'; } >> "$akaunting_nginx_site"
  329. function_check nginx_limits
  330. nginx_limits "$AKAUNTING_DOMAIN_NAME" '15m'
  331. { echo " try_files \$uri \$uri/ @akaunting;";
  332. echo ' }';
  333. echo '';
  334. echo ' location @akaunting {';
  335. echo " rewrite ^(.*)\$ /index.php?p=\$1 last;";
  336. echo ' }';
  337. echo '';
  338. echo ' location ~ /\.(ht|git) {';
  339. echo ' deny all;';
  340. echo ' }';
  341. echo '';
  342. echo '}'; } >> "$akaunting_nginx_site"
  343. else
  344. echo -n '' > "$akaunting_nginx_site"
  345. fi
  346. { echo 'server {';
  347. echo " listen 127.0.0.1:$AKAUNTING_ONION_PORT default_server;";
  348. echo " server_name $AKAUNTING_ONION_HOSTNAME;";
  349. echo ''; } >> "$akaunting_nginx_site"
  350. function_check nginx_compress
  351. nginx_compress "$AKAUNTING_DOMAIN_NAME"
  352. echo '' >> "$akaunting_nginx_site"
  353. function_check nginx_security_options
  354. nginx_security_options "$AKAUNTING_DOMAIN_NAME"
  355. { echo '';
  356. echo ' access_log /dev/null;';
  357. echo ' error_log /dev/null;';
  358. echo '';
  359. echo " root /var/www/$AKAUNTING_DOMAIN_NAME/htdocs;";
  360. echo '';
  361. echo ' index index.php;';
  362. echo '';
  363. echo ' location ~ \.php {';
  364. echo ' include snippets/fastcgi-php.conf;';
  365. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  366. echo ' fastcgi_read_timeout 30;';
  367. echo ' }';
  368. echo '';
  369. echo ' location / {'; } >> "$akaunting_nginx_site"
  370. function_check nginx_limits
  371. nginx_limits "$AKAUNTING_DOMAIN_NAME" '15m'
  372. { echo " try_files \$uri \$uri/ @akaunting;";
  373. echo ' }';
  374. echo '';
  375. echo ' location @akaunting {';
  376. echo " rewrite ^(.*)\$ /index.php?p=\$1 last;";
  377. echo ' }';
  378. echo '';
  379. echo ' location ~ /\.(ht|git) {';
  380. echo ' deny all;';
  381. echo ' }';
  382. echo '}'; } >> "$akaunting_nginx_site"
  383. function_check configure_php
  384. configure_php
  385. function_check create_site_certificate
  386. create_site_certificate "$AKAUNTING_DOMAIN_NAME" 'yes'
  387. function_check nginx_ensite
  388. nginx_ensite "$AKAUNTING_DOMAIN_NAME"
  389. akaunting_remove_bad_links
  390. systemctl restart mariadb
  391. systemctl restart php7.0-fpm
  392. systemctl restart nginx
  393. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a akaunting -p "$AKAUNTING_ADMIN_PASSWORD"
  394. set_completion_param "akaunting domain" "$AKAUNTING_DOMAIN_NAME"
  395. APP_INSTALLED=1
  396. }
  397. # NOTE: deliberately there is no "exit 0"