freedombone-app-smolrss 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #!/bin/bash
  2. #
  3. # _____ _ _
  4. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  5. # | __| _| -_| -_| . | . | | . | . | | -_|
  6. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  7. #
  8. # Freedom in the Cloud
  9. #
  10. # License
  11. # =======
  12. #
  13. # Copyright (C) 2018 Bob Mottram <bob@freedombone.net>
  14. #
  15. # This program is free software: you can redistribute it and/or modify
  16. # it under the terms of the GNU Affero General Public License as published by
  17. # the Free Software Foundation, either version 3 of the License, or
  18. # (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU Affero General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU Affero General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. VARIANTS='full full-vim'
  28. IN_DEFAULT_INSTALL=0
  29. SHOW_ON_ABOUT=1
  30. SHOW_ICANN_ADDRESS_ON_ABOUT=0
  31. SMOLRSS_DOMAIN_NAME=
  32. SMOLRSS_CODE=
  33. SMOLRSS_ONION_PORT=8751
  34. SMOLRSS_REPO="https://github.com/bashrc/smolrss"
  35. SMOLRSS_COMMIT='d9fca3fd76b95c601553a1264ff500c287211105'
  36. smolrss_variables=(ONION_ONLY
  37. SMOLRSS_DOMAIN_NAME
  38. SMOLRSS_CODE
  39. DDNS_PROVIDER
  40. MY_USERNAME)
  41. function logging_on_smolrss {
  42. echo -n ''
  43. }
  44. function logging_off_smolrss {
  45. echo -n ''
  46. }
  47. function remove_user_smolrss {
  48. #remove_username="$1"
  49. echo -n ''
  50. }
  51. function add_user_smolrss {
  52. #new_username="$1"
  53. #new_user_password="$2"
  54. echo '0'
  55. }
  56. function install_interactive_smolrss {
  57. echo -n ''
  58. APP_INSTALLED=1
  59. }
  60. function change_password_smolrss {
  61. #curr_username="$1"
  62. #new_user_password="$2"
  63. echo -n ''
  64. }
  65. function reconfigure_smolrss {
  66. # This is used if you need to switch identity. Dump old keys and generate new ones
  67. echo -n ''
  68. }
  69. function smolrss_add_feed {
  70. data=$(mktemp 2>/dev/null)
  71. dialog --backtitle $"Smol RSS" \
  72. --title $"Add an RSS feed" \
  73. --form "\\n" 8 60 3 \
  74. $"Title:" 1 1 "" 1 12 40 256 \
  75. $"Feed URL:" 2 1 "" 2 12 40 10000 \
  76. 2> "$data"
  77. sel=$?
  78. case $sel in
  79. 1) rm -f "$data"
  80. return;;
  81. 255) rm -f "$data"
  82. return;;
  83. esac
  84. title=$(sed -n 1p < "$data")
  85. url=$(sed -n 2p < "$data")
  86. rm -f "$data"
  87. if [ ! "$title" ]; then
  88. return
  89. fi
  90. if [ ! "$url" ]; then
  91. return
  92. fi
  93. if [[ "$url" == *','* ]]; then
  94. return
  95. fi
  96. if [[ "$url" != *'.'* ]]; then
  97. return
  98. fi
  99. cd "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs" || return
  100. if grep -q "${title}," feeds.txt; then
  101. sed -i "s|${title},.*|${title},${url}|g" feeds.txt
  102. else
  103. echo "${title},${url}" >> feeds.txt
  104. fi
  105. ./create_feeds feeds.txt > feeds.xml
  106. chown www-data:www-data feeds.txt
  107. dialog --title $"Add an RSS feed" \
  108. --msgbox $"${title} has been added" 6 70
  109. }
  110. function smolrss_remove_feed {
  111. data=$(mktemp 2>/dev/null)
  112. dialog --title $"Remove an RSS feed" \
  113. --backtitle $"Smol RSS" \
  114. --inputbox $"Enter the title of the feed to remove" 8 60 2>"$data"
  115. sel=$?
  116. case $sel in
  117. 0)
  118. title=$(<"$data")
  119. if [ "$title" ]; then
  120. cd "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs" || return
  121. if grep -q "${title}," feeds.txt; then
  122. sed -i "/${title},/d" feeds.xml
  123. ./create_feeds feeds.txt > feeds.xml
  124. chown www-data:www-data feeds.txt
  125. dialog --title $"Remove an RSS feed" \
  126. --msgbox $"${title} has been removed" 6 70
  127. fi
  128. fi
  129. ;;
  130. esac
  131. rm -f "$data"
  132. }
  133. function configure_interactive_smolrss {
  134. W=(1 $"Add an RSS feed"
  135. 2 $"Remove an RSS feed"
  136. 3 $'Edit all feeds'
  137. 4 $'Light theme'
  138. 5 $'Dark theme')
  139. read_config_param SMOLRSS_DOMAIN_NAME
  140. while true
  141. do
  142. # shellcheck disable=SC2068
  143. selection=$(dialog --backtitle $"Freedombone Administrator Control Panel" --title $"Smol RSS" --menu $"Choose an operation, or ESC for main menu:" 14 70 5 "${W[@]}" 3>&2 2>&1 1>&3)
  144. if [ ! "$selection" ]; then
  145. break
  146. fi
  147. case $selection in
  148. 1) smolrss_add_feed
  149. ;;
  150. 2) smolrss_remove_feed
  151. ;;
  152. 3) editor "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs/feeds.txt"
  153. cd "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs" || break
  154. ./create_feeds feeds.txt > feeds.xml
  155. chown www-data:www-data feeds.txt
  156. ;;
  157. 4) cd "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs" || break
  158. cp style.light.css style.css
  159. chown www-data:www-data style.css
  160. dialog --title $"Smol RSS theme" \
  161. --msgbox $"Switched theme to light" 6 50
  162. ;;
  163. 5) cd "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs" || break
  164. cp style.dark.css style.css
  165. chown www-data:www-data style.css
  166. dialog --title $"Smol RSS theme" \
  167. --msgbox $"Switched theme to dark" 6 50
  168. ;;
  169. esac
  170. done
  171. }
  172. function upgrade_smolrss {
  173. CURR_SMOLRSS_COMMIT=$(get_completion_param "smolrss commit")
  174. if [[ "$CURR_SMOLRSS_COMMIT" == "$SMOLRSS_COMMIT" ]]; then
  175. return
  176. fi
  177. if grep -q "smolrss domain" "$COMPLETION_FILE"; then
  178. SMOLRSS_DOMAIN_NAME=$(get_completion_param "smolrss domain")
  179. fi
  180. # update to the next commit
  181. set_repo_commit "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs" "smolrss commit" "$SMOLRSS_COMMIT" "$SMOLRSS_REPO"
  182. cd "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs" || return
  183. ./create_feeds feeds.txt > feeds.xml
  184. chown -R www-data:www-data "/var/www/${SMOLRSS_DOMAIN_NAME}/htdocs"
  185. }
  186. function backup_local_smolrss {
  187. SMOLRSS_DOMAIN_NAME='smolrss'
  188. if grep -q "smolrss domain" "$COMPLETION_FILE"; then
  189. SMOLRSS_DOMAIN_NAME=$(get_completion_param "smolrss domain")
  190. fi
  191. source_directory=/var/www/${SMOLRSS_DOMAIN_NAME}/htdocs
  192. suspend_site "${SMOLRSS_DOMAIN_NAME}"
  193. dest_directory=smolrss
  194. backup_directory_to_usb "$source_directory" $dest_directory
  195. restart_site
  196. }
  197. function restore_local_smolrss {
  198. if ! grep -q "smolrss domain" "$COMPLETION_FILE"; then
  199. return
  200. fi
  201. SMOLRSS_DOMAIN_NAME=$(get_completion_param "smolrss domain")
  202. if [ ! "$SMOLRSS_DOMAIN_NAME" ]; then
  203. return
  204. fi
  205. suspend_site "${SMOLRSS_DOMAIN_NAME}"
  206. temp_restore_dir=/root/tempsmolrss
  207. smolrss_dir=/var/www/${SMOLRSS_DOMAIN_NAME}/htdocs
  208. restore_directory_from_usb $temp_restore_dir smolrss
  209. if [ -d $temp_restore_dir ]; then
  210. if [ -d "$temp_restore_dir$smolrss_dir" ]; then
  211. cp -rp "$temp_restore_dir$smolrss_dir"/* "$smolrss_dir"/
  212. else
  213. if [ ! -d "$smolrss_dir" ]; then
  214. mkdir "$smolrss_dir"
  215. fi
  216. cp -rp "$temp_restore_dir"/* "$smolrss_dir"/
  217. fi
  218. chown -R www-data:www-data "$smolrss_dir"
  219. rm -rf $temp_restore_dir
  220. fi
  221. restart_site
  222. }
  223. function backup_remote_smolrss {
  224. SMOLRSS_DOMAIN_NAME='smolrss'
  225. if grep -q "smolrss domain" "$COMPLETION_FILE"; then
  226. SMOLRSS_DOMAIN_NAME=$(get_completion_param "smolrss domain")
  227. fi
  228. source_directory=/var/www/${SMOLRSS_DOMAIN_NAME}/htdocs
  229. suspend_site "${SMOLRSS_DOMAIN_NAME}"
  230. dest_directory=smolrss
  231. backup_directory_to_friend "$source_directory" $dest_directory
  232. restart_site
  233. }
  234. function restore_remote_smolrss {
  235. if ! grep -q "smolrss domain" "$COMPLETION_FILE"; then
  236. return
  237. fi
  238. SMOLRSS_DOMAIN_NAME=$(get_completion_param "smolrss domain")
  239. if [ ! "$SMOLRSS_DOMAIN_NAME" ]; then
  240. return
  241. fi
  242. suspend_site "${SMOLRSS_DOMAIN_NAME}"
  243. temp_restore_dir=/root/tempsmolrss
  244. smolrss_dir=/var/www/${SMOLRSS_DOMAIN_NAME}/htdocs
  245. restore_directory_from_friend $temp_restore_dir smolrss
  246. if [ -d $temp_restore_dir ]; then
  247. if [ -d "$temp_restore_dir$smolrss_dir" ]; then
  248. cp -rp "$temp_restore_dir$smolrss_dir"/* "$smolrss_dir"/
  249. else
  250. if [ ! -d "$smolrss_dir" ]; then
  251. mkdir "$smolrss_dir"
  252. fi
  253. cp -rp $temp_restore_dir/* "$smolrss_dir"/
  254. fi
  255. chown -R www-data:www-data "$smolrss_dir"
  256. rm -rf $temp_restore_dir
  257. fi
  258. restart_site
  259. }
  260. function remove_smolrss {
  261. nginx_dissite "$SMOLRSS_DOMAIN_NAME"
  262. remove_certs "$SMOLRSS_DOMAIN_NAME"
  263. if [ -d "/var/www/$SMOLRSS_DOMAIN_NAME" ]; then
  264. rm -rf "/var/www/$SMOLRSS_DOMAIN_NAME"
  265. fi
  266. if [ -f "/etc/nginx/sites-available/$SMOLRSS_DOMAIN_NAME" ]; then
  267. rm "/etc/nginx/sites-available/$SMOLRSS_DOMAIN_NAME"
  268. fi
  269. remove_onion_service smolrss "${SMOLRSS_ONION_PORT}"
  270. if grep -q "smolrss" /etc/crontab; then
  271. sed -i "/smolrss/d" /etc/crontab
  272. fi
  273. remove_app smolrss
  274. remove_completion_param install_smolrss
  275. sed -i '/smolrss/d' "$COMPLETION_FILE"
  276. remove_ddns_domain "$SMOLRSS_DOMAIN_NAME"
  277. }
  278. function install_smolrss {
  279. apt-get -yq install php-gettext php-curl php-gd php-mysql git curl
  280. apt-get -yq install memcached php-memcached php-intl exiftool libfcgi0ldbl
  281. SMOLRSS_DOMAIN_NAME='smolrss.local'
  282. if [ -d "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs" ]; then
  283. rm -rf "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs"
  284. fi
  285. if [ -d /repos/smolrss ]; then
  286. mkdir "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs"
  287. cp -r -p /repos/smolrss/. "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs"
  288. cd "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs" || exit 324687356
  289. git pull
  290. else
  291. git_clone "$SMOLRSS_REPO" "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs"
  292. fi
  293. if [ ! -d "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs" ]; then
  294. echo $'Unable to clone smolrss repo'
  295. exit 87525
  296. fi
  297. cd "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs" || exit 36587356
  298. git checkout "$SMOLRSS_COMMIT" -b "$SMOLRSS_COMMIT"
  299. set_completion_param "smolrss commit" "$SMOLRSS_COMMIT"
  300. cp feeds.example.txt feeds.txt
  301. ./create_feeds feeds.txt > feeds.xml
  302. chmod g+w "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs"
  303. chown -R www-data:www-data "/var/www/$SMOLRSS_DOMAIN_NAME/htdocs"
  304. add_ddns_domain "$SMOLRSS_DOMAIN_NAME"
  305. SMOLRSS_ONION_HOSTNAME=$(add_onion_service smolrss 80 "${SMOLRSS_ONION_PORT}")
  306. smolrss_nginx_site=/etc/nginx/sites-available/$SMOLRSS_DOMAIN_NAME
  307. echo -n '' > "$smolrss_nginx_site"
  308. { echo 'server {';
  309. echo " listen 127.0.0.1:$SMOLRSS_ONION_PORT default_server;";
  310. echo " server_name $SMOLRSS_ONION_HOSTNAME;";
  311. echo ''; } >> "$smolrss_nginx_site"
  312. nginx_compress "$SMOLRSS_DOMAIN_NAME"
  313. echo '' >> "$smolrss_nginx_site"
  314. nginx_security_options "$SMOLRSS_DOMAIN_NAME"
  315. { echo '';
  316. echo ' access_log /dev/null;';
  317. echo ' error_log /dev/null;';
  318. echo '';
  319. echo " root /var/www/$SMOLRSS_DOMAIN_NAME/htdocs;";
  320. echo '';
  321. echo ' index index.php;';
  322. echo ' location ~ \.php {';
  323. echo ' include snippets/fastcgi-php.conf;';
  324. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  325. echo ' fastcgi_read_timeout 30;';
  326. echo ' fastcgi_param HTTPS off;';
  327. echo ' }';
  328. echo '';
  329. echo ' # Location';
  330. echo ' location / {'; } >> "$smolrss_nginx_site"
  331. nginx_limits "$SMOLRSS_DOMAIN_NAME" '15m'
  332. { echo " try_files \$uri \$uri/ index.php?\$args;";
  333. echo ' }';
  334. echo '}'; } >> "$smolrss_nginx_site"
  335. configure_php
  336. nginx_ensite "$SMOLRSS_DOMAIN_NAME"
  337. systemctl restart php7.0-fpm
  338. systemctl restart nginx
  339. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a smolrss -p "$SMOLRSS_ADMIN_PASSWORD"
  340. set_completion_param "smolrss domain" "$SMOLRSS_DOMAIN_NAME"
  341. APP_INSTALLED=1
  342. }
  343. # NOTE: deliberately there is no "exit 0"