freedombone-app-edith 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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
  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. EDITH__PASSWORD=
  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
  114. editor /var/www/$EDITH_DOMAIN_NAME/htdocs/data
  115. }
  116. function configure_interactive_edith {
  117. while true
  118. do
  119. data=$(tempfile 2>/dev/null)
  120. trap "rm -f $data" 0 1 2 5 15
  121. dialog --backtitle $"Freedombone Control Panel" \
  122. --title $"Edith" \
  123. --radiolist $"Choose an operation:" 10 50 3 \
  124. 1 $"Enable login" off \
  125. 2 $"Browse notes" off \
  126. 3 $"Exit" on 2> $data
  127. sel=$?
  128. case $sel in
  129. 1) break;;
  130. 255) break;;
  131. esac
  132. case $(cat $data) in
  133. 1) edith_enable_login;;
  134. 2) edith_browse;;
  135. 3) break;;
  136. esac
  137. done
  138. }
  139. function upgrade_edith {
  140. CURR_EDITH_COMMIT=$(get_completion_param "edith commit")
  141. if [[ "$CURR_EDITH_COMMIT" == "$EDITH_COMMIT" ]]; then
  142. return
  143. fi
  144. read_config_param EDITH_DOMAIN_NAME
  145. # update to the next commit
  146. function_check set_repo_commit
  147. set_repo_commit /var/www/$EDITH_DOMAIN_NAME/htdocs "edith commit" "$EDITH_COMMIT" $EDITH_REPO
  148. remove_bad_links_edith
  149. chown -R www-data:www-data /var/www/$EDITH_DOMAIN_NAME/htdocs
  150. chmod a+w /var/www/$EDITH_DOMAIN_NAME/htdocs/data
  151. }
  152. function backup_local_edith {
  153. read_config_param EDITH_DOMAIN_NAME
  154. function_check suspend_site
  155. suspend_site ${EDITH_DOMAIN_NAME}
  156. source_directory=/var/www/${EDITH_DOMAIN_NAME}/htdocs/data
  157. function_check backup_directory_to_usb
  158. dest_directory=edith
  159. backup_directory_to_usb $source_directory $dest_directory
  160. function_check restart_site
  161. restart_site
  162. }
  163. function restore_local_edith {
  164. read_config_param EDITH_DOMAIN_NAME
  165. temp_restore_dir=/root/tempedith
  166. edith_dir=/var/www/${EDITH_DOMAIN_NAME}/htdocs/data
  167. function_check restore_directory_from_usb
  168. restore_directory_from_usb $temp_restore_dir edith
  169. if [ -d $temp_restore_dir ]; then
  170. if [ -d cp $temp_restore_dir$edith_dir ]; then
  171. cp -rp $temp_restore_dir$edith_dir $edith_dir/
  172. else
  173. if [ ! -d $edith_dir ]; then
  174. mkdir $edith_dir
  175. chmod a+w $edith_dir
  176. fi
  177. cp -rp $temp_restore_dir/* $edith_dir
  178. fi
  179. chown -R www-data:www-data $edith_dir
  180. rm -rf $temp_restore_dir
  181. fi
  182. }
  183. function backup_remote_edith {
  184. read_config_param EDITH_DOMAIN_NAME
  185. function_check suspend_site
  186. suspend_site ${EDITH_DOMAIN_NAME}
  187. source_directory=/var/www/${EDITH_DOMAIN_NAME}/htdocs/data
  188. function_check backup_directory_to_friend
  189. dest_directory=edith
  190. backup_directory_to_friend $source_directory $dest_directory
  191. function_check restart_site
  192. restart_site
  193. }
  194. function restore_remote_edith {
  195. read_config_param EDITH_DOMAIN_NAME
  196. temp_restore_dir=/root/tempedith
  197. edith_dir=/var/www/${EDITH_DOMAIN_NAME}/htdocs/data
  198. function_check restore_directory_from_friend
  199. restore_directory_from_friend $temp_restore_dir edith
  200. if [ -d $temp_restore_dir ]; then
  201. if [ -d cp $temp_restore_dir$edith_dir ]; then
  202. cp -rp $temp_restore_dir$edith_dir $edith_dir/
  203. else
  204. if [ ! -d $edith_dir ]; then
  205. mkdir $edith_dir
  206. chmod a+w $edith_dir
  207. fi
  208. cp -rp $temp_restore_dir/* $edith_dir
  209. fi
  210. chown -R www-data:www-data $edith_dir
  211. rm -rf $temp_restore_dir
  212. fi
  213. }
  214. function remove_edith {
  215. nginx_dissite $EDITH_DOMAIN_NAME
  216. if [ -f /etc/nginx/sites-available/$EDITH_DOMAIN_NAME ]; then
  217. rm /etc/nginx/sites-available/$EDITH_DOMAIN_NAME
  218. fi
  219. if [ -d /var/www/$EDITH_DOMAIN_NAME ]; then
  220. rm -rf /var/www/$EDITH_DOMAIN_NAME
  221. fi
  222. function_check remove_onion_service
  223. remove_onion_service edith ${EDITH_ONION_PORT}
  224. sed -i '/edith/d' $COMPLETION_FILE
  225. if [ -f /etc/nginx/.edithpasswd ]; then
  226. rm /etc/nginx/.edithpasswd
  227. fi
  228. function_check remove_nodejs
  229. remove_nodejs edith
  230. remove_certs $EDITH_DOMAIN_NAME
  231. remove_app edith
  232. function_check remove_ddns_domain
  233. remove_ddns_domain $EDITH_DOMAIN_NAME
  234. }
  235. function install_edith {
  236. apt-get -yq install php-gettext php-curl php-gd php-mysql git curl
  237. apt-get -yq install memcached php-memcached php-intl exiftool libfcgi0ldbl
  238. function_check install_nodejs
  239. install_nodejs edith
  240. if [ ! ${EDITH_PASSWORD} ]; then
  241. if [ -f ${IMAGE_PASSWORD_FILE} ]; then
  242. EDITH_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  243. else
  244. EDITH_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  245. fi
  246. fi
  247. if [ -d /var/www/$EDITH_DOMAIN_NAME/htdocs ]; then
  248. rm -rf /var/www/$EDITH_DOMAIN_NAME/htdocs
  249. fi
  250. if [ -d /repos/edith ]; then
  251. mkdir /var/www/$EDITH_DOMAIN_NAME/htdocs
  252. cp -r -p /repos/edith/. /var/www/$EDITH_DOMAIN_NAME/htdocs
  253. cd /var/www/$EDITH_DOMAIN_NAME/htdocs
  254. git pull
  255. else
  256. function_check git_clone
  257. git_clone $EDITH_REPO /var/www/$EDITH_DOMAIN_NAME/htdocs
  258. fi
  259. if [ ! -d /var/www/$EDITH_DOMAIN_NAME/htdocs ]; then
  260. echo $'Unable to clone edith repo'
  261. exit 537593569
  262. fi
  263. cd /var/www/$EDITH_DOMAIN_NAME/htdocs
  264. git checkout $EDITH_COMMIT -b $EDITH_COMMIT
  265. set_completion_param "edith commit" "$EDITH_COMMIT"
  266. if [ ! -d /var/www/$EDITH_DOMAIN_NAME/htdocs/data ]; then
  267. mkdir -p /var/www/$EDITH_DOMAIN_NAME/htdocs/data
  268. fi
  269. EDITH_ONION_HOSTNAME=$(add_onion_service edith 80 ${EDITH_ONION_PORT})
  270. edith_nginx_site=/etc/nginx/sites-available/$EDITH_DOMAIN_NAME
  271. if [[ $ONION_ONLY == "no" ]]; then
  272. function_check nginx_http_redirect
  273. nginx_http_redirect $EDITH_DOMAIN_NAME "index index.php"
  274. echo 'server {' >> $edith_nginx_site
  275. echo ' listen 443 ssl;' >> $edith_nginx_site
  276. echo ' listen [::]:443 ssl;' >> $edith_nginx_site
  277. echo " server_name $EDITH_DOMAIN_NAME;" >> $edith_nginx_site
  278. echo '' >> $edith_nginx_site
  279. function_check nginx_compress
  280. nginx_compress $EDITH_DOMAIN_NAME
  281. echo '' >> $edith_nginx_site
  282. echo ' # Security' >> $edith_nginx_site
  283. function_check nginx_ssl
  284. nginx_ssl $EDITH_DOMAIN_NAME
  285. function_check nginx_disable_sniffing
  286. nginx_disable_sniffing $EDITH_DOMAIN_NAME
  287. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $edith_nginx_site
  288. echo '' >> $edith_nginx_site
  289. echo ' access_log /dev/null;' >> $edith_nginx_site
  290. echo ' error_log /dev/null;' >> $edith_nginx_site
  291. echo '' >> $edith_nginx_site
  292. echo " root /var/www/$EDITH_DOMAIN_NAME/htdocs;" >> $edith_nginx_site
  293. echo '' >> $edith_nginx_site
  294. echo ' index index.php;' >> $edith_nginx_site
  295. echo '' >> $edith_nginx_site
  296. echo ' # PHP' >> $edith_nginx_site
  297. echo ' location ~ \.php {' >> $edith_nginx_site
  298. echo ' include snippets/fastcgi-php.conf;' >> $edith_nginx_site
  299. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;' >> $edith_nginx_site
  300. echo ' fastcgi_read_timeout 30;' >> $edith_nginx_site
  301. echo ' }' >> $edith_nginx_site
  302. echo '' >> $edith_nginx_site
  303. echo ' # Location' >> $edith_nginx_site
  304. echo ' location / {' >> $edith_nginx_site
  305. function_check nginx_limits
  306. nginx_limits $EDITH_DOMAIN_NAME '15m'
  307. echo ' try_files $uri $uri/ /index.php?$args;' >> $edith_nginx_site
  308. echo " auth_basic \"${EDITH_LOGIN_TEXT}\";" >> $edith_nginx_site
  309. echo ' auth_basic_user_file /etc/nginx/.edithpasswd;' >> $edith_nginx_site
  310. echo ' }' >> $edith_nginx_site
  311. echo '}' >> $edith_nginx_site
  312. else
  313. echo -n '' > $edith_nginx_site
  314. fi
  315. echo 'server {' >> $edith_nginx_site
  316. echo " listen 127.0.0.1:$EDITH_ONION_PORT default_server;" >> $edith_nginx_site
  317. echo " server_name $EDITH_ONION_HOSTNAME;" >> $edith_nginx_site
  318. echo '' >> $edith_nginx_site
  319. function_check nginx_compress
  320. nginx_compress $EDITH_DOMAIN_NAME
  321. echo '' >> $edith_nginx_site
  322. function_check nginx_disable_sniffing
  323. nginx_disable_sniffing $EDITH_DOMAIN_NAME
  324. echo '' >> $edith_nginx_site
  325. echo ' access_log /dev/null;' >> $edith_nginx_site
  326. echo ' error_log /dev/null;' >> $edith_nginx_site
  327. echo '' >> $edith_nginx_site
  328. echo " root /var/www/$EDITH_DOMAIN_NAME/htdocs;" >> $edith_nginx_site
  329. echo '' >> $edith_nginx_site
  330. echo ' index index.php;' >> $edith_nginx_site
  331. echo '' >> $edith_nginx_site
  332. echo ' # PHP' >> $edith_nginx_site
  333. echo ' location ~ \.php {' >> $edith_nginx_site
  334. echo ' include snippets/fastcgi-php.conf;' >> $edith_nginx_site
  335. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;' >> $edith_nginx_site
  336. echo ' fastcgi_read_timeout 30;' >> $edith_nginx_site
  337. echo ' }' >> $edith_nginx_site
  338. echo '' >> $edith_nginx_site
  339. echo ' # Location' >> $edith_nginx_site
  340. echo ' location / {' >> $edith_nginx_site
  341. function_check nginx_limits
  342. nginx_limits $EDITH_DOMAIN_NAME '15m'
  343. echo ' try_files $uri $uri/ /index.php?$args;' >> $edith_nginx_site
  344. echo " auth_basic \"${EDITH_LOGIN_TEXT}\";" >> $edith_nginx_site
  345. echo ' auth_basic_user_file /etc/nginx/.edithpasswd;' >> $edith_nginx_site
  346. echo ' }' >> $edith_nginx_site
  347. echo '}' >> $edith_nginx_site
  348. function_check configure_php
  349. configure_php
  350. function_check create_site_certificate
  351. create_site_certificate $EDITH_DOMAIN_NAME 'yes'
  352. # create a password for users
  353. if [ ! -f /etc/nginx/.edithpasswd ]; then
  354. touch /etc/nginx/.edithpasswd
  355. fi
  356. if grep -q "$MY_USERNAME:" /etc/nginx/.edithpasswd; then
  357. sed -i "/$MY_USERNAME:/d" /etc/nginx/.edithpasswd
  358. fi
  359. echo -n "$EDITH_PASSWORD" | htpasswd -i -s -c /etc/nginx/.edithpasswd $MY_USERNAME
  360. if [ ! -f /etc/nginx/.edithpasswd ]; then
  361. echo $'/etc/nginx/.edithpasswd not found'
  362. exit 6537683563
  363. fi
  364. ${PROJECT_NAME}-pass -u $MY_USERNAME -a edith -p "$EDITH_PASSWORD"
  365. cp /var/www/$EDITH_DOMAIN_NAME/htdocs/htaccess.example /var/www/$EDITH_DOMAIN_NAME/htdocs/.htaccess
  366. cd /var/www/$EDITH_DOMAIN_NAME/htdocs
  367. npm install -g coffeescript uglify-js
  368. cake build
  369. if [ ! "$?" = "0" ]; then
  370. echo $'Unable to build Edith'
  371. exit 7396483635
  372. fi
  373. cp config.example.php config.php
  374. if [[ $ONION_ONLY == "no" ]]; then
  375. sed -i "s|define('EDITH_URI'.*|define('EDITH_URI', 'https://$EDITH_DOMAIN_NAME');|g" config.php
  376. else
  377. sed -i "s|define('EDITH_URI'.*|define('EDITH_URI', 'http://$EDITH_ONION_HOSTNAME');|g" config.php
  378. fi
  379. set_completion_param "edith domain" "$EDITH_DOMAIN_NAME"
  380. set_completion_param "edith onion domain" "$EDITH_ONION_HOSTNAME"
  381. remove_bad_links_edith
  382. chown -R www-data:www-data /var/www/$EDITH_DOMAIN_NAME/htdocs
  383. chmod a+w /var/www/$EDITH_DOMAIN_NAME/htdocs/data
  384. nginx_ensite $EDITH_DAEMON_NAME
  385. systemctl restart nginx
  386. APP_INSTALLED=1
  387. }
  388. function install_interactive_edith {
  389. if [ ! $ONION_ONLY ]; then
  390. ONION_ONLY='no'
  391. fi
  392. if [[ $ONION_ONLY != "no" ]]; then
  393. GHOST_DOMAIN_NAME='edith.local'
  394. write_config_param "EDITH_DOMAIN_NAME" "$EDITH_DOMAIN_NAME"
  395. else
  396. function_check interactive_site_details
  397. interactive_site_details "edith" "EDITH_DOMAIN_NAME" "EDITH_CODE"
  398. fi
  399. APP_INSTALLED=1
  400. }
  401. # NOTE: deliberately no exit 0