freedombone-app-postactiv 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # postactiv application
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2016 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 media'
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=1
  33. POSTACTIV_DOMAIN_NAME=
  34. POSTACTIV_CODE=
  35. POSTACTIV_ONION_PORT=8100
  36. POSTACTIV_REPO="https://git.gnu.io/maiyannah/postActiv"
  37. POSTACTIV_COMMIT='a07d28888d97e207288c86b81905e42dae90aada'
  38. POSTACTIV_ADMIN_PASSWORD=
  39. # script which causes old posts to expire
  40. postactiv_expire_script=/usr/bin/postactiv-expire
  41. postactiv_variables=(POSTACTIV_COMMIT
  42. ONION_ONLY
  43. POSTACTIV_DOMAIN_NAME
  44. POSTACTIV_CODE
  45. DDNS_PROVIDER
  46. MY_USERNAME)
  47. function remove_user_postactiv {
  48. remove_username="$1"
  49. function_check get_completion_param
  50. POSTACTIV_DOMAIN_NAME=$(get_completion_param "postactiv domain")
  51. if [ -d /var/www/$POSTACTIV_DOMAIN_NAME ]; then
  52. cd /var/www/$POSTACTIV_DOMAIN_NAME/htdocs
  53. php scripts/deleteprofile.php -n $remove_username -y
  54. fi
  55. }
  56. function add_user_postactiv {
  57. new_username="$1"
  58. new_user_password="$2"
  59. POSTACTIV_DOMAIN_NAME=$(get_completion_param "postactiv domain")
  60. if [ -d /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs ]; then
  61. cd /var/www/$POSTACTIV_DOMAIN_NAME/htdocs
  62. php scripts/registeruser.php -n $new_username -w "$new_user_password" -e "$new_username@$HOSTNAME"
  63. ${PROJECT_NAME}-addemail -u $new_username -e "noreply@$POSTACTIV_DOMAIN_NAME" -g postactiv --public no
  64. echo '0'
  65. else
  66. echo '1'
  67. fi
  68. }
  69. function install_interactive_postactiv {
  70. if [ ! $ONION_ONLY ]; then
  71. ONION_ONLY='no'
  72. fi
  73. if [[ $ONION_ONLY != "no" ]]; then
  74. POSTACTIV_DOMAIN_NAME='postactiv.local'
  75. else
  76. POSTACTIV_DETAILS_COMPLETE=
  77. while [ ! $POSTACTIV_DETAILS_COMPLETE ]
  78. do
  79. data=$(tempfile 2>/dev/null)
  80. trap "rm -f $data" 0 1 2 5 15
  81. if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
  82. dialog --backtitle $"Freedombone Configuration" \
  83. --title $"PostActiv Configuration" \
  84. --form $"\nPlease enter your postactiv details.\n\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 14 65 2 \
  85. $"Domain:" 1 1 "$(grep 'POSTACTIV_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
  86. $"Code:" 2 1 "$(grep 'POSTACTIV_CODE' temp.cfg | awk -F '=' '{print $2}')" 2 25 33 255 \
  87. 2> $data
  88. else
  89. dialog --backtitle $"Freedombone Configuration" \
  90. --title $"PostActiv Configuration" \
  91. --form $"\nPlease enter your postactiv details.\n\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 14 65 2 \
  92. $"Domain:" 1 1 "$(grep 'POSTACTIV_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
  93. 2> $data
  94. fi
  95. sel=$?
  96. case $sel in
  97. 1) exit 1;;
  98. 255) exit 1;;
  99. esac
  100. POSTACTIV_DOMAIN_NAME=$(cat $data | sed -n 1p)
  101. img_url=$(cat $data | sed -n 3p)
  102. if [ $POSTACTIV_DOMAIN_NAME ]; then
  103. if [[ $POSTACTIV_DOMAIN_NAME == "$HUBZILLA_DOMAIN_NAME" ]]; then
  104. POSTACTIV_DOMAIN_NAME=""
  105. fi
  106. TEST_DOMAIN_NAME=$POSTACTIV_DOMAIN_NAME
  107. validate_domain_name
  108. if [[ $TEST_DOMAIN_NAME != $POSTACTIV_DOMAIN_NAME ]]; then
  109. POSTACTIV_DOMAIN_NAME=
  110. dialog --title $"Domain name validation" --msgbox "$TEST_DOMAIN_NAME" 15 50
  111. else
  112. if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
  113. POSTACTIV_CODE=$(cat $data | sed -n 4p)
  114. validate_freedns_code "$POSTACTIV_CODE"
  115. if [ ! $VALID_CODE ]; then
  116. POSTACTIV_DOMAIN_NAME=
  117. fi
  118. fi
  119. fi
  120. fi
  121. if [ $POSTACTIV_DOMAIN_NAME ]; then
  122. POSTACTIV_DETAILS_COMPLETE="yes"
  123. fi
  124. done
  125. # save the results in the config file
  126. write_config_param "POSTACTIV_CODE" "$POSTACTIV_CODE"
  127. fi
  128. write_config_param "POSTACTIV_DOMAIN_NAME" "$POSTACTIV_DOMAIN_NAME"
  129. APP_INSTALLED=1
  130. }
  131. function change_password_postactiv {
  132. echo -n ''
  133. }
  134. function get_mariadb_postactiv_admin_password {
  135. if [ -f /home/$MY_USERNAME/README ]; then
  136. if grep -q "MariaDB postactiv admin password" /home/$MY_USERNAME/README; then
  137. POSTACTIV_ADMIN_PASSWORD=$(cat /home/$MY_USERNAME/README | grep "MariaDB postactiv admin password" | awk -F ':' '{print $2}' | sed 's/^ *//')
  138. fi
  139. if grep -q "postactiv administrator password" /home/$MY_USERNAME/README; then
  140. POSTACTIV_ADMIN_PASSWORD=$(cat /home/$MY_USERNAME/README | grep "postactiv administrator password" | awk -F ':' '{print $2}' | sed 's/^ *//')
  141. fi
  142. fi
  143. }
  144. function postactiv_create_database {
  145. function_check get_mariadb_postactiv_admin_password
  146. get_mariadb_postactiv_admin_password
  147. if [ -f $IMAGE_PASSWORD_FILE ]; then
  148. POSTACTIV_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  149. else
  150. if [ ! $POSTACTIV_ADMIN_PASSWORD ]; then
  151. POSTACTIV_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  152. fi
  153. fi
  154. if [ ! $POSTACTIV_ADMIN_PASSWORD ]; then
  155. return
  156. fi
  157. function_check create_database
  158. create_database postactiv "$POSTACTIV_ADMIN_PASSWORD" $MY_USERNAME
  159. }
  160. function postactiv_running_script {
  161. # check that the daemon is running
  162. echo '#!/bin/bash' > /etc/cron.hourly/postactiv-daemons
  163. echo 'daemon_lines=$(ps aux | grep "scripts/queuedaemon.php" | grep "/var/www")' >> /etc/cron.hourly/postactiv-daemons
  164. echo 'if [[ $daemon_lines != *"/var/www/"* ]]; then' >> /etc/cron.hourly/postactiv-daemons
  165. echo " ADMIN_USER=\$(cat $COMPLETION_FILE | grep 'Admin user' | awk -F ':' '{print \$2}')" >> /etc/cron.hourly/postactiv-daemons
  166. echo ' MY_EMAIL_ADDRESS=$ADMIN_USER@$HOSTNAME' >> /etc/cron.hourly/postactiv-daemons
  167. echo ' echo "Restarting postactiv daemons" | mail -s "postactiv daemons not found" $MY_EMAIL_ADDRESS' >> /etc/cron.hourly/postactiv-daemons
  168. echo " cd /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs" >> /etc/cron.hourly/postactiv-daemons
  169. echo ' scripts/startdaemons.sh' >> /etc/cron.hourly/postactiv-daemons
  170. echo 'fi' >> /etc/cron.hourly/postactiv-daemons
  171. chmod +x /etc/cron.hourly/postactiv-daemons
  172. }
  173. function reconfigure_postactiv {
  174. echo -n ''
  175. }
  176. function upgrade_postactiv {
  177. if grep -q "postactiv domain" $COMPLETION_FILE; then
  178. POSTACTIV_DOMAIN_NAME=$(get_completion_param "postactiv domain")
  179. fi
  180. # update to the next commit
  181. function_check set_repo_commit
  182. set_repo_commit /var/www/$POSTACTIV_DOMAIN_NAME/htdocs "postactiv commit" "$POSTACTIV_COMMIT" $POSTACTIV_REPO
  183. function_check postactiv_running_script
  184. postactiv_running_script
  185. }
  186. function backup_local_postactiv {
  187. POSTACTIV_DOMAIN_NAME='postactiv'
  188. if grep -q "postactiv domain" $COMPLETION_FILE; then
  189. POSTACTIV_DOMAIN_NAME=$(get_completion_param "postactiv domain")
  190. fi
  191. source_directory=/var/www/${POSTACTIV_DOMAIN_NAME}/htdocs
  192. if [ -d $source_directory ]; then
  193. dest_directory=postactiv
  194. function_check suspend_site
  195. suspend_site ${POSTACTIV_DOMAIN_NAME}
  196. function_check backup_directory_to_usb
  197. backup_directory_to_usb $source_directory $dest_directory
  198. function_check backup_database_to_usb
  199. backup_database_to_usb postactiv
  200. function_check restart_site
  201. restart_site
  202. fi
  203. }
  204. function restore_local_postactiv {
  205. if ! grep -q "postactiv domain" $COMPLETION_FILE; then
  206. return
  207. fi
  208. POSTACTIV_DOMAIN_NAME=$(get_completion_param "postactiv domain")
  209. if [ $POSTACTIV_DOMAIN_NAME ]; then
  210. temp_restore_dir=/root/temppostactiv
  211. postactiv_dir=/var/www/${POSTACTIV_DOMAIN_NAME}/htdocs
  212. # stop the daemons
  213. cd $postactiv_dir
  214. scripts/stopdaemons.sh
  215. function_check postactiv_create_database
  216. postactiv_create_database
  217. restore_database postactiv ${POSTACTIV_DOMAIN_NAME}
  218. if [ -d $temp_restore_dir ]; then
  219. rm -rf $temp_restore_dir
  220. fi
  221. # start the daemons
  222. cd $postactiv_dir
  223. scripts/startdaemons.sh
  224. fi
  225. }
  226. function backup_remote_postactiv {
  227. if grep -q "postactiv domain" $COMPLETION_FILE; then
  228. POSTACTIV_DOMAIN_NAME=$(get_completion_param "postactiv domain")
  229. temp_backup_dir=/var/www/${POSTACTIV_DOMAIN_NAME}/htdocs
  230. if [ -d $temp_backup_dir ]; then
  231. function_check suspend_site
  232. suspend_site ${POSTACTIV_DOMAIN_NAME}
  233. function_check backup_database_to_friend
  234. backup_database_to_friend postactiv
  235. function_check backup_directory_to_friend
  236. backup_directory_to_friend $temp_backup_dir postactiv
  237. function_check restart_site
  238. restart_site
  239. else
  240. echo $"postactiv domain specified but not found in ${temp_backup_dir}"
  241. fi
  242. fi
  243. }
  244. function restore_remote_postactiv {
  245. if grep -q "postactiv domain" $COMPLETION_FILE; then
  246. POSTACTIV_DOMAIN_NAME=$(get_completion_param "postactiv domain")
  247. # stop the daemons
  248. cd /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs
  249. ./scripts/stopdaemons.sh
  250. function_check postactiv_create_database
  251. postactiv_create_database
  252. function_check restore_database_from_friend
  253. restore_database_from_friend postactiv ${POSTACTIV_DOMAIN_NAME}
  254. if [ -d /root/temppostactiv ]; then
  255. rm -rf /root/temppostactiv
  256. fi
  257. # start the daemons
  258. cd /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs
  259. ./scripts/startdaemons.sh
  260. fi
  261. }
  262. function remove_postactiv {
  263. if [ ${#POSTACTIV_DOMAIN_NAME} -eq 0 ]; then
  264. return
  265. fi
  266. read_config_param "POSTACTIV_DOMAIN_NAME"
  267. read_config_param "MY_USERNAME"
  268. echo "Removing $POSTACTIV_DOMAIN_NAME"
  269. nginx_dissite $POSTACTIV_DOMAIN_NAME
  270. remove_certs $POSTACTIV_DOMAIN_NAME
  271. if [ -f /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/scripts/stopdaemons.sh ]; then
  272. cd /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/scripts
  273. ./scripts/stopdaemons.sh
  274. fi
  275. if [ -d /var/www/$POSTACTIV_DOMAIN_NAME ]; then
  276. rm -rf /var/www/$POSTACTIV_DOMAIN_NAME
  277. fi
  278. if [ -f /etc/nginx/sites-available/$POSTACTIV_DOMAIN_NAME ]; then
  279. rm /etc/nginx/sites-available/$POSTACTIV_DOMAIN_NAME
  280. fi
  281. function_check drop_database
  282. drop_database postactiv
  283. function_check remove_onion_service
  284. remove_onion_service postactiv ${POSTACTIV_ONION_PORT}
  285. if grep -q "postactiv" /etc/crontab; then
  286. sed -i "/postactiv/d" /etc/crontab
  287. fi
  288. remove_app postactiv
  289. remove_completion_param install_postactiv
  290. sed -i '/postactiv/d' $COMPLETION_FILE
  291. if [ -f /etc/cron.hourly/postactiv-daemons ]; then
  292. rm /etc/cron.hourly/postactiv-daemons
  293. fi
  294. sed -i '/Postactiv/d' /home/$MY_USERNAME/README
  295. remove_backup_database_local postactiv
  296. function_check remove_ddns_domain
  297. remove_ddns_domain $POSTACTIV_DOMAIN_NAME
  298. }
  299. function install_postactiv_main {
  300. if [ ! $POSTACTIV_DOMAIN_NAME ]; then
  301. echo $'No domain name was given for postactiv'
  302. exit 7359
  303. fi
  304. if [[ $(app_is_installed postactiv_main) == "1" ]]; then
  305. return
  306. fi
  307. function_check postactiv_running_script
  308. postactiv_running_script
  309. function_check install_mariadb
  310. install_mariadb
  311. function_check get_mariadb_password
  312. get_mariadb_password
  313. function_check repair_databases_script
  314. repair_databases_script
  315. apt-get -yq install php-gettext php5-curl php5-gd php5-mysql git curl php-xml-parser
  316. apt-get -yq install php5-memcached php5-intl
  317. if [ ! -d /var/www/$POSTACTIV_DOMAIN_NAME ]; then
  318. mkdir /var/www/$POSTACTIV_DOMAIN_NAME
  319. fi
  320. if [ ! -d /var/www/$POSTACTIV_DOMAIN_NAME/htdocs ]; then
  321. function_check git_clone
  322. git_clone $POSTACTIV_REPO /var/www/$POSTACTIV_DOMAIN_NAME/htdocs
  323. if [ ! -d /var/www/$POSTACTIV_DOMAIN_NAME/htdocs ]; then
  324. echo $'Unable to clone postactiv repo'
  325. exit 87525
  326. fi
  327. fi
  328. cd /var/www/$POSTACTIV_DOMAIN_NAME/htdocs
  329. git checkout $POSTACTIV_COMMIT -b $POSTACTIV_COMMIT
  330. set_completion_param "postactiv commit" "$POSTACTIV_COMMIT"
  331. chmod a+w /var/www/$POSTACTIV_DOMAIN_NAME/htdocs
  332. chown www-data:www-data /var/www/$POSTACTIV_DOMAIN_NAME/htdocs
  333. chmod +x /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/scripts/maildaemon.php
  334. function_check postactiv_create_database
  335. postactiv_create_database
  336. if [ ! -f "/etc/aliases" ]; then
  337. touch /etc/aliases
  338. fi
  339. if ! grep -q "www-data: root" /etc/aliases; then
  340. echo 'www-data: root' >> /etc/aliases
  341. fi
  342. if ! grep -q "/var/www/$POSTACTIV_DOMAIN_NAME/htdocs/scripts/maildaemon.php" /etc/aliases; then
  343. echo "*: /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/scripts/maildaemon.php" >> /etc/aliases
  344. fi
  345. function_check add_ddns_domain
  346. add_ddns_domain $POSTACTIV_DOMAIN_NAME
  347. postactiv_nginx_site=/etc/nginx/sites-available/$POSTACTIV_DOMAIN_NAME
  348. if [[ $ONION_ONLY == "no" ]]; then
  349. function_check nginx_http_redirect
  350. nginx_http_redirect $POSTACTIV_DOMAIN_NAME
  351. echo 'server {' >> $postactiv_nginx_site
  352. echo ' listen 443 ssl;' >> $postactiv_nginx_site
  353. echo ' listen [::]:443 ssl;' >> $postactiv_nginx_site
  354. echo " server_name $POSTACTIV_DOMAIN_NAME;" >> $postactiv_nginx_site
  355. echo '' >> $postactiv_nginx_site
  356. echo ' # Security' >> $postactiv_nginx_site
  357. function_check nginx_ssl
  358. nginx_ssl $POSTACTIV_DOMAIN_NAME
  359. function_check nginx_disable_sniffing
  360. nginx_disable_sniffing $POSTACTIV_DOMAIN_NAME
  361. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $postactiv_nginx_site
  362. echo '' >> $postactiv_nginx_site
  363. echo ' # Logs' >> $postactiv_nginx_site
  364. echo ' access_log /dev/null;' >> $postactiv_nginx_site
  365. echo ' error_log /dev/null;' >> $postactiv_nginx_site
  366. echo '' >> $postactiv_nginx_site
  367. echo ' # Root' >> $postactiv_nginx_site
  368. echo " root /var/www/$POSTACTIV_DOMAIN_NAME/htdocs;" >> $postactiv_nginx_site
  369. echo '' >> $postactiv_nginx_site
  370. echo ' # Index' >> $postactiv_nginx_site
  371. echo ' index index.php;' >> $postactiv_nginx_site
  372. echo '' >> $postactiv_nginx_site
  373. echo ' # PHP' >> $postactiv_nginx_site
  374. echo ' location ~ \.php {' >> $postactiv_nginx_site
  375. echo ' include snippets/fastcgi-php.conf;' >> $postactiv_nginx_site
  376. echo ' fastcgi_pass unix:/var/run/php5-fpm.sock;' >> $postactiv_nginx_site
  377. echo ' }' >> $postactiv_nginx_site
  378. echo '' >> $postactiv_nginx_site
  379. echo ' # Location' >> $postactiv_nginx_site
  380. echo ' location / {' >> $postactiv_nginx_site
  381. function_check nginx_limits
  382. nginx_limits $POSTACTIV_DOMAIN_NAME '15m'
  383. echo ' try_files $uri $uri/ @postactiv;' >> $postactiv_nginx_site
  384. echo ' }' >> $postactiv_nginx_site
  385. echo '' >> $postactiv_nginx_site
  386. echo ' # Fancy URLs' >> $postactiv_nginx_site
  387. echo ' location @postactiv {' >> $postactiv_nginx_site
  388. echo ' rewrite ^(.*)$ /index.php?p=$1 last;' >> $postactiv_nginx_site
  389. echo ' }' >> $postactiv_nginx_site
  390. echo '' >> $postactiv_nginx_site
  391. echo ' # Restrict access that is unnecessary anyway' >> $postactiv_nginx_site
  392. echo ' location ~ /\.(ht|git) {' >> $postactiv_nginx_site
  393. echo ' deny all;' >> $postactiv_nginx_site
  394. echo ' }' >> $postactiv_nginx_site
  395. echo '' >> $postactiv_nginx_site
  396. nginx_keybase ${POSTACTIV_DOMAIN_NAME}
  397. echo '}' >> $postactiv_nginx_site
  398. else
  399. echo -n '' > $postactiv_nginx_site
  400. fi
  401. echo 'server {' >> $postactiv_nginx_site
  402. echo " listen 127.0.0.1:$POSTACTIV_ONION_PORT default_server;" >> $postactiv_nginx_site
  403. echo " server_name $POSTACTIV_DOMAIN_NAME;" >> $postactiv_nginx_site
  404. echo '' >> $postactiv_nginx_site
  405. function_check nginx_disable_sniffing
  406. nginx_disable_sniffing $POSTACTIV_DOMAIN_NAME
  407. echo '' >> $postactiv_nginx_site
  408. echo ' # Logs' >> $postactiv_nginx_site
  409. echo ' access_log /dev/null;' >> $postactiv_nginx_site
  410. echo ' error_log /dev/null;' >> $postactiv_nginx_site
  411. echo '' >> $postactiv_nginx_site
  412. echo ' # Root' >> $postactiv_nginx_site
  413. echo " root /var/www/$POSTACTIV_DOMAIN_NAME/htdocs;" >> $postactiv_nginx_site
  414. echo '' >> $postactiv_nginx_site
  415. echo ' # Index' >> $postactiv_nginx_site
  416. echo ' index index.php;' >> $postactiv_nginx_site
  417. echo '' >> $postactiv_nginx_site
  418. echo ' # PHP' >> $postactiv_nginx_site
  419. echo ' location ~ \.php {' >> $postactiv_nginx_site
  420. echo ' include snippets/fastcgi-php.conf;' >> $postactiv_nginx_site
  421. echo ' fastcgi_pass unix:/var/run/php5-fpm.sock;' >> $postactiv_nginx_site
  422. echo ' }' >> $postactiv_nginx_site
  423. echo '' >> $postactiv_nginx_site
  424. echo ' # Location' >> $postactiv_nginx_site
  425. echo ' location / {' >> $postactiv_nginx_site
  426. function_check nginx_limits
  427. nginx_limits $POSTACTIV_DOMAIN_NAME '15m'
  428. echo ' try_files $uri $uri/ @postactiv;' >> $postactiv_nginx_site
  429. echo ' }' >> $postactiv_nginx_site
  430. echo '' >> $postactiv_nginx_site
  431. echo ' # Fancy URLs' >> $postactiv_nginx_site
  432. echo ' location @postactiv {' >> $postactiv_nginx_site
  433. echo ' rewrite ^(.*)$ /index.php?p=$1 last;' >> $postactiv_nginx_site
  434. echo ' }' >> $postactiv_nginx_site
  435. echo '' >> $postactiv_nginx_site
  436. echo ' # Restrict access that is unnecessary anyway' >> $postactiv_nginx_site
  437. echo ' location ~ /\.(ht|git) {' >> $postactiv_nginx_site
  438. echo ' deny all;' >> $postactiv_nginx_site
  439. echo ' }' >> $postactiv_nginx_site
  440. echo '' >> $postactiv_nginx_site
  441. nginx_keybase ${POSTACTIV_DOMAIN_NAME}
  442. echo '}' >> $postactiv_nginx_site
  443. function_check configure_php
  444. configure_php
  445. function_check create_site_certificate
  446. create_site_certificate $POSTACTIV_DOMAIN_NAME 'yes'
  447. # Ensure that the database gets backed up locally, if remote
  448. # backups are not being used
  449. function_check backup_databases_script_header
  450. backup_databases_script_header
  451. function_check backup_database_local
  452. backup_database_local postactiv
  453. function_check nginx_ensite
  454. nginx_ensite $POSTACTIV_DOMAIN_NAME
  455. # NOTE: For the typical case always enable SSL and only
  456. # disable it if in onion only mode. This is due to complexities
  457. # with the way URLs are generated by postactiv
  458. postactiv_ssl='always'
  459. if [[ $ONION_ONLY != 'no' ]]; then
  460. postactiv_ssl='never'
  461. fi
  462. POSTACTIV_ONION_HOSTNAME=$(add_onion_service postactiv 80 ${POSTACTIV_ONION_PORT})
  463. POSTACTIV_SERVER=${POSTACTIV_DOMAIN_NAME}
  464. if [[ $ONION_ONLY != 'no' ]]; then
  465. POSTACTIV_SERVER=${POSTACTIV_ONION_HOSTNAME}
  466. fi
  467. # Create the configuration
  468. postactiv_installer=/var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/scripts/install_cli.php
  469. if [ ! -f $postactiv_installer ]; then
  470. echo $'No postactiv commandline installer found'
  471. exit 53026
  472. fi
  473. ${postactiv_installer} --server "${POSTACTIV_SERVER}" \
  474. --host="localhost" --database="postactiv" \
  475. --dbtype=mysql --username="root" -v \
  476. --password="$MARIADB_PASSWORD" \
  477. --sitename=$"postactiv" --fancy='yes' \
  478. --admin-nick="$MY_USERNAME" \
  479. --admin-pass="$POSTACTIV_ADMIN_PASSWORD" \
  480. --site-profile="community" \
  481. --ssl=${postactiv_ssl}
  482. # There can be a lot of warnings here so the return value check is disabled
  483. #if [ ! "$?" = "0" ]; then
  484. # # failed to install
  485. # echo $'Could not install postactiv'
  486. # exit 72357
  487. #fi
  488. # check postactiv has a config file
  489. postactiv_config_file=/var/www/$POSTACTIV_DOMAIN_NAME/htdocs/config.php
  490. if [ ! -f $postactiv_config_file ]; then
  491. echo $'Postactiv config.php not found'
  492. exit 87586
  493. fi
  494. # Some useful settings
  495. if ! grep -q "Recommended postactiv settings" $postactiv_config_file; then
  496. echo "" >> $postactiv_config_file
  497. echo "// Recommended postactiv settings" >> $postactiv_config_file
  498. echo "\$config['thumbnail']['maxsize'] = 3000;" >> $postactiv_config_file
  499. echo "\$config['profile']['delete'] = true;" >> $postactiv_config_file
  500. echo "\$config['profile']['changenick'] = true;" >> $postactiv_config_file
  501. echo "\$config['public']['localonly'] = false;" >> $postactiv_config_file
  502. echo "addPlugin('StoreRemoteMedia');" >> $postactiv_config_file
  503. echo "\$config['queue']['enabled'] = true;" >> $postactiv_config_file
  504. echo "\$config['queue']['daemon'] = true;" >> $postactiv_config_file
  505. echo "\$config['ostatus']['hub_retries'] = 3;" >> $postactiv_config_file
  506. fi
  507. # This improves performance
  508. sed -i "s|//\$config\['db'\]\['schemacheck'\].*|\$config\['db'\]\['schemacheck'\] = 'script';|g" $postactiv_config_file
  509. # remove the install script
  510. if [ -f /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/install.php ]; then
  511. rm /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/install.php
  512. fi
  513. systemctl restart php5-fpm
  514. systemctl restart nginx
  515. ${PROJECT_NAME}-addemail -u $MY_USERNAME -e "noreply@$POSTACTIV_DOMAIN_NAME" -g postactiv --public no
  516. # some post-install instructions for the user
  517. if ! grep -q $"Postactiv administrator" /home/$MY_USERNAME/README; then
  518. echo '' >> /home/$MY_USERNAME/README
  519. echo '' >> /home/$MY_USERNAME/README
  520. echo $'# Postactiv' >> /home/$MY_USERNAME/README
  521. echo $"Postactiv administrator nickname: $MY_USERNAME" >> /home/$MY_USERNAME/README
  522. echo $"Postactiv administrator password: $POSTACTIV_ADMIN_PASSWORD" >> /home/$MY_USERNAME/README
  523. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/README
  524. chmod 600 /home/$MY_USERNAME/README
  525. else
  526. if [ -f /home/$MY_USERNAME/README ]; then
  527. sed -i "s|Postactiv administrator password.*|Postactiv administrator password: $POSTACTIV_ADMIN_PASSWORD|g" /home/$MY_USERNAME/README
  528. fi
  529. fi
  530. if ! grep -q "Postactiv onion domain" /home/$MY_USERNAME/README; then
  531. echo $"Postactiv onion domain: ${POSTACTIV_ONION_HOSTNAME}" >> /home/$MY_USERNAME/README
  532. echo '' >> /home/$MY_USERNAME/README
  533. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/README
  534. chmod 600 /home/$MY_USERNAME/README
  535. else
  536. if [ -f /home/$MY_USERNAME/README ]; then
  537. sed -i "s|Postactiv onion domain.*|Postactiv onion domain: ${POSTACTIV_ONION_HOSTNAME}|g" /home/$MY_USERNAME/README
  538. fi
  539. fi
  540. set_completion_param "postactiv domain" "$POSTACTIV_DOMAIN_NAME"
  541. install_completed postactiv_main
  542. }
  543. function expire_postactiv_posts {
  544. # To prevent the database size from growing endlessly this script expires posts
  545. # after a number of months
  546. if [ ! -d /var/www/$POSTACTIV_DOMAIN_NAME/htdocs ]; then
  547. return
  548. fi
  549. echo '<?php' > $postactiv_expire_script
  550. echo '' >> $postactiv_expire_script
  551. echo '// postactiv post expiry script, based on StatExpire by Tony Baldwin' >> $postactiv_expire_script
  552. echo '// https://github.com/tonybaldwin/statexpire' >> $postactiv_expire_script
  553. echo '' >> $postactiv_expire_script
  554. echo '$oldate=date(("Y-m-d"), strtotime("-3 months"));' >> $postactiv_expire_script
  555. echo '$username="root";' >> $postactiv_expire_script
  556. echo "\$password=trim(file_get_contents(\"$DATABASE_PASSWORD_FILE\"));" >> $postactiv_expire_script
  557. echo '$database="postactiv";' >> $postactiv_expire_script
  558. echo '' >> $postactiv_expire_script
  559. echo 'if (!$link = mysql_connect("localhost", $username, $password)) {' >> $postactiv_expire_script
  560. echo ' echo "Could not connect to mariadb";' >> $postactiv_expire_script
  561. echo ' exit;' >> $postactiv_expire_script
  562. echo '}' >> $postactiv_expire_script
  563. echo '' >> $postactiv_expire_script
  564. echo 'if (!mysql_select_db($database, $link)) {' >> $postactiv_expire_script
  565. echo ' echo "Could not select postactiv database";' >> $postactiv_expire_script
  566. echo ' exit;' >> $postactiv_expire_script
  567. echo '}' >> $postactiv_expire_script
  568. echo '' >> $postactiv_expire_script
  569. echo "\$notice_query=\"DELETE FROM notice WHERE created <= '\$oldate 01:01:01'\";" >> $postactiv_expire_script
  570. echo "\$conversation_query=\"DELETE FROM conversation WHERE created <= '$oldate 01:01:01'\";" >> $postactiv_expire_script
  571. echo "\$reply_query=\"DELETE FROM reply WHERE modified <= '\$oldate 01:01:01'\";" >> $postactiv_expire_script
  572. echo '' >> $postactiv_expire_script
  573. echo 'mysql_query($notice_query);' >> $postactiv_expire_script
  574. echo '$rowaff1=mysql_affected_rows();' >> $postactiv_expire_script
  575. echo 'mysql_query($conversation_query);' >> $postactiv_expire_script
  576. echo '$rowaff2=mysql_affected_rows();' >> $postactiv_expire_script
  577. echo 'mysql_query($reply_query);' >> $postactiv_expire_script
  578. echo '$rowaff3=mysql_affected_rows();' >> $postactiv_expire_script
  579. echo 'mysql_close();' >> $postactiv_expire_script
  580. echo '' >> $postactiv_expire_script
  581. echo 'echo "Expire postactiv posts: $rowaff1 notices, $rowaff2 conversations, and $rowaff3 replies deleted from database.\n";' >> $postactiv_expire_script
  582. chmod +x $postactiv_expire_script
  583. # Add a cron job
  584. if ! grep -q "$postactiv_expire_script" /etc/crontab; then
  585. echo "10 3 5 * * root /usr/bin/timeout 500 /usr/bin/php $postactiv_expire_script" >> /etc/crontab
  586. fi
  587. # remove old expire script
  588. if [ -f /etc/cron.weekly/clear-postactiv-database ]; then
  589. rm /etc/cron.weekly/clear-postactiv-database
  590. fi
  591. }
  592. function install_postactiv {
  593. if [ ! $ONION_ONLY ]; then
  594. ONION_ONLY='no'
  595. fi
  596. install_postactiv_main
  597. expire_postactiv_posts
  598. # unleash the daemons!
  599. /etc/cron.hourly/postactiv-daemons
  600. APP_INSTALLED=1
  601. }
  602. # NOTE: deliberately there is no "exit 0"