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 6.1KB

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