freedombone-app-postactiv 31KB

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