freedombone-app-htmly 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Htmly functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-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 writer"
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=1
  33. HTMLY_DOMAIN_NAME=
  34. HTMLY_CODE=
  35. HTMLY_ONION_PORT=8086
  36. HTMLY_REPO="https://github.com/danpros/htmly"
  37. HTMLY_COMMIT='bf5fe9486160be4da86d8987d3e5c977e1dc6d32'
  38. HTMLY_TITLE="My Blog"
  39. HTMLY_SUBTITLE="Another ${PROJECT_NAME} blog"
  40. htmly_variables=(HTMLY_REPO
  41. HTMLY_DOMAIN_NAME
  42. HTMLY_CODE
  43. HTMLY_TITLE
  44. HTMLY_SUBTITLE
  45. ONION_ONLY
  46. DDNS_PROVIDER
  47. MY_USERNAME)
  48. function logging_on_htmly {
  49. echo -n ''
  50. }
  51. function logging_off_htmly {
  52. echo -n ''
  53. }
  54. function set_avatar_from_url {
  55. AVATAR="$1"
  56. read_config_param "HTMLY_DOMAIN_NAME"
  57. BASE_DIR=/var/www/$HTMLY_DOMAIN_NAME/htdocs
  58. if [ ! -d $BASE_DIR/customimages ]; then
  59. mkdir $BASE_DIR/customimages
  60. fi
  61. # download the image
  62. cd $BASE_DIR/customimages
  63. # convert to png
  64. wget $AVATAR -O avatar
  65. if [[ $AVATAR == *".gif" ]]; then
  66. mv avatar avatar.gif
  67. mogrify -format png avatar.gif
  68. fi
  69. if [[ $AVATAR == *".jpg" ]]; then
  70. mv avatar avatar.jpg
  71. mogrify -format png avatar.jpg
  72. fi
  73. if [[ $AVATAR == *".jpeg" ]]; then
  74. mv avatar avatar.jpeg
  75. mogrify -format png avatar.jpeg
  76. fi
  77. if [ -f avatar ]; then
  78. mv avatar avatar.png
  79. fi
  80. # standard size
  81. mogrify -resize 150x150 avatar.png
  82. if [ ! -f $BASE_DIR/customimages/avatar.png ]; then
  83. echo $'Avatar image could not be downloaded'
  84. return
  85. fi
  86. chown -R www-data:www-data $BASE_DIR/customimages
  87. AVATAR_SET=1
  88. }
  89. function remove_user_htmly {
  90. remove_username="$1"
  91. ${PROJECT_NAME}-pass -u $remove_username --rmapp htmly
  92. if [ -f /var/www/${HTMLY_DOMAIN_NAME}/htdocs/config/users/${remove_username}.ini ]; then
  93. rm /var/www/${HTMLY_DOMAIN_NAME}/htdocs/config/users/${remove_username}.ini
  94. fi
  95. }
  96. function add_user_htmly {
  97. if [[ $(app_is_installed htmly) == "0" ]]; then
  98. echo '0'
  99. return
  100. fi
  101. new_username="$1"
  102. new_user_password="$2"
  103. ${PROJECT_NAME}-pass -u $new_username -a htmly -p "$new_user_password"
  104. if [ ! -d /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/users ]; then
  105. echo '2'
  106. return
  107. fi
  108. NEW_USER_PASSWORD_HASH=$(${PROJECT_NAME}-sec --htmlyhash "$new_user_password")
  109. if [ ${#NEW_USER_PASSWORD_HASH} -lt 8 ]; then
  110. echo '3'
  111. return
  112. fi
  113. echo ';Password' > /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/users/$new_username.ini
  114. echo "password = $NEW_USER_PASSWORD_HASH" >> /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/users/$new_username.ini
  115. echo 'encryption = password_hash' >> /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/users/$new_username.ini
  116. echo ';Role' >> /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/users/$new_username.ini
  117. echo 'role = admin' >> /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/users/$new_username.ini
  118. echo '0'
  119. }
  120. function configure_interactive_htmly {
  121. data=$(tempfile 2>/dev/null)
  122. trap "rm -f $data" 0 1 2 5 15
  123. dialog --title $"Change htmly avatar" \
  124. --backtitle $"Freedombone Control Panel" \
  125. --inputbox $"Enter a URL for an image. It should be approximately a square image." 8 75 2>$data
  126. sel=$?
  127. case $sel in
  128. 0)
  129. IMAGE_URL=$(<$data)
  130. if [ ${#IMAGE_URL} -gt 5 ]; then
  131. clear
  132. AVATAR_SET=
  133. set_avatar_from_url $IMAGE_URL
  134. if [ $AVATAR_SET ]; then
  135. dialog --title $"Change htmly avatar" \
  136. --msgbox $"Your htmly avatar has been changed" 6 40
  137. fi
  138. fi
  139. ;;
  140. esac
  141. }
  142. function install_interactive_htmly {
  143. if [ ! $ONION_ONLY ]; then
  144. ONION_ONLY='no'
  145. fi
  146. if [[ $ONION_ONLY != "no" ]]; then
  147. HTMLY_TITLE='My Htmly Blog'
  148. HTMLY_DOMAIN_NAME='htmly.local'
  149. write_config_param "HTMLY_TITLE" "$HTMLY_TITLE"
  150. write_config_param "HTMLY_DOMAIN_NAME" "$HTMLY_DOMAIN_NAME"
  151. else
  152. function_check interactive_site_details_with_title
  153. interactive_site_details_with_title "htmly" "HTMLY_TITLE" "HTMLY_DOMAIN_NAME" "HTMLY_CODE"
  154. fi
  155. APP_INSTALLED=1
  156. }
  157. function change_password_htmly {
  158. set_completion_param "htmly domain" "$HTMLY_DOMAIN_NAME"
  159. HTMLY_DOMAIN_NAME=$(get_completion_param "htmly domain")
  160. HTMLY_USERNAME="$1"
  161. HTMLY_PASSWORD="$2"
  162. if [ ${#HTMLY_PASSWORD} -lt 8 ]; then
  163. echo $'Htmly password is too short'
  164. return
  165. fi
  166. ${PROJECT_NAME}-pass -u "$HTMLY_USERNAME" -a htmly -p "$HTMLY_PASSWORD"
  167. HTMLY_PASSWORD_HASH=$(${PROJECT_NAME}-sec --htmlyhash "$HTMLY_PASSWORD")
  168. if [ ${#HTMLY_PASSWORD_HASH} -lt 8 ]; then
  169. echo $'Htmly admin password could not be hashed'
  170. exit 625728
  171. fi
  172. sed -i "s|password =.*|password = $HTMLY_PASSWORD_HASH|g" /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/users/$HTMLY_USERNAME.ini
  173. }
  174. function reconfigure_htmly {
  175. echo -n ''
  176. }
  177. function upgrade_htmly {
  178. CURR_HTMLY_COMMIT=$(get_completion_param "htmly commit")
  179. if [[ "$CURR_HTMLY_COMMIT" == "$HTMLY_COMMIT" ]]; then
  180. return
  181. fi
  182. read_config_param "HTMLY_DOMAIN_NAME"
  183. function_check set_repo_commit
  184. set_repo_commit /var/www/$HTMLY_DOMAIN_NAME/htdocs "htmly commit" "$HTMLY_COMMIT" $HTMLY_REPO
  185. }
  186. function backup_local_htmly {
  187. HTMLY_DOMAIN_NAME='htmly.local'
  188. if grep -q "htmly domain" $COMPLETION_FILE; then
  189. HTMLY_DOMAIN_NAME=$(get_completion_param "htmly domain")
  190. fi
  191. source_directory=/var/www/${HTMLY_DOMAIN_NAME}/htdocs
  192. if [ -d $source_directory ]; then
  193. dest_directory=htmly
  194. function_check suspend_site
  195. suspend_site ${HTMLY_DOMAIN_NAME}
  196. function_check backup_directory_to_usb
  197. backup_directory_to_usb $source_directory $dest_directory
  198. function_check restart_site
  199. restart_site
  200. fi
  201. }
  202. function restore_local_htmly {
  203. HTMLY_DOMAIN_NAME='htmly.local'
  204. if grep -q "htmly domain" $COMPLETION_FILE; then
  205. HTMLY_DOMAIN_NAME=$(get_completion_param "htmly domain")
  206. fi
  207. if [ $HTMLY_DOMAIN_NAME ]; then
  208. temp_restore_dir=/root/temphtmly
  209. if [ -d $USB_MOUNT/backup/htmly ]; then
  210. restore_directory_from_usb $temp_restore_dir htmly
  211. else
  212. restore_directory_from_usb $temp_restore_dir blog
  213. fi
  214. if [ -d /var/www/${HTMLY_DOMAIN_NAME}/htdocs ]; then
  215. if [ -d /var/www/${HTMLY_DOMAIN_NAME}/previous ]; then
  216. rm -rf /var/www/${HTMLY_DOMAIN_NAME}/previous
  217. fi
  218. mv /var/www/${HTMLY_DOMAIN_NAME}/htdocs /var/www/${HTMLY_DOMAIN_NAME}/previous
  219. fi
  220. if [ -d ${temp_restore_dir}/var/www/${HTMLY_DOMAIN_NAME}/htdocs ]; then
  221. temp_source_dir=$(find ${temp_restore_dir} -name htdocs)
  222. cp -r ${temp_source_dir} /var/www/${HTMLY_DOMAIN_NAME}/
  223. else
  224. if [ ! -d /var/www/${HTMLY_DOMAIN_NAME}/htdocs ]; then
  225. mkdir /var/www/${HTMLY_DOMAIN_NAME}/htdocs
  226. fi
  227. cp -r ${temp_restore_dir}/* /var/www/${HTMLY_DOMAIN_NAME}/htdocs/
  228. fi
  229. if [ ! "$?" = "0" ]; then
  230. if [ -d /var/www/${HTMLY_DOMAIN_NAME}/previous ]; then
  231. mv /var/www/${HTMLY_DOMAIN_NAME}/previous /var/www/${HTMLY_DOMAIN_NAME}/htdocs
  232. fi
  233. set_user_permissions
  234. backup_unmount_drive
  235. exit 54675
  236. fi
  237. rm -rf ${temp_restore_dir}
  238. if [ ! -d /var/www/${HTMLY_DOMAIN_NAME}/htdocs/content ]; then
  239. echo $"No content directory found after restoring htmly"
  240. set_user_permissions
  241. backup_unmount_drive
  242. exit 34578
  243. fi
  244. chown -R www-data:www-data /var/www/${HTMLY_DOMAIN_NAME}/htdocs
  245. # Ensure that the bundled SSL cert is being used
  246. if [ -f /etc/ssl/certs/${HTMLY_DOMAIN_NAME}.bundle.crt ]; then
  247. sed -i "s|${HTMLY_DOMAIN_NAME}.crt|${HTMLY_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${HTMLY_DOMAIN_NAME}
  248. fi
  249. for d in /home/*/ ; do
  250. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  251. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  252. if [ -d /var/www/${HTMLY_DOMAIN_NAME}/htdocs/content/$USERNAME/htmly/uncategorized/post ]; then
  253. mv /var/www/${HTMLY_DOMAIN_NAME}/htdocs/content/$USERNAME/htmly/*.md /var/www/${HTMLY_DOMAIN_NAME}/htdocs/content/$USERNAME/htmly/uncategorized/post
  254. fi
  255. fi
  256. done
  257. if [ -d /etc/letsencrypt/live/${HTMLY_DOMAIN_NAME} ]; then
  258. ln -s /etc/letsencrypt/live/${HTMLY_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${HTMLY_DOMAIN_NAME}.key
  259. ln -s /etc/letsencrypt/live/${HTMLY_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${HTMLY_DOMAIN_NAME}.pem
  260. fi
  261. fi
  262. }
  263. function backup_remote_htmly {
  264. if grep -q "htmly domain" $COMPLETION_FILE; then
  265. HTMLY_DOMAIN_NAME=$(get_completion_param "htmly domain")
  266. temp_backup_dir=/var/www/${HTMLY_DOMAIN_NAME}/htdocs
  267. if [ -d $temp_backup_dir ]; then
  268. echo $"Backing up htmly"
  269. backup_directory_to_friend $temp_backup_dir htmly
  270. echo $"Backup of htmly complete"
  271. else
  272. echo $"Htmly domain specified but not found in $temp_backup_dir"
  273. exit 2578
  274. fi
  275. fi
  276. }
  277. function restore_remote_htmly {
  278. if [ -d $SERVER_DIRECTORY/backup/htmly ]; then
  279. HTMLY_DOMAIN_NAME=$(get_completion_param "htmly domain")
  280. echo $"Restoring htmly installation $HTMLY_DOMAIN_NAME"
  281. temp_restore_dir=/root/temphtmly
  282. mkdir $temp_restore_dir
  283. function_check restore_directory_from_friend
  284. restore_directory_from_friend $temp_restore_dir htmly
  285. if [ -d /var/www/${HTMLY_DOMAIN_NAME}/htdocs ]; then
  286. if [ -d /var/www/${HTMLY_DOMAIN_NAME}/previous ]; then
  287. rm -rf /var/www/${HTMLY_DOMAIN_NAME}/previous
  288. fi
  289. mv /var/www/${HTMLY_DOMAIN_NAME}/htdocs /var/www/${HTMLY_DOMAIN_NAME}/previous
  290. fi
  291. if [ -d ${temp_restore_dir}/var/www/${HTMLY_DOMAIN_NAME}/htdocs ]; then
  292. temp_source_dir=$(find ${temp_restore_dir} -name htdocs)
  293. cp -r ${temp_source_dir} /var/www/${HTMLY_DOMAIN_NAME}/
  294. else
  295. if [ ! -d /var/www/${HTMLY_DOMAIN_NAME}/htdocs ]; then
  296. mkdir /var/www/${HTMLY_DOMAIN_NAME}/htdocs
  297. fi
  298. cp -r ${temp_restore_dir}/* /var/www/${HTMLY_DOMAIN_NAME}/htdocs/
  299. fi
  300. if [ ! "$?" = "0" ]; then
  301. if [ -d /var/www/${HTMLY_DOMAIN_NAME}/previous ]; then
  302. mv /var/www/${HTMLY_DOMAIN_NAME}/previous /var/www/${HTMLY_DOMAIN_NAME}/htdocs
  303. fi
  304. exit 593
  305. fi
  306. rm -rf ${temp_restore_dir}
  307. if [ ! -d /var/www/${HTMLY_DOMAIN_NAME}/htdocs/content ]; then
  308. echo $"No content directory found after restoring htmly"
  309. exit 287
  310. fi
  311. # Ensure that the bundled SSL cert is being used
  312. if [ -f /etc/ssl/certs/${HTMLY_DOMAIN_NAME}.bundle.crt ]; then
  313. sed -i "s|${HTMLY_DOMAIN_NAME}.crt|${HTMLY_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${HTMLY_DOMAIN_NAME}
  314. fi
  315. for d in /home/*/ ; do
  316. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  317. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  318. if [ -d /var/www/${HTMLY_DOMAIN_NAME}/htdocs/content/$USERNAME/htmly/uncategorized/post ]; then
  319. mv /var/www/${HTMLY_DOMAIN_NAME}/htdocs/content/$USERNAME/htmly/*.md /var/www/${HTMLY_DOMAIN_NAME}/htdocs/content/$USERNAME/htmly/uncategorized/post
  320. fi
  321. fi
  322. done
  323. if [ -d /etc/letsencrypt/live/${HTMLY_DOMAIN_NAME} ]; then
  324. ln -s /etc/letsencrypt/live/${HTMLY_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${HTMLY_DOMAIN_NAME}.key
  325. ln -s /etc/letsencrypt/live/${HTMLY_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${HTMLY_DOMAIN_NAME}.pem
  326. fi
  327. echo $"Restore of htmly complete"
  328. fi
  329. }
  330. function remove_htmly {
  331. if [ ${#HTMLY_DOMAIN_NAME} -eq 0 ]; then
  332. return
  333. fi
  334. read_config_param "HTMLY_DOMAIN_NAME"
  335. nginx_dissite $HTMLY_DOMAIN_NAME
  336. remove_certs ${HTMLY_DOMAIN_NAME}
  337. if [ -f /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME ]; then
  338. rm -f /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  339. fi
  340. if [ -d /var/www/$HTMLY_DOMAIN_NAME ]; then
  341. rm -rf /var/www/$HTMLY_DOMAIN_NAME
  342. fi
  343. remove_config_param HTMLY_DOMAIN_NAME
  344. remove_config_param HTMLY_CODE
  345. function_check remove_onion_service
  346. remove_onion_service htmly ${HTMLY_ONION_PORT}
  347. remove_completion_param "install_htmly"
  348. sed -i '/Htmly/d' $COMPLETION_FILE
  349. sed -i '/htmly/d' $COMPLETION_FILE
  350. function_check remove_ddns_domain
  351. remove_ddns_domain $HTMLY_DOMAIN_NAME
  352. }
  353. function install_htmly_social_networks {
  354. # set social networks
  355. if grep -q "social.hubzilla" /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini; then
  356. sed -i "s|;social.hubzilla|social.hubzilla|g" /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini
  357. sed -i "s|social.hubzilla.*|social.hubzilla = \"$HUBZILLA_DOMAIN_NAME\"|g" /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini
  358. fi
  359. if grep -q "social.gnusocial" /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini; then
  360. sed -i "s|;social.gnusocial|social.gnusocial|g" /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini
  361. sed -i "s|social.gnusocial.*|social.gnusocial = \"$MICROHTMLY_DOMAIN_NAME\"|g" /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini
  362. fi
  363. # clear proprietary social network strings
  364. sed -i 's|social.facebook.*|social.facebook = ""|g' /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini
  365. sed -i 's|social.twitter.*|social.twitter = ""|g' /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini
  366. sed -i 's|social.google.*|social.google = ""|g' /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini
  367. }
  368. function install_htmly_user {
  369. # create a user password
  370. if [ -f $IMAGE_PASSWORD_FILE ]; then
  371. HTMLY_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  372. else
  373. HTMLY_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  374. fi
  375. # create a user
  376. HTMLY_ADMIN_PASSWORD_HASH=$(${PROJECT_NAME}-sec --htmlyhash "$HTMLY_ADMIN_PASSWORD")
  377. if [ ${#HTMLY_ADMIN_PASSWORD_HASH} -lt 8 ]; then
  378. echo $'Htmly admin password could not be hashed'
  379. exit 625728
  380. fi
  381. echo ';Password' > /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/users/$MY_USERNAME.ini
  382. echo "password = $HTMLY_ADMIN_PASSWORD_HASH" >> /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/users/$MY_USERNAME.ini
  383. echo 'encryption = password_hash' >> /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/users/$MY_USERNAME.ini
  384. echo ';Role' >> /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/users/$MY_USERNAME.ini
  385. echo 'role = admin' >> /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/users/$MY_USERNAME.ini
  386. }
  387. function install_htmly_settings {
  388. cp /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini.example /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini
  389. sed -i "s|site.url.*|site.url = '/'|g" /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini
  390. sed -i "s|blog.title.*|blog.title = \"$HTMLY_TITLE\"|g" /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini
  391. sed -i "s|blog.tagline.*|blog.tagline = \"$HTMLY_SUBTITLE\"|g" /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini
  392. sed -i 's|timezone.*|timezone = "Europe/London"|g' /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini
  393. sed -i "s|Your name|$MY_NAME|g" /var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini
  394. }
  395. function install_htmly_website {
  396. function_check nginx_http_redirect
  397. nginx_http_redirect $HTMLY_DOMAIN_NAME
  398. echo 'server {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  399. echo ' listen 443 ssl;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  400. echo ' listen [::]:443 ssl;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  401. echo " root /var/www/$HTMLY_DOMAIN_NAME/htdocs;" >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  402. echo " server_name $HTMLY_DOMAIN_NAME;" >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  403. echo ' access_log /dev/null;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  404. echo " error_log /dev/null;" >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  405. echo ' index index.php;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  406. echo ' charset utf-8;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  407. echo ' proxy_read_timeout 86400s;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  408. function_check nginx_ssl
  409. nginx_ssl $HTMLY_DOMAIN_NAME
  410. function_check nginx_disable_sniffing
  411. nginx_disable_sniffing $HTMLY_DOMAIN_NAME
  412. echo ' add_header Strict-Transport-Security "max-age=0;";' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  413. echo '' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  414. echo ' # rewrite to front controller as default rule' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  415. echo ' location / {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  416. function_check nginx_limits
  417. nginx_limits $HTMLY_DOMAIN_NAME
  418. echo ' rewrite ^/(.*) /index.php?q=$uri&$args last;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  419. echo ' }' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  420. echo '' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  421. nginx_keybase ${HTMLY_DOMAIN_NAME}
  422. echo '' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  423. echo ' # statically serve these file types when possible' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  424. echo ' # otherwise fall back to front controller' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  425. echo ' # allow browser to cache them' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  426. echo ' # added .htm for advanced source code editor library' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  427. echo ' location ~* \.(jpg|jpeg|gif|png|ico|css|js|htm|html|ttf|woff|svg)$ {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  428. echo ' expires 30d;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  429. echo ' try_files $uri /index.php?q=$uri&$args;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  430. echo ' }' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  431. echo '' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  432. echo ' # block these file types' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  433. echo ' location ~* \.(tpl|md|tgz|log|out)$ {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  434. echo ' deny all;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  435. echo ' }' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  436. echo '' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  437. echo ' # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  438. echo ' # or a unix socket' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  439. echo ' location ~* \.php$ {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  440. echo ' # Zero-day exploit defense.' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  441. echo ' # http://forum.nginx.org/read.php?2,88845,page=3' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  442. echo " # Won't work properly (404 error) if the file is not stored on this" >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  443. echo " # server, which is entirely possible with php-fpm/php-fcgi." >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  444. echo " # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on" >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  445. echo " # another machine. And then cross your fingers that you won't get hacked." >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  446. echo ' try_files $uri $uri/ /index.php;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  447. echo ' # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  448. echo ' fastcgi_split_path_info ^(.+\.php)(/.+)$;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  449. echo ' # With php-cgi alone:' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  450. echo ' # fastcgi_pass 127.0.0.1:9000;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  451. echo ' # With php-fpm:' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  452. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  453. echo ' include fastcgi_params;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  454. echo ' fastcgi_read_timeout 30;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  455. echo ' fastcgi_index index.php;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  456. echo ' fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  457. echo ' }' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  458. echo '' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  459. echo ' # deny access to all dot files' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  460. echo ' location ~ /\. {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  461. echo ' deny all;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  462. echo ' }' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  463. echo '' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  464. echo ' #deny access to store' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  465. echo ' location ~ /store {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  466. echo ' deny all;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  467. echo ' }' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  468. echo ' location ~ /(data|conf|bin|inc)/ {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  469. echo ' deny all;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  470. echo ' }' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  471. echo ' location ~ /\.ht {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  472. echo ' deny all;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  473. echo ' }' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  474. echo '}' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  475. echo '' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  476. }
  477. function install_htmly_website_onion {
  478. echo 'server {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  479. echo " listen 127.0.0.1:${HTMLY_ONION_PORT} default_server;" >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  480. echo " root /var/www/$HTMLY_DOMAIN_NAME/htdocs;" >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  481. echo " server_name $HTMLY_DOMAIN_NAME;" >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  482. echo ' access_log /dev/null;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  483. echo " error_log /dev/null;" >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  484. echo ' index index.php;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  485. echo ' charset utf-8;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  486. echo ' proxy_read_timeout 86400s;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  487. function_check nginx_disable_sniffing
  488. nginx_disable_sniffing $HTMLY_DOMAIN_NAME
  489. echo ' add_header Strict-Transport-Security "max-age=0;";' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  490. echo '' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  491. echo ' # rewrite to front controller as default rule' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  492. echo ' location / {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  493. function_check nginx_limits
  494. nginx_limits $HTMLY_DOMAIN_NAME
  495. echo ' rewrite ^/(.*) /index.php?q=$uri&$args last;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  496. echo ' }' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  497. echo '' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  498. nginx_keybase ${HTMLY_DOMAIN_NAME}
  499. echo '' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  500. echo ' # statically serve these file types when possible' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  501. echo ' # otherwise fall back to front controller' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  502. echo ' # allow browser to cache them' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  503. echo ' # added .htm for advanced source code editor library' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  504. echo ' location ~* \.(jpg|jpeg|gif|png|ico|css|js|htm|html|ttf|woff|svg)$ {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  505. echo ' expires 30d;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  506. echo ' try_files $uri /index.php?q=$uri&$args;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  507. echo ' }' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  508. echo '' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  509. echo ' # block these file types' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  510. echo ' location ~* \.(tpl|md|tgz|log|out)$ {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  511. echo ' deny all;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  512. echo ' }' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  513. echo '' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  514. echo ' # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  515. echo ' # or a unix socket' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  516. echo ' location ~* \.php$ {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  517. function_check nginx_limits
  518. nginx_limits $HTMLY_DOMAIN_NAME
  519. echo ' # Zero-day exploit defense.' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  520. echo ' # http://forum.nginx.org/read.php?2,88845,page=3' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  521. echo " # Won't work properly (404 error) if the file is not stored on this" >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  522. echo " # server, which is entirely possible with php-fpm/php-fcgi." >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  523. echo " # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on" >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  524. echo " # another machine. And then cross your fingers that you won't get hacked." >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  525. echo ' try_files $uri $uri/ /index.php;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  526. echo ' # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  527. echo ' fastcgi_split_path_info ^(.+\.php)(/.+)$;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  528. echo ' # With php-cgi alone:' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  529. echo ' # fastcgi_pass 127.0.0.1:9000;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  530. echo ' # With php-fpm:' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  531. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  532. echo ' include fastcgi_params;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  533. echo ' fastcgi_read_timeout 30;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  534. echo ' fastcgi_index index.php;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  535. echo ' fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  536. echo ' }' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  537. echo '' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  538. echo ' # deny access to all dot files' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  539. echo ' location ~ /\. {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  540. echo ' deny all;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  541. echo ' }' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  542. echo '' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  543. echo ' #deny access to store' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  544. echo ' location ~ /store {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  545. echo ' deny all;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  546. echo ' }' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  547. echo ' location ~ /(data|conf|bin|inc)/ {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  548. echo ' deny all;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  549. echo ' }' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  550. echo ' location ~ /\.ht {' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  551. echo ' deny all;' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  552. echo ' }' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  553. echo '}' >> /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  554. }
  555. function install_htmly_from_repo {
  556. if [ ! -d /var/www/$HTMLY_DOMAIN_NAME ]; then
  557. mkdir /var/www/$HTMLY_DOMAIN_NAME
  558. fi
  559. cd /var/www/$HTMLY_DOMAIN_NAME
  560. if [ -d /repos/htmly ]; then
  561. mkdir htdocs
  562. cp -r -p /repos/htmly/. htdocs
  563. cd htdocs
  564. git pull
  565. else
  566. git_clone $HTMLY_REPO htdocs
  567. fi
  568. cd htdocs
  569. git checkout $HTMLY_COMMIT -b $HTMLY_COMMIT
  570. set_completion_param "htmly commit" "$HTMLY_COMMIT"
  571. }
  572. function install_htmly {
  573. if [ ! $ONION_ONLY ]; then
  574. ONION_ONLY='no'
  575. fi
  576. if [ ! $HTMLY_DOMAIN_NAME ]; then
  577. echo $'The htmly domain name was not specified'
  578. exit 5062
  579. fi
  580. # for the avatar changing command
  581. apt-get -yq install imagemagick libfcgi0ldbl
  582. function_check install_htmly_from_repo
  583. install_htmly_from_repo
  584. if [[ $ONION_ONLY == "no" ]]; then
  585. function_check install_htmly_website
  586. install_htmly_website
  587. else
  588. echo -n '' > /etc/nginx/sites-available/$HTMLY_DOMAIN_NAME
  589. fi
  590. function_check install_htmly_website_onion
  591. install_htmly_website_onion
  592. function_check create_site_certificate
  593. create_site_certificate $HTMLY_DOMAIN_NAME 'yes'
  594. function_check configure_php
  595. configure_php
  596. function_check install_htmly_settings
  597. install_htmly_settings
  598. function_check install_htmly_social_networks
  599. install_htmly_social_networks
  600. function_check install_htmly_user
  601. install_htmly_user
  602. chown -R www-data:www-data /var/www/$HTMLY_DOMAIN_NAME/htdocs
  603. HTMLY_ONION_HOSTNAME=$(add_onion_service htmly 80 ${HTMLY_ONION_PORT})
  604. function_check nginx_ensite
  605. nginx_ensite $HTMLY_DOMAIN_NAME
  606. systemctl restart php7.0-fpm
  607. systemctl restart nginx
  608. ${PROJECT_NAME}-pass -u $MY_USERNAME -a htmly -p "$HTMLY_ADMIN_PASSWORD"
  609. function_check add_ddns_domain
  610. add_ddns_domain $HTMLY_DOMAIN_NAME
  611. set_completion_param "htmly domain" "$HTMLY_DOMAIN_NAME"
  612. APP_INSTALLED=1
  613. }
  614. # NOTE: deliberately no exit 0