freedombone-app-edith 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Edith: an ultra simple notes application
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2018 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. VARIANTS='full full-vim writer'
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=1
  33. EDITH_REPO="https://github.com/sunny/edith"
  34. EDITH_COMMIT='59f571e24be9e75d127a0f130591acf8d7a86ce3'
  35. EDITH_DOMAIN_NAME=
  36. EDITH_CODE=
  37. EDITH_ONION_PORT=8278
  38. EDITH_LOGIN_TEXT=$"Edith login"
  39. edith_variables=(MY_USERNAME
  40. MY_EMAIL_ADDRESS
  41. ONION_ONLY
  42. EDITH_DOMAIN_NAME
  43. EDITH_CODE
  44. DEFAULT_LANGUAGE)
  45. function remove_bad_links_edith {
  46. read_config_param EDITH_DOMAIN_NAME
  47. edith_dir="/var/www/$EDITH_DOMAIN_NAME/htdocs"
  48. # copy jquery locally
  49. jquery_version='1.12.4'
  50. if [ ! -f $edith_dir/jquery-${jquery_version}.js ]; then
  51. cd $edith_dir || exit 246824628
  52. wget https://code.jquery.com/jquery-${jquery_version}.js
  53. if [ -f $edith_dir/jquery-${jquery_version}.js ]; then
  54. jquery_hash=$(sha256sum $edith_dir/jquery-${jquery_version}.js | awk -F ' ' '{print $1}')
  55. if [[ "$jquery_hash" != '430f36f9b5f21aae8cc9dca6a81c4d3d84da5175eaedcf2fdc2c226302cb3575' ]]; then
  56. echo $'Unexpected jquery hash value'
  57. exit 6783653856
  58. fi
  59. else
  60. echo $"Unable to download https://code.jquery.com/jquery-${jquery_version}.js"
  61. exit 7384673583
  62. fi
  63. fi
  64. sed -i "s|//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js|jquery-${jquery_version}.js|g" $edith_dir/templates/default.php
  65. sed -i '/googleapi/d' $edith_dir/templates/remark.php
  66. sed -i "s|//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js|jquery-${jquery_version}.js|g" $edith_dir/templates/html.php
  67. }
  68. function change_password_edith {
  69. curr_username="$1"
  70. new_user_password="$2"
  71. sed -i "/${curr_username}:/d" /etc/nginx/.edithpasswd
  72. echo -n "$new_user_password" | htpasswd -i -s -c /etc/nginx/.edithpasswd "${curr_username}"
  73. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a "${curr_username}" -p "$new_user_password"
  74. }
  75. function logging_on_edith {
  76. echo -n ''
  77. }
  78. function logging_off_edith {
  79. echo -n ''
  80. }
  81. function reconfigure_edith {
  82. echo -n ''
  83. }
  84. function edith_enable_login {
  85. read_config_param EDITH_DOMAIN_NAME
  86. dialog --title $"Enable Edith login" \
  87. --backtitle $"Freedombone Control Panel" \
  88. --defaultno \
  89. --yesno $"\\nDo you want to add a login so that random web users can't access your notes?" 10 60
  90. sel=$?
  91. case $sel in
  92. 0) if grep -q '#auth_basic' "/etc/nginx/sites-available/$EDITH_DOMAIN_NAME"; then
  93. sed -i 's|#auth_basic|auth_basic|g' "/etc/nginx/sites-available/$EDITH_DOMAIN_NAME"
  94. systemctl restart nginx
  95. fi
  96. read_config_param "$MY_USERNAME"
  97. EDITH_PASSWORD=$("${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a edith)
  98. dialog --title $"Enable Edith login" \
  99. --msgbox $"Edith logins are now enabled with the password $EDITH_PASSWORD" 6 65
  100. ;;
  101. 1) if ! grep -q '#auth_basic' "/etc/nginx/sites-available/$EDITH_DOMAIN_NAME"; then
  102. sed -i 's|auth_basic|#auth_basic|g' "/etc/nginx/sites-available/$EDITH_DOMAIN_NAME"
  103. systemctl restart nginx
  104. fi
  105. dialog --title $"Disable Edith login" \
  106. --msgbox $"Edith logins are now disabled. Anyone can access your stream." 6 65
  107. ;;
  108. esac
  109. }
  110. function edith_browse {
  111. read_config_param EDITH_DOMAIN_NAME
  112. cd "/var/www/$EDITH_DOMAIN_NAME/htdocs/data" || exit 24682642
  113. editor "/var/www/$EDITH_DOMAIN_NAME/htdocs/data"
  114. }
  115. function configure_interactive_edith {
  116. W=(1 $"Enable login"
  117. 2 $"Browse notes")
  118. while true
  119. do
  120. # shellcheck disable=SC2068
  121. selection=$(dialog --backtitle $"Freedombone Administrator Control Panel" --title $"Edith" --menu $"Choose an operation, or ESC to exit:" 10 60 2 "${W[@]}" 3>&2 2>&1 1>&3)
  122. if [ ! "$selection" ]; then
  123. break
  124. fi
  125. case $selection in
  126. 1) edith_enable_login;;
  127. 2) edith_browse;;
  128. esac
  129. done
  130. }
  131. function upgrade_edith {
  132. CURR_EDITH_COMMIT=$(get_completion_param "edith commit")
  133. if [[ "$CURR_EDITH_COMMIT" == "$EDITH_COMMIT" ]]; then
  134. return
  135. fi
  136. read_config_param EDITH_DOMAIN_NAME
  137. # update to the next commit
  138. function_check set_repo_commit
  139. set_repo_commit "/var/www/$EDITH_DOMAIN_NAME/htdocs" "edith commit" "$EDITH_COMMIT" "$EDITH_REPO"
  140. remove_bad_links_edith
  141. chown -R www-data:www-data "/var/www/$EDITH_DOMAIN_NAME/htdocs"
  142. chmod a+w "/var/www/$EDITH_DOMAIN_NAME/htdocs/data"
  143. }
  144. function backup_local_edith {
  145. read_config_param EDITH_DOMAIN_NAME
  146. function_check suspend_site
  147. suspend_site "${EDITH_DOMAIN_NAME}"
  148. source_directory="/var/www/${EDITH_DOMAIN_NAME}/htdocs/data"
  149. function_check backup_directory_to_usb
  150. dest_directory=edith
  151. backup_directory_to_usb "$source_directory" "$dest_directory"
  152. function_check restart_site
  153. restart_site
  154. }
  155. function restore_local_edith {
  156. read_config_param EDITH_DOMAIN_NAME
  157. temp_restore_dir=/root/tempedith
  158. edith_dir="/var/www/${EDITH_DOMAIN_NAME}/htdocs/data"
  159. function_check restore_directory_from_usb
  160. restore_directory_from_usb $temp_restore_dir edith
  161. if [ -d $temp_restore_dir ]; then
  162. if [ -d $temp_restore_dir$edith_dir ]; then
  163. cp -rp $temp_restore_dir$edith_dir $edith_dir/
  164. else
  165. if [ ! -d $edith_dir ]; then
  166. mkdir $edith_dir
  167. chmod a+w $edith_dir
  168. fi
  169. cp -rp $temp_restore_dir/* $edith_dir
  170. fi
  171. chown -R www-data:www-data $edith_dir
  172. rm -rf $temp_restore_dir
  173. fi
  174. }
  175. function backup_remote_edith {
  176. read_config_param EDITH_DOMAIN_NAME
  177. function_check suspend_site
  178. suspend_site "${EDITH_DOMAIN_NAME}"
  179. source_directory="/var/www/${EDITH_DOMAIN_NAME}/htdocs/data"
  180. function_check backup_directory_to_friend
  181. dest_directory=edith
  182. backup_directory_to_friend "$source_directory" "$dest_directory"
  183. function_check restart_site
  184. restart_site
  185. }
  186. function restore_remote_edith {
  187. read_config_param EDITH_DOMAIN_NAME
  188. temp_restore_dir=/root/tempedith
  189. edith_dir="/var/www/${EDITH_DOMAIN_NAME}/htdocs/data"
  190. function_check restore_directory_from_friend
  191. restore_directory_from_friend $temp_restore_dir edith
  192. if [ -d $temp_restore_dir ]; then
  193. if [ -d $temp_restore_dir$edith_dir ]; then
  194. cp -rp $temp_restore_dir$edith_dir $edith_dir/
  195. else
  196. if [ ! -d $edith_dir ]; then
  197. mkdir $edith_dir
  198. chmod a+w $edith_dir
  199. fi
  200. cp -rp $temp_restore_dir/* $edith_dir
  201. fi
  202. chown -R www-data:www-data $edith_dir
  203. rm -rf $temp_restore_dir
  204. fi
  205. }
  206. function remove_edith {
  207. nginx_dissite $EDITH_DOMAIN_NAME
  208. if [ -f "/etc/nginx/sites-available/$EDITH_DOMAIN_NAME" ]; then
  209. rm "/etc/nginx/sites-available/$EDITH_DOMAIN_NAME"
  210. fi
  211. if [ -d "/var/www/$EDITH_DOMAIN_NAME" ]; then
  212. rm -rf "/var/www/$EDITH_DOMAIN_NAME"
  213. fi
  214. function_check remove_onion_service
  215. remove_onion_service edith ${EDITH_ONION_PORT}
  216. sed -i '/edith/d' "$COMPLETION_FILE"
  217. if [ -f /etc/nginx/.edithpasswd ]; then
  218. rm /etc/nginx/.edithpasswd
  219. fi
  220. function_check remove_nodejs
  221. remove_nodejs edith
  222. remove_certs $EDITH_DOMAIN_NAME
  223. remove_app edith
  224. function_check remove_ddns_domain
  225. remove_ddns_domain $EDITH_DOMAIN_NAME
  226. }
  227. function install_edith {
  228. apt-get -yq install php-gettext php-curl php-gd php-mysql git curl
  229. apt-get -yq install memcached php-memcached php-intl exiftool libfcgi0ldbl
  230. function_check install_nodejs
  231. install_nodejs edith
  232. if [ ! "${EDITH_PASSWORD}" ]; then
  233. if [ -f "${IMAGE_PASSWORD_FILE}" ]; then
  234. EDITH_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
  235. else
  236. EDITH_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  237. fi
  238. fi
  239. if [ -d "/var/www/$EDITH_DOMAIN_NAME/htdocs" ]; then
  240. rm -rf "/var/www/$EDITH_DOMAIN_NAME/htdocs"
  241. fi
  242. if [ -d /repos/edith ]; then
  243. mkdir "/var/www/$EDITH_DOMAIN_NAME/htdocs"
  244. cp -r -p /repos/edith/. "/var/www/$EDITH_DOMAIN_NAME/htdocs"
  245. cd "/var/www/$EDITH_DOMAIN_NAME/htdocs" || exit 24682462
  246. git pull
  247. else
  248. function_check git_clone
  249. git_clone "$EDITH_REPO" "/var/www/$EDITH_DOMAIN_NAME/htdocs"
  250. fi
  251. if [ ! -d /var/www/$EDITH_DOMAIN_NAME/htdocs ]; then
  252. echo $'Unable to clone edith repo'
  253. exit 537593569
  254. fi
  255. cd "/var/www/$EDITH_DOMAIN_NAME/htdocs" || exit 2648248
  256. git checkout "$EDITH_COMMIT" -b "$EDITH_COMMIT"
  257. set_completion_param "edith commit" "$EDITH_COMMIT"
  258. if [ ! -d "/var/www/$EDITH_DOMAIN_NAME/htdocs/data" ]; then
  259. mkdir -p "/var/www/$EDITH_DOMAIN_NAME/htdocs/data"
  260. fi
  261. EDITH_ONION_HOSTNAME=$(add_onion_service edith 80 ${EDITH_ONION_PORT})
  262. edith_nginx_site="/etc/nginx/sites-available/$EDITH_DOMAIN_NAME"
  263. if [[ "$ONION_ONLY" == "no" ]]; then
  264. function_check nginx_http_redirect
  265. nginx_http_redirect "$EDITH_DOMAIN_NAME" "index index.php"
  266. { echo 'server {';
  267. echo ' listen 443 ssl;';
  268. echo ' #listen [::]:443 ssl;';
  269. echo " server_name $EDITH_DOMAIN_NAME;";
  270. echo ''; } >> "$edith_nginx_site"
  271. function_check nginx_compress
  272. nginx_compress "$EDITH_DOMAIN_NAME"
  273. echo '' >> "$edith_nginx_site"
  274. echo ' # Security' >> "$edith_nginx_site"
  275. function_check nginx_ssl
  276. nginx_ssl "$EDITH_DOMAIN_NAME"
  277. function_check nginx_security_options
  278. nginx_security_options "$EDITH_DOMAIN_NAME"
  279. { echo ' add_header Strict-Transport-Security max-age=15768000;';
  280. echo '';
  281. echo ' access_log /dev/null;';
  282. echo ' error_log /dev/null;';
  283. echo '';
  284. echo " root /var/www/$EDITH_DOMAIN_NAME/htdocs;";
  285. echo '';
  286. echo ' index index.php;';
  287. echo '';
  288. echo ' # PHP';
  289. echo ' location ~ \.php {';
  290. echo ' include snippets/fastcgi-php.conf;';
  291. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  292. echo ' fastcgi_read_timeout 30;';
  293. echo ' }';
  294. echo '';
  295. echo ' # Location';
  296. echo ' location / {'; } >> "$edith_nginx_site"
  297. function_check nginx_limits
  298. nginx_limits "$EDITH_DOMAIN_NAME" '15m'
  299. { echo " try_files \$uri \$uri/ /index.php?\$args;";
  300. echo " auth_basic \"${EDITH_LOGIN_TEXT}\";";
  301. echo ' auth_basic_user_file /etc/nginx/.edithpasswd;';
  302. echo ' }';
  303. echo '}'; } >> "$edith_nginx_site"
  304. else
  305. echo -n '' > "$edith_nginx_site"
  306. fi
  307. { echo 'server {';
  308. echo " listen 127.0.0.1:$EDITH_ONION_PORT default_server;";
  309. echo " server_name $EDITH_ONION_HOSTNAME;";
  310. echo ''; } >> "$edith_nginx_site"
  311. function_check nginx_compress
  312. nginx_compress "$EDITH_DOMAIN_NAME"
  313. echo '' >> "$edith_nginx_site"
  314. function_check nginx_security_options
  315. nginx_security_options "$EDITH_DOMAIN_NAME"
  316. { echo '';
  317. echo ' access_log /dev/null;';
  318. echo ' error_log /dev/null;';
  319. echo '';
  320. echo " root /var/www/$EDITH_DOMAIN_NAME/htdocs;";
  321. echo '';
  322. echo ' index index.php;';
  323. echo '';
  324. echo ' # PHP';
  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';
  332. echo ' location / {'; } >> "$edith_nginx_site"
  333. function_check nginx_limits
  334. nginx_limits $EDITH_DOMAIN_NAME '15m'
  335. { echo " try_files \$uri \$uri/ /index.php?\$args;";
  336. echo " auth_basic \"${EDITH_LOGIN_TEXT}\";";
  337. echo ' auth_basic_user_file /etc/nginx/.edithpasswd;';
  338. echo ' }';
  339. echo '}'; } >> "$edith_nginx_site"
  340. function_check configure_php
  341. configure_php
  342. function_check create_site_certificate
  343. create_site_certificate "$EDITH_DOMAIN_NAME" 'yes'
  344. # create a password for users
  345. if [ ! -f /etc/nginx/.edithpasswd ]; then
  346. touch /etc/nginx/.edithpasswd
  347. fi
  348. if grep -q "$MY_USERNAME:" /etc/nginx/.edithpasswd; then
  349. sed -i "/$MY_USERNAME:/d" /etc/nginx/.edithpasswd
  350. fi
  351. echo -n "$EDITH_PASSWORD" | htpasswd -i -s -c /etc/nginx/.edithpasswd "$MY_USERNAME"
  352. if [ ! -f /etc/nginx/.edithpasswd ]; then
  353. echo $'/etc/nginx/.edithpasswd not found'
  354. exit 6537683563
  355. fi
  356. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a edith -p "$EDITH_PASSWORD"
  357. cp "/var/www/$EDITH_DOMAIN_NAME/htdocs/htaccess.example" "/var/www/$EDITH_DOMAIN_NAME/htdocs/.htaccess"
  358. cd "/var/www/$EDITH_DOMAIN_NAME/htdocs" || exit 26482468
  359. npm install -g coffeescript uglify-js
  360. if ! cake build; then
  361. echo $'Unable to build Edith'
  362. exit 7396483635
  363. fi
  364. cp config.example.php config.php
  365. if [[ "$ONION_ONLY" == "no" ]]; then
  366. sed -i "s|define('EDITH_URI'.*|define('EDITH_URI', 'https://$EDITH_DOMAIN_NAME');|g" config.php
  367. else
  368. sed -i "s|define('EDITH_URI'.*|define('EDITH_URI', 'http://$EDITH_ONION_HOSTNAME');|g" config.php
  369. fi
  370. set_completion_param "edith domain" "$EDITH_DOMAIN_NAME"
  371. set_completion_param "edith onion domain" "$EDITH_ONION_HOSTNAME"
  372. remove_bad_links_edith
  373. chown -R www-data:www-data "/var/www/$EDITH_DOMAIN_NAME/htdocs"
  374. chmod a+w "/var/www/$EDITH_DOMAIN_NAME/htdocs/data"
  375. nginx_ensite "$EDITH_DAEMON_NAME"
  376. systemctl restart nginx
  377. APP_INSTALLED=1
  378. }
  379. function install_interactive_edith {
  380. if [ ! "$ONION_ONLY" ]; then
  381. ONION_ONLY='no'
  382. fi
  383. if [[ "$ONION_ONLY" != "no" ]]; then
  384. EDITH_DOMAIN_NAME='edith.local'
  385. write_config_param "EDITH_DOMAIN_NAME" "$EDITH_DOMAIN_NAME"
  386. else
  387. function_check interactive_site_details
  388. interactive_site_details "edith" "EDITH_DOMAIN_NAME" "EDITH_CODE"
  389. fi
  390. APP_INSTALLED=1
  391. }
  392. # NOTE: deliberately no exit 0