freedombone-app-edith 15KB

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