freedombone-app-lychee 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Lychee photo album
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2014-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 writer"
  29. IN_DEFAULT_INSTALL=0
  30. SHOW_ON_ABOUT=1
  31. LYCHEE_DOMAIN_NAME=
  32. LYCHEE_CODE=
  33. LYCHEE_ONION_PORT=8105
  34. LYCHEE_REPO="https://github.com/electerious/Lychee"
  35. LYCHEE_COMMIT='27f207dcbac8488629ffc3b5a9cac78ae123bee9'
  36. LYCHEE_SHORT_DESCRIPTION=$'Photo album'
  37. LYCHEE_DESCRIPTION=$'Photo album'
  38. LYCHEE_MOBILE_APP_URL=
  39. lychee_variables=(LYCHEE_REPO
  40. LYCHEE_DOMAIN_NAME
  41. LYCHEE_CODE
  42. ONION_ONLY
  43. DDNS_PROVIDER
  44. MY_USERNAME)
  45. function logging_on_lychee {
  46. echo -n ''
  47. }
  48. function logging_off_lychee {
  49. echo -n ''
  50. }
  51. function lychee_create_database {
  52. if [ -f "${IMAGE_PASSWORD_FILE}" ]; then
  53. LYCHEE_ADMIN_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
  54. else
  55. if [ ! "${LYCHEE_ADMIN_PASSWORD}" ]; then
  56. LYCHEE_ADMIN_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  57. fi
  58. fi
  59. if [ ! "$LYCHEE_ADMIN_PASSWORD" ]; then
  60. return
  61. fi
  62. function_check create_database
  63. create_database lychee "$LYCHEE_ADMIN_PASSWORD"
  64. }
  65. function remove_user_lychee {
  66. remove_username="$1"
  67. "${PROJECT_NAME}-pass" -u "$remove_username" --rmapp lychee
  68. }
  69. function add_user_lychee {
  70. if [[ $(app_is_installed lychee) == "0" ]]; then
  71. echo '0'
  72. return
  73. fi
  74. new_username="$1"
  75. new_user_password="$2"
  76. "${PROJECT_NAME}-pass" -u "$new_username" -a lychee -p "$new_user_password"
  77. echo '0'
  78. }
  79. function install_interactive_lychee {
  80. if [ ! "$ONION_ONLY" ]; then
  81. ONION_ONLY='no'
  82. fi
  83. if [[ $ONION_ONLY != "no" ]]; then
  84. LYCHEE_DOMAIN_NAME='lychee.local'
  85. write_config_param "LYCHEE_DOMAIN_NAME" "$LYCHEE_DOMAIN_NAME"
  86. else
  87. function_check interactive_site_details
  88. interactive_site_details "lychee" "LYCHEE_DOMAIN_NAME" "LYCHEE_CODE"
  89. fi
  90. APP_INSTALLED=1
  91. }
  92. function configure_interactive_lychee {
  93. function_check get_mariadb_password
  94. get_mariadb_password
  95. dialog --title $"Lychee Configuration" \
  96. --msgbox $"\\nYou can initially install the system with:\\n\\n Username: root\\n Password: $MARIADB_PASSWORD" 10 70
  97. }
  98. function change_password_lychee {
  99. # LYCHEE_USERNAME="$1"
  100. LYCHEE_PASSWORD="$2"
  101. if [ ${#LYCHEE_PASSWORD} -lt 8 ]; then
  102. echo $'Lychee password is too short'
  103. return
  104. fi
  105. # TODO: This doesn't actually change the password
  106. #${PROJECT_NAME}-pass -u $LYCHEE_USERNAME -a lychee -p "$LYCHEE_PASSWORD"
  107. }
  108. function reconfigure_lychee {
  109. echo -n ''
  110. }
  111. function upgrade_lychee {
  112. CURR_LYCHEE_COMMIT=$(get_completion_param "lychee commit")
  113. if [[ "$CURR_LYCHEE_COMMIT" == "$LYCHEE_COMMIT" ]]; then
  114. return
  115. fi
  116. read_config_param "LYCHEE_DOMAIN_NAME"
  117. function_check set_repo_commit
  118. set_repo_commit /var/www/$LYCHEE_DOMAIN_NAME/htdocs "lychee commit" "$LYCHEE_COMMIT" $LYCHEE_REPO
  119. }
  120. function backup_local_lychee {
  121. LYCHEE_DOMAIN_NAME='lychee.local'
  122. if grep -q "lychee domain" "$COMPLETION_FILE"; then
  123. LYCHEE_DOMAIN_NAME=$(get_completion_param "lychee domain")
  124. fi
  125. lychee_path="/var/www/${LYCHEE_DOMAIN_NAME}/htdocs"
  126. if [ -d "$lychee_path" ]; then
  127. function_check backup_database_to_usb
  128. backup_database_to_usb lychee
  129. backup_directory_to_usb "$lychee_path" lychee
  130. restart_site
  131. fi
  132. }
  133. function restore_local_lychee {
  134. LYCHEE_DOMAIN_NAME='lychee.local'
  135. if grep -q "lychee domain" "$COMPLETION_FILE"; then
  136. LYCHEE_DOMAIN_NAME=$(get_completion_param "lychee domain")
  137. fi
  138. if [ "$LYCHEE_DOMAIN_NAME" ]; then
  139. suspend_site "${LYCHEE_DOMAIN_NAME}"
  140. function_check lychee_create_database
  141. lychee_create_database
  142. function_check restore_database
  143. restore_database lychee "${LYCHEE_DOMAIN_NAME}"
  144. if [ -f "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/data/config.php" ]; then
  145. MARIADB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
  146. sed -i "s|dbPassword.*|dbPassword = '$MARIADB_PASSWORD';|g" "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/data/config.php"
  147. MARIADB_PASSWORD=
  148. fi
  149. restart_site
  150. chown -R lychee: "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/"
  151. fi
  152. }
  153. function backup_remote_lychee {
  154. LYCHEE_DOMAIN_NAME='lychee.local'
  155. if grep -q "lychee domain" "$COMPLETION_FILE"; then
  156. LYCHEE_DOMAIN_NAME=$(get_completion_param "lychee domain")
  157. fi
  158. temp_backup_dir=/var/www/${LYCHEE_DOMAIN_NAME}/htdocs
  159. if [ -d "$temp_backup_dir" ]; then
  160. suspend_site "${LYCHEE_DOMAIN_NAME}"
  161. backup_database_to_friend lychee
  162. backup_directory_to_friend "$temp_backup_dir" lychee
  163. restart_site
  164. else
  165. echo $"Lychee domain specified but not found in /var/www/${LYCHEE_DOMAIN_NAME}"
  166. exit 2578
  167. fi
  168. }
  169. function restore_remote_lychee {
  170. LYCHEE_DOMAIN_NAME='lychee.local'
  171. if grep -q "lychee domain" "$COMPLETION_FILE"; then
  172. LYCHEE_DOMAIN_NAME=$(get_completion_param "lychee domain")
  173. fi
  174. suspend_site "${LYCHEE_DOMAIN_NAME}"
  175. function_check restore_database_from_friend
  176. function_check lychee_create_database
  177. lychee_create_database
  178. restore_database_from_friend lychee "${LYCHEE_DOMAIN_NAME}"
  179. if [ -f "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/data/config.php" ]; then
  180. MARIADB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
  181. sed -i "s|dbPassword.*|dbPassword = '$MARIADB_PASSWORD';|g" "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/data/config.php"
  182. MARIADB_PASSWORD=
  183. fi
  184. restart_site
  185. chown -R lychee: "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/"
  186. }
  187. function remove_lychee {
  188. if [ ${#LYCHEE_DOMAIN_NAME} -eq 0 ]; then
  189. return
  190. fi
  191. read_config_param "LYCHEE_DOMAIN_NAME"
  192. nginx_dissite "$LYCHEE_DOMAIN_NAME"
  193. remove_certs "${LYCHEE_DOMAIN_NAME}"
  194. drop_database lychee
  195. remove_backup_database_local lychee
  196. if [ -f "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME" ]; then
  197. rm -f "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  198. fi
  199. if [ -d "/var/www/$LYCHEE_DOMAIN_NAME" ]; then
  200. rm -rf "/var/www/$LYCHEE_DOMAIN_NAME"
  201. fi
  202. remove_config_param LYCHEE_DOMAIN_NAME
  203. remove_config_param LYCHEE_CODE
  204. function_check remove_onion_service
  205. remove_onion_service lychee "${LYCHEE_ONION_PORT}"
  206. remove_completion_param "install_lychee"
  207. sed -i '/Lychee/d' "$COMPLETION_FILE"
  208. sed -i '/lychee/d' "$COMPLETION_FILE"
  209. function_check remove_ddns_domain
  210. remove_ddns_domain "$LYCHEE_DOMAIN_NAME"
  211. }
  212. function install_lychee_website {
  213. function_check nginx_http_redirect
  214. nginx_http_redirect "$LYCHEE_DOMAIN_NAME"
  215. { echo 'server {';
  216. echo ' listen 443 ssl;';
  217. echo ' #listen [::]:443 ssl;';
  218. echo " root /var/www/$LYCHEE_DOMAIN_NAME/htdocs;";
  219. echo " server_name $LYCHEE_DOMAIN_NAME;";
  220. echo ' access_log /dev/null;';
  221. echo " error_log /dev/null;";
  222. echo ' index index.html;';
  223. echo ' charset utf-8;';
  224. echo ' proxy_read_timeout 86400s;'; } >> "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  225. function_check nginx_ssl
  226. nginx_ssl "$LYCHEE_DOMAIN_NAME"
  227. function_check nginx_security_options
  228. nginx_security_options "$LYCHEE_DOMAIN_NAME"
  229. { echo ' add_header Strict-Transport-Security "max-age=0;";';
  230. echo '';
  231. echo ' # rewrite to front controller as default rule';
  232. echo ' location / {'; } >> "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  233. function_check nginx_limits
  234. nginx_limits "$LYCHEE_DOMAIN_NAME"
  235. { echo ' }';
  236. echo '';
  237. echo ' # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000';
  238. echo ' # or a unix socket';
  239. echo ' location ~* \.php$ {';
  240. echo ' # Zero-day exploit defense.';
  241. echo ' # http://forum.nginx.org/read.php?2,88845,page=3';
  242. echo " # Won't work properly (404 error) if the file is not stored on this";
  243. echo " # server, which is entirely possible with php-fpm/php-fcgi.";
  244. echo " # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on";
  245. echo " # another machine. And then cross your fingers that you won't get hacked.";
  246. echo " try_files \$uri \$uri/ /index.html;";
  247. echo ' # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini';
  248. echo ' fastcgi_split_path_info ^(.+\.php)(/.+)$;';
  249. echo ' # With php-cgi alone:';
  250. echo ' # fastcgi_pass 127.0.0.1:9000;';
  251. echo ' # With php-fpm:';
  252. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  253. echo ' include fastcgi_params;';
  254. echo ' fastcgi_read_timeout 30;';
  255. echo ' fastcgi_index index.html;';
  256. echo " fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;";
  257. echo ' }';
  258. echo '';
  259. echo ' # deny access to all dot files';
  260. echo ' location ~ /\. {';
  261. echo ' deny all;';
  262. echo ' }';
  263. echo '';
  264. echo ' #deny access to store';
  265. echo ' location ~ /store {';
  266. echo ' deny all;';
  267. echo ' }';
  268. echo ' location ~ /(data|conf|bin|inc)/ {';
  269. echo ' deny all;';
  270. echo ' }';
  271. echo ' location ~ /\.ht {';
  272. echo ' deny all;';
  273. echo ' }';
  274. echo '}';
  275. echo ''; } >> "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  276. }
  277. function install_lychee_website_onion {
  278. { echo 'server {';
  279. echo " listen 127.0.0.1:${LYCHEE_ONION_PORT} default_server;";
  280. echo " root /var/www/$LYCHEE_DOMAIN_NAME/htdocs;";
  281. echo " server_name $LYCHEE_ONION_HOSTNAME;";
  282. echo ' access_log /dev/null;';
  283. echo " error_log /dev/null;";
  284. echo ' index index.html;';
  285. echo ' charset utf-8;';
  286. echo ' proxy_read_timeout 86400s;'; } >> "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  287. function_check nginx_security_options
  288. nginx_security_options "$LYCHEE_DOMAIN_NAME"
  289. { echo ' add_header Strict-Transport-Security "max-age=0;";';
  290. echo '';
  291. echo ' # rewrite to front controller as default rule';
  292. echo ' location / {'; } >> "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  293. function_check nginx_limits
  294. nginx_limits "$LYCHEE_DOMAIN_NAME"
  295. { echo ' }';
  296. echo '';
  297. echo ' # block these file types';
  298. echo ' location ~* \.(tpl|md|tgz|log|out)$ {';
  299. echo ' deny all;';
  300. echo ' }';
  301. echo '';
  302. echo ' # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000';
  303. echo ' # or a unix socket';
  304. echo ' location ~* \.php$ {'; } >> "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  305. function_check nginx_limits
  306. nginx_limits "$LYCHEE_DOMAIN_NAME"
  307. { echo ' # Zero-day exploit defense.';
  308. echo ' # http://forum.nginx.org/read.php?2,88845,page=3';
  309. echo " # Won't work properly (404 error) if the file is not stored on this";
  310. echo " # server, which is entirely possible with php-fpm/php-fcgi.";
  311. echo " # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on";
  312. echo " # another machine. And then cross your fingers that you won't get hacked.";
  313. echo " try_files \$uri \$uri/ /index.html;";
  314. echo ' # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini';
  315. echo ' fastcgi_split_path_info ^(.+\.php)(/.+)$;';
  316. echo ' # With php-cgi alone:';
  317. echo ' # fastcgi_pass 127.0.0.1:9000;';
  318. echo ' # With php-fpm:';
  319. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  320. echo ' include fastcgi_params;';
  321. echo ' fastcgi_read_timeout 30;';
  322. echo ' fastcgi_index index.html;';
  323. echo " fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;";
  324. echo ' }';
  325. echo '';
  326. echo ' # deny access to all dot files';
  327. echo ' location ~ /\. {';
  328. echo ' deny all;';
  329. echo ' }';
  330. echo '';
  331. echo ' #deny access to store';
  332. echo ' location ~ /store {';
  333. echo ' deny all;';
  334. echo ' }';
  335. echo ' location ~ /(data|conf|bin|inc)/ {';
  336. echo ' deny all;';
  337. echo ' }';
  338. echo ' location ~ /\.ht {';
  339. echo ' deny all;';
  340. echo ' }';
  341. echo '}'; } >> "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  342. }
  343. function install_lychee_from_repo {
  344. if [ ! -d "/var/www/$LYCHEE_DOMAIN_NAME" ]; then
  345. mkdir "/var/www/$LYCHEE_DOMAIN_NAME"
  346. fi
  347. cd "/var/www/$LYCHEE_DOMAIN_NAME" || exit 682468246
  348. if [ -d /repos/lychee ]; then
  349. mkdir htdocs
  350. cp -r -p /repos/lychee/. htdocs
  351. cd htdocs || exit 963756345
  352. git pull
  353. else
  354. git_clone "$LYCHEE_REPO" htdocs
  355. fi
  356. cd htdocs || exit 1437534858
  357. git checkout "$LYCHEE_COMMIT" -b "$LYCHEE_COMMIT"
  358. set_completion_param "lychee commit" "$LYCHEE_COMMIT"
  359. }
  360. function install_lychee {
  361. if [ ! $ONION_ONLY ]; then
  362. ONION_ONLY='no'
  363. fi
  364. if [ ! "$LYCHEE_DOMAIN_NAME" ]; then
  365. echo $'The lychee domain name was not specified'
  366. exit 543672
  367. fi
  368. # for the avatar changing command
  369. apt-get -yq install imagemagick exif zip php-mcrypt mcrypt libfcgi0ldbl
  370. function_check install_lychee_from_repo
  371. install_lychee_from_repo
  372. if [[ $ONION_ONLY == "no" ]]; then
  373. function_check install_lychee_website
  374. install_lychee_website
  375. else
  376. echo -n '' > "/etc/nginx/sites-available/$LYCHEE_DOMAIN_NAME"
  377. fi
  378. LYCHEE_ONION_HOSTNAME=$(add_onion_service lychee 80 ${LYCHEE_ONION_PORT})
  379. function_check install_lychee_website_onion
  380. install_lychee_website_onion
  381. function_check create_site_certificate
  382. create_site_certificate "$LYCHEE_DOMAIN_NAME" 'yes'
  383. function_check configure_php
  384. configure_php
  385. chmod -R 1777 "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/uploads/"
  386. chmod -R 1777 "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/data/"
  387. chown -R www-data:www-data "/var/www/$LYCHEE_DOMAIN_NAME/htdocs"
  388. chmod 755 "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/uploads/big/index.html"
  389. chmod 755 "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/uploads/medium/index.html"
  390. chmod 755 "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/uploads/import/index.html"
  391. chmod 755 "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/uploads/thumb/index.html"
  392. chmod 755 "/var/www/$LYCHEE_DOMAIN_NAME/htdocs/data/.gitignore"
  393. function_check nginx_ensite
  394. nginx_ensite "$LYCHEE_DOMAIN_NAME"
  395. function_check install_mariadb
  396. install_mariadb
  397. function_check get_mariadb_password
  398. get_mariadb_password
  399. function_check lychee_create_database
  400. lychee_create_database
  401. systemctl restart mariadb
  402. systemctl restart php7.0-fpm
  403. systemctl restart nginx
  404. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a lychee -p "$LYCHEE_ADMIN_PASSWORD"
  405. function_check add_ddns_domain
  406. add_ddns_domain "$LYCHEE_DOMAIN_NAME"
  407. set_completion_param "lychee domain" "$LYCHEE_DOMAIN_NAME"
  408. APP_INSTALLED=1
  409. }
  410. # NOTE: deliberately no exit 0