freedombone-app-akaunting 16KB

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