freedombone-app-postactiv 26KB

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