freedombone-utils-gnusocialtools 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # functions common to GNU Social server varieties
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2017 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. QVITTER_THEME_REPO="https://git.gnu.io/h2p/Qvitter.git"
  31. QVITTER_THEME_COMMIT='a7f82628402db3a7579bb9b2877da3c5737da77b'
  32. PLEROMA_REPO="https://gitgud.io/lambadalambda/pleroma-fe"
  33. PLEROMA_COMMIT='7ee87c7618bfba986ec7a04581273629a1db9983'
  34. function pleroma_set_background_image_from_url {
  35. domain_name="$1"
  36. url="$2"
  37. title="$3"
  38. if [ ${#domain_name} -eq 0 ]; then
  39. echo "1"
  40. return
  41. fi
  42. ext=
  43. if [ ${#url} -gt 0 ]; then
  44. if [[ "$url" == *".jpeg" || "$url" == *".jpg" ]]; then
  45. ext="jpg"
  46. fi
  47. if [[ "$url" == *".png" ]]; then
  48. ext="png"
  49. fi
  50. if [[ "$url" == *".gif" ]]; then
  51. ext="gif"
  52. fi
  53. fi
  54. if [ ${#ext} -gt 0 ]; then
  55. if [ -d /var/www/${domain_name}/htdocs/static ]; then
  56. cd /var/www/${domain_name}/htdocs/static
  57. # remove any existing image
  58. if [ -f bg_custom.${ext} ]; then
  59. rm bg_custom.${ext}
  60. fi
  61. # get the new image
  62. wget "$url" -O bg_custom.${ext}
  63. if [ ! -f bg_custom.${ext} ]; then
  64. echo "$url"
  65. echo $'Custom background image for pleroma could not be downloaded'
  66. echo "1"
  67. return
  68. fi
  69. sed -i "s|\"background\":.*|\"background\": \"/static/bg_custom.${ext}\",|g" config.json
  70. fi
  71. else
  72. echo "2"
  73. return
  74. fi
  75. # customise the logo
  76. if [ -f /var/www/${domain_name}/htdocs/static/logo.png ]; then
  77. if [ -f ~/freedombone/img/logo_fbone3.png ]; then
  78. cp ~/freedombone/img/logo_fbone3.png /var/www/${domain_name}/htdocs/static/logo.png
  79. else
  80. if [ -f /home/$MY_USERNAME/freedombone/img/logo_fbone3.png ]; then
  81. cp /home/$MY_USERNAME/freedombone/img/logo_fbone3.png /var/www/${domain_name}/htdocs/static/logo.png
  82. fi
  83. fi
  84. fi
  85. # customise the title
  86. if [ -f /var/www/${domain_name}/htdocs/static/config.json ]; then
  87. sed -i "s|\"name\":.*|\"name\": \"${title}\",|g" /var/www/${domain_name}/htdocs/static/config.json
  88. fi
  89. echo "0"
  90. }
  91. function install_qvitter {
  92. domain_name=$1
  93. app_name=$2
  94. # update to the next commit
  95. function_check set_repo_commit
  96. set_repo_commit /var/www/${domain_name}/htdocs/local/plugins/Qvitter "${app_name} theme commit" "$QVITTER_THEME_COMMIT" $QVITTER_THEME_REPO
  97. # customise with project logo
  98. if [ -f /var/www/${domain_name}/htdocs/local/plugins/Qvitter/img/sprite.png ]; then
  99. if [ -f ~/freedombone/img/logo_fbone3.png ]; then
  100. cp ~/freedombone/img/logo_fbone3.png /var/www/${domain_name}/htdocs/local/plugins/Qvitter/img/sprite.png
  101. else
  102. if [ -f /home/$MY_USERNAME/freedombone/img/logo_fbone3.png ]; then
  103. cp /home/$MY_USERNAME/freedombone/img/logo_fbone3.png /var/www/${domain_name}/htdocs/local/plugins/Qvitter/img/sprite.png
  104. fi
  105. fi
  106. fi
  107. if [[ $(app_is_installed "${app_name}_theme") == "1" ]]; then
  108. return
  109. fi
  110. apt-get -yq install wget
  111. if [ ! -d /var/www/${domain_name}/htdocs/local/plugins ]; then
  112. mkdir -p /var/www/${domain_name}/htdocs/local/plugins
  113. fi
  114. cd /var/www/${domain_name}/htdocs/local/plugins
  115. function_check git_clone
  116. git_clone $QVITTER_THEME_REPO Qvitter
  117. cd /var/www/${domain_name}/htdocs/local/plugins/Qvitter
  118. git checkout $QVITTER_THEME_COMMIT -b $QVITTER_THEME_COMMIT
  119. config_file=/var/www/${domain_name}/htdocs/config.php
  120. if ! grep -q "addPlugin('Qvitter')" $config_file; then
  121. echo "" >> $config_file
  122. echo "// Qvitter settings" >> $config_file
  123. echo "addPlugin('Qvitter');" >> $config_file
  124. echo "\$config['site']['qvitter']['enabledbydefault'] = true;" >> $config_file
  125. echo "\$config['site']['qvitter']['defaultbackgroundcolor'] = '#f4f4f4';" >> $config_file
  126. echo "\$config['site']['qvitter']['defaultlinkcolor'] = '#0084B4';" >> $config_file
  127. echo "\$config['site']['qvitter']['timebetweenpolling'] = 30000; // 30 secs" >> $config_file
  128. if [[ $ONION_ONLY == 'no' ]]; then
  129. echo "\$config['site']['qvitter']['urlshortenerapiurl'] = 'http://qttr.at/shortener.php';" >> $config_file
  130. echo "\$config['site']['qvitter']['urlshortenersignature'] = 'b6afeec983';" >> $config_file
  131. fi
  132. echo "\$config['site']['qvitter']['favicon'] = 'img/favicon.ico?v=4';" >> $config_file
  133. echo "\$config['site']['qvitter']['sprite'] = Plugin::staticPath('Qvitter', '').'img/sprite.png?v=40';" >> $config_file
  134. echo "\$config['site']['qvitter']['enablewelcometext'] = false;" >> $config_file
  135. echo "\$config['site']['qvitter']['blocked_ips'] = array();" >> $config_file
  136. fi
  137. # customise with project logo
  138. if [ -f /var/www/${domain_name}/htdocs/local/plugins/Qvitter/img/sprite.png ]; then
  139. if [ -f ~/freedombone/img/gnusocial_sprite.png ]; then
  140. cp ~/freedombone/img/gnusocial_sprite.png /var/www/${domain_name}/htdocs/local/plugins/Qvitter/img/sprite.png
  141. fi
  142. fi
  143. chown -R www-data:www-data /var/www/${domain_name}/htdocs
  144. cd /var/www/${domain_name}/htdocs
  145. php scripts/upgrade.php
  146. php scripts/checkschema.php
  147. chown -R www-data:www-data /var/www/${domain_name}/htdocs
  148. set_completion_param "${app_name} theme commit" "$QVITTER_THEME_COMMIT"
  149. install_completed ${app_name}_theme
  150. }
  151. function install_gnusocial_default_background {
  152. gnusocial_type=$1
  153. domain_name=$2
  154. # customise the logo
  155. if [ -f /var/www/${domain_name}/htdocs/static/logo.png ]; then
  156. if [ -f ~/freedombone/img/logo_fbone3.png ]; then
  157. cp ~/freedombone/img/logo_fbone3.png /var/www/${domain_name}/htdocs/static/logo.png
  158. else
  159. if [ -f /home/$MY_USERNAME/freedombone/img/logo_fbone3.png ]; then
  160. cp /home/$MY_USERNAME/freedombone/img/logo_fbone3.png /var/www/${domain_name}/htdocs/static/logo.png
  161. fi
  162. fi
  163. fi
  164. if [ ! -f /var/www/${domain_name}/htdocs/static/bg.jpg ]; then
  165. return
  166. fi
  167. if [ -f ~/freedombone/img/backgrounds/${gnusocial_type}.jpg ]; then
  168. cp ~/freedombone/img/backgrounds/${gnusocial_type}.jpg /var/www/${domain_name}/htdocs/static/bg.jpg
  169. else
  170. if [ -f /home/$MY_USERNAME/freedombone/img/backgrounds/${gnusocial_type}.jpg ]; then
  171. cp /home/$MY_USERNAME/freedombone/img/backgrounds${gnusocial_type}.jpg /var/www/${domain_name}/htdocs/static/bg.jpg
  172. fi
  173. fi
  174. }
  175. function install_pleroma {
  176. app_name="$1"
  177. pleroma_domain="$2"
  178. background_url="$3"
  179. title="$4"
  180. if [ ! -d $INSTALL_DIR/pleroma ]; then
  181. function_check git_clone
  182. git_clone $PLEROMA_REPO $INSTALL_DIR/pleroma
  183. if [ ! -d $INSTALL_DIR/pleroma ]; then
  184. echo $'Unable to clone pleroma repo'
  185. exit 682252
  186. fi
  187. fi
  188. cd $INSTALL_DIR/pleroma
  189. git checkout $PLEROMA_COMMIT -b $PLEROMA_COMMIT
  190. set_completion_param "${app_name} pleroma commit" "$PLEROMA_COMMIT"
  191. npm install -g yarn
  192. npm install -g eslint@3.19.0
  193. npm install -g eslint-plugin-promise@3.5.0
  194. npm install -g moment@2.18.1
  195. yarn
  196. npm run build
  197. if [ ! -d $INSTALL_DIR/pleroma/dist ]; then
  198. echo 'Unable to build pleroma'
  199. exit 7629352
  200. fi
  201. if [ ! -f $INSTALL_DIR/pleroma/dist/index.html ]; then
  202. echo $'Unable to build pleroma index.html'
  203. exit 5282682
  204. fi
  205. cp -r $INSTALL_DIR/pleroma/dist/* /var/www/${pleroma_domain}/htdocs/
  206. pleroma_set_background_image_from_url "$pleroma_domain" "$background_url" "$title"
  207. nginx_site=/etc/nginx/sites-available/${pleroma_domain}
  208. sed -i 's|index index.php;|index index.html;|g' $nginx_site
  209. if [ -f /var/www/${pleroma_domain}/htdocs/index.php ]; then
  210. mv /var/www/${pleroma_domain}/htdocs/index.php /var/www/${pleroma_domain}/htdocs/index_qvitter.php
  211. fi
  212. sed -i 's|index.php|index_qvitter.php|g' $nginx_site
  213. chown -R www-data:www-data /var/www/${pleroma_domain}/htdocs
  214. }
  215. function upgrade_pleroma {
  216. domain_name="$1"
  217. app_name="$2"
  218. background_url="$3"
  219. title="$4"
  220. if [ -d $INSTALL_DIR/pleroma ]; then
  221. set_repo_commit $INSTALL_DIR/pleroma "${app_name} pleroma commit" "$PLEROMA_COMMIT" $PLEROMA_REPO
  222. cd $INSTALL_DIR/pleroma
  223. npm run build
  224. if [ ! -d $INSTALL_DIR/pleroma/dist ]; then
  225. echo 'Unable to build pleroma'
  226. exit 268362
  227. fi
  228. if [ -f /var/www/${domain_name}/htdocs/static/config.json ]; then
  229. cp /var/www/${domain_name}/htdocs/static/config.json /var/www/${domain_name}/htdocs/static/config.json.old
  230. fi
  231. cp -r $INSTALL_DIR/pleroma/dist/* /var/www/${domain_name}/htdocs/
  232. pleroma_set_background_image_from_url "$domain_name" "$background_url" "$title"
  233. if [ -f /var/www/${domain_name}/htdocs/static/config.json.old ]; then
  234. mv /var/www/${domain_name}/htdocs/static/config.json.old /var/www/${domain_name}/htdocs/static/config.json
  235. fi
  236. if [ -f /var/www/${domain_name}/htdocs/index.php ]; then
  237. mv /var/www/${domain_name}/htdocs/index.php /var/www/${domain_name}/htdocs/index_qvitter.php
  238. fi
  239. chown -R www-data:www-data /var/www/${domain_name}/htdocs
  240. else
  241. install_pleroma "${app_name}" "${domain_name}" "${background_url}" "${title}"
  242. fi
  243. }
  244. function gnusocial_hourly_script {
  245. gnusocial_type=$1
  246. domain_name=$2
  247. # check that the daemon is running
  248. echo '#!/bin/bash' > /etc/cron.hourly/${gnusocial_type}-daemons
  249. echo -n 'daemon_lines=$(ps aux | grep "' >> /etc/cron.hourly/${gnusocial_type}-daemons
  250. echo -n "${domain_name}" >> /etc/cron.hourly/${gnusocial_type}-daemons
  251. echo '/htdocs/scripts/queuedaemon.php" | grep "/var/www")' >> /etc/cron.hourly/${gnusocial_type}-daemons
  252. echo "cd /var/www/${domain_name}/htdocs" >> /etc/cron.hourly/${gnusocial_type}-daemons
  253. echo 'if [[ $daemon_lines != *"/var/www/"* ]]; then' >> /etc/cron.hourly/${gnusocial_type}-daemons
  254. echo " ADMIN_USER=\$(cat $COMPLETION_FILE | grep 'Admin user' | awk -F ':' '{print \$2}')" >> /etc/cron.hourly/${gnusocial_type}-daemons
  255. echo ' MY_EMAIL_ADDRESS=$ADMIN_USER@$HOSTNAME' >> /etc/cron.hourly/${gnusocial_type}-daemons
  256. echo -n " echo \"Restarting ${gnusocial_type} daemons\" | mail -s \"${gnusocial_type} " >> /etc/cron.hourly/${gnusocial_type}-daemons
  257. echo 'daemons not found" $MY_EMAIL_ADDRESS' >> /etc/cron.hourly/${gnusocial_type}-daemons
  258. echo ' scripts/startdaemons.sh' >> /etc/cron.hourly/${gnusocial_type}-daemons
  259. echo 'fi' >> /etc/cron.hourly/${gnusocial_type}-daemons
  260. echo 'php scripts/delete_orphan_files.php > /dev/null' >> /etc/cron.hourly/${gnusocial_type}-daemons
  261. echo 'php scripts/clean_thumbnails.php -y > /dev/null' >> /etc/cron.hourly/${gnusocial_type}-daemons
  262. echo 'php scripts/clean_file_table.php -y > /dev/null' >> /etc/cron.hourly/${gnusocial_type}-daemons
  263. echo 'php scripts/upgrade.php > /dev/null' >> /etc/cron.hourly/${gnusocial_type}-daemons
  264. chmod +x /etc/cron.hourly/${gnusocial_type}-daemons
  265. }
  266. function expire_gnusocial_posts {
  267. domain_name=$1
  268. gnusocial_type=$2
  269. expire_months=$3
  270. if [ ! $expire_months ]; then
  271. expire_months=3
  272. fi
  273. # To prevent the database size from growing endlessly this script expires posts
  274. # after a number of months
  275. if [ ! -d /var/www/${domain_name}/htdocs ]; then
  276. return
  277. fi
  278. gnusocial_expire_script=/usr/bin/${gnusocial_type}-expire
  279. echo '<?php' > $gnusocial_expire_script
  280. echo '' >> $gnusocial_expire_script
  281. echo "// ${gnusocial_type} post expiry script, based on StatExpire by Tony Baldwin" >> $gnusocial_expire_script
  282. echo '// https://github.com/tonybaldwin/statexpire' >> $gnusocial_expire_script
  283. echo '' >> $gnusocial_expire_script
  284. echo "\$oldate=date((\"Y-m-d\"), strtotime(\"-${expire_months} months\"));" >> $gnusocial_expire_script
  285. echo '$username="root";' >> $gnusocial_expire_script
  286. echo "\$password=shell_exec('${PROJECT_NAME}-pass -u root -a mariadb');" >> $gnusocial_expire_script
  287. echo "\$database=\"${gnusocial_type}\";" >> $gnusocial_expire_script
  288. echo '' >> $gnusocial_expire_script
  289. echo 'if (!$link = mysql_connect("localhost", $username, $password)) {' >> $gnusocial_expire_script
  290. echo ' echo "Could not connect to mariadb";' >> $gnusocial_expire_script
  291. echo ' exit;' >> $gnusocial_expire_script
  292. echo '}' >> $gnusocial_expire_script
  293. echo '' >> $gnusocial_expire_script
  294. echo 'if (!mysql_select_db($database, $link)) {' >> $gnusocial_expire_script
  295. echo " echo \"Could not select ${gnusocial_type} database\";" >> $gnusocial_expire_script
  296. echo ' exit;' >> $gnusocial_expire_script
  297. echo '}' >> $gnusocial_expire_script
  298. echo '' >> $gnusocial_expire_script
  299. echo "\$notice_query=\"DELETE FROM notice WHERE created <= '\$oldate 01:01:01'\";" >> $gnusocial_expire_script
  300. echo "\$conversation_query=\"DELETE FROM conversation WHERE created <= '$oldate 01:01:01'\";" >> $gnusocial_expire_script
  301. echo "\$reply_query=\"DELETE FROM reply WHERE modified <= '\$oldate 01:01:01'\";" >> $gnusocial_expire_script
  302. echo '' >> $gnusocial_expire_script
  303. echo 'mysql_query($notice_query);' >> $gnusocial_expire_script
  304. echo '$rowaff1=mysql_affected_rows();' >> $gnusocial_expire_script
  305. echo 'mysql_query($conversation_query);' >> $gnusocial_expire_script
  306. echo '$rowaff2=mysql_affected_rows();' >> $gnusocial_expire_script
  307. echo 'mysql_query($reply_query);' >> $gnusocial_expire_script
  308. echo '$rowaff3=mysql_affected_rows();' >> $gnusocial_expire_script
  309. echo 'mysql_close();' >> $gnusocial_expire_script
  310. echo '' >> $gnusocial_expire_script
  311. echo -n "echo \"Expire ${gnusocial_type} posts: " >> $gnusocial_expire_script
  312. echo '$rowaff1 notices, $rowaff2 conversations, and $rowaff3 replies deleted from database.\n";' >> $gnusocial_expire_script
  313. chmod +x $gnusocial_expire_script
  314. # Add a cron job
  315. if ! grep -q "${gnusocial_type}_expire_script" /etc/crontab; then
  316. echo "10 3 5 * * root /usr/bin/timeout 500 /usr/bin/php ${gnusocial_expire_script}" >> /etc/crontab
  317. fi
  318. # remove old expire script
  319. if [ -f /etc/cron.weekly/clear-${gnusocial_type}-database ]; then
  320. rm /etc/cron.weekly/clear-${gnusocial_type}-database
  321. fi
  322. }
  323. # NOTE: deliberately there is no "exit 0"