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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.Pygments;
  10. using Teknik.Areas.Error.Controllers;
  11. using Teknik.Areas.Paste.ViewModels;
  12. using Teknik.Areas.Users.Utility;
  13. using Teknik.Controllers;
  14. using Teknik.Filters;
  15. using Teknik.Utilities;
  16. using Teknik.Models;
  17. using Teknik.Attributes;
  18. namespace Teknik.Areas.Paste.Controllers
  19. {
  20. [TeknikAuthorize]
  21. public class PasteController : DefaultController
  22. {
  23. private TeknikEntities db = new TeknikEntities();
  24. [TrackPageView]
  25. [AllowAnonymous]
  26. public ActionResult Index()
  27. {
  28. ViewBag.Title = "Paste - " + Config.Title;
  29. 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.";
  30. PasteCreateViewModel model = new PasteCreateViewModel();
  31. return View(model);
  32. }
  33. [TrackDownload]
  34. [AllowAnonymous]
  35. public ActionResult ViewPaste(string type, string url, string password)
  36. {
  37. Models.Paste paste = db.Pastes.Where(p => p.Url == url).FirstOrDefault();
  38. if (paste != null)
  39. {
  40. ViewBag.Title = ((string.IsNullOrEmpty(paste.Title)) ? string.Empty : paste.Title + " - ") + Config.Title + " Paste";
  41. 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.";
  42. // Increment Views
  43. paste.Views += 1;
  44. db.Entry(paste).State = EntityState.Modified;
  45. db.SaveChanges();
  46. // Check Expiration
  47. if (PasteHelper.CheckExpiration(paste))
  48. {
  49. db.Pastes.Remove(paste);
  50. db.SaveChanges();
  51. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  52. }
  53. PasteViewModel model = new PasteViewModel();
  54. model.Url = url;
  55. model.Content = paste.Content;
  56. model.Title = paste.Title;
  57. model.Syntax = paste.Syntax;
  58. model.DatePosted = paste.DatePosted;
  59. byte[] data = Encoding.UTF8.GetBytes(paste.Content);
  60. // The paste has a password set
  61. if (!string.IsNullOrEmpty(paste.HashedPassword))
  62. {
  63. string hash = string.Empty;
  64. if (!string.IsNullOrEmpty(password))
  65. {
  66. byte[] passBytes = Utilities.SHA384.Hash(paste.Key, password);
  67. hash = passBytes.ToHex();
  68. // We need to convert old pastes to the new password scheme
  69. if (paste.Transfers.ToList().Exists(t => t.Type == TransferTypes.ASCIIPassword))
  70. {
  71. hash = Encoding.ASCII.GetString(passBytes);
  72. // Remove the transfer types
  73. paste.Transfers.Clear();
  74. db.Entry(paste).State = EntityState.Modified;
  75. db.SaveChanges();
  76. }
  77. }
  78. if (string.IsNullOrEmpty(password) || hash != paste.HashedPassword)
  79. {
  80. PasswordViewModel passModel = new PasswordViewModel();
  81. passModel.Url = url;
  82. passModel.Type = type;
  83. // Redirect them to the password request page
  84. return View("~/Areas/Paste/Views/Paste/PasswordNeeded.cshtml", passModel);
  85. }
  86. data = Convert.FromBase64String(paste.Content);
  87. // Now we decrypt the content
  88. byte[] ivBytes = Encoding.Unicode.GetBytes(paste.IV);
  89. byte[] keyBytes = AES.CreateKey(password, ivBytes, paste.KeySize);
  90. data = AES.Decrypt(data, keyBytes, ivBytes);
  91. model.Content = Encoding.Unicode.GetString(data);
  92. }
  93. if (type.ToLower() == "full" || type.ToLower() == "simple")
  94. {
  95. // Transform content into HTML
  96. if (!Highlighter.Lexers.ToList().Exists(l => l.Aliases.Contains(model.Syntax)))
  97. {
  98. model.Syntax = "text";
  99. }
  100. Highlighter highlighter = new Highlighter();
  101. // Add a space in front of the content due to bug with pygment (No idea why yet)
  102. model.Content = highlighter.HighlightToHtml(" " + model.Content, model.Syntax, Config.PasteConfig.SyntaxVisualStyle, generateInlineStyles: true, fragment: true);
  103. }
  104. switch (type.ToLower())
  105. {
  106. case "full":
  107. return View("~/Areas/Paste/Views/Paste/Full.cshtml", model);
  108. case "simple":
  109. return View("~/Areas/Paste/Views/Paste/Simple.cshtml", model);
  110. case "raw":
  111. return Content(model.Content, "text/plain");
  112. case "download":
  113. //Create File
  114. var cd = new System.Net.Mime.ContentDisposition
  115. {
  116. FileName = url,
  117. Inline = true
  118. };
  119. Response.AppendHeader("Content-Disposition", cd.ToString());
  120. return File(data, "application/octet-stream");
  121. default:
  122. return View("~/Areas/Paste/Views/Paste/Full.cshtml", model);
  123. }
  124. }
  125. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  126. }
  127. [HttpPost]
  128. [AllowAnonymous]
  129. public ActionResult Paste([Bind(Include = "Content, Title, Syntax, ExpireLength, ExpireUnit, Password, Hide")]PasteCreateViewModel model)
  130. {
  131. if (ModelState.IsValid)
  132. {
  133. if (Config.PasteConfig.Enabled)
  134. {
  135. try
  136. {
  137. Models.Paste paste = PasteHelper.CreatePaste(model.Content, model.Title, model.Syntax, model.ExpireUnit, model.ExpireLength ?? 1, model.Password, model.Hide);
  138. if (model.ExpireUnit == "view")
  139. {
  140. paste.Views = -1;
  141. }
  142. if (User.Identity.IsAuthenticated)
  143. {
  144. Users.Models.User user = UserHelper.GetUser(db, User.Identity.Name);
  145. if (user != null)
  146. {
  147. paste.UserId = user.UserId;
  148. }
  149. }
  150. db.Pastes.Add(paste);
  151. db.SaveChanges();
  152. return Redirect(Url.SubRouteUrl("p", "Paste.View", new { type = "Full", url = paste.Url }));
  153. }
  154. catch (Exception ex)
  155. {
  156. return Redirect(Url.SubRouteUrl("error", "Error.500", new { exception = ex }));
  157. }
  158. }
  159. Redirect(Url.SubRouteUrl("error", "Error.Http403"));
  160. }
  161. return View("~/Areas/Paste/Views/Paste/Index.cshtml", model);
  162. }
  163. }
  164. }