freedombone-app-htmly 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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-2018 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" || exit 2468246
  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';
  114. echo "password = $NEW_USER_PASSWORD_HASH";
  115. echo 'encryption = password_hash';
  116. echo ';Role';
  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=$(mktemp 2>/dev/null)
  122. dialog --title $"Change htmly avatar" \
  123. --backtitle $"Freedombone Control Panel" \
  124. --inputbox $"Enter a URL for an image. It should be approximately a square image." 8 75 2>"$data"
  125. sel=$?
  126. case $sel in
  127. 0)
  128. IMAGE_URL=$(<"$data")
  129. if [ ${#IMAGE_URL} -gt 5 ]; then
  130. clear
  131. AVATAR_SET=
  132. set_avatar_from_url "$IMAGE_URL"
  133. if [ $AVATAR_SET ]; then
  134. dialog --title $"Change htmly avatar" \
  135. --msgbox $"Your htmly avatar has been changed" 6 40
  136. fi
  137. fi
  138. ;;
  139. esac
  140. rm -f "$data"
  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. # shellcheck disable=SC2086
  219. mv /var/www/${HTMLY_DOMAIN_NAME}/htdocs /var/www/${HTMLY_DOMAIN_NAME}/previous
  220. fi
  221. if [ -d "${temp_restore_dir}/var/www/${HTMLY_DOMAIN_NAME}/htdocs" ]; then
  222. temp_source_dir=$(find ${temp_restore_dir} -name htdocs)
  223. cp -r "${temp_source_dir}" "/var/www/${HTMLY_DOMAIN_NAME}/"
  224. else
  225. if [ ! -d "/var/www/${HTMLY_DOMAIN_NAME}/htdocs" ]; then
  226. mkdir "/var/www/${HTMLY_DOMAIN_NAME}/htdocs"
  227. fi
  228. cp -r "${temp_restore_dir}/"* "/var/www/${HTMLY_DOMAIN_NAME}/htdocs/"
  229. fi
  230. # shellcheck disable=SC2181
  231. if [ ! "$?" = "0" ]; then
  232. if [ -d "/var/www/${HTMLY_DOMAIN_NAME}/previous" ]; then
  233. # shellcheck disable=SC2086
  234. mv /var/www/${HTMLY_DOMAIN_NAME}/previous /var/www/${HTMLY_DOMAIN_NAME}/htdocs
  235. fi
  236. set_user_permissions
  237. backup_unmount_drive
  238. exit 54675
  239. fi
  240. rm -rf ${temp_restore_dir}
  241. if [ ! -d "/var/www/${HTMLY_DOMAIN_NAME}/htdocs/content" ]; then
  242. echo $"No content directory found after restoring htmly"
  243. set_user_permissions
  244. backup_unmount_drive
  245. exit 34578
  246. fi
  247. chown -R www-data:www-data "/var/www/${HTMLY_DOMAIN_NAME}/htdocs"
  248. # Ensure that the bundled SSL cert is being used
  249. if [ -f "/etc/ssl/certs/${HTMLY_DOMAIN_NAME}.bundle.crt" ]; then
  250. sed -i "s|${HTMLY_DOMAIN_NAME}.crt|${HTMLY_DOMAIN_NAME}.bundle.crt|g" "/etc/nginx/sites-available/${HTMLY_DOMAIN_NAME}"
  251. fi
  252. for d in /home/*/ ; do
  253. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  254. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  255. if [ -d "/var/www/${HTMLY_DOMAIN_NAME}/htdocs/content/$USERNAME/htmly/uncategorized/post" ]; then
  256. # shellcheck disable=SC2086
  257. mv /var/www/${HTMLY_DOMAIN_NAME}/htdocs/content/$USERNAME/htmly/*.md /var/www/${HTMLY_DOMAIN_NAME}/htdocs/content/$USERNAME/htmly/uncategorized/post
  258. fi
  259. fi
  260. done
  261. if [ -d "/etc/letsencrypt/live/${HTMLY_DOMAIN_NAME}" ]; then
  262. ln -s "/etc/letsencrypt/live/${HTMLY_DOMAIN_NAME}/privkey.pem" "/etc/ssl/private/${HTMLY_DOMAIN_NAME}.key"
  263. ln -s "/etc/letsencrypt/live/${HTMLY_DOMAIN_NAME}/fullchain.pem" "/etc/ssl/certs/${HTMLY_DOMAIN_NAME}.pem"
  264. fi
  265. fi
  266. }
  267. function backup_remote_htmly {
  268. if grep -q "htmly domain" "$COMPLETION_FILE"; then
  269. HTMLY_DOMAIN_NAME=$(get_completion_param "htmly domain")
  270. temp_backup_dir=/var/www/${HTMLY_DOMAIN_NAME}/htdocs
  271. if [ -d "$temp_backup_dir" ]; then
  272. echo $"Backing up htmly"
  273. backup_directory_to_friend "$temp_backup_dir" htmly
  274. echo $"Backup of htmly complete"
  275. else
  276. echo $"Htmly domain specified but not found in $temp_backup_dir"
  277. exit 2578
  278. fi
  279. fi
  280. }
  281. function restore_remote_htmly {
  282. if [ -d "$SERVER_DIRECTORY/backup/htmly" ]; then
  283. HTMLY_DOMAIN_NAME=$(get_completion_param "htmly domain")
  284. echo $"Restoring htmly installation $HTMLY_DOMAIN_NAME"
  285. temp_restore_dir=/root/temphtmly
  286. mkdir $temp_restore_dir
  287. function_check restore_directory_from_friend
  288. restore_directory_from_friend $temp_restore_dir htmly
  289. if [ -d "/var/www/${HTMLY_DOMAIN_NAME}/htdocs" ]; then
  290. if [ -d "/var/www/${HTMLY_DOMAIN_NAME}/previous" ]; then
  291. rm -rf "/var/www/${HTMLY_DOMAIN_NAME}/previous"
  292. fi
  293. # shellcheck disable=SC2086
  294. mv /var/www/${HTMLY_DOMAIN_NAME}/htdocs /var/www/${HTMLY_DOMAIN_NAME}/previous
  295. fi
  296. if [ -d "${temp_restore_dir}/var/www/${HTMLY_DOMAIN_NAME}/htdocs" ]; then
  297. temp_source_dir=$(find ${temp_restore_dir} -name htdocs)
  298. cp -r "${temp_source_dir}" "/var/www/${HTMLY_DOMAIN_NAME}/"
  299. else
  300. if [ ! -d "/var/www/${HTMLY_DOMAIN_NAME}/htdocs" ]; then
  301. mkdir "/var/www/${HTMLY_DOMAIN_NAME}/htdocs"
  302. fi
  303. cp -r "${temp_restore_dir}/"* "/var/www/${HTMLY_DOMAIN_NAME}/htdocs/"
  304. fi
  305. # shellcheck disable=SC2181
  306. if [ ! "$?" = "0" ]; then
  307. if [ -d "/var/www/${HTMLY_DOMAIN_NAME}/previous" ]; then
  308. # shellcheck disable=SC2086
  309. mv /var/www/${HTMLY_DOMAIN_NAME}/previous /var/www/${HTMLY_DOMAIN_NAME}/htdocs
  310. fi
  311. exit 593
  312. fi
  313. rm -rf ${temp_restore_dir}
  314. if [ ! -d "/var/www/${HTMLY_DOMAIN_NAME}/htdocs/content" ]; then
  315. echo $"No content directory found after restoring htmly"
  316. exit 287
  317. fi
  318. # Ensure that the bundled SSL cert is being used
  319. if [ -f "/etc/ssl/certs/${HTMLY_DOMAIN_NAME}.bundle.crt" ]; then
  320. sed -i "s|${HTMLY_DOMAIN_NAME}.crt|${HTMLY_DOMAIN_NAME}.bundle.crt|g" "/etc/nginx/sites-available/${HTMLY_DOMAIN_NAME}"
  321. fi
  322. for d in /home/*/ ; do
  323. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  324. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  325. if [ -d "/var/www/${HTMLY_DOMAIN_NAME}/htdocs/content/$USERNAME/htmly/uncategorized/post" ]; then
  326. # shellcheck disable=SC2086
  327. mv /var/www/${HTMLY_DOMAIN_NAME}/htdocs/content/$USERNAME/htmly/*.md /var/www/${HTMLY_DOMAIN_NAME}/htdocs/content/$USERNAME/htmly/uncategorized/post
  328. fi
  329. fi
  330. done
  331. if [ -d "/etc/letsencrypt/live/${HTMLY_DOMAIN_NAME}" ]; then
  332. ln -s "/etc/letsencrypt/live/${HTMLY_DOMAIN_NAME}/privkey.pem" "/etc/ssl/private/${HTMLY_DOMAIN_NAME}.key"
  333. ln -s "/etc/letsencrypt/live/${HTMLY_DOMAIN_NAME}/fullchain.pem" "/etc/ssl/certs/${HTMLY_DOMAIN_NAME}.pem"
  334. fi
  335. echo $"Restore of htmly complete"
  336. fi
  337. }
  338. function remove_htmly {
  339. if [ ${#HTMLY_DOMAIN_NAME} -eq 0 ]; then
  340. return
  341. fi
  342. read_config_param "HTMLY_DOMAIN_NAME"
  343. nginx_dissite "$HTMLY_DOMAIN_NAME"
  344. remove_certs "${HTMLY_DOMAIN_NAME}"
  345. if [ -f "/etc/nginx/sites-available/$HTMLY_DOMAIN_NAME" ]; then
  346. rm -f "/etc/nginx/sites-available/$HTMLY_DOMAIN_NAME"
  347. fi
  348. if [ -d "/var/www/$HTMLY_DOMAIN_NAME" ]; then
  349. rm -rf "/var/www/$HTMLY_DOMAIN_NAME"
  350. fi
  351. remove_config_param HTMLY_DOMAIN_NAME
  352. remove_config_param HTMLY_CODE
  353. function_check remove_onion_service
  354. remove_onion_service htmly ${HTMLY_ONION_PORT}
  355. remove_completion_param "install_htmly"
  356. sed -i '/Htmly/d' "$COMPLETION_FILE"
  357. sed -i '/htmly/d' "$COMPLETION_FILE"
  358. function_check remove_ddns_domain
  359. remove_ddns_domain "$HTMLY_DOMAIN_NAME"
  360. }
  361. function install_htmly_social_networks {
  362. # set social networks
  363. if grep -q "social.hubzilla" "/var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini"; then
  364. sed -i "s|;social.hubzilla|social.hubzilla|g" "/var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini"
  365. sed -i "s|social.hubzilla.*|social.hubzilla = \"$HUBZILLA_DOMAIN_NAME\"|g" "/var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini"
  366. fi
  367. if grep -q "social.gnusocial" "/var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini"; then
  368. sed -i "s|;social.gnusocial|social.gnusocial|g" "/var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini"
  369. sed -i "s|social.gnusocial.*|social.gnusocial = \"$MICROHTMLY_DOMAIN_NAME\"|g" "/var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini"
  370. fi
  371. # clear proprietary social network strings
  372. sed -i 's|social.facebook.*|social.facebook = ""|g' "/var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini"
  373. sed -i 's|social.twitter.*|social.twitter = ""|g' "/var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini"
  374. sed -i 's|social.google.*|social.google = ""|g' "/var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini"
  375. }
  376. function install_htmly_user {
  377. # create a user password
  378. if [ -f "$IMAGE_PASSWORD_FILE" ]; then
  379. HTMLY_ADMIN_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
  380. else
  381. HTMLY_ADMIN_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  382. fi
  383. # create a user
  384. HTMLY_ADMIN_PASSWORD_HASH=$("${PROJECT_NAME}-sec" --htmlyhash "$HTMLY_ADMIN_PASSWORD")
  385. if [ ${#HTMLY_ADMIN_PASSWORD_HASH} -lt 8 ]; then
  386. echo $'Htmly admin password could not be hashed'
  387. exit 625728
  388. fi
  389. { echo ';Password';
  390. echo "password = $HTMLY_ADMIN_PASSWORD_HASH";
  391. echo 'encryption = password_hash';
  392. echo ';Role';
  393. echo 'role = admin'; } > "/var/www/$HTMLY_DOMAIN_NAME/htdocs/config/users/$MY_USERNAME.ini"
  394. }
  395. function install_htmly_settings {
  396. cp "/var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini.example" "/var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini"
  397. sed -i "s|site.url.*|site.url = '/'|g" "/var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini"
  398. sed -i "s|blog.title.*|blog.title = \"$HTMLY_TITLE\"|g" "/var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini"
  399. sed -i "s|blog.tagline.*|blog.tagline = \"$HTMLY_SUBTITLE\"|g" "/var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini"
  400. sed -i 's|timezone.*|timezone = "Europe/London"|g' "/var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini"
  401. sed -i "s|Your name|$MY_NAME|g" "/var/www/$HTMLY_DOMAIN_NAME/htdocs/config/config.ini"
  402. }
  403. function install_htmly_website {
  404. function_check nginx_http_redirect
  405. nginx_http_redirect "$HTMLY_DOMAIN_NAME"
  406. { echo 'server {';
  407. echo ' listen 443 ssl;';
  408. echo ' #listen [::]:443 ssl;';
  409. echo " root /var/www/$HTMLY_DOMAIN_NAME/htdocs;";
  410. echo " server_name $HTMLY_DOMAIN_NAME;";
  411. echo ' access_log /dev/null;';
  412. echo " error_log /dev/null;";
  413. echo ' index index.php;';
  414. echo ' charset utf-8;';
  415. echo ' proxy_read_timeout 86400s;'; } >> "/etc/nginx/sites-available/$HTMLY_DOMAIN_NAME"
  416. function_check nginx_ssl
  417. nginx_ssl "$HTMLY_DOMAIN_NAME"
  418. function_check nginx_security_options
  419. nginx_security_options "$HTMLY_DOMAIN_NAME"
  420. { echo ' add_header Strict-Transport-Security "max-age=0;";';
  421. echo '';
  422. echo ' # rewrite to front controller as default rule';
  423. echo ' location / {'; } >> "/etc/nginx/sites-available/$HTMLY_DOMAIN_NAME"
  424. function_check nginx_limits
  425. nginx_limits "$HTMLY_DOMAIN_NAME"
  426. { echo " rewrite ^/(.*) /index.php?q=\$uri&\$args last;";
  427. echo ' }';
  428. echo '';
  429. echo ' # statically serve these file types when possible';
  430. echo ' # otherwise fall back to front controller';
  431. echo ' # allow browser to cache them';
  432. echo ' # added .htm for advanced source code editor library';
  433. echo ' location ~* \.(jpg|jpeg|gif|png|ico|css|js|htm|html|ttf|woff|svg)$ {';
  434. echo ' expires 30d;';
  435. echo " try_files \$uri /index.php?q=\$uri&\$args;";
  436. echo ' }';
  437. echo '';
  438. echo ' # block these file types';
  439. echo ' location ~* \.(tpl|md|tgz|log|out)$ {';
  440. echo ' deny all;';
  441. echo ' }';
  442. echo '';
  443. echo ' # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000';
  444. echo ' # or a unix socket';
  445. echo ' location ~* \.php$ {';
  446. echo ' # Zero-day exploit defense.';
  447. echo ' # http://forum.nginx.org/read.php?2,88845,page=3';
  448. echo " # Won't work properly (404 error) if the file is not stored on this";
  449. echo " # server, which is entirely possible with php-fpm/php-fcgi.";
  450. echo " # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on";
  451. echo " # another machine. And then cross your fingers that you won't get hacked.";
  452. echo " try_files \$uri \$uri/ /index.php;";
  453. echo ' # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini';
  454. echo ' fastcgi_split_path_info ^(.+\.php)(/.+)$;';
  455. echo ' # With php-cgi alone:';
  456. echo ' # fastcgi_pass 127.0.0.1:9000;';
  457. echo ' # With php-fpm:';
  458. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  459. echo ' include fastcgi_params;';
  460. echo ' fastcgi_read_timeout 30;';
  461. echo ' fastcgi_index index.php;';
  462. echo " fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;";
  463. echo ' }';
  464. echo '';
  465. echo ' # deny access to all dot files';
  466. echo ' location ~ /\. {';
  467. echo ' deny all;';
  468. echo ' }';
  469. echo '';
  470. echo ' #deny access to store';
  471. echo ' location ~ /store {';
  472. echo ' deny all;';
  473. echo ' }';
  474. echo ' location ~ /(data|conf|bin|inc)/ {';
  475. echo ' deny all;';
  476. echo ' }';
  477. echo ' location ~ /\.ht {';
  478. echo ' deny all;';
  479. echo ' }';
  480. echo '}';
  481. echo ''; } >> "/etc/nginx/sites-available/$HTMLY_DOMAIN_NAME"
  482. }
  483. function install_htmly_website_onion {
  484. { echo 'server {';
  485. echo " listen 127.0.0.1:${HTMLY_ONION_PORT} default_server;";
  486. echo " root /var/www/$HTMLY_DOMAIN_NAME/htdocs;"; } >> "/etc/nginx/sites-available/$HTMLY_DOMAIN_NAME"
  487. if [[ "$ONION_ONLY" != 'yes' ]]; then
  488. echo " server_name $HTMLY_DOMAIN_NAME;" >> "/etc/nginx/sites-available/$HTMLY_DOMAIN_NAME"
  489. else
  490. echo " server_name $HTMLY_ONION_HOSTNAME;" >> "/etc/nginx/sites-available/$HTMLY_DOMAIN_NAME"
  491. fi
  492. { echo ' access_log /dev/null;';
  493. echo " error_log /dev/null;";
  494. echo ' index index.php;';
  495. echo ' charset utf-8;';
  496. echo ' proxy_read_timeout 86400s;'; } >> "/etc/nginx/sites-available/$HTMLY_DOMAIN_NAME"
  497. function_check nginx_security_options
  498. nginx_security_options "$HTMLY_DOMAIN_NAME"
  499. { echo ' add_header Strict-Transport-Security "max-age=0;";';
  500. echo '';
  501. echo ' # rewrite to front controller as default rule';
  502. echo ' location / {'; } >> "/etc/nginx/sites-available/$HTMLY_DOMAIN_NAME"
  503. function_check nginx_limits
  504. nginx_limits "$HTMLY_DOMAIN_NAME"
  505. { echo " rewrite ^/(.*) /index.php?q=\$uri&\$args last;";
  506. echo ' }';
  507. echo '';
  508. echo ' # statically serve these file types when possible';
  509. echo ' # otherwise fall back to front controller';
  510. echo ' # allow browser to cache them';
  511. echo ' # added .htm for advanced source code editor library';
  512. echo ' location ~* \.(jpg|jpeg|gif|png|ico|css|js|htm|html|ttf|woff|svg)$ {';
  513. echo ' expires 30d;';
  514. echo " try_files \$uri /index.php?q=\$uri&\$args;";
  515. echo ' }';
  516. echo '';
  517. echo ' # block these file types';
  518. echo ' location ~* \.(tpl|md|tgz|log|out)$ {';
  519. echo ' deny all;';
  520. echo ' }';
  521. echo '';
  522. echo ' # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000';
  523. echo ' # or a unix socket';
  524. echo ' location ~* \.php$ {'; } >> "/etc/nginx/sites-available/$HTMLY_DOMAIN_NAME"
  525. function_check nginx_limits
  526. nginx_limits "$HTMLY_DOMAIN_NAME"
  527. { echo ' # Zero-day exploit defense.';
  528. echo ' # http://forum.nginx.org/read.php?2,88845,page=3';
  529. echo " # Won't work properly (404 error) if the file is not stored on this";
  530. echo " # server, which is entirely possible with php-fpm/php-fcgi.";
  531. echo " # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on";
  532. echo " # another machine. And then cross your fingers that you won't get hacked.";
  533. echo " try_files \$uri \$uri/ /index.php;";
  534. echo ' # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini';
  535. echo ' fastcgi_split_path_info ^(.+\.php)(/.+)$;';
  536. echo ' # With php-cgi alone:';
  537. echo ' # fastcgi_pass 127.0.0.1:9000;';
  538. echo ' # With php-fpm:';
  539. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  540. echo ' include fastcgi_params;';
  541. echo ' fastcgi_read_timeout 30;';
  542. echo ' fastcgi_index index.php;';
  543. echo " fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;";
  544. echo ' }';
  545. echo '';
  546. echo ' # deny access to all dot files';
  547. echo ' location ~ /\. {';
  548. echo ' deny all;';
  549. echo ' }';
  550. echo '';
  551. echo ' #deny access to store';
  552. echo ' location ~ /store {';
  553. echo ' deny all;';
  554. echo ' }';
  555. echo ' location ~ /(data|conf|bin|inc)/ {';
  556. echo ' deny all;';
  557. echo ' }';
  558. echo ' location ~ /\.ht {';
  559. echo ' deny all;';
  560. echo ' }';
  561. echo '}'; } >> "/etc/nginx/sites-available/$HTMLY_DOMAIN_NAME"
  562. }
  563. function install_htmly_from_repo {
  564. if [ ! -d "/var/www/$HTMLY_DOMAIN_NAME" ]; then
  565. mkdir "/var/www/$HTMLY_DOMAIN_NAME"
  566. fi
  567. cd "/var/www/$HTMLY_DOMAIN_NAME" || exit 34684682
  568. if [ -d /repos/htmly ]; then
  569. mkdir htdocs
  570. cp -r -p /repos/htmly/. htdocs
  571. cd htdocs || exit 3468735
  572. git pull
  573. else
  574. git_clone $HTMLY_REPO htdocs
  575. fi
  576. cd htdocs || exit 3479835
  577. git checkout $HTMLY_COMMIT -b $HTMLY_COMMIT
  578. set_completion_param "htmly commit" "$HTMLY_COMMIT"
  579. }
  580. function install_htmly {
  581. if [ ! "$ONION_ONLY" ]; then
  582. ONION_ONLY='no'
  583. fi
  584. if [ ! "$HTMLY_DOMAIN_NAME" ]; then
  585. echo $'The htmly domain name was not specified'
  586. exit 5062
  587. fi
  588. # for the avatar changing command
  589. apt-get -yq install imagemagick libfcgi0ldbl
  590. function_check install_htmly_from_repo
  591. install_htmly_from_repo
  592. if [[ $ONION_ONLY == "no" ]]; then
  593. function_check install_htmly_website
  594. install_htmly_website
  595. else
  596. echo -n '' > "/etc/nginx/sites-available/$HTMLY_DOMAIN_NAME"
  597. fi
  598. HTMLY_ONION_HOSTNAME=$(add_onion_service htmly 80 ${HTMLY_ONION_PORT})
  599. function_check install_htmly_website_onion
  600. install_htmly_website_onion
  601. function_check create_site_certificate
  602. create_site_certificate "$HTMLY_DOMAIN_NAME" 'yes'
  603. function_check configure_php
  604. configure_php
  605. function_check install_htmly_settings
  606. install_htmly_settings
  607. function_check install_htmly_social_networks
  608. install_htmly_social_networks
  609. function_check install_htmly_user
  610. install_htmly_user
  611. chown -R www-data:www-data "/var/www/$HTMLY_DOMAIN_NAME/htdocs"
  612. function_check nginx_ensite
  613. nginx_ensite "$HTMLY_DOMAIN_NAME"
  614. systemctl restart php7.0-fpm
  615. systemctl restart nginx
  616. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a htmly -p "$HTMLY_ADMIN_PASSWORD"
  617. function_check add_ddns_domain
  618. add_ddns_domain "$HTMLY_DOMAIN_NAME"
  619. set_completion_param "htmly domain" "$HTMLY_DOMAIN_NAME"
  620. APP_INSTALLED=1
  621. }
  622. # NOTE: deliberately no exit 0