Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
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.

254 lines
7.3 KiB

  1. const mysql = require('mysql');
  2. const sanitizer = require('sanitizer');
  3. const Promise = require('promise');
  4. const crypto = require('crypto');
  5. const qs = require('querystring');
  6. const con = mysql.createConnection({
  7. host: "localhost",
  8. user: "blog_user",
  9. password: "password",
  10. database: "blog_name"
  11. });
  12. con.connect(function(err) {
  13. if (err) throw err;
  14. });
  15. /**
  16. * Function used to query the database for records
  17. *
  18. * @param sqlStatement
  19. * @returns {Array}
  20. */
  21. var fetch = function(sqlStatement)
  22. {
  23. return new Promise(function(resolve, reject)
  24. {
  25. con.query(sqlStatement, function (err, result)
  26. {
  27. if (err)
  28. {
  29. reject();
  30. }
  31. resolve(result);
  32. });
  33. });
  34. };
  35. module.exports=
  36. {
  37. /**
  38. * Function used to use insert statements into the database
  39. *
  40. * Don't worry, the input gets sanitized
  41. *
  42. * @param sqlStatement
  43. * @return the id of the new record - if there is one
  44. */
  45. insert : function(sqlStatement)
  46. {
  47. return new Promise(function(resolve, reject)
  48. {
  49. con.query(sanitizer.sanitize(sqlStatement), function (err, result)
  50. {
  51. if (err)
  52. {
  53. console.log(err);
  54. resolve(0);
  55. }
  56. resolve(result.insertId);
  57. });
  58. })
  59. },
  60. /**
  61. * Not to be mistaken for getPostData() in @file utils/utils.js,
  62. * this function extracts a post entry from the sql server
  63. *
  64. * @param requestURL url user used to request blog post
  65. * @return {*} the entry found in the data base -- if any
  66. */
  67. getPost : function(requestURL)
  68. {
  69. return new Promise(function(resolve, reject)
  70. {
  71. var splitURL = requestURL.split("/")
  72. var q = "select * from categories where url='" + splitURL[1] + "'";
  73. fetch(q).then(function (result_category)
  74. {
  75. if(result_category.length != 0)
  76. {
  77. var q2 = "select * from posts where category_id='" + result_category[0].category_id +
  78. "' and url='" + splitURL[2] + "'";
  79. fetch(q2).then(function (result_posts)
  80. {
  81. if(result_posts != 0)
  82. {
  83. resolve(result_posts[0]);
  84. }
  85. else
  86. {
  87. resolve(0);
  88. }
  89. });
  90. }
  91. else
  92. {
  93. resolve(0);
  94. }
  95. });
  96. });
  97. },
  98. /**
  99. * Function used to retrieve all categories when making the sidebar
  100. *
  101. * @return {Promise<Response> | * | Array}
  102. */
  103. getCategories : function()
  104. {
  105. var q = "select * from categories";
  106. return fetch(q);
  107. },
  108. /**
  109. * Function which currently returns all posts of a particular category from the database
  110. * @param requestURL
  111. * @return {*|Promise}
  112. */
  113. getPostsFromCategory: function(requestURL)
  114. {
  115. return new Promise(function(resolve, reject)
  116. {
  117. var q = "select * from categories where name ='" + requestURL + "' limit 1";
  118. console.log(q);
  119. fetch(q).then(function(categories)
  120. {
  121. if(categories.length != 0)
  122. {
  123. var qPosts = "select * from posts where category_id='" + categories[0].category_id + "'";
  124. console.log(qPosts);
  125. resolve(fetch(qPosts));
  126. }
  127. else
  128. {
  129. resolve([]);
  130. }
  131. });
  132. });
  133. },
  134. /**
  135. * Helper method which returns a list of objects which contains the url and name of thee ten most recent posts
  136. *
  137. * {[name: , url: ],[name: , url: ],[name: , url: ],...}
  138. *
  139. * @return {*|Promise}
  140. */
  141. getRecentPosts: function()
  142. {
  143. return new Promise(function(resolve, reject)
  144. {
  145. var q = "select name,url, category_id from posts order by post_id desc limit 10";
  146. fetch(q).then(function(sqlPosts)
  147. {
  148. var promises = [];
  149. sqlPosts.forEach(function(post)
  150. {
  151. promises.push(new Promise(function(res, rej)
  152. {
  153. var getCategory = "select url from categories where category_id='" + post.category_id + "'";
  154. fetch(getCategory).then(function(urls)
  155. {
  156. var obj = new Object();
  157. obj.name = post.name;
  158. obj.url = post.url;
  159. obj.category = urls[0].url;
  160. res(obj);
  161. });
  162. }));
  163. });
  164. Promise.all(promises).then(function(goodies)
  165. {
  166. resolve(goodies);
  167. });
  168. });
  169. });
  170. },
  171. getPopularPosts: function()
  172. {
  173. return new Promise(function(resolve, reject)
  174. {
  175. var q = "select * from popular_posts";
  176. fetch(q).then(function(sqlPosts)
  177. {
  178. });
  179. });
  180. },
  181. /**
  182. * Function which checks to see if a user successfully logged in based on
  183. * the post data which they sent
  184. *
  185. * @param postData the post data
  186. * @return {*|Promise} a json object with {pass: , user: }
  187. * the pass is whether or not they logged in successfully and the user is
  188. * the username they successfully logged in with
  189. */
  190. checkLogin: function(postData)
  191. {
  192. var post = qs.parse(postData);
  193. return new Promise(function(resolve, reject)
  194. {
  195. var result = Object();
  196. result.pass = false;
  197. if(post.username && post.password)
  198. {
  199. var cleanName = sanitizer.sanitize(post.username);
  200. var cleanPassword = sanitizer.sanitize(post.password);
  201. var getSalt = "select * from users where user_name='" + cleanName + "'";
  202. fetch(getSalt).then(function(saltResult)
  203. {
  204. if(saltResult.length == 1)
  205. {
  206. var hashedPassword = crypto.createHash('sha256')
  207. .update(cleanPassword + saltResult[0].salt)
  208. .digest('hex');
  209. if(saltResult[0].password === hashedPassword)
  210. {
  211. //yay!
  212. result.pass = true;
  213. result.user = cleanName;
  214. resolve(result);
  215. }
  216. else
  217. {
  218. //wrong password
  219. resolve(result)
  220. }
  221. }
  222. else
  223. {
  224. //incorrect username
  225. resolve(result);
  226. }
  227. })
  228. }
  229. else
  230. {
  231. //no login attempts were made
  232. resolve(result);
  233. }
  234. });
  235. }
  236. };