The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
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.

PasteController.cs 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using Teknik.Areas.Error.Controllers;
  10. using Teknik.Areas.Paste.ViewModels;
  11. using Teknik.Controllers;
  12. using Teknik.Helpers;
  13. using Teknik.Models;
  14. namespace Teknik.Areas.Paste.Controllers
  15. {
  16. public class PasteController : DefaultController
  17. {
  18. private TeknikEntities db = new TeknikEntities();
  19. [AllowAnonymous]
  20. public ActionResult Index()
  21. {
  22. ViewBag.Title = Config.Title + " Paste";
  23. PasteCreateViewModel model = new PasteCreateViewModel();
  24. return View(model);
  25. }
  26. [AllowAnonymous]
  27. public ActionResult ViewPaste(string type, string url, string password)
  28. {
  29. Models.Paste paste = db.Pastes.Where(p => p.Url == url).FirstOrDefault();
  30. if (paste != null)
  31. {
  32. ViewBag.Title = ((string.IsNullOrEmpty(paste.Title)) ? string.Empty : paste.Title + " - ") + Config.Title + " Paste";
  33. // Increment Views
  34. paste.Views += 1;
  35. db.Entry(paste).State = EntityState.Modified;
  36. db.SaveChanges();
  37. // Check Expiration
  38. if (PasteHelper.CheckExpiration(paste))
  39. {
  40. db.Pastes.Remove(paste);
  41. db.SaveChanges();
  42. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  43. }
  44. PasteViewModel model = new PasteViewModel();
  45. model.Url = url;
  46. model.Content = paste.Content;
  47. model.Title = paste.Title;
  48. model.Syntax = paste.Syntax;
  49. model.DatePosted = paste.DatePosted;
  50. byte[] data = Encoding.Unicode.GetBytes(paste.Content);
  51. // The paste has a password set
  52. if (!string.IsNullOrEmpty(paste.HashedPassword))
  53. {
  54. if (string.IsNullOrEmpty(password) || Helpers.SHA384.Hash(paste.Key, password) != paste.HashedPassword)
  55. {
  56. PasswordViewModel passModel = new PasswordViewModel();
  57. passModel.Url = url;
  58. passModel.Type = type;
  59. // Redirect them to the password request page
  60. return View("~/Areas/Paste/Views/Paste/PasswordNeeded.cshtml", passModel);
  61. }
  62. // Now we decrypt the content
  63. byte[] ivBytes = Encoding.Unicode.GetBytes(paste.IV);
  64. byte[] keyBytes = AES.CreateKey(password, ivBytes, paste.KeySize);
  65. data = AES.Decrypt(data, keyBytes, ivBytes);
  66. model.Content = Encoding.Unicode.GetString(data);
  67. }
  68. switch (type.ToLower())
  69. {
  70. case "full":
  71. return View("~/Areas/Paste/Views/Paste/Full.cshtml", model);
  72. case "simple":
  73. return View("~/Areas/Paste/Views/Paste/Simple.cshtml", model);
  74. case "raw":
  75. return Content(model.Content, "text/plain");
  76. case "download":
  77. //Create File
  78. var cd = new System.Net.Mime.ContentDisposition
  79. {
  80. FileName = url,
  81. Inline = true
  82. };
  83. Response.AppendHeader("Content-Disposition", cd.ToString());
  84. return File(data, "application/octet-stream");
  85. default:
  86. return View("~/Areas/Paste/Views/Paste/Full.cshtml", model);
  87. }
  88. }
  89. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  90. }
  91. [HttpPost]
  92. [AllowAnonymous]
  93. [ValidateAntiForgeryToken]
  94. public ActionResult Paste([Bind(Include = "Content, Title, Syntax, ExpireLength, ExpireUnit, Password, Hide")]PasteCreateViewModel model)
  95. {
  96. if (ModelState.IsValid)
  97. {
  98. if (Config.PasteConfig.Enabled)
  99. {
  100. try
  101. {
  102. Models.Paste paste = PasteHelper.CreatePaste(model.Content, model.Title, model.Syntax, model.ExpireUnit, model.ExpireLength ?? 1, model.Password, model.Hide);
  103. if (model.ExpireUnit == "view")
  104. {
  105. paste.Views = -1;
  106. }
  107. if (User.Identity.IsAuthenticated)
  108. {
  109. Profile.Models.User user = db.Users.Where(u => u.Username == User.Identity.Name).FirstOrDefault();
  110. if (user != null)
  111. {
  112. paste.UserId = user.UserId;
  113. }
  114. }
  115. db.Pastes.Add(paste);
  116. db.SaveChanges();
  117. return Redirect(Url.SubRouteUrl("paste", "Paste.View", new { type = "Full", url = paste.Url, password = model.Password }));
  118. }
  119. catch (Exception ex)
  120. {
  121. return Redirect(Url.SubRouteUrl("error", "Error.500", new { exception = ex }));
  122. }
  123. }
  124. Redirect(Url.SubRouteUrl("error", "Error.Http403"));
  125. }
  126. return View("~/Areas/Paste/Views/Paste/Index.cshtml", model);
  127. }
  128. }
  129. }