You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.9 KiB

4 years ago
  1. #!/bin/bash
  2. # Variables
  3. blog_dir=~/repos/blog-cms
  4. check() {
  5. if [ "$EUID" -ne 0 ]
  6. then
  7. echo "Please run as root"
  8. exit
  9. fi
  10. }
  11. confirm() {
  12. confirmvar=""
  13. while [[ "$confirmvar" == "" ]]
  14. do
  15. read -p "y/n/q: " confirmvar
  16. if [ "${confirmvar}" == "y" ]
  17. then
  18. echo true
  19. elif [ "${confirmvar}" == "n" ]
  20. then
  21. echo false
  22. fi
  23. done
  24. }
  25. newpost() {
  26. c=false
  27. while [[ ${c} == false ]]
  28. do
  29. read -p "Title: " title
  30. echo -e "Subject 1:\n\t0 - no subject\n\t1 - Programming\n\t2 - Pentesting\n\t3 - Personal"
  31. sub1=$(subjectselect)
  32. echo -e "Subject 1:\n\t0 - no subject\n\t1 - Programming\n\t2 - Pentesting\n\t3 - Personal"
  33. sub2=$(subjectselect)
  34. echo -e "Title: ${title}\nSubject 1: ${sub1}\nSubject 2: ${sub2}\n\nAre you sure?\n"
  35. c=$(confirm)
  36. done
  37. dirname=$(echo ${title} | sed 's/\ /_/g')
  38. mkdir -p ${blog_dir}/posts/${dirname}/static
  39. cd ${blog_dir}/posts/${dirname}
  40. echo ${title} >> title
  41. echo ${sub1} >> .sub1
  42. echo ${sub2} >> .sub2
  43. touch {intro.html,body.html}
  44. pwd
  45. ls -al
  46. }
  47. subjectselect() {
  48. read -p "0/1/2/3: " tmpvar
  49. if [ "$tmpvar" == "1" ]
  50. then
  51. echo "Programming"
  52. elif [ "$tmpvar" == "2" ]
  53. then
  54. echo "Pentesting"
  55. elif [ "$tmpvar" == "3" ]
  56. then
  57. echo "Personal"
  58. elif [ "$tmpvar" == "0" ]
  59. then
  60. echo ""
  61. fi
  62. }
  63. listandReturn() { printf "Listing contents of %s.\\n" "$1"
  64. ls -rc "$1" | awk -F '/' '{print $NF}' | nl
  65. read -erp "Pick an entry by number to $2, or press ctrl-c to cancel. " number
  66. chosen="$(ls -rc "$1" | nl | grep -w "$number" | awk '{print $2}')"
  67. basefile="$(basename "$chosen")" && base="${basefile%.*}" ;}
  68. upload() {
  69. #check
  70. listandReturn ${blog_dir}/posts
  71. #cd ${blog_dir}/posts/${basefile}
  72. titlevar=$(cat ${blog_dir}/posts/${basefile}/title)
  73. subvar1=$(cat ${blog_dir}/posts/${basefile}/.sub1)
  74. subvar2=$(cat ${blog_dir}/posts/${basefile}/.sub2)
  75. echo "Add intro pic?"
  76. if [[ $(confirm) == true ]]
  77. then
  78. listandReturn ${blog_dir}/posts/${basefile}/static/
  79. picvar=${basefile}
  80. fi
  81. echo -n Mysql password:
  82. read -s sqlpassword
  83. scp ${blog_dir}/posts/${basefile}/{intro.html,body.html} root@tovijaeschke.xyz:/var/lib/mysql-files/
  84. scp ${blog_dir}/posts/${basefile}/static/* root@tovijaeschke.xyz:/usr/share/nginx/personal/static/
  85. if [[ "$picvar" == "" ]]
  86. then
  87. ssh root@tovijaeschke.xyz "chown mysql:mysql /var/lib/mysql-files/* &&
  88. chmod go+rw /var/lib/mysql-files/* &&
  89. mysql -u root -p${sqlpassword} -D PersonalWebsite -e \"INSERT INTO Posts (subject,subject2,title,intro,body) VALUES ('${subvar1}', '${subvar2}', '${titlevar}', LOAD_FILE('/var/lib/mysql-files/intro.html'), LOAD_FILE('/var/lib/mysql-files/body.html'));\""
  90. else
  91. ssh root@tovijaeschke.xyz "chown mysql:mysql /var/lib/mysql-files/* &&
  92. chmod go+rw /var/lib/mysql-files/* &&
  93. mysql -u root -p${sqlpassword} -D PersonalWebsite -e \"INSERT INTO Posts (subject,subject2,title,pic,intro,body) VALUES ('${subvar1}', '${subvar2}', '${titlevar}', '${picvar}', LOAD_FILE('/var/lib/mysql-files/intro.html'), LOAD_FILE('/var/lib/mysql-files/body.html'));\""
  94. fi
  95. }
  96. preview() {
  97. mkdir -p /tmp/preview
  98. cp -r ${blog_dir}/.preview/* /tmp/preview/
  99. listandReturn ${blog_dir}/posts
  100. title=$(cat ${blog_dir}/posts/${basefile}/title | tr -d '\n')
  101. intro=$(cat ${blog_dir}/posts/${basefile}/intro.html | tr -d '\n')
  102. body=$(cat ${blog_dir}/posts/${basefile}/body.html | tr -d '\n')
  103. cp -r ${blog_dir}/posts/${basefile}/static/ /tmp/preview
  104. sed -i .bak 's|TITLE_PREVIEW|'"${title}"'|g' /tmp/preview/post.html
  105. sed -i .bak 's|INTRO_PREVIEW|'"${intro}"'|g' /tmp/preview/post.html
  106. sed -i .bak 's|BODY_PREVIEW|'"${body}"'|g' /tmp/preview/post.html
  107. if [[ $(uname) == "Linux" ]]; then
  108. firefox /tmp/preview/post.html
  109. else
  110. open -a firefox -g /tmp/preview/post.html
  111. fi
  112. }
  113. deletedraft() {
  114. echo "Which draft would you like to delete?"
  115. listandReturn ${blog_dir}/posts
  116. echo "Are you sure you want to delete \"$(cat ${blog_dir}/posts/${basefile}/title)?\""
  117. c=$(confirm)
  118. if [ ${c} != false ]
  119. then
  120. rm -rf ${blog_dir}/posts/${basefile}
  121. fi
  122. }
  123. deletepost() {
  124. echo -n Mysql password:
  125. read -s sqlpassword
  126. echo -e "\nWhich post would you like to delete?"
  127. ssh root@tovijaeschke.xyz "mysql -u root -p${sqlpassword} -D PersonalWebsite -B --disable-column-names -e \"SELECT id,title FROM Posts;\""
  128. read -p "\nWhich post would you like to delete?" post
  129. ssh root@tovijaeschke.xyz "mysql -u root -p${sqlpassword} -D PersonalWebsite -e \"DELETE FROM Posts WHERE id=${post};\" && \
  130. post=\$(mysql -B -u root -p${sqlpassword} -D PersonalWebsite --disable-column-names -e \"SELECT MAX(id) FROM Posts;\") && \
  131. mysql -u root -p${sqlpassword} -D PersonalWebsite -e \"ALTER TABLE Posts AUTO_INCREMENT=\$post;\""
  132. }
  133. helpmsg() {
  134. printf "\nBLOG UPLOAD SCRIPT\n\n\tn - new draft\n\tp - preview post\n\tu - upload post\n\tdd - delete draft\n\tdp - delete post\n\th - help message\n\n"
  135. }
  136. case "$1" in
  137. n*) newpost ;;
  138. p*) preview ;;
  139. u*) upload ;;
  140. dd*) deletedraft ;;
  141. dp*) deletepost ;;
  142. h*) helpmsg ;;
  143. *) helpmsg ;;
  144. esac