privatebin.js 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207
  1. /**
  2. * PrivateBin
  3. *
  4. * a zero-knowledge paste bin
  5. *
  6. * @see {@link https://github.com/PrivateBin/PrivateBin}
  7. * @copyright 2012 Sébastien SAUVAGE ({@link http://sebsauvage.net})
  8. * @license {@link https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License}
  9. * @version 1.1
  10. * @name PrivateBin
  11. * @namespace
  12. */
  13. /** global: Base64 */
  14. /** global: FileReader */
  15. /** global: RawDeflate */
  16. /** global: history */
  17. /** global: navigator */
  18. /** global: prettyPrint */
  19. /** global: prettyPrintOne */
  20. /** global: showdown */
  21. /** global: sjcl */
  22. // Immediately start random number generator collector.
  23. sjcl.random.startCollectors();
  24. // main application start, called when DOM is fully loaded
  25. jQuery(document).ready(function() {
  26. // run main controller
  27. $.PrivateBin.Controller.init();
  28. });
  29. jQuery.PrivateBin = function($, sjcl, Base64, RawDeflate) {
  30. 'use strict';
  31. /**
  32. * static Helper methods
  33. *
  34. * @param {object} window
  35. * @param {object} document
  36. * @class
  37. */
  38. var Helper = (function (window, document) {
  39. var me = {};
  40. /**
  41. * character to HTML entity lookup table
  42. *
  43. * @see {@link https://github.com/janl/mustache.js/blob/master/mustache.js#L60}
  44. * @private
  45. * @enum {Object}
  46. * @readonly
  47. */
  48. var entityMap = {
  49. '&': '&',
  50. '<': '&lt;',
  51. '>': '&gt;',
  52. '"': '&quot;',
  53. "'": '&#39;',
  54. '/': '&#x2F;',
  55. '`': '&#x60;',
  56. '=': '&#x3D;'
  57. };
  58. /**
  59. * cache for script location
  60. *
  61. * @private
  62. * @enum {string|null}
  63. */
  64. var baseUri = null;
  65. /**
  66. * converts a duration (in seconds) into human friendly approximation
  67. *
  68. * @name Helper.secondsToHuman
  69. * @function
  70. * @param {number} seconds
  71. * @return {Array}
  72. */
  73. me.secondsToHuman = function(seconds)
  74. {
  75. var v;
  76. if (seconds < 60)
  77. {
  78. v = Math.floor(seconds);
  79. return [v, 'second'];
  80. }
  81. if (seconds < 60 * 60)
  82. {
  83. v = Math.floor(seconds / 60);
  84. return [v, 'minute'];
  85. }
  86. if (seconds < 60 * 60 * 24)
  87. {
  88. v = Math.floor(seconds / (60 * 60));
  89. return [v, 'hour'];
  90. }
  91. // If less than 2 months, display in days:
  92. if (seconds < 60 * 60 * 24 * 60)
  93. {
  94. v = Math.floor(seconds / (60 * 60 * 24));
  95. return [v, 'day'];
  96. }
  97. v = Math.floor(seconds / (60 * 60 * 24 * 30));
  98. return [v, 'month'];
  99. };
  100. /**
  101. * text range selection
  102. *
  103. * @see {@link https://stackoverflow.com/questions/985272/jquery-selecting-text-in-an-element-akin-to-highlighting-with-your-mouse}
  104. * @name Helper.selectText
  105. * @function
  106. * @param {HTMLElement} element
  107. */
  108. me.selectText = function(element)
  109. {
  110. var range, selection;
  111. // MS
  112. if (document.body.createTextRange) {
  113. range = document.body.createTextRange();
  114. range.moveToElementText(element);
  115. range.select();
  116. } else if (window.getSelection){
  117. selection = window.getSelection();
  118. range = document.createRange();
  119. range.selectNodeContents(element);
  120. selection.removeAllRanges();
  121. selection.addRange(range);
  122. }
  123. };
  124. /**
  125. * set text of a jQuery element (required for IE),
  126. *
  127. * @name Helper.setElementText
  128. * @function
  129. * @param {jQuery} $element - a jQuery element
  130. * @param {string} text - the text to enter
  131. */
  132. me.setElementText = function($element, text)
  133. {
  134. // For IE<10: Doesn't support white-space:pre-wrap; so we have to do this...
  135. if ($('#oldienotice').is(':visible')) {
  136. var html = me.htmlEntities(text).replace(/\n/ig, '\r\n<br>');
  137. $element.html('<pre>' + html + '</pre>');
  138. }
  139. // for other (sane) browsers:
  140. else
  141. {
  142. $element.text(text);
  143. }
  144. };
  145. /**
  146. * convert URLs to clickable links.
  147. * URLs to handle:
  148. * <pre>
  149. * magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7
  150. * http://example.com:8800/zero/?6f09182b8ea51997#WtLEUO5Epj9UHAV9JFs+6pUQZp13TuspAUjnF+iM+dM=
  151. * http://user:example.com@localhost:8800/zero/?6f09182b8ea51997#WtLEUO5Epj9UHAV9JFs+6pUQZp13TuspAUjnF+iM+dM=
  152. * </pre>
  153. *
  154. * @name Helper.urls2links
  155. * @function
  156. * @param {Object} element - a jQuery DOM element
  157. */
  158. me.urls2links = function($element)
  159. {
  160. var markup = '<a href="$1" rel="nofollow">$1</a>';
  161. $element.html(
  162. $element.html().replace(
  163. /((http|https|ftp):\/\/[\w?=&.\/-;#@~%+-]+(?![\w\s?&.\/;#~%"=-]*>))/ig,
  164. markup
  165. )
  166. );
  167. $element.html(
  168. $element.html().replace(
  169. /((magnet):[\w?=&.\/-;#@~%+-]+)/ig,
  170. markup
  171. )
  172. );
  173. };
  174. /**
  175. * minimal sprintf emulation for %s and %d formats
  176. *
  177. * @see {@link https://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format#4795914}
  178. * @name Helper.sprintf
  179. * @function
  180. * @param {string} format
  181. * @param {...*} args - one or multiple parameters injected into format string
  182. * @return {string}
  183. */
  184. me.sprintf = function()
  185. {
  186. var args = Array.prototype.slice.call(arguments);
  187. var format = args[0],
  188. i = 1;
  189. return format.replace(/%((%)|s|d)/g, function (m) {
  190. // m is the matched format, e.g. %s, %d
  191. var val;
  192. if (m[2]) {
  193. val = m[2];
  194. } else {
  195. val = args[i];
  196. // A switch statement so that the formatter can be extended.
  197. switch (m)
  198. {
  199. case '%d':
  200. val = parseFloat(val);
  201. if (isNaN(val)) {
  202. val = 0;
  203. }
  204. break;
  205. default:
  206. // Default is %s
  207. }
  208. ++i;
  209. }
  210. return val;
  211. });
  212. };
  213. /**
  214. * get value of cookie, if it was set, empty string otherwise
  215. *
  216. * @see {@link http://www.w3schools.com/js/js_cookies.asp}
  217. * @name Helper.getCookie
  218. * @function
  219. * @param {string} cname
  220. * @return {string}
  221. */
  222. me.getCookie = function(cname) {
  223. var name = cname + '=',
  224. ca = document.cookie.split(';');
  225. for (var i = 0; i < ca.length; ++i) {
  226. var c = ca[i];
  227. while (c.charAt(0) === ' ')
  228. {
  229. c = c.substring(1);
  230. }
  231. if (c.indexOf(name) === 0)
  232. {
  233. return c.substring(name.length, c.length);
  234. }
  235. }
  236. return '';
  237. };
  238. /**
  239. * get the current location (without search or hash part of the URL),
  240. * eg. http://example.com/path/?aaaa#bbbb --> http://example.com/path/
  241. *
  242. * @name Helper.baseUri
  243. * @function
  244. * @return {string}
  245. */
  246. me.baseUri = function()
  247. {
  248. // check for cached version
  249. if (baseUri !== null) {
  250. return baseUri;
  251. }
  252. // get official base uri string, from base tag in head of HTML
  253. baseUri = document.baseURI;
  254. // if base uri contains query string (when no base tag is present),
  255. // it is unwanted
  256. if (baseUri.indexOf('?')) {
  257. // so we built our own baseuri
  258. baseUri = window.location.origin + window.location.pathname;
  259. }
  260. return baseUri;
  261. };
  262. /**
  263. * convert all applicable characters to HTML entities
  264. *
  265. * @see {@link https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet#RULE_.231_-_HTML_Escape_Before_Inserting_Untrusted_Data_into_HTML_Element_Content}
  266. * @name Helper.htmlEntities
  267. * @function
  268. * @param {string} str
  269. * @return {string} escaped HTML
  270. */
  271. me.htmlEntities = function(str) {
  272. return String(str).replace(
  273. /[&<>"'`=\/]/g, function(s) {
  274. return entityMap[s];
  275. });
  276. };
  277. return me;
  278. })(window, document);
  279. /**
  280. * internationalization module
  281. *
  282. * @param {object} window
  283. * @param {object} document
  284. * @class
  285. */
  286. var I18n = (function (window, document) {
  287. var me = {};
  288. /**
  289. * const for string of loaded language
  290. *
  291. * @private
  292. * @prop {string}
  293. * @readonly
  294. */
  295. var languageLoadedEvent = 'languageLoaded';
  296. /**
  297. * supported languages, minus the built in 'en'
  298. *
  299. * @private
  300. * @prop {string[]}
  301. * @readonly
  302. */
  303. var supportedLanguages = ['de', 'es', 'fr', 'it', 'no', 'pl', 'oc', 'ru', 'sl', 'zh'];
  304. /**
  305. * built in language
  306. *
  307. * @private
  308. * @prop {string|null}
  309. */
  310. var language = null;
  311. /**
  312. * translation cache
  313. *
  314. * @private
  315. * @enum {Object}
  316. */
  317. var translations = {};
  318. /**
  319. * translate a string, alias for I18n.translate()
  320. *
  321. * for a full description see me.translate
  322. *
  323. * @name I18n._
  324. * @function
  325. * @param {jQuery} $element - optional
  326. * @param {string} messageId
  327. * @param {...*} args - one or multiple parameters injected into placeholders
  328. * @return {string}
  329. */
  330. me._ = function()
  331. {
  332. return me.translate.apply(this, arguments);
  333. };
  334. /**
  335. * translate a string
  336. *
  337. * Optionally pass a jQuery element as the first parameter, to automatically
  338. * let the text of this element be replaced. In case the (asynchronously
  339. * loaded) language is not downloadet yet, this will make sure the string
  340. * is replaced when it is actually loaded.
  341. * So for easy translations passing the jQuery object to apply it to is
  342. * more save, especially when they are loaded in the beginning.
  343. *
  344. * @name I18n.translate
  345. * @function
  346. * @param {jQuery} $element - optional
  347. * @param {string} messageId
  348. * @param {...*} args - one or multiple parameters injected into placeholders
  349. * @return {string}
  350. */
  351. me.translate = function()
  352. {
  353. // convert parameters to array
  354. var args = Array.prototype.slice.call(arguments),
  355. messageId,
  356. $element = null;
  357. // parse arguments
  358. if (args[0] instanceof jQuery) {
  359. // optional jQuery element as first parameter
  360. $element = args[0];
  361. args.shift();
  362. }
  363. // extract messageId from arguments
  364. var usesPlurals = $.isArray(args[0]);
  365. if (usesPlurals) {
  366. // use the first plural form as messageId, otherwise the singular
  367. messageId = (args[0].length > 1 ? args[0][1] : args[0][0]);
  368. } else {
  369. messageId = args[0];
  370. }
  371. if (messageId.length === 0) {
  372. return messageId;
  373. }
  374. // if no translation string cannot be found (in translations object)
  375. if (!translations.hasOwnProperty(messageId)) {
  376. // if language is still loading and we have an elemt assigned
  377. if (language === null && $element !== null) {
  378. // handle the error by attaching the language loaded event
  379. var orgArguments = arguments;
  380. $(document).on(languageLoadedEvent, function () {
  381. // re-execute this function
  382. me.translate.apply(this, orgArguments);
  383. // log to show that the previous error could be mitigated
  384. console.log('Fixed missing translation of \'' + messageId + '\' with now loaded language ' + language);
  385. });
  386. // and fall back to English for now until the real language
  387. // file is loaded
  388. }
  389. // for all other langauges than English for which thsi behaviour
  390. // is expected as it is built-in, log error
  391. if (language !== 'en') {
  392. console.error('Missing translation for: \'' + messageId + '\' in language ' + language);
  393. // fallback to English
  394. }
  395. // save English translation (should be the same on both sides)
  396. translations[messageId] = args[0];
  397. }
  398. // lookup plural translation
  399. if (usesPlurals && $.isArray(translations[messageId])) {
  400. var n = parseInt(args[1] || 1, 10),
  401. key = me.getPluralForm(n),
  402. maxKey = translations[messageId].length - 1;
  403. if (key > maxKey) {
  404. key = maxKey;
  405. }
  406. args[0] = translations[messageId][key];
  407. args[1] = n;
  408. } else {
  409. // lookup singular translation
  410. args[0] = translations[messageId];
  411. }
  412. // format string
  413. var output = Helper.sprintf.apply(this, args);
  414. // if $element is given, apply text to element
  415. if ($element !== null) {
  416. $element.text(output);
  417. }
  418. return output;
  419. };
  420. /**
  421. * per language functions to use to determine the plural form
  422. *
  423. * @see {@link http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html}
  424. * @name I18n.getPluralForm
  425. * @function
  426. * @param {number} n
  427. * @return {number} array key
  428. */
  429. me.getPluralForm = function(n) {
  430. switch (language)
  431. {
  432. case 'fr':
  433. case 'oc':
  434. case 'zh':
  435. return (n > 1 ? 1 : 0);
  436. case 'pl':
  437. return (n === 1 ? 0 : (n % 10 >= 2 && n %10 <=4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2));
  438. case 'ru':
  439. return (n % 10 === 1 && n % 100 !== 11 ? 0 : (n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2));
  440. case 'sl':
  441. return (n % 100 === 1 ? 1 : (n % 100 === 2 ? 2 : (n % 100 === 3 || n % 100 === 4 ? 3 : 0)));
  442. // de, en, es, it, no
  443. default:
  444. return (n !== 1 ? 1 : 0);
  445. }
  446. };
  447. /**
  448. * load translations into cache
  449. *
  450. * @name I18n.loadTranslations
  451. * @function
  452. */
  453. me.loadTranslations = function()
  454. {
  455. var newLanguage = Helper.getCookie('lang');
  456. // auto-select language based on browser settings
  457. if (newLanguage.length === 0) {
  458. newLanguage = (navigator.language || navigator.userLanguage).substring(0, 2);
  459. }
  460. // if language is already used skip update
  461. if (newLanguage === language) {
  462. return;
  463. }
  464. // if language is built-in (English) skip update
  465. if (newLanguage === 'en') {
  466. language = 'en';
  467. return;
  468. }
  469. // if language is not supported, show error
  470. if (supportedLanguages.indexOf(newLanguage) === -1) {
  471. console.error('Language \'%s\' is not supported. Translation failed, fallback to English.', newLanguage);
  472. language = 'en';
  473. return;
  474. }
  475. // load strings from JSON
  476. $.getJSON('i18n/' + newLanguage + '.json', function(data) {
  477. language = newLanguage;
  478. translations = data;
  479. $(document).triggerHandler(languageLoadedEvent);
  480. }).fail(function (data, textStatus, errorMsg) {
  481. console.error('Language \'%s\' could not be loaded (%s: %s). Translation failed, fallback to English.', newLanguage, textStatus, errorMsg);
  482. language = 'en';
  483. });
  484. };
  485. return me;
  486. })(window, document);
  487. /**
  488. * handles everything related to en/decryption
  489. *
  490. * @class
  491. */
  492. var CryptTool = (function () {
  493. var me = {};
  494. /**
  495. * compress a message (deflate compression), returns base64 encoded data
  496. *
  497. * @name cryptToolcompress
  498. * @function
  499. * @private
  500. * @param {string} message
  501. * @return {string} base64 data
  502. */
  503. function compress(message)
  504. {
  505. return Base64.toBase64( RawDeflate.deflate( Base64.utob(message) ) );
  506. }
  507. /**
  508. * decompress a message compressed with cryptToolcompress()
  509. *
  510. * @name cryptTooldecompress
  511. * @function
  512. * @private
  513. * @param {string} data - base64 data
  514. * @return {string} message
  515. */
  516. function decompress(data)
  517. {
  518. return Base64.btou( RawDeflate.inflate( Base64.fromBase64(data) ) );
  519. }
  520. /**
  521. * compress, then encrypt message with given key and password
  522. *
  523. * @name CryptTool.cipher
  524. * @function
  525. * @param {string} key
  526. * @param {string} password
  527. * @param {string} message
  528. * @return {string} data - JSON with encrypted data
  529. */
  530. me.cipher = function(key, password, message)
  531. {
  532. // Galois Counter Mode, keysize 256 bit, authentication tag 128 bit
  533. var options = {
  534. mode: 'gcm',
  535. ks: 256,
  536. ts: 128
  537. };
  538. if ((password || '').trim().length === 0)
  539. {
  540. return sjcl.encrypt(key, compress(message), options);
  541. }
  542. return sjcl.encrypt(key + sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(password)), compress(message), options);
  543. };
  544. /**
  545. * decrypt message with key, then decompress
  546. *
  547. * @name CryptTool.decipher
  548. * @function
  549. * @param {string} key
  550. * @param {string} password
  551. * @param {string} data - JSON with encrypted data
  552. * @return {string} decrypted message
  553. */
  554. me.decipher = function(key, password, data)
  555. {
  556. if (data !== undefined)
  557. {
  558. try
  559. {
  560. return decompress(sjcl.decrypt(key, data));
  561. }
  562. catch(err)
  563. {
  564. try
  565. {
  566. return decompress(sjcl.decrypt(key + sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(password)), data));
  567. }
  568. catch(e)
  569. {
  570. // ignore error, because ????? @TODO
  571. }
  572. }
  573. }
  574. return '';
  575. };
  576. /**
  577. * checks whether the crypt tool is ready.
  578. *
  579. * @name CryptTool.isReady
  580. * @function
  581. * @return {bool}
  582. */
  583. me.isEntropyReady = function()
  584. {
  585. return sjcl.random.isReady();
  586. };
  587. /**
  588. * checks whether the crypt tool is ready.
  589. *
  590. * @name CryptTool.isReady
  591. * @function
  592. * @param {function} func
  593. */
  594. me.addEntropySeedListener = function(func)
  595. {
  596. sjcl.random.addEventListener('seeded', func);
  597. };
  598. /**
  599. * returns a random symmetric key
  600. *
  601. * @name CryptTool.getSymmetricKey
  602. * @function
  603. * @return {string} func
  604. */
  605. me.getSymmetricKey = function(func)
  606. {
  607. return sjcl.codec.base64.fromBits(sjcl.random.randomWords(8, 0), 0);
  608. };
  609. /**
  610. * initialize crypt tool
  611. *
  612. * @name CryptTool.init
  613. * @function
  614. */
  615. me.init = function()
  616. {
  617. // will fail earlier as sjcl is already passed as a parameter
  618. // if (typeof sjcl !== 'object') {
  619. // Alert.showError(
  620. // I18n._('The library %s is not available.', 'sjcl') +
  621. // I18n._('Messages cannot be decrypted or encrypted.')
  622. // );
  623. // }
  624. };
  625. return me;
  626. })();
  627. /**
  628. * (modal) Data source (aka MVC)
  629. *
  630. * @param {object} window
  631. * @param {object} document
  632. * @class
  633. */
  634. var Modal = (function (window, document) {
  635. var me = {};
  636. var $cipherData;
  637. var id = null, symmetricKey = null;
  638. /**
  639. * returns the expiration set in the HTML
  640. *
  641. * @name Modal.getExpirationDefault
  642. * @function
  643. * @return string
  644. * @TODO the template can be simplified as #pasteExpiration is no longer modified (only default value)
  645. */
  646. me.getExpirationDefault = function()
  647. {
  648. return $('#pasteExpiration').val();
  649. };
  650. /**
  651. * returns the format set in the HTML
  652. *
  653. * @name Modal.getFormatDefault
  654. * @function
  655. * @return string
  656. * @TODO the template can be simplified as #pasteFormatter is no longer modified (only default value)
  657. */
  658. me.getFormatDefault = function()
  659. {
  660. return $('#pasteFormatter').val();
  661. };
  662. /**
  663. * check if cipher data was supplied
  664. *
  665. * @name Modal.getCipherData
  666. * @function
  667. * @return boolean
  668. */
  669. me.hasCipherData = function()
  670. {
  671. return (me.getCipherData().length > 0);
  672. };
  673. /**
  674. * returns the cipher data
  675. *
  676. * @name Modal.getCipherData
  677. * @function
  678. * @return string
  679. */
  680. me.getCipherData = function()
  681. {
  682. return $cipherData.text();
  683. };
  684. /**
  685. * get the pastes unique identifier from the URL,
  686. * eg. http://example.com/path/?c05354954c49a487#dfdsdgdgdfgdf returns c05354954c49a487
  687. *
  688. * @name Modal.getPasteId
  689. * @function
  690. * @return {string} unique identifier
  691. */
  692. me.getPasteId = function()
  693. {
  694. if (id === null) {
  695. id = window.location.search.substring(1);
  696. }
  697. return id;
  698. };
  699. /**
  700. * return the deciphering key stored in anchor part of the URL
  701. *
  702. * @name Modal.getPasteKey
  703. * @function
  704. * @return {string} key
  705. */
  706. me.getPasteKey = function()
  707. {
  708. if (symmetricKey === null) {
  709. symmetricKey = window.location.hash.substring(1);
  710. // Some web 2.0 services and redirectors add data AFTER the anchor
  711. // (such as &utm_source=...). We will strip any additional data.
  712. var ampersandPos = symmetricKey.indexOf('&');
  713. if (ampersandPos > -1)
  714. {
  715. symmetricKey = symmetricKey.substring(0, ampersandPos);
  716. }
  717. }
  718. return symmetricKey;
  719. };
  720. /**
  721. * init navigation manager
  722. *
  723. * preloads jQuery elements
  724. *
  725. * @name Modal.init
  726. * @function
  727. */
  728. me.init = function()
  729. {
  730. $cipherData = $('#cipherdata');
  731. };
  732. return me;
  733. })(window, document);
  734. /**
  735. * Helper functions for user interface
  736. *
  737. * everything directly UI-related, which fits nowhere else
  738. *
  739. * @param {object} window
  740. * @param {object} document
  741. * @class
  742. */
  743. var UiHelper = (function (window, document) {
  744. var me = {};
  745. /**
  746. * handle history (pop) state changes
  747. *
  748. * currently this does only handle redirects to the home page.
  749. *
  750. * @private
  751. * @function
  752. * @param {Event} event
  753. */
  754. function historyChange(event)
  755. {
  756. var currentLocation = Helper.baseUri();
  757. if (event.originalEvent.state === null && // no state object passed
  758. event.originalEvent.target.location.href === currentLocation && // target location is home page
  759. window.location.href === currentLocation // and we are not already on the home page
  760. ) {
  761. // redirect to home page
  762. window.location.href = currentLocation;
  763. }
  764. };
  765. /**
  766. * reload the page
  767. *
  768. * This takes the user to the PrivateBin homepage.
  769. *
  770. * @name UiHelper.reloadHome
  771. * @function
  772. */
  773. me.reloadHome = function()
  774. {
  775. window.location.href = Helper.baseUri();
  776. };
  777. /**
  778. * initialize
  779. *
  780. * @name UiHelper.init
  781. * @function
  782. */
  783. me.init = function()
  784. {
  785. // update link to home page
  786. $('.reloadlink').prop('href', Helper.baseUri());
  787. $(window).on('popstate', historyChange);
  788. };
  789. return me;
  790. })(window, document);
  791. /**
  792. * Alert/error manager
  793. *
  794. * @param {object} window
  795. * @param {object} document
  796. * @class
  797. */
  798. var Alert = (function (window, document) {
  799. var me = {};
  800. var $errorMessage,
  801. $status;
  802. /**
  803. * display a status message
  804. *
  805. * @name Alert.showStatus
  806. * @function
  807. * @param {string} message - text to display
  808. * @param {boolean} [spin=false] - (optional) tell if the "spinning" animation should be displayed, defaults to false
  809. */
  810. me.showStatus = function(message, spin)
  811. {
  812. // spin is ignored for now
  813. $status.text(message);
  814. };
  815. /**
  816. * hides any status messages
  817. *
  818. * @name Alert.hideMessages
  819. * @function
  820. */
  821. me.hideMessages = function()
  822. {
  823. $status.html(' ');
  824. $errorMessage.addClass('hidden');
  825. };
  826. /**
  827. * display an error message
  828. *
  829. * @name Alert.showError
  830. * @function
  831. * @param {string} message - text to display
  832. */
  833. me.showError = function(message)
  834. {
  835. console.error('Error shown: ' + message);
  836. $errorMessage.removeClass('hidden');
  837. $errorMessage.find(':last').text(' ' + message);
  838. };
  839. /**
  840. * init status manager
  841. *
  842. * preloads jQuery elements
  843. *
  844. * @name Alert.init
  845. * @function
  846. */
  847. me.init = function()
  848. {
  849. // hide "no javascript" message
  850. $('#noscript').hide();
  851. $errorMessage = $('#errormessage');
  852. $status = $('#status');
  853. // display status returned by php code, if any (eg. paste was properly deleted)
  854. // @TODO remove this by handling errors in a different way
  855. if ($status.text().length > 0)
  856. {
  857. me.showStatus($status.text());
  858. return;
  859. }
  860. // keep line height even if content empty
  861. $status.html(' '); // @TODO what? remove?
  862. // display error message from php code
  863. if ($errorMessage.text().length > 1) {
  864. Alert.showError($errorMessage.text());
  865. }
  866. };
  867. return me;
  868. })(window, document);
  869. /**
  870. * handles paste status/result
  871. *
  872. * @param {object} window
  873. * @param {object} document
  874. * @class
  875. */
  876. var PasteStatus = (function (window, document) {
  877. var me = {};
  878. var $pasteSuccess,
  879. $shortenButton,
  880. $pasteUrl,
  881. $remainingTime;
  882. /**
  883. * forward to URL shortener
  884. *
  885. * @private
  886. * @function
  887. * @param {Event} event
  888. */
  889. function sendToShortener(event)
  890. {
  891. window.location.href = $shortenButton.data('shortener')
  892. + encodeURIComponent($pasteUrl.attr('href'));
  893. }
  894. /**
  895. * Forces opening the paste if the link does not do this automatically.
  896. *
  897. * This is necessary as browsers will not reload the page when it is
  898. * already loaded (which is fake as it is set via history.pushState()).
  899. *
  900. * @name Controller.pasteLinkClick
  901. * @function
  902. * @param {Event} event
  903. */
  904. function pasteLinkClick(event)
  905. {
  906. // check if location is (already) shown in URL bar
  907. if (window.location.href === $pasteUrl.attr('href')) {
  908. // if so we need to load link by reloading the current site
  909. window.location.reload(true);
  910. }
  911. }
  912. /**
  913. * creates a notification after a successfull paste upload
  914. *
  915. * @name PasteStatus.createPasteNotification
  916. * @function
  917. * @param {string} url
  918. * @param {string} deleteUrl
  919. */
  920. me.createPasteNotification = function(url, deleteUrl)
  921. {
  922. $('#pastelink').find(':first').html(
  923. I18n._(
  924. 'Your paste is <a id="pasteurl" href="%s">%s</a> <span id="copyhint">(Hit [Ctrl]+[c] to copy)</span>',
  925. url, url
  926. )
  927. );
  928. // save newly created element
  929. $pasteUrl = $('#pasteurl');
  930. // and add click event
  931. $pasteUrl.click(pasteLinkClick);
  932. // shorten button
  933. $('#deletelink').html('<a href="' + deleteUrl + '">' + I18n._('Delete data') + '</a>');
  934. // show result
  935. $pasteSuccess.removeClass('hidden');
  936. // we pre-select the link so that the user only has to [Ctrl]+[c] the link
  937. Helper.selectText($pasteUrl[0]);
  938. };
  939. /**
  940. * shows the remaining time
  941. *
  942. * @function
  943. * @param {object} pasteMetaData
  944. */
  945. me.showRemainingTime = function(pasteMetaData)
  946. {
  947. if (pasteMetaData.burnafterreading) {
  948. // display paste "for your eyes only" if it is deleted
  949. // actually remove paste, before we claim it is deleted
  950. Controller.removePaste(Modal.getPasteId(), 'burnafterreading');
  951. I18n._($remainingTime.find(':last'), "FOR YOUR EYES ONLY. Don't close this window, this message can't be displayed again.");
  952. $remainingTime.addClass('foryoureyesonly');
  953. // discourage cloning (it cannot really be prevented)
  954. TopNav.hideCloneButton();
  955. } else if (pasteMetaData.expire_date) {
  956. // display paste expiration
  957. var expiration = Helper.secondsToHuman(pasteMetaData.remaining_time),
  958. expirationLabel = [
  959. 'This document will expire in %d ' + expiration[1] + '.',
  960. 'This document will expire in %d ' + expiration[1] + 's.'
  961. ];
  962. I18n._($remainingTime.find(':last'), expirationLabel, expiration[0]);
  963. $remainingTime.removeClass('foryoureyesonly')
  964. } else {
  965. // never expires
  966. return;
  967. }
  968. // in the end, display notification
  969. $remainingTime.removeClass('hidden');
  970. };
  971. /**
  972. * init status manager
  973. *
  974. * preloads jQuery elements
  975. *
  976. * @name Alert.init
  977. * @function
  978. */
  979. me.init = function()
  980. {
  981. $shortenButton = $('#shortenbutton');
  982. $pasteSuccess = $('#pasteSuccess');
  983. // $pasteUrl is saved in me.createPasteNotification() after creation
  984. $remainingTime = $('#remainingtime');
  985. // bind elements
  986. $shortenButton.click(sendToShortener);
  987. };
  988. return me;
  989. })(window, document);
  990. /**
  991. * password prompt
  992. *
  993. * @param {object} window
  994. * @param {object} document
  995. * @class
  996. */
  997. var Prompt = (function (window, document) {
  998. var me = {};
  999. var $passwordModal,
  1000. $passwordForm,
  1001. $passwordDecrypt;
  1002. var password = '',
  1003. passwordCallback = null;
  1004. /**
  1005. * ask the user for the password and set it
  1006. *
  1007. * the callback set via setPasswordCallback is executed
  1008. *
  1009. * @name Prompt.requestPassword()
  1010. * @function
  1011. */
  1012. me.requestPassword = function()
  1013. {
  1014. // show new bootstrap method
  1015. $passwordModal.modal({
  1016. backdrop: 'static',
  1017. keyboard: false
  1018. });
  1019. };
  1020. /**
  1021. * get cached password or password from easy Prompt
  1022. *
  1023. * If you do not get a password with this function, use
  1024. * requestPassword
  1025. *
  1026. * @name Prompt.getPassword
  1027. * @function
  1028. */
  1029. me.getPassword = function()
  1030. {
  1031. if (password.length !== 0) {
  1032. return password;
  1033. }
  1034. if ($passwordModal.length === 0) {
  1035. // old method for page template
  1036. var newPassword = Prompt(I18n._('Please enter the password for this paste:'), '');
  1037. if (newPassword === null) {
  1038. throw 'password Prompt canceled';
  1039. }
  1040. if (password.length === 0) {
  1041. // recursive…
  1042. me.getPassword();
  1043. } else {
  1044. password = newPassword;
  1045. }
  1046. }
  1047. return password;
  1048. };
  1049. /**
  1050. * setsthe callback called when password is entered
  1051. *
  1052. * @name Prompt.setPasswordCallback
  1053. * @function
  1054. * @param {functions} setPasswordCallback
  1055. */
  1056. me.setPasswordCallback = function(callback)
  1057. {
  1058. passwordCallback = callback;
  1059. };
  1060. /**
  1061. * submit a password in the Modal dialog
  1062. *
  1063. * @private
  1064. * @function
  1065. * @param {Event} event
  1066. */
  1067. function submitPasswordModal(event)
  1068. {
  1069. // get input
  1070. password = $passwordDecrypt.val();
  1071. // hide modal
  1072. $passwordModal.modal('hide');
  1073. if (passwordCallback !== null) {
  1074. passwordCallback();
  1075. }
  1076. event.preventDefault();
  1077. }
  1078. /**
  1079. * init status manager
  1080. *
  1081. * preloads jQuery elements
  1082. *
  1083. * @name Controller.init
  1084. * @function
  1085. */
  1086. me.init = function()
  1087. {
  1088. $passwordModal = $('#passwordmodal');
  1089. $passwordForm = $('#passwordform');
  1090. $passwordDecrypt = $('#passworddecrypt');
  1091. // bind events
  1092. // focus password input when it is shown
  1093. $passwordModal.on('shown.bs.Modal', function () {
  1094. $passwordDecrypt.focus();
  1095. });
  1096. // handle Modal password submission
  1097. $passwordForm.submit(submitPasswordModal);
  1098. };
  1099. return me;
  1100. })(window, document);
  1101. /**
  1102. * Manage paste/message input, and preview tab
  1103. *
  1104. * Note that the actual preview is handled by PasteViewer.
  1105. *
  1106. * @param {object} window
  1107. * @param {object} document
  1108. * @class
  1109. */
  1110. var Editor = (function (window, document) {
  1111. var me = {};
  1112. var $message,
  1113. $messageEdit,
  1114. $messagePreview,
  1115. $editorTabs;
  1116. var isPreview = false;
  1117. /**
  1118. * support input of tab character
  1119. *
  1120. * @name Editor.supportTabs
  1121. * @function
  1122. * @param {Event} event
  1123. * @TODO doc what is @this here?
  1124. * @TODO replace this with $message ??
  1125. */
  1126. function supportTabs(event)
  1127. {
  1128. var keyCode = event.keyCode || event.which;
  1129. // tab was pressed
  1130. if (keyCode === 9)
  1131. {
  1132. // prevent the textarea to lose focus
  1133. event.preventDefault();
  1134. // get caret position & selection
  1135. var val = this.value,
  1136. start = this.selectionStart,
  1137. end = this.selectionEnd;
  1138. // set textarea value to: text before caret + tab + text after caret
  1139. this.value = val.substring(0, start) + '\t' + val.substring(end);
  1140. // put caret at right position again
  1141. this.selectionStart = this.selectionEnd = start + 1;
  1142. }
  1143. }
  1144. /**
  1145. * view the Editor tab
  1146. *
  1147. * @name Editor.viewEditor
  1148. * @function
  1149. * @param {Event} event - optional
  1150. */
  1151. function viewEditor(event)
  1152. {
  1153. // toggle buttons
  1154. $messageEdit.addClass('active');
  1155. $messagePreview.removeClass('active');
  1156. PasteViewer.hide();
  1157. // reshow input
  1158. $message.removeClass('hidden');
  1159. me.focusInput();
  1160. // finish
  1161. isPreview = false;
  1162. // prevent jumping of page to top
  1163. if (typeof event !== 'undefined') {
  1164. event.preventDefault();
  1165. }
  1166. }
  1167. /**
  1168. * view the preview tab
  1169. *
  1170. * @name Editor.viewPreview
  1171. * @function
  1172. * @param {Event} event
  1173. */
  1174. function viewPreview(event)
  1175. {
  1176. // toggle buttons
  1177. $messageEdit.removeClass('active');
  1178. $messagePreview.addClass('active');
  1179. // hide input as now preview is shown
  1180. $message.addClass('hidden');
  1181. // show preview
  1182. $('#errormessage').find(':last')
  1183. PasteViewer.setText($message.val());
  1184. PasteViewer.run();
  1185. // finish
  1186. isPreview = true;
  1187. // prevent jumping of page to top
  1188. if (typeof event !== 'undefined') {
  1189. event.preventDefault();
  1190. }
  1191. }
  1192. /**
  1193. * get the state of the preview
  1194. *
  1195. * @name Editor.isPreview
  1196. * @function
  1197. */
  1198. me.isPreview = function()
  1199. {
  1200. return isPreview;
  1201. }
  1202. /**
  1203. * reset the Editor view
  1204. *
  1205. * @name Editor.resetInput
  1206. * @function
  1207. */
  1208. me.resetInput = function()
  1209. {
  1210. // go back to input
  1211. if (isPreview) {
  1212. viewEditor();
  1213. }
  1214. // clear content
  1215. $message.val('');
  1216. };
  1217. /**
  1218. * shows the Editor
  1219. *
  1220. * @name Editor.show
  1221. * @function
  1222. */
  1223. me.show = function()
  1224. {
  1225. $message.removeClass('hidden');
  1226. $editorTabs.removeClass('hidden');
  1227. };
  1228. /**
  1229. * hides the Editor
  1230. *
  1231. * @name Editor.reset
  1232. * @function
  1233. */
  1234. me.hide = function()
  1235. {
  1236. $message.addClass('hidden');
  1237. $editorTabs.addClass('hidden');
  1238. };
  1239. /**
  1240. * focuses the message input
  1241. *
  1242. * @name Editor.focusInput
  1243. * @function
  1244. */
  1245. me.focusInput = function()
  1246. {
  1247. $message.focus();
  1248. };
  1249. /**
  1250. * returns the current text
  1251. *
  1252. * @name Editor.getText
  1253. * @function
  1254. * @return {string}
  1255. */
  1256. me.getText = function()
  1257. {
  1258. return $message.val()
  1259. };
  1260. /**
  1261. * init status manager
  1262. *
  1263. * preloads jQuery elements
  1264. *
  1265. * @name Editor.init
  1266. * @function
  1267. */
  1268. me.init = function()
  1269. {
  1270. $message = $('#message');
  1271. $editorTabs = $('#editorTabs');
  1272. // bind events
  1273. $message.keydown(supportTabs);
  1274. // bind click events to tab switchers (a), but save parent of them
  1275. // (li)
  1276. $messageEdit = $('#messageedit').click(viewEditor).parent();
  1277. $messagePreview = $('#messagepreview').click(viewPreview).parent();
  1278. };
  1279. return me;
  1280. })(window, document);
  1281. /**
  1282. * (view) Parse and show paste.
  1283. *
  1284. * @param {object} window
  1285. * @param {object} document
  1286. * @class
  1287. */
  1288. var PasteViewer = (function (window, document) {
  1289. var me = {};
  1290. var $clonedFile,
  1291. $plainText,
  1292. $placeholder,
  1293. $prettyMessage,
  1294. $prettyPrint;
  1295. var text,
  1296. format = 'plaintext',
  1297. isDisplayed = false,
  1298. isChanged = true; // by default true as nothing was parsed yet
  1299. /**
  1300. * apply the set format on paste and displays it
  1301. *
  1302. * @private
  1303. * @function
  1304. */
  1305. function parsePaste()
  1306. {
  1307. // skip parsing if no text is given
  1308. if (text === '') {
  1309. return;
  1310. }
  1311. // set text
  1312. Helper.setElementText($plainText, text);
  1313. Helper.setElementText($prettyPrint, text);
  1314. switch (format) {
  1315. case 'markdown':
  1316. var converter = new showdown.Converter({
  1317. strikethrough: true,
  1318. tables: true,
  1319. tablesHeaderId: true
  1320. });
  1321. $plainText.html(
  1322. converter.makeHtml(text)
  1323. );
  1324. // add table classes from bootstrap css
  1325. $plainText.find('table').addClass('table-condensed table-bordered');
  1326. break;
  1327. case 'syntaxhighlighting':
  1328. // @TODO is this really needed or is "one" enough?
  1329. if (typeof prettyPrint === 'function')
  1330. {
  1331. prettyPrint();
  1332. }
  1333. $prettyPrint.html(
  1334. prettyPrintOne(
  1335. Helper.htmlEntities(text), null, true
  1336. )
  1337. );
  1338. // fall through, as the rest is the same
  1339. default: // = 'plaintext'
  1340. // convert URLs to clickable links
  1341. Helper.urls2links($plainText);
  1342. Helper.urls2links($prettyPrint);
  1343. $prettyPrint.css('white-space', 'pre-wrap');
  1344. $prettyPrint.css('word-break', 'normal');
  1345. $prettyPrint.removeClass('prettyprint');
  1346. }
  1347. }
  1348. /**
  1349. * displays the paste
  1350. *
  1351. * @private
  1352. * @function
  1353. */
  1354. function showPaste()
  1355. {
  1356. // instead of "nothing" better display a placeholder
  1357. if (text === '') {
  1358. $placeholder.removeClass('hidden')
  1359. return;
  1360. }
  1361. // otherwise hide the placeholder
  1362. $placeholder.addClass('hidden')
  1363. switch (format) {
  1364. case 'markdown':
  1365. $plainText.removeClass('hidden');
  1366. $prettyMessage.addClass('hidden');
  1367. break;
  1368. default:
  1369. $plainText.addClass('hidden');
  1370. $prettyMessage.removeClass('hidden');
  1371. break;
  1372. }
  1373. }
  1374. /**
  1375. * sets the format in which the text is shown
  1376. *
  1377. * @name PasteViewer.setFormat
  1378. * @function
  1379. * @param {string} the the new format
  1380. */
  1381. me.setFormat = function(newFormat)
  1382. {
  1383. if (format !== newFormat) {
  1384. format = newFormat;
  1385. isChanged = true;
  1386. }
  1387. };
  1388. /**
  1389. * returns the current format
  1390. *
  1391. * @name PasteViewer.setFormat
  1392. * @function
  1393. * @return {string}
  1394. */
  1395. me.getFormat = function()
  1396. {
  1397. return format;
  1398. };
  1399. /**
  1400. * returns whether the current view is pretty printed
  1401. *
  1402. * @name PasteViewer.isPrettyPrinted
  1403. * @function
  1404. * @return {bool}
  1405. */
  1406. me.isPrettyPrinted = function()
  1407. {
  1408. return $prettyPrint.hasClass('prettyprinted');
  1409. };
  1410. /**
  1411. * sets the text to show
  1412. *
  1413. * @name Editor.init
  1414. * @function
  1415. * @param {string} newText the text to show
  1416. */
  1417. me.setText = function(newText)
  1418. {
  1419. if (text !== newText) {
  1420. text = newText;
  1421. isChanged = true;
  1422. }
  1423. };
  1424. /**
  1425. * show/update the parsed text (preview)
  1426. *
  1427. * @name PasteViewer.run
  1428. * @function
  1429. */
  1430. me.run = function()
  1431. {
  1432. if (isChanged) {
  1433. parsePaste();
  1434. isChanged = false;
  1435. }
  1436. if (!isDisplayed) {
  1437. showPaste();
  1438. isDisplayed = true;
  1439. }
  1440. };
  1441. /**
  1442. * hide parsed text (preview)
  1443. *
  1444. * @name PasteViewer.hide
  1445. * @function
  1446. */
  1447. me.hide = function()
  1448. {
  1449. if (!isDisplayed) {
  1450. console.warn('PasteViewer was called to hide the parsed view, but it is already hidden.');
  1451. }
  1452. $plainText.addClass('hidden');
  1453. $prettyMessage.addClass('hidden');
  1454. $placeholder.addClass('hidden');
  1455. isDisplayed = false;
  1456. };
  1457. /**
  1458. * init status manager
  1459. *
  1460. * preloads jQuery elements
  1461. *
  1462. * @name Editor.init
  1463. * @function
  1464. */
  1465. me.init = function()
  1466. {
  1467. $plainText = $('#plaintext');
  1468. $placeholder = $('#placeholder');
  1469. $prettyMessage = $('#prettymessage');
  1470. $prettyPrint = $('#prettyprint');
  1471. // check requirements
  1472. if (typeof prettyPrintOne !== 'function') {
  1473. Alert.showError(
  1474. I18n._('The library %s is not available.', 'pretty print') +
  1475. I18n._('This may cause display errors.')
  1476. );
  1477. }
  1478. if (typeof showdown !== 'object') {
  1479. Alert.showError(
  1480. I18n._('The library %s is not available.', 'showdown') +
  1481. I18n._('This may cause display errors.')
  1482. );
  1483. }
  1484. // get default option from template/HTML or fall back to set value
  1485. format = Modal.getFormatDefault() || format;
  1486. };
  1487. return me;
  1488. })(window, document);
  1489. /**
  1490. * (view) Show attachment and preview if possible
  1491. *
  1492. * @param {object} window
  1493. * @param {object} document
  1494. * @class
  1495. */
  1496. var AttachmentViewer = (function (window, document) {
  1497. var me = {};
  1498. var $attachment,
  1499. $attachmentLink,
  1500. $clonedFile,
  1501. $attachmentPreview,
  1502. $fileWrap;
  1503. var attachmentChanged = false,
  1504. attachmentHasPreview = false;
  1505. /**
  1506. * sets the attachment but does not yet show it
  1507. *
  1508. * @name AttachmentViewer.setAttachment
  1509. * @function
  1510. * @param {string} attachmentData - base64-encoded data of file
  1511. * @param {string} fileName - optional, file name
  1512. */
  1513. me.setAttachment = function(attachmentData, fileName)
  1514. {
  1515. var imagePrefix = 'data:image/';
  1516. $attachmentLink.attr('href', attachmentData);
  1517. if (typeof fileName !== 'undefined') {
  1518. $attachmentLink.attr('download', fileName);
  1519. }
  1520. // if the attachment is an image, display it
  1521. if (attachmentData.substring(0, imagePrefix.length) === imagePrefix) {
  1522. $attachmentPreview.html(
  1523. $(document.createElement('img'))
  1524. .attr('src', attachmentData)
  1525. .attr('class', 'img-thumbnail')
  1526. );
  1527. attachmentHasPreview = true;
  1528. }
  1529. attachmentChanged = true;
  1530. };
  1531. /**
  1532. * displays the attachment
  1533. *
  1534. * @name AttachmentViewer.showAttachment
  1535. * @function
  1536. */
  1537. me.showAttachment = function()
  1538. {
  1539. $attachment.removeClass('hidden');
  1540. if (attachmentHasPreview) {
  1541. $attachmentPreview.removeClass('hidden');
  1542. }
  1543. }
  1544. /**
  1545. * removes the existing attachment
  1546. *
  1547. * @name AttachmentViewer.removeAttachment
  1548. * @function
  1549. */
  1550. me.removeAttachment = function()
  1551. {
  1552. // (new)
  1553. $attachment.addClass('hidden');
  1554. $attachmentPreview.addClass('hidden');
  1555. $clonedFile.addClass('hidden');
  1556. // removes the saved decrypted file data
  1557. $attachmentLink.attr('href', '');
  1558. // the only way to deselect the file is to recreate the input // @TODO really?
  1559. $fileWrap.html($fileWrap.html());
  1560. $fileWrap.removeClass('hidden');
  1561. // reset internal variables
  1562. };
  1563. /**
  1564. * checks if there is an attachment
  1565. *
  1566. * @name AttachmentViewer.hasAttachment
  1567. * @function
  1568. */
  1569. me.hasAttachment = function()
  1570. {
  1571. return typeof $attachmentLink.attr('href') !== 'undefined'
  1572. };
  1573. /**
  1574. * return the attachment
  1575. *
  1576. * @name AttachmentViewer.getAttachment
  1577. * @function
  1578. * @returns {array}
  1579. */
  1580. me.getAttachment = function()
  1581. {
  1582. return [
  1583. $attachmentLink.attr('href'),
  1584. $attachmentLink.attr('download')
  1585. ];
  1586. };
  1587. /**
  1588. * initiate
  1589. *
  1590. * preloads jQuery elements
  1591. *
  1592. * @name AttachmentViewer.init
  1593. * @function
  1594. */
  1595. me.init = function()
  1596. {
  1597. $attachmentPreview = $('#attachmentPreview');
  1598. $attachment = $('#attachment');
  1599. $attachmentLink = $('#attachment a');
  1600. $clonedFile = $('#clonedfile');
  1601. $fileWrap = $('#filewrap');
  1602. };
  1603. return me;
  1604. })(window, document);
  1605. /**
  1606. * (view) Shows discussion thread and handles replies
  1607. *
  1608. * @param {object} window
  1609. * @param {object} document
  1610. * @class
  1611. */
  1612. var DiscussionViewer = (function (window, document) {
  1613. var me = {};
  1614. var $comments,
  1615. $discussion;
  1616. /**
  1617. * display a status message for replying to comments
  1618. *
  1619. * @name Controller.showStatus
  1620. * @function
  1621. * @param {string} message - text to display
  1622. * @param {boolean} [spin=false] - (optional) tell if the "spinning" animation should be displayed, defaults to false
  1623. */
  1624. me.showReplyStatus = function(message, spin)
  1625. {
  1626. if (spin || false) {
  1627. $replyalert.find('.spinner').removeClass('hidden')
  1628. }
  1629. $replyalert.text(message);
  1630. };
  1631. /**
  1632. * display an error message
  1633. *
  1634. * @name Alert.showError
  1635. * @function
  1636. * @param {string} message - text to display
  1637. */
  1638. me.showReplyError = function(message)
  1639. {
  1640. $replyalert.addClass('Alert-danger');
  1641. $replyalert.addClass($errorMessage.attr('class')); // @TODO ????
  1642. $replyalert.text(message);
  1643. };
  1644. /**
  1645. * open the comment entry when clicking the "Reply" button of a comment
  1646. *
  1647. * @name PasteViewer.openReply
  1648. * @function
  1649. * @param {Event} event
  1650. */
  1651. me.openReply = function(event)
  1652. {
  1653. event.preventDefault();
  1654. // remove any other reply area
  1655. $('div.reply').remove();
  1656. var source = $(event.target),
  1657. commentid = event.data.commentid,
  1658. hint = I18n._('Optional nickname...'),
  1659. $reply = $('#replytemplate');
  1660. $reply.find('button').click(
  1661. {parentid: commentid},
  1662. me.sendComment
  1663. );
  1664. source.after($reply);
  1665. $replyStatus = $('#replystatus'); // when ID --> put into HTML
  1666. $('#replymessage').focus();
  1667. };
  1668. /**
  1669. * initiate
  1670. *
  1671. * preloads jQuery elements
  1672. *
  1673. * @name AttachmentViewer.init
  1674. * @function
  1675. */
  1676. me.init = function()
  1677. {
  1678. $comments = $('#comments');
  1679. $discussion = $('#discussion');
  1680. // $replyStatus in openReply()
  1681. };
  1682. return me;
  1683. })(window, document);
  1684. /**
  1685. * Manage top (navigation) bar
  1686. *
  1687. * @param {object} window
  1688. * @param {object} document
  1689. * @class
  1690. */
  1691. var TopNav = (function (window, document) {
  1692. var me = {};
  1693. var createButtonsDisplayed = false;
  1694. var viewButtonsDisplayed = false;
  1695. var $attach,
  1696. $burnAfterReading,
  1697. $burnAfterReadingOption,
  1698. $cloneButton,
  1699. $expiration,
  1700. $fileRemoveButton,
  1701. $formatter,
  1702. $newButton,
  1703. $openDiscussionOption,
  1704. $openDiscussion,
  1705. $password,
  1706. $passwordInput,
  1707. $rawTextButton,
  1708. $sendButton,
  1709. $loadingIndicator;
  1710. var pasteExpiration = '1week';
  1711. /**
  1712. * set the expiration on bootstrap templates in dropdown
  1713. *
  1714. * @name TopNav.updateExpiration
  1715. * @function
  1716. * @param {Event} event
  1717. */
  1718. function updateExpiration(event)
  1719. {
  1720. // get selected option
  1721. var target = $(event.target);
  1722. // update dropdown display and save new expiration time
  1723. $('#pasteExpirationDisplay').text(target.text());
  1724. pasteExpiration = target.data('expiration');
  1725. event.preventDefault();
  1726. }
  1727. /**
  1728. * set the format on bootstrap templates in dropdown
  1729. *
  1730. * @name TopNav.updateFormat
  1731. * @function
  1732. * @param {Event} event
  1733. */
  1734. function updateFormat(event)
  1735. {
  1736. // get selected option
  1737. var $target = $(event.target);
  1738. // update dropdown display and save new format
  1739. var newFormat = $target.data('format');
  1740. $('#pasteFormatterDisplay').text($target.text());
  1741. PasteViewer.setFormat(newFormat);
  1742. // update preview
  1743. if (Editor.isPreview()) {
  1744. PasteViewer.run();
  1745. }
  1746. event.preventDefault();
  1747. }
  1748. /**
  1749. * when "burn after reading" is checked, disable discussion
  1750. *
  1751. * @name TopNav.changeBurnAfterReading
  1752. * @function
  1753. */
  1754. function changeBurnAfterReading()
  1755. {
  1756. if ($burnAfterReading.is(':checked')) {
  1757. $openDiscussionOption.addClass('buttondisabled');
  1758. $openDiscussion.prop('checked', false);
  1759. // if button is actually disabled, force-enable it and uncheck other button
  1760. $burnAfterReadingOption.removeClass('buttondisabled');
  1761. } else {
  1762. $openDiscussionOption.removeClass('buttondisabled');
  1763. }
  1764. }
  1765. /**
  1766. * when discussion is checked, disable "burn after reading"
  1767. *
  1768. * @name TopNav.changeOpenDiscussion
  1769. * @function
  1770. */
  1771. function changeOpenDiscussion()
  1772. {
  1773. if ($openDiscussion.is(':checked')) {
  1774. $burnAfterReadingOption.addClass('buttondisabled');
  1775. $burnAfterReading.prop('checked', false);
  1776. // if button is actually disabled, force-enable it and uncheck other button
  1777. $openDiscussionOption.removeClass('buttondisabled');
  1778. } else {
  1779. $burnAfterReadingOption.removeClass('buttondisabled');
  1780. }
  1781. }
  1782. /**
  1783. * return raw text
  1784. *
  1785. * @name TopNav.rawText
  1786. * @function
  1787. * @param {Event} event
  1788. */
  1789. function rawText(event)
  1790. {
  1791. var paste = PasteViewer.getFormat() === 'markdown' ?
  1792. $prettyPrint.text() : $plainText.text();
  1793. history.pushState(
  1794. null, document.title, Helper.baseUri() + '?' +
  1795. Modal.getPasteId() + '#' + Modal.getPasteKey()
  1796. );
  1797. // we use text/html instead of text/plain to avoid a bug when
  1798. // reloading the raw text view (it reverts to type text/html)
  1799. var newDoc = document.open('text/html', 'replace');
  1800. newDoc.write('<pre>' + Helper.htmlEntities(paste) + '</pre>');
  1801. newDoc.close();
  1802. event.preventDefault();
  1803. }
  1804. /**
  1805. * saves the language in a cookie and reloads the page
  1806. *
  1807. * @name TopNav.setLanguage
  1808. * @function
  1809. * @param {Event} event
  1810. */
  1811. function setLanguage(event)
  1812. {
  1813. document.cookie = 'lang=' + $(event.target).data('lang');
  1814. UiHelper.reloadHome();
  1815. }
  1816. /**
  1817. * Shows all elements belonging to viwing an existing pastes
  1818. *
  1819. * @name TopNav.hideAllElem
  1820. * @function
  1821. */
  1822. me.showViewButtons = function()
  1823. {
  1824. if (viewButtonsDisplayed) {
  1825. console.log('showViewButtons: view buttons are already displayed');
  1826. return;
  1827. }
  1828. $cloneButton.removeClass('hidden');
  1829. $rawTextButton.removeClass('hidden');
  1830. viewButtonsDisplayed = true;
  1831. };
  1832. /**
  1833. * Hides all elements belonging to existing pastes
  1834. *
  1835. * @name TopNav.hideAllElem
  1836. * @function
  1837. */
  1838. me.hideViewButtons = function()
  1839. {
  1840. if (!viewButtonsDisplayed) {
  1841. console.log('hideViewButtons: view buttons are already hidden');
  1842. return;
  1843. }
  1844. $newButton.removeClass('hidden');
  1845. $cloneButton.addClass('hidden');
  1846. $rawTextButton.addClass('hidden');
  1847. viewButtonsDisplayed = false;
  1848. };
  1849. /**
  1850. * shows all elements needed when creating a new paste
  1851. *
  1852. * @name TopNav.setLanguage
  1853. * @function
  1854. */
  1855. me.showCreateButtons = function()
  1856. {
  1857. if (createButtonsDisplayed) {
  1858. console.log('showCreateButtons: create buttons are already displayed');
  1859. return;
  1860. }
  1861. $sendButton.removeClass('hidden');
  1862. $expiration.removeClass('hidden');
  1863. $formatter.removeClass('hidden');
  1864. $burnAfterReadingOption.removeClass('hidden');
  1865. $openDiscussionOption.removeClass('hidden');
  1866. $newButton.removeClass('hidden');
  1867. $password.removeClass('hidden');
  1868. $attach.removeClass('hidden');
  1869. // $clonedFile.removeClass('hidden'); // @TODO
  1870. createButtonsDisplayed = true;
  1871. };
  1872. /**
  1873. * shows all elements needed when creating a new paste
  1874. *
  1875. * @name TopNav.setLanguage
  1876. * @function
  1877. */
  1878. me.hideCreateButtons = function()
  1879. {
  1880. if (!createButtonsDisplayed) {
  1881. console.log('hideCreateButtons: create buttons are already hidden');
  1882. return;
  1883. }
  1884. $newButton.addClass('hidden');
  1885. $sendButton.addClass('hidden');
  1886. $expiration.addClass('hidden');
  1887. $formatter.addClass('hidden');
  1888. $burnAfterReadingOption.addClass('hidden');
  1889. $openDiscussionOption.addClass('hidden');
  1890. $password.addClass('hidden');
  1891. $attach.addClass('hidden');
  1892. // $clonedFile.addClass('hidden'); // @TODO
  1893. createButtonsDisplayed = false;
  1894. };
  1895. /**
  1896. * only shows the "new paste" button
  1897. *
  1898. * @name TopNav.setLanguage
  1899. * @function
  1900. */
  1901. me.showNewPasteButton = function()
  1902. {
  1903. $newButton.removeClass('hidden');
  1904. };
  1905. /**
  1906. * only hides the clone button
  1907. *
  1908. * @name TopNav.hideCloneButton
  1909. * @function
  1910. */
  1911. me.hideCloneButton = function()
  1912. {
  1913. $cloneButton.addClass('hidden');
  1914. };
  1915. /**
  1916. * only hides the raw text button
  1917. *
  1918. * @name TopNav.hideRawButton
  1919. * @function
  1920. */
  1921. me.hideRawButton = function()
  1922. {
  1923. $rawTextButton.addClass('hidden');
  1924. };
  1925. /**
  1926. * shows a loading message, optionally with a percentage
  1927. *
  1928. * @name TopNav.showLoading
  1929. * @function
  1930. * @param {string} message optional, default: 'Loading…'
  1931. * @param {int} percentage optional, default: null
  1932. */
  1933. me.showLoading = function(message, percentage)
  1934. {
  1935. // default message text
  1936. if (typeof message === 'undefined') {
  1937. message = I18n._('Loading…');
  1938. }
  1939. // currently percentage parameter is ignored
  1940. if (message !== null) {
  1941. $loadingIndicator.find(':last').text(message);
  1942. }
  1943. $loadingIndicator.removeClass('hidden');
  1944. };
  1945. /**
  1946. * hides the loading message
  1947. *
  1948. * @name TopNav.hideLoading
  1949. * @function
  1950. */
  1951. me.hideLoading = function()
  1952. {
  1953. $loadingIndicator.addClass('hidden');
  1954. };
  1955. /**
  1956. * collapses the navigation bar if nedded
  1957. *
  1958. * @name TopNav.collapseBar
  1959. * @function
  1960. */
  1961. me.collapseBar = function()
  1962. {
  1963. var $bar = $('.navbar-toggle');
  1964. // check if bar is expanded
  1965. if ($bar.hasClass('collapse in')) {
  1966. // if so, toggle it
  1967. $bar.click();
  1968. }
  1969. };
  1970. /**
  1971. * returns the currently set expiration time
  1972. *
  1973. * @name TopNav.getExpiration
  1974. * @function
  1975. * @return {int}
  1976. */
  1977. me.getExpiration = function()
  1978. {
  1979. return pasteExpiration;
  1980. };
  1981. /**
  1982. * returns the currently selected file(s)
  1983. *
  1984. * @name TopNav.getFileList
  1985. * @function
  1986. * @return {FileList|null}
  1987. */
  1988. me.getFileList = function()
  1989. {
  1990. var $file = $('#file');
  1991. // if no file given, return null
  1992. if (!$file.length || !$file[0].files.length) {
  1993. return null;
  1994. }
  1995. // @TODO is this really necessary
  1996. if (!($file[0].files && $file[0].files[0])) {
  1997. return null;
  1998. }
  1999. return $file[0].files;
  2000. };
  2001. /**
  2002. * returns the state of the burn after reading checkbox
  2003. *
  2004. * @name TopNav.getExpiration
  2005. * @function
  2006. * @return {bool}
  2007. */
  2008. me.getBurnAfterReading = function()
  2009. {
  2010. return $burnAfterReading.is(':checked');
  2011. };
  2012. /**
  2013. * returns the state of the discussion checkbox
  2014. *
  2015. * @name TopNav.getOpenDiscussion
  2016. * @function
  2017. * @return {bool}
  2018. */
  2019. me.getOpenDiscussion = function()
  2020. {
  2021. return $openDiscussion.is(':checked');
  2022. };
  2023. /**
  2024. * returns the entered password
  2025. *
  2026. * @name TopNav.getPassword
  2027. * @function
  2028. * @return {string}
  2029. */
  2030. me.getPassword = function()
  2031. {
  2032. return $passwordInput.val();
  2033. };
  2034. /**
  2035. * init navigation manager
  2036. *
  2037. * preloads jQuery elements
  2038. *
  2039. * @name TopNav.init
  2040. * @function
  2041. */
  2042. me.init = function()
  2043. {
  2044. $attach = $('#attach');
  2045. $burnAfterReading = $('#burnafterreading');
  2046. $burnAfterReadingOption = $('#burnafterreadingoption');
  2047. $cloneButton = $('#clonebutton');
  2048. $expiration = $('#expiration');
  2049. $fileRemoveButton = $('#fileremovebutton');
  2050. $formatter = $('#formatter');
  2051. $newButton = $('#newbutton');
  2052. $openDiscussionOption = $('#opendiscussionoption');
  2053. $openDiscussion = $('#opendiscussion');
  2054. $password = $('#password');
  2055. $passwordInput = $('#passwordinput');
  2056. $rawTextButton = $('#rawtextbutton');
  2057. $sendButton = $('#sendbutton');
  2058. $loadingIndicator = $('#loadingindicator');
  2059. // bootstrap template drop down
  2060. $('#language ul.dropdown-menu li a').click(me.setLanguage);
  2061. // page template drop down
  2062. $('#language select option').click(me.setLanguage);
  2063. // bind events
  2064. $burnAfterReading.change(changeBurnAfterReading);
  2065. $openDiscussionOption.change(changeOpenDiscussion);
  2066. $newButton.click(Controller.newPaste);
  2067. $sendButton.click(PasteEncrypter.submitPaste);
  2068. $cloneButton.click(Controller.clonePaste);
  2069. $rawTextButton.click(rawText);
  2070. $fileRemoveButton.click(me.removeAttachment);
  2071. // bootstrap template drop downs
  2072. $('ul.dropdown-menu li a', $('#expiration').parent()).click(updateExpiration);
  2073. $('ul.dropdown-menu li a', $('#formatter').parent()).click(updateFormat);
  2074. // initiate default state of checkboxes
  2075. changeBurnAfterReading();
  2076. changeOpenDiscussion();
  2077. // get default value from template or fall back to set value
  2078. pasteExpiration = Modal.getExpirationDefault() || pasteExpiration;
  2079. };
  2080. return me;
  2081. })(window, document);
  2082. /**
  2083. * Responsible for AJAX requests, transparently handles encryption…
  2084. *
  2085. * @class
  2086. */
  2087. var Uploader = (function () {
  2088. var me = {};
  2089. var successFunc = null,
  2090. failureFunc = null,
  2091. url,
  2092. data,
  2093. randomKey,
  2094. password;
  2095. /**
  2096. * public variable ('constant') for errors to prevent magic numbers
  2097. *
  2098. * @readonly
  2099. * @enum {Object}
  2100. */
  2101. me.error = {
  2102. okay: 0,
  2103. custom: 1,
  2104. unknown: 2,
  2105. serverError: 3
  2106. };
  2107. /**
  2108. * ajaxHeaders to send in AJAX requests
  2109. *
  2110. * @private
  2111. * @readonly
  2112. * @enum {Object}
  2113. */
  2114. var ajaxHeaders = {'X-Requested-With': 'JSONHttpRequest'};
  2115. /**
  2116. * called after successful upload
  2117. *
  2118. * @function
  2119. * @param {int} status
  2120. * @param {int} data - optional
  2121. */
  2122. function success(status, result)
  2123. {
  2124. // add useful data to result
  2125. result.encryptionKey = randomKey;
  2126. result.requestData = data;
  2127. if (successFunc !== null) {
  2128. successFunc(status, result);
  2129. }
  2130. }
  2131. /**
  2132. * called after a upload failure
  2133. *
  2134. * @name Uploader.submitPasteUpload
  2135. * @function
  2136. * @param {int} status - internal code
  2137. * @param {int} data - original error code
  2138. */
  2139. function fail(status, result)
  2140. {
  2141. if (failureFunc !== null) {
  2142. failureFunc(status, result);
  2143. }
  2144. }
  2145. /**
  2146. * actually uploads the data
  2147. *
  2148. * @name Uploader.run
  2149. * @function
  2150. */
  2151. me.run = function()
  2152. {
  2153. $.ajax({
  2154. type: 'POST',
  2155. url: url,
  2156. data: data,
  2157. dataType: 'json',
  2158. headers: ajaxHeaders,
  2159. success: function(result) {
  2160. if (result.status === 0) {
  2161. success(0, result);
  2162. } else if (result.status === 1) {
  2163. fail(1, result);
  2164. } else {
  2165. fail(2, result);
  2166. }
  2167. }
  2168. })
  2169. .fail(function(jqXHR, textStatus, errorThrown) {
  2170. console.error(textStatus, errorThrown);
  2171. fail(3, jqXHR);
  2172. });
  2173. };
  2174. /**
  2175. * set success function
  2176. *
  2177. * @name Uploader.setSuccess
  2178. * @function
  2179. * @param {function} func
  2180. */
  2181. me.setUrl = function(newUrl)
  2182. {
  2183. url = newUrl;
  2184. };
  2185. /**
  2186. * set success function
  2187. *
  2188. * @name Uploader.setSuccess
  2189. * @function
  2190. * @param {function} func
  2191. */
  2192. me.setSuccess = function(func)
  2193. {
  2194. successFunc = func;
  2195. };
  2196. /**
  2197. * set failure function
  2198. *
  2199. * @name Uploader.setSuccess
  2200. * @function
  2201. * @param {function} func
  2202. */
  2203. me.setFailure = function(func)
  2204. {
  2205. failureFunc = func;
  2206. };
  2207. /**
  2208. * prepares a new upload
  2209. *
  2210. * @name Uploader.prepare
  2211. * @function
  2212. * @param {string} newPassword
  2213. * @return {object}
  2214. */
  2215. me.prepare = function(newPassword)
  2216. {
  2217. // set password
  2218. password = newPassword;
  2219. // entropy should already be checked!
  2220. // generate a new random key
  2221. randomKey = CryptTool.getSymmetricKey();
  2222. // reset data
  2223. successFunc = null;
  2224. failureFunc = null;
  2225. url = Helper.baseUri()
  2226. data = {};
  2227. };
  2228. /**
  2229. * encrypts and sets the data
  2230. *
  2231. * @name Uploader.setData
  2232. * @function
  2233. * @param {string} index
  2234. * @param {mixed} element
  2235. */
  2236. me.setData = function(index, element)
  2237. {
  2238. data[index] = CryptTool.cipher(randomKey, password, element);
  2239. };
  2240. /**
  2241. * set the additional metadata to send unencrypted
  2242. *
  2243. * @name Uploader.setUnencryptedData
  2244. * @function
  2245. * @param {string} index
  2246. * @param {mixed} element
  2247. */
  2248. me.setUnencryptedData = function(index, element)
  2249. {
  2250. data[index] = element;
  2251. };
  2252. /**
  2253. * set the additional metadata to send unencrypted passed at once
  2254. *
  2255. * @name Uploader.setUnencryptedData
  2256. * @function
  2257. * @param {object} newData
  2258. */
  2259. me.setUnencryptedBulkData = function(newData)
  2260. {
  2261. $.extend(data, newData);
  2262. };
  2263. /**
  2264. * init Uploader
  2265. *
  2266. * @name Uploader.init
  2267. * @function
  2268. */
  2269. me.init = function()
  2270. {
  2271. // nothing yet
  2272. };
  2273. return me;
  2274. })();
  2275. /**
  2276. * (controller) Responsible for encrypting paste and sending it to server.
  2277. *
  2278. * @name state
  2279. * @class
  2280. */
  2281. var PasteEncrypter = (function () {
  2282. var me = {};
  2283. var requirementsChecked = false;
  2284. /**
  2285. * checks whether there is a suitable amount of entrophy
  2286. *
  2287. * @private
  2288. * @function
  2289. * @param {function} retryCallback - the callback to execute to retry the upload
  2290. * @return {bool}
  2291. */
  2292. function checkRequirements(retryCallback) {
  2293. // skip double requirement checks
  2294. if (requirementsChecked === true) {
  2295. return false;
  2296. }
  2297. if (!CryptTool.isEntropyReady()) {
  2298. // display a message and wait
  2299. Alert.showStatus(I18n._('Please move your mouse for more entropy...'));
  2300. CryptTool.addEntropySeedListener(retryCallback);
  2301. return false;
  2302. }
  2303. requirementsChecked = true;
  2304. return true;
  2305. }
  2306. /**
  2307. * called after successful upload
  2308. *
  2309. * @private
  2310. * @function
  2311. * @param {int} status
  2312. * @param {int} data
  2313. */
  2314. function showCreatedPaste(status, data) {
  2315. TopNav.hideLoading();
  2316. var url = Helper.baseUri() + '?' + data.id + '#' + data.encryptionKey,
  2317. deleteUrl = Helper.baseUri() + '?pasteid=' + data.id + '&deletetoken=' + data.deletetoken;
  2318. Alert.hideMessages();
  2319. // show notification
  2320. PasteStatus.createPasteNotification(url, deleteUrl)
  2321. // show new URL in browser bar
  2322. history.pushState({type: 'newpaste'}, document.title, url);
  2323. TopNav.showViewButtons();
  2324. TopNav.hideRawButton();
  2325. Editor.hide();
  2326. // parse and show text
  2327. // (preparation already done in me.submitPaste())
  2328. PasteViewer.run();
  2329. }
  2330. /**
  2331. * adds attachments to the Uploader
  2332. *
  2333. * @private
  2334. * @function
  2335. * @param {File|null|undefined} file - optional, falls back to cloned attachment
  2336. * @param {function} callback - excuted when action is successful
  2337. */
  2338. function encryptAttachments(file, callback) {
  2339. if (typeof file !== 'undefined' && file !== null) {
  2340. // check file reader requirements for upload
  2341. if (typeof FileReader === 'undefined') {
  2342. Alert.showError(I18n._('Your browser does not support uploading encrypted files. Please use a newer browser.'));
  2343. // cancels process as it does not execute callback
  2344. return;
  2345. }
  2346. var reader = new FileReader();
  2347. // closure to capture the file information
  2348. reader.onload = function(event) {
  2349. Uploader.setData('attachment', event.target.result);
  2350. Uploader.setData('attachmentname', file.name);
  2351. // run callback
  2352. callback();
  2353. };
  2354. // actually read first file
  2355. reader.readAsDataURL(file);
  2356. } else if (AttachmentViewer.hasAttachment()) {
  2357. // fall back to cloned part
  2358. var attachment = AttachmentViewer.getAttachment();
  2359. Uploader.setData('attachment', attachment[0]);
  2360. Uploader.setUnencryptedData('attachmentname', attachment[1]); // @TODO does not encrypt file name??!
  2361. callback();
  2362. } else {
  2363. // if there are no attachments, this is of course still successful
  2364. callback();
  2365. }
  2366. }
  2367. /**
  2368. * send a reply in a discussion
  2369. *
  2370. * @name PasteEncrypter.sendComment
  2371. * @function
  2372. * @param {Event} event
  2373. * @TODO WIP
  2374. */
  2375. me.sendComment = function(event)
  2376. {
  2377. event.preventDefault();
  2378. $errorMessage.addClass('hidden');
  2379. // do not send if no data
  2380. var replyMessage = $('#replymessage');
  2381. if (replyMessage.val().length === 0)
  2382. {
  2383. return;
  2384. }
  2385. me.showStatus(I18n._('Sending comment...'), true);
  2386. var parentid = event.data.parentid,
  2387. key = Modal.getPasteKey(),
  2388. cipherdata = CryptTool.cipher(key, $passwordInput.val(), replyMessage.val()),
  2389. ciphernickname = '',
  2390. nick = $('#nickname').val();
  2391. if (nick.length > 0)
  2392. {
  2393. ciphernickname = CryptTool.cipher(key, $passwordInput.val(), nick);
  2394. }
  2395. var dataToSend = {
  2396. data: cipherdata,
  2397. parentid: parentid,
  2398. pasteid: Modal.getPasteId(),
  2399. nickname: ciphernickname
  2400. };
  2401. $.ajax({
  2402. type: 'POST',
  2403. url: Helper.baseUri(),
  2404. data: dataToSend,
  2405. dataType: 'json',
  2406. headers: ajaxHeaders,
  2407. success: function(data) {
  2408. if (data.status === 0)
  2409. {
  2410. status.showStatus(I18n._('Comment posted.'));
  2411. $.ajax({
  2412. type: 'GET',
  2413. url: Helper.baseUri() + '?' + Modal.getPasteId(),
  2414. dataType: 'json',
  2415. headers: ajaxHeaders,
  2416. success: function(data) {
  2417. if (data.status === 0)
  2418. {
  2419. me.displayMessages(data);
  2420. }
  2421. else if (data.status === 1)
  2422. {
  2423. Alert.showError(I18n._('Could not refresh display: %s', data.message));
  2424. }
  2425. else
  2426. {
  2427. Alert.showError(I18n._('Could not refresh display: %s', I18n._('unknown status')));
  2428. }
  2429. }
  2430. })
  2431. .fail(function() {
  2432. Alert.showError(I18n._('Could not refresh display: %s', I18n._('server error or not responding')));
  2433. });
  2434. }
  2435. else if (data.status === 1)
  2436. {
  2437. Alert.showError(I18n._('Could not post comment: %s', data.message));
  2438. }
  2439. else
  2440. {
  2441. Alert.showError(I18n._('Could not post comment: %s', I18n._('unknown status')));
  2442. }
  2443. }
  2444. })
  2445. .fail(function() {
  2446. Alert.showError(I18n._('Could not post comment: %s', I18n._('server error or not responding')));
  2447. });
  2448. };
  2449. /**
  2450. * sends a new paste to server
  2451. *
  2452. * @name PasteEncrypter.submitPaste
  2453. * @function
  2454. */
  2455. me.submitPaste = function()
  2456. {
  2457. // UI loading state
  2458. TopNav.hideCreateButtons();
  2459. TopNav.showLoading(I18n._('Sending paste...'), 0);
  2460. TopNav.collapseBar();
  2461. // get data
  2462. var plainText = Editor.getText(),
  2463. format = PasteViewer.getFormat(),
  2464. files = TopNav.getFileList();
  2465. // do not send if there is no data
  2466. if (plainText.length === 0 && files === null) {
  2467. // revert loading status…
  2468. TopNav.hideLoading();
  2469. TopNav.showCreateButtons();
  2470. return;
  2471. }
  2472. TopNav.showLoading(I18n._('Sending paste...'), 10);
  2473. // check entropy
  2474. if (!checkRequirements(function () {
  2475. me.submitPaste();
  2476. })) {
  2477. return; // to prevent multiple executions
  2478. }
  2479. // prepare Uploader
  2480. Uploader.prepare(TopNav.getPassword());
  2481. // set success/fail functions
  2482. Uploader.setSuccess(showCreatedPaste);
  2483. Uploader.setFailure(function (status, data) {
  2484. // revert loading status…
  2485. TopNav.hideLoading();
  2486. TopNav.showCreateButtons();
  2487. // show error message
  2488. switch (status) {
  2489. case Uploader.error['custom']:
  2490. Alert.showError(I18n._('Could not create paste: %s', data.message));
  2491. break;
  2492. case Uploader.error['unknown']:
  2493. Alert.showError(I18n._('Could not create paste: %s', I18n._('unknown status')));
  2494. break;
  2495. case Uploader.error['serverError']:
  2496. Alert.showError(I18n._('Could not create paste: %s', I18n._('server error or not responding')));
  2497. break;
  2498. default:
  2499. Alert.showError(I18n._('Could not create paste: %s', I18n._('unknown error')));
  2500. break;
  2501. }
  2502. });
  2503. // fill it with unencrypted submitted options
  2504. Uploader.setUnencryptedBulkData({
  2505. expire: TopNav.getExpiration(),
  2506. formatter: format,
  2507. burnafterreading: TopNav.getBurnAfterReading() ? 1 : 0,
  2508. opendiscussion: TopNav.getOpenDiscussion() ? 1 : 0
  2509. });
  2510. // prepare PasteViewer for later preview
  2511. PasteViewer.setText(plainText);
  2512. PasteViewer.setFormat(format);
  2513. // encrypt cipher data
  2514. Uploader.setData('data', plainText);
  2515. // encrypt attachments
  2516. encryptAttachments(
  2517. files === null ? null : files[0],
  2518. function () {
  2519. // send data
  2520. Uploader.run();
  2521. }
  2522. );
  2523. };
  2524. /**
  2525. * initialize
  2526. *
  2527. * @name PasteEncrypter.init
  2528. * @function
  2529. */
  2530. me.init = function()
  2531. {
  2532. // nothing yet
  2533. };
  2534. return me;
  2535. })();
  2536. /**
  2537. * (controller) Responsible for decrypting cipherdata and passing data to view.
  2538. *
  2539. * @name state
  2540. * @class
  2541. */
  2542. var PasteDecrypter = (function () {
  2543. var me = {};
  2544. /**
  2545. * decrypt the actual paste text
  2546. *
  2547. * @private
  2548. * @function
  2549. * @param {object} paste - paste data in object form
  2550. * @param {string} key
  2551. * @param {string} password
  2552. * @return {bool} - whether action was successful
  2553. */
  2554. function decryptPaste(paste, key, password)
  2555. {
  2556. // try decryption without password
  2557. var plaintext = CryptTool.decipher(key, password, paste.data);
  2558. // if it fails, request password
  2559. if (plaintext.length === 0 && password.length === 0) {
  2560. // get password
  2561. password = Prompt.getPassword();
  2562. // if password is there, re-try
  2563. if (password.length !== 0) {
  2564. // recursive
  2565. // note: an infinite loop is prevented as the previous if
  2566. // clause checks whether a password is already set and ignores
  2567. // error with password being passed
  2568. return decryptPaste(paste, key, password);
  2569. }
  2570. // trigger password request
  2571. Prompt.requestPassword();
  2572. // the callback (via setPasswordCallback()) should have been set
  2573. // by parent function
  2574. return false;
  2575. }
  2576. // if all tries failed, we can only throw an error
  2577. if (plaintext.length === 0) {
  2578. throw 'failed to decipher message';
  2579. }
  2580. // on success show paste
  2581. PasteViewer.setFormat(paste.meta.formatter);
  2582. PasteViewer.setText(plaintext);
  2583. // trigger to show the text (attachment loaded afterwards)
  2584. PasteViewer.run();
  2585. return true;
  2586. }
  2587. /**
  2588. * decrypts any attachment
  2589. *
  2590. * @private
  2591. * @function
  2592. * @param {object} paste - paste data in object form
  2593. * @param {string} key
  2594. * @param {string} password
  2595. * @return {bool} - whether action was successful
  2596. */
  2597. function decryptAttachment(paste, key, password)
  2598. {
  2599. // decrypt attachment
  2600. var attachment = CryptTool.decipher(key, password, paste.attachment);
  2601. if (attachment.length === 0) {
  2602. throw 'failed to decipher attachment';
  2603. }
  2604. // decrypt attachment name
  2605. var attachmentName;
  2606. if (paste.attachmentname) {
  2607. attachmentName = attachmentName = CryptTool.decipher(key, password, paste.attachmentname);
  2608. if (attachmentName.length === 0) {
  2609. // @TODO considering the buggy cloning (?, see other todo comment) this might affect previous pastes
  2610. throw 'failed to decipher attachment name';
  2611. }
  2612. }
  2613. AttachmentViewer.setAttachment(attachment, attachmentName);
  2614. AttachmentViewer.showAttachment();
  2615. }
  2616. /**
  2617. * show decrypted text in the display area, including discussion (if open)
  2618. *
  2619. * @name PasteDecrypter.run
  2620. * @function
  2621. * @param {Object} [paste] - (optional) object including comments to display (items = array with keys ('data','meta'))
  2622. */
  2623. me.run = function(paste)
  2624. {
  2625. TopNav.showLoading('Decrypting paste…');
  2626. if (typeof paste === 'undefined') {
  2627. paste = $.parseJSON(Modal.getCipherData());
  2628. }
  2629. var key = Modal.getPasteKey(),
  2630. password = Prompt.getPassword();
  2631. if (PasteViewer.isPrettyPrinted()) {
  2632. console.error('Too pretty! (don\'t know why this check)'); //@TODO
  2633. return;
  2634. }
  2635. // try to decrypt the paste
  2636. try {
  2637. Prompt.setPasswordCallback(function () {
  2638. me.run(paste);
  2639. });
  2640. // try to decrypt paste and if it fails (because the password is
  2641. // missing) return to let JS continue and wait for user
  2642. if (!decryptPaste(paste, key, password)) {
  2643. return;
  2644. }
  2645. // decrypt attachments
  2646. if (paste.attachment) {
  2647. decryptAttachment(paste, key, password);
  2648. }
  2649. } catch(err) {
  2650. TopNav.hideLoading();
  2651. // log and show error
  2652. console.error(err);
  2653. Alert.showError(I18n._('Could not decrypt data (Wrong key?)')); // @TODO error is not translated
  2654. // still go on to potentially show potentially partially decrypted data
  2655. }
  2656. // shows the remaining time (until) deletion
  2657. PasteStatus.showRemainingTime(paste.meta);
  2658. // if the discussion is opened on this paste, display it
  2659. // @TODO BELOW
  2660. if (paste.meta.opendiscussion) {
  2661. $comments.html('');
  2662. var $divComment;
  2663. // iterate over comments
  2664. for (var i = 0; i < paste.comments.length; ++i)
  2665. {
  2666. var $place = $comments,
  2667. comment = paste.comments[i],
  2668. commentText = CryptTool.decipher(key, password, comment.data),
  2669. $parentComment = $('#comment_' + comment.parentid);
  2670. $divComment = $('<article><div class="comment" id="comment_' + comment.id
  2671. + '"><div class="commentmeta"><span class="nickname"></span>'
  2672. + '<span class="commentdate"></span></div>'
  2673. + '<div class="commentdata"></div>'
  2674. + '<button class="btn btn-default btn-sm">'
  2675. + I18n._('Reply') + '</button></div></article>');
  2676. var $divCommentData = $divComment.find('div.commentdata');
  2677. // if parent comment exists
  2678. if ($parentComment.length)
  2679. {
  2680. // shift comment to the right
  2681. $place = $parentComment;
  2682. }
  2683. $divComment.find('button').click({commentid: comment.id}, me.openReply);
  2684. Helper.setElementText($divCommentData, commentText);
  2685. Helper.urls2links($divCommentData);
  2686. // try to get optional nickname
  2687. var nick = CryptTool.decipher(key, password, comment.meta.nickname);
  2688. if (nick.length > 0)
  2689. {
  2690. $divComment.find('span.nickname').text(nick);
  2691. }
  2692. else
  2693. {
  2694. divComment.find('span.nickname').html('<i>' + I18n._('Anonymous') + '</i>');
  2695. }
  2696. $divComment.find('span.commentdate')
  2697. .text(' (' + (new Date(comment.meta.postdate * 1000).toLocaleString()) + ')')
  2698. .attr('title', 'CommentID: ' + comment.id);
  2699. // if an avatar is available, display it
  2700. if (comment.meta.vizhash)
  2701. {
  2702. $divComment.find('span.nickname')
  2703. .before(
  2704. '<img src="' + comment.meta.vizhash + '" class="vizhash" title="' +
  2705. I18n._('Anonymous avatar (Vizhash of the IP address)') + '" /> '
  2706. );
  2707. }
  2708. $place.append($divComment);
  2709. }
  2710. // add 'add new comment' area
  2711. $divComment = $(
  2712. '<div class="comment"><button class="btn btn-default btn-sm">' +
  2713. I18n._('Add comment') + '</button></div>'
  2714. );
  2715. $divComment.find('button').click({commentid: Modal.getPasteId()}, me.openReply);
  2716. $comments.append($divComment);
  2717. $discussion.removeClass('hidden');
  2718. }
  2719. TopNav.hideLoading();
  2720. TopNav.showViewButtons();
  2721. };
  2722. /**
  2723. * initialize
  2724. *
  2725. * @name PasteDecrypter.init
  2726. * @function
  2727. */
  2728. me.init = function()
  2729. {
  2730. // nothing yet
  2731. };
  2732. return me;
  2733. })();
  2734. /**
  2735. * (controller) main PrivateBin logic
  2736. *
  2737. * @param {object} window
  2738. * @param {object} document
  2739. * @class
  2740. */
  2741. var Controller = (function (window, document) {
  2742. var me = {};
  2743. /**
  2744. * creates a new paste
  2745. *
  2746. * @name Controller.newPaste
  2747. * @function
  2748. */
  2749. me.newPaste = function()
  2750. {
  2751. TopNav.hideViewButtons();
  2752. TopNav.showCreateButtons();
  2753. PasteViewer.hide();
  2754. Editor.resetInput();
  2755. Editor.show();
  2756. Editor.focusInput();
  2757. Alert.hideMessages();
  2758. };
  2759. /**
  2760. * clone the current paste
  2761. *
  2762. * @name Controller.clonePaste
  2763. * @function
  2764. * @param {Event} event
  2765. */
  2766. me.clonePaste = function(event)
  2767. {
  2768. me.stateNewPaste();
  2769. // erase the id and the key in url
  2770. history.replaceState(null, document.title, Helper.baseUri());
  2771. Alert.hideMessages();
  2772. if ($attachmentLink.attr('href'))
  2773. {
  2774. $clonedFile.removeClass('hidden');
  2775. $fileWrap.addClass('hidden');
  2776. }
  2777. $message.val(
  2778. PasteViewer.getFormat() === 'markdown' ?
  2779. $prettyPrint.val() : $plainText.val()
  2780. );
  2781. TopNav.collapseBar();
  2782. };
  2783. /**
  2784. * removes a saved paste
  2785. *
  2786. * @name Controller.removePaste
  2787. * @function
  2788. * @param {string} pasteId
  2789. * @param {string} deleteToken
  2790. */
  2791. me.removePaste = function(pasteId, deleteToken) {
  2792. // unfortunately many web servers don't support DELETE (and PUT) out of the box
  2793. // so we use a POST request
  2794. Uploader.prepare();
  2795. Uploader.setUrl(Helper.baseUri() + '?' + pasteId);
  2796. Uploader.setUnencryptedData('deletetoken', deleteToken);
  2797. Uploader.setFailure(function () {
  2798. Controller.showError(I18n._('Could not delete the paste, it was not stored in burn after reading mode.'));
  2799. })
  2800. Uploader.run();
  2801. };
  2802. /**
  2803. * application start
  2804. *
  2805. * @name Controller.init
  2806. * @function
  2807. */
  2808. me.init = function()
  2809. {
  2810. // first load translations
  2811. I18n.loadTranslations();
  2812. // initialize other modules/"classes"
  2813. Alert.init();
  2814. Uploader.init();
  2815. Modal.init();
  2816. CryptTool.init();
  2817. UiHelper.init();
  2818. TopNav.init();
  2819. Editor.init();
  2820. PasteStatus.init();
  2821. PasteViewer.init();
  2822. AttachmentViewer.init();
  2823. DiscussionViewer.init();
  2824. PasteEncrypter.init();
  2825. PasteDecrypter.init();
  2826. Prompt.init();
  2827. // display an existing paste
  2828. if (Modal.hasCipherData()) {
  2829. // missing decryption key in URL?
  2830. if (window.location.hash.length === 0) {
  2831. Alert.showError(I18n._('Cannot decrypt paste: Decryption key missing in URL (Did you use a redirector or an URL shortener which strips part of the URL?)'));
  2832. return;
  2833. }
  2834. // show proper elements on screen
  2835. PasteDecrypter.run();
  2836. return;
  2837. }
  2838. // otherwise create a new paste
  2839. me.newPaste();
  2840. };
  2841. return me;
  2842. })(window, document);
  2843. return {
  2844. Helper: Helper,
  2845. I18n: I18n,
  2846. CryptTool: CryptTool,
  2847. TopNav: TopNav,
  2848. Alert: Alert,
  2849. Uploader: Uploader,
  2850. Controller: Controller
  2851. };
  2852. }(jQuery, sjcl, Base64, RawDeflate);