The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

PasteController.cs 7.5KB

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