freedombone-app-edith 15KB

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