HTML.pm 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924
  1. package Trog::Routes::HTML;
  2. use v5.36;
  3. use re '/aa';
  4. no warnings qw{once};
  5. use POSIX qw{strftime};
  6. use Errno qw{ENOENT};
  7. use File::Touch();
  8. use File::Basename qw{basename};
  9. use List::Util();
  10. use List::MoreUtils();
  11. use Ref::Util();
  12. use Capture::Tiny qw{capture};
  13. use HTML::SocialMeta;
  14. use Clone qw{clone};
  15. use Encode qw{encode_utf8};
  16. use Digest::MD5 qw{md5_hex};
  17. use Digest::SHA qw{sha256_hex};
  18. use JSON::MaybeXS();
  19. use IO::Compress::Gzip;
  20. use Path::Tiny();
  21. use File::Basename qw{dirname};
  22. use URI();
  23. use URI::Escape();
  24. use FindBin::libs;
  25. use Trog::Log qw{:all};
  26. use Trog::Utils;
  27. use Trog::Config;
  28. use Trog::Auth;
  29. use Trog::Data;
  30. use Trog::FileHandler;
  31. use Trog::Themes;
  32. use Trog::Renderer;
  33. use Trog::Email;
  34. use Trog::DataSource;
  35. use CGI::Cookie ();
  36. our $landing_page = 'default.tx';
  37. # Note to maintainers: never ever remove backends from this list.
  38. # the auth => 1 is a crucial protection.
  39. our %routes = (
  40. default => {
  41. callback => \&Trog::Routes::HTML::setup,
  42. nomap => 1,
  43. },
  44. '/index' => {
  45. method => 'GET',
  46. callback => \&Trog::Routes::HTML::index,
  47. },
  48. # This should only be enabled to debug
  49. # '/setup' => {
  50. # method => 'GET',
  51. # callback => \&Trog::Routes::HTML::setup,
  52. # },
  53. '/login' => {
  54. method => 'GET',
  55. callback => \&Trog::Routes::HTML::login,
  56. noindex => 1,
  57. },
  58. '/logout' => {
  59. method => 'GET',
  60. callback => \&Trog::Routes::HTML::logout,
  61. noindex => 1,
  62. nocache => 1,
  63. },
  64. '/auth' => {
  65. method => 'POST',
  66. callback => \&Trog::Routes::HTML::login,
  67. nocache => 1,
  68. noindex => 1,
  69. },
  70. '/totp' => {
  71. method => 'GET',
  72. auth => 1,
  73. callback => \&Trog::Routes::HTML::totp,
  74. },
  75. '/post/save' => {
  76. method => 'POST',
  77. auth => 1,
  78. callback => \&Trog::Routes::HTML::post_save,
  79. },
  80. '/post/delete' => {
  81. method => 'POST',
  82. auth => 1,
  83. callback => \&Trog::Routes::HTML::post_delete,
  84. },
  85. '/config/save' => {
  86. method => 'POST',
  87. auth => 1,
  88. callback => \&Trog::Routes::HTML::config_save,
  89. },
  90. '/themeclone' => {
  91. method => 'POST',
  92. auth => 1,
  93. callback => \&Trog::Routes::HTML::themeclone,
  94. },
  95. '/profile' => {
  96. method => 'POST',
  97. auth => 1,
  98. callback => \&Trog::Routes::HTML::profile,
  99. },
  100. '/manual' => {
  101. method => 'GET',
  102. auth => 1,
  103. callback => \&Trog::Routes::HTML::manual,
  104. },
  105. '/lib/(.*)' => {
  106. method => 'GET',
  107. auth => 1,
  108. captures => ['module'],
  109. callback => \&Trog::Routes::HTML::manual,
  110. },
  111. '/password_reset' => {
  112. method => 'GET',
  113. callback => \&Trog::Routes::HTML::resetpass,
  114. noindex => 1,
  115. },
  116. # When we are logged in, we get a diff ver of the page.
  117. '/secure/password_reset' => {
  118. method => 'GET',
  119. callback => \&Trog::Routes::HTML::resetpass,
  120. auth => 1,
  121. },
  122. '/request_password_reset' => {
  123. method => 'POST',
  124. callback => \&Trog::Routes::HTML::do_resetpass,
  125. noindex => 1,
  126. nocache => 1,
  127. },
  128. '/request_totp_clear' => {
  129. method => 'POST',
  130. callback => \&Trog::Routes::HTML::do_totp_clear,
  131. noindex => 1,
  132. nocache => 1,
  133. },
  134. '/processed' => {
  135. method => 'GET',
  136. callback => \&Trog::Routes::HTML::processed,
  137. noindex => 1,
  138. },
  139. '/metrics' => {
  140. method => 'GET',
  141. auth => 1,
  142. callback => \&Trog::Routes::HTML::metrics,
  143. },
  144. '/admin/sessions' => {
  145. method => 'GET',
  146. auth => 1,
  147. callback => \&Trog::Routes::HTML::sessions,
  148. noindex => 1,
  149. nocache => 1,
  150. },
  151. '/guest/screenshot/(.*)/(.*)' => {
  152. method => 'GET',
  153. auth => 1,
  154. callback => \&Trog::Routes::HTML::guest_screenshot,
  155. # 'guest', not 'domain': the router sets $query->{domain} to the
  156. # request's own host *after* it applies captures, so a capture by that
  157. # name never survives to the callback.
  158. captures => [qw{hypervisor guest}],
  159. noindex => 1,
  160. nocache => 1,
  161. },
  162. '/guest/act' => {
  163. method => 'POST',
  164. auth => 1,
  165. callback => \&Trog::Routes::HTML::guest_act,
  166. noindex => 1,
  167. nocache => 1,
  168. },
  169. '/admin/wyzzerdd' => {
  170. method => 'GET',
  171. auth => 1,
  172. callback => \&Trog::Routes::HTML::post_wizard,
  173. noindex => 1,
  174. nocache => 1,
  175. },
  176. '/admin/wyzzerdd/save' => {
  177. method => 'POST',
  178. auth => 1,
  179. callback => \&Trog::Routes::HTML::post_wizard_save,
  180. noindex => 1,
  181. nocache => 1,
  182. },
  183. #TODO transform into posts?
  184. '/sitemap',
  185. => {
  186. method => 'GET',
  187. callback => \&Trog::Routes::HTML::sitemap,
  188. },
  189. '/sitemap_index.xml',
  190. => {
  191. method => 'GET',
  192. callback => \&Trog::Routes::HTML::sitemap,
  193. data => { xml => 1 },
  194. },
  195. '/sitemap_index.xml.gz',
  196. => {
  197. method => 'GET',
  198. callback => \&Trog::Routes::HTML::sitemap,
  199. data => { xml => 1, compressed => 1 },
  200. },
  201. '/sitemap/static.xml' => {
  202. method => 'GET',
  203. callback => \&Trog::Routes::HTML::sitemap,
  204. data => { xml => 1, map => 'static' },
  205. },
  206. '/sitemap/static.xml.gz' => {
  207. method => 'GET',
  208. callback => \&Trog::Routes::HTML::sitemap,
  209. data => { xml => 1, compressed => 1, map => 'static' },
  210. },
  211. '/sitemap/(.*).xml' => {
  212. method => 'GET',
  213. callback => \&Trog::Routes::HTML::sitemap,
  214. data => { xml => 1 },
  215. captures => ['map'],
  216. },
  217. '/sitemap/(.*).xml.gz' => {
  218. method => 'GET',
  219. callback => \&Trog::Routes::HTML::sitemap,
  220. data => { xml => 1, compressed => 1 },
  221. captures => ['map'],
  222. },
  223. '/humans.txt' => {
  224. method => 'GET',
  225. callback => \&Trog::Routes::HTML::posts,
  226. data => { tag => ['about'] },
  227. },
  228. '/styles/avatars.css' => {
  229. method => 'GET',
  230. callback => \&Trog::Routes::HTML::avatars,
  231. data => { tag => ['about'] },
  232. },
  233. '/avatar/(.*)' => {
  234. method => 'GET',
  235. callback => \&Trog::Routes::HTML::libravatar,
  236. captures => ['avatar_hash'],
  237. noindex => 1,
  238. nocache => 1,
  239. },
  240. '/favicon.ico' => {
  241. method => 'GET',
  242. callback => \&Trog::Routes::HTML::icon,
  243. },
  244. '/totp_qr/(.*)' => {
  245. auth => 1,
  246. method => 'GET',
  247. callback => \&Trog::Routes::HTML::totp_qr,
  248. },
  249. '/styles/rss-style.xsl' => {
  250. method => 'GET',
  251. callback => \&Trog::Routes::HTML::rss_style,
  252. },
  253. );
  254. # Grab theme routes
  255. my $themed = 0;
  256. my $theme_dir = Trog::Themes::get_dir();
  257. if ($theme_dir) {
  258. my $theme_mod = "$theme_dir/routes.pm";
  259. if ( -f $theme_mod ) { ## no critic (ProhibitFiletest_f) -- theme routes, else the default theme
  260. use lib '.';
  261. require $theme_mod;
  262. @routes{ keys(%Theme::routes) } = values(%Theme::routes);
  263. $themed = 1;
  264. }
  265. else {
  266. # Use the special "default" theme
  267. require Theme;
  268. }
  269. }
  270. =head1 PRIMARY ROUTE
  271. =head2 _feedback_redirect($query, $to, $failure, $message)
  272. Redirect to $to, carrying the outcome of a save so the destination page can put
  273. it in the jsalert banner.
  274. We redirect rather than rendering in place so that a save stays a
  275. POST-redirect-GET -- refreshing the page the user lands on must not re-submit
  276. the post. That means the outcome has to travel in the URL, as there is nowhere
  277. else to put it.
  278. =cut
  279. sub _feedback_redirect ( $query, $to, $failure, $message ) {
  280. my $cookie = _feedback_cookie( $failure, $message );
  281. # Appended to what see_also() built rather than replacing it, so this still
  282. # logs and still redirects the way every other redirect in here does.
  283. my $response = $query->{tpsgi}->see_also($to);
  284. push( @{ $response->[1] }, 'Set-Cookie' => $cookie );
  285. return $response;
  286. }
  287. =head2 _feedback_cookie($failure, $message) = STRING
  288. The Set-Cookie which carries a save's outcome to the page it redirects to.
  289. A cookie because a redirect's own headers are gone by the time the destination
  290. renders, and a cookie is the one header that survives the hop. It used to be a
  291. query parameter, which meant a save's message was in the URL: a validation error
  292. carrying a Carp::Always stack trace built a URL of several thousand characters,
  293. and tPSGI answered the redirect it had just issued with a 419. The user was
  294. shown neither the page nor the error.
  295. Trimmed to one line, because the banner puts it in a JS string literal, and
  296. capped at $feedback_max characters, because a cookie has a size limit of its own
  297. and nothing a person needs to read is longer than that. The whole of it is in
  298. the log either way.
  299. =cut
  300. our $feedback_max = 512;
  301. sub _feedback_cookie ( $failure, $message ) {
  302. $message //= '';
  303. $message =~ s/\s*\n+\s*/; /g;
  304. $message =~ s/;\s*$//;
  305. $message = substr( $message, 0, $feedback_max - 3 ) . '...' if length($message) > $feedback_max;
  306. my $value = ( $failure ? 1 : 0 ) . ':' . URI::Escape::uri_escape($message);
  307. # HttpOnly because the banner is rendered server side and no script has any
  308. # business reading it; Max-Age because a message nobody collected should not
  309. # follow them around; Path=/ because the destination is wherever they were.
  310. return "tcmsfeedback=$value; Path=/; HttpOnly; SameSite=Lax; Max-Age=30";
  311. }
  312. =head2 _absorb_feedback($query)
  313. Turn the parameters _feedback_redirect() put in the URL back into the failure
  314. and message the jsalert banner reads.
  315. Done in index() rather than in each route, so that a save can hand feedback to
  316. any destination that renders a page, whichever route ends up serving it.
  317. =cut
  318. sub _absorb_feedback ($query) {
  319. my ( $failure, $message ) = _feedback_from_cookie( $query->{cookies} );
  320. # The query parameters are still read, for a redirect issued by a worker
  321. # which had not been restarted yet, and for anyone who has one bookmarked.
  322. if ( !defined $failure ) {
  323. my ( $saved, $failed ) = ( $query->{saved}, $query->{savefailed} );
  324. return unless defined $saved || defined $failed;
  325. $failure = defined $failed ? 1 : 0;
  326. $message = defined $failed ? $failed : $saved;
  327. }
  328. $query->{failure} = $failure;
  329. $query->{message} = $message || ( $failure ? 'Save failed.' : 'Saved.' );
  330. # A page showing a banner is not a page to cache: the banner is for the one
  331. # person who just saved something, and a static of it would be served to
  332. # everybody. The query parameter form was accidentally safe here, since a
  333. # query string skips the render cache on its own; a cookie is not.
  334. $query->{nocache} = 1;
  335. # Read once. Without this the banner reappears on every page until the
  336. # cookie expires.
  337. $query->{feedback_seen} = 1;
  338. # Nothing further to navigate to -- we are already there. Without this the
  339. # banner's success branch would bounce us onwards.
  340. delete $query->{to};
  341. return;
  342. }
  343. =head2 _feedback_from_cookie($cookies) = ($failure, $message)
  344. Pull a save's outcome back out of the cookie _feedback_redirect set.
  345. Returns nothing when there isn't one, so that the caller can fall back to the
  346. query parameters this replaced.
  347. =cut
  348. sub _feedback_from_cookie ($cookies) {
  349. return () unless $cookies;
  350. local $@;
  351. my $jar = eval { CGI::Cookie->parse($cookies) };
  352. return () unless ref $jar eq 'HASH' && $jar->{tcmsfeedback};
  353. my $value = $jar->{tcmsfeedback}->value // '';
  354. my ( $failure, $message ) = $value =~ m/^([01]):(.*)$/s;
  355. return () unless defined $failure;
  356. return ( int($failure), URI::Escape::uri_unescape($message) );
  357. }
  358. =head2 index
  359. Implements the primary route used by all pages not behind auth.
  360. Most subsequent functions simply pass content to this function.
  361. =cut
  362. sub index ( $query, $content = '', $i_styles = [], $i_scripts = [] ) {
  363. $query->{theme_dir} = Trog::Themes::td();
  364. _absorb_feedback($query);
  365. my $to_render = $query->{template} // $landing_page;
  366. $content ||= Trog::Renderer->render( template => $to_render, data => $query, component => 1, contenttype => 'text/html' );
  367. return $content if ref $content eq "ARRAY";
  368. my @styles;
  369. unshift( @styles, qw{embed.css} ) if $query->{embed};
  370. unshift( @styles, qw{screen.css structure.css} );
  371. push( @styles, @$i_styles );
  372. my @p_styles = qw{structure.css print.css};
  373. unshift( @p_styles, qw{embed.css} ) if $query->{embed};
  374. my @series = _get_series(0);
  375. my $title = $query->{primary_post}{title} // $query->{title} // $Theme::default_title // 'tCMS';
  376. # Handle link "unfurling" correctly
  377. my ( $default_tags, $meta_desc, $meta_tags ) = _build_social_meta( $query, $title );
  378. #Do embed content
  379. my $tmpl = $query->{embed} ? 'embed.tx' : 'index.tx';
  380. $query->{theme_dir} =~ s/^\/www\///;
  381. # Grab the avatar class for the logged in user
  382. if ( $query->{user} ) {
  383. $query->{user_class} = Trog::Auth::username2classname( $query->{user} );
  384. }
  385. state $data;
  386. $data //= Trog::Data->new( Trog::Config::get() );
  387. my ( $search_lang, $search_help ) = _search_language($query);
  388. # Consumed, so expire it: otherwise the banner reappears on every page the
  389. # reader visits until the cookie times out on its own.
  390. my %headers;
  391. %headers = ( 'Set-Cookie' => 'tcmsfeedback=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0' ) if $query->{feedback_seen};
  392. return finish_render(
  393. $tmpl,
  394. {
  395. %$query,
  396. search_lang => $search_lang,
  397. search_help => $search_help,
  398. theme_dir => Trog::Themes::td(),
  399. content => $content,
  400. title => $title,
  401. categories => \@series,
  402. stylesheets => \@styles,
  403. print_styles => \@p_styles,
  404. scripts => $i_scripts,
  405. show_madeby => $Theme::show_madeby ? 1 : 0,
  406. embed => $query->{embed} ? 1 : 0,
  407. embed_video => $query->{primary_post}{is_video},
  408. default_tags => $default_tags,
  409. meta_desc => $meta_desc,
  410. meta_tags => $meta_tags,
  411. },
  412. %headers,
  413. );
  414. }
  415. sub _build_social_meta ( $query, $title ) {
  416. return ( undef, undef, undef ) unless $query->{social_meta} && $query->{route} && $query->{domain};
  417. my $default_tags = $Theme::default_tags;
  418. $default_tags .= ',' . join( ',', @{ $query->{primary_post}->{tags} } ) if $default_tags && $query->{primary_post}->{tags};
  419. my $primary_data = ref $query->{primary_post}{data} eq 'ARRAY' ? $query->{primary_post}{data}[0] : $query->{primary_post}{data};
  420. my $meta_desc = $primary_data // $Theme::description // "tCMS Site";
  421. $meta_desc = Trog::Utils::strip_and_trunc($meta_desc) || '';
  422. my $meta_tags = '';
  423. my $card_type = 'summary';
  424. $card_type = 'featured_image' if $query->{primary_post} && $query->{primary_post}{is_image};
  425. $card_type = 'player' if $query->{primary_post} && $query->{primary_post}{is_video};
  426. my $image = $Theme::default_image ? "https://$query->{domain}/" . Trog::Themes::get_dir() . "/$Theme::default_image" : '';
  427. $image = "https://$query->{domain}/$query->{primary_post}{preview}" if $query->{primary_post} && $query->{primary_post}{preview};
  428. $image = "https://$query->{domain}/$query->{primary_post}{href}" if $query->{primary_post} && $query->{primary_post}{is_image};
  429. my $primary_route = "https://$query->{domain}/$query->{route}";
  430. $primary_route =~ s/[\/]+/\//g;
  431. my $display_name = $Theme::display_name || 'Another tCMS Site';
  432. my $extra_tags = '';
  433. my %sopts = (
  434. site => '',
  435. image => '',
  436. fb_app_id => '',
  437. site_name => $display_name,
  438. app_name => $display_name,
  439. title => $title,
  440. description => $meta_desc,
  441. url => $primary_route,
  442. );
  443. $sopts{site} = $Theme::twitter_account if $Theme::twitter_account;
  444. $sopts{image} = $image if $image;
  445. $sopts{fb_app_id} = $Theme::fb_app_id if $Theme::fb_app_id;
  446. if ( $query->{primary_post} && $query->{primary_post}{is_video} ) {
  447. $sopts{player} = "https://$query->{domain}/$query->{primary_post}{href}";
  448. #XXX don't hardcode this
  449. $sopts{player_width} = 1280;
  450. $sopts{player_height} = 720;
  451. $extra_tags .= "<meta property='og:video:type' content='$query->{primary_post}{content_type}' />\n";
  452. }
  453. my $social = HTML::SocialMeta->new(%sopts);
  454. $meta_tags = eval { $social->create($card_type) };
  455. $meta_tags =~ s/content="video"/content="video:other"/mg if $meta_tags;
  456. $meta_tags .= $extra_tags if $extra_tags;
  457. print STDERR "WARNING: Theme misconfigured, social media tags will not be included\n$@\n" if $theme_dir && !$meta_tags;
  458. return ( $default_tags, $meta_desc, $meta_tags );
  459. }
  460. =head1 NORMAL ROUTES
  461. These are expected to either return a 200, or redirect to something which does.
  462. =head2 setup
  463. One time setup page; should only display to the first user to visit the site which we presume to be the administrator.
  464. =cut
  465. sub setup ($query) {
  466. File::Touch::touch("config/setup");
  467. Trog::Renderer->render(
  468. template => 'notconfigured.tx',
  469. data => {
  470. title => 'tCMS Requires Setup to Continue...',
  471. stylesheets => _build_themed_styles( ['notconfigured.css'] ),
  472. %$query,
  473. },
  474. contenttype => 'text/html',
  475. code => 200,
  476. );
  477. }
  478. =head2 totp
  479. Enable 2 factor auth via TOTP for the currently authenticated user.
  480. Returns a page with a QR code & TOTP uri for pasting into your authenticator app of choice.
  481. =cut
  482. sub totp ($query) {
  483. my $active_user = $query->{user};
  484. my $domain = $query->{domain};
  485. $query->{failure} //= -1;
  486. my ( $uri, $qr, $failure, $message, $totp ) = Trog::Auth::totp( $active_user, $domain );
  487. my $now_tm = time;
  488. my $now_string = strftime( "%a %b %e T%H:%M:%SZ %Y", gmtime($now_tm) );
  489. return Trog::Routes::HTML::index(
  490. {
  491. title => 'Enable TOTP 2-Factor Auth',
  492. theme_dir => Trog::Themes::td(),
  493. uri => $uri,
  494. qr => $qr,
  495. failure => $failure,
  496. message => $message,
  497. template => 'totp.tx',
  498. is_admin => 1,
  499. cur_code => $totp->expected_totp_code($now_tm),
  500. timestamp => $now_string,
  501. %$query,
  502. },
  503. undef,
  504. [qw{post.css}],
  505. );
  506. }
  507. =head2 login
  508. Sets the user cookie if the provided user exists, or sets up the user as an admin with the provided credentials in the event that no users exist.
  509. =cut
  510. sub login ($query) {
  511. # Redirect if we actually have a logged in user.
  512. # Note to future me -- this user value is overwritten explicitly in server.psgi.
  513. # If that ever changes, you will die
  514. $query->{to} //= $query->{route};
  515. $query->{to} = '/config' if List::Util::any { $query->{to} eq $_ } qw{/login /logout};
  516. if ( $query->{user} ) {
  517. DEBUG("Login by $query->{user}, redirecting to $query->{to}");
  518. return $query->{tpsgi}->see_also( $query->{to} );
  519. }
  520. #Check and see if we have no users. If so we will just accept whatever creds are passed.
  521. my $hasusers = -f "config/has_users"; ## no critic (ProhibitFiletest_f) -- a flag file, only ever touched
  522. my $btnmsg = $hasusers ? "Log In" : "Register";
  523. my $headers;
  524. my $has_totp = 0;
  525. if ( $query->{username} && $query->{password} ) {
  526. if ( !$hasusers ) {
  527. # Make the first user
  528. Trog::Auth::useradd( $query->{username}, $query->{display_name}, $query->{password}, ['admin'], $query->{contact_email} );
  529. # Add a stub user page and the initial series.
  530. my $dat = Trog::Data->new( Trog::Config::get() );
  531. _setup_initial_db( $dat, $query->{username}, $query->{display_name}, $query->{contact_email} );
  532. # Ensure we stop registering new users
  533. File::Touch::touch("config/has_users");
  534. }
  535. $query->{failed} = 1;
  536. my $cookie = Trog::Auth::mksession( $query->{username}, $query->{password}, $query->{token}, $query->{ip} // '' );
  537. if ($cookie) {
  538. # TODO secure / sameSite cookie to kill csrf, maybe do rememberme with Expires=~0
  539. my $secure = '';
  540. $secure = '; Secure' if $query->{scheme} eq 'https';
  541. $headers = {
  542. "Set-Cookie" => "tcmslogin=$cookie; HttpOnly; SameSite=Strict$secure",
  543. };
  544. $query->{failed} = 0;
  545. }
  546. }
  547. $query->{failed} //= -1;
  548. return Trog::Renderer->render(
  549. template => 'login.tx',
  550. data => {
  551. title => 'tCMS 2 ~ Login',
  552. to => $query->{to},
  553. failure => int( $query->{failed} ),
  554. message => int( $query->{failed} ) < 1 ? "Login Successful, Redirecting..." : "Login Failed.",
  555. btnmsg => $btnmsg,
  556. stylesheets => _build_themed_styles( [qw{structure.css screen.css login.css}] ),
  557. theme_dir => Trog::Themes::td(),
  558. has_users => $hasusers,
  559. %$query,
  560. },
  561. headers => $headers,
  562. contenttype => 'text/html',
  563. code => 200,
  564. nocache => $query->{nocache},
  565. );
  566. }
  567. sub _setup_initial_db ( $dat, $user, $display_name, $contact_email ) {
  568. $dat->add(
  569. {
  570. "aclname" => "series",
  571. "acls" => [],
  572. "callback" => "Trog::Routes::HTML::series",
  573. method => 'GET',
  574. "data" => "Series",
  575. "href" => "/series",
  576. "local_href" => "/series",
  577. "preview" => "/img/sys/testpattern.jpg",
  578. "tags" => [qw{series topbar}],
  579. visibility => 'public',
  580. "title" => "Series",
  581. user => $user,
  582. form => 'series.tx',
  583. child_form => 'series.tx',
  584. aliases => [],
  585. },
  586. {
  587. "aclname" => "about",
  588. "acls" => [],
  589. "callback" => "Trog::Routes::HTML::series",
  590. method => 'GET',
  591. "data" => "About",
  592. "href" => "/about",
  593. "local_href" => "/about",
  594. "preview" => "/img/sys/testpattern.jpg",
  595. "tags" => [qw{series topbar public}],
  596. visibility => 'public',
  597. "title" => "About",
  598. user => $user,
  599. form => 'series.tx',
  600. child_form => 'profile.tx',
  601. aliases => [],
  602. },
  603. {
  604. "aclname" => "config",
  605. acls => [],
  606. "callback" => "Trog::Routes::HTML::config",
  607. 'method' => 'GET',
  608. "content_type" => "text/html",
  609. "data" => "Config",
  610. "href" => "/config",
  611. "local_href" => "/config",
  612. "preview" => "/img/sys/testpattern.jpg",
  613. "tags" => [qw{admin}],
  614. visibility => 'private',
  615. "title" => "Configure tCMS",
  616. user => $user,
  617. aliases => [],
  618. },
  619. {
  620. title => $display_name,
  621. data => 'Default user',
  622. preview => '/img/avatar/humm.gif',
  623. wallpaper => '/img/sys/testpattern.jpg',
  624. tags => ['about'],
  625. visibility => 'public',
  626. acls => ['admin'],
  627. local_href => "/users/$display_name",
  628. display_name => $display_name,
  629. contact_email => $contact_email,
  630. callback => "Trog::Routes::HTML::users",
  631. method => 'GET',
  632. user => $user,
  633. form => 'profile.tx',
  634. aliases => [],
  635. },
  636. );
  637. }
  638. =head2 logout
  639. Deletes your users' session and opens the index.
  640. =cut
  641. sub logout ($query) {
  642. Trog::Auth::killsession( $query->{user}, $query->{ip} // '' ) if $query->{user};
  643. delete $query->{user};
  644. return Trog::Routes::HTML::index($query);
  645. }
  646. =head2 config
  647. Renders the configuration page, or redirects you back to the login page.
  648. =cut
  649. sub config ( $query = {} ) {
  650. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  651. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  652. $query->{failure} //= -1;
  653. #XXX ACHTUNG config::simple has this brain damaged behavior of returning a multiple element array when you access something that does not exist.
  654. #XXX straight up dying would be preferrable.
  655. #XXX anyways, this means you can NEVER NEVER NEVER access a param from within a hash directly. YOU HAVE BEEN WARNED!
  656. my $conf = Trog::Config::get();
  657. state $theme = $conf->param('general.theme') // '';
  658. state $dm = $conf->param('general.data_model') // 'DUMMY';
  659. state $embeds = $conf->param('security.allow_embeds_from') // '';
  660. state $hostname = $conf->param('general.hostname') // '';
  661. return Trog::Routes::HTML::index(
  662. {
  663. title => 'Configure tCMS',
  664. theme_dir => Trog::Themes::td(),
  665. stylesheets => [qw{config.css}],
  666. scripts => [qw{post.js}],
  667. themes => _get_themes() || [],
  668. data_models => _get_data_models(),
  669. current_theme => $theme,
  670. current_data_model => $dm,
  671. message => $query->{message},
  672. failure => $query->{failure},
  673. to => '/config',
  674. scheme => $query->{scheme},
  675. embeds => $embeds,
  676. is_admin => 1,
  677. template => 'config.tx',
  678. %$query,
  679. hostname => $hostname,
  680. },
  681. undef,
  682. [qw{config.css}],
  683. );
  684. }
  685. =head2 resetpass
  686. =head2 do_resetpass
  687. =head2 do_totp_clear
  688. Routes for user service of their authentication details.
  689. =cut
  690. sub resetpass ($query) {
  691. $query->{failure} //= -1;
  692. my $is_admin = grep { $_ eq 'admin' } @{ $query->{user_acls} };
  693. return Trog::Routes::HTML::index(
  694. {
  695. title => 'Request Authentication Resets',
  696. theme_dir => Trog::Themes::td(),
  697. stylesheets => [qw{config.css}],
  698. scripts => [qw{post.js}],
  699. message => $query->{message},
  700. failure => $query->{failure},
  701. scheme => $query->{scheme},
  702. template => 'resetpass.tx',
  703. is_admin => $is_admin,
  704. %$query,
  705. },
  706. undef,
  707. [qw{config.css}],
  708. );
  709. }
  710. sub do_resetpass ($query) {
  711. my $user = $query->{username};
  712. # User Does not exist
  713. return $query->{tpsgi}->forbidden($query) if !Trog::Auth::user_exists($user);
  714. # User exists, but is not logged in this session
  715. return $query->{tpsgi}->forbidden($query) if !$query->{user} && Trog::Auth::user_has_session($user);
  716. my $token = Trog::Utils::uuid();
  717. my $newpass = $query->{password} // Trog::Utils::uuid();
  718. my $res = Trog::Auth::add_change_request( type => 'reset_pass', user => $user, secret => $newpass, token => $token );
  719. die "Could not add auth change request!" unless $res;
  720. # If the user is logged in, just do the deed, otherwise send them the token in an email
  721. if ( $query->{user} ) {
  722. return $query->{tpsgi}->see_also("/api/auth_change_request/$token");
  723. }
  724. Trog::Email::contact(
  725. $user,
  726. "root\@$query->{domain}",
  727. "$query->{domain}: Password reset URL for $user",
  728. { uri => "$query->{scheme}://$query->{domain}/api/auth_change_request/$token", template => 'password_reset.tx' }
  729. );
  730. return $query->{tpsgi}->see_also("/processed");
  731. }
  732. sub do_totp_clear ($query) {
  733. my $user = $query->{username};
  734. # User Does not exist
  735. return $query->{tpsgi}->forbidden($query) if !Trog::Auth::user_exists($user);
  736. # User exists, but is not logged in this session
  737. return $query->{tpsgi}->forbidden($query) if !$query->{user} && Trog::Auth::user_has_session($user);
  738. my $token = Trog::Utils::uuid();
  739. my $res = Trog::Auth::add_change_request( type => 'clear_totp', user => $user, token => $token );
  740. die "Could not add auth change request!" unless $res;
  741. # If the user is logged in, just do the deed, otherwise send them the token in an email
  742. if ( $query->{user} ) {
  743. return $query->{tpsgi}->see_also("/api/auth_change_request/$token");
  744. }
  745. Trog::Email::contact(
  746. $user,
  747. "root\@$query->{domain}",
  748. "$query->{domain}: Password reset URL for $user",
  749. { uri => "$query->{scheme}://$query->{domain}/api/auth_change_request/$token", template => 'totp_reset.tx' }
  750. );
  751. return $query->{tpsgi}->see_also("/processed");
  752. }
  753. sub _get_series ( $edit = 0 ) {
  754. state $data;
  755. $data //= Trog::Data->new( Trog::Config::get() );
  756. my @series = $data->get(
  757. acls => [qw{public}],
  758. tags => [qw{topbar}],
  759. limit => 10,
  760. page => 1,
  761. );
  762. @series = map { $_->{local_href} = "/post$_->{local_href}"; $_ } @series if $edit;
  763. return @series;
  764. }
  765. sub _get_themes {
  766. my $dir = 'www/themes';
  767. opendir( my $dh, $dir ) || do { die "Can't opendir $dir: $!" unless $!{ENOENT} };
  768. my @tdirs = grep { !/^\./ && -d "$dir/$_" } readdir($dh);
  769. closedir $dh;
  770. return \@tdirs;
  771. }
  772. sub _get_data_models {
  773. foreach my $incdir (@INC) {
  774. my $dir = "$incdir/Trog/Data";
  775. next unless -d $dir;
  776. opendir( my $dh, $dir ) || die "Can't opendir $dir: $!";
  777. my @dmods = map { s/\.pm$//g; $_ } grep { /\.pm$/ && -f "$dir/$_" } readdir($dh); ## no critic (ProhibitFiletest_f) -- building a menu of module names
  778. closedir $dh;
  779. return \@dmods;
  780. }
  781. die "Could not find tCMS data modules! Is tCMS in \@INC?";
  782. }
  783. =head2 _get_datasources
  784. Every Trog::DataSource::* module on disk.
  785. The wizard offers these as somewhere other than the datastore for a post type
  786. to get its posts from, and post_wizard_save checks a submitted one against this
  787. list -- the name ends up in a sidecar and is later require()d, so it has to be
  788. something we found rather than something we were told.
  789. =cut
  790. sub _get_datasources {
  791. my %found;
  792. foreach my $incdir (@INC) {
  793. my $dir = "$incdir/Trog/DataSource";
  794. next unless -d $dir;
  795. opendir( my $dh, $dir ) or next;
  796. $found{"Trog::DataSource::$_"} = 1 foreach map { s/\.pm$//r } grep { /\.pm$/ && -f "$dir/$_" } readdir($dh); ## no critic (ProhibitFiletest_f) -- building an allowlist of names
  797. closedir $dh;
  798. }
  799. return [ sort keys(%found) ];
  800. }
  801. =head2 _datasource_editable($module)
  802. Whether a post type backed by $module should be given an editor.
  803. A datasource says so with an EDITABLE constant. One that does not say is
  804. assumed editable, since that is what a post type is unless it has a reason not
  805. to be.
  806. =cut
  807. sub _datasource_editable ($module) {
  808. return 1 unless $module;
  809. return 1 unless Trog::DataSource::load($module);
  810. return 1 unless $module->can('EDITABLE');
  811. return $module->EDITABLE ? 1 : 0;
  812. }
  813. =head2 config_save
  814. Implements /config/save route. Saves what little configuration we actually use to config/main.cfg
  815. =cut
  816. sub config_save ($query) {
  817. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  818. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  819. my $conf = Trog::Config::get();
  820. $conf->param( 'general.theme', $query->{theme} ) if defined $query->{theme};
  821. $conf->param( 'general.data_model', $query->{data_model} ) if $query->{data_model};
  822. $conf->param( 'security.allow_embeds_from', $query->{embeds} ) if $query->{embeds};
  823. $conf->param( 'general.hostname', $query->{hostname} ) if $query->{hostname};
  824. $query->{failure} = 1;
  825. $query->{message} = "Failed to save configuration!";
  826. if ( $conf->write($Trog::Config::home_cfg) ) {
  827. $query->{failure} = 0;
  828. $query->{message} = "Configuration updated succesfully.";
  829. }
  830. #We need to reap the children with outdated configuration.
  831. $query->{tpsgi}->signal_restart_parent();
  832. return config($query);
  833. }
  834. =head2 themeclone
  835. Clone a theme by copying a directory.
  836. =cut
  837. sub themeclone ($query) {
  838. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  839. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  840. my ( $theme, $newtheme ) = ( $query->{theme}, $query->{newtheme} );
  841. my $themedir = 'www/themes';
  842. $query->{failure} = 1;
  843. $query->{message} = "Failed to clone theme '$theme' as '$newtheme'!";
  844. require File::Copy::Recursive;
  845. if ( $theme && $newtheme && File::Copy::Recursive::dircopy( "$themedir/$theme", "$themedir/$newtheme" ) ) {
  846. $query->{failure} = 0;
  847. $query->{message} = "Successfully cloned theme '$theme' as '$newtheme'.";
  848. }
  849. return $query->{tpsgi}->see_also('/config');
  850. }
  851. =head2 post_save
  852. Saves posts submitted via the /post pages
  853. =cut
  854. sub post_save ($qq) {
  855. return $qq->{tpsgi}->see_also('/login') unless $qq->{user};
  856. return $qq->{tpsgi}->forbidden($qq) unless grep { $_ eq 'admin' } @{ $qq->{user_acls} };
  857. my $query = clone($qq);
  858. my $to = delete $query->{to};
  859. #Copy this down since it will be deleted later
  860. my $acls = $query->{acls};
  861. $query->{tags} = Trog::Utils::coerce_array( $query->{tags} );
  862. # Support data with multiple pages like presentations
  863. $query->{data} = Trog::Utils::coerce_array( $query->{data} ) if $query->{data_is_array};
  864. $query->{attachments} = Trog::Utils::coerce_array( $query->{attachments} );
  865. # Filter bits and bobs XXX this is done deeper in, may require removal
  866. delete $query->{primary_post};
  867. delete $query->{social_meta};
  868. delete $query->{deflate};
  869. delete $query->{acls};
  870. # Ensure there are no null tags
  871. @{ $query->{tags} } = grep { defined $_ } @{ $query->{tags} };
  872. # Posts will always be GET
  873. $query->{method} = 'GET';
  874. state $data;
  875. $data //= Trog::Data->new( Trog::Config::get() );
  876. # A post that doesn't match its type's schema is the user's mistake, not a
  877. # server error, so hand the validation errors back rather than dying.
  878. local $@;
  879. eval { $data->add($query); 1 } or do {
  880. my @errors = Ref::Util::is_arrayref($@) ? @{$@} : ($@);
  881. my $why = "Post failed validation:\n" . join( "\n", map { _short_error($_) } @errors );
  882. # The full detail goes to the log; the user gets it in the banner on
  883. # the page they came from, rather than a dead-end 400.
  884. WARN("Rejected post from $qq->{user}: $why");
  885. return _feedback_redirect( $qq, '/secure' . $to, 1, $why );
  886. };
  887. # Instruct tpsgi to invalidate the cached render.
  888. $qq->{tpsgi}->add_post_close_callback(
  889. sub {
  890. # XXX there is not a great way to find what needs to be re-rendered because posts can include other posts.
  891. # As such we just have to nuke all the .html renders.
  892. $qq->{tpsgi}->invalidate_renders('html');
  893. }
  894. );
  895. # Force a reload of the routing table
  896. $qq->{tpsgi}->signal_restart_parent();
  897. return _feedback_redirect( $qq, '/secure' . $to, 0, "Saved post '$query->{title}'." );
  898. }
  899. =head2 profile
  900. Saves / updates new users.
  901. =cut
  902. sub profile ($query) {
  903. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  904. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  905. # Find the user's post and edit it
  906. state $data;
  907. $data //= Trog::Data->new( Trog::Config::get() );
  908. my @userposts = $data->get( tags => ['about'], acls => [qw{admin}] );
  909. # Users are always self-authored, you see
  910. my $user_obj = List::Util::first { ( $_->{user} || '' ) eq $query->{username} } @userposts;
  911. my $username = $query->{username};
  912. my $password = $query->{password};
  913. my $changed =
  914. $username ne ( $user_obj->{user} // '' )
  915. || $password
  916. || ( $query->{contact_email} // '' ) ne ( $user_obj->{contact_email} // '' )
  917. || ( $query->{display_name} // '' ) ne ( $user_obj->{display_name} // '' );
  918. #Make sure it is "self-authored", redact pw
  919. $query->{user} = delete $query->{username};
  920. delete $query->{password};
  921. # Use the display name as the title
  922. $query->{title} = $query->{display_name};
  923. my %merged = (
  924. %$user_obj,
  925. %$query,
  926. $query->{display_name} ? ( local_href => "/users/$query->{display_name}" ) : ( local_href => $user_obj->{local_href} ),
  927. );
  928. # Validate before touching the auth database, not after. useradd() does an
  929. # INSERT OR REPLACE on the user row, which cascades to the session table and
  930. # logs the user straight out -- doing that and *then* rejecting the post
  931. # leaves the credentials disagreeing with the profile post they came from.
  932. my @errors = Trog::DataModule::validate( clone( \%merged ) );
  933. if (@errors) {
  934. my $why = "Post failed validation:\n" . join( "\n", @errors );
  935. WARN("Rejected profile update from $query->{user}: $why");
  936. return $query->{tpsgi}->badrequest( $query, $why );
  937. }
  938. if ($changed) {
  939. my $for_user = Trog::Auth::acls4user($username);
  940. #TODO support non-admin users
  941. my @acls = @$for_user ? @$for_user : qw{admin};
  942. Trog::Auth::useradd( $username, $merged{display_name}, $password, \@acls, $merged{contact_email} );
  943. }
  944. return post_save( \%merged );
  945. }
  946. =head2 post_delete
  947. deletes posts.
  948. =cut
  949. sub post_delete ($query) {
  950. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  951. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  952. state $data;
  953. $data //= Trog::Data->new( Trog::Config::get() );
  954. $data->delete($query) and die "Could not delete post";
  955. return $query->{tpsgi}->see_also( $query->{to} );
  956. }
  957. =head2 _enrich_post($post, $query, $cache)
  958. Resolve a post type's declared relations onto the post, and run its enrich sub
  959. if it has one.
  960. A type says what it depends on in its sidecar rather than in code here -- see
  961. Trog::DataModule::relations_for. An entry naming a 'from' field resolves the
  962. UUID in that field into the post it refers to; one without gets every post of
  963. the target type, which is what an editor picker or a "show me all of these"
  964. display wants.
  965. $cache is a per-request memo of the per-type lookups. Without it a page of
  966. invoices re-scans every entities post once per invoice.
  967. =cut
  968. sub _enrich_post ( $post, $query, $cache = {} ) {
  969. return unless Ref::Util::is_hashref($post) && $post->{form};
  970. my $relations = Trog::DataModule::relations_for( $post->{form} );
  971. foreach my $as ( keys(%$relations) ) {
  972. my $relation = $relations->{$as};
  973. next unless Ref::Util::is_hashref($relation);
  974. my ($target) = ( $relation->{form} // '' ) =~ m/^([A-Za-z0-9_-]+\.tx)$/;
  975. next unless $target;
  976. $cache->{$target} //= [ _post_helper( { form => $target }, [], $query->{user_acls} ) ];
  977. # No 'from' means the relation is the whole set rather than one of them.
  978. my $from = $relation->{from};
  979. if ( !$from ) {
  980. $post->{$as} = $cache->{$target};
  981. next;
  982. }
  983. my $wanted = $post->{$from};
  984. $post->{$as} = defined $wanted ? List::Util::first { $_->{id} eq $wanted } @{ $cache->{$target} } : undef;
  985. }
  986. _enrich_callback( $post, $query );
  987. return;
  988. }
  989. # Anything a type needs computed rather than merely fetched. The sub is named
  990. # in the sidecar, so validate it the same way a post's own callback field is --
  991. # a sidecar naming an arbitrary sub is a privilege question, not a typo.
  992. sub _enrich_callback ( $post, $query ) {
  993. my $meta = Trog::DataModule::type_meta_for( $post->{form} );
  994. my $sub = $meta->{'x-tcms-post-type'}{enrich};
  995. return unless $sub && !ref $sub;
  996. my ($modname) = $sub =~ m/^([\w:]+)::\w+$/;
  997. if ( !$modname ) {
  998. WARN("Post type '$post->{form}' declares a malformed enrich sub '$sub'");
  999. return;
  1000. }
  1001. my $modpath = $modname;
  1002. $modpath =~ s{::}{/}g;
  1003. $modpath .= '.pm';
  1004. local $@;
  1005. eval { require $modpath; 1 } or do {
  1006. WARN("Post type '$post->{form}' declares enrich sub '$sub', but $modname will not load: $@");
  1007. return;
  1008. };
  1009. no strict 'refs';
  1010. if ( !defined &{$sub} ) {
  1011. WARN("Post type '$post->{form}' declares enrich sub '$sub', which does not exist");
  1012. return;
  1013. }
  1014. my %extra = &{$sub}( $post, $query );
  1015. use strict;
  1016. @$post{ keys(%extra) } = values(%extra);
  1017. return;
  1018. }
  1019. =head2 series
  1020. Series specific view, much like the users/ route
  1021. Displays identified series, not all series.
  1022. =cut
  1023. sub series ($query) {
  1024. my $is_admin = grep { $_ eq 'admin' } @{ $query->{user_acls} };
  1025. # rip away /secure if present
  1026. $query->{route} =~ s|^/secure||;
  1027. #we are either viewed one of two ways, /post/$id or /$aclname
  1028. my ( undef, $aclname, $id ) = split( '/', $query->{route} );
  1029. $query->{aclname} = $aclname if !$id;
  1030. $query->{id} = $id if $id;
  1031. # Don't show topbar series on the series page. That said, don't exclude it from direct series view.
  1032. $query->{exclude_tags} = ['topbar'] if !$is_admin && $aclname && $aclname eq 'series';
  1033. #XXX I'd prefer to overload id to actually *be* the aclname...
  1034. # but this way, accomodates things like the flat file time-indexing hack.
  1035. # TODO I should probably have it for all posts, and make *everything* a series.
  1036. # WE can then do threaded comments/posts.
  1037. # That will essentially necessitate it *becoming* the ID for real.
  1038. #Grab the relevant tag (aclname), then pass that to posts
  1039. #
  1040. # Deliberately not $query: this is looking up the one series post named by
  1041. # the route, and handing it the reader's pagination pages *that* lookup.
  1042. # Asking for page two of a single post gets nothing, so a series carrying a
  1043. # ?page= in its URL could not find itself and 404'd on its own page. Latent
  1044. # until something started emitting page numbers -- see _paginate_offset.
  1045. my @posts = _post_helper( { %$query, page => 1, limit => 1 }, ['series'], $query->{user_acls} );
  1046. # The series post carries its *children's* relations, so that an editor
  1047. # picker rendered on this page has something to populate from -- that is
  1048. # what $primary_post.entities is on an invoice series. Keyed on child_form
  1049. # rather than form: the series is a series.tx, its children are what
  1050. # declare the dependency.
  1051. if ( $posts[0] ) {
  1052. my $relations = Trog::DataModule::relations_for( $posts[0]{child_form} );
  1053. if (%$relations) {
  1054. # Resolve against a stand-in wearing the child's form, then copy
  1055. # only the relation keys back, so nothing else about the series
  1056. # post gets overwritten.
  1057. my $stand_in = { %{ $posts[0] }, form => $posts[0]{child_form} };
  1058. # posts() widens user_acls to what a viewer can actually see, but
  1059. # that happens after this -- so resolving here against the raw list
  1060. # means a logged out visitor resolves every relation against no
  1061. # acls at all, and finds nothing however public the target is.
  1062. my @acls = ( @{ $query->{user_acls} }, 'public' );
  1063. push( @acls, 'private' ) if $is_admin;
  1064. _enrich_post( $stand_in, { %$query, user_acls => \@acls } );
  1065. @{ $posts[0] }{ keys(%$relations) } = @{$stand_in}{ keys(%$relations) };
  1066. }
  1067. }
  1068. delete $query->{id};
  1069. delete $query->{aclname};
  1070. $query->{subhead} = $posts[0]->{data};
  1071. $query->{title} = $posts[0]->{title};
  1072. $query->{tag} = $posts[0]->{aclname};
  1073. $query->{primary_post} = $posts[0];
  1074. $query->{in_series} = 1;
  1075. return posts($query);
  1076. }
  1077. =head2 avatars
  1078. Returns the avatars.css.
  1079. =cut
  1080. sub avatars ($query) {
  1081. push( @{ $query->{user_acls} }, 'public' );
  1082. my $tags = Trog::Utils::coerce_array( $query->{tag} );
  1083. my @posts = _post_helper( $query, $tags, $query->{user_acls} );
  1084. if (@posts) {
  1085. # Set the eTag so that we don't get a re-fetch
  1086. $query->{etag} = "$posts[0]{id}-$posts[0]{version}";
  1087. }
  1088. @posts = map { $_->{id} =~ tr/-/_/; $_->{id} = "a_$_->{id}"; $_ } @posts;
  1089. return Trog::Renderer->render(
  1090. template => 'avatars.tx',
  1091. data => {
  1092. users => \@posts,
  1093. %$query,
  1094. },
  1095. code => 200,
  1096. contenttype => 'text/css',
  1097. );
  1098. }
  1099. =head2 libravatar
  1100. Implements the libravatar protocol (https://wiki.libravatar.org/running_your_own/).
  1101. Accepts GET /avatar/{hash} where {hash} is the MD5 or SHA-256 of the user's
  1102. lowercased email address, and redirects to their profile avatar image.
  1103. Returns 404 when no matching user is found or the user has no avatar.
  1104. =cut
  1105. sub libravatar ($query) {
  1106. my $hash = lc( $query->{avatar_hash} // '' );
  1107. my $users = Trog::Auth::users_with_emails();
  1108. my $matched_user;
  1109. for my $u (@$users) {
  1110. next unless $u->{contact_email};
  1111. my $email = lc( $u->{contact_email} );
  1112. if ( $hash eq sha256_hex($email) || $hash eq md5_hex($email) ) {
  1113. $matched_user = $u->{name};
  1114. last;
  1115. }
  1116. }
  1117. return $query->{tpsgi}->notfound($query) unless $matched_user;
  1118. my @posts = _post_helper( { author => $matched_user }, ['about'], [qw{public admin}] );
  1119. my $preview = @posts ? $posts[0]->{preview} : '';
  1120. return $query->{tpsgi}->notfound($query) unless $preview;
  1121. return $query->{tpsgi}->redirect_permanent("/$preview");
  1122. }
  1123. =head2 users
  1124. Implements direct user profile view.
  1125. =cut
  1126. sub users ($query) {
  1127. # rip away /secure if present
  1128. $query->{route} =~ s|^/secure||;
  1129. # Capture the username
  1130. my ( undef, undef, $display_name ) = split( '/', $query->{route} );
  1131. $display_name = URI::Escape::uri_unescape($display_name);
  1132. my $username = Trog::Auth::display2username($display_name);
  1133. return $query->{tpsgi}->notfound($query) unless $username;
  1134. $query->{username} //= $username;
  1135. push( @{ $query->{user_acls} }, 'public' );
  1136. $query->{exclude_tags} = ['about'];
  1137. # Don't show topbar series on the series page. That said, don't exclude it from direct series view.
  1138. my $is_admin = grep { $_ eq 'admin' } @{ $query->{user_acls} };
  1139. push( @{ $query->{exclude_tags} }, 'topbar' ) if !$is_admin;
  1140. my @posts = _post_helper( { author => $query->{username} }, ['about'], $query->{user_acls} );
  1141. $query->{id} = $posts[0]->{id};
  1142. $query->{title} = $posts[0]->{display_name};
  1143. $posts[0]->{title} = $posts[0]->{display_name};
  1144. $query->{user_obj} = $posts[0];
  1145. $query->{primary_post} = $posts[0];
  1146. $query->{in_series} = 1;
  1147. return posts($query);
  1148. }
  1149. =head2 posts
  1150. Display multi or single posts, supports RSS and pagination.
  1151. =cut
  1152. sub posts ( $query, $direct = 0 ) {
  1153. # Before we build the render data, not after: posts.tx is rendered here and
  1154. # handed to index() as content, so index()'s own absorb would be too late.
  1155. _absorb_feedback($query);
  1156. # Allow rss.xml to tell what posts to loop over
  1157. my $fmt = $query->{format} || '';
  1158. #Process the input URI to capture tag/id
  1159. $query->{route} //= $query->{to};
  1160. # rip away /secure if present
  1161. $query->{route} =~ s|^/secure||;
  1162. my ( undef, undef, $id ) = split( '/', $query->{route} );
  1163. my $tags = Trog::Utils::coerce_array( $query->{tag} );
  1164. $query->{id} = $id if $id && !$query->{in_series};
  1165. my $is_admin = grep { $_ eq 'admin' } @{ $query->{user_acls} };
  1166. push( @{ $query->{user_acls} }, 'public' );
  1167. push( @{ $query->{user_acls} }, 'unlisted' ) if $query->{id};
  1168. push( @{ $query->{user_acls} }, 'private' ) if $is_admin;
  1169. my @posts;
  1170. # Discover this user's visibility, so we can make them post in this category by default
  1171. my $user_visibility = 'public';
  1172. if ( $query->{user_obj} ) {
  1173. #Optimize the /users/* route
  1174. @posts = ( $query->{user_obj} );
  1175. $user_visibility = $query->{user_obj}->{visibility};
  1176. }
  1177. else {
  1178. if ( $query->{user} ) {
  1179. my @me = _post_helper( { author => $query->{user} }, ['about'], $query->{user_acls} );
  1180. $user_visibility = $me[0]->{visibility};
  1181. }
  1182. @posts = _post_helper( $query, $tags, $query->{user_acls} );
  1183. }
  1184. # A post type can say its posts come from somewhere other than the
  1185. # datastore -- see Trog::DataSource::Virt, which builds them out of libvirt
  1186. # guests. The series is still an ordinary post; only its children are
  1187. # synthesized, and they are rebuilt on every view rather than stored.
  1188. @posts = _datasource_posts( $query, \@posts );
  1189. if ( $query->{id} ) {
  1190. $query->{primary_post} = $posts[0] if @posts;
  1191. }
  1192. #OK, so if we have a user as the ID we found, go grab the rest of their posts
  1193. if ( $query->{id} && @posts && List::Util::any { $_ eq 'about' } @{ $posts[0]->{tags} } ) {
  1194. my $user = shift(@posts);
  1195. my $id = delete $query->{id};
  1196. $query->{author} = $user->{user};
  1197. @posts = _post_helper( $query, $tags, $query->{user_acls} );
  1198. @posts = grep { $_->{id} ne $id } @posts;
  1199. unshift @posts, $user;
  1200. }
  1201. if ( !$is_admin ) {
  1202. return $query->{tpsgi}->notfound($query) unless @posts;
  1203. }
  1204. # Set the eTag so that we don't get a re-fetch
  1205. $query->{etag} = "$posts[0]{id}-$posts[0]{version}" if @posts;
  1206. #Correct page headers
  1207. my $ph = $themed ? _themed_title( $query->{route} ) : $query->{route};
  1208. return _rss( $query, $ph, \@posts ) if $fmt eq 'rss';
  1209. #XXX Is used by the sitemap, maybe just fix there?
  1210. my @post_aliases = map { $_->{local_href} } _get_series();
  1211. # List the available headers/footers
  1212. my $headers = Trog::Themes::themed_templates_in_dir( "headers", 'text/html', 1 );
  1213. my $footers = Trog::Themes::themed_templates_in_dir( "footers", 'text/html', 1 );
  1214. #XXX used to be post.css, but probably not good anymore?
  1215. my $styles = [];
  1216. # Build page title if it wasn't set by a wrapping sub
  1217. $query->{title} = "$query->{domain} : $query->{title}" if $query->{title} && $query->{domain};
  1218. $query->{title} ||= @$tags && $query->{domain} ? "$query->{domain} : @$tags" : undef;
  1219. #Handle paginator vars
  1220. $query->{limit} ||= 25;
  1221. my $limit = int( $query->{limit} );
  1222. my $now_year = ( localtime(time) )[5] + 1900;
  1223. my $oldest_year = $now_year - 20; #XXX actually find oldest post year
  1224. # Datasource pages page by offset, everything else by the created cursor.
  1225. # See _paginate_offset() for why they cannot share.
  1226. my ( $paginate_offset, $page_of, $pages_total ) = ( 0, 1, 1 );
  1227. if ( $query->{is_datasource} ) {
  1228. $paginate_offset = 1;
  1229. ( $page_of, $pages_total, @posts ) = _paginate_offset( $query, $limit, @posts );
  1230. }
  1231. my $older = !@posts ? 0 : $posts[-1]->{created};
  1232. $query->{failure} //= -1;
  1233. $query->{id} //= '';
  1234. my $newer = !@posts ? 0 : $posts[0]->{created};
  1235. #XXX messed up data has to be fixed unfortunately
  1236. @$tags = List::Util::uniq @$tags;
  1237. #Filter displaying visibility tags
  1238. my @visibuddies = qw{public unlisted private};
  1239. foreach my $post (@posts) {
  1240. @{ $post->{tags} } = grep {
  1241. my $tag = $_;
  1242. !grep { $tag eq $_ } @visibuddies
  1243. } @{ $post->{tags} };
  1244. }
  1245. #XXX note that we are explicitly relying on the first tag to be the ACL
  1246. my $aclselected = $tags->[0] || '';
  1247. my @acls = map {
  1248. $_->{selected} = $_->{aclname} eq $aclselected ? 'selected' : '';
  1249. $_
  1250. } _post_helper( {}, ['series'], $query->{user_acls} );
  1251. my $forms = Trog::Themes::themed_templates_in_dir( "forms", 'text/html', 1 );
  1252. my $edittype = $query->{primary_post} ? $query->{primary_post}->{child_form} : $query->{form};
  1253. my $tiled = $query->{primary_post} ? !$is_admin && $query->{primary_post}->{tiled} : 0;
  1254. state $data;
  1255. $data //= Trog::Data->new( Trog::Config::get() );
  1256. # Grab the rest of the tags to dump into the edit form
  1257. my @tags_all = $data->tags();
  1258. #Filter out the visibilities and special series tags
  1259. @tags_all = grep {
  1260. my $subj = $_;
  1261. scalar( grep { $_ eq $subj } qw{public private unlisted admin series about topbar} ) == 0
  1262. } @tags_all;
  1263. @posts = map {
  1264. my $subject = $_;
  1265. my @et = grep {
  1266. my $subj = $_;
  1267. grep { $subj eq $_ } @tags_all
  1268. } @{ $subject->{tags} };
  1269. @et = grep { $_ ne $aclselected } @et;
  1270. $_->{extra_tags} = \@et;
  1271. $_
  1272. } @posts;
  1273. my @et = List::MoreUtils::singleton( @$tags, @tags_all );
  1274. $query->{author} = $query->{primary_post}{user} // $posts[0]{user};
  1275. # Resolve each post's declared relations. One cache for the whole page, so
  1276. # 25 invoices don't mean 25 scans of every entities post.
  1277. my %relation_cache;
  1278. _enrich_post( $_, $query, \%relation_cache ) foreach @posts;
  1279. #XXX the only reason this is needed is due to direct=1
  1280. # Last thing before the data becomes a page: anything a type marks private
  1281. # goes no further for a reader who cannot edit. Deliberately after the
  1282. # datasource has run -- a datasource may well need a private field, like
  1283. # the connection uri it takes to reach a hypervisor, to produce anything
  1284. # at all.
  1285. if ( !$is_admin ) {
  1286. my %seen;
  1287. _redact_private( $_, \%seen ) foreach ( @posts, $query->{primary_post} );
  1288. }
  1289. #XXX is this even used?
  1290. my $content = Trog::Renderer->render(
  1291. template => 'posts.tx',
  1292. data => {
  1293. acls => \@acls,
  1294. can_edit => $is_admin,
  1295. forms => $forms,
  1296. post => { tags => $tags, extra_tags => \@et, form => $edittype, visibility => $user_visibility, addpost => 1 },
  1297. post_visibilities => \@visibuddies,
  1298. failure => $query->{failure},
  1299. to => $query->{to},
  1300. message => $query->{message} // ( $query->{failure} ? "Failed to add post!" : "Successfully added Post as $query->{id}" ),
  1301. direct => $direct,
  1302. title => $query->{title},
  1303. author => $query->{primary_post}{user} // $posts[0]{user},
  1304. primary_post => $query->{primary_post},
  1305. style => $query->{style},
  1306. posts => \@posts,
  1307. like => $query->{like},
  1308. in_series => exists $query->{in_series} || !!( $query->{route} =~ m/^\/series\// ),
  1309. route => $query->{route},
  1310. limit => $limit,
  1311. pages => $paginate_offset ? $pages_total > 1 : scalar(@posts) == $limit,
  1312. paginate_offset => $paginate_offset,
  1313. page => $page_of,
  1314. pages_total => $pages_total,
  1315. older => $older,
  1316. newer => $newer,
  1317. sizes => [ 25, 50, 100 ],
  1318. rss => !$query->{id} && !$query->{older},
  1319. tiled => $tiled,
  1320. category => $ph,
  1321. subhead => $query->{subhead},
  1322. headers => $headers,
  1323. footers => $footers,
  1324. years => [ reverse( $oldest_year .. $now_year ) ],
  1325. months => [ 0 .. 11 ],
  1326. embed => $query->{embed},
  1327. nochrome => $query->{nochrome},
  1328. },
  1329. contenttype => 'text/html',
  1330. component => 1,
  1331. );
  1332. # Something exploded
  1333. return $content if ref $content eq "ARRAY";
  1334. return $content if $direct;
  1335. return Trog::Routes::HTML::index( $query, $content, $styles );
  1336. }
  1337. sub _themed_title ($path) {
  1338. return $path unless %Theme::paths;
  1339. return $Theme::paths{$path} ? $Theme::paths{$path} : $path;
  1340. }
  1341. =head2 _redact_private($post, $seen)
  1342. Drop the fields a post type marks private, for a reader who isn't an editor.
  1343. Done to the data on its way to the template rather than with a guard inside the
  1344. template: a type can be rendered by more than one template, templates get
  1345. edited by people who did not write the type, and a value that was never handed
  1346. over cannot be printed by mistake. Recurses, because a post's relations are
  1347. posts too and carry their own private fields.
  1348. =cut
  1349. sub _redact_private ( $post, $seen = {} ) {
  1350. return unless Ref::Util::is_hashref($post);
  1351. # Relations can point back at what pulled them in.
  1352. return if $seen->{$post}++;
  1353. if ( $post->{form} ) {
  1354. delete $post->{$_} foreach @{ Trog::DataModule::private_fields_for( $post->{form} ) };
  1355. }
  1356. foreach my $value ( values(%$post) ) {
  1357. if ( Ref::Util::is_hashref($value) ) { _redact_private( $value, $seen ) }
  1358. elsif ( Ref::Util::is_arrayref($value) ) { _redact_private( $_, $seen ) foreach @$value }
  1359. }
  1360. return;
  1361. }
  1362. =head2 _datasource_posts($query, $posts)
  1363. Swap a series' children for whatever its datasource says they are, if its child
  1364. type declares one.
  1365. Returns the posts unchanged for every ordinary type, which is nearly all of
  1366. them -- this is a hook, not a detour every page takes.
  1367. =cut
  1368. sub _datasource_posts ( $query, $posts ) {
  1369. my $series = $query->{primary_post};
  1370. return @$posts unless Ref::Util::is_hashref($series) && $series->{child_form};
  1371. my $source = Trog::DataSource::for_type( $series->{child_form} );
  1372. return @$posts unless $source;
  1373. return @$posts unless Trog::DataSource::load($source);
  1374. my $builder = $source->can('posts');
  1375. if ( !$builder ) {
  1376. WARN("Datasource '$source' has no posts() to call");
  1377. return @$posts;
  1378. }
  1379. local $@;
  1380. my @synthesized = eval { $builder->( $series, $query ) };
  1381. if ($@) {
  1382. my $err = "$@";
  1383. $err =~ s/\n.*//s;
  1384. WARN("Datasource '$source' failed: $err");
  1385. return @$posts;
  1386. }
  1387. # The search the reader typed was applied to the datastore by get(), and
  1388. # then thrown away with the posts it filtered -- these are built fresh and
  1389. # have never been near it. Ask the source to apply it, or apply the default
  1390. # on its behalf, so that searching a datasource page searches the page.
  1391. my $filter = $source->can('filter') // \&Trog::DataSource::filter;
  1392. my @filtered = eval { $filter->( $query, @synthesized ) };
  1393. if ($@) {
  1394. my $err = "$@";
  1395. $err =~ s/\n.*//s;
  1396. WARN("Datasource '$source' could not filter its posts: $err");
  1397. @filtered = @synthesized;
  1398. }
  1399. # Pagination hands out page 2 of an order, so settle on one.
  1400. my $order = $source->can('order') // \&Trog::DataSource::order;
  1401. my @ordered = eval { $order->( $query, @filtered ) };
  1402. if ($@) {
  1403. my $err = "$@";
  1404. $err =~ s/\n.*//s;
  1405. WARN("Datasource '$source' could not order its posts: $err");
  1406. @ordered = @filtered;
  1407. }
  1408. # Which the paginator below reads, since these page by offset rather than by
  1409. # the created cursor stored posts use.
  1410. $query->{is_datasource} = 1;
  1411. # A source which cannot say when its posts go stale must not have its pages
  1412. # saved as statics: nothing else would ever invalidate them, so the cached
  1413. # copy could never become right again. See Trog::DataSource::cacheable.
  1414. $query->{nocache} = 1 unless Trog::DataSource::cacheable($source);
  1415. return @ordered;
  1416. }
  1417. =head2 _paginate_offset($query, $limit, @posts) = ($page, $pages_total, @page_of_posts)
  1418. The slice of @posts the reader asked for, by page number.
  1419. Stored posts page by a cursor: the datastore is asked for the $limit posts older
  1420. than a timestamp, and the paginator hands back the oldest one on the page as the
  1421. next cursor. That is the right shape when the alternative is reading the whole
  1422. datastore to skip most of it.
  1423. It is the wrong shape for a datasource, which has already built every post by
  1424. the time we see them -- and it does not work at all for one which stamps them
  1425. all with the time it built them, as libvirt guests are. Every cursor on such a
  1426. page is the same instant, so Prev asks for everything older than now and gets
  1427. nothing. Offset costs nothing here and moves.
  1428. =cut
  1429. sub _paginate_offset ( $query, $limit, @posts ) {
  1430. $limit = 25 if !$limit || $limit < 1;
  1431. my $total = scalar(@posts);
  1432. my $pages_total = $total ? int( ( $total + $limit - 1 ) / $limit ) : 1;
  1433. # Straight off a query string, so digits only -- the same coercion older and
  1434. # newer get, rather than int() warning its way through a word. Past the end
  1435. # shows the last page rather than an empty one, which is what a stale
  1436. # bookmark deserves.
  1437. ( my $wanted = $query->{page} // '' ) =~ s/[^0-9]//g;
  1438. my $page = length($wanted) ? int($wanted) : 1;
  1439. $page = 1 if $page < 1;
  1440. $page = $pages_total if $page > $pages_total;
  1441. my $offset = ( $page - 1 ) * $limit;
  1442. my @slice = $offset < $total ? @posts[ $offset .. List::Util::min( $offset + $limit, $total ) - 1 ] : ();
  1443. return ( $page, $pages_total, @slice );
  1444. }
  1445. =head2 _search_language($query)
  1446. What to tell the reader the search box searches, as (language, help link).
  1447. The data model's answer describes what it can do to the datastore, which is the
  1448. right answer for nearly every page. It is the wrong one for a page whose posts
  1449. are built by a datasource rather than read out of the datastore, so such a page
  1450. gets to say so.
  1451. =cut
  1452. sub _search_language ($query) {
  1453. state $data;
  1454. $data //= Trog::Data->new( Trog::Config::get() );
  1455. my $series = $query->{primary_post};
  1456. my $source = Ref::Util::is_hashref($series) && $series->{child_form} ? Trog::DataSource::for_type( $series->{child_form} ) : undef;
  1457. return ( $data->lang(), $data->help() ) unless $source && Trog::DataSource::load($source);
  1458. my $lang = $source->can('lang') // \&Trog::DataSource::lang;
  1459. my $help = $source->can('help') // \&Trog::DataSource::help;
  1460. return ( scalar $lang->(), scalar $help->() );
  1461. }
  1462. sub _post_helper ( $query, $tags, $acls ) {
  1463. state $data;
  1464. $data //= Trog::Data->new( Trog::Config::get() );
  1465. $query->{page} ||= 1;
  1466. $query->{limit} ||= 25;
  1467. my @d = $data->get(
  1468. older => $query->{older},
  1469. newer => $query->{newer},
  1470. page => int( $query->{page} ),
  1471. limit => int( $query->{limit} ),
  1472. tags => $tags,
  1473. exclude_tags => $query->{exclude_tags},
  1474. acls => $acls,
  1475. aclname => $query->{aclname},
  1476. like => $query->{like},
  1477. author => $query->{author},
  1478. id => $query->{id},
  1479. version => $query->{version},
  1480. form => $query->{form},
  1481. );
  1482. return map {
  1483. my $subj = $_;
  1484. $subj->{local_href} = "/secure$_->{local_href}";
  1485. $subj
  1486. } @d if $query->{user};
  1487. return @d;
  1488. }
  1489. =head2 sitemap
  1490. Return the sitemap index unless the static or a set of dynamic routes is requested.
  1491. We have a maximum of 99,990,000 posts we can make under this model
  1492. As we have 10,000 * 10,000 posts which are indexable via the sitemap format.
  1493. 1 top level index slot (10k posts) is taken by our static routes, the rest will be /posts.
  1494. Passing ?xml=1 will result in an appropriate sitemap.xml instead.
  1495. This is used to generate the static sitemaps as expected by search engines.
  1496. Passing compressed=1 will gzip the output.
  1497. =cut
  1498. sub sitemap ($query) {
  1499. state $data;
  1500. $data //= Trog::Data->new( Trog::Config::get() );
  1501. state $etag = "sitemap-" . time();
  1502. my ( @to_map, $is_index, $route_type );
  1503. my $warning = '';
  1504. $query->{map} //= '';
  1505. if ( $query->{map} eq 'static' ) {
  1506. # Return the map of static routes
  1507. $route_type = 'Static Routes';
  1508. @to_map = grep { !defined $routes{$_}->{captures} && !$routes{$_}->{auth} && !$routes{$_}->{noindex} && !$routes{$_}->{nomap} } keys(%routes);
  1509. }
  1510. elsif ( !$query->{map} ) {
  1511. # Return the index instead
  1512. @to_map = ('static');
  1513. my $tot = $data->count();
  1514. my $size = 50000;
  1515. my $pages = int( $tot / $size ) + ( ( $tot % $size ) ? 1 : 0 );
  1516. # Truncate pages at 10k due to standard
  1517. my $clamped = $pages > 49999 ? 49999 : $pages;
  1518. $warning = "More posts than possible to represent in sitemaps & index! Old posts have been truncated." if $pages > 49999;
  1519. foreach my $page ( $clamped .. 1 ) {
  1520. push( @to_map, "$page" );
  1521. }
  1522. $is_index = 1;
  1523. }
  1524. else {
  1525. $route_type = "Posts: Page $query->{map}";
  1526. # Return the map of the particular range of dynamic posts
  1527. $query->{limit} = 50000;
  1528. $query->{page} = $query->{map};
  1529. @to_map = _post_helper( $query, [], ['public'] );
  1530. }
  1531. if ( $query->{xml} ) {
  1532. DEBUG("RENDER SITEMAP XML");
  1533. my $sm;
  1534. my $xml_date = time();
  1535. my $fmt = "xml";
  1536. $fmt .= ".gz" if $query->{compressed};
  1537. if ( !$query->{map} ) {
  1538. require WWW::SitemapIndex::XML;
  1539. $sm = WWW::SitemapIndex::XML->new();
  1540. foreach my $url (@to_map) {
  1541. $sm->add(
  1542. loc => "http://$query->{domain}/sitemap/$url.$fmt",
  1543. lastmod => $xml_date,
  1544. );
  1545. }
  1546. }
  1547. else {
  1548. require WWW::Sitemap::XML;
  1549. $sm = WWW::Sitemap::XML->new();
  1550. my $changefreq = $query->{map} eq 'static' ? 'monthly' : 'daily';
  1551. foreach my $url (@to_map) {
  1552. my $true_uri = "http://$query->{domain}$url";
  1553. if ( ref $url eq 'HASH' ) {
  1554. my $is_user_page = grep { $_ eq 'about' } @{ $url->{tags} };
  1555. $true_uri = "http://$query->{domain}/posts/$url->{id}";
  1556. $true_uri = "http://$query->{domain}/users/$url->{title}" if $is_user_page;
  1557. }
  1558. my %out = (
  1559. loc => $true_uri,
  1560. lastmod => $xml_date,
  1561. mobile => 1,
  1562. changefreq => $changefreq,
  1563. priority => 1.0,
  1564. );
  1565. if ( ref $url eq 'HASH' ) {
  1566. #add video & preview image if applicable
  1567. $out{images} = [
  1568. {
  1569. loc => "http://$query->{domain}$url->{href}",
  1570. caption => $url->{data},
  1571. title => substr( $url->{title}, 0, 100 ),
  1572. }
  1573. ]
  1574. if $url->{is_image};
  1575. # Truncate descriptions
  1576. my $desc = substr( $url->{data}, 0, 2048 ) || '';
  1577. my $href = $url->{href} || '';
  1578. my $preview = $url->{preview} || '';
  1579. my $domain = $query->{domain} || '';
  1580. $out{videos} = [
  1581. {
  1582. content_loc => "http://$domain$href",
  1583. thumbnail_loc => "http://$domain$preview",
  1584. title => substr( $url->{title}, 0, 100 ) || '',
  1585. description => $desc,
  1586. }
  1587. ]
  1588. if $url->{is_video};
  1589. }
  1590. $sm->add(%out);
  1591. }
  1592. }
  1593. my $xml = $sm->as_xml();
  1594. require IO::String;
  1595. my $buf = IO::String->new();
  1596. my $ct = 'application/xml';
  1597. $xml->toFH( $buf, 0 );
  1598. seek $buf, 0, 0;
  1599. if ( $query->{compressed} ) {
  1600. require IO::Compress::Gzip;
  1601. my $compressed = IO::String->new();
  1602. IO::Compress::Gzip::gzip( $buf => $compressed );
  1603. $ct = 'application/gzip';
  1604. $buf = $compressed;
  1605. seek $compressed, 0, 0;
  1606. }
  1607. #XXX This is one of the few exceptions where we don't use finish_render, as it *requires* gzip.
  1608. return [ 200, [ "Content-type" => $ct, 'ETag' => $etag ], $buf ];
  1609. }
  1610. @to_map = sort @to_map unless $is_index;
  1611. my $styles = ['sitemap.css'];
  1612. $query->{title} = "$query->{domain} : Sitemap";
  1613. $query->{template} = 'sitemap.tx',
  1614. $query->{to_map} = \@to_map,
  1615. $query->{is_index} = $is_index,
  1616. $query->{route_type} = $route_type,
  1617. $query->{etag} = $etag;
  1618. return Trog::Routes::HTML::index( $query, undef, $styles );
  1619. }
  1620. sub _rss ( $query, $subtitle, $posts ) {
  1621. require XML::RSS;
  1622. my $rss = XML::RSS->new( version => '2.0', stylesheet => '/styles/rss-style.xsl' );
  1623. my $now = DateTime->from_epoch( epoch => time() );
  1624. my $port = $query->{port} ? ":$query->{port}" : '';
  1625. $rss->channel(
  1626. title => "$query->{domain}",
  1627. subtitle => $subtitle,
  1628. link => "http://$query->{domain}$port/$query->{route}?format=xml",
  1629. language => 'en', #TODO localization
  1630. description => "$query->{domain} : $query->{route}",
  1631. pubDate => $now,
  1632. lastBuildDate => $now,
  1633. );
  1634. $rss->image(
  1635. title => $query->{domain},
  1636. url => "/favicon.ico",
  1637. link => "http://$query->{domain}$port",
  1638. width => 32,
  1639. height => 32,
  1640. description => "$query->{domain} favicon",
  1641. );
  1642. foreach my $post (@$posts) {
  1643. my $url = "http://$query->{domain}$port$post->{local_href}";
  1644. _post2rss( $rss, $url, $post );
  1645. next unless ref $post->{aliases} eq 'ARRAY';
  1646. foreach my $alias ( @{ $post->{aliases} } ) {
  1647. $url = "http://$query->{domain}$port$alias";
  1648. _post2rss( $rss, $url, $post );
  1649. }
  1650. }
  1651. return Trog::Renderer->render(
  1652. template => 'raw.tx',
  1653. data => {
  1654. etag => $query->{etag},
  1655. body => encode_utf8( $rss->as_string ),
  1656. scheme => $query->{scheme},
  1657. },
  1658. headers => { 'Content-Disposition' => 'inline; filename="rss.xml"' },
  1659. #XXX if you do the "proper" content-type of application/rss+xml, browsers download rather than display.
  1660. contenttype => "text/xml",
  1661. code => 200,
  1662. );
  1663. }
  1664. sub _post2rss ( $rss, $url, $post ) {
  1665. $rss->add_item(
  1666. title => $post->{title},
  1667. permaLink => $url,
  1668. link => $url,
  1669. enclosure => { url => $url, type => "text/html" },
  1670. description => "<![CDATA[$post->{data}]]>",
  1671. pubDate => DateTime->from_epoch( epoch => $post->{created} ), #TODO format like Thu, 23 Aug 1999 07:00:00 GMT
  1672. author => $post->{user}, #TODO translate to "email (user)" format
  1673. );
  1674. }
  1675. =head2 manual
  1676. Implements the /manual and /lib/* routes.
  1677. Basically a thin wrapper around Pod::Html.
  1678. =cut
  1679. sub manual ($query) {
  1680. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  1681. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  1682. require Pod::Html;
  1683. require Capture::Tiny;
  1684. #Fix links from Pod::HTML
  1685. $query->{module} =~ s/\.html$//g if $query->{module};
  1686. $query->{failure} //= -1;
  1687. my $infile = $query->{module} ? "$query->{module}.pm" : 'tCMS/Manual.pod';
  1688. my $found = 0;
  1689. foreach my $libdir (@INC) {
  1690. $found = $libdir if -f "$libdir/$infile"; ## no critic (ProhibitFiletest_f) -- which @INC dir supplies the pod
  1691. last if $found;
  1692. }
  1693. return $query->{tpsgi}->notfound($query) unless $found;
  1694. my $content = capture { Pod::Html::pod2html( qw{--podpath=lib --podroot=.}, "--infile=$found/$infile" ) };
  1695. return Trog::Routes::HTML::index(
  1696. {
  1697. title => 'tCMS Manual',
  1698. theme_dir => Trog::Themes::td(),
  1699. content => $content,
  1700. template => 'manual.tx',
  1701. is_admin => 1,
  1702. %$query,
  1703. },
  1704. undef,
  1705. ['post.css'],
  1706. );
  1707. }
  1708. =head2 processed
  1709. Implements /processed.
  1710. The "we got it, go check your email" page, shown after a request that finishes
  1711. out of band -- password and TOTP resets, chiefly. Deliberately says nothing
  1712. about whether the account existed.
  1713. =cut
  1714. sub processed ($query) {
  1715. return Trog::Routes::HTML::index(
  1716. {
  1717. title => "Your request has been processed",
  1718. theme_dir => Trog::Themes::td(),
  1719. },
  1720. "Your request has been processed.<br /><br />You will recieve subsequent communications about this matter via means you have provided earlier.",
  1721. ['post.css']
  1722. );
  1723. }
  1724. =head2 metrics
  1725. Implements /metrics. Admin only.
  1726. Renders the request metrics dashboard. The page pulls its actual numbers from
  1727. /api/requests_per over XHR and draws them with chart.js, so there's nothing to
  1728. compute here.
  1729. =cut
  1730. sub metrics ($query) {
  1731. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  1732. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  1733. $query->{failure} //= -1;
  1734. return Trog::Routes::HTML::index(
  1735. {
  1736. title => 'tCMS Metrics',
  1737. theme_dir => Trog::Themes::td(),
  1738. template => 'metrics.tx',
  1739. is_admin => 1,
  1740. %$query,
  1741. },
  1742. undef,
  1743. ['post.css'],
  1744. ['chart.js'],
  1745. );
  1746. }
  1747. =head2 sessions
  1748. Implements /sessions. Admin only.
  1749. The session audit log: logins, logouts and failures, most recent first.
  1750. Filterable by username and event type via the filter_user and filter_event
  1751. parameters, and capped by limit (100 by default).
  1752. Timestamps are formatted here rather than in the template, and session ids are
  1753. truncated to their first 8 characters -- there's no reason to put a live
  1754. session id on a page.
  1755. =cut
  1756. sub sessions ($query) {
  1757. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  1758. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  1759. my $limit = int( $query->{limit} // 100 );
  1760. my $events = Trog::Auth::audit_log(
  1761. limit => $limit,
  1762. username => $query->{filter_user} // '',
  1763. event_type => $query->{filter_event} // '',
  1764. );
  1765. # Format timestamps for display
  1766. @$events = map {
  1767. my $e = {%$_};
  1768. my @t = localtime( $e->{event_time} );
  1769. $e->{event_ts} = sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $t[5] + 1900, $t[4] + 1, $t[3], $t[2], $t[1], $t[0] );
  1770. $e->{session_short} = $e->{session_id} ? substr( $e->{session_id}, 0, 8 ) . '...' : '';
  1771. $e
  1772. } @$events;
  1773. return Trog::Routes::HTML::index(
  1774. {
  1775. title => 'tCMS Session Audit Log',
  1776. theme_dir => Trog::Themes::td(),
  1777. template => 'sessions.tx',
  1778. is_admin => 1,
  1779. events => $events,
  1780. limit => $limit,
  1781. filter_user => $query->{filter_user} // '',
  1782. filter_event => $query->{filter_event} // '',
  1783. %$query,
  1784. },
  1785. undef,
  1786. ['post.css'],
  1787. );
  1788. }
  1789. # The canned includes the wizard is willing to splice into a generated form.
  1790. # Deliberately a whitelist keyed on the checkbox name -- the include name ends
  1791. # up interpolated straight into a template file, so it can never come from the
  1792. # submitted data.
  1793. our %wizard_includes = (
  1794. inc_preview => 'preview.tx',
  1795. inc_visibility => 'visibility.tx',
  1796. inc_acls => 'acls.tx',
  1797. inc_tags => 'tags.tx',
  1798. inc_aliases => 'aliases.tx',
  1799. inc_attachments => 'attachments.tx',
  1800. );
  1801. # Emitted in this order, so generated forms read like the hand-written ones.
  1802. our @wizard_include_order = qw{inc_preview inc_visibility inc_acls inc_tags inc_aliases inc_attachments};
  1803. # Every checkbox on the wizard, and which of them start out ticked.
  1804. # Every post has a title, a visibility and a set of acls, so a type does not
  1805. # get to decide whether its editor collects them -- see post_wizard_save.
  1806. our @wizard_mandatory = qw{inc_title_input inc_visibility inc_acls};
  1807. our @wizard_optional_includes = grep {
  1808. my $include = $_;
  1809. !grep { $include eq $_ } @wizard_mandatory
  1810. } @wizard_include_order;
  1811. our @wizard_checkboxes = ( qw{wrapper inc_post_title inc_post_tags}, @wizard_optional_includes, 'overwrite' );
  1812. our %wizard_checked_by_default = map { $_ => 1 } qw{wrapper inc_post_title inc_post_tags inc_preview inc_tags inc_aliases};
  1813. # Which HTML input the wizard emits, and what that field is in the sidecar's
  1814. # OpenAPIv3 schema. No 'file': _process() in Trog::DataModule only knows how to
  1815. # turn the four hardcoded upload fields into hrefs, so a custom file field would
  1816. # store an HTTP::Body::OctetStream that nothing ever picks up. Point folks at
  1817. # the attachment uploader instead.
  1818. our %wizard_field_types = (
  1819. relation => { type => 'relation' },
  1820. text => { type => 'string' },
  1821. url => { type => 'string' },
  1822. date => { type => 'string' },
  1823. textarea => { type => 'string' },
  1824. number => { type => 'integer' },
  1825. checkbox => { type => 'boolean' },
  1826. );
  1827. # Field names which aren't in %schema but would still collide with something.
  1828. our @wizard_reserved = qw{app to form data_is_array addpost extra_tags is_image is_video is_audio is_profile content_type version};
  1829. =head2 guest_screenshot
  1830. Implements GET /guest/screenshot/$hypervisor/$guest. Admin only.
  1831. Served as its own route rather than inlined into the guest listing, so that a
  1832. page of guests renders immediately and the browser fetches the console
  1833. captures in parallel instead of us taking them one after another.
  1834. =cut
  1835. sub guest_screenshot ($query) {
  1836. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  1837. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  1838. my $hypervisor = _guest_hypervisor( $query, $query->{hypervisor} );
  1839. return $query->{tpsgi}->notfound($query) unless $hypervisor;
  1840. require Trog::DataSource::Virt;
  1841. my ( $path, $why ) = Trog::DataSource::Virt::screenshot( $hypervisor, $query->{guest} );
  1842. if ( !$path ) {
  1843. WARN("Could not screenshot '$query->{guest}': $why");
  1844. return $query->{tpsgi}->notfound($query);
  1845. }
  1846. return $query->{tpsgi}->serve(
  1847. $query->{route}, $path, $query->{start}, $query->{streaming},
  1848. $query->{ranges}, $query->{last_fetched}, $query->{deflate},
  1849. );
  1850. }
  1851. =head2 guest_act
  1852. Implements POST /guest/act. Admin only.
  1853. Powering a guest on or off, snapshotting it, or destroying it. Separate from
  1854. everything that merely reads a guest, and the only route which reaches
  1855. Trog::DataSource::Virt::act.
  1856. =cut
  1857. sub guest_act ($query) {
  1858. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  1859. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  1860. my $to = $query->{to} || '/';
  1861. my $hypervisor = _guest_hypervisor( $query, $query->{hypervisor} );
  1862. return _feedback_redirect( $query, $to, 1, 'No such hypervisor.' ) unless $hypervisor;
  1863. require Trog::DataSource::Virt;
  1864. # Same reason the screenshot route captures 'guest': a form field called
  1865. # 'domain' would be overwritten by the router with the request's host.
  1866. my ( $ok, $message ) = Trog::DataSource::Virt::act(
  1867. $query->{action}, $hypervisor->{conn_uri}, $query->{guest}, $query->{user},
  1868. );
  1869. return _feedback_redirect( $query, $to, $ok ? 0 : 1, "$query->{guest}: $message" );
  1870. }
  1871. # The hypervisor post a guest route was asked about. Looked up rather than
  1872. # taken from the request: the connection URI is not something a form gets to
  1873. # hand us.
  1874. sub _guest_hypervisor ( $query, $id ) {
  1875. return undef unless $id;
  1876. my @found = _post_helper( { id => $id }, [], $query->{user_acls} );
  1877. return undef unless @found && ( $found[0]{conn_uri} // '' );
  1878. return $found[0];
  1879. }
  1880. =head2 post_wizard
  1881. Implements /admin/wyzzerdd. Admin only.
  1882. The post type wizard: lists the post types which currently exist and offers a
  1883. form for building a new one. See post_wizard_save() for what happens when that
  1884. form comes back.
  1885. Re-rendered by post_wizard_save() on both success and failure, so it keeps the
  1886. submitted values and checkbox states rather than making the admin retype
  1887. everything after a rejected submission.
  1888. =cut
  1889. sub post_wizard ($query) {
  1890. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  1891. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  1892. $query->{failure} //= -1;
  1893. # Get the existing post types
  1894. my $forms = Trog::Themes::themed_templates_in_dir( "forms", 'text/html', 1 );
  1895. # Sticky checkboxes. Nothing is submitted on a fresh GET, so fall back to
  1896. # the defaults rather than rendering everything unticked.
  1897. my %checked = map { $_ => ( $query->{wizard_submitted} ? $query->{$_} : $wizard_checked_by_default{$_} ) ? 'checked' : '' } @wizard_checkboxes;
  1898. return Trog::Routes::HTML::index(
  1899. {
  1900. # Sticky form values first, so a failed submission doesn't lose the
  1901. # admin's work -- but everything the template actually depends on is
  1902. # set *after* this, as the whole of %$query is attacker controlled.
  1903. %$query,
  1904. title => 'tCMS Post Wizard',
  1905. theme_dir => Trog::Themes::td(),
  1906. template => 'post_wizard.tx',
  1907. is_admin => 1,
  1908. forms => $forms,
  1909. forms_dir => Trog::Themes::forms_dir(),
  1910. datasources => _get_datasources(),
  1911. datasource => _wizard_scalar( $query->{datasource} ),
  1912. checked => \%checked,
  1913. name => _wizard_scalar( $query->{name} ),
  1914. title_placeholder => _wizard_scalar( $query->{title_placeholder} ),
  1915. body_form => _wizard_scalar( $query->{body_form} ),
  1916. display => _wizard_scalar( $query->{display} ),
  1917. failure => $query->{failure},
  1918. message => $query->{message} // '',
  1919. to => $query->{to} // '',
  1920. },
  1921. undef,
  1922. ['post.css'],
  1923. );
  1924. }
  1925. =head2 post_wizard_save
  1926. Implements POST /admin/wyzzerdd/save.
  1927. Writes a new post type template into whichever forms directory is currently in
  1928. use, along with a JSON sidecar naming its custom fields so that
  1929. Trog::DataModule::schema_for will merge it over the base post schema, so the
  1930. type's own fields survive validation at save time.
  1931. =cut
  1932. sub post_wizard_save ($query) {
  1933. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  1934. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  1935. # Anchored capture rather than a substitution, so anything containing a '/'
  1936. # or a '..' simply fails to match instead of being silently scrubbed into
  1937. # something that still escapes the directory.
  1938. my ($name) = _wizard_scalar( $query->{name} ) =~ m/^([A-Za-z0-9_-]+)$/;
  1939. return _wizard_fail( $query, "Post type names may only contain letters, numbers, '-' and '_'." ) unless $name;
  1940. # Whichever forms dir is in use: the theme's if it has one, the stock one
  1941. # otherwise. Never mkdir it -- silently conjuring a forms/ dir inside a
  1942. # theme is not something an admin asked for by pressing this button.
  1943. my $dir = Trog::Themes::forms_dir();
  1944. # -d only. A -w here would check mode bits against our uid, which says
  1945. # nothing about read-only mounts, ACLs or immutable flags -- and it is the
  1946. # files, not the directory, that get written. The eval around the two
  1947. # spew_utf8 calls below reports the real errno when a write actually fails.
  1948. return _wizard_fail( $query, "Forms directory '$dir' does not exist." ) unless -d $dir;
  1949. my $tx_file = "$dir/$name.tx";
  1950. my $json_file = "$dir/$name.json";
  1951. my $overwriting = -e $tx_file;
  1952. return _wizard_fail( $query, "Post type '$name.tx' already exists. Tick 'Overwrite' if you meant to replace it." )
  1953. if $overwriting && !$query->{overwrite};
  1954. # Whitelisted against what is actually on disk: this name goes into a
  1955. # sidecar and is require()d later, so a regex is not enough.
  1956. my $datasource = _wizard_scalar( $query->{datasource} );
  1957. if ( $datasource && !grep { $_ eq $datasource } @{ _get_datasources() } ) {
  1958. return _wizard_fail( $query, "'$datasource' is not a datasource I can find." );
  1959. }
  1960. my $fields = _wizard_fields($query);
  1961. # Not up for discussion. A post with no visibility gets an undef pushed
  1962. # into its tags by _process, which is a tag nothing matches -- so the post
  1963. # is invisible to everyone but an admin, for no reason anybody chose. A
  1964. # title and a set of acls are equally non-negotiable: every other form has
  1965. # them, and a type whose editor cannot set them produces posts nobody can
  1966. # title or restrict.
  1967. $query->{$_} = 1 foreach @wizard_mandatory;
  1968. my @includes = grep { $query->{$_} } @wizard_include_order;
  1969. my $body_form = _wizard_scalar( $query->{body_form} ) eq 'form_multi.tx' ? 'form_multi.tx' : 'form_common.tx';
  1970. my $spec = _wizard_sidecar( $name, $fields, \@includes, $body_form, $datasource );
  1971. local $@;
  1972. eval {
  1973. Path::Tiny::path($tx_file)->spew_utf8( _wizard_template( $query, $fields, \@includes, $body_form, $datasource ) );
  1974. Path::Tiny::path($json_file)->spew_utf8( JSON::MaybeXS->new( pretty => 1, canonical => 1 )->encode($spec) );
  1975. 1;
  1976. } or return _wizard_fail( $query, "Failed to write post type '$name': $@" );
  1977. INFO("Post type '$name' written to $tx_file by $query->{user}");
  1978. # Whatever the data model makes of an indexed field, it wants the whole
  1979. # picture rather than this type's share of it -- see _wizard_reindex.
  1980. my $index_error = _wizard_reindex( $name, $fields );
  1981. return _wizard_fail( $query, "Post type '$name' was written, but indexing its fields failed: $index_error" ) if $index_error;
  1982. # Nothing else is needed to publish a new type: themed_templates_in_dir()
  1983. # re-reads
  1984. # the directory every call, and Xslate resolves includes lazily, so both the
  1985. # wizard's own list and the series child_form dropdown pick it up on the
  1986. # next request without a restart. Overwriting an *existing* type is another
  1987. # matter -- that changes how already-rendered anonymous pages should look.
  1988. if ($overwriting) {
  1989. $query->{tpsgi}->add_post_close_callback(
  1990. sub {
  1991. $query->{tpsgi}->invalidate_renders('html');
  1992. }
  1993. );
  1994. }
  1995. return post_wizard(
  1996. {
  1997. %$query,
  1998. wizard_submitted => 1,
  1999. failure => 0,
  2000. message => "Created post type '$name.tx'.",
  2001. to => '/admin/wyzzerdd',
  2002. }
  2003. );
  2004. }
  2005. =head2 _short_error($error) = STRING
  2006. An error as a person should see it: the first line, without the file and line
  2007. number Perl glued onto the end.
  2008. add() dies with an arrayref of validation errors precisely so that they arrive
  2009. clean, but a failure further down -- a write that could not land, say -- is an
  2010. ordinary string die, and Carp::Always decorates those with the entire call
  2011. stack. That whole thing used to go to the user.
  2012. The undecorated original is logged; this is only what reaches the banner.
  2013. =cut
  2014. sub _short_error ( $error = '' ) {
  2015. $error = "$error";
  2016. $error =~ s/\n.*//s;
  2017. $error =~ s/\s+at\s+\S+\s+line\s+\d+\.?\s*$//;
  2018. return $error;
  2019. }
  2020. =head2 _wizard_reindex($name, $fields)
  2021. Bring the data model's idea of which custom fields are queryable into line with
  2022. what the post types on disk declare.
  2023. Every type's indexed fields, not just the one that was saved: two types can
  2024. declare a field of the same name, so reconciling against one of them alone would
  2025. have saving that type drop the other's index.
  2026. The type being saved is taken from the field list in hand rather than by reading
  2027. back the sidecar just written. schema_for() caches on the mtime of the forms
  2028. directory, which has one second of resolution, so a read here can legitimately
  2029. land on the generation before this save.
  2030. Returns the error if there was one, and nothing if all was well. A failure to
  2031. index is not a failure to create the post type: the type is written by this
  2032. point, works, and is only missing an optimisation.
  2033. =cut
  2034. sub _wizard_reindex ( $name, $fields ) {
  2035. my %indexed = map { $_->{name} => 1 } grep { $_->{indexed} } @$fields;
  2036. my $this_type = "$name.tx";
  2037. foreach my $form ( @{ Trog::Themes::themed_templates_in_dir( 'forms', 'text/html', 1 ) } ) {
  2038. next if $form eq $this_type;
  2039. $indexed{$_} = 1 foreach @{ Trog::DataModule::indexed_fields_for($form) };
  2040. }
  2041. local $@;
  2042. eval {
  2043. my $data = Trog::Data->new( Trog::Config::get() );
  2044. $data->index_fields( sort keys %indexed );
  2045. 1;
  2046. } or do {
  2047. my $error = $@;
  2048. WARN("Could not reconcile custom field indexes: $error");
  2049. return $error;
  2050. };
  2051. return;
  2052. }
  2053. # Repeated params arrive as arrayrefs; anywhere we want one string, insist on one string.
  2054. sub _wizard_scalar ( $value = '' ) {
  2055. return '' if !defined $value || ref $value;
  2056. return $value;
  2057. }
  2058. sub _wizard_fail ( $query, $message ) {
  2059. WARN($message);
  2060. return post_wizard(
  2061. {
  2062. %$query,
  2063. wizard_submitted => 1,
  2064. failure => 1,
  2065. message => $message,
  2066. }
  2067. );
  2068. }
  2069. =head2 _kolon_safe
  2070. Make a submitted string safe to splice into a generated Kolon template.
  2071. Newlines have to go, as a ':' at the start of a line is a Kolon directive, and
  2072. the angle brackets have to go so that '<:' can never be reassembled.
  2073. =cut
  2074. sub _kolon_safe ( $string = '' ) {
  2075. $string = _wizard_scalar($string);
  2076. $string =~ s/[\r\n]+/ /g;
  2077. $string =~ s/&/&amp;/g;
  2078. $string =~ s/</&lt;/g;
  2079. $string =~ s/>/&gt;/g;
  2080. $string =~ s/"/&quot;/g;
  2081. return $string;
  2082. }
  2083. =head2 _wizard_fields
  2084. Zip the parallel param_* arrays submitted by the wizard back into a list of
  2085. field definitions. Every row always submits exactly one of each param, so the
  2086. arrays stay aligned even when a row is left blank.
  2087. =cut
  2088. sub _wizard_fields ($query) {
  2089. my $names = Trog::Utils::coerce_array( $query->{param_name} );
  2090. my $types = Trog::Utils::coerce_array( $query->{param_type} );
  2091. my $labels = Trog::Utils::coerce_array( $query->{param_label} );
  2092. my $phs = Trog::Utils::coerce_array( $query->{param_placeholder} );
  2093. my $reqs = Trog::Utils::coerce_array( $query->{param_required} );
  2094. my $rforms = Trog::Utils::coerce_array( $query->{param_relation_form} );
  2095. my $rmodes = Trog::Utils::coerce_array( $query->{param_relation_mode} );
  2096. my $private = Trog::Utils::coerce_array( $query->{param_private} );
  2097. my $indexed = Trog::Utils::coerce_array( $query->{param_indexed} );
  2098. my ( @fields, %seen );
  2099. foreach my $index ( 0 .. $#$names ) {
  2100. my ($fname) = lc( _wizard_scalar( $names->[$index] ) ) =~ m/^([a-z][a-z0-9_]{0,31})$/;
  2101. # Blank and bogus rows just get dropped.
  2102. next unless $fname;
  2103. next if $seen{$fname}++;
  2104. # Never let a custom field shadow a core post attribute.
  2105. next if exists $Trog::DataModule::post_schema{properties}{$fname};
  2106. next if grep { $fname eq $_ } @wizard_reserved;
  2107. my $type = _wizard_scalar( $types->[$index] );
  2108. $type = 'text' unless $wizard_field_types{$type};
  2109. # A relation is only a relation if it names a target type that looks
  2110. # like one; anything else falls back to being a plain text field.
  2111. my ($rform) = _wizard_scalar( $rforms->[$index] ) =~ m/^([A-Za-z0-9_-]+\.tx)$/;
  2112. my $rmode = _wizard_scalar( $rmodes->[$index] ) eq 'all' ? 'all' : 'one';
  2113. $type = 'text' if $type eq 'relation' && !$rform;
  2114. push(
  2115. @fields,
  2116. {
  2117. name => $fname,
  2118. type => $type,
  2119. label => _kolon_safe( $labels->[$index] ) || ucfirst($fname),
  2120. placeholder => _kolon_safe( $phs->[$index] ),
  2121. required => _wizard_scalar( $reqs->[$index] ) ? 1 : 0,
  2122. relation_form => $rform,
  2123. relation_mode => $rmode,
  2124. private => _wizard_scalar( $private->[$index] ) ? 1 : 0,
  2125. indexed => _wizard_scalar( $indexed->[$index] ) ? 1 : 0,
  2126. }
  2127. );
  2128. }
  2129. return \@fields;
  2130. }
  2131. =head2 _wizard_sidecar
  2132. Turn the wizard's field list into the post type's JSON sidecar: an OpenAPIv3
  2133. object schema describing what this type ingests, with the wizard's own UI
  2134. metadata hung off x- extension keys so there's only one file to keep in sync.
  2135. Trog::DataModule::schema_for() merges this over the base post schema.
  2136. =cut
  2137. sub _wizard_sidecar ( $name, $fields, $includes, $body_form, $datasource = '' ) {
  2138. my %properties;
  2139. my @required;
  2140. my %relations;
  2141. foreach my $field (@$fields) {
  2142. # A relation pulling in every post of its target type isn't a field the
  2143. # post stores at all -- it is purely something injected at render, so
  2144. # it gets a relations entry and no property.
  2145. if ( $field->{type} eq 'relation' && $field->{relation_mode} eq 'all' ) {
  2146. $relations{ $field->{name} } = { form => $field->{relation_form} };
  2147. next;
  2148. }
  2149. my $property = { %{ $wizard_field_types{ $field->{type} } } };
  2150. $property->{'x-tcms-input'} = $field->{type};
  2151. $property->{'x-tcms-label'} = $field->{label};
  2152. $property->{'x-tcms-placeholder'} = $field->{placeholder} if length $field->{placeholder};
  2153. # Only written when it is true: a field is public unless it says so,
  2154. # and a sidecar full of x-tcms-private:false reads like the opposite.
  2155. $property->{'x-tcms-private'} = JSON::MaybeXS::true() if $field->{private};
  2156. # Likewise unindexed unless asked for. The sidecar is the declaration;
  2157. # post_wizard_save() reconciles the data model against it.
  2158. $property->{'x-tcms-indexed'} = JSON::MaybeXS::true() if $field->{indexed};
  2159. if ( $field->{type} eq 'relation' ) {
  2160. $property->{'x-tcms-relation-form'} = $field->{relation_form};
  2161. # The stored field holds a UUID; the resolved post lands beside it.
  2162. $relations{"$field->{name}_post"} = { form => $field->{relation_form}, from => $field->{name} };
  2163. }
  2164. $properties{ $field->{name} } = $property;
  2165. push( @required, $field->{name} ) if $field->{required};
  2166. }
  2167. my %spec = (
  2168. 'x-tcms-post-type' => {
  2169. name => $name,
  2170. generated => JSON::MaybeXS::true(),
  2171. generator => 'Trog::Routes::HTML::post_wizard_save',
  2172. body_form => $body_form,
  2173. includes => [ map { $wizard_includes{$_} } @$includes ],
  2174. },
  2175. type => 'object',
  2176. properties => \%properties,
  2177. );
  2178. $spec{required} = \@required if @required;
  2179. $spec{'x-tcms-relations'} = \%relations if %relations;
  2180. $spec{'x-tcms-datasource'} = $datasource if $datasource;
  2181. return \%spec;
  2182. }
  2183. sub _wizard_field_html ($field) {
  2184. my ( $n, $t, $l, $p ) = @$field{qw{name type label placeholder}};
  2185. my $required = $field->{required} ? 'required ' : '';
  2186. if ( $t eq 'relation' ) {
  2187. # 'all' relations aren't stored, so there is nothing to edit.
  2188. return '' if $field->{relation_mode} eq 'all';
  2189. # Left empty on purpose: post_relations.js fills it from
  2190. # /api/posts_of_form, so the picker can't go stale against the
  2191. # generated template.
  2192. return qq| $l<br /><select ${required}class="cooltext relation-picker" name="$n" data-relation-form="$field->{relation_form}" data-selected="<: \$post.$n :>"></select>\n|;
  2193. }
  2194. return qq| $l<br /><textarea ${required}class="cooltext" name="$n" placeholder="$p"><: \$post.$n :></textarea>\n|
  2195. if $t eq 'textarea';
  2196. return qq| <label for="<: \$post.id :>-$n">$l<input id="<: \$post.id :>-$n" class="coolcb" type="checkbox" name="$n" value="1" <: if ( \$post.$n ) { "checked" } :> /></label><br />\n|
  2197. if $t eq 'checkbox';
  2198. return qq| $l<br /><input ${required}class="cooltext" type="$t" name="$n" placeholder="$p" value="<: \$post.$n :>" />\n|;
  2199. }
  2200. =head2 _wizard_template
  2201. Assemble the actual .tx file, following the shape of the hand-written forms:
  2202. a display half guarded by !$post.addpost, and an edit half guarded by $can_edit.
  2203. =cut
  2204. sub _wizard_template ( $query, $fields, $includes, $body_form, $datasource = '' ) {
  2205. my $placeholder = _kolon_safe( $query->{title_placeholder} ) || 'Iowa Man Destroys Moon';
  2206. # NOT escaped, on purpose. Letting an admin write template code is the
  2207. # entire point of the wizard, and it is no more privileged than the data
  2208. # field of any post, which already gets run through render_it(). Both
  2209. # sides of this route are behind the admin ACL.
  2210. my $display = _wizard_scalar( $query->{display} );
  2211. my $out = "<!-- Generated by the tCMS Post Type Wizard. Regenerating this type will overwrite any hand edits. -->\n";
  2212. # A series can ask for its children to be tiled, which is a layout the
  2213. # wrapper has to carry -- see how file.tx and series.tx do it. Harmless
  2214. # when the series hasn't asked, and it means a generated type gets tiling
  2215. # for free rather than being the one kind of post that ignores the setting.
  2216. $out .= qq|<div class="post <: \$style :> <: \$tiled ? 'tile' : '' :>">\n| if $query->{wrapper};
  2217. $out .= qq| : if ( !\$post.addpost ) {\n|;
  2218. $out .= qq| : include "post_title.tx";\n| if $query->{inc_post_title};
  2219. $out .= qq| : include "post_tags.tx";\n| if $query->{inc_post_tags};
  2220. $out .= "$display\n" if $display;
  2221. $out .= qq| : }\n|;
  2222. # A datasource can say its posts are built rather than written, in which
  2223. # case an editor would be a form saving something nobody can edit -- and
  2224. # for Trog::DataSource::Virt, one that writes a real post to sit alongside
  2225. # the synthesized ones and collide with them.
  2226. if ( !_datasource_editable($datasource) ) {
  2227. $out .= qq|</div>\n| if $query->{wrapper};
  2228. return $out;
  2229. }
  2230. $out .= qq|\n|;
  2231. $out .= qq| : if ( \$can_edit ) {\n|;
  2232. $out .= qq| <div class="postedit">\n|;
  2233. $out .= qq| : include "edit_head.tx";\n|;
  2234. $out .= qq| <form class="Submissions" action="/post/save" method="POST" enctype="multipart/form-data">\n|;
  2235. $out .= qq| Title *<br /><input required class="cooltext" type="text" name="title" placeholder="$placeholder" value="<: \$post.title :>" />\n|
  2236. if $query->{inc_title_input};
  2237. $out .= _wizard_field_html($_) foreach @$fields;
  2238. foreach my $include (@$includes) {
  2239. $out .= qq| : include "$wizard_includes{$include}";\n|;
  2240. }
  2241. # Always last, and never optional: this is what carries the post body, the
  2242. # app/to/id/form hiddens and the submit button.
  2243. $out .= qq| : include "$body_form";\n|;
  2244. $out .= qq| </form>\n|;
  2245. $out .= qq| : include "edit_foot.tx";\n|;
  2246. $out .= qq| </div>\n|;
  2247. $out .= qq| : }\n|;
  2248. $out .= qq|</div>\n| if $query->{wrapper};
  2249. return $out;
  2250. }
  2251. # basically a file rewrite rule for themes
  2252. =head2 icon
  2253. Implements the /img/icon/* routes.
  2254. Serves a themed icon, falling back to the stock one. A rewrite rule with extra
  2255. steps, essentially, so that themes can replace individual icons.
  2256. =cut
  2257. sub icon ($query) {
  2258. my $path = $query->{route};
  2259. my $tpath = Trog::Themes::themed("img/icon/$path");
  2260. return $query->{tpsgi}->serve( $path, $tpath, $query->{start}, $query->{streaming}, $query->{ranges}, $query->{last_fetched}, $query->{deflate} );
  2261. }
  2262. =head2 totp_qr
  2263. Implements the /totp/* routes.
  2264. Serves the QR code generated when a user enables TOTP. These live outside
  2265. www/, so they go out through serve() rather than the static file handler --
  2266. handing someone else's enrolment QR out to the world would be handing them the
  2267. shared secret.
  2268. =cut
  2269. sub totp_qr ($query) {
  2270. my $fname = basename( $query->{route} );
  2271. # For authenticated content, we have to now do a serve().
  2272. return $query->{tpsgi}->serve(
  2273. "totp/$fname",
  2274. "totp/$fname",
  2275. $query->{start},
  2276. $query->{streaming},
  2277. $query->{ranges},
  2278. $query->{last_fetched},
  2279. $query->{deflate},
  2280. );
  2281. }
  2282. =head2 rss_style
  2283. Implements /styles/rss-style.xsl.
  2284. The XSL stylesheet which makes the RSS feed legible in a browser.
  2285. rss-style.tx pulls the header and footer in with component(), which works here
  2286. where an include directive would not: the output is XSL rather than HTML, so
  2287. the two are rendered separately and composed, and the header is asked for
  2288. without a doctype.
  2289. =cut
  2290. sub rss_style ($query) {
  2291. $query->{port} = ":$query->{port}" if $query->{port};
  2292. $query->{title} = qq{<xsl:value-of select="rss/channel/title"/>};
  2293. $query->{no_doctype} = 1;
  2294. return Trog::Renderer->render(
  2295. template => 'rss-style.tx',
  2296. contenttype => 'text/xsl',
  2297. data => $query,
  2298. code => 200,
  2299. );
  2300. }
  2301. sub _build_themed_styles ($styles) {
  2302. my @styles = map { ( Trog::Themes::themed_style("$_") ) } @{ Trog::Utils::coerce_array($styles) };
  2303. return \@styles;
  2304. }
  2305. sub _build_themed_scripts ($scripts) {
  2306. my @scripts = map { Trog::Themes::themed_script("$_") } @{ Trog::Utils::coerce_array($scripts) };
  2307. return \@scripts;
  2308. }
  2309. =head2 finish_render($template, $vars, %headers)
  2310. Render a full page, as opposed to a component.
  2311. Fills in the defaults every page needs (lang, title, status code, content type,
  2312. cache control), resolves stylesheet and script names through the theme, makes
  2313. their paths absolute, and hands the lot to Trog::Renderer.
  2314. The page's own template asks for the header and footer with component(), so the
  2315. resolved stylesheet and script lists are passed on to Trog::Component::Header
  2316. rather than being rendered into a string here.
  2317. Just about everything in this module ends up here; index() is the usual way in.
  2318. =cut
  2319. sub finish_render ( $template, $vars, %headers ) {
  2320. #XXX default vars that need to be pulled from config
  2321. $vars->{lang} //= 'en-US';
  2322. $vars->{title} //= 'tCMS';
  2323. $vars->{stylesheets} //= [];
  2324. $vars->{print_styles} //= [qw{structure.css print.css}];
  2325. $vars->{scripts} //= [];
  2326. # Theme-ize the paths
  2327. $vars->{stylesheets} = [ @{ _build_themed_styles( $vars->{stylesheets} ) } ];
  2328. $vars->{print_styles} = [ @{ _build_themed_styles( $vars->{print_styles} ) } ];
  2329. $vars->{scripts} = [ map { s/^www\///; $_ } @{ _build_themed_scripts( $vars->{scripts} ) } ];
  2330. # Add in avatars.css, it's special
  2331. push( @{ $vars->{stylesheets} }, "/styles/avatars.css" );
  2332. # Absolute-ize the paths for scripts & stylesheets
  2333. @{ $vars->{stylesheets} } = map { CORE::index( $_, '/' ) == 0 ? $_ : "/$_" } @{ $vars->{stylesheets} };
  2334. @{ $vars->{print_styles} } = map { CORE::index( $_, '/' ) == 0 ? $_ : "/$_" } @{ $vars->{print_styles} };
  2335. @{ $vars->{scripts} } = map { CORE::index( $_, '/' ) == 0 ? $_ : "/$_" } @{ $vars->{scripts} };
  2336. # TODO Smash together the stylesheets and minify
  2337. $vars->{contenttype} //= $Trog::Vars::content_types{html};
  2338. $vars->{cachecontrol} //= $Trog::Vars::cache_control{revalidate};
  2339. $vars->{code} ||= 200;
  2340. $vars->{theme_dir} =~ s/^\/www\/// if $vars->{theme_dir};
  2341. # %headers was accepted and then ignored, which nothing noticed because
  2342. # nothing passed any. index() passes one now, to expire the feedback cookie
  2343. # once its message has been rendered.
  2344. return Trog::Renderer->render(
  2345. template => $template,
  2346. data => $vars,
  2347. contenttype => 'text/html',
  2348. code => $vars->{code},
  2349. ( %headers ? ( headers => \%headers ) : () ),
  2350. );
  2351. }
  2352. 1;